Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
use core_external\external_api;
18
use core_external\external_function_parameters;
19
use core_external\external_multiple_structure;
20
use core_external\external_single_structure;
21
use core_external\external_value;
22
 
23
/**
24
 * External course participation api.
25
 *
26
 * This api is mostly read only, the actual enrol and unenrol
27
 * support is in each enrol plugin.
28
 *
29
 * @package    enrol_manual
30
 * @category   external
31
 * @copyright  2011 Jerome Mouneyrac
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @since Moodle 2.2
34
 */
35
class enrol_manual_external extends external_api {
36
 
37
    /**
38
     * Returns description of method parameters.
39
     *
40
     * @return external_function_parameters
41
     * @since Moodle 2.2
42
     */
43
    public static function enrol_users_parameters() {
44
        return new external_function_parameters(
45
                array(
46
                    'enrolments' => new external_multiple_structure(
47
                            new external_single_structure(
48
                                    array(
49
                                        'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
50
                                        'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'),
51
                                        'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'),
52
                                        'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL),
53
                                        'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL),
54
                                        'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL)
55
                                    )
56
                            )
57
                    )
58
                )
59
        );
60
    }
61
 
62
    /**
63
     * Enrolment of users.
64
     *
65
     * Function throw an exception at the first error encountered.
66
     * @param array $enrolments  An array of user enrolment
67
     * @since Moodle 2.2
68
     */
69
    public static function enrol_users($enrolments) {
70
        global $DB, $CFG;
71
 
72
        require_once($CFG->libdir . '/enrollib.php');
73
 
74
        $params = self::validate_parameters(self::enrol_users_parameters(),
75
                array('enrolments' => $enrolments));
76
 
77
        $transaction = $DB->start_delegated_transaction(); // Rollback all enrolment if an error occurs
78
                                                           // (except if the DB doesn't support it).
79
 
80
        // Retrieve the manual enrolment plugin.
81
        $enrol = enrol_get_plugin('manual');
82
        if (empty($enrol)) {
83
            throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
84
        }
85
 
86
        foreach ($params['enrolments'] as $enrolment) {
87
            // Ensure the current user is allowed to run this function in the enrolment context.
88
            $context = context_course::instance($enrolment['courseid'], IGNORE_MISSING);
89
            self::validate_context($context);
90
 
91
            // Check that the user has the permission to manual enrol.
92
            require_capability('enrol/manual:enrol', $context);
93
 
1441 ariadna 94
            $user = core_user::get_user($enrolment['userid'], strictness: MUST_EXIST);
95
            core_user::require_active_user($user);
96
 
1 efrain 97
            // Throw an exception if user is not able to assign the role.
98
            $roles = get_assignable_roles($context);
99
            if (!array_key_exists($enrolment['roleid'], $roles)) {
100
                $errorparams = new stdClass();
101
                $errorparams->roleid = $enrolment['roleid'];
102
                $errorparams->courseid = $enrolment['courseid'];
1441 ariadna 103
                $errorparams->userid = $user->id;
1 efrain 104
                throw new moodle_exception('wsusercannotassign', 'enrol_manual', '', $errorparams);
105
            }
106
 
107
            // Check manual enrolment plugin instance is enabled/exist.
108
            $instance = null;
109
            $enrolinstances = enrol_get_instances($enrolment['courseid'], true);
110
            foreach ($enrolinstances as $courseenrolinstance) {
111
              if ($courseenrolinstance->enrol == "manual") {
112
                  $instance = $courseenrolinstance;
113
                  break;
114
              }
115
            }
116
            if (empty($instance)) {
117
                $errorparams = new stdClass();
118
                $errorparams->courseid = $enrolment['courseid'];
119
                throw new moodle_exception('wsnoinstance', 'enrol_manual', '', $errorparams);
120
            }
121
 
122
            // Check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin).
123
            if (!$enrol->allow_enrol($instance)) {
124
                $errorparams = new stdClass();
125
                $errorparams->roleid = $enrolment['roleid'];
126
                $errorparams->courseid = $enrolment['courseid'];
1441 ariadna 127
                $errorparams->userid = $user->id;
1 efrain 128
                throw new moodle_exception('wscannotenrol', 'enrol_manual', '', $errorparams);
129
            }
130
 
131
            // Finally proceed the enrolment.
132
            $enrolment['timestart'] = isset($enrolment['timestart']) ? $enrolment['timestart'] : 0;
133
            $enrolment['timeend'] = isset($enrolment['timeend']) ? $enrolment['timeend'] : 0;
134
            $enrolment['status'] = (isset($enrolment['suspend']) && !empty($enrolment['suspend'])) ?
135
                    ENROL_USER_SUSPENDED : ENROL_USER_ACTIVE;
136
 
1441 ariadna 137
            $enrol->enrol_user($instance, $user->id, $enrolment['roleid'],
1 efrain 138
                    $enrolment['timestart'], $enrolment['timeend'], $enrolment['status']);
139
 
140
        }
141
 
142
        $transaction->allow_commit();
143
    }
