teachers_time_by_session_report.php 7.3 KB

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