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
 * Provides the {@see mod_subcourse\task\fetch_grades} class.
19
 *
20
 * @package     mod_subcourse
21
 * @category    task
22
 * @copyright   2014 David Mudrak <david@moodle.com>
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_subcourse\task;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
require_once($CFG->dirroot.'/mod/subcourse/locallib.php');
31
 
32
/**
33
 * Fetches remote grades into all subcourse instances
34
 *
35
 * @copyright 2014 David Mudrak <david@moodle.com>
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class fetch_grades extends \core\task\scheduled_task {
39
 
40
    /**
41
     * Returns a descriptive name for this task shown to admins
42
     *
43
     * @return string
44
     */
45
    public function get_name() {
46
        return get_string('taskfetchgrades', 'mod_subcourse');
47
    }
48
 
49
    /**
50
     * Performs the task
51
     *
52
     * @throws moodle_exception on an error (the job will be retried)
53
     */
54
    public function execute() {
55
        global $DB;
56
 
57
        $subcourses = $DB->get_records("subcourse", null, "", "id, course, refcourse, fetchpercentage");
58
 
59
        if (empty($subcourses)) {
60
            return;
61
        }
62
 
63
        $updatedids = [];
64
 
65
        foreach ($subcourses as $subcourse) {
66
 
67
            if (empty($subcourse->refcourse)) {
68
                mtrace("Subcourse {$subcourse->id}: no referenced course configured ... skipped");
69
                continue;
70
            }
71
 
72
            mtrace("Subcourse {$subcourse->id}: fetching grades from course {$subcourse->refcourse} ".
73
               "to course {$subcourse->course} ... ", "");
74
            $result = subcourse_grades_update($subcourse->course, $subcourse->id, $subcourse->refcourse,
75
                null, false, false, [], $subcourse->fetchpercentage);
76
 
77
            if ($result == GRADE_UPDATE_OK) {
78
                $updatedids[] = $subcourse->id;
79
                mtrace("ok");
80
 
81
            } else {
82
                mtrace("failed with error code ".$result);
83
            }
84
        }
85
 
86
        if (!empty($updatedids)) {
87
            subcourse_update_timefetched($updatedids);
88
        }
89
    }
90
}