Skip to content

Commit

Permalink
feature - fourOneMerge - Course size report tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticDalo committed Nov 27, 2023
1 parent 1074b7a commit 113bc79
Show file tree
Hide file tree
Showing 23 changed files with 2,393 additions and 0 deletions.
11 changes: 11 additions & 0 deletions report/coursesize/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: ci

on: [push, pull_request]

jobs:
test:
uses: catalyst/catalyst-moodle-workflows/.github/workflows/ci.yml@main
secrets:
moodle_org_token: ${{ secrets.MOODLE_ORG_TOKEN }}
with:
disable_phpunit: true
23 changes: 23 additions & 0 deletions report/coursesize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Course Size Report

This plugin provides approximate disk usage by Moodle courses.


## Branches

| Moodle version | Branch | PHP |
| ----------------- | ------------------ | ---- |
| Moodle 3.9+ | `MOODLE_39_STABLE` | 7.2+ |
| Totara 12+ | `totara-master` | 7.2+ |
| Moodle 3.5 to 3.8 | `MOODLE_35_STABLE` | 7.2+ |

There are 2 known shortcomings with this plugin
* If the same file is used multiple times within a course, the report will report an inflated disk usage figure as the files
will be counted each time even though Moodle only stores one copy of the file on disk.
* If the same file is used within multiple courses it will be counted in each course and there is no indicator within the
report to inform the user if they delete the course or files within the course they will not free that amount from disk.

It should be possible to improve the report to address these issues - we'd greatly appreciate any patches to improve the plugin!


Copyright 2014 Catalyst IT http://www.catalyst.net.nz
46 changes: 46 additions & 0 deletions report/coursesize/classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Privacy Subsystem implementation for report_courseoverview.
*
* @package report_coursesize
* @copyright 2018 Catalyst IT
* @author Dan Marsden
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace report_coursesize\privacy;

/**
* Privacy Subsystem for report_coursesize implementing null_provider.
*
* @copyright 2018 Catalyst IT
* @author Dan Marsden
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason() : string {
return 'privacy:metadata';
}
}
96 changes: 96 additions & 0 deletions report/coursesize/classes/task/report_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* report_coursesize tasks
*
* @package report_coursesize
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_coursesize\task;

/**
* report_async tasks
*
* @package report_coursesize
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_async extends \core\task\scheduled_task {

/**
* Get task name
*/
public function get_name() {
return get_string('pluginname', 'report_coursesize');
}

/**
* Execute task
*/
public function execute() {
global $DB, $CFG;
require_once($CFG->dirroot . '/report/coursesize/locallib.php');

if (get_config('report_coursesize', 'calcmethod') == 'cron') {

mtrace("Generating report_coursesize cache...");
set_time_limit(0);

// First we delete the old data, then we re-populate it, wrap in a transaction to help keep it together.
$transaction = $DB->start_delegated_transaction();

// Clean up cache table.
$DB->delete_records('report_coursesize');

// Generate report_coursesize table.
$basesql = report_coursesize_filesize_sql();
$sql = "INSERT INTO {report_coursesize} (course, filesize) $basesql ";
$DB->execute($sql);

// Now calculate size of backups.
$basesql = report_coursesize_backupsize_sql();

$sql = "UPDATE {report_coursesize} rc
SET backupsize = (SELECT bf.filesize FROM ($basesql) bf WHERE bf.course = rc.course)";
$DB->execute($sql);

$transaction->allow_commit();

// Ignore the result now. The call will only cache the data internally.
report_coursesize_get_usersizes();
set_config('coursesizeupdated', time(), 'report_coursesize');

mtrace("report_coursesize cache updated.");
}
// Check if the path ends with a "/" otherwise an exception will be thrown.
$sitedatadir = $CFG->dataroot;
if (is_dir($sitedatadir)) {
// Only append a "/" if it doesn't already end with one.
if (substr($sitedatadir, -1) !== '/') {
$sitedatadir .= '/';
}
}

// Total files usage either hasn't been stored, or is out of date.
$totalusage = get_directory_size($sitedatadir);
set_config('filessize', $totalusage, 'report_coursesize');
set_config('filessizeupdated', time(), 'report_coursesize');

mtrace("report_coursesize overall directory size updated");
}
}
89 changes: 89 additions & 0 deletions report/coursesize/course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Course breakdown.
*
* @package report_coursesize
* @copyright 2017 Catalyst IT {@link http://www.catalyst.net.nz}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');

$courseid = required_param('id', PARAM_INT);

admin_externalpage_setup('reportcoursesize');

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

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

foreach ($cxsizes as $cxdata) {
$row = array();
$row[] = $cxdata->component;
$row[] = $cxdata->filearea;
$row[] = display_size($cxdata->filesize);

$coursetable->data[] = $row;
}
$cxsizes->close();

// Calculate filesize shared with other courses.
$sizesql = "SELECT SUM(filesize) FROM (SELECT DISTINCT contenthash, filesize
FROM {files} f
JOIN {context} ctx ON f.contextid = ctx.id
WHERE ".$DB->sql_concat('ctx.path', "'/'")." NOT LIKE ?
AND f.contenthash IN (SELECT DISTINCT f.contenthash
FROM {files} f
JOIN {context} ctx ON f.contextid = ctx.id
WHERE ".$DB->sql_concat('ctx.path', "'/'")." LIKE ?
AND f.filename != '.')) b";
$size = $DB->get_field_sql($sizesql, array($contextcheck, $contextcheck));
if (!empty($size)) {
$size = display_size($size);
}


// All the processing done, the rest is just output stuff.

print $OUTPUT->header();

print $OUTPUT->heading(get_string('coursesize', 'report_coursesize'). " - ". format_string($course->fullname));
print $OUTPUT->box(get_string('coursereport', 'report_coursesize'));
if (!empty($size)) {
print $OUTPUT->box(get_string('sharedusagecourse', 'report_coursesize', $size));
}

print html_writer::table($coursetable);
print $OUTPUT->footer();
38 changes: 38 additions & 0 deletions report/coursesize/db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Version information
*
* @package report_coursesize
* @copyright 2014 Catalyst IT {@link http://www.catalyst.net.nz}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

/* allows the user to view the site's data usage summary */
'report/coursesize:view' => array(
'riskbitmask' => RISK_CONFIG,
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
),
);
34 changes: 34 additions & 0 deletions report/coursesize/db/caches.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
// 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Plugin cache definitions.
*
* @package report_coursesize
* @author Kateryna Degtyariova <[email protected]>
* @copyright 2022 Catalyst IT {@link http://www.catalyst.net.nz}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$definitions = array(
'topuserdata' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
'simpledata' => true,
),
);
22 changes: 22 additions & 0 deletions report/coursesize/db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="report/coursesize/db" VERSION="20220308" COMMENT="XMLDB file for Moodle report/coursesize"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="report_coursesize" COMMENT="Coursesize calculations cache.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<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"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
Loading

0 comments on commit 113bc79

Please sign in to comment.