student.php 12 KB

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