From 48c8b5d609df6972205082e06c53f1c5125ff682 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 30 Nov 2023 14:03:24 -0700 Subject: [PATCH] feature - fourOneMerge - Course size report - cleanup and comments --- .../coursesize/classes/task/report_async.php | 4 +- report/coursesize/db/install.xml | 2 + report/coursesize/db/upgrade.php | 3 +- report/coursesize/index.php | 4 +- .../coursesize/lang/en/report_coursesize.php | 4 +- report/coursesize/locallib.php | 3 + report/coursesize/settings.php | 3 +- theme/lsu.php | 126 +++++------------- 8 files changed, 50 insertions(+), 99 deletions(-) diff --git a/report/coursesize/classes/task/report_async.php b/report/coursesize/classes/task/report_async.php index a4c2844d722b6..d99d104234fb3 100644 --- a/report/coursesize/classes/task/report_async.php +++ b/report/coursesize/classes/task/report_async.php @@ -46,7 +46,7 @@ public function execute() { global $DB, $CFG; require_once($CFG->dirroot . '/report/coursesize/locallib.php'); - + // BEGIN LSU - Store course size and history. // Are we using cron or no? if (get_config('report_coursesize', 'calcmethod') == 'cron') { @@ -82,6 +82,8 @@ public function execute() { WHERE bf.course = rc.course $historysql )"; + + // END LSU - Store course size and history. $DB->execute($sql); $transaction->allow_commit(); diff --git a/report/coursesize/db/install.xml b/report/coursesize/db/install.xml index 60542d75b2711..a9564f72927e4 100644 --- a/report/coursesize/db/install.xml +++ b/report/coursesize/db/install.xml @@ -10,7 +10,9 @@ + + diff --git a/report/coursesize/db/upgrade.php b/report/coursesize/db/upgrade.php index 5be7a44655976..9aca92e275bf7 100644 --- a/report/coursesize/db/upgrade.php +++ b/report/coursesize/db/upgrade.php @@ -60,7 +60,7 @@ function xmldb_report_coursesize_upgrade($oldversion) { // Coursesize savepoint reached. upgrade_plugin_savepoint(true, 2021030802, 'report', 'coursesize'); } - + // BEGIN LSU - Store course size and history. if ($oldversion < 2023112801) { $rctable = new xmldb_table('report_coursesize'); @@ -70,5 +70,6 @@ function xmldb_report_coursesize_upgrade($oldversion) { } } + // END LSU - Store course size and history. return true; } diff --git a/report/coursesize/index.php b/report/coursesize/index.php index 48e1ecec6886c..db8551cbbc049 100644 --- a/report/coursesize/index.php +++ b/report/coursesize/index.php @@ -129,7 +129,7 @@ if (isset($reportconfig->calcmethod) && ($reportconfig->calcmethod) == 'live') { $live = true; } - // BEGIN LSU In case course size history is enabled. + // BEGIN LSU - Store course size and history. if ($live) { $filesql = report_coursesize_filesize_sql(); $sql = "SELECT c.id, c.shortname, c.category, ca.name, rc.filesize, rc.timestamp @@ -151,7 +151,7 @@ $extracoursesql $bytimestamp ORDER BY rc.filesize DESC"; - // END LSU In case course size history is enabled. + // END LSU - Store course size and history. $courses = $DB->get_records_sql($sql, $courseparams); diff --git a/report/coursesize/lang/en/report_coursesize.php b/report/coursesize/lang/en/report_coursesize.php index 2802bb5c05f19..61b750868bcb2 100644 --- a/report/coursesize/lang/en/report_coursesize.php +++ b/report/coursesize/lang/en/report_coursesize.php @@ -54,10 +54,10 @@ $string['numberofusers'] = 'Top number of users'; $string['numberofusershelp'] = 'How many of the top users the report will show.'; -// BEGIN LSU Course Size. +// BEGIN LSU - Store course size and history. $string['keephistory'] = 'Keep size history'; $string['keephistoryhelp'] = 'Keep the course size history to see growth rate.'; -// END LSU Course Size. +// END LSU - Store course size and history. $string['filearea'] = 'File area'; $string['allcourses'] = 'All courses'; diff --git a/report/coursesize/locallib.php b/report/coursesize/locallib.php index 1e689948c1371..2ca45601cfd75 100644 --- a/report/coursesize/locallib.php +++ b/report/coursesize/locallib.php @@ -26,7 +26,10 @@ * Get sql snippet for course filesizes. * @return string */ +// BEGIN LSU - Store course size and history. +// Added - $processtime as func param and in return. function report_coursesize_filesize_sql($processtime = 0) { +// END LSU - Store course size and history. $sqlunion = "UNION ALL SELECT c.id, f.filesize FROM {block_instances} bi diff --git a/report/coursesize/settings.php b/report/coursesize/settings.php index 5e7345db4fcd0..e48e71a8b1660 100644 --- a/report/coursesize/settings.php +++ b/report/coursesize/settings.php @@ -51,11 +51,12 @@ 10, PARAM_INT )); - + // BEGIN LSU - Course size history. $settings->add(new admin_setting_configcheckbox( 'report_coursesize/keephistory', get_string('keephistory', 'report_coursesize'), get_string('keephistoryhelp', 'report_coursesize'), 1 )); + // END LSU - Course size history. } diff --git a/theme/lsu.php b/theme/lsu.php index 30f35ce2d344d..181a26e76649f 100644 --- a/theme/lsu.php +++ b/theme/lsu.php @@ -23,22 +23,21 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -// namespace theme_lsu; - defined('MOODLE_INTERNAL') || die(); -// use moodle_url; -// use stdClass; - global $CFG; /** * General LSU Class with various functions. - * */ class lsu_theme_snippets { - + + /** + * Little snippet to display the size of the course using the bootstrap + * progressbar. + * @param int $isadmin - Is the current user admin or no? + * @return string - The html to display. + */ public function show_course_size($isadmin = 0) { global $OUTPUT, $COURSE, $CFG, $USER; @@ -52,7 +51,7 @@ public function show_course_size($isadmin = 0) { // number_format( $myNumber, 2, '.', '' ); // Let's format this number so it's readable. $size = $this->formatBytes($coursesize); - + // What is the percentage of it being full. $displayclass = $this->get_bootstrap_barlevel($percent); $show_course_size_link = ""; @@ -78,12 +77,12 @@ public function show_course_size($isadmin = 0) { '" style="width: ' . $percent . '%">' . '' . $percentage . '%' . ''; - + return $coursesnippet; } /** - * Based on the percentage show the type of bar to use. + * Based on the percentage show the type of bar to use. * @param [int] $percentage number ranging from 0-100 * @return [string] partial string used in the div-class. */ @@ -98,14 +97,18 @@ private function get_bootstrap_barlevel($percentage) { } else if ($percentage >= 75) { return "danger"; } - } + /** + * Get the total file size of a course. + * @param int $courseid - The course id. + * @return int - Total size. + */ private function get_file_size($courseid = 0) { - + global $COURSE, $DB; if ($courseid == 0) { - $courseid = $COURSE->id; + $courseid = $COURSE->id; } // Search the report_coursesize table first. @@ -163,106 +166,45 @@ private function get_file_size($courseid = 0) { } } - private function formatBytes($bytes, $precision = 2) { - $units = array('B', 'KB', 'MB', 'GB', 'TB'); + /** + * Format a data size number to make it human readable. + * @param int $bytes - The size in bytes. + * @param int $precision - The number of decimal places. + * @return bool + */ + private function formatBytes($bytes, $precision = 2) { + $units = array('B', 'KB', 'MB', 'GB', 'TB'); - $bytes = max($bytes, 0); - $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); - $pow = min($pow, count($units) - 1); + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); - // Uncomment one of the following alternatives $bytes = $bytes / pow(1024, $pow); - // $bytes2 = $bytes / (1 << (10 * $pow)); - return round($bytes, $precision) . ' ' . $units[$pow]; + return round($bytes, $precision) . ' ' . $units[$pow]; } - /* - function is_user_with_role($rolename, $courseid = 0, $userid = 0) { - $result = false; - global $DB, $USER, $COURSE; - if ($courseid == 0) { - $courseid = $COURSE->id; - } - if ($userid == 0) { - $userid = $USER->id; - } - - $roles = get_user_roles(context_course::instance($courseid), $userid, false); - foreach ($roles as $role) { - if ($role->shortname == $rolename) { - $result = true; - break; - } - } - return $result; - } - */ - /** - * Check to see if this user is the role you are searching for in current course. - * @param integer $courseid The course id, will get from GLOBAL if not passed in. - * @param integer $userid The user id, will get from GLOBAL if not passed in. - * @param string $role What role to search for. - * @return bool + * Check to see if this user is a student. + * @return bool */ function are_you_student() { global $DB, $COURSE, $USER, $SESSION; - + if (isset($SESSION->lsustudent) && $SESSION->lsustudent == true) { return $SESSION->lsustudent; } - - // $role = 'student'; + if ($COURSE->id == 0) { return; } $context = context_course::instance($COURSE->id); - // Test 0 ---------------------------------------- - // $time_start = microtime(true); + // If user can edit grades then let them see how big it is. $isStudent0 = has_capability('moodle/grade:edit', $context, $USER); - // $time_end = microtime(true); - // $execution_time0 = ($time_end - $time_start); - - // Test 1 ---------------------------------------- - // $time_start = microtime(true); - // $isStudent1 = current(get_user_roles($context, $USER->id))->shortname == 'student' ? true : false; // instead of shortname you can also use roleid - // $time_end = microtime(true); - // $execution_time1 = ($time_end - $time_start); - - // Test 2 ---------------------------------------- - // $time_start = microtime(true); - // $isStudent2 = !has_capability ('moodle/course:update', $context) ? true : false; - // $time_end = microtime(true); - // $execution_time2 = ($time_end - $time_start); - - // Test 3 ---------------------------------------- - // $time_start = microtime(true); - - // $sql = "SELECT * FROM mdl_role_assignments AS ra - // LEFT JOIN mdl_user_enrolments AS ue ON ra.userid = ue.userid - // LEFT JOIN mdl_role AS r ON ra.roleid = r.id - // LEFT JOIN mdl_context AS c ON c.id = ra.contextid - // LEFT JOIN mdl_enrol AS e ON e.courseid = c.instanceid AND ue.enrolid = e.id - // WHERE r.shortname = ? AND ue.userid = ? AND e.courseid = ?"; - - // $result = $DB->get_record_sql($sql, array($role, $USER->id, $COURSE->id)); - // $time_end = microtime(true); - // $execution_time3 = ($time_end - $time_start); - // $isStudent3 = false; - // if (isset($result->roleid)) { - // $isStudent3 = $result->roleid == 5 ? true : false; - // } - - // error_log("\n TEST 0 -> Are you the father: ". $isStudent0 ." - Total Execution Time0: ".$execution_time0." Mins\n"); - // error_log("\n TEST 1 -> Are you the father: ". $isStudent1 ." - Total Execution Time1: ".$execution_time1." Mins\n"); - // error_log("\n TEST 2 -> Are you the father: ". $isStudent2 ." - Total Execution Time2: ".$execution_time2." Mins\n"); - // error_log("\n TEST 3 -> Are you the father: ". $isStudent3 ." - Total Execution Time3: ".$execution_time3." Mins\n"); - if ($isStudent0 == true) { // They are NOT a student, return false. $SESSION->lsupstudent = false;