statsUtils.lib.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. {
  39. $query = @mysql_query($sql);
  40. if (mysql_errno())
  41. {
  42. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  43. }
  44. $res = @mysql_fetch_array($query);
  45. return $res[0];
  46. }
  47. /**
  48. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  49. * @param sql : a sql query (as a string)
  50. * @desc Return many results of a query in a 1 column tab
  51. */
  52. function getManyResults1Col($sql)
  53. {
  54. $res = mysql_query($sql);
  55. if (mysql_errno())
  56. {
  57. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  58. }
  59. else
  60. {
  61. $i = 0;
  62. while ($resA = mysql_fetch_array($res))
  63. {
  64. $resu[$i++] = $resA[0];
  65. }
  66. }
  67. return $resu;
  68. }
  69. /**
  70. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  71. * @param sql : a sql query (as a string)
  72. * @desc Return many results of a query
  73. */
  74. function getManyResults2Col($sql)
  75. {
  76. $res = mysql_query($sql);
  77. if (mysql_errno())
  78. {
  79. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  80. }
  81. else
  82. {
  83. $i = 0;
  84. while ($resA = mysql_fetch_array($res))
  85. {
  86. $resu[$i][0] = $resA[0];
  87. $resu[$i][1] = $resA[1];
  88. $i++;
  89. }
  90. }
  91. return $resu;
  92. }
  93. /**
  94. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  95. * @param sql : a sql query (as a string)
  96. * @desc Return many results of a query in a 3 column tab
  97. in $resu[$i][0], $resu[$i][1],$resu[$i][2]
  98. */
  99. function getManyResults3Col($sql)
  100. {
  101. $res = mysql_query($sql);
  102. if (mysql_errno())
  103. {
  104. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  105. }
  106. else
  107. {
  108. $i = 0;
  109. while ($resA = mysql_fetch_array($res))
  110. {
  111. $resu[$i][0]=$resA[0];
  112. $resu[$i][1]=$resA[1];
  113. $resu[$i][2]=$resA[2];
  114. $i++;
  115. }
  116. }
  117. return $resu;
  118. }
  119. /**
  120. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  121. * @param sql : a sql query (as a string)
  122. * @desc Return many results of a query in a X column tab
  123. in $resu[$i][0], $resu[$i][1],$resu[$i][2],...
  124. this function is more 'standard' but use a little
  125. more ressources
  126. So I encourage to use the dedicated for 1, 2 or 3
  127. columns of results
  128. */
  129. function getManyResultsXCol($sql,$X)
  130. {
  131. $res = mysql_query($sql);
  132. if (mysql_errno())
  133. {
  134. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  135. }
  136. else
  137. {
  138. $i = 0;
  139. while ($resA = mysql_fetch_array($res))
  140. {
  141. for($j = 0; $j < $X ; $j++)
  142. {
  143. $resu[$i][$j]=$resA[$j];
  144. }
  145. $i++;
  146. }
  147. }
  148. return $resu;
  149. }
  150. /**
  151. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  152. * @param sql : a sql query (as a string)
  153. * @return hours_array
  154. * @desc Return an assoc array. Keys are the hours, values are
  155. the number of time this hours was found.
  156. key 'total' return the sum of all number of time hours
  157. appear
  158. */
  159. function hoursTab($sql)
  160. {
  161. $hours_array = array('total' => 0);
  162. $res = mysql_query($sql);
  163. if (mysql_errno())
  164. {
  165. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  166. }
  167. else
  168. {
  169. while($row = mysql_fetch_row($res))
  170. {
  171. $date_array = getdate($row[0]);
  172. if($date_array['hours'] == $last_hours)
  173. {
  174. $hours_array[$date_array['hours']]++;
  175. }
  176. else
  177. {
  178. $hours_array[$date_array['hours']] = 1;
  179. $last_hours = $date_array['hours'];
  180. }
  181. $hours_array['total']++;
  182. }
  183. mysql_free_result($res);
  184. }
  185. return $hours_array;
  186. }
  187. /**
  188. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  189. * @param sql : a sql query (as a string)
  190. * @return days_array
  191. * @desc Return an assoc array. Keys are the days, values are
  192. the number of time this hours was found.
  193. key "total" return the sum of all number of time days
  194. appear
  195. */
  196. function daysTab($sql)
  197. {
  198. $MonthsShort = array(get_lang('JanuaryShort'), get_lang('FebruaryShort'), get_lang('MarchShort'), get_lang('AprilShort'), get_lang('MayShort'), get_lang('JuneShort'), get_lang('JulyShort'), get_lang('AugustShort'), get_lang('SeptemberShort'), get_lang('OctoberShort'), get_lang('NovemberShort'), get_lang('DecemberShort'));
  199. $days_array = array('total' => 0);
  200. $res = mysql_query($sql);
  201. if (mysql_errno())
  202. {
  203. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  204. }
  205. else
  206. {
  207. while($row = mysql_fetch_row($res))
  208. {
  209. $date_array = getdate($row[0]);
  210. $display_date = $date_array['mday'].' '.$MonthsShort[$date_array['mon']-1].' '.$date_array['year'];
  211. if ($date_array['mday'] == $last_day)
  212. {
  213. $days_array[$display_date]++;
  214. }
  215. else
  216. {
  217. $days_array[$display_date] = 1;
  218. $last_day = $display_date;
  219. }
  220. $days_array['total']++;
  221. }
  222. mysql_free_result($res);
  223. }
  224. return $days_array;
  225. }
  226. /**
  227. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  228. * @param sql : a sql query (as a string)
  229. * @return month_array
  230. * @desc Return an assoc array. Keys are the days, values are
  231. the number of time this hours was found.
  232. key "total" return the sum of all number of time days
  233. appear
  234. */
  235. function monthTab($sql)
  236. {
  237. $MonthsLong = array (get_lang('JanuaryLong'), get_lang('FebruaryLong'), get_lang('MarchLong'), get_lang('AprilLong'), get_lang('MayLong'), get_lang('JuneLong'), get_lang('JulyLong'), get_lang('AugustLong'), get_lang('SeptemberLong'), get_lang('OctoberLong'), get_lang('NovemberLong'), get_lang('DecemberLong'));
  238. $month_array = array('total' => 0);
  239. $res = mysql_query($sql);
  240. if (mysql_errno())
  241. {
  242. echo "\n<!-- **** ".mysql_errno().": ".mysql_error()." In : $sql **** -->\n";
  243. }
  244. else
  245. {
  246. // init tab with all months
  247. for($i = 0; $i < 12; $i++)
  248. {
  249. $month_array[$MonthsLong[$i]] = 0;
  250. }
  251. while($row = mysql_fetch_row($res))
  252. {
  253. $date_array = getdate($row[0]);
  254. $month_array[$MonthsLong[$date_array['mon']-1]]++;
  255. $month_array['total']++;
  256. }
  257. mysql_free_result($res);
  258. }
  259. return $month_array;
  260. }
  261. /**
  262. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  263. * @param period_array : an array provided by hoursTab($sql) or daysTab($sql)
  264. * @param periodTitle : title of the first column, type of period
  265. * @param linkOnPeriod :
  266. * @desc Display a 4 column array
  267. Columns are : hour of day, graph, number of hits and %
  268. First line are titles
  269. next are informations
  270. Last is total number of hits
  271. */
  272. function makeHitsTable($period_array, $periodTitle, $linkOnPeriod = '???')
  273. {
  274. echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' align=center class='minitext'>";
  275. // titles
  276. echo "<tr bgcolor='#E6E6E6' align='center'>
  277. <td width='15%' >
  278. <b>$periodTitle</b>
  279. </td>
  280. <td width='60%'>
  281. &nbsp;
  282. </td>
  283. <td width='10%'>
  284. <b>".get_lang('Hits')."</b>
  285. </td>
  286. <td width='15%'>
  287. <b>%</b>
  288. </td>
  289. </tr>
  290. ";
  291. $factor = 4;
  292. $maxSize = $factor * 100; //pixels
  293. while(list($periodPiece, $cpt) = each($period_array))
  294. {
  295. if($periodPiece != 'total')
  296. {
  297. $pourcent = round(100 * $cpt / $period_array['total']);
  298. $barwidth = $factor * $pourcent ;
  299. echo "<tr>
  300. <td align='center' width='15%'>";
  301. echo $periodPiece;
  302. echo "</td>
  303. <td width='60%' style='padding-top: 3px;' align='center'>"
  304. // display hitbar
  305. ."<img src='../img/bar_1.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  306. if($pourcent != 0)
  307. echo "<img src='../img/bar_1u.gif' width='$barwidth' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  308. // display 100% bar
  309. if($pourcent != 100 && $pourcent != 0)
  310. echo "<img src='../img/bar_1m.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  311. if($pourcent != 100)
  312. echo "<img src='../img/bar_1r.gif' width='".($maxSize-$barwidth)."' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  313. echo "<img src='../img/bar_1.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />
  314. </td>
  315. <td align='center' width='10%'>
  316. $cpt
  317. </td>
  318. <td align='center' width='15%'>
  319. $pourcent %
  320. </td>
  321. </tr>
  322. ";
  323. }
  324. }
  325. echo "<tr bgcolor='#E6E6E6'>
  326. <td width='15%' align='center'>
  327. ".get_lang('Total')."
  328. </td>
  329. <td align='right' width='60%'>
  330. &nbsp;
  331. </td>
  332. <td align='center' width='10%'>
  333. ".$period_array['total']."
  334. </td>
  335. <td width='15%'>
  336. &nbsp;
  337. </td>
  338. </tr>
  339. ";
  340. echo "</table>";
  341. }
  342. /**
  343. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  344. * @param array_of_results : a 2 columns array
  345. * @param title1 : string, title of the first column
  346. * @param title2 : string, title of the ... second column
  347. * @desc display a 2 column tab from an array
  348. titles of columns are title1 and title2
  349. */
  350. function buildTab2col($array_of_results, $title1, $title2)
  351. {
  352. echo "<table cellpadding='2' cellspacing='1' border='1' align='center'>\n";
  353. echo "<tr>
  354. <td bgcolor='#E6E6E6'>
  355. $title1
  356. </td>
  357. <td bgcolor='#E6E6E6'>
  358. $title2
  359. </td>
  360. </tr>\n";
  361. if (is_array($array_of_results))
  362. {
  363. for($j = 0 ; $j < count($array_of_results) ; $j++)
  364. {
  365. echo '<tr>';
  366. echo '<td bgcolor="#eeeeee">'.$array_of_results[$j][0].'</td>';
  367. echo '<td align="right">'.$array_of_results[$j][1].'</td>';
  368. echo "</tr>\n";
  369. }
  370. }
  371. else
  372. {
  373. echo '<tr>';
  374. echo '<td colspan="2" align="center">'.get_lang('NoResult').'</td>';
  375. echo "</tr>\n";
  376. }
  377. echo "</table>\n";
  378. }
  379. /**
  380. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  381. * @param array_of_results : a 2 columns array
  382. * @desc display a 2 column tab from an array
  383. this tab has no title
  384. */
  385. function buildTab2ColNoTitle($array_of_results)
  386. {
  387. echo "<table cellpadding='3' cellspacing='1' border='0' align='center'>\n";
  388. if (is_array($array_of_results))
  389. {
  390. for($j = 0 ; $j < count($array_of_results) ; $j++)
  391. {
  392. echo '<tr>';
  393. echo '<td bgcolor="#eeeeee">'.$array_of_results[$j][0].'</td>';
  394. echo '<td align="right">&nbsp;&nbsp;'.$array_of_results[$j][1].'</td>';
  395. echo "</tr>\n";
  396. }
  397. }
  398. else
  399. {
  400. echo '<tr>';
  401. echo '<td colspan="2" align="center">'.get_lang('NoResult').'</td>';
  402. echo "</tr>\n";
  403. }
  404. echo "</table>\n";
  405. }
  406. /**
  407. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  408. * @param array_of_results : a 2 columns array
  409. * @desc this function is used to display
  410. integrity errors in the platform
  411. if array_of_results is not an array there is
  412. no error, else errors are displayed
  413. */
  414. function buildTabDefcon($array_of_results)
  415. {
  416. echo "<table width='60%' cellpadding='2' cellspacing='1' border='0' align=center class='minitext'>\n";
  417. if (is_array($array_of_results))
  418. {
  419. // there are some strange cases...
  420. echo '<tr>';
  421. echo '<td colspan="2" align="center" bgcolor="#eeeeee"><font color="#ff0000">'.get_lang('Defcon').'</font></td>';
  422. echo "</tr>\n";
  423. for($j = 0 ; $j < count($array_of_results) ; $j++)
  424. {
  425. if($array_of_results[$j][0] == "")
  426. {
  427. $key = get_lang('NULLValue');
  428. }
  429. else
  430. {
  431. $key = $array_of_results[$j][0];
  432. }
  433. echo '<tr>';
  434. echo '<td width="70%" class="content">'.$key.'</td>';
  435. echo '<td width="30%" align="right">'.$array_of_results[$j][1].'</td>';
  436. echo "</tr>\n";
  437. }
  438. }
  439. else
  440. {
  441. // all right
  442. echo '<tr>';
  443. echo '<td colspan="2" align="center"><font color="#00ff00">'.get_lang('AllRight').'</font></td>';
  444. echo "</tr>\n";
  445. }
  446. echo "</table>\n";
  447. }
  448. ?>