users.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Report on users followed (filtered by status given in URL).
  5. *
  6. * @package chamilo.reporting
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. api_block_anonymous_users();
  11. $allowToTrack = api_is_platform_admin(true, true) ||
  12. api_is_teacher() ||
  13. api_is_student_boss();
  14. if (!$allowToTrack) {
  15. api_not_allowed(true);
  16. }
  17. $nameTools = get_lang('Users');
  18. $export_csv = isset($_GET['export']) && $_GET['export'] == 'csv' ? true : false;
  19. $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
  20. $active = isset($_GET['active']) ? intval($_GET['active']) : 1;
  21. $sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
  22. $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
  23. $this_section = SECTION_TRACKING;
  24. $interbreadcrumb[] = [
  25. "url" => "index.php",
  26. "name" => get_lang('MySpace'),
  27. ];
  28. if (isset($_GET["user_id"]) && $_GET["user_id"] != "" && !isset($_GET["type"])) {
  29. $interbreadcrumb[] = [
  30. "url" => "teachers.php",
  31. "name" => get_lang('Teachers'),
  32. ];
  33. }
  34. if (isset($_GET["user_id"]) && $_GET["user_id"] != "" && isset($_GET["type"]) && $_GET["type"] == "coach") {
  35. $interbreadcrumb[] = ["url" => "coaches.php", "name" => get_lang('Tutors')];
  36. }
  37. function get_count_users()
  38. {
  39. $sleepingDays = isset($_GET['sleeping_days']) ? (int) $_GET['sleeping_days'] : null;
  40. $active = isset($_GET['active']) ? (int) $_GET['active'] : 1;
  41. $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
  42. $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
  43. $lastConnectionDate = null;
  44. if (!empty($sleepingDays)) {
  45. $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago'));
  46. }
  47. return SessionManager::getCountUserTracking(
  48. $keyword,
  49. $active,
  50. $lastConnectionDate,
  51. null,
  52. null,
  53. $status
  54. );
  55. }
  56. function get_users($from, $limit, $column, $direction)
  57. {
  58. $active = isset($_GET['active']) ? $_GET['active'] : 1;
  59. $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
  60. $sleepingDays = isset($_GET['sleeping_days']) ? (int) $_GET['sleeping_days'] : null;
  61. $sessionId = isset($_GET['id_session']) ? (int) $_GET['id_session'] : 0;
  62. $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
  63. $lastConnectionDate = null;
  64. if (!empty($sleepingDays)) {
  65. $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago'));
  66. }
  67. $is_western_name_order = api_is_western_name_order();
  68. $coach_id = api_get_user_id();
  69. $drhLoaded = false;
  70. if (api_is_drh()) {
  71. if (api_drh_can_access_all_session_content()) {
  72. $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  73. 'drh_all',
  74. api_get_user_id(),
  75. false,
  76. $from,
  77. $limit,
  78. '',
  79. $direction,
  80. $keyword,
  81. $active,
  82. $lastConnectionDate,
  83. null,
  84. null,
  85. $status
  86. );
  87. $drhLoaded = true;
  88. }
  89. }
  90. if ($drhLoaded === false) {
  91. $students = UserManager::getUsersFollowedByUser(
  92. api_get_user_id(),
  93. $status,
  94. false,
  95. false,
  96. false,
  97. $from,
  98. $limit,
  99. '',
  100. $direction,
  101. $active,
  102. $lastConnectionDate,
  103. COURSEMANAGER,
  104. $keyword
  105. );
  106. }
  107. $all_datas = [];
  108. foreach ($students as $student_data) {
  109. $student_id = $student_data['user_id'];
  110. if (isset($_GET['id_session'])) {
  111. $courses = Tracking :: get_course_list_in_session_from_student($student_id, $sessionId);
  112. }
  113. $avg_time_spent = $avg_student_score = $avg_student_progress = 0;
  114. $nb_courses_student = 0;
  115. if (!empty($courses)) {
  116. foreach ($courses as $course_code) {
  117. $courseInfo = api_get_course_info($course_code);
  118. $courseId = $courseInfo['real_id'];
  119. if (CourseManager::is_user_subscribed_in_course($student_id, $course_code, true)) {
  120. $avg_time_spent += Tracking::get_time_spent_on_the_course($student_id, $courseId, $_GET['id_session']);
  121. $my_average = Tracking::get_avg_student_score($student_id, $course_code);
  122. if (is_numeric($my_average)) {
  123. $avg_student_score += $my_average;
  124. }
  125. $avg_student_progress += Tracking::get_avg_student_progress($student_id, $course_code);
  126. $nb_courses_student++;
  127. }
  128. }
  129. }
  130. if ($nb_courses_student > 0) {
  131. $avg_time_spent = $avg_time_spent / $nb_courses_student;
  132. $avg_student_score = $avg_student_score / $nb_courses_student;
  133. $avg_student_progress = $avg_student_progress / $nb_courses_student;
  134. } else {
  135. $avg_time_spent = null;
  136. $avg_student_score = null;
  137. $avg_student_progress = null;
  138. }
  139. $row = [];
  140. if ($is_western_name_order) {
  141. $row[] = $student_data['firstname'];
  142. $row[] = $student_data['lastname'];
  143. } else {
  144. $row[] = $student_data['lastname'];
  145. $row[] = $student_data['firstname'];
  146. }
  147. $string_date = Tracking::get_last_connection_date($student_id, true);
  148. $first_date = Tracking::get_first_connection_date($student_id);
  149. $row[] = $first_date;
  150. $row[] = $string_date;
  151. if (isset($_GET['id_coach']) && intval($_GET['id_coach']) != 0) {
  152. $detailsLink = '<a href="myStudents.php?student='.$student_id.'&id_coach='.$coach_id.'&id_session='.$sessionId.'">
  153. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>';
  154. } else {
  155. $detailsLink = '<a href="myStudents.php?student='.$student_id.'">
  156. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>';
  157. }
  158. $row[] = $detailsLink;
  159. $all_datas[] = $row;
  160. }
  161. return $all_datas;
  162. }
  163. if ($export_csv) {
  164. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  165. } else {
  166. $is_western_name_order = api_is_western_name_order();
  167. }
  168. $sort_by_first_name = api_sort_by_first_name();
  169. $actionsLeft = '';
  170. if (api_is_drh()) {
  171. $menu_items = [
  172. Display::url(
  173. Display::return_icon('statistics.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM),
  174. api_get_path(WEB_CODE_PATH)."auth/my_progress.php"
  175. ),
  176. Display::url(
  177. Display::return_icon('user_na.png', get_lang('Students'), [], ICON_SIZE_MEDIUM),
  178. '#'
  179. ),
  180. Display::url(
  181. Display::return_icon('teacher.png', get_lang('Trainers'), [], ICON_SIZE_MEDIUM),
  182. 'teachers.php'
  183. ),
  184. Display::url(
  185. Display::return_icon('course.png', get_lang('Courses'), [], ICON_SIZE_MEDIUM),
  186. 'course.php'
  187. ),
  188. Display::url(
  189. Display::return_icon('session.png', get_lang('Sessions'), [], ICON_SIZE_MEDIUM),
  190. 'session.php'
  191. ),
  192. Display::url(
  193. Display::return_icon('skills.png', get_lang('Skills'), [], ICON_SIZE_MEDIUM),
  194. 'skills.php'
  195. ),
  196. ];
  197. $nb_menu_items = count($menu_items);
  198. if ($nb_menu_items > 1) {
  199. foreach ($menu_items as $key => $item) {
  200. $actionsLeft .= $item;
  201. }
  202. }
  203. } elseif (api_is_student_boss()) {
  204. $actionsLeft .= Display::url(
  205. Display::return_icon('statistics.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM),
  206. api_get_path(WEB_CODE_PATH)."auth/my_progress.php"
  207. );
  208. $actionsLeft .= Display::url(
  209. Display::return_icon('user_na.png', get_lang('Students'), [], ICON_SIZE_MEDIUM),
  210. '#'
  211. );
  212. $actionsLeft .= Display::url(
  213. Display::return_icon("statistics.png", get_lang("CompanyReport"), [], ICON_SIZE_MEDIUM),
  214. api_get_path(WEB_CODE_PATH)."mySpace/company_reports.php"
  215. );
  216. $actionsLeft .= Display::url(
  217. Display::return_icon(
  218. "certificate_list.png",
  219. get_lang("GradebookSeeListOfStudentsCertificates"),
  220. [],
  221. ICON_SIZE_MEDIUM
  222. ),
  223. api_get_path(WEB_CODE_PATH)."gradebook/certificate_report.php"
  224. );
  225. }
  226. $actionsRight = Display::url(
  227. Display::return_icon('printer.png', get_lang('Print'), [], ICON_SIZE_MEDIUM),
  228. 'javascript: void(0);',
  229. ['onclick' => 'javascript: window.print();']
  230. );
  231. $actionsRight .= Display::url(
  232. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], ICON_SIZE_MEDIUM),
  233. api_get_self().'?export=csv&keyword='.$keyword
  234. );
  235. $toolbar = Display::toolbarAction('toolbar-user', [$actionsLeft, $actionsRight]);
  236. $table = new SortableTable(
  237. 'tracking_student',
  238. 'get_count_users',
  239. 'get_users',
  240. ($is_western_name_order xor $sort_by_first_name) ? 1 : 0,
  241. 10
  242. );
  243. $params = [
  244. 'keyword' => $keyword,
  245. 'active' => $active,
  246. 'sleeping_days' => $sleepingDays,
  247. 'status' => $status,
  248. ];
  249. $table->set_additional_parameters($params);
  250. if ($is_western_name_order) {
  251. $table->set_header(0, get_lang('FirstName'), false);
  252. $table->set_header(1, get_lang('LastName'), false);
  253. } else {
  254. $table->set_header(0, get_lang('LastName'), false);
  255. $table->set_header(1, get_lang('FirstName'), false);
  256. }
  257. $table->set_header(2, get_lang('FirstLogin'), false);
  258. $table->set_header(3, get_lang('LastConnexion'), false);
  259. $table->set_header(4, get_lang('Details'), false);
  260. if ($export_csv) {
  261. if ($is_western_name_order) {
  262. $csv_header[] = [
  263. get_lang('FirstName'),
  264. get_lang('LastName'),
  265. get_lang('FirstLogin'),
  266. get_lang('LastConnexion'),
  267. ];
  268. } else {
  269. $csv_header[] = [
  270. get_lang('LastName'),
  271. get_lang('FirstName'),
  272. get_lang('FirstLogin'),
  273. get_lang('LastConnexion'),
  274. ];
  275. }
  276. }
  277. $form = new FormValidator(
  278. 'search_user',
  279. 'get',
  280. api_get_path(WEB_CODE_PATH).'mySpace/users.php'
  281. );
  282. $form->addElement(
  283. 'select',
  284. 'status',
  285. get_lang('Status'),
  286. [
  287. '' => '',
  288. STUDENT => get_lang('Student'),
  289. COURSEMANAGER => get_lang('Teacher'),
  290. DRH => get_lang('DRH'),
  291. ]
  292. );
  293. $form = Tracking::setUserSearchForm($form);
  294. $form->setDefaults($params);
  295. if ($export_csv) {
  296. // send the csv file if asked
  297. $content = $table->get_table_data();
  298. foreach ($content as &$row) {
  299. unset($row[4]);
  300. }
  301. $csv_content = array_merge($csv_header, $content);
  302. ob_end_clean();
  303. Export::arrayToCsv($csv_content, 'reporting_student_list');
  304. exit;
  305. } else {
  306. Display::display_header($nameTools);
  307. echo $toolbar;
  308. echo Display::page_subheader($nameTools);
  309. if (isset($active)) {
  310. if ($active) {
  311. $activeLabel = get_lang('ActiveUsers');
  312. } else {
  313. $activeLabel = get_lang('InactiveUsers');
  314. }
  315. echo Display::page_subheader2($activeLabel);
  316. }
  317. $form->display();
  318. $table->display();
  319. }
  320. Display::display_footer();