3 /// Bulk user registration script from a comma separated file
4 /// Returns list of users with their user ids
6 require_once('../config.php');
7 require_once($CFG->libdir
.'/uploadlib.php');
8 require_once($CFG->libdir
.'/adminlib.php');
9 $adminroot = admin_get_root();
10 admin_externalpage_setup('uploadusers', $adminroot);
12 $createpassword = optional_param('createpassword', 0, PARAM_BOOL
);
13 $updateaccounts = optional_param('updateaccounts', 0, PARAM_BOOL
);
14 $allowrenames = optional_param('allowrenames', 0, PARAM_BOOL
);
18 require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM
, SITEID
));
20 if (! $site = get_site()) {
21 error("Could not find site-level course");
24 if (!$adminuser = get_admin()) {
25 error("Could not find site admin");
28 $strfile = get_string('file');
29 $struser = get_string('user');
30 $strusersnew = get_string('usersnew');
31 $strusersupdated = get_string('usersupdated');
32 $struploadusers = get_string('uploadusers');
33 $straddnewuser = get_string('importuser');
35 $csv_encode = '/\&\#44/';
36 if (isset($CFG->CSV_DELIMITER
)) {
37 $csv_delimiter = '\\' . $CFG->CSV_DELIMITER
;
38 $csv_delimiter2 = $CFG->CSV_DELIMITER
;
40 if (isset($CFG->CSV_ENCODE
)) {
41 $csv_encode = '/\&\#' . $CFG->CSV_ENCODE
. '/';
44 $csv_delimiter = "\,";
45 $csv_delimiter2 = ",";
50 admin_externalpage_print_header($adminroot);
53 /// If a file has been uploaded, then process it
56 $um = new upload_manager('userfile',false,false,null,false,0);
58 if ($um->preprocess_files() && confirm_sesskey()) {
59 $filename = $um->files
['userfile']['tmp_name'];
61 // Large files are likely to take their time and memory. Let PHP know
62 // that we'll take longer, and that the process should be recycled soon
65 @raise_memory_limit
("192M");
66 if (function_exists('apache_child_terminate')) {
67 @apache_child_terminate
();
70 //Fix mac/dos newlines
71 $text = my_file_get_contents($filename);
72 $text = preg_replace('!\r\n?!',"\n",$text);
73 $fp = fopen($filename, "w");
77 $fp = fopen($filename, "r");
79 // make arrays of valid fields for error checking
80 $required = array("username" => 1,
81 "password" => !$createpassword,
85 $optionalDefaults = array("mnethostid" => 1,
93 $optional = array("idnumber" => 1,
103 "autosubscribe" => 1,
126 "password" => $createpassword,
127 "oldusername" => $allowrenames);
129 // --- get header (field names) ---
130 $header = split($csv_delimiter, fgets($fp,1024));
131 // check for valid field names
132 foreach ($header as $i => $h) {
133 $h = trim($h); $header[$i] = $h; // remove whitespace
134 if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
135 error(get_string('invalidfieldname', 'error', $h), 'uploaduser.php?sesskey='.$USER->sesskey
);
137 if (isset($required[$h])) {
141 // check for required fields
142 foreach ($required as $key => $value) {
143 if ($value) { //required field missing
144 error(get_string('fieldrequired', 'error', $key), 'uploaduser.php?sesskey='.$USER->sesskey
);
147 $linenum = 2; // since header is line 1
155 // Will use this course array a lot
156 // so fetch it early and keep it in memory
157 $courses = get_courses('all','c.sortorder','c.id,c.shortname,c.fullname,c.sortorder,c.teacher,c.visible');
159 while (!feof ($fp)) {
160 foreach ($optionalDefaults as $key => $value) {
161 $user->$key = addslashes($adminuser->$key);
163 //Note: commas within a field should be encoded as , (for comma separated csv files)
164 //Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
165 $line = split($csv_delimiter, fgets($fp,1024));
166 foreach ($line as $key => $value) {
167 //decode encoded commas
168 $record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
170 if ($record[$header[0]]) {
171 // add a new user to the database
173 // add fields to object $user
174 foreach ($record as $name => $value) {
175 // check for required values
176 if (isset($required[$name]) and !$value) {
177 error(get_string('missingfield', 'error', $name). " ".
178 get_string('erroronline', 'error', $linenum) .". ".
179 get_string('processingstops', 'error'),
180 'uploaduser.php?sesskey='.$USER->sesskey
);
182 // password needs to be encrypted
183 else if ($name == "password" && !empty($value)) {
184 $user->password
= hash_internal_user_password($value);
186 else if ($name == "username") {
187 $user->username
= addslashes(moodle_strtolower($value));
191 $user->{$name} = addslashes($value);
194 $user->confirmed
= 1;
195 $user->timemodified
= time();
197 $username = $user->username
;
198 $addcourse[0] = isset($user->course1
) ?
$user->course1
: NULL;
199 $addcourse[1] = isset($user->course2
) ?
$user->course2
: NULL;
200 $addcourse[2] = isset($user->course3
) ?
$user->course3
: NULL;
201 $addcourse[3] = isset($user->course4
) ?
$user->course4
: NULL;
202 $addcourse[4] = isset($user->course5
) ?
$user->course5
: NULL;
203 $addgroup[0] = isset($user->group1
) ?
$user->group1
: NULL;
204 $addgroup[1] = isset($user->group2
) ?
$user->group2
: NULL;
205 $addgroup[2] = isset($user->group3
) ?
$user->group3
: NULL;
206 $addgroup[3] = isset($user->group4
) ?
$user->group4
: NULL;
207 $addgroup[4] = isset($user->group5
) ?
$user->group5
: NULL;
208 $addtype[0] = isset($user->type1
) ?
$user->type1
: NULL;
209 $addtype[1] = isset($user->type2
) ?
$user->type2
: NULL;
210 $addtype[2] = isset($user->type3
) ?
$user->type3
: NULL;
211 $addtype[3] = isset($user->type4
) ?
$user->type4
: NULL;
212 $addtype[4] = isset($user->type5
) ?
$user->type5
: NULL;
213 $addrole[0] = isset($user->role1
) ?
$user->role1
: NULL;
214 $addrole[1] = isset($user->role2
) ?
$user->role2
: NULL;
215 $addrole[2] = isset($user->role3
) ?
$user->role3
: NULL;
216 $addrole[3] = isset($user->role4
) ?
$user->role4
: NULL;
217 $addrole[4] = isset($user->role5
) ?
$user->role5
: NULL;
219 for ($i=0; $i<5; $i++
) {
222 foreach ($courses as $eachcourse) {
223 for ($i=0; $i<5; $i++
) {
224 if ($eachcourse->shortname
== $addcourse[$i]) {
225 $course[$i] = $eachcourse;
230 // before insert/update, check whether we should be updating
231 // an old record instead
232 if ($allowrenames && !empty($user->oldusername
) ) {
233 $user->oldusername
= moodle_strtolower($user->oldusername
);
234 if ($olduser = get_record('user', 'username', $user->oldusername
, 'mnethostid', $user->mnethostid
)) {
235 if (set_field('user', 'username', $user->username
, 'username', $user->oldusername
)) {
236 notify(get_string('userrenamed', 'admin') . " : $user->oldusername $user->username");
239 notify(get_string('usernotrenamedexists', 'error') . " : $user->oldusername $user->username");
244 notify(get_string('usernotrenamedmissing', 'error') . " : $user->oldusername $user->username");
250 if ($olduser = get_record("user", "username", $username, "mnethostid", $user->mnethostid
)) {
251 if ($updateaccounts) {
252 // Record is being updated
253 $user->id
= $olduser->id
;
254 if (update_record('user', $user)) {
255 notify("$user->id , $user->username ".get_string('useraccountupdated', 'admin'));
258 notify(get_string('usernotupdatederror', 'error', $username));
263 //Record not added - user is already registered
264 //In this case, output userid from previous registration
265 //This can be used to obtain a list of userids for existing users
266 notify("$olduser->id ".get_string('usernotaddedregistered', 'error', $username));
272 if ($user->id
= insert_record("user", $user)) {
273 notify("$struser: $user->id = $user->username");
275 if (empty($user->password
) && $createpassword) {
276 // passwords will be created and sent out on cron
277 insert_record('user_preferences', array( userid
=> $user->id
,
278 name
=> 'create_password',
280 insert_record('user_preferences', array( userid
=> $user->id
,
281 name
=> 'auth_forcepasswordchange',
285 // Record not added -- possibly some other error
286 notify(get_string('usernotaddederror', 'error', $username));
291 for ($i=0; $i<5; $i++
) {
292 if ($addcourse[$i] && !$course[$i]) {
293 notify(get_string('unknowncourse', 'error', $addcourse[$i]));
296 for ($i=0; $i<5; $i++
) {
300 notify(get_string('coursegroupunknown','error',$addgroup[$i]));
302 if ($groupid = groups_get_group_by_name($course[$i]->id
, $addgroup[$i])) { //TODO:check.
303 $groupid[$i] = $groupid;
305 notify(get_string('groupunknown','error',$addgroup[$i]));
310 for ($i=0; $i<5; $i++
) { /// Enrol into courses if necessary
312 if (isset($addrole[$i])) {
313 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course[$i]->id
);
314 if (!user_can_assign($coursecontext, $addrole[$i])) {
315 notify('--> Can not assign role in course'); //TODO: localize
317 $ret = role_assign($addrole[$i], $user->id
, 0, $coursecontext->id
);
318 } else if (isset($addtype[$i])) {
319 switch ($addtype[$i]) {
321 $ret = add_teacher($user->id
, $course[$i]->id
, 1);
324 case 3: // non-editing teacher
325 $ret = add_teacher($user->id
, $course[$i]->id
, 0);
329 $ret = enrol_student($user->id
, $course[$i]->id
);
333 $ret = enrol_student($user->id
, $course[$i]->id
);
336 notify('-->'. get_string('enrolledincourse', '', $addcourse[$i]));
338 notify('-->'.get_string('enrolledincoursenot', '', $addcourse[$i]));
342 for ($i=0; $i<5; $i++
) { // Add user to groups if necessary
343 if ($course[$i] && $groupid[$i]) {
344 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course[$i]->id
);
345 if (count(get_user_roles($coursecontext, $user->id
))) {
346 if (add_user_to_group($groupid[$i], $user->id
)) {
347 notify('-->' . get_string('addedtogroup','',$addgroup[$i]));
349 notify('-->' . get_string('addedtogroupnot','',$addgroup[$i]));
352 notify('-->' . get_string('addedtogroupnotenrolled','',$addgroup[$i]));
361 notify("$strusersnew: $usersnew");
362 notify(get_string('usersupdated', 'admin') . ": $usersupdated");
363 notify(get_string('errors', 'admin') . ": $userserrors");
365 notify(get_string('usersrenamed', 'admin') . ": $renames");
366 notify(get_string('renameerrors', 'admin') . ": $renameerrors");
372 print_heading_with_help($struploadusers, 'uploadusers');
374 $noyesoptions = array( get_string('no'), get_string('yes') );
376 $maxuploadsize = get_max_upload_file_size();
377 echo '<div style="text-align:center">';
378 echo '<form method="post" enctype="multipart/form-data" action="uploaduser.php"><div>'.
379 $strfile.' <input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />'.
380 '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />'.
381 '<input type="file" name="userfile" size="30" />';
382 print_heading(get_string('settings'));
383 echo '<table style="margin-left:auto;margin-right:auto">';
384 echo '<tr><td>' . get_string('passwordhandling', 'auth') . '</td><td>';
385 $passwordopts = array( 0 => get_string('infilefield', 'auth'),
386 1 => get_string('createpasswordifneeded', 'auth'),
388 choose_from_menu($passwordopts, 'createpassword', $createpassword);
391 echo '<tr><td>' . get_string('updateaccounts', 'admin') . '</td><td>';
392 choose_from_menu($noyesoptions, 'updateaccounts', $updateaccounts);
395 echo '<tr><td>' . get_string('allowrenames', 'admin') . '</td><td>';
396 choose_from_menu($noyesoptions, 'allowrenames', $allowrenames);
398 echo '</table><br />';
399 echo '<input type="submit" value="'.$struploadusers.'" />';
400 echo '</div></form><br />';
403 admin_externalpage_print_footer($adminroot);
407 function my_file_get_contents($filename, $use_include_path = 0) {
408 /// Returns the file as one big long string
411 $file = @fopen
($filename, "rb", $use_include_path);
413 while (!feof($file)) {
414 $data .= fread($file, 1024);