* @copyrights CBLUE SPRL 2011 * @package chamilo.reports */ //exit; $cidReset = true; // including files require_once __DIR__.'/../inc/global.inc.php'; require_once 'reports.lib.php'; require_once 'multiquery.lib.php'; // protect script api_block_anonymous_users(); // defining constants // current section $this_section = SECTION_REPORTS; // setting the name of the tool $tool_name = get_lang('Reports'); // loading templates reports_loadTemplates(); // random suffix for div id (to enable multiple report per page) $idsuffix = rand(); // "Link" type if ($_REQUEST['format'] == 'link') { // converting post vars to get uri $params = ''; $kv = array(); foreach ($_POST as $key => $value) { if ($key != 'format') { $kv[] = $key.'='.urlencode($value); } } $query_string = join("&", $kv); die(''.get_lang('ReportTypeLink').''); } if ($_REQUEST['format'] == 'directlink') { foreach (array('jquery.dataTables.min.js') as $js) { $htmlHeadXtra[] = ''."\n"; } $htmlCSSXtra[] = 'dataTable.css'; ?> '; } // outputing a link to csv file instead of outputing csv data directly if ($_REQUEST['format'] == 'csv') { // converting post vars to get uri $params = ''; $kv = array(); foreach ($_POST as $key => $value) { if ($key != 'format') { $kv[] = $key.'='.urlencode($value); } } $query_string = join("&", $kv); die(''.get_lang('DownloadFile').''); } else { if ($_REQUEST['format'] == 'downloadcsv') { if ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') == false) ) { header("Pragma: must-revalidate"); header("Cache-Control: must-revalidate"); header("Content-type: application/vnd.ms-excel"); } else { header("Content-type: text/csv"); } $date = date("Y-m-d"); $filename = "reporting-$date.csv"; header("Content-Disposition: attachment; filename=$filename"); $_REQUEST['format'] = 'csv'; } } if (is_array($reports_template[$_REQUEST['type']])) { $query = $reports_template[$_REQUEST['type']]['getSQL'](); if (!is_array($query)) { $query = array($query); } if ($_REQUEST['format'] == 'sql') { die(var_export($query, true)); } $result = multiquery_query($query); // check number of result $numberOfResult = multiquery_num_rows($result); if ($numberOfResult == 0) { // This case should be taken care of by the display template itself, we should let the script run //die(get_lang('NoDataAvailable')); } } else { die(''.get_lang('ErrorWhileBuildingReport').''); } if ($_REQUEST['format'] == 'html' || $_REQUEST['format'] == 'directlink') { if (isset($reports_template[$_REQUEST['type']]['html_header'])) { echo $reports_template[$_REQUEST['type']]['html_header']; } echo ''; echo ''; // FIXME style // counting fields $nfields = multiquery_num_fields($result); $columns = array(); $columns_islink = array(); echo ''; for ($i = 0; $i < $nfields; $i++) { $columns[$i] = multiquery_field_name($result, $i); if (substr($columns[$i], -5, 5) != '_link') { $column_islink[$i] = false; echo ''; } else { $columns_islink[$i] = true; } } // checking resolving link column id $columns_flip = array_flip($columns); $columns_link = array(); for ($i = 0; $i < $nfields; $i++) { if ($column_islink[$i] == false && array_key_exists($columns[$i].'_link', $columns_flip)) { $columns_link[$i] = $columns_flip[$columns[$i].'_link']; } else { $columns_link[$i] = ''; } } echo ''; while ($row = multiquery_fetch_row($result)) { echo ''; for ($i = 0; $i < $nfields; $i++) { if (!$columns_islink[$i]) { // ignore links if ($columns_link[$i] != '') // link is defined { if (substr($columns_link[$i], 0, 10) == 'javascript') { echo ''; } else { echo ''; } } else { echo ''; } } } echo "\n"; } echo '
'.$columns[$i].'
'.$row[$i].''.$row[$i].''.$row[$i].'
'; if ($_REQUEST['format'] == 'directlink') { echo '
'; Display::display_footer(); } } else { if ($_REQUEST['format'] == 'csv') { $nfields = multiquery_num_fields($result); $columns = array(); $columns_islink = array(); for ($i = 0; $i < $nfields; $i++) { $columns[$i] = multiquery_field_name($result, $i); if (substr($columns[$i], -5, 5) != '_link') { $column_islink[$i] = false; echo csv_escaping($columns[$i]).','; } else { $columns_islink[$i] = true; } } echo "\n"; while ($row = multiquery_fetch_row($result)) { for ($i = 0; $i < $nfields; $i++) { if (!$columns_islink[$i]) // ignore links { echo csv_escaping($row[$i]).','; } } // fixme echo "\n"; } } else { die(get_lang('UnknownFormat')); } } function csv_escaping($value, $csv_separator = ',') { $value = str_replace('"', '""', $value); if (strpos($value, '""') or strpos($value, $csv_separator) or $value != trim($value)) { $value = '"'.$value.'"'; } return $value; } ?>