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
namespace tool_courserating\task;
18
 
19
use tool_courserating\api;
20
 
21
/**
22
 * Task to recalculate ratings on all courses
23
 *
24
 * @package     tool_courserating
25
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
26
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class reindex extends \core\task\adhoc_task {
29
 
30
    /**
31
     * Name of the task
32
     *
33
     * @return \lang_string|string
34
     */
35
    public function get_name() {
36
        return get_string('reindextask', 'tool_courserating');
37
    }
38
 
39
    /**
40
     * Execute the task
41
     *
42
     * @return void
43
     */
44
    public function execute() {
45
        $courseid = $this->get_custom_data()->courseid ?? 0;
46
        try {
47
            api::reindex($courseid);
48
        } catch (\Throwable $t) {
49
            debugging($t->getMessage()."\n\n".$t->getTraceAsString());
50
        }
51
    }
52
 
53
    /**
54
     * Schedule the task to run on the next cron
55
     */
56
    public static function schedule() {
57
        self::schedule_course(0);
58
    }
59
 
60
    /**
61
     * Schedule the task to run on the next cron, for individual course
62
     *
63
     * @param int $courseid
64
     */
65
    public static function schedule_course(int $courseid = 0) {
66
        global $USER;
67
 
68
        $task = new static();
69
        $task->set_userid($USER->id);
70
        $task->set_custom_data(['courseid' => $courseid]);
71
 
72
        \core\task\manager::queue_adhoc_task($task, true);
73
    }
74
}