works_in_session_report.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Course;
  4. use Chamilo\CoreBundle\Entity\Session;
  5. use Chamilo\UserBundle\Entity\User;
  6. /**
  7. * Courses reporting.
  8. *
  9. * @package chamilo.reporting
  10. */
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. api_block_anonymous_users(true);
  13. if (api_is_student()) {
  14. api_not_allowed(true);
  15. exit;
  16. }
  17. $toolName = get_lang('Works in session report');
  18. $em = Database::getManager();
  19. $session = null;
  20. if (api_is_platform_admin()) {
  21. $sessionList = SessionManager::get_sessions_list();
  22. } elseif (api_is_drh()) {
  23. $sessionList = SessionManager::get_sessions_followed_by_drh(api_get_user_id());
  24. } else {
  25. $sessionList = Tracking::get_sessions_coached_by_user(api_get_user_id());
  26. }
  27. $form = new FormValidator('work_report', 'GET');
  28. $selectSession = $form->addSelect('session', get_lang('Session'), [0 => get_lang('none')]);
  29. $form->addButtonFilter(get_lang('Filter'));
  30. foreach ($sessionList as $sessionInfo) {
  31. $selectSession->addOption($sessionInfo['name'], $sessionInfo['id']);
  32. }
  33. $sessionId = isset($_GET['session']) ? (int) $_GET['session'] : 0;
  34. $session = null;
  35. if (!empty($sessionId)) {
  36. $form->setDefaults(['session' => $sessionId]);
  37. /** @var Session $session */
  38. $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
  39. }
  40. $coursesInfo = [];
  41. $usersInfo = [];
  42. if ($session) {
  43. $sessionCourses = $session->getCourses();
  44. foreach ($sessionCourses as $sessionCourse) {
  45. /** @var Course $course */
  46. $course = $sessionCourse->getCourse();
  47. $coursesInfo[$course->getId()] = $course->getCode();
  48. $userCourseSubscriptions = $session->getUserCourseSubscriptionsByStatus($course, Session::STUDENT);
  49. foreach ($userCourseSubscriptions as $userCourseSubscription) {
  50. /** @var User $user */
  51. $user = $userCourseSubscription->getUser();
  52. if (!array_key_exists($user->getId(), $usersInfo)) {
  53. $usersInfo[$user->getId()] = [
  54. 'code' => $user->getOfficialCode(),
  55. 'complete_name' => UserManager::formatUserFullName($user),
  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().'_score'] = null;
  64. $usersInfo[$user->getId()][$course->getId().'_progress'] = null;
  65. $usersInfo[$user->getId()][$course->getId().'_last_sent_date'] = null;
  66. if (!$session->hasStudentInCourse($user, $course)) {
  67. continue;
  68. }
  69. $usersInfo[$user->getId()][$course->getId().'_score'] = Tracking::get_avg_student_score(
  70. $user->getId(),
  71. $course->getCode(),
  72. [],
  73. $session->getId(),
  74. false,
  75. false,
  76. true
  77. );
  78. $usersInfo[$user->getId()][$course->getId().'_progress'] = Tracking::get_avg_student_progress(
  79. $user->getId(),
  80. $course->getCode(),
  81. [],
  82. $session->getId()
  83. );
  84. $lastPublication = Tracking::getLastStudentPublication(
  85. $user,
  86. 'work',
  87. $course,
  88. $session
  89. );
  90. if (!$lastPublication) {
  91. continue;
  92. }
  93. $usersInfo[$user->getId()][$course->getId().'_last_sent_date'] = api_get_local_time(
  94. $lastPublication->getSentDate()->getTimestamp()
  95. );
  96. }
  97. }
  98. }
  99. if (isset($_GET['export']) && $session && ($coursesInfo && $usersInfo)) {
  100. $fileName = 'works_in_session_'.api_get_local_time();
  101. $dataToExport = [];
  102. $dataToExport[] = [$toolName, $session->getName()];
  103. $dataToExport['headers'][] = get_lang('Code');
  104. $dataToExport['headers'][] = get_lang('Learner name');
  105. $dataToExport['headers'][] = get_lang('Time spent in portal');
  106. $dataToExport['headers'][] = get_lang('First login in platform');
  107. $dataToExport['headers'][] = get_lang('Latest login in platform');
  108. foreach ($coursesInfo as $courseCode) {
  109. $dataToExport['headers'][] = $courseCode.' ('.get_lang('Best score').')';
  110. $dataToExport['headers'][] = get_lang('Progress');
  111. $dataToExport['headers'][] = get_lang('Last sent work date');
  112. }
  113. foreach ($usersInfo as $user) {
  114. $dataToExport[] = $user;
  115. }
  116. switch ($_GET['export']) {
  117. case 'xls':
  118. Export::export_table_xls_html($dataToExport, $fileName);
  119. break;
  120. case 'csv':
  121. Export::arrayToCsv($dataToExport, $fileName);
  122. break;
  123. }
  124. exit;
  125. }
  126. $interbreadcrumb[] = [
  127. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/index.php',
  128. 'name' => get_lang('Reporting'),
  129. ];
  130. $actions = null;
  131. if ($session) {
  132. $actions = Display::url(
  133. Display::return_icon('export_csv.png', get_lang('CSV export'), [], ICON_SIZE_MEDIUM),
  134. api_get_self().'?'.http_build_query(['export' => 'csv', 'session' => $session->getId()])
  135. );
  136. $actions .= Display::url(
  137. Display::return_icon('export_excel.png', get_lang('Excel export'), [], ICON_SIZE_MEDIUM),
  138. api_get_self().'?'.http_build_query(['export' => 'xls', 'session' => $session->getId()])
  139. );
  140. }
  141. $view = new Template($toolName);
  142. $view->assign('form', $form->returnForm());
  143. if ($session) {
  144. $view->assign('session', ['name' => $session->getName()]);
  145. $view->assign('courses', $coursesInfo);
  146. $view->assign('users', $usersInfo);
  147. }
  148. $template = $view->get_template('my_space/works_in_session_report.tpl');
  149. $content = $view->fetch($template);
  150. $view->assign('header', $toolName);
  151. if ($actions) {
  152. $view->assign(
  153. 'actions',
  154. Display::toolbarAction('toolbar', [$actions])
  155. );
  156. }
  157. $view->assign('content', $content);
  158. $view->display_one_col_template();