From 5d50b7bd0ed4d4acc964ada4c81a4c06ef5b6d06 Mon Sep 17 00:00:00 2001 From: carlos alvarado Date: Tue, 10 Aug 2021 10:17:08 -0500 Subject: [PATCH] Tracking: Add stats page for courses usage - refs #2709 --- main/admin/statistics/index.php | 163 ++++++++++++++++++++++++++++++++ main/inc/lib/course.lib.php | 80 ++++++++++++++++ 2 files changed, 243 insertions(+) diff --git a/main/admin/statistics/index.php b/main/admin/statistics/index.php index b03f77c8ee..0718b82e10 100755 --- a/main/admin/statistics/index.php +++ b/main/admin/statistics/index.php @@ -357,6 +357,7 @@ $tools = [ get_lang('System') => [ 'report=activities' => get_lang('ImportantActivities'), 'report=user_session' => get_lang('PortalUserSessionStats'), + 'report=courses_usage' => get_lang('CoursesUsage'), ], get_lang('Social') => [ 'report=messagereceived' => get_lang('MessagesReceived'), @@ -372,6 +373,168 @@ $course_categories = Statistics::getCourseCategories(); $content = ''; switch ($report) { + case 'courses_usage': + $form = new FormValidator('courses_usage', 'get'); + $nextElement = 0; + $currentPage = 0; + $start = 0; + $pagged = 10; + $coursesList = []; + $op = []; + $today = new DateTime(); + $reportPost = isset($_POST['report']) ? $_POST['report'] : null; + $endDate = $today->format('Y-m-d'); + $pag = 0; + $fechas = [ + 'day' => $today->setTimestamp(strtotime('-1 day'))->format('Y-m-d'), + 'week' => $today->setTimestamp(strtotime('-1 week'))->format('Y-m-d'), + 'month' => $today->setTimestamp(strtotime('-1 month'))->format('Y-m-d'), + '6month' => $today->setTimestamp(strtotime('-6 month'))->format('Y-m-d'), + 'year' => $today->setTimestamp(strtotime('-1 year'))->format('Y-m-d'), + '2year' => $today->setTimestamp(strtotime('-2 year'))->format('Y-m-d'), + 'total' => null, + ]; + $courses = CourseManager::get_course_list(); + $coursesTotal = count($courses); + if (0 < $coursesTotal) { + $start = isset($_GET['start']) ? (int) ($_GET['start']) : 0; + } + + $start = abs($start); + + $termina = ($start + $pagged) < $pagged ? $pagged : $start + $pagged; + foreach ($courses as $course) { + $courseId = $course['id']; + $sessions = 0; + $courseTotal = 0; + $visit = 0; + $indexCourseList = count($coursesList); + $item = []; + if ($indexCourseList >= $start && $indexCourseList < $termina) { + foreach ($fechas as $index => $date) { + $startDate = $date; + $courseTotal = count(CourseManager::getAccessCourse( + $courseId, + 0, + 0, + $startDate, + $endDate + )); + $sessions = count(CourseManager::getAccessCourse( + $courseId, + 1, + 0, + $startDate, + $endDate + )); + $visit = count(CourseManager::getAccessCourse( + $courseId, + 3, + 0, + $startDate, + $endDate + )); + $temp = [ + 'start' => $startDate, + 'course' => $visit, + 'course_id' => $course['id'], + 'session' => $sessions, + 'count' => $sessions + $courseTotal, + ]; + $item[$index] = $temp; + } + $coursesList[$indexCourseList] = [ + $course['title'], + $item['day']['count'], + $item['week']['count'], + $item['month']['count'], + $item['6month']['count'], + $item['year']['count'], + $item['2year']['count'], + $courseTotal, + $sessions, + $item['total']['count'], + ]; + if (0 == $nextElement) { + $nextElement = $indexCourseList; + } + $op[] = $coursesList[$indexCourseList]; + } else { + $coursesList[$indexCourseList] = null; + } + if ($indexCourseList >= ($termina)) { + break; + } + + if (1 == count($coursesList) % $pagged) { + $currentPage++; + } + } + $headerName = [ + [get_lang('Course'), false], + [get_lang('Today'), false], + [get_lang('ThisWeek'), false], + [get_lang('ThisMonth'), false], + ["6 ".get_lang('MinMonths'), false], + ["1 ".get_lang('Year'), false], + ["2 ".get_lang('Years'), false], + [get_lang('NumAccess')." (".get_lang('Course').")", false], + [get_lang('NumAccess')." (".get_lang('Session').")", false], + [get_lang('AbsoluteTotal')." (".get_lang('Visited').")", false], + ]; + $query_vars = []; + $query_vars['start'] = $nextElement; + $query_vars['report'] = 'courses_usage'; + $paging_options = []; + $paging_options['per_page'] = $pagged; + $nextCourseIndex = ($start + $pagged); + $previousCourseIndex = ($start - 10) < 0 ? 0 : ($start - 10); + + $pag = (int) ($coursesTotal / $pagged); + if ($pag < ($coursesTotal / $pagged)) { + $pag++; + } + + $nextLink = Display::url( + Display::return_icon( + 'action_next.png', + get_lang('NextPage'), + [], + ICON_SIZE_MEDIUM + ), + api_get_self()."?&start=$nextCourseIndex&report=courses_usage", + ['class' => 'btn'] + ); + + $previousLink = Display::url( + Display::return_icon( + 'action_prev.png', + get_lang('PreviousPage'), + [], + ICON_SIZE_MEDIUM + ), + api_get_self()."?&start=$previousCourseIndex&report=courses_usage", + ['class' => 'btn'] + ); + $table = Display::return_sortable_table( + $headerName, + $op, + 'ASC', + $paging_options, + $query_vars); + + $form->addHtml( + $table + ); + $html = "
 
