statistics.lib.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides some functions for statistics
  5. * @package chamilo.statistics
  6. */
  7. class Statistics
  8. {
  9. /**
  10. * Converts a number of bytes in a formatted string
  11. * @param int $size
  12. * @return string Formatted file size
  13. */
  14. static function make_size_string($size)
  15. {
  16. if ($size < pow(2, 10)) {
  17. return $size." bytes";
  18. }
  19. if ($size >= pow(2, 10) && $size < pow(2, 20)) {
  20. return round($size / pow(2, 10), 0)." KB";
  21. }
  22. if ($size >= pow(2, 20) && $size < pow(2, 30)) {
  23. return round($size / pow(2, 20), 1)." MB";
  24. }
  25. if ($size > pow(2, 30)) {
  26. return round($size / pow(2, 30), 2)." GB";
  27. }
  28. }
  29. /**
  30. * Count courses
  31. * @param string $category_code Code of a course category. Default: count
  32. * all courses.
  33. * @return int Number of courses counted
  34. */
  35. static function count_courses($category_code = null)
  36. {
  37. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  38. $access_url_rel_course_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  39. $current_url_id = api_get_current_access_url_id();
  40. if (api_is_multiple_url_enabled()) {
  41. $sql = "SELECT COUNT(*) AS number FROM ".$course_table." as c, ".$access_url_rel_course_table." as u
  42. WHERE u.c_id = c.id AND access_url_id='".$current_url_id."'";
  43. if (isset ($category_code)) {
  44. $sql .= " AND category_code = '".Database::escape_string($category_code)."'";
  45. }
  46. } else {
  47. $sql = "SELECT COUNT(*) AS number FROM ".$course_table." ";
  48. if (isset ($category_code)) {
  49. $sql .= " WHERE category_code = '".Database::escape_string($category_code)."'";
  50. }
  51. }
  52. $res = Database::query($sql);
  53. $obj = Database::fetch_object($res);
  54. return $obj->number;
  55. }
  56. /**
  57. * Count courses by visibility
  58. * @param int Visibility (0 = closed, 1 = private, 2 = open, 3 = public)
  59. * all courses.
  60. * @return int Number of courses counted
  61. */
  62. static function count_courses_by_visibility($vis = null)
  63. {
  64. if (!isset($vis)) {
  65. return 0;
  66. }
  67. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  68. $access_url_rel_course_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  69. $current_url_id = api_get_current_access_url_id();
  70. if (api_is_multiple_url_enabled()) {
  71. $sql = "SELECT COUNT(*) AS number
  72. FROM ".$course_table." as c, ".$access_url_rel_course_table." as u
  73. WHERE u.c_id = c.id AND access_url_id='".$current_url_id."'";
  74. if (isset ($vis)) {
  75. $sql .= " AND visibility = ".intval($vis);
  76. }
  77. } else {
  78. $sql = "SELECT COUNT(*) AS number FROM ".$course_table." ";
  79. if (isset ($vis)) {
  80. $sql .= " WHERE visibility = ".intval($vis);
  81. }
  82. }
  83. $res = Database::query($sql);
  84. $obj = Database::fetch_object($res);
  85. return $obj->number;
  86. }
  87. /**
  88. * Count users
  89. * @param int optional, user status (COURSEMANAGER or STUDENT), if it's not setted it'll count all users.
  90. * @param string optional, code of a course category. Default: count only users without filtering category
  91. * @param bool count invisible courses (todo)
  92. * @param bool count only active users (false to only return currently active users)
  93. * @return int Number of users counted
  94. */
  95. static function count_users(
  96. $status = null,
  97. $category_code = null,
  98. $count_invisible_courses = true,
  99. $only_active = false
  100. ) {
  101. // Database table definitions
  102. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  103. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  104. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  105. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  106. $current_url_id = api_get_current_access_url_id();
  107. $active_filter = $only_active ? ' AND active=1' : '';
  108. $status_filter = isset($status) ? ' AND status = '.intval($status) : '';
  109. if (api_is_multiple_url_enabled()) {
  110. $sql = "SELECT COUNT(DISTINCT(u.user_id)) AS number
  111. FROM $user_table as u, $access_url_rel_user_table as url
  112. WHERE u.user_id=url.user_id AND access_url_id='".$current_url_id."' $status_filter $active_filter";
  113. if (isset ($category_code)) {
  114. $sql = "SELECT COUNT(DISTINCT(cu.user_id)) AS number
  115. FROM $course_user_table cu, $course_table c, $access_url_rel_user_table as url
  116. WHERE c.id = cu.c_id AND
  117. c.category_code = '".Database::escape_string($category_code)."' AND
  118. cu.user_id=url.user_id AND
  119. access_url_id='".$current_url_id."' $status_filter $active_filter";
  120. }
  121. } else {
  122. $sql = "SELECT COUNT(DISTINCT(user_id)) AS number FROM $user_table WHERE 1=1 $status_filter $active_filter";
  123. if (isset ($category_code)) {
  124. $status_filter = isset($status) ? ' AND status = '.intval($status) : '';
  125. $sql = "SELECT COUNT(DISTINCT(cu.user_id)) AS number
  126. FROM $course_user_table cu, $course_table c
  127. WHERE c.id = cu.c_id AND c.category_code = '".Database::escape_string(
  128. $category_code
  129. )."' $status_filter $active_filter";
  130. }
  131. }
  132. $res = Database::query($sql);
  133. $obj = Database::fetch_object($res);
  134. return $obj->number;
  135. }
  136. /**
  137. * Count activities from track_e_default_table
  138. * @return int Number of activities counted
  139. */
  140. static function get_number_of_activities()
  141. {
  142. $track_e_default = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  143. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  144. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  145. $current_url_id = api_get_current_access_url_id();
  146. if (api_is_multiple_url_enabled()) {
  147. $sql = "SELECT count(default_id) AS total_number_of_items FROM $track_e_default, $table_user user, $access_url_rel_user_table url WHERE default_user_id = user.user_id AND user.user_id=url.user_id AND access_url_id='".$current_url_id."'";
  148. } else {
  149. $sql = "SELECT count(default_id) AS total_number_of_items FROM $track_e_default, $table_user user WHERE default_user_id = user.user_id ";
  150. }
  151. if (isset($_GET['keyword'])) {
  152. $keyword = Database::escape_string(trim($_GET['keyword']));
  153. $sql .= " AND (user.username LIKE '%".$keyword."%' OR default_event_type LIKE '%".$keyword."%' OR default_value_type LIKE '%".$keyword."%' OR default_value LIKE '%".$keyword."%') ";
  154. }
  155. $res = Database::query($sql);
  156. $obj = Database::fetch_object($res);
  157. return $obj->total_number_of_items;
  158. }
  159. /**
  160. * Get activities data to display
  161. */
  162. static function get_activities_data($from, $number_of_items, $column, $direction)
  163. {
  164. global $dateTimeFormatLong;
  165. $track_e_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  166. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  167. $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  168. $current_url_id = api_get_current_access_url_id();
  169. $column = intval($column);
  170. $from = intval($from);
  171. $number_of_items = intval($number_of_items);
  172. if (!in_array($direction, array('ASC', 'DESC'))) {
  173. $direction = 'DESC';
  174. }
  175. if (api_is_multiple_url_enabled()) {
  176. $sql = "SELECT ".
  177. "default_event_type as col0, ".
  178. "default_value_type as col1, ".
  179. "default_value as col2, ".
  180. "user.username as col3, ".
  181. "user.user_id as col4, ".
  182. "default_date as col5 ".
  183. "FROM $track_e_default as track_default, $table_user as user, $access_url_rel_user_table as url ".
  184. "WHERE track_default.default_user_id = user.user_id AND url.user_id=user.user_id AND access_url_id='".$current_url_id."'";
  185. } else {
  186. $sql = "SELECT ".
  187. "default_event_type as col0, ".
  188. "default_value_type as col1, ".
  189. "default_value as col2, ".
  190. "user.username as col3, ".
  191. "user.user_id as col4, ".
  192. "default_date as col5 ".
  193. "FROM $track_e_default track_default, $table_user user ".
  194. "WHERE track_default.default_user_id = user.user_id ";
  195. }
  196. if (isset($_GET['keyword'])) {
  197. $keyword = Database::escape_string(trim($_GET['keyword']));
  198. $sql .= " AND (user.username LIKE '%".$keyword."%' OR default_event_type LIKE '%".$keyword."%' OR default_value_type LIKE '%".$keyword."%' OR default_value LIKE '%".$keyword."%') ";
  199. }
  200. if (!empty($column) && !empty($direction)) {
  201. $sql .= " ORDER BY col$column $direction";
  202. } else {
  203. $sql .= " ORDER BY col5 DESC ";
  204. }
  205. $sql .= " LIMIT $from, $number_of_items ";
  206. $res = Database::query($sql);
  207. $activities = array();
  208. while ($row = Database::fetch_row($res)) {
  209. if (strpos($row[1], '_object') === false) {
  210. $row[2] = $row[2];
  211. } else {
  212. if (!empty($row[2])) {
  213. $row[2] = unserialize($row[2]);
  214. if (is_array($row[2]) && !empty($row[2])) {
  215. $row[2] = Text::implode_with_key(', ', $row[2]);
  216. }
  217. }
  218. }
  219. if (!empty($row['default_date']) && $row['default_date'] != '0000-00-00 00:00:00') {
  220. $row['default_date'] = api_get_local_time($row['default_date']);
  221. } else {
  222. $row['default_date'] = '-';
  223. }
  224. if (!empty($row[4])) { //user ID
  225. $row[3] = Display::url(
  226. $row[3],
  227. api_get_path(WEB_CODE_PATH).'admin/user_information?user_id='.$row[5],
  228. array('title' => get_lang('UserInfo'))
  229. );
  230. $row[4] = TrackingUserLog::get_ip_from_user_event($row[4], $row[5], true);
  231. if (empty($row[4])) {
  232. $row[4] = get_lang('Unknown');
  233. }
  234. }
  235. $activities[] = $row;
  236. }
  237. return $activities;
  238. }
  239. /**
  240. * Get all course categories
  241. * @return array All course categories (code => name)
  242. */
  243. static function get_course_categories()
  244. {
  245. $category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  246. $sql = "SELECT code, name FROM $category_table ORDER BY tree_pos";
  247. $res = Database::query($sql);
  248. $categories = array();
  249. while ($category = Database::fetch_object($res)) {
  250. $categories[$category->code] = $category->name;
  251. }
  252. return $categories;
  253. }
  254. /**
  255. * Rescale data
  256. * @param array $data The data that should be rescaled
  257. * @param int $max The maximum value in the rescaled data (default = 500);
  258. * @return array The rescaled data, same key as $data
  259. */
  260. static function rescale($data, $max = 500)
  261. {
  262. $data_max = 1;
  263. foreach ($data as $index => $value) {
  264. $data_max = ($data_max < $value ? $value : $data_max);
  265. }
  266. reset($data);
  267. $result = array();
  268. $delta = $max / $data_max;
  269. foreach ($data as $index => $value) {
  270. $result[$index] = (int)round($value * $delta);
  271. }
  272. return $result;
  273. }
  274. /**
  275. * Show statistics
  276. * @param string $title The title
  277. * @param array $stats
  278. * @param bool $show_total
  279. * @param bool $is_file_size
  280. */
  281. static function print_stats($title, $stats, $show_total = true, $is_file_size = false)
  282. {
  283. $total = 0;
  284. $data = Statistics::rescale($stats);
  285. echo '<table class="data_table" cellspacing="0" cellpadding="3">
  286. <tr><th colspan="'.($show_total ? '4' : '3').'">'.$title.'</th></tr>';
  287. $i = 0;
  288. foreach ($stats as $subtitle => $number) {
  289. $total += $number;
  290. }
  291. foreach ($stats as $subtitle => $number) {
  292. if (!$is_file_size) {
  293. $number_label = number_format($number, 0, ',', '.');
  294. } else {
  295. $number_label = Statistics::make_size_string($number);
  296. }
  297. $percentage = ($total > 0 ? number_format(100 * $number / $total, 1, ',', '.') : '0');
  298. echo '<tr class="row_'.($i % 2 == 0 ? 'odd' : 'even').'">
  299. <td width="150">'.$subtitle.'</td>
  300. <td width="550">'.Display::bar_progress($percentage, false).'</td>
  301. <td align="right">'.$number_label.'</td>';
  302. if ($show_total) {
  303. echo '<td align="right"> '.$percentage.'%</td>';
  304. }
  305. echo '</tr>';
  306. $i++;
  307. }
  308. if ($show_total) {
  309. if (!$is_file_size) {
  310. $total_label = number_format($total, 0, ',', '.');
  311. } else {
  312. $total_label = Statistics::make_size_string($total);
  313. }
  314. echo '<tr><th colspan="4" align="right">'.get_lang('Total').': '.$total_label.'</td></tr>';
  315. }
  316. echo '</table>';
  317. }
  318. /**
  319. * Show some stats about the number of logins
  320. * @param string $type month, hour or day
  321. */
  322. static function print_login_stats($type)
  323. {
  324. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  325. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  326. $current_url_id = api_get_current_access_url_id();
  327. if (api_is_multiple_url_enabled()) {
  328. $table_url = ", $access_url_rel_user_table";
  329. $where_url = " WHERE login_user_id=user_id AND access_url_id='".$current_url_id."'";
  330. $where_url_last = ' AND login_date > DATE_SUB(NOW(),INTERVAL 1 %s)';
  331. } else {
  332. $table_url = '';
  333. $where_url = '';
  334. $where_url_last = ' WHERE login_date > DATE_SUB(NOW(),INTERVAL 1 %s)';
  335. }
  336. switch ($type) {
  337. case 'month':
  338. $months = api_get_months_long();
  339. $period = get_lang('PeriodMonth');
  340. $sql = "SELECT DATE_FORMAT( login_date, '%Y-%m' ) AS stat_date , count( login_id ) AS number_of_logins FROM ".$table.$table_url.$where_url." GROUP BY stat_date ORDER BY login_date ";
  341. $sql_last_x = "SELECT DATE_FORMAT( login_date, '%Y-%m' ) AS stat_date , count( login_id ) AS number_of_logins FROM ".$table.$table_url.$where_url.sprintf(
  342. $where_url_last,
  343. 'YEAR'
  344. )." GROUP BY stat_date ORDER BY login_date ";
  345. break;
  346. case 'hour':
  347. $period = get_lang('PeriodHour');
  348. $sql = "SELECT DATE_FORMAT( login_date, '%H' ) AS stat_date , count( login_id ) AS number_of_logins FROM ".$table.$table_url.$where_url." GROUP BY stat_date ORDER BY stat_date ";
  349. $sql_last_x = "SELECT DATE_FORMAT( login_date, '%H' ) AS stat_date , count( login_id ) AS number_of_logins FROM ".$table.$table_url.$where_url.sprintf(
  350. $where_url_last,
  351. 'DAY'
  352. )." GROUP BY stat_date ORDER BY stat_date ";
  353. break;
  354. case 'day':
  355. $week_days = api_get_week_days_long();
  356. $period = get_lang('PeriodDay');
  357. $sql = "SELECT DATE_FORMAT( login_date, '%w' ) AS stat_date , count( login_id ) AS number_of_logins FROM ".$table.$table_url.$where_url." GROUP BY stat_date ORDER BY DATE_FORMAT( login_date, '%w' ) ";
  358. $sql_last_x = "SELECT DATE_FORMAT( login_date, '%w' ) AS stat_date , count( login_id ) AS number_of_logins FROM ".$table.$table_url.$where_url.sprintf(
  359. $where_url_last,
  360. 'WEEK'
  361. )." GROUP BY stat_date ORDER BY DATE_FORMAT( login_date, '%w' ) ";
  362. break;
  363. }
  364. $res_last_x = Database::query($sql_last_x);
  365. $result_last_x = array();
  366. while ($obj = Database::fetch_object($res_last_x)) {
  367. $stat_date = $obj->stat_date;
  368. switch ($type) {
  369. case 'month':
  370. $stat_date = explode('-', $stat_date);
  371. $stat_date[1] = $months[$stat_date[1] - 1];
  372. $stat_date = implode(' ', $stat_date);
  373. break;
  374. case 'day':
  375. $stat_date = $week_days[$stat_date];
  376. break;
  377. }
  378. $result_last_x[$stat_date] = $obj->number_of_logins;
  379. }
  380. Statistics::print_stats(get_lang('LastLogins').' ('.$period.')', $result_last_x, true);
  381. flush(); //flush web request at this point to see something already while the full data set is loading
  382. echo '<br />';
  383. $res = Database::query($sql);
  384. $result = array();
  385. while ($obj = Database::fetch_object($res)) {
  386. $stat_date = $obj->stat_date;
  387. switch ($type) {
  388. case 'month':
  389. $stat_date = explode('-', $stat_date);
  390. $stat_date[1] = $months[$stat_date[1] - 1];
  391. $stat_date = implode(' ', $stat_date);
  392. break;
  393. case 'day':
  394. $stat_date = $week_days[$stat_date];
  395. break;
  396. }
  397. $result[$stat_date] = $obj->number_of_logins;
  398. }
  399. Statistics::print_stats(get_lang('AllLogins').' ('.$period.')', $result, true);
  400. }
  401. /**
  402. * Print the number of recent logins
  403. */
  404. static function print_recent_login_stats()
  405. {
  406. $total_logins = array();
  407. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  408. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  409. $current_url_id = api_get_current_access_url_id();
  410. if (api_is_multiple_url_enabled()) {
  411. $table_url = ", $access_url_rel_user_table";
  412. $where_url = " AND login_user_id=user_id AND access_url_id='".$current_url_id."'";
  413. } else {
  414. $table_url = '';
  415. $where_url = '';
  416. }
  417. $sql[get_lang(
  418. 'Thisday'
  419. )] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 1 DAY) >= NOW() $where_url";
  420. $sql[get_lang(
  421. 'Last7days'
  422. )] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 7 DAY) >= NOW() $where_url";
  423. $sql[get_lang(
  424. 'Last31days'
  425. )] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 31 DAY) >= NOW() $where_url";
  426. $sql[get_lang(
  427. 'Total'
  428. )] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE 1=1 $where_url";
  429. foreach ($sql as $index => $query) {
  430. $res = Database::query($query);
  431. $obj = Database::fetch_object($res);
  432. $total_logins[$index] = $obj->number;
  433. }
  434. Statistics::print_stats(get_lang('Logins'), $total_logins, false);
  435. }
  436. /**
  437. * Show some stats about the accesses to the different course tools
  438. */
  439. static function print_tool_stats()
  440. {
  441. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  442. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  443. $access_url_rel_course_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  444. $current_url_id = api_get_current_access_url_id();
  445. $tools = array(
  446. 'announcement',
  447. 'assignment',
  448. 'calendar_event',
  449. 'chat',
  450. 'conference',
  451. 'course_description',
  452. 'document',
  453. 'dropbox',
  454. 'group',
  455. 'learnpath',
  456. 'link',
  457. 'quiz',
  458. 'student_publication',
  459. 'user',
  460. 'forum'
  461. );
  462. $tool_names = array();
  463. foreach ($tools as $tool) {
  464. $tool_names[$tool] = get_lang(ucfirst($tool), '');
  465. }
  466. if (api_is_multiple_url_enabled()) {
  467. $sql = "SELECT access_tool, count( access_id ) AS number_of_logins
  468. FROM $table, $access_url_rel_course_table u, $tableCourse c
  469. WHERE access_tool IN ('".implode(
  470. "','",
  471. $tools
  472. )."') AND c.id = u.c_id AND c.id = c_id AND access_url_id='".$current_url_id."' ".
  473. "GROUP BY access_tool ";
  474. } else {
  475. $sql = "SELECT access_tool, count( access_id ) AS number_of_logins FROM $table ".
  476. "WHERE access_tool IN ('".implode("','", $tools)."') ".
  477. "GROUP BY access_tool ";
  478. }
  479. $res = Database::query($sql);
  480. $result = array();
  481. while ($obj = Database::fetch_object($res)) {
  482. $result[$tool_names[$obj->access_tool]] = $obj->number_of_logins;
  483. }
  484. Statistics::print_stats(get_lang('PlatformToolAccess'), $result, true);
  485. }
  486. /**
  487. * Show some stats about the number of courses per language
  488. */
  489. static function print_course_by_language_stats()
  490. {
  491. $table = Database :: get_main_table(TABLE_MAIN_COURSE);
  492. $access_url_rel_course_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  493. $current_url_id = api_get_current_access_url_id();
  494. if (api_is_multiple_url_enabled()) {
  495. $sql = "SELECT course_language, count( c.code ) AS number_of_courses ".
  496. "FROM $table as c, $access_url_rel_course_table as u
  497. WHERE u.c_id = c.id AND access_url_id='".$current_url_id."'
  498. GROUP BY course_language ORDER BY number_of_courses DESC";
  499. } else {
  500. $sql = "SELECT course_language, count(code) AS number_of_courses ".
  501. "FROM $table GROUP BY course_language ORDER BY number_of_courses DESC";
  502. }
  503. $res = Database::query($sql);
  504. $result = array();
  505. while ($obj = Database::fetch_object($res)) {
  506. $result[$obj->course_language] = $obj->number_of_courses;
  507. }
  508. Statistics::print_stats(get_lang('CountCourseByLanguage'), $result, true);
  509. }
  510. /**
  511. * Shows the number of users having their picture uploaded in Dokeos.
  512. */
  513. static function print_user_pictures_stats()
  514. {
  515. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  516. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  517. $current_url_id = api_get_current_access_url_id();
  518. if (api_is_multiple_url_enabled()) {
  519. $url_condition = ", $access_url_rel_user_table as url WHERE url.user_id=u.user_id AND access_url_id='".$current_url_id."'";
  520. $url_condition2 = " AND url.user_id=u.user_id AND access_url_id='".$current_url_id."'";
  521. $table = ", $access_url_rel_user_table as url ";
  522. }
  523. $sql = "SELECT COUNT(*) AS n FROM $user_table as u ".$url_condition;
  524. $res = Database::query($sql);
  525. $count1 = Database::fetch_object($res);
  526. $sql = "SELECT COUNT(*) AS n FROM $user_table as u $table ".
  527. "WHERE LENGTH(picture_uri) > 0 $url_condition2";
  528. $res = Database::query($sql);
  529. $count2 = Database::fetch_object($res);
  530. // #users without picture
  531. $result[get_lang('No')] = $count1->n - $count2->n;
  532. $result[get_lang('Yes')] = $count2->n; // #users with picture
  533. Statistics::print_stats(get_lang('CountUsers').' ('.get_lang('UserPicture').')', $result, true);
  534. }
  535. static function print_activities_stats()
  536. {
  537. echo '<h4>'.get_lang('ImportantActivities').'</h4>';
  538. // Create a search-box
  539. $form = new FormValidator('search_simple', 'get', api_get_path(
  540. WEB_CODE_PATH
  541. ).'admin/statistics/index.php', '', 'width=200px', false);
  542. $renderer =& $form->defaultRenderer();
  543. $renderer->setElementTemplate('<span>{element}</span> ');
  544. $form->addElement('hidden', 'report', 'activities');
  545. $form->addElement('hidden', 'activities_direction', 'DESC');
  546. $form->addElement('hidden', 'activities_column', '4');
  547. $form->addElement('text', 'keyword', get_lang('keyword'));
  548. $form->addElement('style_submit_button', 'submit', get_lang('Search'), 'class="search"');
  549. echo '<div class="actions">';
  550. $form->display();
  551. echo '</div>';
  552. $table = new SortableTable('activities', array(
  553. 'Statistics',
  554. 'get_number_of_activities'
  555. ), array(
  556. 'Statistics',
  557. 'get_activities_data'
  558. ), 5, 50, 'DESC');
  559. $parameters = array();
  560. $parameters['report'] = 'activities';
  561. if (isset($_GET['keyword'])) {
  562. $parameters['keyword'] = Security::remove_XSS($_GET['keyword']);
  563. }
  564. $table->set_additional_parameters($parameters);
  565. $table->set_header(0, get_lang('EventType'));
  566. $table->set_header(1, get_lang('DataType'));
  567. $table->set_header(2, get_lang('Value'));
  568. $table->set_header(3, get_lang('UserName'));
  569. $table->set_header(4, get_lang('IPAddress'));
  570. $table->set_header(5, get_lang('Date'));
  571. $table->display();
  572. }
  573. /**
  574. * Shows statistics about the time of last visit to each course.
  575. */
  576. static function print_course_last_visit()
  577. {
  578. $access_url_rel_course_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  579. $current_url_id = api_get_current_access_url_id();
  580. $columns[0] = 'c_id';
  581. $columns[1] = 'access_date';
  582. $sql_order[SORT_ASC] = 'ASC';
  583. $sql_order[SORT_DESC] = 'DESC';
  584. $per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 10;
  585. $page_nr = isset($_GET['page_nr']) ? intval($_GET['page_nr']) : 1;
  586. $column = isset($_GET['column']) ? intval($_GET['column']) : 0;
  587. $date_diff = isset($_GET['date_diff']) ? intval($_GET['date_diff']) : 60;
  588. if (!in_array($_GET['direction'], array(SORT_ASC, SORT_DESC))) {
  589. $direction = SORT_ASC;
  590. } else {
  591. $direction = isset($_GET['direction']) ? $_GET['direction'] : SORT_ASC;
  592. }
  593. $form = new FormValidator('courselastvisit', 'get');
  594. $form->addElement('hidden', 'report', 'courselastvisit');
  595. $form->add_textfield('date_diff', get_lang('Days'), true);
  596. $form->addRule('date_diff', 'InvalidNumber', 'numeric');
  597. $form->addElement('style_submit_button', 'submit', get_lang('Search'), 'class="search"');
  598. if (!isset($_GET['date_diff'])) {
  599. $defaults['date_diff'] = 60;
  600. } else {
  601. $defaults['date_diff'] = Security::remove_XSS($_GET['date_diff']);
  602. }
  603. $form->setDefaults($defaults);
  604. $form->display();
  605. $values = $form->exportValues();
  606. $date_diff = $values['date_diff'];
  607. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  608. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  609. if (api_is_multiple_url_enabled()) {
  610. $sql = "SELECT access_date, c.code FROM $table s , $access_url_rel_course_table u, $tableCourse c
  611. WHERE c.id = u.c_id AND c.id = s.c_id AND access_url_id='".$current_url_id."' ".
  612. "GROUP BY access_cours_code ".
  613. "HAVING s.c_id <> '' ".
  614. "AND DATEDIFF( '".date('Y-m-d h:i:s')."' , access_date ) <= ".$date_diff;
  615. } else {
  616. $sql = "SELECT access_date, c.code FROM $table , $tableCourse c
  617. WHERE c_id = c.id
  618. GROUP BY c_id
  619. HAVING c_id <> ''AND
  620. DATEDIFF( '".date('Y-m-d h:i:s')."' , access_date ) <= ".$date_diff;
  621. }
  622. $res = Database::query($sql);
  623. $number_of_courses = Database::num_rows($res);
  624. $sql .= ' ORDER BY '.$columns[$column].' '.$sql_order[$direction];
  625. $from = ($page_nr - 1) * $per_page;
  626. $sql .= ' LIMIT '.$from.','.$per_page;
  627. echo '<p>'.get_lang('LastAccess').' &gt;= '.$date_diff.' '.get_lang('Days').'</p>';
  628. $res = Database::query($sql);
  629. if (Database::num_rows($res) > 0) {
  630. $courses = array();
  631. while ($obj = Database::fetch_object($res)) {
  632. $course = array();
  633. $course[] = '<a href="'.api_get_path(WEB_PATH).'courses/'.$obj->code.'">'.$obj->code.' <a>';
  634. //Allow sort by date hiding the numerical date
  635. $course[] = '<span style="display:none;">'.$obj->access_date.'</span>'.api_convert_and_format_date(
  636. $obj->access_date
  637. );
  638. $courses[] = $course;
  639. }
  640. $parameters['date_diff'] = $date_diff;
  641. $parameters['report'] = 'courselastvisit';
  642. $table_header[] = array(get_lang("CourseCode"), true);
  643. $table_header[] = array(get_lang("LastAccess"), true);
  644. Display :: display_sortable_table(
  645. $table_header,
  646. $courses,
  647. array('column' => $column, 'direction' => $direction),
  648. array(),
  649. $parameters
  650. );
  651. } else {
  652. echo get_lang('NoSearchResults');
  653. }
  654. }
  655. /**
  656. * Displays the statistics of the messages sent and received by each user in the social network
  657. * @param string Type of message: 'sent' or 'received'
  658. * @return array Message list
  659. */
  660. static function get_messages($message_type)
  661. {
  662. $message_table = Database::get_main_table(TABLE_MAIN_MESSAGE);
  663. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  664. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  665. $current_url_id = api_get_current_access_url_id();
  666. switch ($message_type) {
  667. case 'sent':
  668. $field = 'user_sender_id';
  669. break;
  670. case 'received':
  671. $field = 'user_receiver_id';
  672. break;
  673. }
  674. if (api_is_multiple_url_enabled()) {
  675. $sql = "SELECT lastname, firstname, username, COUNT($field) AS count_message ".
  676. "FROM ".$access_url_rel_user_table." as url, ".$message_table." m ".
  677. "LEFT JOIN ".$user_table." u ON m.$field = u.user_id ".
  678. "WHERE url.user_id = m.$field AND access_url_id='".$current_url_id."' ".
  679. "GROUP BY m.$field ORDER BY count_message DESC ";
  680. } else {
  681. $sql = "SELECT lastname, firstname, username, COUNT($field) AS count_message ".
  682. "FROM ".$message_table." m ".
  683. "LEFT JOIN ".$user_table." u ON m.$field = u.user_id ".
  684. "GROUP BY m.$field ORDER BY count_message DESC ";
  685. }
  686. $res = Database::query($sql);
  687. $messages_sent = array();
  688. while ($messages = Database::fetch_array($res)) {
  689. if (empty($messages['username'])) {
  690. $messages['username'] = get_lang('Unknown');
  691. }
  692. $users = api_get_person_name(
  693. $messages['firstname'],
  694. $messages['lastname']
  695. ).'<br />('.$messages['username'].')';
  696. $messages_sent[$users] = $messages['count_message'];
  697. }
  698. return $messages_sent;
  699. }
  700. /**
  701. * Count the number of friends for social network users
  702. */
  703. static function get_friends()
  704. {
  705. $user_friend_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  706. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  707. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  708. $current_url_id = api_get_current_access_url_id();
  709. if (api_is_multiple_url_enabled()) {
  710. $sql = "SELECT lastname, firstname, username, COUNT(friend_user_id) AS count_friend ".
  711. "FROM ".$access_url_rel_user_table." as url, ".$user_friend_table." uf ".
  712. "LEFT JOIN ".$user_table." u ON uf.user_id = u.user_id ".
  713. "WHERE uf.relation_type <> '".USER_RELATION_TYPE_RRHH."' AND uf.user_id = url.user_id AND access_url_id='".$current_url_id."' ".
  714. "GROUP BY uf.user_id ORDER BY count_friend DESC ";
  715. } else {
  716. $sql = "SELECT lastname, firstname, username, COUNT(friend_user_id) AS count_friend ".
  717. "FROM ".$user_friend_table." uf ".
  718. "LEFT JOIN ".$user_table." u ON uf.user_id = u.user_id ".
  719. "WHERE uf.relation_type <> '".USER_RELATION_TYPE_RRHH."' ".
  720. "GROUP BY uf.user_id ORDER BY count_friend DESC ";
  721. }
  722. $res = Database::query($sql);
  723. $list_friends = array();
  724. while ($friends = Database::fetch_array($res)) {
  725. $users = api_get_person_name(
  726. $friends['firstname'],
  727. $friends['lastname']
  728. ).'<br />('.$friends['username'].')';
  729. $list_friends[$users] = $friends['count_friend'];
  730. }
  731. return $list_friends;
  732. }
  733. /**
  734. * Print the number of users that didn't login for a certain period of time
  735. */
  736. static function print_users_not_logged_in_stats()
  737. {
  738. $total_logins = array();
  739. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  740. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  741. $current_url_id = api_get_current_access_url_id();
  742. $total = self::count_users();
  743. if (api_is_multiple_url_enabled()) {
  744. $table_url = ", $access_url_rel_user_table";
  745. $where_url = " AND login_user_id=user_id AND access_url_id='".$current_url_id."'";
  746. } else {
  747. $table_url = '';
  748. $where_url = '';
  749. }
  750. $sql[get_lang('Thisday')] =
  751. "SELECT count(distinct(login_user_id)) AS number ".
  752. " FROM $table $table_url ".
  753. " WHERE DATE_ADD(login_date, INTERVAL 1 DAY) >= NOW() $where_url";
  754. $sql[get_lang('Last7days')] =
  755. "SELECT count(distinct(login_user_id)) AS number ".
  756. " FROM $table $table_url ".
  757. " WHERE DATE_ADD(login_date, INTERVAL 7 DAY) >= NOW() $where_url";
  758. $sql[get_lang('Last31days')] =
  759. "SELECT count(distinct(login_user_id)) AS number ".
  760. " FROM $table $table_url ".
  761. " WHERE DATE_ADD(login_date, INTERVAL 31 DAY) >= NOW() $where_url";
  762. $sql[sprintf(get_lang('LastXMonths'), 6)] =
  763. "SELECT count(distinct(login_user_id)) AS number ".
  764. " FROM $table $table_url ".
  765. " WHERE DATE_ADD(login_date, INTERVAL 6 MONTH) >= NOW() $where_url";
  766. $sql[get_lang('NeverConnected')] =
  767. "SELECT count(distinct(login_user_id)) AS number ".
  768. " FROM $table $table_url WHERE 1=1 $where_url";
  769. foreach ($sql as $index => $query) {
  770. $res = Database::query($query);
  771. $obj = Database::fetch_object($res);
  772. $r = $total - $obj->number;
  773. $total_logins[$index] = $r < 0 ? 0 : $r;
  774. }
  775. Statistics::print_stats(get_lang('StatsUsersDidNotLoginInLastPeriods'), $total_logins, false);
  776. }
  777. }