From 087cc147bf51ae8123a30a3505346b43939b3150 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 13 Feb 2024 15:42:41 -0700 Subject: [PATCH] feature - Course Size tool updates for testing. --- report/coursesize/course.php | 147 ++++++++++++++++-- report/coursesize/index.php | 18 ++- .../coursesize/lang/en/report_coursesize.php | 5 + report/coursesize/settings.php | 46 +++++- theme/lsu.php | 144 ++++++++++++++++- theme/snap/layout/course.php | 9 +- 6 files changed, 336 insertions(+), 33 deletions(-) diff --git a/report/coursesize/course.php b/report/coursesize/course.php index 809f9e4efa8ed..9b2c528c81b6a 100644 --- a/report/coursesize/course.php +++ b/report/coursesize/course.php @@ -24,39 +24,158 @@ require_once('../../config.php'); require_once($CFG->libdir.'/adminlib.php'); +require_once($CFG->dirroot.'/theme/lsu.php'); $courseid = required_param('id', PARAM_INT); -admin_externalpage_setup('reportcoursesize'); +// $isspeshul = lsu_snippets::testy($courseid); + + +$isspeshul = lsu_snippets::role_check_course_size($courseid); + +// Check to see if we are allowing special access to this page. +if (!$isspeshul['found']) { + admin_externalpage_setup('reportcoursesize'); +} else { + + // Getting the following warnings as they are done in admin_externalpage_setup() + + // - $PAGE->context was not set. + // - You may have forgotten to call require_login() or $PAGE->set_context(). + // - This page did not call $PAGE->set_url(...). Using http://lsu/report/coursesize/course.php?id=38581 +} $course = $DB->get_record('course', array('id' => $courseid)); $context = context_course::instance($course->id); $contextcheck = $context->path . '/%'; -$sizesql = "SELECT a.component, a.filearea, SUM(a.filesize) as filesize - FROM (SELECT DISTINCT f.contenthash, f.component, f.filesize, f.filearea - FROM {files} f - JOIN {context} ctx ON f.contextid = ctx.id - WHERE ".$DB->sql_concat('ctx.path', "'/'")." LIKE ? - AND f.filename != '.') a - GROUP BY a.component, a.filearea"; +// $sizesql = "SELECT a.component, a.filearea, SUM(a.filesize) as filesize +// FROM (SELECT DISTINCT f.contenthash, f.component, f.filesize, f.filearea +// FROM {files} f +// JOIN {context} ctx ON f.contextid = ctx.id +// WHERE ".$DB->sql_concat('ctx.path', "'/'")." LIKE ? +// AND f.filename != '.') a +// GROUP BY a.component, a.filearea"; + +$sizesql = "SELECT mcs.section, mcs.name, mm.name as modname, ff.filename, + ff.filesize, ff.filearea AS filearea, ff.component AS filecomp + FROM ( + SELECT ctx.id, ctx.instanceid, f.filename, f.filesize, f.filearea, f.component + FROM {files} f + JOIN {context} ctx ON f.contextid = ctx.id + WHERE ".$DB->sql_concat('ctx.path', "'/'")." LIKE ? AND f.filename != '.' + ) AS ff -$cxsizes = $DB->get_recordset_sql($sizesql, array($contextcheck)); + LEFT JOIN {course_modules} mcm ON mcm.id = ff.instanceid AND mcm.course=? + LEFT JOIN {modules} mm ON mm.id = mcm.module + LEFT JOIN {course_sections} mcs ON mcs.id = mcm.section + LEFT JOIN {course} mc ON mc.id = mcm.course + + ORDER BY mcs.section"; + +$cxsizes = $DB->get_recordset_sql($sizesql, array($contextcheck, $courseid)); $coursetable = new html_table(); -$coursetable->align = array('right', 'right', 'right'); -$coursetable->head = array(get_string('plugin'), get_string('filearea', 'report_coursesize'), get_string('size')); -$coursetable->data = array(); +$coursetable->attributes['class'] = 'table'; +$coursetable->responsive = true; +// $coursetable->align = array('right', 'right', 'right'); + +// $coursetable->head = array( +// // 'Section', +// 'Section Name', +// // get_string('plugin'), +// 'Activity Type', +// // get_string('filearea','report_coursesize'), +// 'File name', +// get_string('size') +// ); + +// $coursetable->rowclasses[0] = 'fucker'; +// $tableheader = new html_table_cell( + +// 'Section Name', +// 'Activity Type', +// 'File name', +// get_string('size') +// ), +// array('id'=>'headerblahblahblah'))); +// $tableheader->colspan = count($coursetable->head); +// $tableheader->colclasses = array ('centeralign'); +$fackisthis = new html_table_cell('Section Name'); + +error_log("\n\nThe fack is this: ". var_dump($fackisthis). "\n\n"); +error_log("\n\nThe fack is this: ". print_r($fackisthis, true). "\n\n"); + +$headerlist = array( + new html_table_cell('Section Name'), + new html_table_cell('Activity Type'), + new html_table_cell('File name'), + new html_table_cell(get_string('size')) +); +$tableheader = new html_table_row($headerlist); + +$tableheader->header = true; +$tableheader->attributes['class'] = 'table-primary bold'; +$coursetable->data[] = $tableheader; + +$sectionstart = true; +$currentsection = 0; +$sizetotal = 0; foreach ($cxsizes as $cxdata) { + $activitytype = $cxdata->modname; + + if ($cxdata->section == null) { + $activitytype = $cxdata->filearea; + $sectionlink = ''; + } else { + // Each time the loop hits a new section let's reset and then create a header. + if ($currentsection != $cxdata->section) { + $sectionstart = true; + $currentsection = $cxdata->section; + } + + $sectionlink = '#section-'.$cxdata->section; + if ($sectionstart) { + // make the rest of the rows for the course section regular. + $header = new html_table_cell(html_writer::tag('span', "Section ".$cxdata->section, array('id'=>'coursesize_header'))); + $header->header = true; + $header->colspan = count($headerlist); + $header->colclasses = array ('centeralign'); + $header = new html_table_row(array($header)); + $header->attributes['class'] = 'table-info'; + + $sectionstart = false; + $coursetable->data[] = $header; + } + } + $row = array(); - $row[] = $cxdata->component; - $row[] = $cxdata->filearea; + // $row[] = $cxdata->section; + $row[] = $cxdata->name; + $row[] = $activitytype; + $row[] = ''.$cxdata->filename.''; $row[] = display_size($cxdata->filesize); $coursetable->data[] = $row; + + $sizetotal += $cxdata->filesize; } + +// Now the final total row. +$footertitle = new html_table_cell(html_writer::tag('span', "Total Size: ", array())); +$footersize = new html_table_cell(html_writer::tag('span', display_size($sizetotal), array())); +$footertitle->colspan = count($headerlist) - 1; +$footer = new html_table_row(array( + $footertitle, + $footersize +)); +$footer->cells[0]->style = 'text-align: right;'; +// $footer->cells[1]->style = 'text-align: right;'; +$footer->attributes['class'] = 'table-primary bold'; +$coursetable->data[] = $footer; + $cxsizes->close(); // Calculate filesize shared with other courses. diff --git a/report/coursesize/index.php b/report/coursesize/index.php index aef2bb38a3642..97c3e30f03c75 100644 --- a/report/coursesize/index.php +++ b/report/coursesize/index.php @@ -2,7 +2,7 @@ // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by + // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // @@ -96,8 +96,7 @@ $totaldate = get_string('never'); $totalusage = 0; } - - $totalusagereadable = display_size($totalusage); + $totalusagereadable = display_size((int)$totalusage); $systemsize = $systembackupsize = 0; $coursesql = 'SELECT cx.id, c.id as courseid ' . @@ -189,7 +188,7 @@ $row[] = '' . $course->shortname . ''; $row[] = '' . $course->name . ''; - $readablesize = display_size($course->filesize); + $readablesize = display_size((int)$course->filesize); $a = new stdClass; $a->bytes = $course->filesize; $a->shortname = $course->shortname; @@ -199,7 +198,10 @@ $summarylink = new moodle_url('/report/coursesize/course.php', array('id' => $course->id)); $summary = html_writer::link($summarylink, ' ' . get_string('coursesummary', 'report_coursesize')); $row[] = "shortname . "\" title=\"$bytesused\">$readablesize" . $summary; - $row[] = "" . display_size($course->backupsize) . ""; + if ($course->backupsize == "" || $course->backupsize == null) { + $course->backupsize = 0; + } + $row[] = "" . display_size((int)$course->backupsize) . ""; $coursetable->data[] = $row; $downloaddata[] = array($course->shortname, $course->name, str_replace(',', '', $readablesize), str_replace(',', '', display_size($course->backupsize))); @@ -231,9 +233,15 @@ $row[] = display_size($totalsize); $row[] = display_size($totalbackupsize); $coursetable->data[] = $row; + + + $downloaddata[] = [get_string('total'), '', display_size($totalsize), display_size($totalbackupsize)]; unset($courses); + + + $systemsizereadable = display_size($systemsize); $systembackupreadable = display_size($systembackupsize); diff --git a/report/coursesize/lang/en/report_coursesize.php b/report/coursesize/lang/en/report_coursesize.php index 61b750868bcb2..6be1f0d303396 100644 --- a/report/coursesize/lang/en/report_coursesize.php +++ b/report/coursesize/lang/en/report_coursesize.php @@ -57,6 +57,11 @@ // BEGIN LSU - Store course size and history. $string['keephistory'] = 'Keep size history'; $string['keephistoryhelp'] = 'Keep the course size history to see growth rate.'; +$string['rolepicker'] = 'Additional roles to have access.'; +$string['rolepickerhelp'] = 'List the role followed by true/false to see course size meter and the course breakdown.'. + '
For example:'. + '
TA, false'. + '
Manager, true'; // END LSU - Store course size and history. $string['filearea'] = 'File area'; diff --git a/report/coursesize/settings.php b/report/coursesize/settings.php index e48e71a8b1660..3a7e09965aa63 100644 --- a/report/coursesize/settings.php +++ b/report/coursesize/settings.php @@ -33,6 +33,7 @@ $settings = new admin_settingpage('report_coursesize_settings', new lang_string('pluginname', 'report_coursesize')); if ($ADMIN->fulltree) { + $settings->add(new admin_setting_configselect( 'report_coursesize/calcmethod', new lang_string('calcmethod', 'report_coursesize'), @@ -52,11 +53,44 @@ 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 - )); + $settings->add( + new admin_setting_configcheckbox( + 'report_coursesize/keephistory', + get_string('keephistory', 'report_coursesize'), + get_string('keephistoryhelp', 'report_coursesize'), + 1 + ) + ); + + // $settings->add( + // new admin_setting_configmultiselect( + // 'report_coursesize/roles', + // get_string('rolepicker', 'report_coursesize'), + // get_string('rolepickerhelp', 'report_coursesize'), + // null, + // $options + // ) + // ); + // $settings->add( + // new admin_setting_pickroles( + // 'report_coursesize/roles', + // get_string('config_roles', 'block_backadel'), + // get_string('config_roles_desc', + // 'block_backadel'), + // array() + // ) + // ); + // -------------------------------- + // Manually added role Settings. + $options = ["LXD", "IT", "Teachers", "Managers"]; + $settings->add( + new admin_setting_configtextarea( + 'report_coursesize_manualroles', + get_string('rolepicker', 'report_coursesize'), + get_string('rolepickerhelp', 'report_coursesize'), + $options, + PARAM_TEXT + ) + ); // END LSU - Course size history. } diff --git a/theme/lsu.php b/theme/lsu.php index 535797e730fb3..b6dc8e5394a90 100644 --- a/theme/lsu.php +++ b/theme/lsu.php @@ -38,7 +38,7 @@ class lsu_theme_snippets { * @param int $isadmin - Is the current user admin or no? * @return string - The html to display. */ - public function show_course_size($isadmin = 0) { + public function show_course_size($isallowed = 0) { global $OUTPUT, $COURSE, $CFG, $USER; $coursesize = $this->get_file_size($COURSE->id); @@ -55,7 +55,8 @@ public function show_course_size($isadmin = 0) { // What is the percentage of it being full. $displayclass = $this->get_bootstrap_barlevel($percent); $show_course_size_link = ""; - if ($isadmin) { + + if ($isallowed) { $show_course_size_link = ' 720, Weekly => 168) + * + * Example2: comma + * LXD, true + * IT, false + * Becomes (LXD => true, IT => false) + + * Example3: index + * LXD + * IT + * Becomes (0 => LXD, 1 => IT) + * + * @param string $configstring setting + * @param string $arraytype by default multi, comma, use mirror to miror key/value + * @param bool $booly is the second param using bool? (see example2) + * + * @return array + */ + public static function config_to_array($configstring, $arraytype = "multi", $booly = false) { + + $configname = get_config('moodle', $configstring); + if ($configname == false) { + return false; + } + + $final = array(); + + if ($arraytype == "multi") { + $breakstripped = preg_replace("/\r|\n/", " ", $configname); + + self::quick_string_clean($breakstripped); + $exploded = explode(" ", $breakstripped); + $explodedcount = count($exploded); + // Now convert to array and transform to an assoc. array. + for ($i = 0; $i < $explodedcount; $i += 2) { + $final[$exploded[$i + 1]] = $exploded[$i]; + } + + } else if ($arraytype == "comma") { + // Replace the line breaks with tilde. + $breakstripped = preg_replace("/\r|\n/", "~", $configname); + // Clean the string. + $breakstripped = self::quick_string_clean($breakstripped); + // You might get a double tilde depending on OS + $breakstripped = str_replace("~~", "~", $breakstripped); + // Now break into chunks + $exploded = explode("~", $breakstripped); + $explodedcount = count($exploded); + // Now convert to array and transform to an assoc. array. + for ($i = 0; $i < $explodedcount; $i++) { + $temp = explode(",", $exploded[$i]); + $temp[1] = $booly ? (bool)$temp[1] : $temp[1]; + $final[$temp[0]] = $temp[1]; + } + } else if ($arraytype == "index") { + + $final = $exploded; + } + return $final; + } + + public static function role_check_course_size($cid = 0) { + global $OUTPUT, $COURSE, $CFG, $USER; + + $found = false; + $access = false; + + $context = context_course::instance($cid); + $roles = get_user_roles($context, $USER->id, true); + + if (empty($roles)) { + return array( + "found" => false, + "access" => false + ); + } + $role = key($roles); + $rolename = $roles[$role]->shortname; + $customroles = self::config_to_array('report_coursesize_manualroles', "comma", true); + + + foreach ($customroles as $k => $v) { + if (strtolower($k) == strtolower($rolename)) { + $found = true; + $access = $v; + } + } + return array( + "found" => $found, + "access" => $access + ); + } + + public static function testy() { + + global $DB; + + $courseid = 38581; + $course = $DB->get_record('course', array('id' => $courseid)); + $info = get_fast_modinfo($course); + + print_object($info); + } +} \ No newline at end of file diff --git a/theme/snap/layout/course.php b/theme/snap/layout/course.php index f520637605ff9..cdb169b9b6f71 100644 --- a/theme/snap/layout/course.php +++ b/theme/snap/layout/course.php @@ -80,14 +80,17 @@ if ($showcs) { include_once($CFG->dirroot. "/theme/lsu.php"); $showcssnippet = new lsu_theme_snippets(); + $roles = get_config('report_coursesize', 'report_coursesize/roles'); // Was calling this func twice so call once and send to show_course_size. $isadmin = is_siteadmin(); - // Then later in code: - if (!$showcssnippet->are_you_student($isadmin)) { + $isspeshul = lsu_snippets::role_check_course_size($COURSE->id); + // Checks if they are a student or someone that can edit grades + // Also checks if they match a role in the settings that is allowed. + if (!$showcssnippet->are_you_student()) { // User does NOT have a student role in a course. echo '
'; - echo $showcssnippet->show_course_size($isadmin); + echo $showcssnippet->show_course_size($isadmin ?: $isspeshul["access"]); echo '
'; } }