From bebdf2d739944d1c26b8be3da69a90f5d3a84f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20S=C3=A1nchez?= <114572427+BorjaSanchezBeezNest@users.noreply.github.com> Date: Thu, 17 Nov 2022 17:21:17 +0100 Subject: [PATCH] Admin - Add quarterly report in statistics - refs BT#16108 Author: @BorjaSanchezBeezNest --- main/admin/statistics/index.php | 149 ++++ main/inc/ajax/statistics.ajax.php | 696 ++++++++++++++++++ main/inc/lib/api.lib.php | 16 + main/inc/lib/internationalization.lib.php | 59 ++ main/inc/lib/statistics.lib.php | 235 ++++-- main/inc/lib/tracking.lib.php | 41 ++ main/inc/lib/usermanager.lib.php | 142 +++- .../block_global_info.class.php | 10 +- 8 files changed, 1279 insertions(+), 69 deletions(-) diff --git a/main/admin/statistics/index.php b/main/admin/statistics/index.php index d22e5f1767..ec044f90c8 100755 --- a/main/admin/statistics/index.php +++ b/main/admin/statistics/index.php @@ -361,6 +361,7 @@ $tools = [ 'report=activities' => get_lang('ImportantActivities'), 'report=user_session' => get_lang('PortalUserSessionStats'), 'report=courses_usage' => get_lang('CoursesUsage'), + 'report=quarterly_report' => get_lang('QuarterlyReport'), ], get_lang('Social') => [ 'report=messagereceived' => get_lang('MessagesReceived'), @@ -1706,6 +1707,154 @@ switch ($report) { break; case 'lti_tool_lp': $content .= Statistics::printLtiLearningPath(); + break; + case 'quarterly_report': + + $htmlHeadXtra[] .= ''; + $htmlHeadXtra[] .= ''; + + $htmlHeadXtra[] .= ''; + + $htmlHeadXtra[] .= ''; + + $htmlHeadXtra[] .= ''; + + $htmlHeadXtra[] .= ''; + + if (api_get_current_access_url_id() === 1) { + $htmlHeadXtra[] .= ''; + } + + $content .= Display::tag('H4', get_lang('ReportQuarterlyUsers'), ['style' => 'margin-bottom: 25px;']); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlyUsers();', 'class' => 'btn btn-default'] + ); + $content .= Display::div('', ['id' => 'tracking-report-quarterly-users', 'style' => 'margin: 30px;']); + + $content .= Display::tag('H4', get_lang('ReportQuarterlyCourses'), ['style' => 'margin-bottom: 25px;']); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlyCourses();', 'class' => 'btn btn-default'] + ); + $content .= Display::div('', ['id' => 'tracking-report-quarterly-courses', 'style' => 'margin: 30px;']); + + $content .= Display::tag('H4', get_lang('ReportQuarterlyHoursOfTraining'), ['style' => 'margin-bottom: 25px;']); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlyHoursOfTraining();', 'class' => 'btn btn-default'] + ); + $content .= Display::div( + '', + [ + 'id' => 'tracking-report-quarterly-hours-of-training', + 'style' => 'margin: 30px;', + ] + ); + $content .= Display::tag( + 'H4', + get_lang('ReportQuarterlyNumberOfCertificatesGenerated'), + ['style' => 'margin-bottom: 25px;'] + ); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlyCertificatesGenerated();', 'class' => 'btn btn-default'] + ); + $content .= Display::div( + '', + ['id' => 'tracking-report-quarterly-number-of-certificates-generated', 'style' => 'margin: 30px;'] + ); + $content .= Display::tag( + 'H4', + get_lang('ReportQuarterlySessionsByDuration'), + ['style' => 'margin-bottom: 25px;'] + ); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlySessionsByDuration();', 'class' => 'btn btn-default'] + ); + $content .= Display::div( + '', + ['id' => 'tracking-report-quarterly-sessions-by-duration', 'style' => 'margin: 30px;'] + ); + $content .= Display::tag( + 'H4', + get_lang('ReportQuarterlyCoursesAndSessions'), + ['style' => 'margin-bottom: 25px;'] + ); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlyCoursesAndSessions();', 'class' => 'btn btn-default'] + ); + $content .= Display::div( + '', + [ + 'id' => 'tracking-report-quarterly-courses-and-sessions', + 'style' => 'margin: 30px;', + ] + ); + + if (api_get_current_access_url_id() === 1) { + $content .= Display::tag( + 'H4', + get_lang('ReportQuarterlyTotalDiskUsage'), + ['style' => 'margin-bottom: 25px;'] + ); + $content .= Display::url( + get_lang('Show'), + 'javascript://', + ['onclick' => 'loadReportQuarterlyTotalDiskUsage();', 'class' => 'btn btn-default'] + ); + $content .= Display::div( + '', + [ + 'id' => 'tracking-report-quarterly-total-disk-usage', + 'style' => 'margin: 30px;', + ] + ); + } + break; } diff --git a/main/inc/ajax/statistics.ajax.php b/main/inc/ajax/statistics.ajax.php index 7b1cc409a9..06184ab2ee 100644 --- a/main/inc/ajax/statistics.ajax.php +++ b/main/inc/ajax/statistics.ajax.php @@ -725,4 +725,700 @@ switch ($action) { header('Content-type: application/json'); echo json_encode($list); break; + case 'report_quarterly_users': + + $currentQuarterDates = getQuarterDates(); + $pre1QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-3 month') + ->format('Y-m-d') + ); + $pre2QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-6 month') + ->format('Y-m-d') + ); + $pre3QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-9 month') + ->format('Y-m-d') + ); + $pre4QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-12 month') + ->format('Y-m-d') + ); + $pre5QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-15 month') + ->format('Y-m-d') + ); + + // Make de headers for the table + $headers = [ + '', + $pre5QuarterDates['quarter_title'], + $pre4QuarterDates['quarter_title'], + $pre3QuarterDates['quarter_title'], + $pre2QuarterDates['quarter_title'], + $pre1QuarterDates['quarter_title'], + get_lang('YoY'), + $currentQuarterDates['quarter_title'].'*', + ]; + + // Get the data for the number of user registered row (2) + $countUsersTotal = UserManager::get_number_of_users( + null, + null, + null + ); + $countUsersPre1Quarter = UserManager::get_number_of_users( + null, + null, + null, + null, + $pre1QuarterDates['quarter_end'] + ); + $countUsersPre2Quarter = UserManager::get_number_of_users( + null, + null, + null, + null, + $pre2QuarterDates['quarter_end'] + ); + $countUsersPre3Quarter = UserManager::get_number_of_users( + null, + null, + null, + null, + $pre3QuarterDates['quarter_end'] + ); + $countUsersPre4Quarter = UserManager::get_number_of_users( + null, + null, + null, + null, + $pre4QuarterDates['quarter_end'] + ); + $countUsersPre5Quarter = UserManager::get_number_of_users( + null, + null, + null, + null, + $pre5QuarterDates['quarter_end'] + ); + // Calculate percent for first row + $percentIncrementUsersRegistered = api_calculate_increment_percent( + $countUsersPre1Quarter, + $countUsersPre5Quarter + ); + + // Get the data for number of users connected row (3) + $countUsersConnectedCurrentQuarter = count( + Statistics::getLoginsByDate( + $currentQuarterDates['quarter_start'], + $currentQuarterDates['quarter_end'] + ) + ); + $countUsersConnectedPre1Quarter = count( + Statistics::getLoginsByDate( + $pre1QuarterDates['quarter_start'], + $pre1QuarterDates['quarter_end'] + ) + ); + $countUsersConnectedPre2Quarter = count( + Statistics::getLoginsByDate( + $pre2QuarterDates['quarter_start'], + $pre2QuarterDates['quarter_end'] + ) + ); + $countUsersConnectedPre3Quarter = count( + Statistics::getLoginsByDate( + $pre3QuarterDates['quarter_start'], + $pre3QuarterDates['quarter_end'] + ) + ); + $countUsersConnectedPre4Quarter = count( + Statistics::getLoginsByDate( + $pre4QuarterDates['quarter_start'], + $pre4QuarterDates['quarter_end'] + ) + ); + $countUsersConnectedPre5Quarter = count( + Statistics::getLoginsByDate( + $pre5QuarterDates['quarter_start'], + $pre5QuarterDates['quarter_end'] + ) + ); + + // Calculate percent for second row + $percentIncrementUsersConnected = api_calculate_increment_percent( + $countUsersConnectedPre1Quarter, + $countUsersConnectedPre5Quarter + ); + + //Make de rows with the recollected data + $rows = []; + $rows[] = [ + get_lang('NumberOfUsersRegisteredTotal'), + $countUsersPre5Quarter, + $countUsersPre4Quarter, + $countUsersPre3Quarter, + $countUsersPre2Quarter, + $countUsersPre1Quarter, + $percentIncrementUsersRegistered, + $countUsersTotal, + ]; + //todo comprobacion + - + $rows[] = [ + get_lang('NumberOfUsersRegisteredCompared'), + '-', + '+'.($countUsersPre1Quarter - $countUsersPre2Quarter), + '+'.($countUsersPre2Quarter - $countUsersPre3Quarter), + '+'.($countUsersPre3Quarter - $countUsersPre4Quarter), + '+'.($countUsersPre4Quarter - $countUsersPre5Quarter), + '-', + '+'.($countUsersTotal - $countUsersPre1Quarter), + ]; + $rows[] = [ + get_lang('NumberOfUsersWhoConnected'), + $countUsersConnectedPre5Quarter, + $countUsersConnectedPre4Quarter, + $countUsersConnectedPre3Quarter, + $countUsersConnectedPre2Quarter, + $countUsersConnectedPre1Quarter, + $percentIncrementUsersConnected, + $countUsersConnectedCurrentQuarter, + ]; + + echo Display::table($headers, $rows, []); + echo Display::label(get_lang('IncompleteDataCurrentQuarter'), 'warning'); + break; + case 'report_quarterly_courses': + + $currentQuarterDates = getQuarterDates(); + $pre1QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-3 month') + ->format('Y-m-d') + ); + $pre2QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-6 month') + ->format('Y-m-d') + ); + $pre3QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-9 month') + ->format('Y-m-d') + ); + $pre4QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-12 month') + ->format('Y-m-d') + ); + $pre5QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-15 month') + ->format('Y-m-d') + ); + + // Make the headers for the table + $headers = [ + '', + $pre5QuarterDates['quarter_title'], + $pre4QuarterDates['quarter_title'], + $pre3QuarterDates['quarter_title'], + $pre2QuarterDates['quarter_title'], + $pre1QuarterDates['quarter_title'], + get_lang('YoY'), + $currentQuarterDates['quarter_title'].'*', + ]; + + // Get the data for the rows + $countCoursesCurrentQuarter = Statistics::countCourses(null, null, null); + $countCoursesPre1Quarter = Statistics::countCourses(null, null, $pre1QuarterDates['quarter_end']); + $countCoursesPre2Quarter = Statistics::countCourses(null, null, $pre2QuarterDates['quarter_end']); + $countCoursesPre3Quarter = Statistics::countCourses(null, null, $pre3QuarterDates['quarter_end']); + $countCoursesPre4Quarter = Statistics::countCourses(null, null, $pre4QuarterDates['quarter_end']); + $countCoursesPre5Quarter = Statistics::countCourses(null, null, $pre5QuarterDates['quarter_end']); + + $auxArrayVisibilities = [ + COURSE_VISIBILITY_OPEN_WORLD, + COURSE_VISIBILITY_OPEN_PLATFORM, + COURSE_VISIBILITY_REGISTERED, + ]; + + $countCoursesAvailableCurrentQuarter = Statistics::countCoursesByVisibility($auxArrayVisibilities); + $countCoursesAvailablePre1Quarter = Statistics::countCoursesByVisibility( + $auxArrayVisibilities, + null, + $pre1QuarterDates['quarter_end'] + ); + $countCoursesAvailablePre2Quarter = Statistics::countCoursesByVisibility( + $auxArrayVisibilities, + null, + $pre2QuarterDates['quarter_end'] + ); + $countCoursesAvailablePre3Quarter = Statistics::countCoursesByVisibility( + $auxArrayVisibilities, + null, + $pre3QuarterDates['quarter_end'] + ); + $countCoursesAvailablePre4Quarter = Statistics::countCoursesByVisibility( + $auxArrayVisibilities, + null, + $pre4QuarterDates['quarter_end'] + ); + $countCoursesAvailablePre5Quarter = Statistics::countCoursesByVisibility( + $auxArrayVisibilities, + null, + $pre5QuarterDates['quarter_end'] + ); + + // Calculate percents for first row + $percentIncrementCourses = api_calculate_increment_percent( + $countCoursesPre1Quarter, + $countCoursesPre5Quarter + ); + // Calculate percents for second row + $percentIncrementUsersRegistered = api_calculate_increment_percent( + $countCoursesAvailablePre1Quarter, + $countCoursesAvailablePre5Quarter + ); + + //Make the rows with the recollected data + $rows = []; + $rows[] = [ + get_lang('NumberOfExistingCoursesTotal'), + $countCoursesPre5Quarter, + $countCoursesPre4Quarter, + $countCoursesPre3Quarter, + $countCoursesPre2Quarter, + $countCoursesPre1Quarter, + $percentIncrementCourses, + $countCoursesCurrentQuarter, + ]; + $rows[] = [ + get_lang('NumberOfAvailableCourses'), + $countCoursesAvailablePre5Quarter, + $countCoursesAvailablePre4Quarter, + $countCoursesAvailablePre3Quarter, + $countCoursesAvailablePre2Quarter, + $countCoursesAvailablePre1Quarter, + $percentIncrementUsersRegistered, + $countCoursesAvailableCurrentQuarter, + ]; + + echo Display::table($headers, $rows, []); + echo Display::label(get_lang('IncompleteDataCurrentQuarter'), 'warning'); + break; + case 'report_quarterly_hours_of_training': + + $currentQuarterDates = getQuarterDates(); + $pre1QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-3 month') + ->format('Y-m-d') + ); + $pre2QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-6 month') + ->format('Y-m-d') + ); + $pre3QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-9 month') + ->format('Y-m-d') + ); + $pre4QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-12 month') + ->format('Y-m-d') + ); + $pre5QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-15 month') + ->format('Y-m-d') + ); + + // Make the headers for the table + $headers = [ + '', + $pre5QuarterDates['quarter_title'], + $pre4QuarterDates['quarter_title'], + $pre3QuarterDates['quarter_title'], + $pre2QuarterDates['quarter_title'], + $pre1QuarterDates['quarter_title'], + get_lang('YoY'), + $currentQuarterDates['quarter_title'].'*', + ]; + + // Get data for the row + $timeSpentCoursesCurrentQuarter = Tracking::getTotalTimeSpentInCourses( + $currentQuarterDates['quarter_start'], + $currentQuarterDates['quarter_end'] + ); + $timeSpentCourses1PreQuarter = Tracking::getTotalTimeSpentInCourses( + $pre1QuarterDates['quarter_start'], + $pre1QuarterDates['quarter_end'] + ); + $timeSpentCourses2PreQuarter = Tracking::getTotalTimeSpentInCourses( + $pre2QuarterDates['quarter_start'], + $pre2QuarterDates['quarter_end'] + ); + $timeSpentCourses3PreQuarter = Tracking::getTotalTimeSpentInCourses( + $pre3QuarterDates['quarter_start'], + $pre3QuarterDates['quarter_end'] + ); + $timeSpentCourses4PreQuarter = Tracking::getTotalTimeSpentInCourses( + $pre4QuarterDates['quarter_start'], + $pre4QuarterDates['quarter_end'] + ); + $timeSpentCourses5PreQuarter = Tracking::getTotalTimeSpentInCourses( + $pre5QuarterDates['quarter_start'], + $pre5QuarterDates['quarter_end'] + ); + + // Calculate percent for the row + $percentIncrementTimeSpent = api_calculate_increment_percent( + $timeSpentCourses1PreQuarter, + $timeSpentCourses5PreQuarter + ); + + //Make the row with the recollected data + $rows = []; + $rows[] = [ + get_lang('NumberOfHoursTrainingFollowed'), + $timeSpentCourses5PreQuarter, + $timeSpentCourses4PreQuarter, + $timeSpentCourses3PreQuarter, + $timeSpentCourses2PreQuarter, + $timeSpentCourses1PreQuarter, + $percentIncrementTimeSpent, + $timeSpentCoursesCurrentQuarter, + ]; + echo Display::table($headers, $rows, []); + echo Display::label(get_lang('IncompleteDataCurrentQuarter'), 'warning'); + + break; + case 'report_quarterly_number_of_certificates_generated': + + $currentQuarterDates = getQuarterDates(); + $pre1QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-3 month') + ->format('Y-m-d') + ); + $pre2QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-6 month') + ->format('Y-m-d') + ); + $pre3QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-9 month') + ->format('Y-m-d') + ); + $pre4QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-12 month') + ->format('Y-m-d') + ); + $pre5QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-15 month') + ->format('Y-m-d') + ); + + // Make the headers for the table + $headers = [ + '', + $pre5QuarterDates['quarter_title'], + $pre4QuarterDates['quarter_title'], + $pre3QuarterDates['quarter_title'], + $pre2QuarterDates['quarter_title'], + $pre1QuarterDates['quarter_title'], + get_lang('YoY'), + $currentQuarterDates['quarter_title'].'*', + ]; + + // Get data for the row + $certificateGeneratedCurrentQuarter = Statistics::countCertificatesByQuarter( + null, + $currentQuarterDates['quarter_end'] + ); + $certificateGenerated1PreQuarter = Statistics::countCertificatesByQuarter( + null, + $pre1QuarterDates['quarter_end'] + ); + $certificateGenerated2PreQuarter = Statistics::countCertificatesByQuarter( + null, + $pre2QuarterDates['quarter_end'] + ); + $certificateGenerated3PreQuarter = Statistics::countCertificatesByQuarter( + null, + $pre3QuarterDates['quarter_end'] + ); + $certificateGenerated4PreQuarter = Statistics::countCertificatesByQuarter( + null, + $pre4QuarterDates['quarter_end'] + ); + $certificateGenerated5PreQuarter = Statistics::countCertificatesByQuarter( + null, + $pre5QuarterDates['quarter_end'] + ); + + // Calculate percent for the row + $percentIncrementCertificateGenerated = api_calculate_increment_percent( + $certificateGenerated1PreQuarter, + $certificateGenerated5PreQuarter + ); + + //Make the row with the recollected data + $rows = []; + $rows[] = [ + get_lang('NumberOfCertificatesGenerated'), + $certificateGenerated5PreQuarter, + $certificateGenerated4PreQuarter, + $certificateGenerated3PreQuarter, + $certificateGenerated2PreQuarter, + $certificateGenerated1PreQuarter, + $percentIncrementCertificateGenerated, + $certificateGeneratedCurrentQuarter, + ]; + + echo Display::table($headers, $rows, []); + echo Display::label(get_lang('IncompleteDataCurrentQuarter'), 'warning'); + + break; + case "report_quarterly_sessions_by_duration": + + $currentQuarterDates = getQuarterDates(); + $pre1QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-3 month') + ->format('Y-m-d') + ); + $pre2QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-6 month') + ->format('Y-m-d') + ); + $pre3QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-9 month') + ->format('Y-m-d') + ); + $pre4QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-12 month') + ->format('Y-m-d') + ); + $pre5QuarterDates = getQuarterDates( + date_create($currentQuarterDates['quarter_start']) + ->modify('-15 month') + ->format('Y-m-d') + ); + + // Make the headers for the table + $headers = [ + get_lang('SessionsByDurationByQuarter'), + $pre5QuarterDates['quarter_title'], + $pre4QuarterDates['quarter_title'], + $pre3QuarterDates['quarter_title'], + $pre2QuarterDates['quarter_title'], + $pre1QuarterDates['quarter_title'], + get_lang('YoY'), + $currentQuarterDates['quarter_title'].'*', + ]; + + // Get the data for the rows + $sessionsDurationCurrentQuarter = Statistics::getSessionsByDuration( + $currentQuarterDates['quarter_start'], + $currentQuarterDates['quarter_end'] + ); + $sessionsDuration1PreQuarter = Statistics::getSessionsByDuration( + $pre1QuarterDates['quarter_start'], + $pre1QuarterDates['quarter_end'] + ); + $sessionsDuration2PreQuarter = Statistics::getSessionsByDuration( + $pre2QuarterDates['quarter_start'], + $pre2QuarterDates['quarter_end'] + ); + $sessionsDuration3PreQuarter = Statistics::getSessionsByDuration( + $pre3QuarterDates['quarter_start'], + $pre3QuarterDates['quarter_end'] + ); + $sessionsDuration4PreQuarter = Statistics::getSessionsByDuration( + $pre4QuarterDates['quarter_start'], + $pre4QuarterDates['quarter_end'] + ); + $sessionsDuration5PreQuarter = Statistics::getSessionsByDuration( + $pre5QuarterDates['quarter_start'], + $pre5QuarterDates['quarter_end'] + ); + + // Calculate percent for the rows + $percentIncrementSessionDuration0 = api_calculate_increment_percent( + $sessionsDuration1PreQuarter['0'], + $sessionsDuration5PreQuarter['0'] + ); + $percentIncrementSessionDuration5 = api_calculate_increment_percent( + $sessionsDuration1PreQuarter['5'], + $sessionsDuration5PreQuarter['5'] + ); + $percentIncrementSessionDuration10 = api_calculate_increment_percent( + $sessionsDuration1PreQuarter['10'], + $sessionsDuration5PreQuarter['10'] + ); + $percentIncrementSessionDuration15 = api_calculate_increment_percent( + $sessionsDuration1PreQuarter['15'], + $sessionsDuration5PreQuarter['15'] + ); + $percentIncrementSessionDuration30 = api_calculate_increment_percent( + $sessionsDuration1PreQuarter['30'], + $sessionsDuration5PreQuarter['30'] + ); + $percentIncrementSessionDuration60 = api_calculate_increment_percent( + $sessionsDuration1PreQuarter['60'], + $sessionsDuration5PreQuarter['60'] + ); + + //Make the rows with the recollected data + $rows = []; + $rows[] = [ + '0-5′', + $sessionsDuration5PreQuarter['0'], + $sessionsDuration4PreQuarter['0'], + $sessionsDuration3PreQuarter['0'], + $sessionsDuration2PreQuarter['0'], + $sessionsDuration1PreQuarter['0'], + $percentIncrementSessionDuration0, + $sessionsDurationCurrentQuarter['0'], + ]; + $rows[] = [ + '6-10′', + $sessionsDuration5PreQuarter['5'], + $sessionsDuration4PreQuarter['5'], + $sessionsDuration3PreQuarter['5'], + $sessionsDuration2PreQuarter['5'], + $sessionsDuration1PreQuarter['5'], + $percentIncrementSessionDuration5, + $sessionsDurationCurrentQuarter['5'], + ]; + $rows[] = [ + '11-15′', + $sessionsDuration5PreQuarter['10'], + $sessionsDuration4PreQuarter['10'], + $sessionsDuration3PreQuarter['10'], + $sessionsDuration2PreQuarter['10'], + $sessionsDuration1PreQuarter['10'], + $percentIncrementSessionDuration10, + $sessionsDurationCurrentQuarter['10'], + ]; + $rows[] = [ + '16-30′', + $sessionsDuration5PreQuarter['15'], + $sessionsDuration4PreQuarter['15'], + $sessionsDuration3PreQuarter['15'], + $sessionsDuration2PreQuarter['15'], + $sessionsDuration1PreQuarter['15'], + $percentIncrementSessionDuration15, + $sessionsDurationCurrentQuarter['15'], + ]; + $rows[] = [ + '31-60′', + $sessionsDuration5PreQuarter['30'], + $sessionsDuration4PreQuarter['30'], + $sessionsDuration3PreQuarter['30'], + $sessionsDuration2PreQuarter['30'], + $sessionsDuration1PreQuarter['30'], + $percentIncrementSessionDuration30, + $sessionsDurationCurrentQuarter['30'], + ]; + $rows[] = [ + '60-∞′', + $sessionsDuration5PreQuarter['60'], + $sessionsDuration4PreQuarter['60'], + $sessionsDuration3PreQuarter['60'], + $sessionsDuration2PreQuarter['60'], + $sessionsDuration1PreQuarter['60'], + $percentIncrementSessionDuration60, + $sessionsDurationCurrentQuarter['60'], + ]; + + echo Display::table($headers, $rows, []); + echo Display::label(get_lang('IncompleteDataCurrentQuarter'), 'warning'); + break; + case "report_quarterly_courses_and_sessions": + + // Make the headers for the tables + $headers = [ + [ + get_lang('ListOfCoursesCodes'), + get_lang('NumberOfSubscribedUsers').'*', + get_lang('NumberOfUsersWhoFinishedCourse'), + ], + [ + get_lang('ListOfCoursesCodesAndSessions'), + get_lang('NumberOfSubscribedUsers').'*', + get_lang('NumberOfUsersWhoFinishedCourse'), + ], + ]; + + // Get the data fot the first table + $courses = UserManager::countUsersWhoFinishedCourses(); + + //Make the rows for first table + $rows = []; + foreach ($courses as $course => $data) { + $course_url = api_get_path(WEB_CODE_PATH).'course_home/course_home.php?cidReq='.$course; + $rows[] = [ + Display::url($course, $course_url, ['target' => SESSION_LINK_TARGET]), + $data['subscribed'], + $data['finished'], + ]; + } + + echo Display::table($headers[0], $rows, []); + + //Get the data for the second table (with sessions) + $courses = UserManager::countUsersWhoFinishedCoursesInSessions(); + + //Make the rows for second table + $rows = []; + foreach ($courses as $course => $data) { + $rows[] = [ + $course, + $data['subscribed'], + $data['finished'], + ]; + } + + echo Display::tag('br', '', ['style' => 'margin-top: 25px;']); + echo Display::table($headers[1], $rows, []); + echo Display::tag('br', '', ['style' => 'margin-top: 25px;']); + echo Display::label(get_lang('AllUsersIncludingInactiveIncluded'), 'warning'); + + break; + case "report_quarterly_total_disk_usage": + + $accessUrlId = api_get_current_access_url_id(); + + if (api_is_windows_os()) { + $message = get_lang('SpaceUsedOnSystemCannotBeMeasuredOnWindows'); + } else { + $dir = api_get_path(SYS_PATH); + $du = exec('du -sh '.$dir, $err); + list($size, $none) = explode("\t", $du); + unset($none); + $limit = 0; + if (isset($_configuration[$accessUrlId]['hosting_limit_disk_space'])) { + $limit = $_configuration[$accessUrlId]['hosting_limit_disk_space']; + } + $message = sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'), $size, $limit); + } + echo Display::tag('H5', $message, ['style' => 'margin-bottom: 25px;']); + break; } diff --git a/main/inc/lib/api.lib.php b/main/inc/lib/api.lib.php index 152187344a..d60f11c844 100755 --- a/main/inc/lib/api.lib.php +++ b/main/inc/lib/api.lib.php @@ -10285,3 +10285,19 @@ function api_filename_has_blacklisted_stream_wrapper(string $filename) return false; } + +/** + * Calculate the percent between two numbers. + * + * @return string + */ +function api_calculate_increment_percent(int $newValue, int $oldValue) +{ + if ($oldValue <= 0) { + $result = " - "; + } else { + $result = ' '.round(100 * (($newValue / $oldValue) - 1), 2).' %'; + } + + return $result; +} diff --git a/main/inc/lib/internationalization.lib.php b/main/inc/lib/internationalization.lib.php index 362acf0661..ac8da3f287 100755 --- a/main/inc/lib/internationalization.lib.php +++ b/main/inc/lib/internationalization.lib.php @@ -1042,6 +1042,65 @@ function api_sort_by_first_name($language = null) return $sort_by_first_name[$language]; } +/** + * Return an array with the quarter dates. + * If no DateTime is not sent, use the current date. + * + * @param string|null $date (optional) The date or null. + * + * @return array E.G.: ['quarter_start' => '2022-10-11', + * 'quarter_end' => '2022-12-31', + * 'quarter_title' => 'Q4 2022'] + */ +function getQuarterDates(string $date = null): array +{ + if (empty($date)) { + $date = api_get_utc_datetime(); + } + + if (strlen($date > 10)) { + $date = substr($date, 0, 10); + } + + $month = substr($date, 5, 2); + $year = substr($date, 0, 4); + + switch ($month) { + case $month >= 1 && $month <= 3: + $start = "$year-01-01"; + $end = "$year-03-31"; + $quarter = 1; + break; + case $month >= 4 && $month <= 6: + $start = "$year-04-01"; + $end = "$year-06-30"; + $quarter = 2; + break; + case $month >= 7 && $month <= 9: + $start = "$year-07-01"; + $end = "$year-09-30"; + $quarter = 3; + break; + case $month >= 10 && $month <= 12: + $start = "$year-10-01"; + $end = "$year-12-31"; + $quarter = 4; + break; + default: + // Should never happen + $start = "$year-01-01"; + $end = "$year-03-31"; + $quarter = 1; + break; + } + + return [ + 'quarter_start' => $start, + 'quarter_end' => $end, + 'quarter_title' => sprintf(get_lang('QuarterXQY'), $quarter, $year), + ]; +} + /** * Multibyte string conversion functions. */ diff --git a/main/inc/lib/statistics.lib.php b/main/inc/lib/statistics.lib.php index 8f09b1388a..825cb70306 100644 --- a/main/inc/lib/statistics.lib.php +++ b/main/inc/lib/statistics.lib.php @@ -34,31 +34,43 @@ class Statistics /** * Count courses. * - * @param string $categoryCode Code of a course category. - * Default: count all courses. + * @param string|null $categoryCode Code of a course category. + * Default: count all courses. + * @param string|null $dateFrom dateFrom + * @param string|null $dateUntil dateUntil * * @return int Number of courses counted */ - public static function countCourses($categoryCode = null) + public static function countCourses(string $categoryCode = null, string $dateFrom = null, string $dateUntil = null) { - $course_table = Database::get_main_table(TABLE_MAIN_COURSE); - $access_url_rel_course_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); + $accessUrlRelCourseTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); $urlId = api_get_current_access_url_id(); if (api_is_multiple_url_enabled()) { $sql = "SELECT COUNT(*) AS number - FROM ".$course_table." as c, $access_url_rel_course_table as u - WHERE u.c_id = c.id AND access_url_id='".$urlId."'"; + FROM ".$courseTable." AS c, $accessUrlRelCourseTable AS u + WHERE u.c_id = c.id AND $accessUrlRelCourseTable='".$urlId."'"; if (isset($categoryCode)) { $sql .= " AND category_code = '".Database::escape_string($categoryCode)."'"; } } else { $sql = "SELECT COUNT(*) AS number - FROM $course_table"; + FROM $courseTable AS c + WHERE 1 = 1"; if (isset($categoryCode)) { - $sql .= " WHERE category_code = '".Database::escape_string($categoryCode)."'"; + $sql .= " WHERE c.category_code = '".Database::escape_string($categoryCode)."'"; } } + if (!empty($dateFrom)) { + $dateFrom = api_get_utc_datetime("$dateFrom 00:00:00"); + $sql .= " AND c.creation_date >= '$dateFrom' "; + } + + if (!empty($dateUntil)) { + $dateUntil = api_get_utc_datetime("$dateUntil 23:59:59"); + $sql .= " AND c.creation_date <= '$dateUntil' "; + } $res = Database::query($sql); $obj = Database::fetch_object($res); @@ -68,31 +80,55 @@ class Statistics /** * Count courses by visibility. * - * @param int $visibility visibility (0 = closed, 1 = private, 2 = open, 3 = public) all courses + * @param array|null $visibility visibility (0 = closed, 1 = private, 2 = open, 3 = public) all courses + * @param string|null $dateFrom dateFrom + * @param string|null $dateUntil dateUntil * * @return int Number of courses counted */ - public static function countCoursesByVisibility($visibility = null) - { - if (!isset($visibility)) { + public static function countCoursesByVisibility( + array $visibility = null, + string $dateFrom = null, + string $dateUntil = null + ) { + if (empty($visibility)) { return 0; + } else { + $visibilityString = ''; + $auxArrayVisibility = []; + if (!is_array($visibility)) { + $visibility = [$visibility]; + } + foreach ($visibility as $item) { + $auxArrayVisibility[] = (int) $item; + } + $visibilityString = implode(',', $auxArrayVisibility); } - $course_table = Database::get_main_table(TABLE_MAIN_COURSE); - $access_url_rel_course_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + + $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); + $accessUrlRelCourseTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); $urlId = api_get_current_access_url_id(); if (api_is_multiple_url_enabled()) { $sql = "SELECT COUNT(*) AS number - FROM $course_table as c, $access_url_rel_course_table as u - WHERE u.c_id = c.id AND access_url_id='".$urlId."'"; - if (isset($visibility)) { - $sql .= " AND visibility = ".intval($visibility); - } + FROM $courseTable AS c, $accessUrlRelCourseTable AS u + WHERE u.c_id = c.id AND u.access_url_id='".$urlId."'"; } else { - $sql = "SELECT COUNT(*) AS number FROM $course_table "; - if (isset($visibility)) { - $sql .= " WHERE visibility = ".intval($visibility); - } + $sql = "SELECT COUNT(*) AS number + FROM $courseTable AS c + WHERE 1 = 1"; + } + $sql .= " AND visibility IN ($visibilityString) "; + + if (!empty($dateFrom)) { + $dateFrom = api_get_utc_datetime("$dateFrom 00:00:00"); + $sql .= " AND c.creation_date >= '$dateFrom' "; + } + + if (!empty($dateUntil)) { + $dateUntil = api_get_utc_datetime("$dateUntil 23:59:59"); + $sql .= " AND c.creation_date <= '$dateUntil' "; } + $res = Database::query($sql); $obj = Database::fetch_object($res); @@ -1609,41 +1645,13 @@ class Statistics return '