statistics.ajax.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. /**
  5. * Responses to AJAX calls.
  6. */
  7. require_once __DIR__.'/../global.inc.php';
  8. api_protect_admin_script();
  9. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  10. $sessionDuration = isset($_GET['session_duration']) ? (int) $_GET['session_duration'] : 0;
  11. $exportFormat = isset($_REQUEST['export_format']) ? $_REQUEST['export_format'] : 'csv';
  12. $operation = isset($_REQUEST['oper']) ? $_REQUEST['oper'] : false;
  13. $order = isset($_REQUEST['sord']) && in_array($_REQUEST['sord'], ['asc', 'desc']) ? $_REQUEST['sord'] : 'asc';
  14. switch ($action) {
  15. case 'get_user_session':
  16. $list = [];
  17. $urlList = UrlManager::get_url_data("url $order");
  18. $sessionUrl = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session=';
  19. $start = isset($_GET['start']) ? Database::escape_string(api_get_utc_datetime($_GET['start'])) : api_get_utc_datetime();
  20. $end = isset($_GET['end']) ? Database::escape_string(api_get_utc_datetime($_GET['end'])) : api_get_utc_datetime();
  21. if (!empty($operation)) {
  22. $list[] = [
  23. 'URL',
  24. get_lang('Session'),
  25. get_lang('Course'),
  26. get_lang('Number of users'),
  27. ];
  28. }
  29. $courseListInfo = [];
  30. foreach ($urlList as $url) {
  31. $urlId = $url['id'];
  32. $sessionList = SessionManager::get_sessions_list([], [], null, null, $urlId);
  33. foreach ($sessionList as $session) {
  34. $sessionId = $session['id'];
  35. $row = [];
  36. $row['url'] = $url['url'];
  37. $row['session'] = Display::url(
  38. $session['name'],
  39. $sessionUrl.$sessionId
  40. );
  41. if (!empty($operation)) {
  42. $row['session'] = strip_tags($row['session']);
  43. }
  44. $courseList = SessionManager::getCoursesInSession($sessionId);
  45. $courseTitleList = [];
  46. $courseListInString = '';
  47. foreach ($courseList as $courseId) {
  48. if (!isset($courseListInfo[$courseId])) {
  49. $courseListInfo[$courseId] = $courseInfo = api_get_course_info_by_id($courseId);
  50. } else {
  51. $courseInfo = $courseListInfo[$courseId];
  52. }
  53. $courseTitleList[] = $courseInfo['title'];
  54. }
  55. $courseListInString = implode(', ', $courseTitleList);
  56. $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  57. $sql = "SELECT
  58. count(DISTINCT user_id) count
  59. FROM $table
  60. WHERE
  61. relation_type = 0 AND
  62. registered_at >= '$start' AND
  63. registered_at <= '$end' AND
  64. session_id = '$sessionId' ";
  65. $result = Database::query($sql);
  66. $result = Database::fetch_array($result);
  67. $row['course'] = $courseListInString;
  68. $row['count'] = $result['count'];
  69. $list[] = $row;
  70. }
  71. }
  72. if (!empty($operation)) {
  73. $fileName = !empty($action) ? api_get_setting('siteName').
  74. '_'.get_lang('Portal user session stats').'_'.api_get_local_time() : 'report';
  75. switch ($exportFormat) {
  76. case 'xls':
  77. Export::arrayToXls($list, $fileName);
  78. break;
  79. case 'xls_html':
  80. //TODO add date if exists
  81. $browser = new Browser();
  82. if ($browser->getPlatform() == Browser::PLATFORM_WINDOWS) {
  83. Export::export_table_xls_html($list, $fileName, 'ISO-8859-15');
  84. } else {
  85. Export::export_table_xls_html($list, $fileName);
  86. }
  87. break;
  88. case 'csv':
  89. default:
  90. Export::arrayToCsv($list, $fileName);
  91. break;
  92. }
  93. }
  94. echo json_encode($list);
  95. break;
  96. case 'recent_logins':
  97. // Give a JSON array to the stats page main/admin/statistics/index.php
  98. // for global recent logins
  99. header('Content-type: application/json');
  100. $list = [];
  101. $all = Statistics::getRecentLoginStats(false, $sessionDuration);
  102. foreach ($all as $tick => $tock) {
  103. $list['labels'][] = $tick;
  104. }
  105. $list['datasets'][0]['label'] = get_lang('Logins');
  106. $list['datasets'][0]['backgroundColor'] = 'rgba(151,187,205,0.2)';
  107. $list['datasets'][0]['borderColor'] = 'rgba(151,187,205,1)';
  108. $list['datasets'][0]['pointBackgroundColor'] = 'rgba(151,187,205,1)';
  109. $list['datasets'][0]['pointBorderColor'] = '#fff';
  110. $list['datasets'][0]['pointHoverBackgroundColor'] = '#fff';
  111. $list['datasets'][0]['pointHoverBorderColor'] = 'rgba(151,187,205,1)';
  112. foreach ($all as $tick => $tock) {
  113. $list['datasets'][0]['data'][] = $tock;
  114. }
  115. $list['datasets'][1]['label'] = get_lang('Distinct users logins');
  116. $list['datasets'][1]['backgroundColor'] = 'rgba(0,204,0,0.2)';
  117. $list['datasets'][1]['borderColor'] = 'rgba(0,204,0,1)';
  118. $list['datasets'][1]['pointBackgroundColor'] = 'rgba(0,204,0,1)';
  119. $list['datasets'][1]['pointBorderColor'] = '#fff';
  120. $list['datasets'][1]['pointHoverBackgroundColor'] = '#fff';
  121. $list['datasets'][1]['pointHoverBorderColor'] = 'rgba(0,204,0,1)';
  122. $distinct = Statistics::getRecentLoginStats(true, $sessionDuration);
  123. foreach ($distinct as $tick => $tock) {
  124. $list['datasets'][1]['data'][] = $tock;
  125. }
  126. echo json_encode($list);
  127. break;
  128. case 'tools_usage':
  129. case 'courses':
  130. case 'courses_by_language':
  131. case 'users':
  132. case 'users_teachers':
  133. case 'users_students':
  134. // Give a JSON array to the stats page main/admin/statistics/index.php
  135. // for global tools usage (number of clicks)
  136. header('Content-type: application/json');
  137. $list = [];
  138. $palette = ChamiloApi::getColorPalette(true, true);
  139. if ($action == 'tools_usage') {
  140. $statsName = 'Tools';
  141. $all = Statistics::getToolsStats();
  142. } elseif ($action == 'courses') {
  143. $statsName = 'CountCours';
  144. $course_categories = Statistics::getCourseCategories();
  145. // total amount of courses
  146. $all = [];
  147. foreach ($course_categories as $code => $name) {
  148. $all[$name] = Statistics::countCourses($code);
  149. }
  150. } elseif ($action == 'courses_by_language') {
  151. $statsName = 'CountCourseByLanguage';
  152. $all = Statistics::printCourseByLanguageStats();
  153. // use slightly different colors than previous chart
  154. for ($k = 0; $k < 3; $k++) {
  155. $item = array_shift($palette);
  156. array_push($palette, $item);
  157. }
  158. } elseif ($action == 'users') {
  159. $statsName = 'NumberOfUsers';
  160. $countInvisible = isset($_GET['count_invisible']) ? (int) $_GET['count_invisible'] : null;
  161. $all = [
  162. get_lang('Trainers') => Statistics::countUsers(COURSEMANAGER, null, $countInvisible),
  163. get_lang('Learners') => Statistics::countUsers(STUDENT, null, $countInvisible),
  164. ];
  165. } elseif ($action == 'users_teachers') {
  166. $statsName = 'Teachers';
  167. $course_categories = Statistics::getCourseCategories();
  168. $countInvisible = isset($_GET['count_invisible']) ? (int) $_GET['count_invisible'] : null;
  169. $all = [];
  170. foreach ($course_categories as $code => $name) {
  171. $name = str_replace(get_lang('Department'), "", $name);
  172. $all[$name] = Statistics::countUsers(COURSEMANAGER, $code, $countInvisible);
  173. }
  174. // use slightly different colors than previous chart
  175. for ($k = 0; $k < 3; $k++) {
  176. $item = array_shift($palette);
  177. array_push($palette, $item);
  178. }
  179. } elseif ($action == 'users_students') {
  180. $statsName = 'Students';
  181. $course_categories = Statistics::getCourseCategories();
  182. $countInvisible = isset($_GET['count_invisible']) ? (int) $_GET['count_invisible'] : null;
  183. $all = [];
  184. foreach ($course_categories as $code => $name) {
  185. $name = str_replace(get_lang('Department'), "", $name);
  186. $all[$name] = Statistics::countUsers(STUDENT, $code, $countInvisible);
  187. }
  188. // use slightly different colors than previous chart
  189. for ($k = 0; $k < 6; $k++) {
  190. $item = array_shift($palette);
  191. array_push($palette, $item);
  192. }
  193. }
  194. foreach ($all as $tick => $tock) {
  195. $list['labels'][] = $tick;
  196. }
  197. $list['datasets'][0]['label'] = get_lang($statsName);
  198. $list['datasets'][0]['borderColor'] = 'rgba(255,255,255,1)';
  199. $i = 0;
  200. foreach ($all as $tick => $tock) {
  201. $j = $i % count($palette);
  202. $list['datasets'][0]['data'][] = $tock;
  203. $list['datasets'][0]['backgroundColor'][] = $palette[$j];
  204. $i++;
  205. }
  206. echo json_encode($list);
  207. break;
  208. }