users.php 12 KB

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