Skip to content

Commit

Permalink
feature - fourOneMerge - Course size report - cleanup and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticDalo committed Nov 30, 2023
1 parent 9e75ec9 commit 48c8b5d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 99 deletions.
4 changes: 3 additions & 1 deletion report/coursesize/classes/task/report_async.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions report/coursesize/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="course id"/>
<FIELD NAME="filesize" TYPE="int" LENGTH="15" NOTNULL="true" SEQUENCE="false" COMMENT="Context size in bytes"/>
<FIELD NAME="backupsize" TYPE="int" LENGTH="15" NOTNULL="false" SEQUENCE="false" COMMENT="size of backups"/>
<!-- BEGIN LSU - Store course size and history. -->
<FIELD NAME="timestamp" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The timestamp at which the record was modified."/>
<!-- END LSU - Store course size and history. -->
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
3 changes: 2 additions & 1 deletion report/coursesize/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -70,5 +70,6 @@ function xmldb_report_coursesize_upgrade($oldversion) {
}

}
// END LSU - Store course size and history.
return true;
}
4 changes: 2 additions & 2 deletions report/coursesize/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions report/coursesize/lang/en/report_coursesize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
3 changes: 3 additions & 0 deletions report/coursesize/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion report/coursesize/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
126 changes: 34 additions & 92 deletions theme/lsu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = "";
Expand All @@ -78,12 +77,12 @@ public function show_course_size($isadmin = 0) {
'" style="width: ' . $percent . '%">' .
'<span class="fg-' . $displayclass . '">' . $percentage . '%</span>' .
'</div></div>';

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.
*/
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 48c8b5d

Please sign in to comment.