144
 
145
    /**
146
     * Returns description of method result value.
147
     *
148
     * @return null
149
     * @since Moodle 2.2
150
     */
151
    public static function enrol_users_returns() {
152
        return null;
153
    }
154
 
155
    /**
156
     * Returns description of method parameters.
157
     *
158
     * @return external_function_parameters
159
     */
160
    public static function unenrol_users_parameters() {
161
        return new external_function_parameters(array(
162
            'enrolments' => new external_multiple_structure(
163
                new external_single_structure(
164
                    array(
165
                        'userid' => new external_value(PARAM_INT, 'The user that is going to be unenrolled'),
166
                        'courseid' => new external_value(PARAM_INT, 'The course to unenrol the user from'),
167
                        'roleid' => new external_value(PARAM_INT, 'The user role', VALUE_OPTIONAL),
168
                    )
169
                )
170
            )
171
        ));
172
    }
173
 
174
    /**
175
     * Unenrolment of users.
176
     *
177
     * @param array $enrolments an array of course user and role ids
178
     * @throws coding_exception
179
     * @throws dml_transaction_exception
180
     * @throws invalid_parameter_exception
181
     * @throws moodle_exception
182
     * @throws required_capability_exception
183
     * @throws restricted_context_exception
184
     */
185
    public static function unenrol_users($enrolments) {
186
        global $CFG, $DB;
187
        $params = self::validate_parameters(self::unenrol_users_parameters(), array('enrolments' => $enrolments));
188
        require_once($CFG->libdir . '/enrollib.php');
189
        $transaction = $DB->start_delegated_transaction(); // Rollback all enrolment if an error occurs.
190
        $enrol = enrol_get_plugin('manual');
191
        if (empty($enrol)) {
192
            throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
193
        }
194
 
195
        foreach ($params['enrolments'] as $enrolment) {
196
            $context = context_course::instance($enrolment['courseid']);
197
            self::validate_context($context);
198
            require_capability('enrol/manual:unenrol', $context);
199
            $instance = $DB->get_record('enrol', array('courseid' => $enrolment['courseid'], 'enrol' => 'manual'));
200
            if (!$instance) {
201
                throw new moodle_exception('wsnoinstance', 'enrol_manual', '', $enrolment);
202
            }
1441 ariadna 203
            $user = core_user::get_user($enrolment['userid'], strictness: MUST_EXIST);
204
            core_user::require_active_user($user);
1 efrain 205
            if (!$enrol->allow_unenrol($instance)) {
206
                throw new moodle_exception('wscannotunenrol', 'enrol_manual', '', $enrolment);
207
            }
1441 ariadna 208
            $enrol->unenrol_user($instance, $user->id);
1 efrain 209
        }
210
        $transaction->allow_commit();
211
    }
212
 
213
    /**
214
     * Returns description of method result value.
215
     *
216
     * @return null
217
     */
218
    public static function unenrol_users_returns() {
219
        return null;
220
    }
221
 
222
}