"; + $html .= ($pag > 1) ? $currentPage." / ".$pag : ''; + $html .= ($previousCourseIndex > 1) ? $previousLink : ''; + $html .= ($nextCourseIndex < $coursesTotal) ? $nextLink : ''; + $html .= '
'; + $form->addHtml($html); + $content = $form->returnForm(); + + break; case 'session_by_date': $sessions = []; if ($validated) { diff --git a/main/inc/lib/course.lib.php b/main/inc/lib/course.lib.php index 128b28ad42..44e266e2cf 100755 --- a/main/inc/lib/course.lib.php +++ b/main/inc/lib/course.lib.php @@ -7164,6 +7164,86 @@ class CourseManager Event::logSubscribedUserInCourse($studentId, $courseId); } + /** + * Returns access to courses based on course id, user, and a start and end date range. + * If withSession is 0, only the courses will be taken. + * If withSession is 1, only the sessions will be taken. + * If withSession is different from 0 and 1, the whole set will be take. + * + * @param int $courseId + * @param int $withSession + * @param int $userId + * @param null $startDate + * @param null $endDate + */ + public static function getAccessCourse( + $courseId = 0, + $withSession = 0, + $userId = 0, + $startDate = null, + $endDate = null + ) { + $where = null; + $courseId = (int) $courseId; + $userId = (int) $userId; + $tblTrackECourse = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); + $wheres = []; + if (0 != $courseId) { + $wheres[] = " course_access.c_id = $courseId "; + } + if (0 != $userId) { + $wheres[] = " course_access.user_id = $userId "; + } + if (!empty($startDate)) { + $startDate = api_get_utc_datetime($startDate, false, true); + $wheres[] = " course_access.login_course_date >= '".$startDate->format('Y-m-d')."' "; + } + if (!empty($endDate)) { + $endDate = api_get_utc_datetime($endDate, false, true); + $wheres[] = " course_access.login_course_date <= '".$endDate->format('Y-m-d')."' "; + } + if (0 == $withSession) { + $wheres[] = " course_access.session_id = 0 "; + } elseif (1 == $withSession) { + $wheres[] = " course_access.session_id != 0 "; + } + + $totalWhere = count($wheres); + for ($i = 0; $i <= $totalWhere; $i++) { + if (isset($wheres[$i])) { + if (empty($where)) { + $where = ' WHERE '; + } + $where .= $wheres[$i]; + if (isset($wheres[$i + 1])) { + $where .= ' AND '; + } + } + } + + $sql = " + SELECT DISTINCT + CAST( course_access.login_course_date AS DATE ) AS login_course_date, + user_id, + c_id + FROM + $tblTrackECourse as course_access + $where + GROUP BY + c_id, + session_id, + CAST( course_access.login_course_date AS DATE ), + user_id + ORDER BY + c_id + "; + $res = Database::query($sql); + $data = Database::store_result($res); + Database::free_result($res); + + return $data; + } + /** * Check if a specific access-url-related setting is a problem or not. *