MDL-51568 self_enrol: New Web Service enrol_self_enrol_user
[moodle.git] / enrol / self / externallib.php
CommitLineData
be9bf94e
RT
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 * Self enrol plugin external functions
19 *
20 * @package enrol_self
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once("$CFG->libdir/externallib.php");
28
29/**
30 * Self enrolment external functions.
31 *
32 * @package enrol_self
33 * @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 * @since Moodle 2.6
36 */
37class enrol_self_external extends external_api {
38
39 /**
40 * Returns description of get_instance_info() parameters.
41 *
42 * @return external_function_parameters
43 */
44 public static function get_instance_info_parameters() {
45 return new external_function_parameters(
46 array('instanceid' => new external_value(PARAM_INT, 'instance id of self enrolment plugin.'))
47 );
48 }
49
50 /**
51 * Return self-enrolment instance information.
52 *
53 * @param int $instanceid instance id of self enrolment plugin.
54 * @return array instance information.
55 */
56 public static function get_instance_info($instanceid) {
57 global $DB, $CFG;
58
59 require_once($CFG->libdir . '/enrollib.php');
60
61 $params = self::validate_parameters(self::get_instance_info_parameters(), array('instanceid' => $instanceid));
62
63 // Retrieve self enrolment plugin.
64 $enrolplugin = enrol_get_plugin('self');
65 if (empty($enrolplugin)) {
66 throw new moodle_exception('invaliddata', 'error');
67 }
68
69 $enrolinstance = $DB->get_record('enrol', array('id' => $params['instanceid']), '*', MUST_EXIST);
70 $coursecontext = context_course::instance($enrolinstance->courseid);
71 $categorycontext = $coursecontext->get_parent_context();
72 self::validate_context($categorycontext);
73
74 $instanceinfo = (array) $enrolplugin->get_enrol_info($enrolinstance);
75 if (isset($instanceinfo['requiredparam']->enrolpassword)) {
76 $instanceinfo['enrolpassword'] = $instanceinfo['requiredparam']->enrolpassword;
77 }
78 unset($instanceinfo->requiredparam);
79
80 return $instanceinfo;
81 }
82
83 /**
84 * Returns description of get_instance_info() result value.
85 *
86 * @return external_description
87 */
88 public static function get_instance_info_returns() {
89 return new external_single_structure(
90 array(
91 'id' => new external_value(PARAM_INT, 'id of course enrolment instance'),
92 'courseid' => new external_value(PARAM_INT, 'id of course'),
93 'type' => new external_value(PARAM_PLUGIN, 'type of enrolment plugin'),
94 'name' => new external_value(PARAM_RAW, 'name of enrolment plugin'),
95 'status' => new external_value(PARAM_RAW, 'status of enrolment plugin'),
96 'enrolpassword' => new external_value(PARAM_RAW, 'password required for enrolment', VALUE_OPTIONAL),
97 )
98 );
99 }
e8ad99ab
JL
100
101 /**
102 * Returns description of method parameters
103 *
104 * @return external_function_parameters
105 * @since Moodle 3.0
106 */
107 public static function enrol_user_parameters() {
108 return new external_function_parameters(
109 array(
110 'courseid' => new external_value(PARAM_INT, 'Id of the course'),
111 'password' => new external_value(PARAM_RAW, 'Enrolment key', VALUE_DEFAULT, ''),
112 'instanceid' => new external_value(PARAM_INT, 'Instance id of self enrolment plugin.', VALUE_DEFAULT, 0)
113 )
114 );
115 }
116
117 /**
118 * Self enrol the current user in the given course.
119 *
120 * @param int $courseid id of course
121 * @param string $password enrolment key
122 * @param int $instanceid instance id of self enrolment plugin
123 * @return array of warnings and status result
124 * @since Moodle 3.0
125 * @throws moodle_exception
126 */
127 public static function enrol_user($courseid, $password = '', $instanceid = 0) {
128 global $CFG;
129
130 require_once($CFG->libdir . '/enrollib.php');
131
132 $params = self::validate_parameters(self::enrol_user_parameters(),
133 array(
134 'courseid' => $courseid,
135 'password' => $password,
136 'instanceid' => $instanceid
137 ));
138
139 $warnings = array();
140
141 $course = get_course($params['courseid']);
142 $context = context_course::instance($course->id);
143 // Note that we can't use validate_context because the user is not enrolled in the course.
144 require_login(null, false, null, false, true);
145
146 if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
147 throw new moodle_exception('coursehidden');
148 }
149
150 // Retrieve the self enrolment plugin.
151 $enrol = enrol_get_plugin('self');
152 if (empty($enrol)) {
153 throw new moodle_exception('canntenrol', 'enrol_self');
154 }
155
156 // We can expect multiple self-enrolment instances.
157 $instances = array();
158 $enrolinstances = enrol_get_instances($course->id, true);
159 foreach ($enrolinstances as $courseenrolinstance) {
160 if ($courseenrolinstance->enrol == "self") {
161 // Instance specified.
162 if (!empty($params['instanceid'])) {
163 if ($courseenrolinstance->id == $params['instanceid']) {
164 $instances[] = $courseenrolinstance;
165 break;
166 }
167 } else {
168 $instances[] = $courseenrolinstance;
169 }
170
171 }
172 }
173 if (empty($instances)) {
174 throw new moodle_exception('canntenrol', 'enrol_self');
175 }
176
177 // Try to enrol the user in the instance/s.
178 $enrolled = false;
179 foreach ($instances as $instance) {
180 $enrolstatus = $enrol->can_self_enrol($instance);
181 if ($enrolstatus === true) {
182 if ($instance->password and $params['password'] !== $instance->password) {
183
184 // Check if we are using group enrolment keys.
185 if ($instance->customint1) {
186 require_once($CFG->dirroot . "/enrol/self/locallib.php");
187
188 if (!enrol_self_check_group_enrolment_key($course->id, $params['password'])) {
189 $warnings[] = array(
190 'item' => 'instance',
191 'itemid' => $instance->id,
192 'warningcode' => '2',
193 'message' => get_string('passwordinvalid', 'enrol_self')
194 );
195 continue;
196 }
197 } else {
198 if ($enrol->get_config('showhint')) {
199 $hint = core_text::substr($instance->password, 0, 1);
200 $warnings[] = array(
201 'item' => 'instance',
202 'itemid' => $instance->id,
203 'warningcode' => '3',
204 'message' => s(get_string('passwordinvalidhint', 'enrol_self', $hint)) // message is PARAM_TEXT.
205 );
206 continue;
207 } else {
208 $warnings[] = array(
209 'item' => 'instance',
210 'itemid' => $instance->id,
211 'warningcode' => '4',
212 'message' => get_string('passwordinvalid', 'enrol_self')
213 );
214 continue;
215 }
216 }
217 }
218
219 // Do the enrolment.
220 $data = array('enrolpassword' => $params['password']);
221 $enrol->enrol_self($instance, (object) $data);
222 $enrolled = true;
223 break;
224 } else {
225 $warnings[] = array(
226 'item' => 'instance',
227 'itemid' => $instance->id,
228 'warningcode' => '1',
229 'message' => $enrolstatus
230 );
231 }
232 }
233
234 $result = array();
235 $result['status'] = $enrolled;
236 $result['warnings'] = $warnings;
237 return $result;
238 }
239
240 /**
241 * Returns description of method result value
242 *
243 * @return external_description
244 * @since Moodle 3.0
245 */
246 public static function enrol_user_returns() {
247 return new external_single_structure(
248 array(
249 'status' => new external_value(PARAM_BOOL, 'status: true if the user is enrolled, false otherwise'),
250 'warnings' => new external_warnings()
251 )
252 );
253 }
be9bf94e 254}