123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Generate a teacher time report in platform by session only
- * @package chamilo.admin
- */
- use \Chamilo\CoreBundle\Entity\Session;
- use \Doctrine\Common\Collections\Criteria;
- $cidReset = true;
- require_once '../inc/global.inc.php';
- require_once api_get_path(SYS_CODE_PATH) . 'work/work.lib.php';
- if (!api_is_platform_admin(true) && !api_is_teacher()) {
- api_not_allowed(true);
- }
- $toolName = get_lang('TeacherTimeReportBySession');
- $em = Database::getManager();
- $sessionsInfo = api_is_platform_admin()
- ? SessionManager::get_sessions_list()
- : Tracking::get_sessions_coached_by_user(api_get_user_id());
- $session = null;
- $form = new FormValidator('teacher_time_report_by_session', 'GET');
- $selectSession = $form->addSelect('session', get_lang('Session'), [0 => get_lang('None')]);
- $form->addButtonFilter(get_lang('Filter'));
- foreach ($sessionsInfo as $sessionInfo) {
- $selectSession->addOption($sessionInfo['name'], $sessionInfo['id']);
- }
- if (isset($_GET['session']) && intval($_GET['session'])) {
- $form->setDefaults(['session' => intval($_GET['session'])]);
- $session = $em->find('ChamiloCoreBundle:Session', intval($_GET['session']));
- }
- $data = [];
- $coursesInfo = [];
- $usersInfo = [];
- if ($session) {
- $sessionCourses = $session->getCourses();
- foreach ($sessionCourses as $sessionCourse) {
- $course = $sessionCourse->getCourse();
- $coursesInfo[$course->getId()] = $course->getCode();
- $criteria = Criteria::create()->where(
- Criteria::expr()->eq("status", Session::COACH)
- );
- $userCourseSubscriptions = $session
- ->getUserCourseSubscriptions()
- ->matching($criteria);
- foreach ($userCourseSubscriptions as $userCourseSubscription) {
- $user = $userCourseSubscription->getUser();
- if (!array_key_exists($user->getId(), $usersInfo)) {
- $usersInfo[$user->getId()] = [
- 'code' => $user->getOfficialCode(),
- 'complete_name' => $user->getCompleteName(),
- 'time_in_platform' => api_time_to_hms(
- Tracking::get_time_spent_on_the_platform($user->getId(), 'ever')
- ),
- 'first_connection' => Tracking::get_first_connection_date($user->getId()),
- 'last_connection' => Tracking::get_last_connection_date($user->getId()),
- ];
- }
- $usersInfo[$user->getId()][$course->getId() . '_number_of_students'] = null;
- $usersInfo[$user->getId()][$course->getId() . '_number_of_works'] = null;
- $usersInfo[$user->getId()][$course->getId() . '_last_work'] = null;
- $usersInfo[$user->getId()][$course->getId() . '_time_spent_of_course'] = null;
- if (!$session->hasCoachInCourseWithStatus($user, $course)) {
- continue;
- }
- $works = $em
- ->getRepository('ChamiloCourseBundle:CStudentPublication')
- ->findByTeacher($user, $course, $session->getId());
- $usersInfo[$user->getId()][$course->getId() . '_number_of_students'] = $sessionCourse->getNbrUsers();
- $usersInfo[$user->getId()][$course->getId() . '_number_of_works'] = count($works);
- $usersInfo[$user->getId()][$course->getId() . '_time_spent_of_course'] = api_time_to_hms(
- Tracking::get_time_spent_on_the_course($user->getId(), $course->getId(), $session->getId())
- );
- $lastWork = array_pop($works);
- if (!$lastWork) {
- continue;
- }
- $lastFormattedDate = api_format_date($lastWork->getSentDate()->getTimestamp(), DATE_TIME_FORMAT_SHORT);
- $usersInfo[$user->getId()][$course->getId() . '_last_work'] = api_format_date(
- $lastWork->getSentDate()->getTimestamp(),
- DATE_TIME_FORMAT_SHORT
- );
- }
- }
- }
- if (isset($_GET['export']) && $session && ($coursesInfo && $usersInfo)) {
- $fileName = get_lang('TeacherTimeReport') . ' ' . api_get_local_time();
- $dataToExport = [];
- $dataToExport[] = [$toolName, $session->getName()];
- $dataToExport['headers'] = [
- get_lang('OfficialCode'),
- get_lang('CoachName'),
- get_lang('TimeSpentOnThePlatform'),
- get_lang('FirstLoginInPlatform'),
- get_lang('LatestLoginInPlatform'),
- ];
- foreach ($coursesInfo as $courseCode) {
- $dataToExport['headers'][] = $courseCode;
- $dataToExport['headers'][] = get_lang('NumberOfWorks');
- $dataToExport['headers'][] = get_lang('LastWork');
- $dataToExport['headers'][] = sprintf(get_lang('TimeReportForCourseX'), $courseCode);
- }
- foreach ($usersInfo as $user) {
- $dataToExport[] = $user;
- }
- foreach ($data as $row) {
- $contents = [
- $row['code'],
- $row['complete_name'],
- $row['time_in_platform'],
- $row['first_connection'],
- $row['last_connection']
- ];
- foreach ($row['courses'] as $course) {
- $headers[] = $course['code'];
- $headers[] = get_lang('NumberOfWorks');
- $headers[] = get_lang('LastWork');
- $headers[] = sprintf(get_lang('TimeReportForCourseX'), $course['code']);
- $contents[] = $course['number_of_students'];
- $contents[] = $course['number_of_works'];
- $contents[] = $course['last_work'];
- $contents[] = $course['time_spent_of_course'];
- }
- $dataToExport[] = [get_lang('Session'), $session->getName()];
- $dataToExport[] = $headers;
- $dataToExport[] = $contents;
- }
- switch ($_GET['export']) {
- case 'xls':
- Export::export_table_xls_html($dataToExport, $fileName);
- break;
- case 'csv':
- Export::arrayToCsv($dataToExport, $fileName);
- break;
- }
- exit;
- }
- $this_section = SECTION_PLATFORM_ADMIN;
- $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH) . 'mySpace/', 'name' => get_lang('Reporting')];
- $interbreadcrumb[] = [
- 'url' => api_get_path(WEB_CODE_PATH) . 'mySpace/session.php',
- 'name' => get_lang('FollowedSessions')
- ];
- $actions = [];
- if ($session) {
- $actions = [
- Display::url(
- Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], ICON_SIZE_MEDIUM),
- api_get_self() . '?' . http_build_query(['export' => 'csv', 'session' => $session->getId()])
- ),
- Display::url(
- Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), [], ICON_SIZE_MEDIUM),
- api_get_self() . '?' . http_build_query(['export' => 'xls', 'session' => $session->getId()])
- )
- ];
- }
- $view = new Template($toolName);
- $view->assign('form', $form->returnForm());
- if ($session) {
- $view->assign('session', ['id' => $session->getId(), 'name' => $session->getName()]);
- $view->assign('courses', $coursesInfo);
- $view->assign('users', $usersInfo);
- }
- $template = $view->get_template('admin/teachers_time_by_session_report.tpl');
- $content = $view->fetch($template);
- $view->assign('header', $toolName);
- $view->assign('actions', implode(' ', $actions));
- $view->assign('content', $content);
- $view->display_one_col_template();
|