reports.php 6.7 KB

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