statsUtils.lib.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This is the statistic utility functions library for Dokeos.
  22. * Include/require it in your code to use its functionality.
  23. *
  24. * @package dokeos.library
  25. ==============================================================================
  26. */
  27. /*
  28. ==============================================================================
  29. FUNCTIONS
  30. ==============================================================================
  31. */
  32. /**
  33. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  34. * @param sql : a sql query (as a string)
  35. * @desc return one result from a sql query (1 single result)
  36. */
  37. function getOneResult($sql) {
  38. $query = Database::query($sql);
  39. if ($query !== false) {
  40. $res = @Database::fetch_array($query, 'NUM');
  41. } else {
  42. $res = array();
  43. }
  44. return $res[0];
  45. }
  46. /**
  47. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  48. * @param sql : a sql query (as a string)
  49. * @desc Return many results of a query in a 1 column tab
  50. */
  51. function getManyResults1Col($sql) {
  52. $res = Database::query($sql);
  53. if ($res !== false) {
  54. $i = 0;
  55. while ($resA = Database::fetch_array($res, 'NUM')) {
  56. $resu[$i++] = $resA[0];
  57. }
  58. }
  59. return $resu;
  60. }
  61. /**
  62. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  63. * @param sql : a sql query (as a string)
  64. * @desc Return many results of a query
  65. */
  66. function getManyResults2Col($sql) {
  67. $res = Database::query($sql);
  68. if ($res !== false) {
  69. $i = 0;
  70. while ($resA = Database::fetch_array($res, 'NUM')) {
  71. $resu[$i][0] = $resA[0];
  72. $resu[$i][1] = $resA[1];
  73. $i++;
  74. }
  75. }
  76. return $resu;
  77. }
  78. /**
  79. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  80. * @param sql : a sql query (as a string)
  81. * @desc Return many results of a query in a 3 column tab
  82. in $resu[$i][0], $resu[$i][1],$resu[$i][2]
  83. */
  84. function getManyResults3Col($sql) {
  85. $res = Database::query($sql);
  86. if ($res !== false) {
  87. $i = 0;
  88. while ($resA = Database::fetch_array($res, 'NUM')) {
  89. $resu[$i][0] = $resA[0];
  90. $resu[$i][1] = $resA[1];
  91. $resu[$i][2] = $resA[2];
  92. $i++;
  93. }
  94. }
  95. return $resu;
  96. }
  97. /**
  98. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  99. * @param sql : a sql query (as a string)
  100. * @desc Return many results of a query in a X column tab
  101. in $resu[$i][0], $resu[$i][1],$resu[$i][2],...
  102. this function is more 'standard' but use a little
  103. more ressources
  104. So I encourage to use the dedicated for 1, 2 or 3
  105. columns of results
  106. */
  107. function getManyResultsXCol($sql, $X) {
  108. $res = Database::query($sql);
  109. if ($res !== false) {
  110. $i = 0;
  111. while ($resA = Database::fetch_array($res, 'NUM')) {
  112. for ($j = 0; $j < $X ; $j++) {
  113. $resu[$i][$j] = $resA[$j];
  114. }
  115. $i++;
  116. }
  117. }
  118. return $resu;
  119. }
  120. /**
  121. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  122. * @param sql : a sql query (as a string)
  123. * @return hours_array
  124. * @desc Return an assoc array. Keys are the hours, values are
  125. the number of time this hours was found.
  126. key 'total' return the sum of all number of time hours
  127. appear
  128. */
  129. function hoursTab($sql) {
  130. $hours_array = array('total' => 0);
  131. $res = Database::query($sql);
  132. if ($res !== false) {
  133. while ($row = Database::fetch_row($res)) {
  134. $date_array = getdate($row[0]);
  135. if ($date_array['hours'] == $last_hours) {
  136. $hours_array[$date_array['hours']]++;
  137. } else {
  138. $hours_array[$date_array['hours']] = 1;
  139. $last_hours = $date_array['hours'];
  140. }
  141. $hours_array['total']++;
  142. }
  143. Database::free_result($res);
  144. }
  145. return $hours_array;
  146. }
  147. /**
  148. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  149. * @param sql : a sql query (as a string)
  150. * @return days_array
  151. * @desc Return an assoc array. Keys are the days, values are
  152. the number of time this hours was found.
  153. key "total" return the sum of all number of time days
  154. appear
  155. */
  156. function daysTab($sql) {
  157. $MonthsShort = api_get_months_short();
  158. $days_array = array('total' => 0);
  159. $res = Database::query($sql);
  160. if ($res !== false) {
  161. while ($row = Database::fetch_row($res)) {
  162. $date_array = getdate($row[0]);
  163. $display_date = $date_array['mday'].' '.$MonthsShort[$date_array['mon'] - 1].' '.$date_array['year'];
  164. if ($date_array['mday'] == $last_day) {
  165. $days_array[$display_date]++;
  166. } else {
  167. $days_array[$display_date] = 1;
  168. $last_day = $display_date;
  169. }
  170. $days_array['total']++;
  171. }
  172. Database::free_result($res);
  173. }
  174. return $days_array;
  175. }
  176. /**
  177. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  178. * @param sql : a sql query (as a string)
  179. * @return month_array
  180. * @desc Return an assoc array. Keys are the days, values are
  181. the number of time this hours was found.
  182. key "total" return the sum of all number of time days
  183. appear
  184. */
  185. function monthTab($sql) {
  186. $MonthsLong = api_get_months_long();
  187. $month_array = array('total' => 0);
  188. $res = Database::query($sql);
  189. if ($res !== false) {
  190. // init tab with all months
  191. for($i = 0; $i < 12; $i++) {
  192. $month_array[$MonthsLong[$i]] = 0;
  193. }
  194. while ($row = Database::fetch_row($res)) {
  195. $date_array = getdate($row[0]);
  196. $month_array[$MonthsLong[$date_array['mon'] - 1]]++;
  197. $month_array['total']++;
  198. }
  199. Database::free_result($res);
  200. }
  201. return $month_array;
  202. }
  203. /**
  204. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  205. * @param period_array : an array provided by hoursTab($sql) or daysTab($sql)
  206. * @param periodTitle : title of the first column, type of period
  207. * @param linkOnPeriod :
  208. * @desc Display a 4 column array
  209. Columns are : hour of day, graph, number of hits and %
  210. First line are titles
  211. next are informations
  212. Last is total number of hits
  213. */
  214. function makeHitsTable($period_array, $periodTitle, $linkOnPeriod = '???') {
  215. echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' align=center class='minitext'>";
  216. // titles
  217. echo "<tr bgcolor='#E6E6E6' align='center'>
  218. <td width='15%' >
  219. <b>$periodTitle</b>
  220. </td>
  221. <td width='60%'>
  222. &nbsp;
  223. </td>
  224. <td width='10%'>
  225. <b>".get_lang('Hits')."</b>
  226. </td>
  227. <td width='15%'>
  228. <b>%</b>
  229. </td>
  230. </tr>
  231. ";
  232. $factor = 4;
  233. $maxSize = $factor * 100; //pixels
  234. while (list($periodPiece, $cpt) = each($period_array)) {
  235. if ($periodPiece != 'total') {
  236. $pourcent = round(100 * $cpt / $period_array['total']);
  237. $barwidth = $factor * $pourcent ;
  238. echo "<tr>
  239. <td align='center' width='15%'>";
  240. echo $periodPiece;
  241. echo "</td>
  242. <td width='60%' style='padding-top: 3px;' align='center'>"
  243. // display hitbar
  244. ."<img src='../img/bar_1.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  245. if($pourcent != 0)
  246. echo "<img src='../img/bar_1u.gif' width='$barwidth' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  247. // display 100% bar
  248. if($pourcent != 100 && $pourcent != 0)
  249. echo "<img src='../img/bar_1m.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  250. if($pourcent != 100)
  251. echo "<img src='../img/bar_1r.gif' width='".($maxSize-$barwidth)."' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  252. echo "<img src='../img/bar_1.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />
  253. </td>
  254. <td align='center' width='10%'>
  255. $cpt
  256. </td>
  257. <td align='center' width='15%'>
  258. $pourcent %
  259. </td>
  260. </tr>
  261. ";
  262. }
  263. }
  264. echo "<tr bgcolor='#E6E6E6'>
  265. <td width='15%' align='center'>
  266. ".get_lang('Total')."
  267. </td>
  268. <td align='right' width='60%'>
  269. &nbsp;
  270. </td>
  271. <td align='center' width='10%'>
  272. ".$period_array['total']."
  273. </td>
  274. <td width='15%'>
  275. &nbsp;
  276. </td>
  277. </tr>
  278. ";
  279. echo "</table>";
  280. }
  281. /**
  282. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  283. * @param array_of_results : a 2 columns array
  284. * @param title1 : string, title of the first column
  285. * @param title2 : string, title of the ... second column
  286. * @desc display a 2 column tab from an array
  287. titles of columns are title1 and title2
  288. */
  289. function buildTab2col($array_of_results, $title1, $title2) {
  290. echo "<table cellpadding='2' cellspacing='1' border='1' align='center'>\n";
  291. echo "<tr>
  292. <td bgcolor='#E6E6E6'>
  293. $title1
  294. </td>
  295. <td bgcolor='#E6E6E6'>
  296. $title2
  297. </td>
  298. </tr>\n";
  299. if (is_array($array_of_results)) {
  300. for ($j = 0 ; $j < count($array_of_results) ; $j++) {
  301. echo '<tr>';
  302. echo '<td bgcolor="#eeeeee">'.$array_of_results[$j][0].'</td>';
  303. echo '<td align="right">'.$array_of_results[$j][1].'</td>';
  304. echo "</tr>\n";
  305. }
  306. } else {
  307. echo '<tr>';
  308. echo '<td colspan="2" align="center">'.get_lang('NoResult').'</td>';
  309. echo "</tr>\n";
  310. }
  311. echo "</table>\n";
  312. }
  313. /**
  314. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  315. * @param array_of_results : a 2 columns array
  316. * @desc display a 2 column tab from an array
  317. this tab has no title
  318. */
  319. function buildTab2ColNoTitle($array_of_results) {
  320. echo "<table cellpadding='3' cellspacing='1' border='0' align='center'>\n";
  321. if (is_array($array_of_results)) {
  322. for ($j = 0 ; $j < count($array_of_results) ; $j++) {
  323. echo '<tr>';
  324. echo '<td bgcolor="#eeeeee">'.$array_of_results[$j][0].'</td>';
  325. echo '<td align="right">&nbsp;&nbsp;'.$array_of_results[$j][1].'</td>';
  326. echo "</tr>\n";
  327. }
  328. } else {
  329. echo '<tr>';
  330. echo '<td colspan="2" align="center">'.get_lang('NoResult').'</td>';
  331. echo "</tr>\n";
  332. }
  333. echo "</table>\n";
  334. }
  335. /**
  336. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  337. * @param array_of_results : a 2 columns array
  338. * @desc this function is used to display
  339. integrity errors in the platform
  340. if array_of_results is not an array there is
  341. no error, else errors are displayed
  342. */
  343. function buildTabDefcon($array_of_results) {
  344. echo "<table width='60%' cellpadding='2' cellspacing='1' border='0' align=center class='minitext'>\n";
  345. if (is_array($array_of_results)) {
  346. // there are some strange cases...
  347. echo '<tr>';
  348. echo '<td colspan="2" align="center" bgcolor="#eeeeee"><font color="#ff0000">'.get_lang('Defcon').'</font></td>';
  349. echo "</tr>\n";
  350. for ($j = 0 ; $j < count($array_of_results) ; $j++) {
  351. if($array_of_results[$j][0] == "") {
  352. $key = get_lang('NULLValue');
  353. } else {
  354. $key = $array_of_results[$j][0];
  355. }
  356. echo '<tr>';
  357. echo '<td width="70%" class="content">'.$key.'</td>';
  358. echo '<td width="30%" align="right">'.$array_of_results[$j][1].'</td>';
  359. echo "</tr>\n";
  360. }
  361. } else {
  362. // all right
  363. echo '<tr>';
  364. echo '<td colspan="2" align="center"><font color="#00ff00">'.get_lang('AllRight').'</font></td>';
  365. echo "</tr>\n";
  366. }
  367. echo "</table>\n";
  368. }