reports.php 7.7 KB

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