teachers_time_by_session_report.php 7.1 KB

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