MDL-53139 admin: case diff issue with email
[moodle.git] / admin / tool / uploaduser / locallib.php
CommitLineData
8bdb31ed
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Bulk user registration functions
19 *
ce15d56d
PS
20 * @package tool
21 * @subpackage uploaduser
8bdb31ed
PS
22 * @copyright 2004 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28define('UU_USER_ADDNEW', 0);
29define('UU_USER_ADDINC', 1);
30define('UU_USER_ADD_UPDATE', 2);
31define('UU_USER_UPDATE', 3);
32
33define('UU_UPDATE_NOCHANGES', 0);
34define('UU_UPDATE_FILEOVERRIDE', 1);
35define('UU_UPDATE_ALLOVERRIDE', 2);
36define('UU_UPDATE_MISSING', 3);
37
38define('UU_BULK_NONE', 0);
39define('UU_BULK_NEW', 1);
40define('UU_BULK_UPDATED', 2);
41define('UU_BULK_ALL', 3);
42
43define('UU_PWRESET_NONE', 0);
44define('UU_PWRESET_WEAK', 1);
45define('UU_PWRESET_ALL', 2);
46
47/**
48 * Tracking of processed users.
49 *
50 * This class prints user information into a html table.
51 *
52 * @package core
53 * @subpackage admin
54 * @copyright 2007 Petr Skoda {@link http://skodak.org}
55 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
56 */
57class uu_progress_tracker {
58 private $_row;
08157bfe 59 public $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'suspended', 'deleted');
8bdb31ed
PS
60
61 /**
62 * Print table header.
63 * @return void
64 */
65 public function start() {
66 $ci = 0;
ce15d56d 67 echo '<table id="uuresults" class="generaltable boxaligncenter flexible-wrap" summary="'.get_string('uploadusersresult', 'tool_uploaduser').'">';
8bdb31ed
PS
68 echo '<tr class="heading r0">';
69 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('status').'</th>';
ce15d56d 70 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('uucsvline', 'tool_uploaduser').'</th>';
8bdb31ed
PS
71 echo '<th class="header c'.$ci++.'" scope="col">ID</th>';
72 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('username').'</th>';
73 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('firstname').'</th>';
74 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('lastname').'</th>';
75 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('email').'</th>';
76 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('password').'</th>';
77 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('authentication').'</th>';
78 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('enrolments', 'enrol').'</th>';
08157bfe 79 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('suspended', 'auth').'</th>';
8bdb31ed
PS
80 echo '<th class="header c'.$ci++.'" scope="col">'.get_string('delete').'</th>';
81 echo '</tr>';
82 $this->_row = null;
83 }
84
85 /**
86 * Flush previous line and start a new one.
87 * @return void
88 */
89 public function flush() {
90 if (empty($this->_row) or empty($this->_row['line']['normal'])) {
91 // Nothing to print - each line has to have at least number
92 $this->_row = array();
93 foreach ($this->columns as $col) {
94 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
95 }
96 return;
97 }
98 $ci = 0;
99 $ri = 1;
100 echo '<tr class="r'.$ri.'">';
101 foreach ($this->_row as $key=>$field) {
102 foreach ($field as $type=>$content) {
103 if ($field[$type] !== '') {
104 $field[$type] = '<span class="uu'.$type.'">'.$field[$type].'</span>';
105 } else {
106 unset($field[$type]);
107 }
108 }
109 echo '<td class="cell c'.$ci++.'">';
110 if (!empty($field)) {
111 echo implode('<br />', $field);
112 } else {
113 echo '&nbsp;';
114 }
115 echo '</td>';
116 }
117 echo '</tr>';
118 foreach ($this->columns as $col) {
119 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
120 }
121 }
122
123 /**
124 * Add tracking info
125 * @param string $col name of column
126 * @param string $msg message
127 * @param string $level 'normal', 'warning' or 'error'
128 * @param bool $merge true means add as new line, false means override all previous text of the same type
129 * @return void
130 */
131 public function track($col, $msg, $level = 'normal', $merge = true) {
132 if (empty($this->_row)) {
133 $this->flush(); //init arrays
134 }
135 if (!in_array($col, $this->columns)) {
136 debugging('Incorrect column:'.$col);
137 return;
138 }
139 if ($merge) {
140 if ($this->_row[$col][$level] != '') {
141 $this->_row[$col][$level] .='<br />';
142 }
143 $this->_row[$col][$level] .= $msg;
144 } else {
145 $this->_row[$col][$level] = $msg;
146 }
147 }
148
149 /**
150 * Print the table end
151 * @return void
152 */
153 public function close() {
154 $this->flush();
155 echo '</table>';
156 }
157}
158
159/**
160 * Validation callback function - verified the column line of csv file.
623966a3 161 * Converts standard column names to lowercase.
8bdb31ed 162 * @param csv_import_reader $cir
623966a3
PS
163 * @param array $stdfields standard user fields
164 * @param array $profilefields custom profile fields
8bdb31ed
PS
165 * @param moodle_url $returnurl return url in case of any error
166 * @return array list of fields
167 */
623966a3 168function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $profilefields, moodle_url $returnurl) {
8bdb31ed
PS
169 $columns = $cir->get_columns();
170
171 if (empty($columns)) {
172 $cir->close();
173 $cir->cleanup();
174 print_error('cannotreadtmpfile', 'error', $returnurl);
175 }
176 if (count($columns) < 2) {
177 $cir->close();
178 $cir->cleanup();
179 print_error('csvfewcolumns', 'error', $returnurl);
180 }
181
182 // test columns
183 $processed = array();
184 foreach ($columns as $key=>$unused) {
623966a3 185 $field = $columns[$key];
2f1e464a 186 $lcfield = core_text::strtolower($field);
623966a3
PS
187 if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) {
188 // standard fields are only lowercase
189 $newfield = $lcfield;
190
191 } else if (in_array($field, $profilefields)) {
192 // exact profile field name match - these are case sensitive
193 $newfield = $field;
194
195 } else if (in_array($lcfield, $profilefields)) {
196 // hack: somebody wrote uppercase in csv file, but the system knows only lowercase profile field
197 $newfield = $lcfield;
198
2a34360e 199 } else if (preg_match('/^(sysrole|cohort|course|group|type|role|enrolperiod|enrolstatus)\d+$/', $lcfield)) {
623966a3
PS
200 // special fields for enrolments
201 $newfield = $lcfield;
202
203 } else {
204 $cir->close();
205 $cir->cleanup();
8bdb31ed
PS
206 print_error('invalidfieldname', 'error', $returnurl, $field);
207 }
623966a3 208 if (in_array($newfield, $processed)) {
8bdb31ed
PS
209 $cir->close();
210 $cir->cleanup();
623966a3 211 print_error('duplicatefieldname', 'error', $returnurl, $newfield);
8bdb31ed 212 }
623966a3 213 $processed[$key] = $newfield;
8bdb31ed
PS
214 }
215
216 return $processed;
217}
218
219/**
220 * Increments username - increments trailing number or adds it if not present.
221 * Varifies that the new username does not exist yet
222 * @param string $username
223 * @return incremented username which does not exist yet
224 */
225function uu_increment_username($username) {
226 global $DB, $CFG;
227
228 if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) {
229 $username = $username.'2';
230 } else {
231 $username = $matches[1][0].($matches[2][0]+1);
232 }
233
234 if ($DB->record_exists('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
235 return uu_increment_username($username);
236 } else {
237 return $username;
238 }
239}
240
241/**
242 * Check if default field contains templates and apply them.
243 * @param string template - potential tempalte string
244 * @param object user object- we need username, firstname and lastname
245 * @return string field value
246 */
247function uu_process_template($template, $user) {
248 if (is_array($template)) {
249 // hack for for support of text editors with format
250 $t = $template['text'];
251 } else {
252 $t = $template;
253 }
254 if (strpos($t, '%') === false) {
255 return $template;
256 }
257
258 $username = isset($user->username) ? $user->username : '';
259 $firstname = isset($user->firstname) ? $user->firstname : '';
260 $lastname = isset($user->lastname) ? $user->lastname : '';
261
262 $callback = partial('uu_process_template_callback', $username, $firstname, $lastname);
263
264 $result = preg_replace_callback('/(?<!%)%([+-~])?(\d)*([flu])/', $callback, $t);
265
266 if (is_null($result)) {
267 return $template; //error during regex processing??
43070e61
PS
268 }
269
270 if (is_array($template)) {
271 $template['text'] = $result;
272 return $t;
8bdb31ed 273 } else {
43070e61 274 return $result;
8bdb31ed
PS
275 }
276}
277
278/**
279 * Internal callback function.
280 */
43070e61 281function uu_process_template_callback($username, $firstname, $lastname, $block) {
8bdb31ed 282 switch ($block[3]) {
43070e61
PS
283 case 'u':
284 $repl = $username;
285 break;
286 case 'f':
287 $repl = $firstname;
288 break;
289 case 'l':
290 $repl = $lastname;
291 break;
292 default:
293 return $block[0];
8bdb31ed 294 }
43070e61 295
8bdb31ed 296 switch ($block[1]) {
43070e61 297 case '+':
2f1e464a 298 $repl = core_text::strtoupper($repl);
43070e61
PS
299 break;
300 case '-':
2f1e464a 301 $repl = core_text::strtolower($repl);
43070e61
PS
302 break;
303 case '~':
2f1e464a 304 $repl = core_text::strtotitle($repl);
43070e61 305 break;
8bdb31ed 306 }
43070e61 307
8bdb31ed 308 if (!empty($block[2])) {
2f1e464a 309 $repl = core_text::substr($repl, 0 , $block[2]);
8bdb31ed
PS
310 }
311
312 return $repl;
313}
314
315/**
316 * Returns list of auth plugins that are enabled and known to work.
317 *
318 * If ppl want to use some other auth type they have to include it
319 * in the CSV file next on each line.
320 *
321 * @return array type=>name
322 */
323function uu_supported_auths() {
9b29f686 324 // Get all the enabled plugins.
8bdb31ed
PS
325 $plugins = get_enabled_auth_plugins();
326 $choices = array();
327 foreach ($plugins as $plugin) {
9b29f686
MN
328 $objplugin = get_auth_plugin($plugin);
329 // If the plugin can not be manually set skip it.
330 if (!$objplugin->can_be_manually_set()) {
8bdb31ed
PS
331 continue;
332 }
333 $choices[$plugin] = get_string('pluginname', "auth_{$plugin}");
334 }
335
336 return $choices;
337}
338
339/**
340 * Returns list of roles that are assignable in courses
341 * @return array
342 */
343function uu_allowed_roles() {
344 // let's cheat a bit, frontpage is guaranteed to exist and has the same list of roles ;-)
bf006d2c 345 $roles = get_assignable_roles(context_course::instance(SITEID), ROLENAME_ORIGINALANDSHORT);
8bdb31ed
PS
346 return array_reverse($roles, true);
347}
348
349/**
350 * Returns mapping of all roles using short role name as index.
351 * @return array
352 */
353function uu_allowed_roles_cache() {
bf006d2c 354 $allowedroles = get_assignable_roles(context_course::instance(SITEID), ROLENAME_SHORT);
8bdb31ed
PS
355 foreach ($allowedroles as $rid=>$rname) {
356 $rolecache[$rid] = new stdClass();
357 $rolecache[$rid]->id = $rid;
358 $rolecache[$rid]->name = $rname;
359 if (!is_numeric($rname)) { // only non-numeric shortnames are supported!!!
360 $rolecache[$rname] = new stdClass();
361 $rolecache[$rname]->id = $rid;
362 $rolecache[$rname]->name = $rname;
363 }
364 }
365 return $rolecache;
366}
bd8dc9ba 367
2a34360e
SP
368/**
369 * Returns mapping of all system roles using short role name as index.
370 * @return array
371 */
372function uu_allowed_sysroles_cache() {
373 $allowedroles = get_assignable_roles(context_system::instance(), ROLENAME_SHORT);
374 foreach ($allowedroles as $rid => $rname) {
375 $rolecache[$rid] = new stdClass();
376 $rolecache[$rid]->id = $rid;
377 $rolecache[$rid]->name = $rname;
378 if (!is_numeric($rname)) { // Only non-numeric shortnames are supported!
379 $rolecache[$rname] = new stdClass();
380 $rolecache[$rname]->id = $rid;
381 $rolecache[$rname]->name = $rname;
382 }
383 }
384 return $rolecache;
385}
386
bd8dc9ba
RT
387/**
388 * Pre process custom profile data, and update it with corrected value
389 *
390 * @param stdClass $data user profile data
391 * @return stdClass pre-processed custom profile data
392 */
393function uu_pre_process_custom_profile_data($data) {
394 global $CFG, $DB;
395 // find custom profile fields and check if data needs to converted.
396 foreach ($data as $key => $value) {
397 if (preg_match('/^profile_field_/', $key)) {
398 $shortname = str_replace('profile_field_', '', $key);
399 if ($fields = $DB->get_records('user_info_field', array('shortname' => $shortname))) {
400 foreach ($fields as $field) {
401 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
402 $newfield = 'profile_field_'.$field->datatype;
403 $formfield = new $newfield($field->id, $data->id);
404 if (method_exists($formfield, 'convert_external_data')) {
405 $data->$key = $formfield->convert_external_data($value);
406 }
407 }
408 }
409 }
410 }
411 return $data;
412}
1b4d2d56
RT
413
414/**
415 * Checks if data provided for custom fields is correct
416 * Currently checking for custom profile field or type menu
417 *
418 * @param array $data user profile data
419 * @return bool true if no error else false
420 */
421function uu_check_custom_profile_data(&$data) {
422 global $CFG, $DB;
423 $noerror = true;
ada40715 424 $testuserid = null;
1b4d2d56 425
ada40715
NK
426 if (!empty($data['username'])) {
427 if (preg_match('/id=(.*)"/i', $data['username'], $result)) {
428 $testuserid = $result[1];
429 }
430 }
431 // Find custom profile fields and check if data needs to converted.
1b4d2d56
RT
432 foreach ($data as $key => $value) {
433 if (preg_match('/^profile_field_/', $key)) {
434 $shortname = str_replace('profile_field_', '', $key);
435 if ($fields = $DB->get_records('user_info_field', array('shortname' => $shortname))) {
436 foreach ($fields as $field) {
437 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
438 $newfield = 'profile_field_'.$field->datatype;
439 $formfield = new $newfield($field->id, 0);
440 if (method_exists($formfield, 'convert_external_data') &&
441 is_null($formfield->convert_external_data($value))) {
442 $data['status'][] = get_string('invaliduserfield', 'error', $shortname);
443 $noerror = false;
444 }
ada40715
NK
445 // Check for duplicate value.
446 if (method_exists($formfield, 'edit_validate_field') ) {
68665272
AN
447 $testuser = new stdClass();
448 $testuser->{$key} = $value;
449 $testuser->id = $testuserid;
ada40715
NK
450 $err = $formfield->edit_validate_field($testuser);
451 if (!empty($err[$key])) {
452 $data['status'][] = $err[$key].' ('.$key.')';
453 $noerror = false;
454 }
455 }
1b4d2d56
RT
456 }
457 }
458 }
459 }
460 return $noerror;
68665272 461}