users.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Student report
  5. * @package chamilo.reporting
  6. */
  7. $cidReset = true;
  8. require_once '../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.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 = $total_assignments = $total_messages = 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. $total_assignments += Tracking :: count_student_assignments($student_id, $course_code);
  114. $total_messages += Tracking :: count_student_messages($student_id, $course_code);
  115. $nb_courses_student++;
  116. }
  117. }
  118. }
  119. if ($nb_courses_student > 0) {
  120. $avg_time_spent = $avg_time_spent / $nb_courses_student;
  121. $avg_student_score = $avg_student_score / $nb_courses_student;
  122. $avg_student_progress = $avg_student_progress / $nb_courses_student;
  123. } else {
  124. $avg_time_spent = null;
  125. $avg_student_score = null;
  126. $avg_student_progress = null;
  127. }
  128. $row = array();
  129. if ($is_western_name_order) {
  130. $row[] = $student_data['firstname'];
  131. $row[] = $student_data['lastname'];
  132. } else {
  133. $row[] = $student_data['lastname'];
  134. $row[] = $student_data['firstname'];
  135. }
  136. $string_date = Tracking :: get_last_connection_date($student_id, true);
  137. $first_date = Tracking :: get_first_connection_date($student_id);
  138. $row[] = $first_date;
  139. $row[] = $string_date;
  140. if (isset($_GET['id_coach']) && intval($_GET['id_coach']) != 0) {
  141. $detailsLink = '<a href="myStudents.php?student='.$student_id.'&id_coach='.$coach_id.'&id_session='.$sessionId.'">
  142. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>';
  143. } else {
  144. $detailsLink = '<a href="myStudents.php?student='.$student_id.'">
  145. '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>';
  146. }
  147. $row[] = $detailsLink;
  148. $all_datas[] = $row;
  149. }
  150. return $all_datas;
  151. }
  152. if ($export_csv) {
  153. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  154. } else {
  155. $is_western_name_order = api_is_western_name_order();
  156. }
  157. $sort_by_first_name = api_sort_by_first_name();
  158. $actionsLeft = '';
  159. if (api_is_drh()) {
  160. $menu_items = array(
  161. Display::url(Display::return_icon('stats.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH)."auth/my_progress.php" ),
  162. Display::url(Display::return_icon('user_na.png', get_lang('Students'), array(), ICON_SIZE_MEDIUM), '#'),
  163. Display::url(Display::return_icon('teacher.png', get_lang('Trainers'), array(), ICON_SIZE_MEDIUM), 'teachers.php'),
  164. Display::url(Display::return_icon('course.png', get_lang('Courses'), array(), ICON_SIZE_MEDIUM), 'course.php'),
  165. Display::url(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_MEDIUM), 'session.php')
  166. );
  167. $nb_menu_items = count($menu_items);
  168. if ($nb_menu_items > 1) {
  169. foreach ($menu_items as $key => $item) {
  170. $actionsLeft .= $item;
  171. }
  172. }
  173. }
  174. $actionsRight = Display::url(Display::return_icon('printer.png', get_lang('Print'), array(), ICON_SIZE_MEDIUM), 'javascript: void(0);', array('onclick'=>'javascript: window.print();'));
  175. $actionsRight .= Display::url(Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), array(), ICON_SIZE_MEDIUM), api_get_self().'?export=csv&keyword='.$keyword);
  176. $toolbar = Display::toolbarAction('toolbar-user', $content = array( 0 => $actionsLeft, 1 => $actionsRight ));
  177. $table = new SortableTable(
  178. 'tracking_student',
  179. 'get_count_users',
  180. 'get_users',
  181. ($is_western_name_order xor $sort_by_first_name) ? 1 : 0,
  182. 10
  183. );
  184. $params = array(
  185. 'keyword' => $keyword,
  186. 'active' => $active,
  187. 'sleeping_days' => $sleepingDays,
  188. 'status' => $status
  189. );
  190. $table->set_additional_parameters($params);
  191. if ($is_western_name_order) {
  192. $table->set_header(0, get_lang('FirstName'), false);
  193. $table->set_header(1, get_lang('LastName'), false);
  194. } else {
  195. $table->set_header(0, get_lang('LastName'), false);
  196. $table->set_header(1, get_lang('FirstName'), false);
  197. }
  198. $table->set_header(2, get_lang('FirstLogin'), false);
  199. $table->set_header(3, get_lang('LastConnexion'), false);
  200. $table->set_header(4, get_lang('Details'), false);
  201. if ($export_csv) {
  202. if ($is_western_name_order) {
  203. $csv_header[] = array (
  204. get_lang('FirstName'),
  205. get_lang('LastName'),
  206. get_lang('FirstLogin'),
  207. get_lang('LastConnexion')
  208. );
  209. } else {
  210. $csv_header[] = array (
  211. get_lang('LastName'),
  212. get_lang('FirstName'),
  213. get_lang('FirstLogin'),
  214. get_lang('LastConnexion')
  215. );
  216. }
  217. }
  218. $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/users.php');
  219. $form->addElement('select', 'status', get_lang('Status'), array(
  220. '' => '',
  221. STUDENT => get_lang('Student'),
  222. COURSEMANAGER => get_lang('Teacher'),
  223. DRH => get_lang('DRH'))
  224. );
  225. $form = Tracking::setUserSearchForm($form);
  226. $form->setDefaults($params);
  227. // send the csv file if asked
  228. $content = $table->get_table_data();
  229. if ($export_csv) {
  230. foreach ($content as &$row) {
  231. unset($row[4]);
  232. }
  233. $csv_content = array_merge($csv_header, $content);
  234. ob_end_clean();
  235. Export :: arrayToCsv($csv_content, 'reporting_student_list');
  236. exit;
  237. } else {
  238. Display::display_header(get_lang('Users'));
  239. echo $toolbar;
  240. $page_title = get_lang('Users');
  241. echo Display::page_subheader($page_title);
  242. if (isset($active)) {
  243. if ($active) {
  244. $activeLabel = get_lang('ActiveUsers');
  245. } else {
  246. $activeLabel = get_lang('InactiveUsers');
  247. }
  248. echo Display::page_subheader2($activeLabel);
  249. }
  250. $form->display();
  251. $table->display();
  252. }
  253. Display :: display_footer();