results = $return; return true; } /** * Exports the complete report as a CSV file * @param string Document path inside the document tool * @param integer Optional user ID * @param boolean Whether to include user fields or not * @return boolean False on error */ public function exportCompleteReportCSV($document_path='', $hotpotato_name) { global $charset; $this->_getExercisesReporting($document_path, $hotpotato_name); $filename = 'exercise_results_'.date('YmdGis').'.csv'; if(!empty($user_id)) { $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv'; } $data = ''; if (api_is_western_name_order()) { if(!empty($this->results[0]['first_name'])) { $data .= get_lang('FirstName').';'; } if(!empty($this->results[0]['last_name'])) { $data .= get_lang('LastName').';'; } } else { if(!empty($this->results[0]['last_name'])) { $data .= get_lang('LastName').';'; } if(!empty($this->results[0]['first_name'])) { $data .= get_lang('FirstName').';'; } } $data .= get_lang('Email').';'; if ($export_user_fields) { //show user fields section with a big th colspan that spans over all fields $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1); $num = count($extra_user_fields); foreach($extra_user_fields as $field) { $data .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";'; } } $data .= get_lang('Title').';'; $data .= get_lang('StartDate').';'; $data .= get_lang('Score').';'; $data .= get_lang('Total').';'; $data .= "\n"; //results foreach($this->results as $row) { if (api_is_western_name_order()) { $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';'; $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';'; } else { $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';'; $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';'; } $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';'; if ($export_user_fields) { //show user fields data, if any, for this user $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true); foreach($user_fields_values as $value) { $data .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";'; } } $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';'; $data .= str_replace("\r\n",' ',$row['exe_date']).';'; $data .= str_replace("\r\n",' ',$row['result']).';'; $data .= str_replace("\r\n",' ',$row['max']).';'; $data .= "\n"; } //output the results $len = strlen($data); header('Content-type: application/octet-stream'); header('Content-Type: application/force-download'); header('Content-length: '.$len); if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) { header('Content-Disposition: filename= '.$filename); } else { header('Content-Disposition: attachment; filename= '.$filename); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { header('Pragma: '); header('Cache-Control: '); header('Cache-Control: public'); // IE cannot download from sessions without a cache } header('Content-Description: '.$filename); header('Content-transfer-encoding: binary'); // @todo add this utf-8 header for all csv files echo "\xEF\xBB\xBF"; // force utf-8 header of csv file echo $data; return true; } /** * Exports the complete report as an XLS file * @return boolean False on error */ public function exportCompleteReportXLS($document_path='',$user_id = null, $export_user_fields= false, $export_filter = 0, $exercise_id=0, $hotpotato_name = null) { global $charset; $this->_getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name); $filename = 'exercise_results_'.date('YmdGis').'.xls'; if (!empty($user_id)) { $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls'; } $workbook = new Spreadsheet_Excel_Writer(); $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH)); $workbook->setVersion(8); // BIFF8 $workbook->send($filename); $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis')); $worksheet->setInputEncoding(api_get_system_encoding()); $line = 0; $column = 0; //skip the first column (row titles) // check if exists column 'user' $with_column_user = false; foreach ($this->results as $result) { if (!empty($result['last_name']) && !empty($result['first_name'])) { $with_column_user = true; break; } } if ($with_column_user) { $worksheet->write($line,$column,get_lang('Email')); $column++; if (api_is_western_name_order()) { $worksheet->write($line,$column,get_lang('FirstName')); $column++; $worksheet->write($line,$column,get_lang('LastName')); $column++; } else { $worksheet->write($line,$column,get_lang('LastName')); $column++; $worksheet->write($line,$column,get_lang('FirstName')); $column++; } } if ($export_user_fields) { //show user fields section with a big th colspan that spans over all fields $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1); //show the fields names for user fields foreach ($extra_user_fields as $field) { $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)); $column++; } } $worksheet->write($line,$column, get_lang('Title')); $column++; $worksheet->write($line,$column, get_lang('StartDate')); $column++; $worksheet->write($line,$column, get_lang('EndDate')); $column++; $worksheet->write($line,$column, get_lang('Duration').' ('.get_lang('MinMinutes').')'); $column++; $worksheet->write($line,$column, get_lang('Score')); $column++; $worksheet->write($line,$column, get_lang('Total')); $column++; $worksheet->write($line,$column, get_lang('Status')); $line++; foreach ($this->results as $row) { $column = 0; if ($with_column_user) { $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)); $column++; if (api_is_western_name_order()) { $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)); $column++; $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)); $column++; } else { $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)); $column++; $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)); $column++; } } if ($export_user_fields) { //show user fields data, if any, for this user $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true); foreach($user_fields_values as $value) { $worksheet->write($line,$column, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)); $column++; } } $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)); $column++; $worksheet->write($line,$column,$row['start_date']); $column++; $worksheet->write($line,$column,$row['end_date']); $column++; $worksheet->write($line,$column,$row['duration']); $column++; $worksheet->write($line,$column,$row['result']); $column++; $worksheet->write($line,$column,$row['max']); $column++; $worksheet->write($line,$column,$row['status']); $line++; } //output the results $workbook->close(); return true; } }