Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Local functions.
19
 *
20
 * @package    report_coursesize
21
 * @copyright  2022 Catalyst IT {@link http://www.catalyst.net.nz}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Get sql snippet for course filesizes.
27
 * @return string
28
 */
29
function report_coursesize_filesize_sql() {
30
    $sqlunion = "UNION ALL
31
                    SELECT c.id, f.filesize
32
                    FROM {block_instances} bi
33
                    JOIN {context} cx1 ON cx1.contextlevel = ".CONTEXT_BLOCK. " AND cx1.instanceid = bi.id
34
                    JOIN {context} cx2 ON cx2.contextlevel = ". CONTEXT_COURSE. " AND cx2.id = bi.parentcontextid
35
                    JOIN {course} c ON c.id = cx2.instanceid
36
                    JOIN {files} f ON f.contextid = cx1.id
37
                UNION ALL
38
                    SELECT c.id, f.filesize
39
                    FROM {course_modules} cm
40
                    JOIN {context} cx ON cx.contextlevel = ".CONTEXT_MODULE." AND cx.instanceid = cm.id
41
                    JOIN {course} c ON c.id = cm.course
42
                    JOIN {files} f ON f.contextid = cx.id";
43
 
44
    return "SELECT id AS course, SUM(filesize) AS filesize
45
              FROM (SELECT c.id, f.filesize
46
                      FROM {course} c
47
                      JOIN {context} cx ON cx.contextlevel = ".CONTEXT_COURSE." AND cx.instanceid = c.id
48
                      JOIN {files} f ON f.contextid = cx.id {$sqlunion}) x
49
                GROUP BY id";
50
}
51
 
52
/**
53
 * Get sql snippet for backup filesizes.
54
 * @return string
55
 */
56
function report_coursesize_backupsize_sql() {
57
    return "SELECT id AS course, SUM(filesize) AS filesize
58
              FROM (SELECT c.id, f.filesize
59
                      FROM {course} c
60
                      JOIN {context} cx ON cx.contextlevel = ".CONTEXT_COURSE." AND cx.instanceid = c.id
61
                      JOIN {files} f ON f.contextid = cx.id AND f.component = 'backup') x
62
            GROUP BY id";
63
}
64
 
65
/**
66
 * Helper function to return user file sizes.
67
 *
68
 * @return void
69
 */
70
function report_coursesize_usersize_sql() {
71
    return "SELECT userid, sum(filesize) totalsize
72
            FROM {files}
73
            WHERE userid is not null
74
        GROUP BY userid ORDER BY totalsize DESC";
75
}
76
 
77
/**
78
 * Helper function to return top users who have most data.
79
 * It also does caching when necessary.
80
 *
81
 * @return array with the user data from DB or cache
82
 */
83
function report_coursesize_get_usersizes() {
84
    global $DB;
85
    $usercache = \cache::make('report_coursesize', 'topuserdata');
86
    $data = $usercache->get('usersizes');
87
 
88
    if ($data && (time() < $data->expiry)) { // Valid cache data.
89
        $usersizes = $data->usersizes;
90
    } else {
91
        $usersizes = $DB->get_records_sql(report_coursesize_usersize_sql(), [], 0, get_config('report_coursesize', 'numberofusers'));
92
 
93
        if (!empty($usersizes)) {
94
            $data = new \stdClass();
95
            // Set expiry period 24 hours.
96
            $data->expiry = time() + 24 * 60 * 60;
97
            $data->usersizes = $usersizes;
98
            $usercache->set('usersizes', $data);
99
        }
100
    }
101
    return $usersizes;
102
}