Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core_course\task;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
use core\task\adhoc_task;
22
 
23
require_once($CFG->libdir . '/gradelib.php');
24
 
25
/**
26
 * Asynchronously regrade a course.
27
 *
28
 * @copyright 2024 onwards Catalyst IT Europe Ltd.
29
 * @author Mark Johnson <mark.johnson@catalyst-eu.net>
30
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @package core_course
32
 */
33
class regrade_final_grades extends adhoc_task {
34
    use \core\task\logging_trait;
35
    use \core\task\stored_progress_task_trait;
36
 
37
    /**
38
     * Create and return an instance of this task for a given course ID.
39
     *
40
     * @param int $courseid
41
     * @return self
42
     */
43
    public static function create(int $courseid): self {
44
        $task = new regrade_final_grades();
45
        $task->set_custom_data((object)['courseid' => $courseid]);
46
        $task->set_component('core');
47
        return $task;
48
    }
49
 
50
    /**
51
     * Run regrade_final_grades for the provided course id.
52
     *
53
     * @return void
54
     * @throws \dml_exception
55
     */
56
    public function execute(): void {
57
        $data = $this->get_custom_data();
58
        $this->start_stored_progress();
59
        $this->log_start("Recalculating grades for course ID {$data->courseid}");
60
        // Ensure the course exists.
61
        try {
62
            $course = get_course($data->courseid);
63
        } catch (\dml_missing_record_exception $e) {
64
            $this->log("Course with id {$data->courseid} not found. It may have been deleted. Skipping regrade.");
65
            return;
66
        }
67
        $this->log("Found course {$course->shortname}. Starting regrade.");
68
        $results = grade_regrade_final_grades($course->id, progress: $this->get_progress());
69
        if (is_array($results)) {
70
            $this->log('Errors reported during regrade:');
71
            foreach ($results as $id => $result) {
72
                $this->log("Grade item {$id}: {$result}", 2);
73
            }
74
        }
75
        $this->log_finish('Regrade complete.');
76
    }
77
}