users.php 9.9 KB

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