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
 * A bulk operation for the coursecompleted enrolment plugin to edit selected users enrolments.
19
 *
20
 * @package   enrol_coursecompleted
21
 * @copyright 2020 eWallah (www.eWallah.net)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
// @codeCoverageIgnoreStart
29
require_once($CFG->dirroot . '/enrol/locallib.php');
30
// @codeCoverageIgnoreEnd
31
 
32
/**
33
 * A bulk operation for the coursecompleted enrolment plugin to edit selected users enrolments.
34
 *
35
 * @package   enrol_coursecompleted
36
 * @copyright 2020 eWallah (www.eWallah.net)
37
 * @author    Renaat Debleu <info@eWallah.net>
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class enrol_coursecompleted_bulkedit extends enrol_bulk_enrolment_operation {
41
 
42
    /**
43
     * Returns the identifier for this bulk operation. This is the key used when the plugin
44
     * returns an array containing all of the bulk operations it supports.
45
     *
46
     * @return string
47
     */
48
    public function get_identifier() {
49
        return 'editselectedusers';
50
    }
51
 
52
    /**
53
     * Returns the title to display for this bulk operation.
54
     *
55
     * @return string
56
     */
57
    public function get_title() {
58
        return get_string('editselectedusers', 'enrol_coursecompleted');
59
    }
60
 
61
    /**
62
     * Returns a enrol_bulk_enrolment_operation extension form to be used
63
     * in collecting required information for this operation to be processed.
64
     *
65
     * @param string|moodle_url|null $defaultaction
66
     * @param mixed $defaultcustomdata
67
     * @return enrol_coursecompleted_deleteselectedusers_form
68
     */
69
    public function get_form($defaultaction = null, $defaultcustomdata = null) {
70
        $data = is_array($defaultcustomdata) ? $defaultcustomdata : [];
71
        $data['title'] = $this->get_title();
72
        $data['message'] = get_string('confirmbulkediteenrolment', 'enrol_coursecompleted');
73
        $data['button'] = get_string('editusers', 'enrol_coursecompleted');
74
        return new \enrol_coursecompleted\form\bulkedit($defaultaction, $data);
75
    }
76
 
77
    /**
78
     * Processes the bulk operation request for the given userids with the provided properties.
79
     *
80
     * @param course_enrolment_manager $manager
81
     * @param array $users
82
     * @param stdClass $properties The data returned by the form.
83
     * @return bool
84
     */
85
    public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
86
        global $DB, $USER;
87
        $context = $manager->get_context();
88
        if (!has_capability("enrol/coursecompleted:manage", $context)) {
89
            return false;
90
        }
91
        $ueids = $updatesql = [];
92
        foreach ($users as $user) {
93
            foreach ($user->enrolments as $enrolment) {
94
                $ueids[] = $enrolment->id;
95
                $courseid = $enrolment->enrolmentinstance->courseid;
96
                // Trigger event.
97
                $event = \core\event\user_enrolment_updated::create(
98
                    [
99
                        'objectid' => $enrolment->id,
100
                        'courseid' => $courseid,
101
                        'context' => \context_course::instance($courseid),
102
                        'relateduserid' => $user->id,
103
                        'other' => ['enrol' => 'coursecompleted'],
104
                    ]
105
                );
106
                $event->trigger();
107
            }
108
        }
109
        list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED);
110
        if ($properties->status == ENROL_USER_ACTIVE || $properties->status == ENROL_USER_SUSPENDED) {
111
            $updatesql[] = 'status = :status';
112
            $params['status'] = (int)$properties->status;
113
        }
114
        if (!empty($properties->timestart)) {
115
            $updatesql[] = 'timestart = :timestart';
116
            $params['timestart'] = (int)$properties->timestart;
117
        }
118
        if (!empty($properties->timeend)) {
119
            $updatesql[] = 'timeend = :timeend';
120
            $params['timeend'] = (int)$properties->timeend;
121
        }
122
        if (empty($updatesql)) {
123
            return true;
124
        }
125
 
126
        $updatesql[] = 'modifierid = :modifierid';
127
        $params['modifierid'] = (int)$USER->id;
128
 
129
        $updatesql[] = 'timemodified = :timemodified';
130
        $params['timemodified'] = time();
131
 
132
        $updatesql = join(', ', $updatesql);
133
        $sql = "UPDATE {user_enrolments} SET $updatesql WHERE id $ueidsql";
134
        cache::make('core', 'coursecontacts')->delete($context->instanceid);
135
        return (bool)$DB->execute($sql, $params);
136
    }
137
}