works_in_session_report.php 5.6 KB

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