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
 * Version information
19
 *
20
 * @package    report_coursesize
21
 * @copyright  2014 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
require_once('../../config.php');
26
require_once($CFG->dirroot.'/report/coursesize/locallib.php');
27
require_once($CFG->libdir.'/adminlib.php');
28
require_once($CFG->libdir.'/csvlib.class.php');
29
 
30
admin_externalpage_setup('reportcoursesize');
31
 
32
$coursecategory = optional_param('category', 0, PARAM_INT);
33
$download = optional_param('download', '', PARAM_INT);
34
$viewtab = optional_param('view', 'coursesize', PARAM_ALPHA);
35
$reportconfig = get_config('report_coursesize');
36
 
37
// If we should show or hide empty courses.
38
if (!defined('REPORT_COURSESIZE_SHOWEMPTYCOURSES')) {
39
    define('REPORT_COURSESIZE_SHOWEMPTYCOURSES', false);
40
}
41
 
42
// Data for the tabs in the report.
43
$tabdata = ['coursesize' => '', 'userstopnum' => $reportconfig->numberofusers];
44
if (!array_key_exists($viewtab, $tabdata)) {
45
    // For invalid parameter value use 'coursesize'.
46
    $viewtab = array_keys($tabdata)[0];
47
}
48
 
49
$tabs = [];
50
foreach ($tabdata as $tabname => $param) {
51
    $tabs[] = new tabobject($tabname, new moodle_url($PAGE->url, ['view' => $tabname]),
52
        get_string($tabname, 'report_coursesize', $param));
53
}
54
 
55
if (empty($download)) {
56
    print $OUTPUT->header();
57
    echo $OUTPUT->tabtree($tabs, $viewtab);
58
}
59
 
