works_in_session_report.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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('WorksInSessionReport');
  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. if (isset($_GET['session']) && intval($_GET['session'])) {
  34. $form->setDefaults(['session' => intval($_GET['session'])]);
  35. /** @var Session $session */
  36. $session = $em->find('ChamiloCoreBundle:Session', intval($_GET['session']));
  37. }
  38. $coursesInfo = [];
  39. $usersInfo = [];
  40. if ($session) {
  41. $sessionCourses = $session->getCourses();
  42. foreach ($sessionCourses as $sessionCourse) {
  43. /** @var Course $course */
  44. $course = $sessionCourse->getCourse();
  45. $coursesInfo[$course->getId()] = $course->getCode();
  46. $userCourseSubscriptions = $session->getUserCourseSubscriptionsByStatus($course, Session::STUDENT);
  47. foreach ($userCourseSubscriptions as $userCourseSubscription) {
  48. /** @var User $user */
  49. $user = $userCourseSubscription->getUser();
  50. if (!array_key_exists($user->getId(), $usersInfo)) {
  51. $usersInfo[$user->getId()] = [
  52. 'code' => $user->getOfficialCode(),
  53. 'complete_name' => $user->getCompleteName(),
  54. 'time_in_platform' => api_time_to_hms(
  55. Tracking::get_time_spent_on_the_platform($user->getId(), 'ever')
  56. ),
  57. 'first_connection' => Tracking::get_first_connection_date($user->getId()),
  58. 'last_connection' => Tracking::get_last_connection_date($user->getId()),
  59. ];
  60. }
  61. $usersInfo[$user->getId()][$course->getId().'_score'] = null;
  62. $usersInfo[$user->getId()][$course->getId().'_progress'] = null;
  63. $usersInfo[$user->getId()][$course->getId().'_last_sent_date'] = null;
  64. if (!$session->hasStudentInCourse($user, $course)) {
  65. continue;
  66. }
  67. $usersInfo[$user->getId()][$course->getId().'_score'] = Tracking::get_avg_student_score(
  68. $user->getId(),
  69. $course->getCode(),
  70. [],
  71. $session->getId(),
  72. false,
  73. false,
  74. true
  75. );
  76. $usersInfo[$user->getId()][$course->getId().'_progress'] = Tracking::get_avg_student_progress(
  77. $user->getId(),
  78. $course->getCode(),
  79. [],
  80. $session->getId()
  81. );
  82. $lastPublication = Tracking::getLastStudentPublication(
  83. $user,
  84. 'work',
  85. $course,
  86. $session
  87. );
  88. if (!$lastPublication) {
  89. continue;
  90. }
  91. $usersInfo[$user->getId()][$course->getId().'_last_sent_date'] = api_get_local_time(
  92. $lastPublication->getSentDate()->getTimestamp()
  93. );
  94. }
  95. }
  96. }
  97. if (isset($_GET['export']) && $session && ($coursesInfo && $usersInfo)) {
  98. $fileName = 'works_in_session_'.api_get_local_time();
  99. $dataToExport = [];
  100. $dataToExport[] = [$toolName, $session->getName()];
  101. $dataToExport['headers'][] = get_lang('OfficialCode');
  102. $dataToExport['headers'][] = get_lang('StudentName');
  103. $dataToExport['headers'][] = get_lang('TimeSpentOnThePlatform');
  104. $dataToExport['headers'][] = get_lang('FirstLoginInPlatform');
  105. $dataToExport['headers'][] = get_lang('LatestLoginInPlatform');
  106. foreach ($coursesInfo as $courseCode) {
  107. $dataToExport['headers'][] = $courseCode.' ('.get_lang('BestScore').')';
  108. $dataToExport['headers'][] = get_lang('Progress');
  109. $dataToExport['headers'][] = get_lang('LastSentWorkDate');
  110. }
  111. foreach ($usersInfo as $user) {
  112. $dataToExport[] = $user;
  113. }
  114. switch ($_GET['export']) {
  115. case 'xls':
  116. Export::export_table_xls_html($dataToExport, $fileName);
  117. break;
  118. case 'csv':
  119. Export::arrayToCsv($dataToExport, $fileName);
  120. break;
  121. }
  122. exit;
  123. }
  124. $interbreadcrumb[] = [
  125. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/index.php',
  126. 'name' => get_lang('MySpace'),
  127. ];
  128. $actions = null;
  129. if ($session) {
  130. $actions = Display::url(
  131. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], ICON_SIZE_MEDIUM),
  132. api_get_self().'?'.http_build_query(['export' => 'csv', 'session' => $session->getId()])
  133. );
  134. $actions .= Display::url(
  135. Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), [], ICON_SIZE_MEDIUM),
  136. api_get_self().'?'.http_build_query(['export' => 'xls', 'session' => $session->getId()])
  137. );
  138. }
  139. $view = new Template($toolName);
  140. $view->assign('form', $form->returnForm());
  141. if ($session) {
  142. $view->assign('session', ['name' => $session->getName()]);
  143. $view->assign('courses', $coursesInfo);
  144. $view->assign('users', $usersInfo);
  145. }
  146. $template = $view->get_template('my_space/works_in_session_report.tpl');
  147. $content = $view->fetch($template);
  148. $view->assign('header', $toolName);
  149. if ($actions) {
  150. $view->assign(
  151. 'actions',
  152. Display::toolbarAction('toolbar', [$actions])
  153. );
  154. }
  155. $view->assign('content', $content);
  156. $view->display_one_col_template();