Skip to content

Commit

Permalink
Update coursesize plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rrusso committed Feb 27, 2024
1 parent 59d8638 commit 46e366d
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 31 deletions.
132 changes: 117 additions & 15 deletions report/coursesize/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,141 @@

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::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";

$cxsizes = $DB->get_recordset_sql($sizesql, array($contextcheck));
// Old query.
// $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
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";

/* TODO: REMOVE ME.
echo"<pre>";
echo"\nContext Check: ";
var_dump($contextcheck);
echo"\nCourse ID: ";
var_dump($courseid);
echo"\nSQL: ";
var_dump($sizesql);
echo"</pre>";
*/

$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;

$fackisthis = new html_table_cell('Section Name');

$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->name;
$row[] = $activitytype;
$row[] = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$courseid. $sectionlink.'">'.$cxdata->filename.'</a>';
$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.
Expand Down
18 changes: 13 additions & 5 deletions report/coursesize/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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 ' .
Expand Down Expand Up @@ -189,7 +188,7 @@
$row[] = '<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $course->id . '">' . $course->shortname . '</a>';
$row[] = '<a href="' . $CFG->wwwroot . '/course/index.php?categoryid=' . $course->category . '">' . $course->name . '</a>';

$readablesize = display_size($course->filesize);
$readablesize = display_size((int)$course->filesize);
$a = new stdClass;
$a->bytes = $course->filesize;
$a->shortname = $course->shortname;
Expand All @@ -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[] = "<span id=\"coursesize_" . $course->shortname . "\" title=\"$bytesused\">$readablesize</span>" . $summary;
$row[] = "<span title=\"$backupbytesused\">" . display_size($course->backupsize) . "</span>";
if ($course->backupsize == "" || $course->backupsize == null) {
$course->backupsize = 0;
}
$row[] = "<span title=\"$backupbytesused\">" . display_size((int)$course->backupsize) . "</span>";
$coursetable->data[] = $row;
$downloaddata[] = array($course->shortname, $course->name, str_replace(',', '', $readablesize),
str_replace(',', '', display_size($course->backupsize)));
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions report/coursesize/lang/en/report_coursesize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'.
'<br>For example:'.
'<br>TA, false'.
'<br>Manager, true';
// END LSU - Store course size and history.

$string['filearea'] = 'File area';
Expand Down
46 changes: 40 additions & 6 deletions report/coursesize/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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.
}
Loading

0 comments on commit 46e366d

Please sign in to comment.