-
Notifications
You must be signed in to change notification settings - Fork 7
/
invitation_form.php
executable file
·530 lines (480 loc) · 22.2 KB
/
invitation_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
// This file is part of Invitation for Moodle - https://moodle.org/
//
// Invitation is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Invitation is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Form to display invitation.
*
* @package enrol_invitation
* @copyright 2021-2024 TNG Consulting Inc. {@link https://www.tngconsulting.ca}
* @author Michael Milette
* @copyright 2013 UC Regents
* @author Rex Lorenzo
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once('locallib.php');
require_once($CFG->dirroot . '/lib/formslib.php');
require_once($CFG->dirroot . '/lib/enrollib.php');
/**
* Class for sending invitation to enrol users in a course when the "Use invitation with default values" field is set to "Yes".
*
* @copyright 2021-2024 TNG Consulting Inc. {@link https://www.tngconsulting.ca}
* @copyright 2013 UC Regents
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class invitation_form extends moodleform {
/**
* The form definition if "Use invitation with default values" is set to "No".
*/
public function definition() {
global $CFG, $USER, $COURSE;
$mform = & $this->_form;
// Get rid of "Collapse all" in Moodle 2.5+.
if (method_exists($mform, 'setDisableShortforms')) {
$mform->setDisableShortforms(true);
}
// Add some hidden fields.
$course = $this->_customdata['course'];
$instance = $this->_customdata['instance'];
$context = $this->_customdata['context'];
$prefilled = $this->_customdata['prefilled'];
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
$mform->setDefault('courseid', $course->id);
// Set roles.
$mform->addElement('header', 'header_role', get_string('header_role', 'enrol_invitation'));
$siteroles = $this->getappropiateroles($course);
$label = get_string('assignrole', 'enrol_invitation');
$rolegroup = [];
foreach ($siteroles as $roletype => $roles) {
$roletypestring = html_writer::tag('div', get_string('archetype' . $roletype, 'role'), ['class' => 'label badge-info']);
$rolegroup[] = &$mform->createElement('static', 'role_type_header', '', $roletypestring);
foreach ($roles as $role) {
$rolestring = $this->formatrolestring($role);
$rolegroup[] = &$mform->createElement('radio', 'roleid', '', $rolestring, $role->id);
}
}
$mform->setDefault('roleid', 3);
$mform->addGroup($rolegroup, 'role_group', $label, '<br>');
$mform->addRule('role_group', get_string('norole', 'enrol_invitation'), 'required');
// Email address field.
$mform->addElement('header', 'header_email', get_string('header_email', 'enrol_invitation'));
$mform->addElement(
'textarea',
'email',
get_string('emailaddressnumber', 'enrol_invitation'),
['maxlength' => 1000, 'class' => 'form-invite-email', 'style' => 'resize: both;', 'cols' => 65, 'rows' => 5]
);
$mform->setType('email', PARAM_TEXT);
// Check for correct email formatting later in validation() function.
$mform->addElement('static', 'email_clarification', '', get_string('email_clarification', 'enrol_invitation'));
$mform->setType('email', PARAM_TEXT);
if ($CFG->branch >= 311) {
$userfields = \core_user\fields::for_identity($context)->get_required_fields();
} else {
$userfields = get_extra_user_fields($context);
}
$options = [
'ajax' => 'enrol_manual/form-potential-user-selector',
'multiple' => true,
'courseid' => $course->id,
'enrolid' => $instance->id,
'perpage' => $CFG->maxusersperpage,
'userfields' => implode(',', $userfields),
'valuehtmlcallback' => function ($value) {
global $OUTPUT;
if ($user = \core_user::get_user($value)) {
$useroptiondata = [
'fullname' => fullname($user),
'idnumber' => $user->idnumber,
'email' => $user->email,
'suspended' => 0,
];
return $OUTPUT->render_from_template('enrol_manual/form-user-selector-suggestion', $useroptiondata);
}
},
];
$mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), [], $options);
if (has_capability('moodle/cohort:manage', $context) || has_capability('moodle/cohort:view', $context)) {
// Check to ensure there is at least one visible cohort before displaying the select box.
// Ideally it would be better to call external_api::call_external_function('core_cohort_search_cohorts')
// (which is used to populate the select box) instead of duplicating logic but there is an issue with globals
// being borked (in this case $PAGE) when combining the usage of fragments and call_external_function().
require_once($CFG->dirroot . '/cohort/lib.php');
$availablecohorts = cohort_get_cohorts($context->id, 0, 1, '');
$availablecohorts = $availablecohorts['cohorts'];
if (!($context instanceof context_system)) {
$availablecohorts = array_merge($availablecohorts, cohort_get_available_cohorts($context, COHORT_ALL, 0, 1, ''));
}
if (!empty($availablecohorts)) {
$options = ['contextid' => $context->id, 'multiple' => true];
$mform->addElement('cohort', 'cohortlist', get_string('selectcohorts', 'enrol_manual'), $options);
}
}
$this->_customdata['registeredonly'] ? $mform->addElement(
'static',
'email_registered',
'',
get_string('registeredonly_help', 'enrol_invitation')
) : null;
// Subject field.
$mform->addElement('text', 'subject', get_string('subject', 'enrol_invitation'), ['class' => 'form-invite-subject']);
$mform->setType('subject', PARAM_TEXT);
$mform->addRule('subject', get_string('required'), 'required');
$defaultsubject = get_string('default_subject', 'enrol_invitation', getcoursesubject($COURSE));
$mform->setDefault('subject', $defaultsubject);
// Message field.
$mform->addElement('editor', 'message', get_string('message', 'enrol_invitation'), ['class' => 'form-invite-message']);
$mform->setType('message', PARAM_RAW);
// Put help text to show what default message invitee gets.
$mform->addHelpButton('message', 'emailmsghtml', 'enrol_invitation', get_string('message_help_link', 'enrol_invitation'));
// Email options.
// Prepare string variables.
$temp = new stdClass();
$temp->email = $USER->email;
$temp->supportemail = !empty($CFG->supportemail) ? $CFG->supportemail : $CFG->noreplyaddress;
$mform->addElement('checkbox', 'show_from_email', '', get_string('show_from_email', 'enrol_invitation', $temp));
$mform->addElement('checkbox', 'notify_inviter', '', get_string('notify_inviter', 'enrol_invitation', $temp));
$mform->setDefault('show_from_email', 1);
$mform->setDefault('notify_inviter', 0);
// Set defaults if the user is resending an invite that expired.
if (!empty($prefilled)) {
$mform->setDefault('role_group[roleid]', $prefilled['roleid']);
$mform->setDefault('email', $prefilled['email']);
$mform->setDefault('subject', $prefilled['subject']);
$mform->setDefault('message', $prefilled['message']);
$mform->setDefault('show_from_email', $prefilled['show_from_email']);
$mform->setDefault('notify_inviter', $prefilled['notify_inviter']);
}
$this->add_action_buttons(false, get_string('inviteusers', 'enrol_invitation'));
}
/**
* Overriding get_data, because we need to be able to handle daysexpire, which is not defined as a regular form element.
*
* @return object
*/
public function get_data() {
$retval = parent::get_data();
// Check if form validated, and if user submitted daysexpire from POST.
if (false) { // Not implemented yet.
if (!empty($retval) && isset($_POST['daysexpire'])) {
if (in_array($_POST['daysexpire'], self::$daysexpireoptions)) {
// Cannot indicate to user a real error message, so just slightly ignore user setting.
$retval->daysexpire = $_POST['daysexpire'];
}
}
}
return $retval;
}
/**
* Given a role record, format string to be displayable to user. Filter out role notes and other information.
*
* @param object $role Record from role table.
* @return string
*/
private function formatrolestring($role) {
$rolestring = html_writer::tag('span', $role->name, ['class' => 'role-name']);
// Role description has a <hr> tag to separate out info for users and admins.
if (strpos($role->description, '<hr />') !== false) {
$roledescription = explode('<hr />', $role->description);
} else if (strpos($role->description, '<hr>') !== false) {
$roledescription = explode('<hr>', $role->description);
} else {
$roledescription[0] = $role->description;
}
// Need to clean html, because tinymce adds a lot of extra tags that mess up formatting.
$roledescription = $roledescription[0];
// Whitelist some formatting tags.
$roledescription = strip_tags($roledescription, '<b><i><strong><ul><li><ol>');
if (!empty($roledescription)) {
$rolestring .= ': ' . $roledescription;
}
return $rolestring;
}
/**
* Private class method to return a list of appropriate roles for given course and user.
*
* @param object $course Course record.
* @return array Returns array of roles indexed by role archetype.
*/
private function getappropiateroles($course) {
global $DB;
$retval = [];
$context = context_course::instance($course->id);
$roles = get_assignable_roles($context);
if (empty($roles)) {
return $retval;
}
// Get full role records for archetype and description.
foreach ($roles as $roleid => $rolename) {
$record = $DB->get_record('role', ['id' => $roleid]);
$record->name = $rolename; // User might have customised name.
$retval[$record->archetype][] = $record;
}
return $retval;
}
/**
* Provides custom validation rules.
* - Validating the email field here, rather than in definition, to allow multiple email addresses to be specified.
* - Validating that access end date is in the future.
*
* @param array $data
* @param array $files
*
* @return array
*/
public function validation($data, $files) {
$errors = [];
$delimiters = "/[;, \r\n]/";
$emaillist = self::parsedsvemails($data['email'], $delimiters);
if (!empty($data['email']) && empty($emaillist)) {
$errors['email'] = get_string('err_email', 'form');
}
return $errors;
}
/**
* Parses a string containing delimiter separated values for email addresses.
* Returns an empty array if an invalid email is found.
*
* @param string $emails string of emails to be parsed
* @param string $delimiters list of delimiters as regex
* @return array $parsedemails array of emails
*/
public static function parsedsvemails($emails, $delimiters) {
$parsedemails = [];
$emails = trim($emails);
if (preg_match($delimiters, $emails)) {
// Multiple email addresses specified.
$dsvemails = preg_split($delimiters, $emails, null, PREG_SPLIT_NO_EMPTY);
foreach ($dsvemails as $emailvalue) {
$emailvalue = trim($emailvalue);
if (!clean_param($emailvalue, PARAM_EMAIL)) {
return [];
}
$parsedemails[] = $emailvalue;
}
} else if (clean_param($emails, PARAM_EMAIL)) {
// Single email.
return (array) $emails;
} else {
return [];
}
return $parsedemails;
}
/**
* Get list of email addresses for users belonging to registered user accounts.
*
* @param array $userlist List of user ids.
* @return array Email addresses.
*/
public static function parse_userlist_emails($userlist) {
global $DB;
$parsedemails = [];
if ($userlist) {
foreach ($userlist as $userid) {
$parsedemails[] = $DB->get_record('user', ['id' => $userid], '*', MUST_EXIST)->email;
}
}
return $parsedemails;
}
/**
* Get list of email addresses belonging to users specified cohorts of a given course.
*
* @param array $cohortlist List of cohort ids.
* @param object $course Course object.
* @return array Email addresses.
*/
public static function parse_cohortlist_emails($cohortlist, $course) {
global $DB;
$parsedemails = [];
if ($cohortlist) {
foreach ($cohortlist as $cohortid) {
$context = context_course::instance($course->id);
[$esql, $params] = get_enrolled_sql($context);
$sql = "SELECT cm.userid FROM {cohort_members} cm LEFT JOIN ($esql) u ON u.id = cm.userid " .
"WHERE cm.cohortid = :cohortid AND u.id IS NULL";
$params['cohortid'] = $cohortid;
$members = $DB->get_fieldset_sql($sql, $params);
foreach ($members as $user) {
$parsedemails[] = $DB->get_record('user', ['id' => $user], '*', MUST_EXIST)->email;
}
}
}
return $parsedemails;
}
}
/**
* Class for sending invitation to enrol users in a course when the "Use invitation with default values" field is set to "No".
*/
class invitation_email_form extends moodleform {
/**
* The form definition if "Use invitation with default values" is set to "Yes".
*/
public function definition() {
global $CFG, $USER;
$mform = & $this->_form;
// Get rid of "Collapse all" in Moodle 2.5+.
if (method_exists($mform, 'setDisableShortforms')) {
$mform->setDisableShortforms(true);
}
// Add some hidden fields.
$course = $this->_customdata['course'];
$instance = $this->_customdata['instance'];
$context = $this->_customdata['context'];
$prefilled = $this->_customdata['prefilled'];
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
$mform->setDefault('courseid', $course->id);
// Email address field.
$mform->addElement('header', 'header_email', get_string('header_email', 'enrol_invitation'));
$mform->addElement(
'textarea',
'email',
get_string('emailaddressnumber', 'enrol_invitation'),
['maxlength' => 1000, 'class' => 'form-invite-email', 'style' => 'resize: both;', 'cols' => 65, 'rows' => 5]
);
$registeredonly = $this->_customdata['registeredonly'] ? '<br>'
. get_string('registeredonly_help', 'enrol_invitation') : '';
$mform->addElement('static', 'email_clarification', '', get_string(
'email_clarification',
'enrol_invitation'
) . $registeredonly);
$mform->setType('email', PARAM_TEXT);
if ($CFG->branch >= 311) {
$userfields = \core_user\fields::for_identity($context)->get_required_fields();
} else {
$userfields = get_extra_user_fields($context);
}
$userfields = \core_user\fields::get_identity_fields($context, false);
$options = [
'ajax' => 'enrol_manual/form-potential-user-selector',
'multiple' => true,
'courseid' => $course->id,
'enrolid' => $instance->id,
'perpage' => $CFG->maxusersperpage,
'userfields' => implode(',', $userfields),
'valuehtmlcallback' => function ($value) {
global $OUTPUT;
if ($user = \core_user::get_user($value)) {
$useroptiondata = [
'fullname' => fullname($user),
'idnumber' => $user->idnumber,
'email' => $user->email,
'suspended' => 0,
];
return $OUTPUT->render_from_template('enrol_manual/form-potential-user-selector', $useroptiondata);
}
},
];
$mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), [], $options);
if (has_capability('moodle/cohort:manage', $context) || has_capability('moodle/cohort:view', $context)) {
// Check to ensure there is at least one visible cohort before displaying the select box.
// Ideally it would be better to call external_api::call_external_function('core_cohort_search_cohorts')
// (which is used to populate the select box) instead of duplicating logic but there is an issue with globals
// being borked (in this case $PAGE) when combining the usage of fragments and call_external_function().
require_once($CFG->dirroot . '/cohort/lib.php');
$availablecohorts = cohort_get_cohorts($context->id, 0, 1, '');
$availablecohorts = $availablecohorts['cohorts'];
if (!($context instanceof context_system)) {
$availablecohorts = array_merge($availablecohorts, cohort_get_available_cohorts($context, COHORT_ALL, 0, 1, ''));
}
if (!empty($availablecohorts)) {
$options = ['contextid' => $context->id, 'multiple' => true];
$mform->addElement('cohort', 'cohortlist', get_string('selectcohorts', 'enrol_manual'), $options);
}
}
// Check for correct email formating later in validation() function.
// Set defaults if the user is resending an invite that expired.
if (!empty($prefilled)) {
$mform->setDefault('role_group[roleid]', $prefilled['roleid']);
$mform->setDefault('email', $prefilled['email']);
$mform->setDefault('subject', $prefilled['subject']);
$mform->setDefault('message', $prefilled['message']);
$mform->setDefault('show_from_email', $prefilled['show_from_email']);
$mform->setDefault('notify_inviter', $prefilled['notify_inviter']);
}
$this->add_action_buttons(false, get_string('inviteusers', 'enrol_invitation'));
}
/**
* Overriding get_data, because we need to be able to handle daysexpire,
* which is not defined as a regular form element.
*
* @return object
*/
public function get_data() {
$retval = parent::get_data();
// Check if form validated, and if user submitted daysexpire from POST.
if (false) { // Not implemented yet.
if (!empty($retval) && isset($_POST['daysexpire'])) {
if (in_array($_POST['daysexpire'], self::$daysexpireoptions)) {
// Cannot indicate to user a real error message, so just slightly ignore user setting.
$retval->daysexpire = $_POST['daysexpire'];
}
}
}
return $retval;
}
/**
* Provides custom validation rules.
* - Validating the email field here, rather than in definition, to allow
* multiple email addresses to be specified.
* - Validating that access end date is in the future.
*
* @param array $data
* @param array $files
*
* @return array
*/
public function validation($data, $files) {
$errors = [];
$delimiters = "/[;, \r\n]/";
$emaillist = self::parsedsvemails($data['email'], $delimiters);
if (empty($emaillist) && empty($data['userlist']) && empty($data['cohortlist'])) {
$errors['email'] = get_string('err_email', 'form');
$errors['userlist'] = get_string('err_userlist', 'enrol_invitation');
$errors['cohortlist'] = get_string('err_cohortlist', 'enrol_invitation');
}
return $errors;
}
/**
* Parses a string containing delimiter seperated values for email addresses.
* Returns an empty array if an invalid email is found.
*
* @param string $emails string of emails to be parsed
* @param string $delimiters list of delimiters as regex
* @return array $parsedemails array of emails
*/
public static function parsedsvemails($emails, $delimiters) {
$parsedemails = [];
$emails = trim($emails);
if (preg_match($delimiters, $emails)) {
// Multiple email addresses specified.
$dsvemails = preg_split($delimiters, $emails, null, PREG_SPLIT_NO_EMPTY);
foreach ($dsvemails as $emailvalue) {
$emailvalue = trim($emailvalue);
if (!clean_param($emailvalue, PARAM_EMAIL)) {
return [];
}
$parsedemails[] = $emailvalue;
}
} else if (clean_param($emails, PARAM_EMAIL)) {
// Single email.
return (array) $emails;
} else {
return [];
}
return $parsedemails;
}
}