statsUtils.lib.inc.php 11 KB

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