statsUtils.lib.inc.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. class StatsUtils
  9. {
  10. /**
  11. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  12. * @param sql : a sql query (as a string)
  13. * @desc return one result from a sql query (1 single result)
  14. */
  15. public static function getOneResult($sql)
  16. {
  17. $query = Database::query($sql);
  18. if ($query !== false) {
  19. $res = @Database::fetch_array($query, 'NUM');
  20. } else {
  21. $res = array();
  22. }
  23. return $res[0];
  24. }
  25. /**
  26. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  27. * @param sql : a sql query (as a string)
  28. * @desc Return many results of a query in a 1 column tab
  29. */
  30. public static function getManyResults1Col($sql)
  31. {
  32. $res = Database::query($sql);
  33. if ($res !== false) {
  34. $i = 0;
  35. while ($resA = Database::fetch_array($res, 'NUM')) {
  36. $resu[$i++] = $resA[0];
  37. }
  38. }
  39. return $resu;
  40. }
  41. /**
  42. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  43. * @param sql : a sql query (as a string)
  44. * @desc Return many results of a query
  45. */
  46. public static function getManyResults2Col($sql)
  47. {
  48. $res = Database::query($sql);
  49. if ($res !== false) {
  50. $i = 0;
  51. while ($resA = Database::fetch_array($res, 'NUM')) {
  52. $resu[$i][0] = $resA[0];
  53. $resu[$i][1] = $resA[1];
  54. $i++;
  55. }
  56. }
  57. return $resu;
  58. }
  59. /**
  60. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  61. * @param sql : a sql query (as a string)
  62. * @desc Return many results of a query in a 3 column tab
  63. * in $resu[$i][0], $resu[$i][1],$resu[$i][2]
  64. */
  65. public static function getManyResults3Col($sql)
  66. {
  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. $resu[$i][2] = $resA[2];
  74. $i++;
  75. }
  76. }
  77. return $resu;
  78. }
  79. /**
  80. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  81. * @param sql : a sql query (as a string)
  82. * @desc Return many results of a query in a X column tab
  83. * in $resu[$i][0], $resu[$i][1],$resu[$i][2],...
  84. * this function is more 'standard' but use a little
  85. * more ressources
  86. * So I encourage to use the dedicated for 1, 2 or 3
  87. * columns of results
  88. */
  89. public static function getManyResultsXCol($sql, $X)
  90. {
  91. $res = Database::query($sql);
  92. if ($res !== false) {
  93. $i = 0;
  94. while ($resA = Database::fetch_array($res, 'NUM')) {
  95. for ($j = 0; $j < $X; $j++) {
  96. $resu[$i][$j] = $resA[$j];
  97. }
  98. $i++;
  99. }
  100. }
  101. return $resu;
  102. }
  103. /**
  104. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  105. * @param sql : a sql query (as a string)
  106. * @return hours_array
  107. * @desc Return an assoc array. Keys are the hours, values are
  108. * the number of time this hours was found.
  109. * key 'total' return the sum of all number of time hours
  110. * appear
  111. */
  112. public static function hoursTab($sql)
  113. {
  114. $hours_array = array('total' => 0);
  115. $res = Database::query($sql);
  116. if ($res !== false) {
  117. $last_hours = -1;
  118. while ($row = Database::fetch_row($res)) {
  119. $date_array = getdate($row[0]);
  120. if ($date_array['hours'] == $last_hours) {
  121. $hours_array[$date_array['hours']]++;
  122. } else {
  123. $hours_array[$date_array['hours']] = 1;
  124. $last_hours = $date_array['hours'];
  125. }
  126. $hours_array['total']++;
  127. }
  128. Database::free_result($res);
  129. }
  130. return $hours_array;
  131. }
  132. /**
  133. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  134. * @param sql : a sql query (as a string)
  135. * @return days_array
  136. * @desc Return an assoc array. Keys are the days, values are
  137. * the number of time this hours was found.
  138. * key "total" return the sum of all number of time days
  139. * appear
  140. */
  141. public static function daysTab($sql)
  142. {
  143. $MonthsShort = api_get_months_short();
  144. $days_array = array('total' => 0);
  145. $res = Database::query($sql);
  146. if ($res !== false) {
  147. $last_day = -1;
  148. while ($row = Database::fetch_row($res)) {
  149. $date_array = getdate($row[0]);
  150. $display_date = $date_array['mday'] . ' ' . $MonthsShort[$date_array['mon'] - 1] . ' ' . $date_array['year'];
  151. if ($date_array['mday'] == $last_day) {
  152. $days_array[$display_date]++;
  153. } else {
  154. $days_array[$display_date] = 1;
  155. $last_day = $display_date;
  156. }
  157. $days_array['total']++;
  158. }
  159. Database::free_result($res);
  160. }
  161. return $days_array;
  162. }
  163. /**
  164. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  165. * @param sql : a sql query (as a string)
  166. * @return month_array
  167. * @desc Return an assoc array. Keys are the days, values are
  168. * the number of time this hours was found.
  169. * key "total" return the sum of all number of time days
  170. * appear
  171. */
  172. public static function monthTab($sql)
  173. {
  174. $MonthsLong = api_get_months_long();
  175. $month_array = array('total' => 0);
  176. $res = Database::query($sql);
  177. if ($res !== false) {
  178. // init tab with all months
  179. for ($i = 0; $i < 12; $i++) {
  180. $month_array[$MonthsLong[$i]] = 0;
  181. }
  182. while ($row = Database::fetch_row($res)) {
  183. $date_array = getdate($row[0]);
  184. $month_array[$MonthsLong[$date_array['mon'] - 1]]++;
  185. $month_array['total']++;
  186. }
  187. Database::free_result($res);
  188. }
  189. return $month_array;
  190. }
  191. /**
  192. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  193. * @param period_array : an array provided by hoursTab($sql) or daysTab($sql)
  194. * @param periodTitle : title of the first column, type of period
  195. * @param linkOnPeriod :
  196. * @desc Display a 4 column array
  197. * Columns are : hour of day, graph, number of hits and %
  198. * First line are titles
  199. * next are informations
  200. * Last is total number of hits
  201. */
  202. public static function makeHitsTable($period_array, $periodTitle, $linkOnPeriod = '???')
  203. {
  204. echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' align=center class='minitext'>";
  205. // titles
  206. echo "<tr bgcolor='#E6E6E6' align='center'>
  207. <td width='15%' >
  208. <b>$periodTitle</b>
  209. </td>
  210. <td width='60%'>
  211. &nbsp;
  212. </td>
  213. <td width='10%'>
  214. <b>" . get_lang('Hits') . "</b>
  215. </td>
  216. <td width='15%'>
  217. <b>%</b>
  218. </td>
  219. </tr>
  220. ";
  221. $factor = 4;
  222. $maxSize = $factor * 100; //pixels
  223. while (list($periodPiece, $cpt) = each($period_array)) {
  224. if ($periodPiece != 'total') {
  225. $pourcent = round(100 * $cpt / $period_array['total']);
  226. $barwidth = $factor * $pourcent;
  227. echo "<tr>
  228. <td align='center' width='15%'>";
  229. echo $periodPiece;
  230. echo "</td>
  231. <td width='60%' style='padding-top: 3px;' align='center'>"
  232. // display hitbar
  233. . "<img src='../img/bar_1.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  234. if ($pourcent != 0) {
  235. echo "<img src='../img/bar_1u.gif' width='$barwidth' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  236. }
  237. // display 100% bar
  238. if ($pourcent != 100 && $pourcent != 0) {
  239. echo "<img src='../img/bar_1m.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  240. }
  241. if ($pourcent != 100) {
  242. echo "<img src='../img/bar_1r.gif' width='" . ($maxSize - $barwidth) . "' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />";
  243. }
  244. echo "<img src='../img/bar_1.gif' width='1' height='12' alt='$periodPiece : $cpt hits &ndash; $pourcent %' />
  245. </td>
  246. <td align='center' width='10%'>
  247. $cpt
  248. </td>
  249. <td align='center' width='15%'>
  250. $pourcent %
  251. </td>
  252. </tr>
  253. ";
  254. }
  255. }
  256. echo "<tr bgcolor='#E6E6E6'>
  257. <td width='15%' align='center'>
  258. " . get_lang('Total') . "
  259. </td>
  260. <td align='right' width='60%'>
  261. &nbsp;
  262. </td>
  263. <td align='center' width='10%'>
  264. " . $period_array['total'] . "
  265. </td>
  266. <td width='15%'>
  267. &nbsp;
  268. </td>
  269. </tr>
  270. ";
  271. echo "</table>";
  272. }
  273. }