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 {@see \mod_subcourse\task\check_completed_refcourses} class
19
 *
20
 * @package     mod_subcourse
21
 * @category    task
22
 * @copyright   2017 David Mudrák <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
use completion_completion;
29
use completion_info;
30
use context_course;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
require_once($CFG->dirroot.'/mod/subcourse/locallib.php');
35
 
36
/**
37
 * Makes sure that all subcourse instances are marked as completed when they should be.
38
 *
39
 * Normally, completed course triggers the subcourse completion automatically
40
 * via observing the event. This task is there for rechecking the completions to catch
41
 * up with courses that were completed in the past (and the event was missed).
42
 *
43
 * @copyright 2017 David Mudrak <david@moodle.com>
44
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
46
class check_completed_refcourses extends \core\task\scheduled_task {
47
 
48
    /**
49
     * Returns a descriptive name for this task shown to admins
50
     *
51
     * @return string
52
     */
53
    public function get_name() {
54
        return get_string('taskcheckcompletedrefcourses', 'mod_subcourse');
55
    }
56
 
57
    /**
58
     * Performs the task
59
     *
60
     * @throws moodle_exception on an error (the job will be retried)
61
     */
62
    public function execute() {
63
        global $CFG, $DB;
64
        require_once($CFG->dirroot.'/lib/completionlib.php');
65
        require_once($CFG->dirroot.'/completion/completion_completion.php');
66
 
67
        if (!completion_info::is_enabled_for_site()) {
68
            mtrace("Completion tracking not enabled on this site");
69
            return;
70
        }
71
 
72
        // Get all subcourses that have completion rule based on refcourse completed.
73
        $rs = $DB->get_recordset('subcourse', ['completioncourse' => 1], 'course');
74
 
75
        // Note the subcourses are sorted by their course. We cache the course
76
        // record and the list of enrolled participants for all subcourse
77
        // instances within one course in the following variable.
78
        $cache = [];
79
 
80
        foreach ($rs as $subcourse) {
81
            $cm = get_coursemodule_from_instance('subcourse', $subcourse->id);
82
 
83
            if (empty($subcourse->refcourse)) {
84
                mtrace("Subcourse {$subcourse->id}: no referenced course configured ... skipped");
85
                continue;
86
            }
87
 
88
            if (!isset($cache[$subcourse->course])) {
89
                // Load the course with this subcourse and students enrolled to it.
90
                // We do not need data from the previous course any more.
91
                $course = $DB->get_record('course', ['id' => $subcourse->course]);
92
                $coursecontext = context_course::instance($course->id);
93
                $cache = [
94
                    $course->id => (object)[
95
                        'course' => $course,
96
                        'participants' => get_enrolled_users($coursecontext, 'mod/subcourse:begraded', 0, "u.id"),
97
                    ]
98
                ];
99
            }
100
 
101
            $completion = new completion_info($cache[$subcourse->course]->course);
102
 
103
            if (!$completion->is_enabled($cm)) {
104
                mtrace("Subcourse {$subcourse->id}: completion tracking not enabled ... skipped");
105
                continue;
106
            }
107
 
108
            mtrace("Subcourse {$subcourse->id}: checking refcourse {$subcourse->refcourse} completions ... ");
109
 
110
            foreach (array_keys($cache[$subcourse->course]->participants) as $userid) {
111
                $coursecompletion = new completion_completion(['userid' => $userid, 'course' => $subcourse->refcourse]);
112
                if ($coursecompletion->is_complete()) {
113
                    // Notify the subcourse to check the completion status.
114
                    mtrace(" - user {$userid}: has completed referenced course, checking subcourse completion");
115
                    $completion->update_state($cm, COMPLETION_COMPLETE, $userid);
116
                }
117
            }
118
 
119
            mtrace(" ... checked ".count($cache[$subcourse->course]->participants)." users");
120
        }
121
 
122
        $rs->close();
123
    }
124
}