This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.php
397 lines (320 loc) · 15.2 KB
/
lib.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
<?php
// This file is part of 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/>.
/**
* local_userenrols
*
* This plugin will import user enrollments and group assignments
* from a delimited text file. It does not create new user accounts
* in Moodle, it will only enroll existing users in a course.
*
* @author Fred Woolard <[email protected]>
* @copyright (c) 2013 Appalachian State Universtiy, Boone, NC
* @license GNU General Public License version 3
* @package local_userenrols
*/
defined('MOODLE_INTERNAL') || die();
require_once("{$CFG->dirroot}/lib/accesslib.php");
require_once("{$CFG->dirroot}/lib/enrollib.php");
require_once("{$CFG->dirroot}/lib/grouplib.php");
require_once("{$CFG->dirroot}/lib/navigationlib.php");
require_once("{$CFG->dirroot}/group/lib.php");
/**
* Hook to insert a link in settings navigation menu block
*
* @param settings_navigation $navigation
* @param course_context $context
* @return void
*/
function local_userenrols_extend_settings_navigation(settings_navigation $navigation, $context)
{
global $CFG;
// If not in a course context, then leave
if ($context == null || $context->contextlevel != CONTEXT_COURSE) {
return;
}
// When on front page there is 'frontpagesettings' node, other
// courses will have 'courseadmin' node
if (null == ($courseadmin_node = $navigation->get('courseadmin'))) {
// Keeps us off the front page
return;
}
if (null == ($useradmin_node = $courseadmin_node->get('users'))) {
return;
}
// Add our link
$useradmin_node->add(
get_string('IMPORT_MENU_LONG', local_userenrols_plugin::PLUGIN_NAME),
new moodle_url("{$CFG->wwwroot}/local/userenrols/import.php", array('id' => $context->instanceid)),
navigation_node::TYPE_SETTING,
get_string('IMPORT_MENU_SHORT', local_userenrols_plugin::PLUGIN_NAME),
null, new pix_icon('i/import', 'import'));
}
/**
* The local plugin class
*/
class local_userenrols_plugin
{
/*
* Class constants
*/
/**
* @const string Reduce chance of typos.
*/
const PLUGIN_NAME = 'local_userenrols';
/**
* @const string Where we put the uploaded files.
*/
const PLUGIN_FILEAREA = 'uploads';
/**
* @const int Max size of upload file.
*/
const MAXFILESIZE = 51200;
/**
* @const string Form id for role_id.
*/
const FORMID_ROLE_ID = 'role_id';
/**
* @const string Form id for user_id (key field to match).
*/
const FORMID_USER_ID_FIELD = 'user_id';
/**
* @const string Form id for group (whether to assign or not).
*/
const FORMID_GROUP = 'group';
/**
* @const string Form id for group_id (direct assignment).
*/
const FORMID_GROUP_ID = 'group_id';
/**
* @const string Form id for group_create (if specified group missing).
*/
const FORMID_GROUP_CREATE = 'group_create';
/**
* @const string Form id for filepicker form element.
*/
const FORMID_FILES = 'filepicker';
/**
* @const string Form id for metacourse (hidden indicator).
*/
const FORMID_METACOURSE = 'metacourse';
/**
* @const string Default user_id form value (key field to match).
*/
const DEFAULT_USER_ID_FIELD = 'username';
/*
* Member vars
*/
/**
* @var array
*/
private static $user_id_field_options = null;
/*
* Methods
*/
/**
* Return list of valid options for user record field matching
*
* @return array
*/
public static function get_user_id_field_options()
{
if (self::$user_id_field_options == null) {
self::$user_id_field_options = array(
'username' => get_string('username'),
'email' => get_string('email'),
'idnumber' => get_string('idnumber')
);
}
return self::$user_id_field_options;
}
/**
* Make a role assignment in the specified course using the specified role
* id for the user whose id information is passed in the line data.
*
* @param stdClass $course Course in which to make the role assignment
* @param stdClass $enrol_instance Enrol instance to use for adding users to course
* @param string $ident_field The field (column) name in Moodle user rec against which to query using the imported data
* @param int $role_id Id of the role to use in the role assignment
* @param boolean $group_assign Whether or not to assign users to groups
* @param int $group_id Id of group to assign to, 0 indicates use group name from import file
* @param boolean $group_create Whether or not to create new groups if needed
* @param stored_file $import_file File in local repository from which to get enrollment and group data
* @return string String message with results
*
* @uses $DB
*/
public static function import_file(stdClass $course, stdClass $enrol_instance, $ident_field, $role_id, $group_assign, $group_id, $group_create, stored_file $import_file)
{
global $DB;
// Default return value
$result = '';
// Need one of these in the loop
$course_context = context_course::instance($course->id);
// Choose the regex pattern based on the $ident_field
switch($ident_field)
{
case 'email':
$regex_pattern = '/^"?\s*([a-z0-9][\w.%-]*@[a-z0-9][a-z0-9.-]{0,61}[a-z0-9]\.[a-z]{2,6})\s*"?(?:\s*[;,\t]\s*"?\s*([a-z0-9][\w\' .,&-\[\]\{\}\(\)]*))?\s*"?$/Ui';
break;
default:
$regex_pattern = '/^"?\s*([a-z0-9][\[email protected]]*)\s*"?(?:\s*[;,\t]\s*"?\s*([a-z0-9][\w\' .,&-\[\]\{\}\(\)]*))?\s*"?$/Ui';
break;
}
// If doing group assignments, want to know the valid
// groups for the course
$selected_group = null;
if ($group_assign) {
if (false === ($existing_groups = groups_get_all_groups($course->id))) {
$existing_groups = array();
}
if ($group_id > 0) {
if (array_key_exists($group_id, $existing_groups)) {
$selected_group = $existing_groups[$group_id];
} else {
// Error condition
return sprintf(get_string('ERR_INVALID_GROUP_ID', self::PLUGIN_NAME), $group_id);
}
}
}
// Iterate the list of active enrol plugins looking for
// the meta course plugin
$metacourse = false;
$enrols_enabled = enrol_get_instances($course->id, true);
foreach($enrols_enabled as $enrol) {
if ($enrol->enrol == 'meta') {
$metacourse = true;
break;
}
}
// Get an instance of the enrol_manual_plugin (not to be confused
// with the enrol_instance arg)
$manual_enrol_plugin = enrol_get_plugin('manual');
$user_rec =
$new_group =
$new_grouping = null;
// Open and fetch the file contents
$fh = $import_file->get_content_file_handle();
$line_num = 0;
while (false !== ($line = fgets($fh))) {
$line_num++;
// Clean these up for each iteration
unset($user_rec, $new_group, $new_grouping);
// Besides whitespace chars, also strip any utf-8
// BOM chars that might be at beginning of file
if (!($line = trim($line, " \n\r\t\v\0\xbb\xbf\xef"))) continue;
// Parse the line, from which we may get one or two
// matches since the group name is an optional item
// on a line by line basis
if (!preg_match($regex_pattern, $line, $matches)) {
$result .= sprintf(get_string('ERR_PATTERN_MATCH', self::PLUGIN_NAME), $line_num, $line);
continue;
}
$ident_value = $matches[1];
$group_name = isset($matches[2]) ? $matches[2] : '';
// User must already exist, we import enrollments
// into courses, not users into the system. Exclude
// records marked as deleted. Because idnumber is
// not enforced unique, possible multiple records
// returned when using that identifying field, so
// use ->get_records method to make that detection
// and inform user
$user_rec_array = $DB->get_records('user', array($ident_field => addslashes($ident_value), 'deleted' => 0));
// Should have one and only one record, otherwise
// report it and move on to the next
$user_rec_count = count($user_rec_array);
if ($user_rec_count == 0) {
// No record found
$result .= sprintf(get_string('ERR_USERID_INVALID', self::PLUGIN_NAME), $line_num, $ident_value);
continue;
} elseif ($user_rec_count > 1) {
// Too many records
$result .= sprintf(get_string('ERR_USER_MULTIPLE_RECS', self::PLUGIN_NAME), $line_num, $ident_value);
continue;
}
$user_rec = array_shift($user_rec_array);
// Fetch all the role assignments this user might have for this course's context
$roles = get_user_roles($course_context, $user_rec->id, false);
// If a user has a role in this course, then we leave it alone and move on
// to the group assignment if there is one. If they have no role, then we
// should go ahead and add one, as long as it is not a metacourse.
if (!$roles && $role_id > 0) {
if ($metacourse) {
$result .= sprintf(get_string('ERR_ENROLL_META', self::PLUGIN_NAME), $line_num, $ident_value);
} else {
try {
$manual_enrol_plugin->enrol_user($enrol_instance, $user_rec->id, $role_id);
}
catch (Exception $exc) {
$result .= sprintf(get_string('ERR_ENROLL_FAILED', self::PLUGIN_NAME), $line_num, $ident_value);
$result .= $exc->getMessage();
continue;
}
}
}
// If no group assignments, or group is from file, but no
// group found, next line
if (!$group_assign ||($group_id == 0 && empty($group_name))) continue;
// If no group pre-selected, see if group from import already
// created for that course
$assign_group_id = 0;
$assign_group_name = '';
if ($selected_group != null) {
$assign_group_id = $selected_group->id;
$assign_group_name = $selected_group->name;
} else {
foreach($existing_groups as $existing_group) {
if ($existing_group->name != $group_name)
continue;
$assign_group_id = $existing_group->id;
$assign_group_name = $existing_group->name;
break;
}
// No group by that name
if ($assign_group_id == 0) {
// Can not create one, next line
if (!$group_create) continue;
// Make a new group for this course
$new_group = new stdClass();
$new_group->name = addslashes($group_name);
$new_group->courseid = $course->id;
if (false === ($assign_group_id = groups_create_group($new_group))) {
$result .= sprintf(get_string('ERR_CREATE_GROUP', self::PLUGIN_NAME), $line_num, $group_name);
continue;
} else {
// Add the new group to our list for the benefit of
// the next contestant. Strip the slashes off the
// name since we do a name comparison earlier when
// trying to find the group in our local cache and
// an escaped semi-colon will cause the test to fail.
$new_group->name =
$assign_group_name = stripslashes($new_group->name);
$new_group->id = $assign_group_id;
$existing_groups[] = $new_group;
}
} // if ($assign_group_id == 0)
}
// Put the user in the group if not aleady in it
if ( !groups_is_member($assign_group_id, $user_rec->id)
&& !groups_add_member($assign_group_id, $user_rec->id)) {
$result .= sprintf(get_string('ERR_GROUP_MEMBER', self::PLUGIN_NAME), $line_num, $ident_value, $assign_group_name);
continue;
}
// Any other work...
} // while fgets
fclose($fh);
return (empty($result)) ? get_string('INF_IMPORT_SUCCESS', self::PLUGIN_NAME) : $result;
} // import_file
} // class