Commit | Line | Data |
---|---|---|
9e492db0 | 1 | <?php |
0a6150ca | 2 | |
3 | /// Bulk user registration script from a comma separated file | |
4 | /// Returns list of users with their user ids | |
5 | ||
e4e38544 | 6 | require('../config.php'); |
cc891abe | 7 | require_once($CFG->libdir.'/adminlib.php'); |
e4e38544 | 8 | require_once($CFG->libdir.'/csvlib.class.php'); |
9 | require_once($CFG->dirroot.'/user/profile/lib.php'); | |
0a5dffcc | 10 | require_once('uploaduser_form.php'); |
1ae083e4 | 11 | |
e4e38544 | 12 | $iid = optional_param('iid', '', PARAM_INT); |
df7ecfe4 | 13 | $previewrows = optional_param('previewrows', 10, PARAM_INT); |
e4e38544 | 14 | $readcount = optional_param('readcount', 0, PARAM_INT); |
3fe2cfb5 | 15 | $uploadtype = optional_param('uutype', 0, PARAM_INT); |
df7ecfe4 | 16 | |
e4e38544 | 17 | define('UU_ADDNEW', 0); |
18 | define('UU_ADDINC', 1); | |
19 | define('UU_ADD_UPDATE', 2); | |
20 | define('UU_UPDATE', 3); | |
066bfbfe | 21 | |
3fe2cfb5 RW |
22 | $choices = array(UU_ADDNEW => get_string('uuoptype_addnew', 'admin'), |
23 | UU_ADDINC => get_string('uuoptype_addinc', 'admin'), | |
24 | UU_ADD_UPDATE => get_string('uuoptype_addupdate', 'admin'), | |
25 | UU_UPDATE => get_string('uuoptype_update', 'admin')); | |
26 | ||
df7ecfe4 | 27 | @set_time_limit(3600); // 1 hour should be enough |
28 | @raise_memory_limit('256M'); | |
29 | if (function_exists('apache_child_terminate')) { | |
e4e38544 | 30 | // if we are running from Apache, give httpd a hint that |
31 | // it can recycle the process after it's done. Apache's | |
df7ecfe4 | 32 | // memory management is truly awful but we can help it. |
33 | @apache_child_terminate(); | |
34 | } | |
0a6150ca | 35 | |
ebff4779 | 36 | require_login(); |
066bfbfe | 37 | admin_externalpage_setup('uploadusers'); |
1ae083e4 | 38 | require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM)); |
0a6150ca | 39 | |
1c0ccfd6 | 40 | $textlib = textlib_get_instance(); |
e4e38544 | 41 | $systemcontext = get_context_instance(CONTEXT_SYSTEM); |
42 | ||
43 | $struserrenamed = get_string('userrenamed', 'admin'); | |
44 | $strusernotrenamedexists = get_string('usernotrenamedexists', 'error'); | |
45 | $strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error'); | |
46 | $strusernotrenamedoff = get_string('usernotrenamedoff', 'error'); | |
47 | $strusernotrenamedadmin = get_string('usernotrenamedadmin', 'error'); | |
48 | ||
49 | $struserupdated = get_string('useraccountupdated', 'admin'); | |
50 | $strusernotupdated = get_string('usernotupdatederror', 'error'); | |
51 | $strusernotupdatednotexists = get_string('usernotupdatednotexists', 'error'); | |
52 | $strusernotupdatedadmin = get_string('usernotupdatedadmin', 'error'); | |
53 | ||
54 | $struseradded = get_string('newuser'); | |
55 | $strusernotadded = get_string('usernotaddedregistered', 'error'); | |
56 | $strusernotaddederror = get_string('usernotaddederror', 'error'); | |
57 | ||
58 | $struserdeleted = get_string('userdeleted', 'admin'); | |
59 | $strusernotdeletederror = get_string('usernotdeletederror', 'error'); | |
60 | $strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error'); | |
61 | $strusernotdeletedoff = get_string('usernotdeletedoff', 'error'); | |
62 | $strusernotdeletedadmin = get_string('usernotdeletedadmin', 'error'); | |
63 | ||
64 | $strcannotassignrole = get_string('cannotassignrole', 'error'); | |
65 | $strduplicateusername = get_string('duplicateusername', 'error'); | |
66 | ||
67 | $struserauthunsupported = get_string('userauthunsupported', 'error'); | |
3fe2cfb5 | 68 | $stremailduplicate = get_string('useremailduplicate', 'error'); |
e4e38544 | 69 | |
70 | $errorstr = get_string('error'); | |
ca23a9c9 | 71 | |
e4e38544 | 72 | $returnurl = $CFG->wwwroot.'/'.$CFG->admin.'/uploaduser.php'; |
73 | $bulknurl = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php'; | |
74 | ||
2a6dcb72 PS |
75 | $today = time(); |
76 | $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); | |
77 | ||
e4e38544 | 78 | // array of all valid fields for validation |
6dbcacee | 79 | $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email', |
80 | 'city', 'country', 'lang', 'auth', 'timezone', 'mailformat', | |
81 | 'maildisplay', 'maildigest', 'htmleditor', 'ajax', 'autosubscribe', | |
82 | 'mnethostid', 'institution', 'department', 'idnumber', 'skype', | |
83 | 'msn', 'aim', 'yahoo', 'icq', 'phone1', 'phone2', 'address', | |
8bdc9cac | 84 | 'url', 'description', 'descriptionformat', 'oldusername', 'emailstop', 'deleted', |
c79106c4 | 85 | 'password'); |
e4e38544 | 86 | |
87 | $PRF_FIELDS = array(); | |
88 | ||
1d8bf5f0 | 89 | if ($prof_fields = $DB->get_records('user_info_field')) { |
e4e38544 | 90 | foreach ($prof_fields as $prof_field) { |
91 | $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname; | |
92 | } | |
93 | unset($prof_fields); | |
94 | } | |
95 | ||
96 | if (empty($iid)) { | |
df7ecfe4 | 97 | $mform = new admin_uploaduser_form1(); |
0a6150ca | 98 | |
df7ecfe4 | 99 | if ($formdata = $mform->get_data()) { |
e4e38544 | 100 | $iid = csv_import_reader::get_new_iid('uploaduser'); |
101 | $cir = new csv_import_reader($iid, 'uploaduser'); | |
df7ecfe4 | 102 | |
e4e38544 | 103 | $content = $mform->get_file_content('userfile'); |
3fe2cfb5 | 104 | $optype = $formdata->uutype; |
e4e38544 | 105 | $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name, 'validate_user_upload_columns'); |
106 | unset($content); | |
df7ecfe4 | 107 | |
e4e38544 | 108 | if ($readcount === false) { |
bd8ee7c1 | 109 | //TODO: need more detailed error info |
110 | print_error('csvloaderror', '', $returnurl); | |
e4e38544 | 111 | } else if ($readcount == 0) { |
5a2a5331 | 112 | print_error('csvemptyfile', 'error', $returnurl); |
ed22a01b | 113 | } |
e4e38544 | 114 | // continue to form2 |
df7ecfe4 | 115 | |
116 | } else { | |
61ef8f9f | 117 | echo $OUTPUT->header(); |
9e492db0 | 118 | |
6c7a5df7 | 119 | echo $OUTPUT->heading_with_help(get_string('uploadusers', 'admin'), 'uploadusers', 'admin'); |
9e492db0 | 120 | |
df7ecfe4 | 121 | $mform->display(); |
73d6f52f | 122 | echo $OUTPUT->footer(); |
df7ecfe4 | 123 | die; |
124 | } | |
e4e38544 | 125 | } else { |
126 | $cir = new csv_import_reader($iid, 'uploaduser'); | |
df7ecfe4 | 127 | } |
128 | ||
e4e38544 | 129 | if (!$columns = $cir->get_columns()) { |
8e9d88f2 | 130 | print_error('cannotreadtmpfile', 'error', $returnurl); |
e4e38544 | 131 | } |
132 | $mform = new admin_uploaduser_form2(null, $columns); | |
133 | // get initial date from form1 | |
3fe2cfb5 | 134 | $mform->set_data(array('iid'=>$iid, 'previewrows'=>$previewrows, 'readcount'=>$readcount, 'uutypelabel'=>$choices[$uploadtype], 'uutype'=>$uploadtype)); |
df7ecfe4 | 135 | |
136 | // If a file has been uploaded, then process it | |
137 | if ($formdata = $mform->is_cancelled()) { | |
e4e38544 | 138 | $cir->cleanup(true); |
139 | redirect($returnurl); | |
df7ecfe4 | 140 | |
294ce987 | 141 | } else if ($formdata = $mform->get_data()) { |
df7ecfe4 | 142 | // Print the header |
61ef8f9f | 143 | echo $OUTPUT->header(); |
2fff8846 | 144 | echo $OUTPUT->heading(get_string('uploadusersresult', 'admin')); |
e4e38544 | 145 | |
146 | $optype = $formdata->uutype; | |
147 | ||
0c4807ab | 148 | $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_UPDATE); |
149 | $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_ADDNEW and $optype != UU_ADDINC); | |
150 | $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_ADDNEW and $optype != UU_ADDINC); | |
151 | $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_ADDNEW and $optype != UU_ADDINC); | |
152 | $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0; | |
153 | $bulk = $formdata->uubulk; | |
154 | $noemailduplicates = $formdata->uunoemailduplicates; | |
e4e38544 | 155 | |
156 | // verification moved to two places: after upload and into form2 | |
157 | $usersnew = 0; | |
158 | $usersupdated = 0; | |
159 | $userserrors = 0; | |
160 | $deletes = 0; | |
161 | $deleteerrors = 0; | |
162 | $renames = 0; | |
163 | $renameerrors = 0; | |
164 | $usersskipped = 0; | |
d6bb2d2f | 165 | $weakpasswords = 0; |
feecd4d6 | 166 | |
e4e38544 | 167 | // caches |
df997f84 PS |
168 | $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway! |
169 | $rolecache = uu_allowed_roles_cache(); // roles lookup cache | |
48265158 | 170 | $manualcache = array(); // cache of used manual enrol plugins in each course |
e4e38544 | 171 | |
172 | $allowedauths = uu_allowed_auths(); | |
173 | $allowedauths = array_keys($allowedauths); | |
17da2e6f | 174 | $availableauths = get_plugin_list('auth'); |
175 | $availableauths = array_keys($availableauths); | |
e4e38544 | 176 | |
df997f84 PS |
177 | // we use only manual enrol plugin here, if it is disabled no enrol is done |
178 | if (enrol_is_enabled('manual')) { | |
179 | $manual = enrol_get_plugin('manual'); | |
180 | } else { | |
181 | $manual = NULL; | |
e4e38544 | 182 | } |
df7ecfe4 | 183 | |
b4bd91ce | 184 | // clear bulk selection |
e4e38544 | 185 | if ($bulk) { |
cd1edf9e | 186 | $SESSION->bulk_users = array(); |
e4e38544 | 187 | } |
df7ecfe4 | 188 | |
e4e38544 | 189 | // init csv import helper |
190 | $cir->init(); | |
191 | $linenum = 1; //column header is first line | |
df7ecfe4 | 192 | |
e4e38544 | 193 | // init upload progress tracker |
194 | $upt = new uu_progress_tracker(); | |
195 | $upt->init(); // start table | |
a2ce7344 | 196 | |
e4e38544 | 197 | while ($line = $cir->next()) { |
198 | $upt->flush(); | |
199 | $linenum++; | |
200 | ||
201 | $upt->track('line', $linenum); | |
202 | ||
d6bb2d2f PS |
203 | $forcechangepassword = false; |
204 | ||
a226a972 | 205 | $user = new stdClass(); |
e4e38544 | 206 | // by default, use the local mnet id (this may be changed in the file) |
207 | $user->mnethostid = $CFG->mnet_localhost_id; | |
208 | // add fields to user object | |
209 | foreach ($line as $key => $value) { | |
210 | if ($value !== '') { | |
df997f84 | 211 | $key = $columns[$key]; |
e4e38544 | 212 | // password is special field |
213 | if ($key == 'password') { | |
214 | if ($value !== '') { | |
215 | $user->password = hash_internal_user_password($value); | |
feecd4d6 | 216 | if (!empty($CFG->passwordpolicy) and !check_password_policy($value, $errmsg)) { |
d6bb2d2f PS |
217 | $forcechangepassword = true; |
218 | $weakpasswords++; | |
feecd4d6 | 219 | } |
e4e38544 | 220 | } |
221 | } else { | |
222 | $user->$key = $value; | |
223 | if (in_array($key, $upt->columns)) { | |
224 | $upt->track($key, $value); | |
225 | } | |
226 | } | |
a7db355a | 227 | } |
228 | } | |
e4e38544 | 229 | |
230 | // get username, first/last name now - we need them in templates!! | |
231 | if ($optype == UU_UPDATE) { | |
232 | // when updating only username is required | |
233 | if (!isset($user->username)) { | |
234 | $upt->track('status', get_string('missingfield', 'error', 'username'), 'error'); | |
235 | $upt->track('username', $errorstr, 'error'); | |
236 | $userserrors++; | |
237 | continue; | |
238 | } | |
239 | ||
240 | } else { | |
241 | $error = false; | |
242 | // when all other ops need firstname and lastname | |
243 | if (!isset($user->firstname) or $user->firstname === '') { | |
244 | $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error'); | |
245 | $upt->track('firstname', $errorstr, 'error'); | |
246 | $error = true; | |
247 | } | |
248 | if (!isset($user->lastname) or $user->lastname === '') { | |
249 | $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error'); | |
250 | $upt->track('lastname', $errorstr, 'error'); | |
251 | $error = true; | |
252 | } | |
253 | if ($error) { | |
254 | $userserrors++; | |
255 | continue; | |
256 | } | |
257 | // we require username too - we might use template for it though | |
258 | if (!isset($user->username)) { | |
259 | if (!isset($formdata->username) or $formdata->username === '') { | |
260 | $upt->track('status', get_string('missingfield', 'error', 'username'), 'error'); | |
261 | $upt->track('username', $errorstr, 'error'); | |
262 | $userserrors++; | |
263 | continue; | |
264 | } else { | |
265 | $user->username = process_template($formdata->username, $user); | |
266 | $upt->track('username', $user->username); | |
267 | } | |
268 | } | |
a7db355a | 269 | } |
e4e38544 | 270 | |
271 | // normalize username | |
07ed083e RW |
272 | $user->username = clean_param($user->username, PARAM_USERNAME); |
273 | ||
e4e38544 | 274 | if (empty($user->username)) { |
275 | $upt->track('status', get_string('missingfield', 'error', 'username'), 'error'); | |
276 | $upt->track('username', $errorstr, 'error'); | |
277 | $userserrors++; | |
278 | continue; | |
279 | } | |
280 | ||
1d8bf5f0 | 281 | if ($existinguser = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid))) { |
e4e38544 | 282 | $upt->track('id', $existinguser->id, 'normal', false); |
283 | } | |
284 | ||
285 | // find out in username incrementing required | |
286 | if ($existinguser and $optype == UU_ADDINC) { | |
287 | $oldusername = $user->username; | |
288 | $user->username = increment_username($user->username, $user->mnethostid); | |
289 | $upt->track('username', '', 'normal', false); // clear previous | |
290 | $upt->track('username', $oldusername.'-->'.$user->username, 'info'); | |
291 | $existinguser = false; | |
292 | } | |
293 | ||
294 | // add default values for remaining fields | |
295 | foreach ($STD_FIELDS as $field) { | |
296 | if (isset($user->$field)) { | |
297 | continue; | |
298 | } | |
299 | // all validation moved to form2 | |
300 | if (isset($formdata->$field)) { | |
301 | // process templates | |
302 | $user->$field = process_template($formdata->$field, $user); | |
303 | } | |
304 | } | |
305 | foreach ($PRF_FIELDS as $field) { | |
306 | if (isset($user->$field)) { | |
307 | continue; | |
308 | } | |
309 | if (isset($formdata->$field)) { | |
310 | // process templates | |
311 | $user->$field = process_template($formdata->$field, $user); | |
312 | } | |
313 | } | |
314 | ||
315 | // delete user | |
316 | if (!empty($user->deleted)) { | |
317 | if (!$allowdeletes) { | |
318 | $usersskipped++; | |
319 | $upt->track('status', $strusernotdeletedoff, 'warning'); | |
320 | continue; | |
321 | } | |
322 | if ($existinguser) { | |
4f0c2d00 | 323 | if (is_siteadmin($existinguser->id)) { |
e4e38544 | 324 | $upt->track('status', $strusernotdeletedadmin, 'error'); |
325 | $deleteerrors++; | |
326 | continue; | |
327 | } | |
328 | if (delete_user($existinguser)) { | |
329 | $upt->track('status', $struserdeleted); | |
330 | $deletes++; | |
331 | } else { | |
332 | $upt->track('status', $strusernotdeletederror, 'error'); | |
333 | $deleteerrors++; | |
6b09974b | 334 | } |
e4e38544 | 335 | } else { |
336 | $upt->track('status', $strusernotdeletedmissing, 'error'); | |
337 | $deleteerrors++; | |
338 | } | |
339 | continue; | |
340 | } | |
341 | // we do not need the deleted flag anymore | |
342 | unset($user->deleted); | |
343 | ||
344 | // renaming requested? | |
345 | if (!empty($user->oldusername) ) { | |
346 | $oldusername = $textlib->strtolower($user->oldusername); | |
347 | if (!$allowrenames) { | |
348 | $usersskipped++; | |
349 | $upt->track('status', $strusernotrenamedoff, 'warning'); | |
350 | continue; | |
351 | } | |
352 | ||
353 | if ($existinguser) { | |
354 | $upt->track('status', $strusernotrenamedexists, 'error'); | |
355 | $renameerrors++; | |
356 | continue; | |
16a1fed4 | 357 | } |
ed22a01b | 358 | |
1d8bf5f0 | 359 | if ($olduser = $DB->get_record('user', array('username'=>$oldusername, 'mnethostid'=>$user->mnethostid))) { |
e4e38544 | 360 | $upt->track('id', $olduser->id, 'normal', false); |
4f0c2d00 | 361 | if (is_siteadmin($olduser->id)) { |
e4e38544 | 362 | $upt->track('status', $strusernotrenamedadmin, 'error'); |
363 | $renameerrors++; | |
a7db355a | 364 | continue; |
0063abee | 365 | } |
df997f84 PS |
366 | $DB->set_field('user', 'username', $user->username, array('id'=>$olduser->id)); |
367 | $upt->track('username', '', 'normal', false); // clear previous | |
368 | $upt->track('username', $oldusername.'-->'.$user->username, 'info'); | |
369 | $upt->track('status', $struserrenamed); | |
370 | $renames++; | |
e4e38544 | 371 | } else { |
372 | $upt->track('status', $strusernotrenamedmissing, 'error'); | |
373 | $renameerrors++; | |
374 | continue; | |
375 | } | |
376 | $existinguser = $olduser; | |
377 | $existinguser->username = $user->username; | |
378 | } | |
379 | ||
380 | // can we process with update or insert? | |
381 | $skip = false; | |
382 | switch ($optype) { | |
383 | case UU_ADDNEW: | |
384 | if ($existinguser) { | |
385 | $usersskipped++; | |
386 | $upt->track('status', $strusernotadded, 'warning'); | |
3fe2cfb5 | 387 | $skip = true; |
e4e38544 | 388 | } |
389 | break; | |
390 | ||
391 | case UU_ADDINC: | |
392 | if ($existinguser) { | |
393 | //this should not happen! | |
394 | $upt->track('status', $strusernotaddederror, 'error'); | |
395 | $userserrors++; | |
396 | continue; | |
397 | } | |
398 | break; | |
399 | ||
400 | case UU_ADD_UPDATE: | |
401 | break; | |
402 | ||
403 | case UU_UPDATE: | |
404 | if (!$existinguser) { | |
405 | $usersskipped++; | |
406 | $upt->track('status', $strusernotupdatednotexists, 'warning'); | |
407 | $skip = true; | |
408 | } | |
409 | break; | |
410 | } | |
411 | ||
412 | if ($skip) { | |
413 | continue; | |
414 | } | |
415 | ||
416 | if ($existinguser) { | |
417 | $user->id = $existinguser->id; | |
418 | ||
4f0c2d00 | 419 | if (is_siteadmin($user->id)) { |
e4e38544 | 420 | $upt->track('status', $strusernotupdatedadmin, 'error'); |
421 | $userserrors++; | |
422 | continue; | |
423 | } | |
424 | ||
425 | if (!$updatetype) { | |
426 | // no updates of existing data at all | |
427 | } else { | |
428 | $existinguser->timemodified = time(); | |
3fe2cfb5 RW |
429 | if (empty($existinguser->timecreated)) { |
430 | if (empty($existinguser->firstaccess)) { | |
431 | $existinguser->timecreated = time(); | |
432 | } else { | |
433 | $existinguser->timecreated = $existinguser->firstaccess; | |
434 | } | |
435 | } | |
436 | ||
e4e38544 | 437 | //load existing profile data |
438 | profile_load_data($existinguser); | |
439 | ||
440 | $allowed = array(); | |
441 | if ($updatetype == 1) { | |
442 | $allowed = $columns; | |
443 | } else if ($updatetype == 2 or $updatetype == 3) { | |
444 | $allowed = array_merge($STD_FIELDS, $PRF_FIELDS); | |
445 | } | |
446 | foreach ($allowed as $column) { | |
447 | if ($column == 'username') { | |
448 | continue; | |
449 | } | |
450 | if ($column == 'password') { | |
451 | if (!$updatepasswords or $updatetype == 3) { | |
a7db355a | 452 | continue; |
e4e38544 | 453 | } else if (!empty($user->password)) { |
454 | $upt->track('password', get_string('updated')); | |
feecd4d6 | 455 | if ($forcechangepassword) { |
456 | set_user_preference('auth_forcepasswordchange', 1, $existinguser->id); | |
457 | } | |
a7db355a | 458 | } |
e4e38544 | 459 | } |
4cc977a6 | 460 | if ((property_exists($existinguser, $column) and property_exists($user, $column)) or in_array($column, $PRF_FIELDS)) { |
e4e38544 | 461 | if ($updatetype == 3 and $existinguser->$column !== '') { |
462 | //missing == non-empty only | |
463 | continue; | |
a7db355a | 464 | } |
e4e38544 | 465 | if ($existinguser->$column !== $user->$column) { |
0c4807ab | 466 | if ($column == 'email') { |
1d8bf5f0 | 467 | if ($DB->record_exists('user', array('email'=>$user->email))) { |
0c4807ab | 468 | if ($noemailduplicates) { |
469 | $upt->track('email', $stremailduplicate, 'error'); | |
470 | $upt->track('status', $strusernotupdated, 'error'); | |
471 | $userserrors++; | |
472 | continue 2; | |
473 | } else { | |
474 | $upt->track('email', $stremailduplicate, 'warning'); | |
475 | } | |
476 | } | |
477 | } | |
e4e38544 | 478 | if ($column != 'password' and in_array($column, $upt->columns)) { |
479 | $upt->track($column, '', 'normal', false); // clear previous | |
480 | $upt->track($column, $existinguser->$column.'-->'.$user->$column, 'info'); | |
481 | } | |
482 | $existinguser->$column = $user->$column; | |
a7db355a | 483 | } |
6b09974b | 484 | } |
a7db355a | 485 | } |
ed22a01b | 486 | |
eab8ed9f | 487 | // do not update record if new auth plugin does not exist! |
e4e38544 | 488 | if (!in_array($existinguser->auth, $availableauths)) { |
489 | $upt->track('auth', get_string('userautherror', 'error', $existinguser->auth), 'error'); | |
490 | $upt->track('status', $strusernotupdated, 'error'); | |
491 | $userserrors++; | |
492 | continue; | |
493 | } else if (!in_array($existinguser->auth, $allowedauths)) { | |
494 | $upt->track('auth', $struserauthunsupported, 'warning'); | |
066bfbfe | 495 | } |
a7db355a | 496 | |
df997f84 PS |
497 | $DB->update_record('user', $existinguser); |
498 | $upt->track('status', $struserupdated); | |
499 | $usersupdated++; | |
e4e38544 | 500 | // save custom profile fields data from csv file |
5d910388 | 501 | profile_save_data($existinguser); |
e6f74ba3 PS |
502 | |
503 | events_trigger('user_updated', $existinguser); | |
e4e38544 | 504 | } |
505 | ||
506 | if ($bulk == 2 or $bulk == 3) { | |
cd1edf9e | 507 | if (!in_array($user->id, $SESSION->bulk_users)) { |
508 | $SESSION->bulk_users[] = $user->id; | |
ed22a01b | 509 | } |
066bfbfe | 510 | } |
ed22a01b | 511 | |
e4e38544 | 512 | } else { |
a7db355a | 513 | // save the user to the database |
514 | $user->confirmed = 1; | |
515 | $user->timemodified = time(); | |
3fe2cfb5 | 516 | $user->timecreated = time(); |
066bfbfe | 517 | |
e4e38544 | 518 | if (!$createpasswords and empty($user->password)) { |
519 | $upt->track('password', get_string('missingfield', 'error', 'password'), 'error'); | |
520 | $upt->track('status', $strusernotaddederror, 'error'); | |
521 | $userserrors++; | |
522 | continue; | |
523 | } | |
524 | ||
525 | // do not insert record if new auth plguin does not exist! | |
526 | if (isset($user->auth)) { | |
527 | if (!in_array($user->auth, $availableauths)) { | |
528 | $upt->track('auth', get_string('userautherror', 'error', $user->auth), 'error'); | |
529 | $upt->track('status', $strusernotaddederror, 'error'); | |
530 | $userserrors++; | |
066bfbfe | 531 | continue; |
e4e38544 | 532 | } else if (!in_array($user->auth, $allowedauths)) { |
533 | $upt->track('auth', $struserauthunsupported, 'warning'); | |
066bfbfe | 534 | } |
066bfbfe | 535 | } |
a2ce7344 | 536 | |
1d8bf5f0 | 537 | if ($DB->record_exists('user', array('email'=>$user->email))) { |
0c4807ab | 538 | if ($noemailduplicates) { |
539 | $upt->track('email', $stremailduplicate, 'error'); | |
540 | $upt->track('status', $strusernotaddederror, 'error'); | |
541 | $userserrors++; | |
542 | continue; | |
543 | } else { | |
544 | $upt->track('email', $stremailduplicate, 'warning'); | |
545 | } | |
546 | } | |
547 | ||
df997f84 PS |
548 | $user->id = $DB->insert_record('user', $user); |
549 | $info = ': ' . $user->username .' (ID = ' . $user->id . ')'; | |
550 | $upt->track('status', $struseradded); | |
551 | $upt->track('id', $user->id, 'normal', false); | |
552 | $usersnew++; | |
553 | if ($createpasswords and empty($user->password)) { | |
554 | // passwords will be created and sent out on cron | |
555 | set_user_preference('create_password', 1, $user->id); | |
556 | set_user_preference('auth_forcepasswordchange', 1, $user->id); | |
557 | $upt->track('password', get_string('new')); | |
e4e38544 | 558 | } |
df997f84 PS |
559 | if ($forcechangepassword) { |
560 | set_user_preference('auth_forcepasswordchange', 1, $user->id); | |
561 | } | |
562 | ||
e4e38544 | 563 | // save custom profile fields data |
564 | profile_save_data($user); | |
565 | ||
2304f629 | 566 | // make sure user context exists |
a5feb176 | 567 | get_context_instance(CONTEXT_USER, $user->id); |
2304f629 | 568 | |
e6f74ba3 PS |
569 | events_trigger('user_created', $user); |
570 | ||
e4e38544 | 571 | if ($bulk == 1 or $bulk == 3) { |
cd1edf9e | 572 | if (!in_array($user->id, $SESSION->bulk_users)) { |
573 | $SESSION->bulk_users[] = $user->id; | |
066bfbfe | 574 | } |
066bfbfe | 575 | } |
e4e38544 | 576 | } |
577 | ||
2686b6c3 | 578 | // find course enrolments, groups, roles/types and enrol periods |
e4e38544 | 579 | foreach ($columns as $column) { |
580 | if (!preg_match('/^course\d+$/', $column)) { | |
581 | continue; | |
582 | } | |
583 | $i = substr($column, 6); | |
6b09974b | 584 | |
df997f84 PS |
585 | if (empty($user->{'course'.$i})) { |
586 | continue; | |
587 | } | |
e4e38544 | 588 | $shortname = $user->{'course'.$i}; |
589 | if (!array_key_exists($shortname, $ccache)) { | |
df997f84 | 590 | if (!$course = $DB->get_record('course', array('shortname'=>$shortname), 'id, shortname')) { |
e4e38544 | 591 | $upt->track('enrolments', get_string('unknowncourse', 'error', $shortname), 'error'); |
a7db355a | 592 | continue; |
066bfbfe | 593 | } |
e4e38544 | 594 | $ccache[$shortname] = $course; |
595 | $ccache[$shortname]->groups = null; | |
596 | } | |
597 | $courseid = $ccache[$shortname]->id; | |
598 | $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); | |
df997f84 PS |
599 | if (!isset($manualcache[$courseid])) { |
600 | if ($instances = enrol_get_instances($courseid, false)) { | |
601 | $manualcache[$courseid] = reset($instances); | |
a7db355a | 602 | } else { |
df997f84 | 603 | $manualcache[$courseid] = false; |
e4e38544 | 604 | } |
df997f84 | 605 | } |
e4e38544 | 606 | |
df997f84 PS |
607 | if ($manual and $manualcache[$courseid]) { |
608 | ||
609 | // find role | |
610 | $rid = false; | |
611 | if (!empty($user->{'role'.$i})) { | |
612 | $addrole = $user->{'role'.$i}; | |
613 | if (array_key_exists($addrole, $rolecache)) { | |
614 | $rid = $rolecache[$addrole]->id; | |
e4e38544 | 615 | } else { |
df997f84 PS |
616 | $upt->track('enrolments', get_string('unknownrole', 'error', $addrole), 'error'); |
617 | continue; | |
a7db355a | 618 | } |
e4e38544 | 619 | |
df997f84 PS |
620 | } else if (!empty($user->{'type'.$i})) { |
621 | // if no role, then find "old" enrolment type | |
622 | $addtype = $user->{'type'.$i}; | |
623 | if ($addtype < 1 or $addtype > 3) { | |
624 | $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error'); | |
625 | continue; | |
626 | } else if (empty($formdata->{'uulegacy'.$addtype})) { | |
627 | continue; | |
628 | } else { | |
629 | $rid = $formdata->{'uulegacy'.$addtype}; | |
630 | } | |
e4e38544 | 631 | } else { |
df997f84 PS |
632 | // no role specified, use the default from manual enrol plugin |
633 | $rid = $manualcache[$courseid]->roleid; | |
a7db355a | 634 | } |
2686b6c3 | 635 | |
df997f84 PS |
636 | if ($rid) { |
637 | // find duration | |
df997f84 PS |
638 | $timeend = 0; |
639 | if (!empty($user->{'enrolperiod'.$i})) { | |
640 | $duration = (int)$user->{'enrolperiod'.$i} * 86400; // convert days to seconds | |
641 | if ($duration > 0) { // sanity check | |
2a6dcb72 | 642 | $timeend = $today + $duration; |
df997f84 PS |
643 | } |
644 | } | |
2686b6c3 | 645 | |
2a6dcb72 | 646 | $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $today, $timeend, true); |
df997f84 | 647 | |
a226a972 | 648 | $a = new stdClass(); |
df997f84 PS |
649 | $a->course = $shortname; |
650 | $a->role = $rolecache[$rid]->name; | |
651 | $upt->track('enrolments', get_string('enrolledincourserole', 'enrol_manual', $a)); | |
066bfbfe | 652 | } |
e4e38544 | 653 | } |
6b09974b | 654 | |
e4e38544 | 655 | // find group to add to |
656 | if (!empty($user->{'group'.$i})) { | |
657 | // make sure user is enrolled into course before adding into groups | |
4f0c2d00 | 658 | if (!is_enrolled($coursecontext, $user->id)) { |
d85f2634 | 659 | $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $user->{'group'.$i}), 'error'); |
e4e38544 | 660 | continue; |
661 | } | |
662 | //build group cache | |
663 | if (is_null($ccache[$shortname]->groups)) { | |
664 | $ccache[$shortname]->groups = array(); | |
35987665 | 665 | if ($groups = groups_get_all_groups($courseid)) { |
e4e38544 | 666 | foreach ($groups as $gid=>$group) { |
a226a972 | 667 | $ccache[$shortname]->groups[$gid] = new stdClass(); |
e4e38544 | 668 | $ccache[$shortname]->groups[$gid]->id = $gid; |
669 | $ccache[$shortname]->groups[$gid]->name = $group->name; | |
670 | if (!is_numeric($group->name)) { // only non-numeric names are supported!!! | |
a226a972 | 671 | $ccache[$shortname]->groups[$group->name] = new stdClass(); |
e4e38544 | 672 | $ccache[$shortname]->groups[$group->name]->id = $gid; |
673 | $ccache[$shortname]->groups[$group->name]->name = $group->name; | |
a7db355a | 674 | } |
a5702569 | 675 | } |
0063abee | 676 | } |
6b09974b | 677 | } |
e4e38544 | 678 | // group exists? |
679 | $addgroup = $user->{'group'.$i}; | |
680 | if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) { | |
b225cf36 | 681 | // if group doesn't exist, create it |
a226a972 | 682 | $newgroupdata = new stdClass(); |
b225cf36 | 683 | $newgroupdata->name = $addgroup; |
684 | $newgroupdata->courseid = $ccache[$shortname]->id; | |
685 | if ($ccache[$shortname]->groups[$addgroup]->id = groups_create_group($newgroupdata)){ | |
686 | $ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name; | |
687 | } else { | |
688 | $upt->track('enrolments', get_string('unknowngroup', 'error', $addgroup), 'error'); | |
689 | continue; | |
690 | } | |
e4e38544 | 691 | } |
692 | $gid = $ccache[$shortname]->groups[$addgroup]->id; | |
693 | $gname = $ccache[$shortname]->groups[$addgroup]->name; | |
694 | ||
9c000a99 | 695 | try { |
696 | if (groups_add_member($gid, $user->id)) { | |
697 | $upt->track('enrolments', get_string('addedtogroup', '', $gname)); | |
df997f84 PS |
698 | } else { |
699 | $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error'); | |
9c000a99 | 700 | } |
701 | } catch (moodle_exception $e) { | |
e4e38544 | 702 | $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error'); |
703 | continue; | |
704 | } | |
a5702569 | 705 | } |
066bfbfe | 706 | } |
707 | } | |
e4e38544 | 708 | $upt->flush(); |
709 | $upt->close(); // close table | |
710 | ||
711 | $cir->close(); | |
712 | $cir->cleanup(true); | |
713 | ||
20486a5a | 714 | echo $OUTPUT->box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults'); |
e4e38544 | 715 | echo '<p>'; |
716 | if ($optype != UU_UPDATE) { | |
717 | echo get_string('userscreated', 'admin').': '.$usersnew.'<br />'; | |
718 | } | |
719 | if ($optype == UU_UPDATE or $optype == UU_ADD_UPDATE) { | |
720 | echo get_string('usersupdated', 'admin').': '.$usersupdated.'<br />'; | |
721 | } | |
722 | if ($allowdeletes) { | |
723 | echo get_string('usersdeleted', 'admin').': '.$deletes.'<br />'; | |
724 | echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'<br />'; | |
725 | } | |
726 | if ($allowrenames) { | |
727 | echo get_string('usersrenamed', 'admin').': '.$renames.'<br />'; | |
728 | echo get_string('renameerrors', 'admin').': '.$renameerrors.'<br />'; | |
729 | } | |
730 | if ($usersskipped) { | |
731 | echo get_string('usersskipped', 'admin').': '.$usersskipped.'<br />'; | |
732 | } | |
d6bb2d2f | 733 | echo get_string('usersweakpassword', 'admin').': '.$weakpasswords.'<br />'; |
e4e38544 | 734 | echo get_string('errors', 'admin').': '.$userserrors.'</p>'; |
20486a5a | 735 | echo $OUTPUT->box_end(); |
e4e38544 | 736 | |
737 | if ($bulk) { | |
8fbce1c8 | 738 | echo $OUTPUT->continue_button($bulknurl); |
e4e38544 | 739 | } else { |
8fbce1c8 | 740 | echo $OUTPUT->continue_button($returnurl); |
e4e38544 | 741 | } |
73d6f52f | 742 | echo $OUTPUT->footer(); |
df7ecfe4 | 743 | die; |
6b09974b | 744 | } |
0a6150ca | 745 | |
df7ecfe4 | 746 | // Print the header |
61ef8f9f | 747 | echo $OUTPUT->header(); |
df7ecfe4 | 748 | |
a5702569 | 749 | /// Print the form |
9e492db0 | 750 | |
6c7a5df7 | 751 | echo $OUTPUT->heading(get_string('uploaduserspreview', 'admin')); |
df7ecfe4 | 752 | |
e4e38544 | 753 | $cir->init(); |
07ed083e | 754 | |
3fe2cfb5 | 755 | $contents = array(); |
e4e38544 | 756 | while ($fields = $cir->next()) { |
3fe2cfb5 RW |
757 | $errormsg = array(); |
758 | $rowcols = array(); | |
759 | foreach($fields as $key =>$field) { | |
760 | $rowcols[$columns[$key]] = $field; | |
761 | } | |
762 | ||
763 | $usernameexist = $DB->record_exists('user', array('username'=>$rowcols['username'])); | |
764 | $emailexist = $DB->record_exists('user', array('email'=>$rowcols['email'])); | |
765 | $cleanusername = clean_param($rowcols['username'], PARAM_USERNAME); | |
766 | $validusername = strcmp($rowcols['username'], $cleanusername); | |
767 | $validemail = validate_email($rowcols['email']); | |
768 | ||
769 | if ($validusername != 0 || !$validemail) { | |
770 | if ($validusername != 0) { | |
771 | $errormsg['username'] = get_string('invalidusernameupload'); | |
772 | } | |
773 | if (!$validemail) { | |
774 | $errormsg['email'] = get_string('invalidemail'); | |
e4e38544 | 775 | } |
df7ecfe4 | 776 | } |
07ed083e | 777 | |
3fe2cfb5 RW |
778 | switch($optype) { |
779 | case UU_ADDNEW: | |
780 | if ($usernameexist || $emailexist ) { | |
781 | $rowcols['action'] = 'skipped'; | |
782 | } else { | |
783 | $rowcols['action'] = 'create'; | |
07ed083e | 784 | } |
3fe2cfb5 RW |
785 | break; |
786 | ||
787 | case UU_ADDINC: | |
788 | if (!$usernameexist && !$emailexist) { | |
789 | $rowcols['action'] = 'create'; | |
790 | } else if ($usernameexist && !$emailexist) { | |
791 | $rowcols['action'] = 'addcountertousername'; | |
792 | $rowcols['username'] = increment_username($rowcols['username'], $CFG->mnet_localhost_id); | |
793 | } else { | |
794 | $rowcols['action'] = 'skipped'; | |
795 | } | |
796 | break; | |
797 | ||
798 | case UU_ADD_UPDATE: | |
799 | $oldusernameexist = ''; | |
800 | if (isset($rowcols['oldusername'])) { | |
801 | $oldusernameexist = $DB->record_exists('user', array('username'=>$rowcols['oldusername'])); | |
802 | } | |
803 | if ($usernameexist || $emailexist || $oldusernameexist ) { | |
804 | $rowcols['action'] = 'update'; | |
805 | } else { | |
806 | $rowcols['action'] = 'create'; | |
807 | } | |
808 | break; | |
809 | ||
810 | case UU_UPDATE: | |
811 | $oldusernameexist = ''; | |
812 | if (isset($rowcols['oldusername'])) { | |
813 | $oldusernameexist = $DB->record_exists('user', array('username'=>$rowcols['oldusername'])); | |
814 | } | |
815 | ||
816 | if ($usernameexist || $emailexist || !empty($oldusernameexist)) { | |
817 | $rowcols['action'] = 'update'; | |
818 | } else { | |
819 | $rowcols['action'] = "skipped"; | |
820 | } | |
821 | break; | |
df7ecfe4 | 822 | } |
df7ecfe4 | 823 | |
3fe2cfb5 RW |
824 | if (!empty($errormsg)){ |
825 | $rowcols['error'] = array(); | |
826 | $rowcols['error'] = $errormsg; | |
827 | } | |
828 | if ($rowcols['action'] != 'skipped') { | |
829 | $contents[] = $rowcols; | |
07ed083e RW |
830 | } |
831 | } | |
3fe2cfb5 | 832 | $cir->close(); |
07ed083e | 833 | |
3fe2cfb5 RW |
834 | //get heading |
835 | $headings = array(); | |
836 | foreach ($contents as $content) { | |
837 | foreach($content as $key => $value) { | |
838 | if (!in_array($key, $headings)) { | |
839 | $headings[] = $key; | |
840 | } | |
841 | } | |
07ed083e | 842 | } |
3fe2cfb5 RW |
843 | |
844 | $table = new html_table(); | |
845 | $table->id = "uupreview"; | |
16be8974 | 846 | $table->attributes['class'] = 'generaltable'; |
3fe2cfb5 RW |
847 | $table->tablealign = 'center'; |
848 | $table->summary = get_string('uploaduserspreview', 'admin'); | |
849 | $table->head = array(); | |
850 | $table->data = array(); | |
851 | ||
852 | //print heading | |
853 | foreach ($headings as $heading) { | |
854 | $table->head[] = s($heading); | |
07ed083e | 855 | } |
07ed083e | 856 | |
3fe2cfb5 | 857 | $haserror = false; |
07ed083e | 858 | $countcontent = 0; |
3fe2cfb5 RW |
859 | if (in_array('error', $headings)) { |
860 | //print error | |
861 | $haserror = true; | |
862 | ||
863 | foreach ($contents as $content) { | |
864 | if (array_key_exists('error', $content)) { | |
865 | $rows = new html_table_row(); | |
866 | foreach ($content as $key => $value) { | |
867 | $cells = new html_table_cell(); | |
868 | $errclass = ''; | |
869 | if (array_key_exists($key, $content['error'])) { | |
870 | $errclass = 'uuerror'; | |
871 | } | |
872 | if ($key == 'error') { | |
873 | $value = join('<br />', $content['error']); | |
874 | } | |
875 | if ($key == 'action') { | |
876 | $value = get_string($content[$key]); | |
877 | } | |
878 | $cells->text = $value; | |
16be8974 | 879 | $cells->attributes['class'] = $errclass; |
3fe2cfb5 RW |
880 | $rows->cells[] = $cells; |
881 | } | |
882 | $countcontent++; | |
883 | $table->data[] = $rows; | |
884 | } | |
885 | } | |
886 | $mform = new admin_uploaduser_form3(); | |
887 | $mform->set_data(array('uutype'=>$uploadtype)); | |
888 | } else if (empty($contents)) { | |
889 | $mform = new admin_uploaduser_form3(); | |
890 | $mform->set_data(array('uutype'=>$uploadtype)); | |
07ed083e | 891 | } else { |
3fe2cfb5 RW |
892 | //print content |
893 | foreach ($contents as $content) { | |
894 | $rows = new html_table_row(); | |
895 | if ($countcontent >= $previewrows) { | |
896 | foreach ($content as $con) { | |
897 | $cells = new html_table_cell(); | |
898 | $cells->text = '...'; | |
899 | } | |
900 | $rows->cells[] = $cells; | |
901 | $table->data[] = $rows; | |
902 | break; | |
903 | } | |
904 | foreach ($headings as $heading) { | |
905 | $cells = new html_table_cell(); | |
906 | if(array_key_exists($heading, $content)) { | |
907 | if ($heading == 'action') { | |
908 | $content[$heading] = get_string($content[$heading]); | |
909 | } | |
910 | $cells->text = $content[$heading]; | |
911 | } else { | |
912 | $cells->text = ''; | |
913 | } | |
914 | $rows->cells[] = $cells; | |
915 | } | |
916 | $table->data[] = $rows; | |
917 | $countcontent++; | |
918 | } | |
07ed083e | 919 | } |
16be8974 | 920 | echo html_writer::table($table); |
3fe2cfb5 RW |
921 | |
922 | if ($haserror) { | |
07ed083e | 923 | |
3fe2cfb5 RW |
924 | echo $OUTPUT->container(get_string('useruploadtype', 'moodle', $choices[$uploadtype]), 'centerpara'); |
925 | echo $OUTPUT->container(get_string('uploadinvalidpreprocessedcount', 'moodle', $countcontent), 'centerpara'); | |
926 | echo $OUTPUT->container(get_string('invalidusername', 'moodle'), 'centerpara'); | |
927 | echo $OUTPUT->container(get_string('uploadfilecontainerror', 'moodle'), 'centerpara'); | |
928 | } else if (empty($contents)) { | |
929 | echo $OUTPUT->container(get_string('uupreprocessedcount', 'admin', $countcontent), 'centerpara'); | |
930 | echo $OUTPUT->container(get_string('uploadfilecontentsnovaliddata'), 'centerpara'); | |
07ed083e | 931 | } else { |
3fe2cfb5 | 932 | echo $OUTPUT->container(get_string('uupreprocessedcount', 'admin', $countcontent), 'centerpara'); |
07ed083e RW |
933 | } |
934 | ||
0a5dffcc | 935 | $mform->display(); |
73d6f52f | 936 | echo $OUTPUT->footer(); |
df7ecfe4 | 937 | die; |
938 | ||
e4e38544 | 939 | ///////////////////////////////////// |
940 | /// Utility functions and classes /// | |
941 | ///////////////////////////////////// | |
942 | ||
943 | class uu_progress_tracker { | |
944 | var $_row; | |
945 | var $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'deleted'); | |
946 | ||
947 | function uu_progress_tracker() { | |
948 | } | |
949 | ||
950 | function init() { | |
951 | $ci = 0; | |
952 | echo '<table id="uuresults" class="generaltable boxaligncenter" summary="'.get_string('uploadusersresult', 'admin').'">'; | |
953 | echo '<tr class="heading r0">'; | |
954 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('status').'</th>'; | |
955 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('uucsvline', 'admin').'</th>'; | |
956 | echo '<th class="header c'.$ci++.'" scope="col">ID</th>'; | |
957 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('username').'</th>'; | |
958 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('firstname').'</th>'; | |
959 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('lastname').'</th>'; | |
960 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('email').'</th>'; | |
961 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('password').'</th>'; | |
962 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('authentication').'</th>'; | |
df997f84 | 963 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('enrolments', 'enrol').'</th>'; |
e4e38544 | 964 | echo '<th class="header c'.$ci++.'" scope="col">'.get_string('delete').'</th>'; |
965 | echo '</tr>'; | |
966 | $this->_row = null; | |
967 | } | |
968 | ||
969 | function flush() { | |
970 | if (empty($this->_row) or empty($this->_row['line']['normal'])) { | |
971 | $this->_row = array(); | |
972 | foreach ($this->columns as $col) { | |
973 | $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>''); | |
974 | } | |
975 | return; | |
976 | } | |
977 | $ci = 0; | |
978 | $ri = 1; | |
07ed083e RW |
979 | echo '<tr class="r'.$ri.'">'; |
980 | foreach ($this->_row as $key=>$field) { | |
e4e38544 | 981 | foreach ($field as $type=>$content) { |
982 | if ($field[$type] !== '') { | |
07ed083e RW |
983 | if ($key == 'username' && $type == 'normal') { |
984 | $field[$type] = clean_param($field[$type], PARAM_USERNAME); | |
985 | } | |
e4e38544 | 986 | $field[$type] = '<span class="uu'.$type.'">'.$field[$type].'</span>'; |
987 | } else { | |
988 | unset($field[$type]); | |
989 | } | |
990 | } | |
991 | echo '<td class="cell c'.$ci++.'">'; | |
992 | if (!empty($field)) { | |
993 | echo implode('<br />', $field); | |
994 | } else { | |
995 | echo ' '; | |
996 | } | |
997 | echo '</td>'; | |
998 | } | |
999 | echo '</tr>'; | |
1000 | foreach ($this->columns as $col) { | |
1001 | $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>''); | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | function track($col, $msg, $level='normal', $merge=true) { | |
1006 | if (empty($this->_row)) { | |
1007 | $this->flush(); //init arrays | |
1008 | } | |
1009 | if (!in_array($col, $this->columns)) { | |
1010 | debugging('Incorrect column:'.$col); | |
1011 | return; | |
1012 | } | |
1013 | if ($merge) { | |
1014 | if ($this->_row[$col][$level] != '') { | |
1015 | $this->_row[$col][$level] .='<br />'; | |
1016 | } | |
1017 | $this->_row[$col][$level] .= s($msg); | |
1018 | } else { | |
1019 | $this->_row[$col][$level] = s($msg); | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | function close() { | |
1024 | echo '</table>'; | |
1025 | } | |
1026 | } | |
df7ecfe4 | 1027 | |
e4e38544 | 1028 | /** |
1029 | * Validation callback function - verified the column line of csv file. | |
1030 | * Converts column names to lowercase too. | |
1031 | */ | |
1032 | function validate_user_upload_columns(&$columns) { | |
1033 | global $STD_FIELDS, $PRF_FIELDS; | |
1034 | ||
1035 | if (count($columns) < 2) { | |
1036 | return get_string('csvfewcolumns', 'error'); | |
df7ecfe4 | 1037 | } |
e4e38544 | 1038 | |
1039 | // test columns | |
1040 | $processed = array(); | |
1041 | foreach ($columns as $key=>$unused) { | |
1042 | $columns[$key] = strtolower($columns[$key]); // no unicode expected here, ignore case | |
1043 | $field = $columns[$key]; | |
1044 | if (!in_array($field, $STD_FIELDS) && !in_array($field, $PRF_FIELDS) &&// if not a standard field and not an enrolment field, then we have an error | |
1045 | !preg_match('/^course\d+$/', $field) && !preg_match('/^group\d+$/', $field) && | |
2686b6c3 | 1046 | !preg_match('/^type\d+$/', $field) && !preg_match('/^role\d+$/', $field) && |
1047 | !preg_match('/^enrolperiod\d+$/', $field)) { | |
e4e38544 | 1048 | return get_string('invalidfieldname', 'error', $field); |
1049 | } | |
1050 | if (in_array($field, $processed)) { | |
1051 | return get_string('csvcolumnduplicates', 'error'); | |
1052 | } | |
1053 | $processed[] = $field; | |
df7ecfe4 | 1054 | } |
e4e38544 | 1055 | return true; |
df7ecfe4 | 1056 | } |
1057 | ||
e4e38544 | 1058 | /** |
1059 | * Increments username - increments trailing number or adds it if not present. | |
1060 | * Varifies that the new username does not exist yet | |
1061 | * @param string $username | |
1062 | * @return incremented username which does not exist yet | |
1063 | */ | |
1064 | function increment_username($username, $mnethostid) { | |
1d8bf5f0 | 1065 | global $DB; |
1066 | ||
e4e38544 | 1067 | if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) { |
1068 | $username = $username.'2'; | |
1069 | } else { | |
1070 | $username = $matches[1][0].($matches[2][0]+1); | |
1071 | } | |
1072 | ||
1d8bf5f0 | 1073 | if ($DB->record_exists('user', array('username'=>$username, 'mnethostid'=>$mnethostid))) { |
e4e38544 | 1074 | return increment_username($username, $mnethostid); |
1075 | } else { | |
1076 | return $username; | |
1077 | } | |
1078 | } | |
df7ecfe4 | 1079 | |
e4e38544 | 1080 | /** |
1081 | * Check if default field contains templates and apply them. | |
1082 | * @param string template - potential tempalte string | |
1083 | * @param object user object- we need username, firstname and lastname | |
1084 | * @return string field value | |
1085 | */ | |
1086 | function process_template($template, $user) { | |
1087 | if (strpos($template, '%') === false) { | |
1088 | return $template; | |
df7ecfe4 | 1089 | } |
e4e38544 | 1090 | |
1091 | // very very ugly hack! | |
1092 | global $template_globals; | |
a226a972 | 1093 | $template_globals = new stdClass(); |
e4e38544 | 1094 | $template_globals->username = isset($user->username) ? $user->username : ''; |
1095 | $template_globals->firstname = isset($user->firstname) ? $user->firstname : ''; | |
1096 | $template_globals->lastname = isset($user->lastname) ? $user->lastname : ''; | |
1097 | ||
1098 | $result = preg_replace_callback('/(?<!%)%([+-~])?(\d)*([flu])/', 'process_template_callback', $template); | |
1099 | ||
1100 | $template_globals = null; | |
1101 | ||
1102 | if (is_null($result)) { | |
1103 | return $template; //error during regex processing?? | |
1104 | } else { | |
1105 | return $result; | |
df7ecfe4 | 1106 | } |
e4e38544 | 1107 | } |
df7ecfe4 | 1108 | |
e4e38544 | 1109 | /** |
1110 | * Internal callback function. | |
1111 | */ | |
1112 | function process_template_callback($block) { | |
1113 | global $template_globals; | |
1114 | $textlib = textlib_get_instance(); | |
1115 | $repl = $block[0]; | |
1116 | ||
1117 | switch ($block[3]) { | |
1118 | case 'u': $repl = $template_globals->username; break; | |
1119 | case 'f': $repl = $template_globals->firstname; break; | |
1120 | case 'l': $repl = $template_globals->lastname; break; | |
1121 | } | |
1122 | switch ($block[1]) { | |
1123 | case '+': $repl = $textlib->strtoupper($repl); break; | |
1124 | case '-': $repl = $textlib->strtolower($repl); break; | |
1125 | case '~': $repl = $textlib->strtotitle($repl); break; | |
1126 | } | |
1127 | if (!empty($block[2])) { | |
1128 | $repl = $textlib->substr($repl, 0 , $block[2]); | |
df7ecfe4 | 1129 | } |
e4e38544 | 1130 | |
1131 | return $repl; | |
df7ecfe4 | 1132 | } |
1133 | ||
e4e38544 | 1134 | /** |
1135 | * Returns list of auth plugins that are enabled and known to work. | |
1136 | */ | |
1137 | function uu_allowed_auths() { | |
df7ecfe4 | 1138 | global $CFG; |
1139 | ||
e4e38544 | 1140 | // only following plugins are guaranteed to work properly |
df997f84 | 1141 | // TODO: add support for more plugins in 2.0 |
e4e38544 | 1142 | $whitelist = array('manual', 'nologin', 'none', 'email'); |
1143 | $plugins = get_enabled_auth_plugins(); | |
1144 | $choices = array(); | |
1145 | foreach ($plugins as $plugin) { | |
370f10b7 | 1146 | $choices[$plugin] = get_string('pluginname', "auth_{$plugin}"); |
df7ecfe4 | 1147 | } |
e4e38544 | 1148 | |
1149 | return $choices; | |
df7ecfe4 | 1150 | } |
1151 | ||
e4e38544 | 1152 | /** |
df997f84 | 1153 | * Returns list of roles that are assignable in courses |
e4e38544 | 1154 | */ |
df997f84 PS |
1155 | function uu_allowed_roles() { |
1156 | // let's cheat a bit, frontpage is guaranteed to exist and has the same list of roles ;-) | |
1157 | $roles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, SITEID), ROLENAME_ORIGINALANDSHORT); | |
1158 | return array_reverse($roles, true); | |
1159 | } | |
e4e38544 | 1160 | |
df997f84 PS |
1161 | function uu_allowed_roles_cache() { |
1162 | $allowedroles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, SITEID), ROLENAME_SHORT); | |
1163 | foreach ($allowedroles as $rid=>$rname) { | |
a226a972 | 1164 | $rolecache[$rid] = new stdClass(); |
df997f84 PS |
1165 | $rolecache[$rid]->id = $rid; |
1166 | $rolecache[$rid]->name = $rname; | |
1167 | if (!is_numeric($rname)) { // only non-numeric shortnames are supported!!! | |
a226a972 | 1168 | $rolecache[$rname] = new stdClass(); |
df997f84 PS |
1169 | $rolecache[$rname]->id = $rid; |
1170 | $rolecache[$rname]->name = $rname; | |
e4e38544 | 1171 | } |
1172 | } | |
df997f84 | 1173 | return $rolecache; |
df7ecfe4 | 1174 | } |