student.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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('Learners');
  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. $webCodePath = api_get_path(WEB_CODE_PATH);
  24. $interbreadcrumb[] = [
  25. "url" => api_is_student_boss() ? "#" : "index.php",
  26. "name" => get_lang('Reporting'),
  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('Coaches')];
  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. $lastConnectionDate = null;
  43. if (!empty($sleepingDays)) {
  44. $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago'));
  45. }
  46. return SessionManager::getCountUserTracking(
  47. $keyword,
  48. $active,
  49. $lastConnectionDate,
  50. null,
  51. null,
  52. api_is_student_boss() ? null : STUDENT
  53. );
  54. }
  55. function get_users($from, $limit, $column, $direction)
  56. {
  57. global $export_csv;
  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. $webCodePath = api_get_path(WEB_CODE_PATH);
  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. $column = 'u.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. $column,
  80. $direction,
  81. $keyword,
  82. $active,
  83. $lastConnectionDate,
  84. null,
  85. null,
  86. api_is_student_boss() ? null : STUDENT
  87. );
  88. $drhLoaded = true;
  89. }
  90. }
  91. if ($drhLoaded === false) {
  92. $students = UserManager::getUsersFollowedByUser(
  93. api_get_user_id(),
  94. api_is_student_boss() ? null : STUDENT,
  95. false,
  96. false,
  97. false,
  98. $from,
  99. $limit,
  100. $column,
  101. $direction,
  102. $active,
  103. $lastConnectionDate,
  104. api_is_student_boss() ? STUDENT_BOSS : COURSEMANAGER,
  105. $keyword
  106. );
  107. }
  108. $url = $webCodePath.'mySpace/myStudents.php';
  109. $all_datas = [];
  110. foreach ($students as $student_data) {
  111. $student_id = $student_data['user_id'];
  112. $student_data = api_get_user_info($student_id);
  113. if (isset($_GET['id_session'])) {
  114. $courses = Tracking :: get_course_list_in_session_from_student($student_id, $sessionId);
  115. }
  116. $avg_time_spent = $avg_student_score = $avg_student_progress = 0;
  117. $nb_courses_student = 0;
  118. if (!empty($courses)) {
  119. foreach ($courses as $course_code) {
  120. $courseInfo = api_get_course_info($course_code);
  121. $courseId = $courseInfo['real_id'];
  122. if (CourseManager::is_user_subscribed_in_course($student_id, $course_code, true)) {
  123. $avg_time_spent += Tracking::get_time_spent_on_the_course(
  124. $student_id,
  125. $courseId,
  126. $_GET['id_session']
  127. );
  128. $my_average = Tracking::get_avg_student_score($student_id, $course_code);
  129. if (is_numeric($my_average)) {
  130. $avg_student_score += $my_average;
  131. }
  132. $avg_student_progress += Tracking::get_avg_student_progress($student_id, $course_code);
  133. $nb_courses_student++;
  134. }
  135. }
  136. }
  137. if ($nb_courses_student > 0) {
  138. $avg_time_spent = $avg_time_spent / $nb_courses_student;
  139. $avg_student_score = $avg_student_score / $nb_courses_student;
  140. $avg_student_progress = $avg_student_progress / $nb_courses_student;
  141. } else {
  142. $avg_time_spent = null;
  143. $avg_student_score = null;
  144. $avg_student_progress = null;
  145. }
  146. $urlDetails = $url."?student=$student_id";
  147. if (isset($_GET['id_coach']) && intval($_GET['id_coach']) != 0) {
  148. $urlDetails = $url."?student=$student_id&id_coach=$coach_id&id_session=$sessionId";
  149. }
  150. $row = [];
  151. if ($is_western_name_order) {
  152. $first = Display::url($student_data['firstname'], $urlDetails);
  153. $last = Display::url($student_data['lastname'], $urlDetails);
  154. } else {
  155. $first = Display::url($student_data['lastname'], $urlDetails);
  156. $last = Display::url($student_data['firstname'], $urlDetails);
  157. }
  158. if ($export_csv) {
  159. $row[] = strip_tags($first);
  160. $row[] = strip_tags($last);
  161. } else {
  162. $row[] = $first;
  163. $row[] = $last;
  164. }
  165. $string_date = Tracking::get_last_connection_date($student_id, true);
  166. $first_date = Tracking::get_first_connection_date($student_id);
  167. $row[] = $first_date;
  168. $row[] = $string_date;
  169. $detailsLink = Display::url(
  170. Display::return_icon('2rightarrow.png', get_lang('Details').' '.$student_data['username']),
  171. $urlDetails,
  172. ['id' => 'details_'.$student_data['username']]
  173. );
  174. $lostPasswordLink = '';
  175. if (api_is_drh() || api_is_platform_admin()) {
  176. $lostPasswordLink = '&nbsp;'.Display::url(
  177. Display::return_icon('edit.png', get_lang('Edit')),
  178. $webCodePath.'mySpace/user_edit.php?user_id='.$student_id
  179. );
  180. }
  181. $row[] = $lostPasswordLink.$detailsLink;
  182. $all_datas[] = $row;
  183. }
  184. return $all_datas;
  185. }
  186. if ($export_csv) {
  187. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  188. } else {
  189. $is_western_name_order = api_is_western_name_order();
  190. }
  191. $sort_by_first_name = api_sort_by_first_name();
  192. $actionsLeft = '';
  193. if (api_is_drh()) {
  194. $menu_items = [
  195. Display::url(
  196. Display::return_icon('statistics.png', get_lang('View my progress'), '', ICON_SIZE_MEDIUM),
  197. $webCodePath.'auth/my_progress.php'
  198. ),
  199. Display::url(
  200. Display::return_icon('user_na.png', get_lang('Learners'), [], ICON_SIZE_MEDIUM),
  201. '#'
  202. ),
  203. Display::url(
  204. Display::return_icon('teacher.png', get_lang('Teachers'), [], ICON_SIZE_MEDIUM),
  205. 'teachers.php'
  206. ),
  207. Display::url(
  208. Display::return_icon('course.png', get_lang('Courses'), [], ICON_SIZE_MEDIUM),
  209. 'course.php'
  210. ),
  211. Display::url(
  212. Display::return_icon('session.png', get_lang('Course sessions'), [], ICON_SIZE_MEDIUM),
  213. 'session.php'
  214. ),
  215. Display::url(
  216. Display::return_icon('skills.png', get_lang('Skills'), [], ICON_SIZE_MEDIUM),
  217. $webCodePath.'social/my_skills_report.php'
  218. ),
  219. ];
  220. $actionsLeft .= implode('', $menu_items);
  221. } elseif (api_is_student_boss()) {
  222. $actionsLeft .= Display::url(
  223. Display::return_icon('statistics.png', get_lang('View my progress'), '', ICON_SIZE_MEDIUM),
  224. $webCodePath.'auth/my_progress.php'
  225. );
  226. $actionsLeft .= Display::url(
  227. Display::return_icon('user_na.png', get_lang('Learners'), [], ICON_SIZE_MEDIUM),
  228. '#'
  229. );
  230. $actionsLeft .= Display::url(
  231. Display::return_icon('skills.png', get_lang('Skills'), [], ICON_SIZE_MEDIUM),
  232. $webCodePath.'social/my_skills_report.php'
  233. );
  234. $actionsLeft .= Display::url(
  235. Display::return_icon('statistics.png', get_lang('Corporate report'), [], ICON_SIZE_MEDIUM),
  236. $webCodePath.'mySpace/company_reports.php'
  237. );
  238. $actionsLeft .= Display::url(
  239. Display::return_icon(
  240. 'certificate_list.png',
  241. get_lang('GradebookSeeListOfLearnersCertificates'),
  242. [],
  243. ICON_SIZE_MEDIUM
  244. ),
  245. $webCodePath.'gradebook/certificate_report.php'
  246. );
  247. }
  248. $actionsRight = Display::url(
  249. Display::return_icon('printer.png', get_lang('Print'), [], ICON_SIZE_MEDIUM),
  250. 'javascript: void(0);',
  251. ['onclick' => 'javascript: window.print();']
  252. );
  253. $actionsRight .= Display::url(
  254. Display::return_icon('export_csv.png', get_lang('CSV export'), [], ICON_SIZE_MEDIUM),
  255. api_get_self().'?export=csv&keyword='.$keyword
  256. );
  257. $toolbar = Display::toolbarAction('toolbar-student', [$actionsLeft, $actionsRight]);
  258. $table = new SortableTable(
  259. 'tracking_student',
  260. 'get_count_users',
  261. 'get_users',
  262. ($is_western_name_order xor $sort_by_first_name) ? 1 : 0,
  263. 10
  264. );
  265. $params = [
  266. 'keyword' => $keyword,
  267. 'active' => $active,
  268. 'sleeping_days' => $sleepingDays,
  269. ];
  270. $table->set_additional_parameters($params);
  271. if ($is_western_name_order) {
  272. $table->set_header(0, get_lang('First name'), false);
  273. $table->set_header(1, get_lang('Last name'), false);
  274. } else {
  275. $table->set_header(0, get_lang('Last name'), false);
  276. $table->set_header(1, get_lang('First name'), false);
  277. }
  278. $table->set_header(2, get_lang('First connection'), false);
  279. $table->set_header(3, get_lang('Latest login'), false);
  280. $table->set_header(4, get_lang('Details'), false);
  281. if ($export_csv) {
  282. if ($is_western_name_order) {
  283. $csv_header[] = [
  284. get_lang('First name'),
  285. get_lang('Last name'),
  286. get_lang('First connection'),
  287. get_lang('Latest login'),
  288. ];
  289. } else {
  290. $csv_header[] = [
  291. get_lang('Last name'),
  292. get_lang('First name'),
  293. get_lang('First connection'),
  294. get_lang('Latest login'),
  295. ];
  296. }
  297. }
  298. $form = new FormValidator(
  299. 'search_user',
  300. 'get',
  301. $webCodePath.'mySpace/student.php'
  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('Users with an active account');
  322. } else {
  323. $activeLabel = get_lang('Users who\'s account has been disabled');
  324. }
  325. echo Display::page_subheader2($activeLabel);
  326. }
  327. $form->display();
  328. $table->display();
  329. }
  330. Display::display_footer();