teachers_time_by_session_report.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Generate a teacher time report in platform by session only
  5. * @package chamilo.admin
  6. */
  7. use \Chamilo\CoreBundle\Entity\Session;
  8. use \Doctrine\Common\Collections\Criteria;
  9. $cidReset = true;
  10. require_once '../inc/global.inc.php';
  11. require_once api_get_path(SYS_CODE_PATH) . 'work/work.lib.php';
  12. if (!api_is_platform_admin(true) && !api_is_teacher()) {
  13. api_not_allowed(true);
  14. }
  15. $toolName = get_lang('TeacherTimeReportBySession');
  16. $em = Database::getManager();
  17. $sessionsInfo = api_is_platform_admin()
  18. ? SessionManager::get_sessions_list()
  19. : Tracking::get_sessions_coached_by_user(api_get_user_id());
  20. $session = null;
  21. $form = new FormValidator('teacher_time_report_by_session', 'GET');
  22. $selectSession = $form->addSelect('session', get_lang('Session'), [0 => get_lang('None')]);
  23. $form->addButtonFilter(get_lang('Filter'));
  24. foreach ($sessionsInfo as $sessionInfo) {
  25. $selectSession->addOption($sessionInfo['name'], $sessionInfo['id']);
  26. }
  27. if (isset($_GET['session']) && intval($_GET['session'])) {
  28. $form->setDefaults(['session' => intval($_GET['session'])]);
  29. $session = $em->find('ChamiloCoreBundle:Session', intval($_GET['session']));
  30. }
  31. $data = [];
  32. $coursesInfo = [];
  33. $usersInfo = [];
  34. if ($session) {
  35. $sessionCourses = $session->getCourses();
  36. foreach ($sessionCourses as $sessionCourse) {
  37. $course = $sessionCourse->getCourse();
  38. $coursesInfo[$course->getId()] = $course->getCode();
  39. $criteria = Criteria::create()->where(
  40. Criteria::expr()->eq("status", Session::COACH)
  41. );
  42. $userCourseSubscriptions = $session
  43. ->getUserCourseSubscriptions()
  44. ->matching($criteria);
  45. foreach ($userCourseSubscriptions as $userCourseSubscription) {
  46. $user = $userCourseSubscription->getUser();
  47. if (!array_key_exists($user->getId(), $usersInfo)) {
  48. $usersInfo[$user->getId()] = [
  49. 'code' => $user->getOfficialCode(),
  50. 'complete_name' => $user->getCompleteName(),
  51. 'time_in_platform' => api_time_to_hms(
  52. Tracking::get_time_spent_on_the_platform($user->getId(), 'ever')
  53. ),
  54. 'first_connection' => Tracking::get_first_connection_date($user->getId()),
  55. 'last_connection' => Tracking::get_last_connection_date($user->getId()),
  56. ];
  57. }
  58. $usersInfo[$user->getId()][$course->getId() . '_number_of_students'] = null;
  59. $usersInfo[$user->getId()][$course->getId() . '_number_of_works'] = null;
  60. $usersInfo[$user->getId()][$course->getId() . '_last_work'] = null;
  61. $usersInfo[$user->getId()][$course->getId() . '_time_spent_of_course'] = null;
  62. if (!$session->hasCoachInCourseWithStatus($user, $course)) {
  63. continue;
  64. }
  65. $works = $em
  66. ->getRepository('ChamiloCourseBundle:CStudentPublication')
  67. ->findByTeacher($user, $course, $session->getId());
  68. $usersInfo[$user->getId()][$course->getId() . '_number_of_students'] = $sessionCourse->getNbrUsers();
  69. $usersInfo[$user->getId()][$course->getId() . '_number_of_works'] = count($works);
  70. $usersInfo[$user->getId()][$course->getId() . '_time_spent_of_course'] = api_time_to_hms(
  71. Tracking::get_time_spent_on_the_course($user->getId(), $course->getId(), $session->getId())
  72. );
  73. $lastWork = array_pop($works);
  74. if (!$lastWork) {
  75. continue;
  76. }
  77. $lastFormattedDate = api_format_date($lastWork->getSentDate()->getTimestamp(), DATE_TIME_FORMAT_SHORT);
  78. $usersInfo[$user->getId()][$course->getId() . '_last_work'] = api_format_date(
  79. $lastWork->getSentDate()->getTimestamp(),
  80. DATE_TIME_FORMAT_SHORT
  81. );
  82. }
  83. }
  84. }
  85. if (isset($_GET['export']) && $session && ($coursesInfo && $usersInfo)) {
  86. $fileName = get_lang('TeacherTimeReport') . ' ' . api_get_local_time();
  87. $dataToExport = [];
  88. $dataToExport[] = [$toolName, $session->getName()];
  89. $dataToExport['headers'] = [
  90. get_lang('OfficialCode'),
  91. get_lang('CoachName'),
  92. get_lang('TimeSpentOnThePlatform'),
  93. get_lang('FirstLoginInPlatform'),
  94. get_lang('LatestLoginInPlatform'),
  95. ];
  96. foreach ($coursesInfo as $courseCode) {
  97. $dataToExport['headers'][] = $courseCode;
  98. $dataToExport['headers'][] = get_lang('NumberOfWorks');
  99. $dataToExport['headers'][] = get_lang('LastWork');
  100. $dataToExport['headers'][] = sprintf(get_lang('TimeReportForCourseX'), $courseCode);
  101. }
  102. foreach ($usersInfo as $user) {
  103. $dataToExport[] = $user;
  104. }
  105. foreach ($data as $row) {
  106. $contents = [
  107. $row['code'],
  108. $row['complete_name'],
  109. $row['time_in_platform'],
  110. $row['first_connection'],
  111. $row['last_connection']
  112. ];
  113. foreach ($row['courses'] as $course) {
  114. $headers[] = $course['code'];
  115. $headers[] = get_lang('NumberOfWorks');
  116. $headers[] = get_lang('LastWork');
  117. $headers[] = sprintf(get_lang('TimeReportForCourseX'), $course['code']);
  118. $contents[] = $course['number_of_students'];
  119. $contents[] = $course['number_of_works'];
  120. $contents[] = $course['last_work'];
  121. $contents[] = $course['time_spent_of_course'];
  122. }
  123. $dataToExport[] = [get_lang('Session'), $session->getName()];
  124. $dataToExport[] = $headers;
  125. $dataToExport[] = $contents;
  126. }
  127. switch ($_GET['export']) {
  128. case 'xls':
  129. Export::export_table_xls_html($dataToExport, $fileName);
  130. break;
  131. case 'csv':
  132. Export::arrayToCsv($dataToExport, $fileName);
  133. break;
  134. }
  135. exit;
  136. }
  137. $this_section = SECTION_PLATFORM_ADMIN;
  138. $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH) . 'mySpace/', 'name' => get_lang('Reporting')];
  139. $interbreadcrumb[] = [
  140. 'url' => api_get_path(WEB_CODE_PATH) . 'mySpace/session.php',
  141. 'name' => get_lang('FollowedSessions')
  142. ];
  143. $actions = [];
  144. if ($session) {
  145. $actions = [
  146. Display::url(
  147. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], ICON_SIZE_MEDIUM),
  148. api_get_self() . '?' . http_build_query(['export' => 'csv', 'session' => $session->getId()])
  149. ),
  150. Display::url(
  151. Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), [], ICON_SIZE_MEDIUM),
  152. api_get_self() . '?' . http_build_query(['export' => 'xls', 'session' => $session->getId()])
  153. )
  154. ];
  155. }
  156. $view = new Template($toolName);
  157. $view->assign('form', $form->returnForm());
  158. if ($session) {
  159. $view->assign('session', ['id' => $session->getId(), 'name' => $session->getName()]);
  160. $view->assign('courses', $coursesInfo);
  161. $view->assign('users', $usersInfo);
  162. }
  163. $template = $view->get_template('admin/teachers_time_by_session_report.tpl');
  164. $content = $view->fetch($template);
  165. $view->assign('header', $toolName);
  166. $view->assign('actions', implode(' ', $actions));
  167. $view->assign('content', $content);
  168. $view->display_one_col_template();