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
 * report_coursesize tasks
19
 *
20
 * @package   report_coursesize
21
 * @copyright Catalyst IT
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace report_coursesize\task;
25
 
26
/**
27
 * report_async tasks
28
 *
29
 * @package   report_coursesize
30
 * @copyright Catalyst IT
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class report_async extends \core\task\scheduled_task {
34
 
35
    /**
36
     * Get task name
37
     */
38
    public function get_name() {
39
        return get_string('pluginname', 'report_coursesize');
40
    }
41
 
42
    /**
43
     * Execute task
44
     */
45
    public function execute() {
46
        global $DB, $CFG;
47
        require_once($CFG->dirroot . '/report/coursesize/locallib.php');
48
 
49
        if (get_config('report_coursesize', 'calcmethod') == 'cron') {
50
 
51
            mtrace("Generating report_coursesize cache...");
52
            set_time_limit(0);
53
 
54
            // First we delete the old data, then we re-populate it, wrap in a transaction to help keep it together.
55
            $transaction = $DB->start_delegated_transaction();
56
 
57
            // Clean up cache table.
58
            $DB->delete_records('report_coursesize');
59
 
60
            // Generate report_coursesize table.
61
            $basesql = report_coursesize_filesize_sql();
62
            $sql = "INSERT INTO {report_coursesize} (course, filesize) $basesql ";
63
            $DB->execute($sql);
64
 
65
            // Now calculate size of backups.
66
            $basesql = report_coursesize_backupsize_sql();
67
 
68
            $sql = "UPDATE {report_coursesize} rc
69
                    SET backupsize = (SELECT bf.filesize FROM ($basesql) bf WHERE bf.course = rc.course)";
70
            $DB->execute($sql);
71
 
72
            $transaction->allow_commit();
73
 
74
            // Ignore the result now. The call will only cache the data internally.
75
            report_coursesize_get_usersizes();
76
            set_config('coursesizeupdated', time(), 'report_coursesize');
77
 
78
            mtrace("report_coursesize cache updated.");
79
        }
80
        // Check if the path ends with a "/" otherwise an exception will be thrown.
81
        $sitedatadir = $CFG->dataroot;
82
        if (is_dir($sitedatadir)) {
83
            // Only append a "/" if it doesn't already end with one.
84
            if (substr($sitedatadir, -1) !== '/') {
85
                $sitedatadir .= '/';
86
            }
87
        }
88
 
89
        // Total files usage either hasn't been stored, or is out of date.
90
        $totalusage = get_directory_size($sitedatadir);
91
        set_config('filessize', $totalusage, 'report_coursesize');
92
        set_config('filessizeupdated', time(), 'report_coursesize');
93
 
94
        mtrace("report_coursesize overall directory size updated");
95
    }
96
}