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 |
* Self enrolment plugin.
|
|
|
19 |
*
|
|
|
20 |
* @package enrol_self
|
|
|
21 |
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Self enrolment plugin implementation.
|
|
|
27 |
* @author Petr Skoda
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class enrol_self_plugin extends enrol_plugin {
|
|
|
31 |
|
|
|
32 |
protected $lasternoller = null;
|
|
|
33 |
protected $lasternollerinstanceid = 0;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Returns optional enrolment information icons.
|
|
|
37 |
*
|
|
|
38 |
* This is used in course list for quick overview of enrolment options.
|
|
|
39 |
*
|
|
|
40 |
* We are not using single instance parameter because sometimes
|
|
|
41 |
* we might want to prevent icon repetition when multiple instances
|
|
|
42 |
* of one type exist. One instance may also produce several icons.
|
|
|
43 |
*
|
|
|
44 |
* @param array $instances all enrol instances of this type in one course
|
|
|
45 |
* @return array of pix_icon
|
|
|
46 |
*/
|
|
|
47 |
public function get_info_icons(array $instances) {
|
|
|
48 |
$key = false;
|
|
|
49 |
$nokey = false;
|
|
|
50 |
foreach ($instances as $instance) {
|
|
|
51 |
if ($this->can_self_enrol($instance, false) !== true) {
|
|
|
52 |
// User can not enrol himself.
|
|
|
53 |
// Note that we do not check here if user is already enrolled for performance reasons -
|
|
|
54 |
// such check would execute extra queries for each course in the list of courses and
|
|
|
55 |
// would hide self-enrolment icons from guests.
|
|
|
56 |
continue;
|
|
|
57 |
}
|
|
|
58 |
if ($instance->password or $instance->customint1) {
|
|
|
59 |
$key = true;
|
|
|
60 |
} else {
|
|
|
61 |
$nokey = true;
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
$icons = array();
|
|
|
65 |
if ($nokey) {
|
|
|
66 |
$icons[] = new pix_icon('withoutkey', get_string('pluginname', 'enrol_self'), 'enrol_self');
|
|
|
67 |
}
|
|
|
68 |
if ($key) {
|
|
|
69 |
$icons[] = new pix_icon('withkey', get_string('pluginname', 'enrol_self'), 'enrol_self');
|
|
|
70 |
}
|
|
|
71 |
return $icons;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Returns localised name of enrol instance
|
|
|
76 |
*
|
|
|
77 |
* @param stdClass $instance (null is accepted too)
|
|
|
78 |
* @return string
|
|
|
79 |
*/
|
|
|
80 |
public function get_instance_name($instance) {
|
|
|
81 |
global $DB;
|
|
|
82 |
|
|
|
83 |
if (empty($instance->name)) {
|
|
|
84 |
if (!empty($instance->roleid) and $role = $DB->get_record('role', array('id'=>$instance->roleid))) {
|
|
|
85 |
$role = ' (' . role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING)) . ')';
|
|
|
86 |
} else {
|
|
|
87 |
$role = '';
|
|
|
88 |
}
|
|
|
89 |
$enrol = $this->get_name();
|
|
|
90 |
return get_string('pluginname', 'enrol_'.$enrol) . $role;
|
|
|
91 |
} else {
|
|
|
92 |
return format_string($instance->name);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function roles_protected() {
|
|
|
97 |
// Users may tweak the roles later.
|
|
|
98 |
return false;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
public function allow_unenrol(stdClass $instance) {
|
|
|
102 |
// Users with unenrol cap may unenrol other users manually manually.
|
|
|
103 |
return true;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public function allow_manage(stdClass $instance) {
|
|
|
107 |
// Users with manage cap may tweak period and status.
|
|
|
108 |
return true;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public function show_enrolme_link(stdClass $instance) {
|
|
|
112 |
|
|
|
113 |
if (true !== $this->can_self_enrol($instance, false)) {
|
|
|
114 |
return false;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
return true;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Return true if we can add a new instance to this course.
|
|
|
122 |
*
|
|
|
123 |
* @param int $courseid
|
|
|
124 |
* @return boolean
|
|
|
125 |
*/
|
|
|
126 |
public function can_add_instance($courseid) {
|
|
|
127 |
$context = context_course::instance($courseid, MUST_EXIST);
|
|
|
128 |
|
|
|
129 |
if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/self:config', $context)) {
|
|
|
130 |
return false;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
return true;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Self enrol user to course
|
|
|
138 |
*
|
|
|
139 |
* @param stdClass $instance enrolment instance
|
|
|
140 |
* @param stdClass $data data needed for enrolment.
|
|
|
141 |
* @return bool|array true if enroled else eddor code and messege
|
|
|
142 |
*/
|
|
|
143 |
public function enrol_self(stdClass $instance, $data = null) {
|
|
|
144 |
global $DB, $USER, $CFG;
|
|
|
145 |
|
|
|
146 |
// Don't enrol user if password is not passed when required.
|
|
|
147 |
if ($instance->password && !isset($data->enrolpassword)) {
|
|
|
148 |
return;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
$timestart = time();
|
|
|
152 |
if ($instance->enrolperiod) {
|
|
|
153 |
$timeend = $timestart + $instance->enrolperiod;
|
|
|
154 |
} else {
|
|
|
155 |
$timeend = 0;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
|
|
|
159 |
|
|
|
160 |
\core\notification::success(get_string('youenrolledincourse', 'enrol'));
|
|
|
161 |
|
|
|
162 |
// Test whether the password is also used as a group key.
|
|
|
163 |
if ($instance->password && $instance->customint1) {
|
|
|
164 |
$groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
|
|
|
165 |
foreach ($groups as $group) {
|
|
|
166 |
if (empty($group->enrolmentkey)) {
|
|
|
167 |
continue;
|
|
|
168 |
}
|
|
|
169 |
if ($group->enrolmentkey === $data->enrolpassword) {
|
|
|
170 |
// Add user to group.
|
|
|
171 |
require_once($CFG->dirroot.'/group/lib.php');
|
|
|
172 |
groups_add_member($group->id, $USER->id);
|
|
|
173 |
break;
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Creates course enrol form, checks if form submitted
|
|
|
181 |
* and enrols user if necessary. It can also redirect.
|
|
|
182 |
*
|
|
|
183 |
* @param stdClass $instance
|
|
|
184 |
* @return string html text, usually a form in a text box
|
|
|
185 |
*/
|
|
|
186 |
public function enrol_page_hook(stdClass $instance) {
|
|
|
187 |
global $CFG, $OUTPUT, $USER;
|
|
|
188 |
|
|
|
189 |
require_once("$CFG->dirroot/enrol/self/locallib.php");
|
|
|
190 |
|
|
|
191 |
$enrolstatus = $this->can_self_enrol($instance);
|
|
|
192 |
|
|
|
193 |
if (true === $enrolstatus) {
|
|
|
194 |
// This user can self enrol using this instance.
|
|
|
195 |
$form = new enrol_self_enrol_form(null, $instance);
|
|
|
196 |
$instanceid = optional_param('instance', 0, PARAM_INT);
|
|
|
197 |
if ($instance->id == $instanceid) {
|
|
|
198 |
if ($data = $form->get_data()) {
|
|
|
199 |
$this->enrol_self($instance, $data);
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
} else {
|
|
|
203 |
// This user can not self enrol using this instance. Using an empty form to keep
|
|
|
204 |
// the UI consistent with other enrolment plugins that returns a form.
|
|
|
205 |
$data = new stdClass();
|
|
|
206 |
$data->header = $this->get_instance_name($instance);
|
|
|
207 |
$data->info = $enrolstatus;
|
|
|
208 |
|
|
|
209 |
// The can_self_enrol call returns a button to the login page if the user is a
|
|
|
210 |
// guest, setting the login url to the form if that is the case.
|
|
|
211 |
$url = isguestuser() ? get_login_url() : null;
|
|
|
212 |
$form = new enrol_self_empty_form($url, $data);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
ob_start();
|
|
|
216 |
$form->display();
|
|
|
217 |
$output = ob_get_clean();
|
|
|
218 |
return $OUTPUT->box($output);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Checks if user can self enrol.
|
|
|
223 |
*
|
|
|
224 |
* @param stdClass $instance enrolment instance
|
|
|
225 |
* @param bool $checkuserenrolment if true will check if user enrolment is inactive.
|
|
|
226 |
* used by navigation to improve performance.
|
|
|
227 |
* @return bool|string true if successful, else error message or false.
|
|
|
228 |
*/
|
|
|
229 |
public function can_self_enrol(stdClass $instance, $checkuserenrolment = true) {
|
|
|
230 |
global $DB, $OUTPUT, $USER;
|
|
|
231 |
|
|
|
232 |
if ($checkuserenrolment) {
|
|
|
233 |
if (isguestuser()) {
|
|
|
234 |
// Can not enrol guest.
|
|
|
235 |
return get_string('noguestaccess', 'enrol') . $OUTPUT->continue_button(get_login_url());
|
|
|
236 |
}
|
|
|
237 |
// Check if user is already enroled.
|
|
|
238 |
if ($DB->get_record('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
|
|
|
239 |
return get_string('canntenrol', 'enrol_self');
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
// Check if self enrolment is available right now for users.
|
|
|
244 |
$result = $this->is_self_enrol_available($instance);
|
|
|
245 |
if ($result !== true) {
|
|
|
246 |
return $result;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
// Check if user has the capability to enrol in this context.
|
|
|
250 |
if (!has_capability('enrol/self:enrolself', context_course::instance($instance->courseid))) {
|
|
|
251 |
return get_string('canntenrol', 'enrol_self');
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
return true;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Does this plugin support some way to self enrol?
|
|
|
259 |
* This function doesn't check user capabilities. Use can_self_enrol to check capabilities.
|
|
|
260 |
*
|
|
|
261 |
* @param stdClass $instance enrolment instance
|
|
|
262 |
* @return bool - true means "Enrol me in this course" link could be available
|
|
|
263 |
*/
|
|
|
264 |
public function is_self_enrol_available(stdClass $instance) {
|
|
|
265 |
global $CFG, $DB, $USER;
|
|
|
266 |
|
|
|
267 |
if ($instance->status != ENROL_INSTANCE_ENABLED) {
|
|
|
268 |
return get_string('canntenrol', 'enrol_self');
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) {
|
|
|
272 |
return get_string('canntenrolearly', 'enrol_self', userdate($instance->enrolstartdate));
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) {
|
|
|
276 |
return get_string('canntenrollate', 'enrol_self', userdate($instance->enrolenddate));
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
if (!$instance->customint6) {
|
|
|
280 |
// New enrols not allowed.
|
|
|
281 |
return get_string('canntenrol', 'enrol_self');
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) {
|
|
|
285 |
return get_string('canntenrol', 'enrol_self');
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
if ($instance->customint3 > 0) {
|
|
|
289 |
// Max enrol limit specified.
|
|
|
290 |
$count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
|
|
|
291 |
if ($count >= $instance->customint3) {
|
|
|
292 |
// Bad luck, no more self enrolments here.
|
|
|
293 |
return get_string('maxenrolledreached', 'enrol_self');
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
if ($instance->customint5) {
|
|
|
298 |
require_once("$CFG->dirroot/cohort/lib.php");
|
|
|
299 |
if (!cohort_is_member($instance->customint5, $USER->id)) {
|
|
|
300 |
$cohort = $DB->get_record('cohort', array('id' => $instance->customint5));
|
|
|
301 |
if (!$cohort) {
|
|
|
302 |
return null;
|
|
|
303 |
}
|
|
|
304 |
$a = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)));
|
|
|
305 |
return markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a));
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
return true;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
* Return information for enrolment instance containing list of parameters required
|
|
|
314 |
* for enrolment, name of enrolment plugin etc.
|
|
|
315 |
*
|
|
|
316 |
* @param stdClass $instance enrolment instance
|
|
|
317 |
* @return stdClass instance info.
|
|
|
318 |
*/
|
|
|
319 |
public function get_enrol_info(stdClass $instance) {
|
|
|
320 |
|
|
|
321 |
$instanceinfo = new stdClass();
|
|
|
322 |
$instanceinfo->id = $instance->id;
|
|
|
323 |
$instanceinfo->courseid = $instance->courseid;
|
|
|
324 |
$instanceinfo->type = $this->get_name();
|
|
|
325 |
$instanceinfo->name = $this->get_instance_name($instance);
|
|
|
326 |
$instanceinfo->status = $this->can_self_enrol($instance);
|
|
|
327 |
|
|
|
328 |
if ($instance->password) {
|
|
|
329 |
$instanceinfo->requiredparam = new stdClass();
|
|
|
330 |
$instanceinfo->requiredparam->enrolpassword = get_string('password', 'enrol_self');
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
// If enrolment is possible and password is required then return ws function name to get more information.
|
|
|
334 |
if ((true === $instanceinfo->status) && $instance->password) {
|
|
|
335 |
$instanceinfo->wsfunction = 'enrol_self_get_instance_info';
|
|
|
336 |
}
|
|
|
337 |
return $instanceinfo;
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
/**
|
|
|
341 |
* Add new instance of enrol plugin with default settings.
|
|
|
342 |
* @param stdClass $course
|
|
|
343 |
* @return int id of new instance
|
|
|
344 |
*/
|
|
|
345 |
public function add_default_instance($course) {
|
|
|
346 |
$fields = $this->get_instance_defaults();
|
|
|
347 |
|
|
|
348 |
if ($this->get_config('requirepassword')) {
|
|
|
349 |
$fields['password'] = generate_password(20);
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
return $this->add_instance($course, $fields);
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Returns defaults for new instances.
|
|
|
357 |
* @return array
|
|
|
358 |
*/
|
|
|
359 |
public function get_instance_defaults() {
|
|
|
360 |
$expirynotify = $this->get_config('expirynotify');
|
|
|
361 |
if ($expirynotify == 2) {
|
|
|
362 |
$expirynotify = 1;
|
|
|
363 |
$notifyall = 1;
|
|
|
364 |
} else {
|
|
|
365 |
$notifyall = 0;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
$fields = array();
|
|
|
369 |
$fields['status'] = $this->get_config('status');
|
|
|
370 |
$fields['roleid'] = $this->get_config('roleid');
|
|
|
371 |
$fields['enrolperiod'] = $this->get_config('enrolperiod');
|
|
|
372 |
$fields['expirynotify'] = $expirynotify;
|
|
|
373 |
$fields['notifyall'] = $notifyall;
|
|
|
374 |
$fields['expirythreshold'] = $this->get_config('expirythreshold');
|
|
|
375 |
$fields['customint1'] = $this->get_config('groupkey');
|
|
|
376 |
$fields['customint2'] = $this->get_config('longtimenosee');
|
|
|
377 |
$fields['customint3'] = $this->get_config('maxenrolled');
|
|
|
378 |
$fields['customint4'] = $this->get_config('sendcoursewelcomemessage');
|
|
|
379 |
$fields['customint5'] = 0;
|
|
|
380 |
$fields['customint6'] = $this->get_config('newenrols');
|
|
|
381 |
|
|
|
382 |
return $fields;
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
/**
|
|
|
386 |
* Send welcome email to specified user.
|
|
|
387 |
*
|
|
|
388 |
* @param stdClass $instance
|
|
|
389 |
* @param stdClass $user user record
|
|
|
390 |
* @return void
|
|
|
391 |
* @deprecated since Moodle 4.4
|
|
|
392 |
* @see \enrol_plugin::send_course_welcome_message_to_user()
|
|
|
393 |
* @todo MDL-81185 Final deprecation in Moodle 4.8.
|
|
|
394 |
*/
|
|
|
395 |
#[\core\attribute\deprecated('enrol_plugin::send_course_welcome_message_to_user', since: '4.4', mdl: 'MDL-4188')]
|
|
|
396 |
protected function email_welcome_message($instance, $user) {
|
|
|
397 |
\core\deprecation::emit_deprecation_if_present(__FUNCTION__);
|
|
|
398 |
$this->send_course_welcome_message_to_user(
|
|
|
399 |
instance: $instance,
|
|
|
400 |
userid: $user->id,
|
|
|
401 |
sendoption: $instance->customint4,
|
|
|
402 |
message: $instance->customtext1,
|
|
|
403 |
);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
/**
|
|
|
407 |
* Sync all meta course links.
|
|
|
408 |
*
|
|
|
409 |
* @param progress_trace $trace
|
|
|
410 |
* @param int $courseid one course, empty mean all
|
|
|
411 |
* @return int 0 means ok, 1 means error, 2 means plugin disabled
|
|
|
412 |
*/
|
|
|
413 |
public function sync(progress_trace $trace, $courseid = null) {
|
|
|
414 |
global $DB;
|
|
|
415 |
|
|
|
416 |
if (!enrol_is_enabled('self')) {
|
|
|
417 |
$trace->finished();
|
|
|
418 |
return 2;
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
// Unfortunately this may take a long time, execution can be interrupted safely here.
|
|
|
422 |
core_php_time_limit::raise();
|
|
|
423 |
raise_memory_limit(MEMORY_HUGE);
|
|
|
424 |
|
|
|
425 |
$trace->output('Verifying self-enrolments...');
|
|
|
426 |
|
|
|
427 |
$params = array('now'=>time(), 'useractive'=>ENROL_USER_ACTIVE, 'courselevel'=>CONTEXT_COURSE);
|
|
|
428 |
$coursesql = "";
|
|
|
429 |
if ($courseid) {
|
|
|
430 |
$coursesql = "AND e.courseid = :courseid";
|
|
|
431 |
$params['courseid'] = $courseid;
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
// Note: the logic of self enrolment guarantees that user logged in at least once (=== u.lastaccess set)
|
|
|
435 |
// and that user accessed course at least once too (=== user_lastaccess record exists).
|
|
|
436 |
|
|
|
437 |
// First deal with users that did not log in for a really long time - they do not have user_lastaccess records.
|
|
|
438 |
$sql = "SELECT e.*, ue.userid
|
|
|
439 |
FROM {user_enrolments} ue
|
|
|
440 |
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
|
|
|
441 |
JOIN {user} u ON u.id = ue.userid
|
|
|
442 |
WHERE :now - u.lastaccess > e.customint2
|
|
|
443 |
$coursesql";
|
|
|
444 |
$rs = $DB->get_recordset_sql($sql, $params);
|
|
|
445 |
foreach ($rs as $instance) {
|
|
|
446 |
$userid = $instance->userid;
|
|
|
447 |
unset($instance->userid);
|
|
|
448 |
$this->unenrol_user($instance, $userid);
|
|
|
449 |
$days = $instance->customint2 / DAYSECS;
|
|
|
450 |
$trace->output("unenrolling user $userid from course $instance->courseid " .
|
|
|
451 |
"as they did not log in for at least $days days", 1);
|
|
|
452 |
}
|
|
|
453 |
$rs->close();
|
|
|
454 |
|
|
|
455 |
// Now unenrol from course user did not visit for a long time.
|
|
|
456 |
$sql = "SELECT e.*, ue.userid
|
|
|
457 |
FROM {user_enrolments} ue
|
|
|
458 |
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
|
|
|
459 |
JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid)
|
|
|
460 |
WHERE :now - ul.timeaccess > e.customint2
|
|
|
461 |
$coursesql";
|
|
|
462 |
$rs = $DB->get_recordset_sql($sql, $params);
|
|
|
463 |
foreach ($rs as $instance) {
|
|
|
464 |
$userid = $instance->userid;
|
|
|
465 |
unset($instance->userid);
|
|
|
466 |
$this->unenrol_user($instance, $userid);
|
|
|
467 |
$days = $instance->customint2 / DAYSECS;
|
|
|
468 |
$trace->output("unenrolling user $userid from course $instance->courseid " .
|
|
|
469 |
"as they did not access the course for at least $days days", 1);
|
|
|
470 |
}
|
|
|
471 |
$rs->close();
|
|
|
472 |
|
|
|
473 |
$trace->output('...user self-enrolment updates finished.');
|
|
|
474 |
$trace->finished();
|
|
|
475 |
|
|
|
476 |
$this->process_expirations($trace, $courseid);
|
|
|
477 |
|
|
|
478 |
return 0;
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
/**
|
|
|
482 |
* Notify users about enrolment expiration.
|
|
|
483 |
*
|
|
|
484 |
* Users may be notified by the expiration time of enrollment or unenrollment due to inactivity. The latter is checked in
|
|
|
485 |
* the last condition of the where clause:
|
|
|
486 |
* days of inactivity + number of days in advance to send the notification > days of inactivity allowed before unenrollment
|
|
|
487 |
*
|
|
|
488 |
* @param int $timenow Current time.
|
|
|
489 |
* @param string $name Name of this enrol plugin.
|
|
|
490 |
* @param progress_trace $trace (accepts bool for backwards compatibility only)
|
|
|
491 |
* @return void
|
|
|
492 |
*/
|
|
|
493 |
protected function fetch_users_and_notify_expiry(int $timenow, string $name, progress_trace $trace): void {
|
|
|
494 |
global $DB, $CFG;
|
|
|
495 |
|
|
|
496 |
$sql = "SELECT ue.*, e.expirynotify, e.notifyall, e.expirythreshold, e.courseid, c.fullname, e.customint2,
|
|
|
497 |
COALESCE(ul.timeaccess, 0) AS timeaccess, ue.timestart
|
|
|
498 |
FROM {user_enrolments} ue
|
|
|
499 |
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :name AND e.expirynotify > 0 AND e.status = :enabled)
|
|
|
500 |
JOIN {course} c ON (c.id = e.courseid)
|
|
|
501 |
JOIN {user} u ON (u.id = ue.userid AND u.deleted = 0 AND u.suspended = 0)
|
|
|
502 |
LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = c.id)
|
|
|
503 |
WHERE ue.status = :active
|
|
|
504 |
AND ((ue.timeend > 0 AND ue.timeend > :now1 AND ue.timeend < (e.expirythreshold + :now2))
|
|
|
505 |
OR (e.customint2 > 0 AND (:now3 - COALESCE(ul.timeaccess, 0) + e.expirythreshold > e.customint2)))
|
|
|
506 |
ORDER BY ue.enrolid ASC, u.lastname ASC, u.firstname ASC, u.id ASC";
|
|
|
507 |
$params = [
|
|
|
508 |
'name' => $name,
|
|
|
509 |
'enabled' => ENROL_INSTANCE_ENABLED,
|
|
|
510 |
'active' => ENROL_USER_ACTIVE,
|
|
|
511 |
'now1' => $timenow,
|
|
|
512 |
'now2' => $timenow,
|
|
|
513 |
'now3' => $timenow,
|
|
|
514 |
];
|
|
|
515 |
|
|
|
516 |
$rs = $DB->get_recordset_sql($sql, $params);
|
|
|
517 |
|
|
|
518 |
$lastenrollid = 0;
|
|
|
519 |
$users = [];
|
|
|
520 |
|
|
|
521 |
foreach ($rs as $ue) {
|
|
|
522 |
$expirycond = ($ue->timeend > 0) && ($ue->timeend > $timenow) && ($ue->timeend < ($ue->expirythreshold + $timenow));
|
|
|
523 |
$inactivitycond = ($ue->customint2 > 0) && (($timenow - $ue->timeaccess + $ue->expirythreshold) > $ue->customint2);
|
|
|
524 |
|
|
|
525 |
$user = $DB->get_record('user', ['id' => $ue->userid]);
|
|
|
526 |
|
|
|
527 |
if ($expirycond) {
|
|
|
528 |
if ($lastenrollid && $lastenrollid != $ue->enrolid) {
|
|
|
529 |
$this->notify_expiry_enroller($lastenrollid, $users, $trace);
|
|
|
530 |
$users = [];
|
|
|
531 |
}
|
|
|
532 |
$lastenrollid = $ue->enrolid;
|
|
|
533 |
|
|
|
534 |
$enroller = $this->get_enroller($ue->enrolid);
|
|
|
535 |
$context = context_course::instance($ue->courseid);
|
|
|
536 |
|
|
|
537 |
$users[] = [
|
|
|
538 |
'fullname' => fullname($user, has_capability('moodle/site:viewfullnames', $context, $enroller)),
|
|
|
539 |
'timeend' => $ue->timeend,
|
|
|
540 |
];
|
|
|
541 |
}
|
|
|
542 |
// Notifications to inactive users only if inactive time limit is set.
|
|
|
543 |
if ($inactivitycond && $ue->notifyall) {
|
|
|
544 |
$ue->message = 'expiryinactivemessageenrolledbody';
|
|
|
545 |
$lastaccess = $ue->timeaccess;
|
|
|
546 |
if (!$lastaccess) {
|
|
|
547 |
$lastaccess = $ue->timestart;
|
|
|
548 |
}
|
|
|
549 |
$ue->inactivetime = floor(($timenow - $lastaccess) / DAYSECS);
|
|
|
550 |
$this->notify_expiry_enrolled($user, $ue, $trace);
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
if ($expirycond) {
|
|
|
554 |
if (!$ue->notifyall) {
|
|
|
555 |
continue;
|
|
|
556 |
}
|
|
|
557 |
|
|
|
558 |
if ($ue->timeend - $ue->expirythreshold + 86400 < $timenow) {
|
|
|
559 |
// Notify enrolled users only once at the start of the threshold.
|
|
|
560 |
$trace->output("user $ue->userid was already notified that enrolment in course $ue->courseid expires on " .
|
|
|
561 |
userdate($ue->timeend, '', $CFG->timezone), 1);
|
|
|
562 |
continue;
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
$this->notify_expiry_enrolled($user, $ue, $trace);
|
|
|
566 |
}
|
|
|
567 |
}
|
|
|
568 |
$rs->close();
|
|
|
569 |
|
|
|
570 |
if ($lastenrollid && $users) {
|
|
|
571 |
$this->notify_expiry_enroller($lastenrollid, $users, $trace);
|
|
|
572 |
}
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
/**
|
|
|
576 |
* Returns the user who is responsible for self enrolments in given instance.
|
|
|
577 |
*
|
|
|
578 |
* Usually it is the first editing teacher - the person with "highest authority"
|
|
|
579 |
* as defined by sort_by_roleassignment_authority() having 'enrol/self:manage'
|
|
|
580 |
* capability.
|
|
|
581 |
*
|
|
|
582 |
* @param int $instanceid enrolment instance id
|
|
|
583 |
* @return stdClass user record
|
|
|
584 |
*/
|
|
|
585 |
protected function get_enroller($instanceid) {
|
|
|
586 |
global $DB;
|
|
|
587 |
|
|
|
588 |
if ($this->lasternollerinstanceid == $instanceid and $this->lasternoller) {
|
|
|
589 |
return $this->lasternoller;
|
|
|
590 |
}
|
|
|
591 |
|
|
|
592 |
$instance = $DB->get_record('enrol', array('id'=>$instanceid, 'enrol'=>$this->get_name()), '*', MUST_EXIST);
|
|
|
593 |
$context = context_course::instance($instance->courseid);
|
|
|
594 |
|
|
|
595 |
if ($users = get_enrolled_users($context, 'enrol/self:manage')) {
|
|
|
596 |
$users = sort_by_roleassignment_authority($users, $context);
|
|
|
597 |
$this->lasternoller = reset($users);
|
|
|
598 |
unset($users);
|
|
|
599 |
} else {
|
|
|
600 |
$this->lasternoller = parent::get_enroller($instanceid);
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
$this->lasternollerinstanceid = $instanceid;
|
|
|
604 |
|
|
|
605 |
return $this->lasternoller;
|
|
|
606 |
}
|
|
|
607 |
|
|
|
608 |
protected function get_expiry_message_body(stdClass $user, stdClass $ue, string $name,
|
|
|
609 |
stdClass $enroller, context $context): string {
|
|
|
610 |
$a = new stdClass();
|
|
|
611 |
$a->course = format_string($ue->fullname, true, ['context' => $context]);
|
|
|
612 |
$a->user = fullname($user, true);
|
|
|
613 |
// If the enrolment duration is disabled, replace timeend with other data.
|
|
|
614 |
if ($ue->timeend == 0) {
|
|
|
615 |
$timenow = time();
|
|
|
616 |
$lastaccess = $ue->timeaccess > 0 ? $ue->timeaccess : $ue->timestart;
|
|
|
617 |
$ue->timeend = $timenow + ($ue->customint2 - ($timenow - $lastaccess));
|
|
|
618 |
}
|
|
|
619 |
$a->timeend = userdate($ue->timeend, '', $user->timezone);
|
|
|
620 |
$a->enroller = fullname($enroller, has_capability('moodle/site:viewfullnames', $context, $user));
|
|
|
621 |
if (isset($ue->inactivetime)) {
|
|
|
622 |
$a->inactivetime = $ue->inactivetime;
|
|
|
623 |
}
|
|
|
624 |
$a->url = new moodle_url('/course/view.php', ['id' => $ue->courseid]);
|
|
|
625 |
return get_string($ue->message ?? 'expirymessageenrolledbody', 'enrol_' . $name, $a);
|
|
|
626 |
}
|
|
|
627 |
|
|
|
628 |
/**
|
|
|
629 |
* Restore instance and map settings.
|
|
|
630 |
*
|
|
|
631 |
* @param restore_enrolments_structure_step $step
|
|
|
632 |
* @param stdClass $data
|
|
|
633 |
* @param stdClass $course
|
|
|
634 |
* @param int $oldid
|
|
|
635 |
*/
|
|
|
636 |
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
|
|
|
637 |
global $DB;
|
|
|
638 |
if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) {
|
|
|
639 |
$merge = false;
|
|
|
640 |
} else {
|
|
|
641 |
$merge = array(
|
|
|
642 |
'courseid' => $data->courseid,
|
|
|
643 |
'enrol' => $this->get_name(),
|
|
|
644 |
'status' => $data->status,
|
|
|
645 |
'roleid' => $data->roleid,
|
|
|
646 |
);
|
|
|
647 |
}
|
|
|
648 |
if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) {
|
|
|
649 |
$instance = reset($instances);
|
|
|
650 |
$instanceid = $instance->id;
|
|
|
651 |
} else {
|
|
|
652 |
if (!empty($data->customint5)) {
|
|
|
653 |
if ($step->get_task()->is_samesite()) {
|
|
|
654 |
// Keep cohort restriction unchanged - we are on the same site.
|
|
|
655 |
} else {
|
|
|
656 |
// Use some id that can not exist in order to prevent self enrolment,
|
|
|
657 |
// because we do not know what cohort it is in this site.
|
|
|
658 |
$data->customint5 = -1;
|
|
|
659 |
}
|
|
|
660 |
}
|
|
|
661 |
$instanceid = $this->add_instance($course, (array)$data);
|
|
|
662 |
}
|
|
|
663 |
$step->set_mapping('enrol', $oldid, $instanceid);
|
|
|
664 |
}
|
|
|
665 |
|
|
|
666 |
/**
|
|
|
667 |
* Restore user enrolment.
|
|
|
668 |
*
|
|
|
669 |
* @param restore_enrolments_structure_step $step
|
|
|
670 |
* @param stdClass $data
|
|
|
671 |
* @param stdClass $instance
|
|
|
672 |
* @param int $oldinstancestatus
|
|
|
673 |
* @param int $userid
|
|
|
674 |
*/
|
|
|
675 |
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
|
|
|
676 |
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status);
|
|
|
677 |
}
|
|
|
678 |
|
|
|
679 |
/**
|
|
|
680 |
* Restore role assignment.
|
|
|
681 |
*
|
|
|
682 |
* @param stdClass $instance
|
|
|
683 |
* @param int $roleid
|
|
|
684 |
* @param int $userid
|
|
|
685 |
* @param int $contextid
|
|
|
686 |
*/
|
|
|
687 |
public function restore_role_assignment($instance, $roleid, $userid, $contextid) {
|
|
|
688 |
// This is necessary only because we may migrate other types to this instance,
|
|
|
689 |
// we do not use component in manual or self enrol.
|
|
|
690 |
role_assign($roleid, $userid, $contextid, '', 0);
|
|
|
691 |
}
|
|
|
692 |
|
|
|
693 |
/**
|
|
|
694 |
* Is it possible to delete enrol instance via standard UI?
|
|
|
695 |
*
|
|
|
696 |
* @param stdClass $instance
|
|
|
697 |
* @return bool
|
|
|
698 |
*/
|
|
|
699 |
public function can_delete_instance($instance) {
|
|
|
700 |
$context = context_course::instance($instance->courseid);
|
|
|
701 |
return has_capability('enrol/self:config', $context);
|
|
|
702 |
}
|
|
|
703 |
|
|
|
704 |
/**
|
|
|
705 |
* Is it possible to hide/show enrol instance via standard UI?
|
|
|
706 |
*
|
|
|
707 |
* @param stdClass $instance
|
|
|
708 |
* @return bool
|
|
|
709 |
*/
|
|
|
710 |
public function can_hide_show_instance($instance) {
|
|
|
711 |
$context = context_course::instance($instance->courseid);
|
|
|
712 |
|
|
|
713 |
if (!has_capability('enrol/self:config', $context)) {
|
|
|
714 |
return false;
|
|
|
715 |
}
|
|
|
716 |
|
|
|
717 |
// If the instance is currently disabled, before it can be enabled,
|
|
|
718 |
// we must check whether the password meets the password policies.
|
|
|
719 |
if ($instance->status == ENROL_INSTANCE_DISABLED) {
|
|
|
720 |
if ($this->get_config('requirepassword')) {
|
|
|
721 |
if (empty($instance->password)) {
|
|
|
722 |
return false;
|
|
|
723 |
}
|
|
|
724 |
}
|
|
|
725 |
// Only check the password if it is set.
|
|
|
726 |
if (!empty($instance->password) && $this->get_config('usepasswordpolicy')) {
|
|
|
727 |
if (!check_password_policy($instance->password, $errmsg)) {
|
|
|
728 |
return false;
|
|
|
729 |
}
|
|
|
730 |
}
|
|
|
731 |
}
|
|
|
732 |
|
|
|
733 |
return true;
|
|
|
734 |
}
|
|
|
735 |
|
|
|
736 |
/**
|
|
|
737 |
* Return an array of valid options for the status.
|
|
|
738 |
*
|
|
|
739 |
* @return array
|
|
|
740 |
*/
|
|
|
741 |
protected function get_status_options() {
|
|
|
742 |
$options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
|
|
|
743 |
ENROL_INSTANCE_DISABLED => get_string('no'));
|
|
|
744 |
return $options;
|
|
|
745 |
}
|
|
|
746 |
|
|
|
747 |
/**
|
|
|
748 |
* Return an array of valid options for the newenrols property.
|
|
|
749 |
*
|
|
|
750 |
* @return array
|
|
|
751 |
*/
|
|
|
752 |
protected function get_newenrols_options() {
|
|
|
753 |
$options = array(1 => get_string('yes'), 0 => get_string('no'));
|
|
|
754 |
return $options;
|
|
|
755 |
}
|
|
|
756 |
|
|
|
757 |
/**
|
|
|
758 |
* Return an array of valid options for the groupkey property.
|
|
|
759 |
*
|
|
|
760 |
* @return array
|
|
|
761 |
*/
|
|
|
762 |
protected function get_groupkey_options() {
|
|
|
763 |
$options = array(1 => get_string('yes'), 0 => get_string('no'));
|
|
|
764 |
return $options;
|
|
|
765 |
}
|
|
|
766 |
|
|
|
767 |
/**
|
|
|
768 |
* Return an array of valid options for the expirynotify property.
|
|
|
769 |
*
|
|
|
770 |
* @return array
|
|
|
771 |
*/
|
|
|
772 |
protected function get_expirynotify_options() {
|
|
|
773 |
$options = array(0 => get_string('no'),
|
|
|
774 |
1 => get_string('expirynotifyenroller', 'enrol_self'),
|
|
|
775 |
2 => get_string('expirynotifyall', 'enrol_self'));
|
|
|
776 |
return $options;
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
/**
|
|
|
780 |
* Return an array of valid options for the longtimenosee property.
|
|
|
781 |
*
|
|
|
782 |
* @return array
|
|
|
783 |
*/
|
|
|
784 |
protected function get_longtimenosee_options() {
|
|
|
785 |
$options = array(0 => get_string('never'),
|
|
|
786 |
1800 * 3600 * 24 => get_string('numdays', '', 1800),
|
|
|
787 |
1000 * 3600 * 24 => get_string('numdays', '', 1000),
|
|
|
788 |
365 * 3600 * 24 => get_string('numdays', '', 365),
|
|
|
789 |
180 * 3600 * 24 => get_string('numdays', '', 180),
|
|
|
790 |
150 * 3600 * 24 => get_string('numdays', '', 150),
|
|
|
791 |
120 * 3600 * 24 => get_string('numdays', '', 120),
|
|
|
792 |
90 * 3600 * 24 => get_string('numdays', '', 90),
|
|
|
793 |
60 * 3600 * 24 => get_string('numdays', '', 60),
|
|
|
794 |
30 * 3600 * 24 => get_string('numdays', '', 30),
|
|
|
795 |
21 * 3600 * 24 => get_string('numdays', '', 21),
|
|
|
796 |
14 * 3600 * 24 => get_string('numdays', '', 14),
|
|
|
797 |
7 * 3600 * 24 => get_string('numdays', '', 7));
|
|
|
798 |
return $options;
|
|
|
799 |
}
|
|
|
800 |
|
|
|
801 |
/**
|
|
|
802 |
* The self enrollment plugin has several bulk operations that can be performed.
|
|
|
803 |
* @param course_enrolment_manager $manager
|
|
|
804 |
* @return array
|
|
|
805 |
*/
|
|
|
806 |
public function get_bulk_operations(course_enrolment_manager $manager) {
|
|
|
807 |
global $CFG;
|
|
|
808 |
require_once($CFG->dirroot.'/enrol/self/locallib.php');
|
|
|
809 |
$context = $manager->get_context();
|
|
|
810 |
$bulkoperations = array();
|
|
|
811 |
if (has_capability("enrol/self:manage", $context)) {
|
|
|
812 |
$bulkoperations['editselectedusers'] = new enrol_self_editselectedusers_operation($manager, $this);
|
|
|
813 |
}
|
|
|
814 |
if (has_capability("enrol/self:unenrol", $context)) {
|
|
|
815 |
$bulkoperations['deleteselectedusers'] = new enrol_self_deleteselectedusers_operation($manager, $this);
|
|
|
816 |
}
|
|
|
817 |
return $bulkoperations;
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
/**
|
|
|
821 |
* Add elements to the edit instance form.
|
|
|
822 |
*
|
|
|
823 |
* @param stdClass $instance
|
|
|
824 |
* @param MoodleQuickForm $mform
|
|
|
825 |
* @param context $context
|
|
|
826 |
* @return bool
|
|
|
827 |
*/
|
|
|
828 |
public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
|
|
|
829 |
global $CFG, $DB;
|
|
|
830 |
|
|
|
831 |
// Merge these two settings to one value for the single selection element.
|
|
|
832 |
if ($instance->notifyall and $instance->expirynotify) {
|
|
|
833 |
$instance->expirynotify = 2;
|
|
|
834 |
}
|
|
|
835 |
unset($instance->notifyall);
|
|
|
836 |
|
|
|
837 |
$nameattribs = array('size' => '20', 'maxlength' => '255');
|
|
|
838 |
$mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs);
|
|
|
839 |
$mform->setType('name', PARAM_TEXT);
|
|
|
840 |
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server');
|
|
|
841 |
|
|
|
842 |
$options = $this->get_status_options();
|
|
|
843 |
$mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options);
|
|
|
844 |
$mform->addHelpButton('status', 'status', 'enrol_self');
|
|
|
845 |
|
|
|
846 |
$options = $this->get_newenrols_options();
|
|
|
847 |
$mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options);
|
|
|
848 |
$mform->addHelpButton('customint6', 'newenrols', 'enrol_self');
|
|
|
849 |
$mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED);
|
|
|
850 |
|
|
|
851 |
$passattribs = array('size' => '20', 'maxlength' => '50');
|
|
|
852 |
$mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'), $passattribs);
|
|
|
853 |
$mform->addHelpButton('password', 'password', 'enrol_self');
|
|
|
854 |
if (empty($instance->id) and $this->get_config('requirepassword')) {
|
|
|
855 |
$mform->addRule('password', get_string('required'), 'required', null, 'client');
|
|
|
856 |
}
|
|
|
857 |
$mform->addRule('password', get_string('maximumchars', '', 50), 'maxlength', 50, 'server');
|
|
|
858 |
|
|
|
859 |
$options = $this->get_groupkey_options();
|
|
|
860 |
$mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options);
|
|
|
861 |
$mform->addHelpButton('customint1', 'groupkey', 'enrol_self');
|
|
|
862 |
|
|
|
863 |
$roles = $this->extend_assignable_roles($context, $instance->roleid);
|
|
|
864 |
$mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles);
|
|
|
865 |
|
|
|
866 |
$options = array('optional' => true, 'defaultunit' => 86400);
|
|
|
867 |
$mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), $options);
|
|
|
868 |
$mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self');
|
|
|
869 |
|
|
|
870 |
$options = $this->get_expirynotify_options();
|
|
|
871 |
$mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options);
|
|
|
872 |
$mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol');
|
|
|
873 |
|
|
|
874 |
$options = array('optional' => false, 'defaultunit' => 86400);
|
|
|
875 |
$mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), $options);
|
|
|
876 |
$mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol');
|
|
|
877 |
$mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0);
|
|
|
878 |
|
|
|
879 |
$options = array('optional' => true);
|
|
|
880 |
$mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), $options);
|
|
|
881 |
$mform->setDefault('enrolstartdate', 0);
|
|
|
882 |
$mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self');
|
|
|
883 |
|
|
|
884 |
$options = array('optional' => true);
|
|
|
885 |
$mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), $options);
|
|
|
886 |
$mform->setDefault('enrolenddate', 0);
|
|
|
887 |
$mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self');
|
|
|
888 |
|
|
|
889 |
$options = $this->get_longtimenosee_options();
|
|
|
890 |
$mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options);
|
|
|
891 |
$mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self');
|
|
|
892 |
|
|
|
893 |
$mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self'));
|
|
|
894 |
$mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self');
|
|
|
895 |
$mform->setType('customint3', PARAM_INT);
|
|
|
896 |
|
|
|
897 |
require_once($CFG->dirroot.'/cohort/lib.php');
|
|
|
898 |
|
|
|
899 |
$cohorts = array(0 => get_string('no'));
|
|
|
900 |
$allcohorts = cohort_get_available_cohorts($context, 0, 0, 0);
|
|
|
901 |
if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
|
|
|
902 |
$c = $DB->get_record('cohort',
|
|
|
903 |
array('id' => $instance->customint5),
|
|
|
904 |
'id, name, idnumber, contextid, visible',
|
|
|
905 |
IGNORE_MISSING);
|
|
|
906 |
if ($c) {
|
|
|
907 |
// Current cohort was not found because current user can not see it. Still keep it.
|
|
|
908 |
$allcohorts[$instance->customint5] = $c;
|
|
|
909 |
}
|
|
|
910 |
}
|
|
|
911 |
foreach ($allcohorts as $c) {
|
|
|
912 |
$cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid)));
|
|
|
913 |
if ($c->idnumber) {
|
|
|
914 |
$cohorts[$c->id] .= ' ['.s($c->idnumber).']';
|
|
|
915 |
}
|
|
|
916 |
}
|
|
|
917 |
if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
|
|
|
918 |
// Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol.
|
|
|
919 |
$cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5);
|
|
|
920 |
}
|
|
|
921 |
if (count($cohorts) > 1) {
|
|
|
922 |
$mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts);
|
|
|
923 |
$mform->addHelpButton('customint5', 'cohortonly', 'enrol_self');
|
|
|
924 |
} else {
|
|
|
925 |
$mform->addElement('hidden', 'customint5');
|
|
|
926 |
$mform->setType('customint5', PARAM_INT);
|
|
|
927 |
$mform->setConstant('customint5', 0);
|
|
|
928 |
}
|
|
|
929 |
|
|
|
930 |
$mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'),
|
|
|
931 |
enrol_send_welcome_email_options());
|
|
|
932 |
$mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
|
|
|
933 |
|
|
|
934 |
$options = array('cols' => '60', 'rows' => '8');
|
|
|
935 |
$mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options);
|
|
|
936 |
$mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol'));
|
|
|
937 |
$mform->hideIf(
|
|
|
938 |
elementname: 'customtext1',
|
|
|
939 |
dependenton: 'customint4',
|
|
|
940 |
condition: 'eq',
|
|
|
941 |
value: ENROL_DO_NOT_SEND_EMAIL,
|
|
|
942 |
);
|
|
|
943 |
|
|
|
944 |
// Static form elements cannot be hidden by hideIf() so we need to add a dummy group.
|
|
|
945 |
// See: https://tracker.moodle.org/browse/MDL-66251.
|
|
|
946 |
$group[] = $mform->createElement(
|
|
|
947 |
'static',
|
|
|
948 |
'customwelcomemessage_extra_help',
|
|
|
949 |
null,
|
|
|
950 |
get_string(
|
|
|
951 |
identifier: 'customwelcomemessage_help',
|
|
|
952 |
component: 'core_enrol',
|
|
|
953 |
),
|
|
|
954 |
);
|
|
|
955 |
$mform->addGroup($group, 'group_customwelcomemessage_extra_help', '', ' ', false);
|
|
|
956 |
$mform->hideIf(
|
|
|
957 |
elementname: 'group_customwelcomemessage_extra_help',
|
|
|
958 |
dependenton: 'customint4',
|
|
|
959 |
condition: 'eq',
|
|
|
960 |
value: ENROL_DO_NOT_SEND_EMAIL,
|
|
|
961 |
);
|
|
|
962 |
|
|
|
963 |
if (enrol_accessing_via_instance($instance)) {
|
|
|
964 |
$warntext = get_string('instanceeditselfwarningtext', 'core_enrol');
|
|
|
965 |
$mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warntext);
|
|
|
966 |
}
|
|
|
967 |
}
|
|
|
968 |
|
|
|
969 |
/**
|
|
|
970 |
* We are a good plugin and don't invent our own UI/validation code path.
|
|
|
971 |
*
|
|
|
972 |
* @return boolean
|
|
|
973 |
*/
|
|
|
974 |
public function use_standard_editing_ui() {
|
|
|
975 |
return true;
|
|
|
976 |
}
|
|
|
977 |
|
|
|
978 |
/**
|
|
|
979 |
* Perform custom validation of the data used to edit the instance.
|
|
|
980 |
*
|
|
|
981 |
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
982 |
* @param array $files array of uploaded files "element_name"=>tmp_file_path
|
|
|
983 |
* @param object $instance The instance loaded from the DB
|
|
|
984 |
* @param context $context The context of the instance we are editing
|
|
|
985 |
* @return array of "element_name"=>"error_description" if there are errors,
|
|
|
986 |
* or an empty array if everything is OK.
|
|
|
987 |
* @return void
|
|
|
988 |
*/
|
|
|
989 |
public function edit_instance_validation($data, $files, $instance, $context) {
|
|
|
990 |
global $CFG;
|
|
|
991 |
require_once("{$CFG->dirroot}/enrol/self/locallib.php");
|
|
|
992 |
|
|
|
993 |
$errors = array();
|
|
|
994 |
|
|
|
995 |
$checkpassword = false;
|
|
|
996 |
|
|
|
997 |
if ($instance->id) {
|
|
|
998 |
// Check the password if we are enabling the plugin again.
|
|
|
999 |
if (($instance->status == ENROL_INSTANCE_DISABLED) && ($data['status'] == ENROL_INSTANCE_ENABLED)) {
|
|
|
1000 |
$checkpassword = true;
|
|
|
1001 |
}
|
|
|
1002 |
|
|
|
1003 |
// Check the password if the instance is enabled and the password has changed.
|
|
|
1004 |
if (($data['status'] == ENROL_INSTANCE_ENABLED) && ($instance->password !== $data['password'])) {
|
|
|
1005 |
$checkpassword = true;
|
|
|
1006 |
}
|
|
|
1007 |
|
|
|
1008 |
// Check the password if we are enabling group enrolment keys.
|
|
|
1009 |
if (!$instance->customint1 && $data['customint1']) {
|
|
|
1010 |
$checkpassword = true;
|
|
|
1011 |
}
|
|
|
1012 |
} else {
|
|
|
1013 |
$checkpassword = true;
|
|
|
1014 |
}
|
|
|
1015 |
|
|
|
1016 |
if ($checkpassword) {
|
|
|
1017 |
$require = $this->get_config('requirepassword');
|
|
|
1018 |
$policy = $this->get_config('usepasswordpolicy');
|
|
|
1019 |
if ($require and trim($data['password']) === '') {
|
|
|
1020 |
$errors['password'] = get_string('required');
|
|
|
1021 |
} else if (!empty($data['password'])) {
|
|
|
1022 |
if ($policy) {
|
|
|
1023 |
$errmsg = '';
|
|
|
1024 |
if (!check_password_policy($data['password'], $errmsg)) {
|
|
|
1025 |
$errors['password'] = $errmsg;
|
|
|
1026 |
}
|
|
|
1027 |
}
|
|
|
1028 |
if ($data['customint1'] && enrol_self_check_group_enrolment_key($instance->courseid, $data['password'])) {
|
|
|
1029 |
$errors['password'] = get_string('passwordmatchesgroupkey', 'enrol_self');
|
|
|
1030 |
}
|
|
|
1031 |
}
|
|
|
1032 |
}
|
|
|
1033 |
|
|
|
1034 |
if ($data['status'] == ENROL_INSTANCE_ENABLED) {
|
|
|
1035 |
if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
|
|
|
1036 |
$errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self');
|
|
|
1037 |
}
|
|
|
1038 |
}
|
|
|
1039 |
|
|
|
1040 |
if (array_key_exists('expirynotify', $data)
|
|
|
1041 |
&& ($data['expirynotify'] > 0 || $data['customint2'])
|
|
|
1042 |
&& $data['expirythreshold'] < 86400) {
|
|
|
1043 |
$errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
|
|
|
1044 |
}
|
|
|
1045 |
|
|
|
1046 |
// Now these ones are checked by quickforms, but we may be called by the upload enrolments tool, or a webservive.
|
|
|
1047 |
if (array_key_exists('name', $data) && core_text::strlen($data['name']) > 255) {
|
|
|
1048 |
$errors['name'] = get_string('err_maxlength', 'form', 255);
|
|
|
1049 |
}
|
|
|
1050 |
$validstatus = array_keys($this->get_status_options());
|
|
|
1051 |
$validnewenrols = array_keys($this->get_newenrols_options());
|
|
|
1052 |
if (core_text::strlen($data['password']) > 50) {
|
|
|
1053 |
$errors['name'] = get_string('err_maxlength', 'form', 50);
|
|
|
1054 |
}
|
|
|
1055 |
$validgroupkey = array_keys($this->get_groupkey_options());
|
|
|
1056 |
$context = context_course::instance($instance->courseid);
|
|
|
1057 |
$validroles = array_keys($this->extend_assignable_roles($context, $instance->roleid));
|
|
|
1058 |
$validexpirynotify = array_keys($this->get_expirynotify_options());
|
|
|
1059 |
$validlongtimenosee = array_keys($this->get_longtimenosee_options());
|
|
|
1060 |
$tovalidate = array(
|
|
|
1061 |
'enrolstartdate' => PARAM_INT,
|
|
|
1062 |
'enrolenddate' => PARAM_INT,
|
|
|
1063 |
'name' => PARAM_TEXT,
|
|
|
1064 |
'customint1' => $validgroupkey,
|
|
|
1065 |
'customint2' => $validlongtimenosee,
|
|
|
1066 |
'customint3' => PARAM_INT,
|
|
|
1067 |
'customint4' => PARAM_INT,
|
|
|
1068 |
'customint5' => PARAM_INT,
|
|
|
1069 |
'customint6' => $validnewenrols,
|
|
|
1070 |
'status' => $validstatus,
|
|
|
1071 |
'enrolperiod' => PARAM_INT,
|
|
|
1072 |
'expirynotify' => $validexpirynotify,
|
|
|
1073 |
'roleid' => $validroles
|
|
|
1074 |
);
|
|
|
1075 |
if (array_key_exists('expirynotify', $data) && $data['expirynotify'] != 0) {
|
|
|
1076 |
$tovalidate['expirythreshold'] = PARAM_INT;
|
|
|
1077 |
}
|
|
|
1078 |
$typeerrors = $this->validate_param_types($data, $tovalidate);
|
|
|
1079 |
$errors = array_merge($errors, $typeerrors);
|
|
|
1080 |
|
|
|
1081 |
return $errors;
|
|
|
1082 |
}
|
|
|
1083 |
|
|
|
1084 |
/**
|
|
|
1085 |
* Add new instance of enrol plugin.
|
|
|
1086 |
* @param object $course
|
|
|
1087 |
* @param array $fields instance fields
|
|
|
1088 |
* @return int id of new instance, null if can not be created
|
|
|
1089 |
*/
|
|
|
1090 |
public function add_instance($course, array $fields = null) {
|
|
|
1091 |
// In the form we are representing 2 db columns with one field.
|
|
|
1092 |
if (!empty($fields) && !empty($fields['expirynotify'])) {
|
|
|
1093 |
if ($fields['expirynotify'] == 2) {
|
|
|
1094 |
$fields['expirynotify'] = 1;
|
|
|
1095 |
$fields['notifyall'] = 1;
|
|
|
1096 |
} else {
|
|
|
1097 |
$fields['notifyall'] = 0;
|
|
|
1098 |
}
|
|
|
1099 |
}
|
|
|
1100 |
|
|
|
1101 |
return parent::add_instance($course, $fields);
|
|
|
1102 |
}
|
|
|
1103 |
|
|
|
1104 |
/**
|
|
|
1105 |
* Update instance of enrol plugin.
|
|
|
1106 |
* @param stdClass $instance
|
|
|
1107 |
* @param stdClass $data modified instance fields
|
|
|
1108 |
* @return boolean
|
|
|
1109 |
*/
|
|
|
1110 |
public function update_instance($instance, $data) {
|
|
|
1111 |
// In the form we are representing 2 db columns with one field.
|
|
|
1112 |
if ($data->expirynotify == 2) {
|
|
|
1113 |
$data->expirynotify = 1;
|
|
|
1114 |
$data->notifyall = 1;
|
|
|
1115 |
} else {
|
|
|
1116 |
$data->notifyall = 0;
|
|
|
1117 |
}
|
|
|
1118 |
// Keep previous/default value of disabled expirythreshold option.
|
|
|
1119 |
if (!$data->expirynotify) {
|
|
|
1120 |
$data->expirythreshold = $instance->expirythreshold;
|
|
|
1121 |
}
|
|
|
1122 |
// Add previous value of newenrols if disabled.
|
|
|
1123 |
if (!isset($data->customint6)) {
|
|
|
1124 |
$data->customint6 = $instance->customint6;
|
|
|
1125 |
}
|
|
|
1126 |
|
|
|
1127 |
return parent::update_instance($instance, $data);
|
|
|
1128 |
}
|
|
|
1129 |
|
|
|
1130 |
/**
|
|
|
1131 |
* Gets a list of roles that this user can assign for the course as the default for self-enrolment.
|
|
|
1132 |
*
|
|
|
1133 |
* @param context $context the context.
|
|
|
1134 |
* @param integer $defaultrole the id of the role that is set as the default for self-enrolment
|
|
|
1135 |
* @return array index is the role id, value is the role name
|
|
|
1136 |
*/
|
|
|
1137 |
public function extend_assignable_roles($context, $defaultrole) {
|
|
|
1138 |
global $DB;
|
|
|
1139 |
|
|
|
1140 |
$roles = get_assignable_roles($context, ROLENAME_BOTH);
|
|
|
1141 |
if (!isset($roles[$defaultrole])) {
|
|
|
1142 |
if ($role = $DB->get_record('role', array('id' => $defaultrole))) {
|
|
|
1143 |
$roles[$defaultrole] = role_get_name($role, $context, ROLENAME_BOTH);
|
|
|
1144 |
}
|
|
|
1145 |
}
|
|
|
1146 |
return $roles;
|
|
|
1147 |
}
|
|
|
1148 |
|
|
|
1149 |
/**
|
|
|
1150 |
* Get the "from" contact which the email will be sent from.
|
|
|
1151 |
*
|
|
|
1152 |
* @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_*
|
|
|
1153 |
* @param $context context where the user will be fetched
|
|
|
1154 |
* @return mixed|stdClass the contact user object.
|
|
|
1155 |
* @deprecated since Moodle 4.4
|
|
|
1156 |
* @see \enrol_plugin::get_welcome_message_contact()
|
|
|
1157 |
* @todo MDL-81185 Final deprecation in Moodle 4.8.
|
|
|
1158 |
*/
|
|
|
1159 |
#[\core\attribute\deprecated('enrol_plugin::get_welcome_message_contact', since: '4.4', mdl: 'MDL-4188')]
|
|
|
1160 |
public function get_welcome_email_contact($sendoption, $context) {
|
|
|
1161 |
\core\deprecation::emit_deprecation_if_present(__FUNCTION__);
|
|
|
1162 |
return $this->get_welcome_message_contact(
|
|
|
1163 |
sendoption: $sendoption,
|
|
|
1164 |
context: $context,
|
|
|
1165 |
);
|
|
|
1166 |
}
|
|
|
1167 |
|
|
|
1168 |
/**
|
|
|
1169 |
* Check if enrolment plugin is supported in csv course upload.
|
|
|
1170 |
*
|
|
|
1171 |
* @return bool
|
|
|
1172 |
*/
|
|
|
1173 |
public function is_csv_upload_supported(): bool {
|
|
|
1174 |
return true;
|
|
|
1175 |
}
|
|
|
1176 |
|
|
|
1177 |
/**
|
|
|
1178 |
* Finds matching instances for a given course.
|
|
|
1179 |
*
|
|
|
1180 |
* @param array $enrolmentdata enrolment data.
|
|
|
1181 |
* @param int $courseid Course ID.
|
|
|
1182 |
* @return stdClass|null Matching instance
|
|
|
1183 |
*/
|
|
|
1184 |
public function find_instance(array $enrolmentdata, int $courseid): ?stdClass {
|
|
|
1185 |
|
|
|
1186 |
$instances = enrol_get_instances($courseid, false);
|
|
|
1187 |
$instance = null;
|
|
|
1188 |
foreach ($instances as $i) {
|
|
|
1189 |
if ($i->enrol == 'self') {
|
|
|
1190 |
// This is bad - we can not really distinguish between self instances. So grab first available.
|
|
|
1191 |
$instance = $i;
|
|
|
1192 |
break;
|
|
|
1193 |
}
|
|
|
1194 |
}
|
|
|
1195 |
return $instance;
|
|
|
1196 |
}
|
|
|
1197 |
|
|
|
1198 |
/**
|
|
|
1199 |
* Fill custom fields data for a given enrolment plugin.
|
|
|
1200 |
*
|
|
|
1201 |
* @param array $enrolmentdata enrolment data.
|
|
|
1202 |
* @param int $courseid Course ID.
|
|
|
1203 |
* @return array Updated enrolment data with custom fields info.
|
|
|
1204 |
*/
|
|
|
1205 |
public function fill_enrol_custom_fields(array $enrolmentdata, int $courseid): array {
|
|
|
1206 |
return $enrolmentdata + ['password' => ''];
|
|
|
1207 |
}
|
|
|
1208 |
|
|
|
1209 |
/**
|
|
|
1210 |
* Updates enrol plugin instance with provided data.
|
|
|
1211 |
* @param int $courseid Course ID.
|
|
|
1212 |
* @param array $enrolmentdata enrolment data.
|
|
|
1213 |
* @param stdClass $instance Instance to update.
|
|
|
1214 |
*
|
|
|
1215 |
* @return stdClass updated instance
|
|
|
1216 |
*/
|
|
|
1217 |
public function update_enrol_plugin_data(int $courseid, array $enrolmentdata, stdClass $instance): stdClass {
|
|
|
1218 |
if (!empty($enrolmentdata['password'])) {
|
|
|
1219 |
$instance->password = $enrolmentdata['password'];
|
|
|
1220 |
}
|
|
|
1221 |
return parent::update_enrol_plugin_data($courseid, $enrolmentdata, $instance);
|
|
|
1222 |
}
|
|
|
1223 |
|
|
|
1224 |
/**
|
|
|
1225 |
* Check if data is valid for a given enrolment plugin
|
|
|
1226 |
*
|
|
|
1227 |
* @param array $enrolmentdata enrolment data to validate.
|
|
|
1228 |
* @param int|null $courseid Course ID.
|
|
|
1229 |
* @return array Errors
|
|
|
1230 |
*/
|
|
|
1231 |
public function validate_enrol_plugin_data(array $enrolmentdata, ?int $courseid = null): array {
|
|
|
1232 |
global $CFG, $DB;
|
|
|
1233 |
|
|
|
1234 |
require_once($CFG->dirroot . "/enrol/self/locallib.php");
|
|
|
1235 |
|
|
|
1236 |
// If password is omitted or empty in csv it will be generated automatically if it is a required policy.
|
|
|
1237 |
|
|
|
1238 |
$errors = parent::validate_enrol_plugin_data($enrolmentdata, $courseid);
|
|
|
1239 |
$policy = $this->get_config('usepasswordpolicy');
|
|
|
1240 |
if (!empty($enrolmentdata['password'])) {
|
|
|
1241 |
if ($policy) {
|
|
|
1242 |
$errarray = get_password_policy_errors($enrolmentdata['password']);
|
|
|
1243 |
foreach ($errarray as $i => $err) {
|
|
|
1244 |
$errors['enrol_self' . $i] = $err;
|
|
|
1245 |
}
|
|
|
1246 |
}
|
|
|
1247 |
|
|
|
1248 |
if ($courseid) {
|
|
|
1249 |
// This is bad - no way to identify which instance it is.
|
|
|
1250 |
// So if any instance in course uses group key we should error.
|
|
|
1251 |
$usegroupenrolmentkeys =
|
|
|
1252 |
$DB->count_records('enrol', ['courseid' => $courseid, 'enrol' => 'self', 'customint1' => 1]);
|
|
|
1253 |
if ($usegroupenrolmentkeys && enrol_self_check_group_enrolment_key($courseid, $enrolmentdata['password'])) {
|
|
|
1254 |
$errors['errorpasswordmatchesgroupkey'] =
|
|
|
1255 |
new lang_string('passwordmatchesgroupkey', 'enrol_self', $enrolmentdata['password']);
|
|
|
1256 |
}
|
|
|
1257 |
}
|
|
|
1258 |
}
|
|
|
1259 |
return $errors;
|
|
|
1260 |
}
|
|
|
1261 |
}
|
|
|
1262 |
|
|
|
1263 |
/**
|
|
|
1264 |
* Get icon mapping for font-awesome.
|
|
|
1265 |
*/
|
|
|
1266 |
function enrol_self_get_fontawesome_icon_map() {
|
|
|
1267 |
return [
|
|
|
1268 |
'enrol_self:withkey' => 'fa-key',
|
|
|
1269 |
'enrol_self:withoutkey' => 'fa-sign-in',
|
|
|
1270 |
];
|
|
|
1271 |
}
|