reports.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Reports
  5. * @author Arnaud Ligot <arnaud@cblue.be>
  6. * @copyrights CBLUE SPRL 2011
  7. * @package chamilo.reports
  8. */
  9. exit;
  10. // name of the language file that needs to be included
  11. $cidReset = true;
  12. // including files
  13. require_once '../inc/global.inc.php';
  14. require_once 'reports.lib.php';
  15. require_once 'multiquery.lib.php';
  16. // protect script
  17. api_block_anonymous_users();
  18. // defining constants
  19. // current section
  20. $this_section = SECTION_REPORTS;
  21. // setting the name of the tool
  22. $tool_name=get_lang('Reports');
  23. // loading templates
  24. reports_loadTemplates();
  25. // random suffix for div id (to enable multiple report per page)
  26. $idsuffix = rand();
  27. // "Link" type
  28. if ($_REQUEST['format'] == 'link') {
  29. // converting post vars to get uri
  30. $params = '';
  31. $kv = array();
  32. foreach ($_POST as $key => $value)
  33. if ($key != 'format')
  34. $kv[] = $key.'='.urlencode($value);
  35. $query_string = join("&", $kv);
  36. die('<a href="reports.php?format=directlink&'.$query_string.'">'.get_lang('ReportTypeLink').'</a>');
  37. }
  38. if ($_REQUEST['format'] == 'directlink') {
  39. foreach (array('jquery.dataTables.min.js') as $js)
  40. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/'.$js.'" type="text/javascript" language="javascript"></script>'."\n";
  41. $htmlCSSXtra[] = 'dataTable.css';
  42. ?>
  43. <script type="text/javascript">
  44. function setSubDataUri(elem, uri) {
  45. $.ajax({
  46. url: uri,
  47. success: function(data) {
  48. $(elem).closest('.result').nextAll().html('');
  49. $(elem).closest('.result').next().html(data);
  50. }
  51. });
  52. }
  53. </script>
  54. <?php
  55. Display::display_header($tool_name);
  56. echo '<div id="result" class="result">';
  57. }
  58. // outputing a link to csv file instead of outputing csv data directly
  59. if ($_REQUEST['format'] == 'csv') {
  60. // converting post vars to get uri
  61. $params = '';
  62. $kv = array();
  63. foreach ($_POST as $key => $value)
  64. if ($key != 'format')
  65. $kv[] = $key.'='.urlencode($value);
  66. $query_string = join("&", $kv);
  67. die('<a href="reports.php?format=downloadcsv&'.$query_string.'">'.get_lang('DownloadFile').'</a>');
  68. } else if ($_REQUEST['format'] == 'downloadcsv') {
  69. if ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') == false)) {
  70. header("Pragma: must-revalidate");
  71. header("Cache-Control: must-revalidate");
  72. header("Content-type: application/vnd.ms-excel");
  73. }
  74. else {
  75. header("Content-type: text/csv");
  76. }
  77. $date = date("Y-m-d");
  78. $filename = "reporting-$date.csv";
  79. header("Content-Disposition: attachment; filename=$filename");
  80. $_REQUEST['format'] = 'csv';
  81. }
  82. if (is_array($reports_template[$_REQUEST['type']])) {
  83. $query = $reports_template[$_REQUEST['type']]['getSQL']();
  84. if (! is_array($query))
  85. $query = array($query);
  86. if ($_REQUEST['format'] == 'sql')
  87. die(var_export($query, true));
  88. $result = multiquery_query($query);
  89. // check number of result
  90. $numberOfResult = multiquery_num_rows($result);
  91. if ($numberOfResult == 0) {
  92. // This case should be taken care of by the display template itself, we should let the script run
  93. //die(get_lang('NoDataAvailable'));
  94. }
  95. } else {
  96. die('<b>'.get_lang('ErrorWhileBuildingReport').'</b>');
  97. }
  98. if ($_REQUEST['format'] == 'html' || $_REQUEST['format'] == 'directlink') {
  99. if (isset($reports_template[$_REQUEST['type']]['html_header'])) {
  100. echo $reports_template[$_REQUEST['type']]['html_header'];
  101. }
  102. echo '<script type="text/javascript" charset="utf-8">
  103. $(document).ready(function() {
  104. $("#reportsData'.$idsuffix.'").dataTable({
  105. "oLanguage":
  106. {
  107. "sProcessing": "Traitement en cours...",
  108. "sLengthMenu": "Afficher _MENU_ éléments",
  109. "sZeroRecords": "Aucun élément à afficher",
  110. "sInfo": "Affichage de l'."'".'élement _START_ à _END_ sur _TOTAL_ éléments",
  111. "sInfoEmpty": "Affichage de l'."'".'élement 0 à 0 sur 0 éléments",
  112. "sInfoFiltered": "(filtré de _MAX_ éléments au total)",
  113. "sInfoPostFix": "",
  114. "sSearch": "Rechercher :",
  115. "sUrl": "",
  116. "oPaginate": {
  117. "sFirst": "Premier",
  118. "sPrevious": "Précédent",
  119. "sNext": "Suivant",
  120. "sLast": "Dernier"
  121. }
  122. }
  123. });
  124. } );
  125. </script>';
  126. echo '<table id="reportsData'.$idsuffix.'" class="display">'; // FIXME style
  127. // counting fields
  128. $nfields = multiquery_num_fields($result);
  129. $columns = array();
  130. $columns_islink = array();
  131. echo '<thead><tr>';
  132. for ($i=0; $i < $nfields; $i++) {
  133. $columns[$i] = multiquery_field_name($result, $i);
  134. if (substr($columns[$i], -5, 5) != '_link') {
  135. $column_islink[$i] = false;
  136. echo '<th>'.$columns[$i].'</th>';
  137. } else
  138. $columns_islink[$i] = true;
  139. }
  140. // checking resolving link column id
  141. $columns_flip = array_flip($columns);
  142. $columns_link = array();
  143. for ($i=0; $i < $nfields; $i++)
  144. if ($column_islink[$i] == false && array_key_exists($columns[$i].'_link', $columns_flip))
  145. $columns_link[$i] = $columns_flip[$columns[$i].'_link'];
  146. else
  147. $columns_link[$i] = '';
  148. echo '</tr></thead><tbody>';
  149. while ($row = multiquery_fetch_row($result)) {
  150. echo '<tr>';
  151. for ($i = 0; $i<$nfields; $i++)
  152. if (!$columns_islink[$i]){ // ignore links
  153. if ($columns_link[$i] != '') // link is defined
  154. if (substr($columns_link[$i],0,10) == 'javascript') {
  155. echo '<td><a href="#" onclick="'.$row[$columns_link[$i]].'">'.$row[$i].'</a></td>';
  156. }
  157. else {
  158. echo '<td><a href="'.$row[$columns_link[$i]].'">'.$row[$i].'</a></td>';
  159. }
  160. else
  161. echo '<td>'.$row[$i].'</td>';
  162. }
  163. echo "</tr>\n";
  164. }
  165. echo '</tbody></table>';
  166. if ($_REQUEST['format'] == 'directlink') {
  167. echo '</div>
  168. <div id="result2" class="result" style="margin: 50px;">
  169. </div>
  170. <div id="result3" class="result" style="margin: 100px;">
  171. </div>
  172. <div id="result4" class="result" style="margin: 150px;">
  173. </div>';
  174. Display::display_footer();
  175. }
  176. } else if ($_REQUEST['format'] == 'csv') {
  177. $nfields = multiquery_num_fields($result);
  178. $columns = array();
  179. $columns_islink = array();
  180. for ($i=0; $i < $nfields; $i++) {
  181. $columns[$i] = multiquery_field_name($result, $i);
  182. if (substr($columns[$i], -5, 5) != '_link') {
  183. $column_islink[$i] = false;
  184. echo csv_escaping($columns[$i]).',';
  185. } else
  186. $columns_islink[$i] = true;
  187. }
  188. echo "\n";
  189. while ($row = multiquery_fetch_row($result)) {
  190. for ($i = 0; $i<$nfields; $i++)
  191. if (!$columns_islink[$i]) // ignore links
  192. echo csv_escaping($row[$i]).','; // fixme
  193. echo "\n";
  194. }
  195. } else die(get_lang('UnknownFormat'));
  196. function csv_escaping($value, $csv_separator = ',') {
  197. $value = str_replace('"','""',$value);
  198. if (strpos($value, '""') or strpos($value, $csv_separator) or $value != trim($value) ) {
  199. $value = '"'.$value.'"';
  200. }
  201. return $value;
  202. }
  203. ?>