works_in_session_report.php 5.5 KB

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