60
if ($viewtab == 'userstopnum') {
61
    $usersizes = report_coursesize_get_usersizes();
62
    if (!empty($usersizes)) {
63
        $usertable = new html_table();
64
        $usertable->align = array('right', 'right');
65
        $usertable->head = array(get_string('user'), get_string('diskusage', 'report_coursesize'));
66
        $usertable->data = array();
67
        $usercount = 0;
68
        foreach ($usersizes as $userid => $size) {
69
            $usercount++;
70
            $user = $DB->get_record('user', array('id' => $userid));
71
            $row = array();
72
            $row[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $userid . '">' . fullname($user) . '</a>';
73
            $row[] = display_size($size->totalsize);
74
            $usertable->data[] = $row;
75
            if ($usercount >= $reportconfig->numberofusers) {
76
                break;
77
            }
78
        }
79
        unset($users);
80
        print $OUTPUT->heading(get_string('userstopnum', 'report_coursesize', $reportconfig->numberofusers));
81
 
82
        if (!isset($usertable)) {
83
            print get_string('nouserfiles', 'report_coursesize');
84
        } else {
85
            print html_writer::table($usertable);
86
        }
87
 
88
    }
89
} else if ($viewtab == 'coursesize') {
90
 
91
    if (!empty($reportconfig->filessize) && !empty($reportconfig->filessizeupdated)) {
92
        // Total files usage has stored by scheduled task.
93
        $totalusage = $reportconfig->filessize;
94
        $totaldate = date("Y-m-d H:i", $reportconfig->filessizeupdated);
95
    } else {
96
        $totaldate = get_string('never');
97
        $totalusage = 0;
98
    }
99
 
100
    $totalusagereadable = display_size($totalusage);
101
    $systemsize = $systembackupsize = 0;
102
 
103
    $coursesql = 'SELECT cx.id, c.id as courseid ' .
104
        'FROM {course} c ' .
105
        ' INNER JOIN {context} cx ON cx.instanceid=c.id AND cx.contextlevel = ' . CONTEXT_COURSE;
106
    $params = array();
107
    $courseparams = array();
108
    $extracoursesql = '';
109
    if (!empty($coursecategory)) {
110
        $context = context_coursecat::instance($coursecategory);
111
        $coursecat = core_course_category::get($coursecategory);
112
        $courses = $coursecat->get_courses(array('recursive' => true, 'idonly' => true));
113
 
114
        if (!empty($courses)) {
115
            list($insql, $courseparams) = $DB->get_in_or_equal($courses, SQL_PARAMS_NAMED);
116
            $extracoursesql = ' WHERE c.id ' . $insql;
117
        } else {
118
            // Don't show any courses if category is selected but category has no courses.
119
            // This stuff really needs a rewrite!
120
            $extracoursesql = ' WHERE c.id is null';
121
        }
122
    }
123
    $coursesql .= $extracoursesql;
124
    $params = array_merge($params, $courseparams);
125
    $courselookup = $DB->get_records_sql($coursesql, $params);
126
 
127
    $live = false;
128
    $backupsizes = [];
129
    if (isset($reportconfig->calcmethod) && ($reportconfig->calcmethod) == 'live') {
130
        $live = true;
131
    }
132
    if ($live) {
133
        $filesql = report_coursesize_filesize_sql();
134
        $sql = "SELECT c.id, c.shortname, c.category, ca.name, rc.filesize
135
          FROM {course} c
136
          JOIN ($filesql) rc on rc.course = c.id ";
137
 
138
        // Generate table of backup filesizes too.
139
        $backupsql = report_coursesize_backupsize_sql();
140
        $backupsizes = $DB->get_records_sql($backupsql);
141
    } else {
142
        $sql = "SELECT c.id, c.shortname, c.category, ca.name, rc.filesize, rc.backupsize
143
          FROM {course} c
144
          JOIN {report_coursesize} rc on rc.course = c.id ";
145
    }
146
 
147
    $sql .= "JOIN {course_categories} ca on c.category = ca.id
148
         $extracoursesql
149
     ORDER BY rc.filesize DESC";
150
    $courses = $DB->get_records_sql($sql, $courseparams);
151
 
152
    $coursetable = new html_table();
153
    $coursetable->align = array('right', 'right', 'left');
154
    $coursetable->head = array(get_string('course'),
155
        get_string('category'),
156
        get_string('diskusage', 'report_coursesize'),
157
        get_string('backupsize', 'report_coursesize'));
158
    $coursetable->data = array();
159
 
160
    $totalsize = 0;
161
    $totalbackupsize = 0;
162
    $downloaddata = array();
163
    $downloaddata[] = array(get_string('course'),
164
        get_string('category'),
165
        get_string('diskusage', 'report_coursesize'),
166
        get_string('backupsize', 'report_coursesize'));;
167
 
168
    $coursesizes = $DB->get_records('report_coursesize');
169
    foreach ($courses as $courseid => $course) {
170
        if ($live) {
171
            if (isset($backupsizes[$course->id])) {
172
                $course->backupsize = $backupsizes[$course->id]->filesize;
173
            } else {
174
                $course->backupsize = 0;
175
            }
176
        }
177
        $totalsize = $totalsize + $course->filesize;
178
        $totalbackupsize = $totalbackupsize + $course->backupsize;
179
        $coursecontext = context_course::instance($course->id);
180
        $course->shortname = format_string($course->shortname, true, ['context' => $coursecontext]);
181
        $course->name = format_string($course->name, true, ['context' => $coursecontext]);
182
        $row = array();
183
        $row[] = '<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $course->id . '">' . $course->shortname . '</a>';
184
        $row[] = '<a href="' . $CFG->wwwroot . '/course/index.php?categoryid=' . $course->category . '">' . $course->name . '</a>';
185
 
186
        $readablesize = display_size($course->filesize);
187
        $a = new stdClass;
188
        $a->bytes = $course->filesize;
189
        $a->shortname = $course->shortname;
190
        $a->backupbytes = $course->backupsize;
191
        $bytesused = get_string('coursebytes', 'report_coursesize', $a);
192
        $backupbytesused = get_string('coursebackupbytes', 'report_coursesize', $a);
193
        $summarylink = new moodle_url('/report/coursesize/course.php', array('id' => $course->id));
194
        $summary = html_writer::link($summarylink, ' ' . get_string('coursesummary', 'report_coursesize'));
195
        $row[] = "<span id=\"coursesize_" . $course->shortname . "\" title=\"$bytesused\">$readablesize</span>" . $summary;
196
        $row[] = "<span title=\"$backupbytesused\">" . display_size($course->backupsize) . "</span>";
197
        $coursetable->data[] = $row;
198
        $downloaddata[] = array($course->shortname, $course->name, str_replace(',', '', $readablesize),
199
            str_replace(',', '', display_size($course->backupsize)));
200
    }
201
 
202
    // Now add the courses that had no sitedata into the table.
203
    if (REPORT_COURSESIZE_SHOWEMPTYCOURSES) {
204
        $a = new stdClass;
205
        $a->bytes = 0;
206
        $a->backupbytes = 0;
207
        foreach ($courses as $cid => $course) {
208
            $course->shortname = format_string($course->shortname, true, context_course::instance($course->id));
209
            $a->shortname = $course->shortname;
210
            $bytesused = get_string('coursebytes', 'report_coursesize', $a);
211
            $bytesused = get_string('coursebackupbytes', 'report_coursesize', $a);
212
            $row = array();
213
            $row[] = '<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $course->id . '">' . $course->shortname . '</a>';
214
            $row[] = "<span title=\"$bytesused\">0</span>";
215
            $row[] = "<span title=\"$bytesused\">0</span>";
216
            $coursetable->data[] = $row;
217
        }
218
    }
219
    // Now add the totals to the bottom of the table.
220
    $coursetable->data[] = array(); // Add empty row before total.
221
    $downloaddata[] = array();
222
    $row = array();
223
    $row[] = get_string('total');
224
    $row[] = '';
225
    $row[] = display_size($totalsize);
226
    $row[] = display_size($totalbackupsize);
227
    $coursetable->data[] = $row;
228
    $downloaddata[] = [get_string('total'), '', display_size($totalsize), display_size($totalbackupsize)];
229
    unset($courses);
230
 
231
    $systemsizereadable = display_size($systemsize);
232
    $systembackupreadable = display_size($systembackupsize);
233
 
234
 
235
    // Add in Course Cat including dropdown to filter.
236
 
237
    $url = '';
238
    $catlookup = $DB->get_records_sql('select id,name from {course_categories}');
239
    $options = ['0' => get_string('allcourses', 'report_coursesize')];
240
    foreach ($catlookup as $cat) {
241
        $options[$cat->id] = format_string($cat->name, true, context_system::instance());
242
    }
243
 
244
    // Add in download option. Exports CSV.
245
 
246
    if ($download == 1) {
247
        $downloadfilename = clean_filename("export_csv");
248
        $csvexport = new csv_export_writer ('commer');
249
        $csvexport->set_filename($downloadfilename);
250
        foreach ($downloaddata as $data) {
251
            $csvexport->add_data($data);
252
        }
253
        $csvexport->download_file();
254
        exit;
255
    }
256
 
257
    if (empty($coursecat)) {
258
        $updatestring = !empty($reportconfig->filessizeupdated) ? userdate($reportconfig->filessizeupdated) : get_string('never');
259
        print $OUTPUT->heading(get_string("sitefilesusage", 'report_coursesize'));
260
        print '<strong>' . get_string("totalsitedata", 'report_coursesize', $totalusagereadable) . '</strong> ';
261
        print get_string('lastupdate', 'report_coursesize', $updatestring) . "<br/><br/>\n";
262
        print get_string('catsystemuse', 'report_coursesize', $systemsizereadable) . "<br/>";
263
        print get_string('catsystembackupuse', 'report_coursesize', $systembackupreadable) . "<br/>";
264
        if (!empty($CFG->filessizelimit)) {
265
            print get_string("sizepermitted", 'report_coursesize', number_format($CFG->filessizelimit)) . "<br/>\n";
266
        }
267
    }
268
    $lastupdate = '';
269
    if (!$live) {
270
        if (empty($reportconfig->coursesizeupdated)) {
271
            $lastupdate = get_string('lastupdatenever', 'report_coursesize');
272
        } else {
273
            $lastupdate = get_string('lastupdate', 'report_coursesize', userdate($reportconfig->coursesizeupdated));
274
        }
275
        $lastupdate = html_writer::span($lastupdate, 'lastupdate');
276
    }
277
    $heading = get_string('coursesize', 'report_coursesize');
278
    if (!empty($coursecat)) {
279
        $heading .= " - " . $coursecat->name;
280
    }
281
    print $OUTPUT->heading($heading . ' ' . $lastupdate);
282
 
283
    $desc = get_string('coursesize_desc', 'report_coursesize');
284
 
285
    if (!REPORT_COURSESIZE_SHOWEMPTYCOURSES) {
286
        $desc .= ' ' . get_string('emptycourseshidden', 'report_coursesize');
287
    }
288
    print $OUTPUT->box($desc);
289
 
290
    $filter = $OUTPUT->single_select($url, 'category', $options, $coursecategory, []);
291
    $filter .= $OUTPUT->single_button(new moodle_url('index.php', array('download' => 1, 'category' => $coursecategory)),
292
        get_string('exportcsv', 'report_coursesize'), 'post', ['class' => 'coursesizedownload']);
293
 
294
    print $OUTPUT->box($filter) . "<br/>";
295
 
296
    print html_writer::table($coursetable);
297
}
298
print $OUTPUT->footer();