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