forked from ucla/moodle-enrol_invitation
-
Notifications
You must be signed in to change notification settings - Fork 8
/
invitation_form.php
executable file
·269 lines (233 loc) · 10.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
<?php
// This file is part of the UCLA Site Invitation Plugin for Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 <http://www.gnu.org/licenses/>.
/**
* Form to display invitation.
*
* @package enrol_invitation
* @copyright 2013 UC Regents
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
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.
*
* @copyright 2013 UC Regents
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class invitation_form extends moodleform {
/**
* The form definition.
*/
public function definition() {
global $CFG, $DB, $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'];
$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'));
$site_roles = $this->get_appropiate_roles($course);
$label = get_string('assignrole', 'enrol_invitation');
$role_group = array();
foreach ($site_roles as $role_type => $roles) {
$role_type_string = html_writer::tag('div',
get_string('archetype'.$role_type, 'role'),
array('class' => 'label badge-info'));
$role_group[] = &$mform->createElement('static', 'role_type_header',
'', $role_type_string);
foreach ($roles as $role) {
$role_string = $this->format_role_string($role);
$role_group[] = &$mform->createElement('radio', 'roleid', '',
$role_string, $role->id);
}
}
$mform->setDefault('roleid', 3);
$mform->addGroup($role_group, 'role_group', $label);
$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'),
array('maxlength' => 1000, 'class' => 'form-invite-email', 'style' => 'resize: both;'));
$mform->addRule('email', null, 'required', null, 'client');
$mform->setType('email', PARAM_TEXT);
// Check for correct email formating later in validation() function.
$mform->addElement('static', 'email_clarification', '', get_string('email_clarification', 'enrol_invitation'));
// Ssubject field.
$mform->addElement('text', 'subject', get_string('subject', 'enrol_invitation'),
array('class' => 'form-invite-subject'));
$mform->setType('subject', PARAM_TEXT);
$mform->addRule('subject', get_string('required'), 'required');
// Default subject is "Site invitation for <course title>".
$default_subject = get_string('default_subject', 'enrol_invitation',
sprintf('%s: %s', $course->shortname, $course->fullname));
$mform->setDefault('subject', $default_subject);
// Message field.
//
// $mform->addElement('textarea', 'message', get_string('message', 'enrol_invitation'),
// array('class' => 'form-invite-message'));
$mform->addElement('editor', 'message', get_string('message', 'enrol_invitation'),
array('class' => 'form-invite-message'));
$mform->setType('message', PARAM_RAW);
// Put help text to show what default message invitee gets.
$mform->addHelpButton('message', 'message', 'enrol_invitation',
get_string('message_help_link', 'enrol_invitation'));
// Email options.
// Prepare string variables.
$temp = new stdClass();
$temp->email = $USER->email;
$temp->supportemail = $CFG->supportemail;
$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 (!empty($retval) && isset($_POST['daysexpire'])) {
if (in_array($_POST['daysexpire'], self::$daysexpire_options)) {
// 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 format_role_string($role) {
$role_string = html_writer::tag('span', $role->name . ':',
array('class' => 'role-name'));
// Role description has a <hr> tag to separate out info for users
// and admins.
$role_description = explode('<hr />', $role->description);
// Need to clean html, because tinymce adds a lot of extra tags that mess up formatting.
$role_description = $role_description[0];
// Whitelist some formatting tags.
$role_description = strip_tags($role_description, '<b><i><strong><ul><li><ol>');
$role_string .= ' ' . $role_description;
return $role_string;
}
/**
* Private class method to return a list of appropiate roles for given
* course and user.
*
* @param object $course Course record.
*
* @return array Returns array of roles indexed by role archetype.
*/
private function get_appropiate_roles($course) {
global $DB;
$retval = array();
$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', array('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 = array();
$delimiters = "/[;, \r\n]/";
$email_list = self::parse_dsv_emails($data['email'], $delimiters);
if (empty($email_list)) {
$errors['email'] = get_string('err_email', 'form');
}
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 $parsed_emails array of emails
*/
public static function parse_dsv_emails($emails, $delimiters) {
$parsed_emails = array();
$emails = trim($emails);
if (preg_match($delimiters, $emails)) {
// Multiple email addresses specified.
$dsv_emails = preg_split($delimiters, $emails, null, PREG_SPLIT_NO_EMPTY);
foreach ($dsv_emails as $email_value) {
$email_value = trim($email_value);
if (!clean_param($email_value, PARAM_EMAIL)) {
return array();
}
$parsed_emails[] = $email_value;
}
} else if (clean_param($emails, PARAM_EMAIL)) {
// Single email.
return (array)$emails;
} else {
return array();
}
return $parsed_emails;
}
}