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
 * Defines the task which removes old tmp files
18
 *
19
 * @package    mod_hvp
20
 * @copyright  2016 Joubel AS <contact@joubel.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
namespace mod_hvp\task;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
/**
29
 * The mod_hvp look for updates task class
30
 *
31
 * @package    mod_hvp
32
 * @copyright  2016 Joubel AS <contact@joubel.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class remove_tmpfiles extends \core\task\scheduled_task {
36
    public function get_name() {
37
        return get_string('removetmpfiles', 'mod_hvp');
38
    }
39
 
40
    public function execute() {
41
        global $DB;
42
        $tmpfiles = $DB->get_records_sql(
43
                "SELECT f.id
44
                   FROM {hvp_tmpfiles} tf
45
                   JOIN {files} f ON f.id = tf.id
46
                  WHERE f.timecreated < ?",
47
                array(time() - 86400)
48
        );
49
        if (empty($tmpfiles)) {
50
            return; // Nothing to clean up.
51
        }
52
 
53
        $fs = get_file_storage();
54
        foreach ($tmpfiles as $tmpfile) {
55
            // Delete file.
56
            $file = $fs->get_file_by_id($tmpfile->id);
57
            $file->delete();
58
 
59
            // Remove tmpfile entry.
60
            $DB->delete_records('hvp_tmpfiles', array('id' => $tmpfile->id));
61
        }
62
    }
63
}