works_in_session_report.php 6.0 KB

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