AutorÃa | Ultima modificación | Ver Log |
<?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/>./*** Local functions.** @package report_coursesize* @copyright 2022 Catalyst IT {@link http://www.catalyst.net.nz}* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later*//*** Get sql snippet for course filesizes.* @return string*/function report_coursesize_filesize_sql() {$sqlunion = "UNION ALLSELECT c.id, f.filesizeFROM {block_instances} biJOIN {context} cx1 ON cx1.contextlevel = ".CONTEXT_BLOCK. " AND cx1.instanceid = bi.idJOIN {context} cx2 ON cx2.contextlevel = ". CONTEXT_COURSE. " AND cx2.id = bi.parentcontextidJOIN {course} c ON c.id = cx2.instanceidJOIN {files} f ON f.contextid = cx1.idUNION ALLSELECT c.id, f.filesizeFROM {course_modules} cmJOIN {context} cx ON cx.contextlevel = ".CONTEXT_MODULE." AND cx.instanceid = cm.idJOIN {course} c ON c.id = cm.courseJOIN {files} f ON f.contextid = cx.id";return "SELECT id AS course, SUM(filesize) AS filesizeFROM (SELECT c.id, f.filesizeFROM {course} cJOIN {context} cx ON cx.contextlevel = ".CONTEXT_COURSE." AND cx.instanceid = c.idJOIN {files} f ON f.contextid = cx.id {$sqlunion}) xGROUP BY id";}/*** Get sql snippet for backup filesizes.* @return string*/function report_coursesize_backupsize_sql() {return "SELECT id AS course, SUM(filesize) AS filesizeFROM (SELECT c.id, f.filesizeFROM {course} cJOIN {context} cx ON cx.contextlevel = ".CONTEXT_COURSE." AND cx.instanceid = c.idJOIN {files} f ON f.contextid = cx.id AND f.component = 'backup') xGROUP BY id";}/*** Helper function to return user file sizes.** @return void*/function report_coursesize_usersize_sql() {return "SELECT userid, sum(filesize) totalsizeFROM {files}WHERE userid is not nullGROUP BY userid ORDER BY totalsize DESC";}/*** Helper function to return top users who have most data.* It also does caching when necessary.** @return array with the user data from DB or cache*/function report_coursesize_get_usersizes() {global $DB;$usercache = \cache::make('report_coursesize', 'topuserdata');$data = $usercache->get('usersizes');if ($data && (time() < $data->expiry)) { // Valid cache data.$usersizes = $data->usersizes;} else {$usersizes = $DB->get_records_sql(report_coursesize_usersize_sql(), [], 0, get_config('report_coursesize', 'numberofusers'));if (!empty($usersizes)) {$data = new \stdClass();// Set expiry period 24 hours.$data->expiry = time() + 24 * 60 * 60;$data->usersizes = $usersizes;$usercache->set('usersizes', $data);}}return $usersizes;}