reports.php 6.6 KB

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