2004-10-03 16:08:27 +00:00
|
|
|
<?php // $Id$
|
2004-01-06 02:48:12 +00:00
|
|
|
|
|
|
|
/// Bulk user registration script from a comma separated file
|
|
|
|
/// Returns list of users with their user ids
|
|
|
|
|
|
|
|
require_once("../config.php");
|
|
|
|
|
|
|
|
optional_variable($numusers, 0);
|
|
|
|
|
|
|
|
require_login();
|
|
|
|
|
|
|
|
if (!isadmin()) {
|
|
|
|
error("You must be an administrator to edit users this way.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $site = get_site()) {
|
|
|
|
error("Could not find site-level course");
|
|
|
|
}
|
|
|
|
|
2004-10-03 16:08:27 +00:00
|
|
|
if (!confirm_sesskey()) {
|
|
|
|
error(get_string('confirmsesskeybad', 'error'));
|
|
|
|
}
|
|
|
|
|
2004-01-07 06:55:35 +00:00
|
|
|
if (!$adminuser = get_admin()) {
|
|
|
|
error("Could not find site admin");
|
|
|
|
}
|
|
|
|
|
2004-01-06 02:48:12 +00:00
|
|
|
$streditmyprofile = get_string("editmyprofile");
|
|
|
|
$stradministration = get_string("administration");
|
|
|
|
$strchoose = get_string("choose");
|
2004-01-07 06:55:35 +00:00
|
|
|
$struser = get_string("user");
|
|
|
|
$strusers = get_string("users");
|
2004-01-06 02:48:12 +00:00
|
|
|
$strusersnew = get_string("usersnew");
|
2004-01-07 06:55:35 +00:00
|
|
|
$struploadusers = get_string("uploadusers");
|
|
|
|
$straddnewuser = get_string("importuser");
|
|
|
|
|
2004-04-14 20:46:39 +00:00
|
|
|
$csv_encode = '/\&\#44/';
|
|
|
|
if (isset($CFG->CSV_DELIMITER)) {
|
2004-04-22 06:02:41 +00:00
|
|
|
$csv_delimiter = '\\' . $CFG->CSV_DELIMITER;
|
|
|
|
$csv_delimiter2 = $CFG->CSV_DELIMITER;
|
|
|
|
|
|
|
|
if (isset($CFG->CSV_ENCODE)) {
|
|
|
|
$csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
|
|
|
|
}
|
2004-04-14 20:46:39 +00:00
|
|
|
} else {
|
2004-04-22 06:02:41 +00:00
|
|
|
$csv_delimiter = "\,";
|
|
|
|
$csv_delimiter2 = ",";
|
2004-04-14 20:46:39 +00:00
|
|
|
}
|
2004-01-06 02:48:12 +00:00
|
|
|
|
|
|
|
/// Print the header
|
|
|
|
|
|
|
|
print_header("$site->shortname: $struploadusers", $site->fullname,
|
|
|
|
"<a href=\"index.php\">$stradministration</a> ->
|
|
|
|
<a href=\"users.php\">$strusers</a> -> $struploadusers");
|
|
|
|
|
|
|
|
|
|
|
|
/// If a file has been uploaded, then process it
|
|
|
|
|
2004-09-15 22:42:10 +00:00
|
|
|
|
|
|
|
require_once($CFG->dirroot.'/lib/uploadlib.php');
|
|
|
|
$um = new upload_manager('userfile',false,false,null,false,0);
|
|
|
|
if ($um->preprocess_files()) {
|
|
|
|
$filename = $um->files['userfile']['tmp_name'];
|
2004-01-06 02:48:12 +00:00
|
|
|
|
|
|
|
//Fix mac/dos newlines
|
|
|
|
$text = my_file_get_contents($filename);
|
|
|
|
$text = preg_replace('!\r\n?!',"\n",$text);
|
|
|
|
$fp = fopen($filename, "w");
|
|
|
|
fwrite($fp,$text);
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
$fp = fopen($filename, "r");
|
|
|
|
|
|
|
|
// make arrays of valid fields for error checking
|
|
|
|
$required = array("username" => 1,
|
|
|
|
"password" => 1,
|
|
|
|
"firstname" => 1,
|
|
|
|
"lastname" => 1,
|
|
|
|
"email" => 1);
|
|
|
|
$optionalDefaults = array("institution" => 1,
|
|
|
|
"department" => 1,
|
|
|
|
"city" => 1,
|
|
|
|
"country" => 1,
|
2005-02-16 14:15:11 +00:00
|
|
|
"lang" => 1,
|
|
|
|
"auth" => 1,
|
2004-01-06 02:48:12 +00:00
|
|
|
"timezone" => 1);
|
|
|
|
$optional = array("idnumber" => 1,
|
|
|
|
"icq" => 1,
|
|
|
|
"phone1" => 1,
|
|
|
|
"phone2" => 1,
|
|
|
|
"address" => 1,
|
|
|
|
"url" => 1,
|
|
|
|
"description" => 1,
|
|
|
|
"mailformat" => 1,
|
2004-01-06 06:15:56 +00:00
|
|
|
"maildisplay" => 1,
|
2004-01-06 02:48:12 +00:00
|
|
|
"htmleditor" => 1,
|
|
|
|
"autosubscribe" => 1,
|
|
|
|
"idnumber" => 1,
|
|
|
|
"icq" => 1,
|
|
|
|
"course1" => 1,
|
|
|
|
"course2" => 1,
|
|
|
|
"course3" => 1,
|
|
|
|
"course4" => 1,
|
2004-02-24 20:15:26 +00:00
|
|
|
"course5" => 1,
|
|
|
|
"group1" => 1,
|
|
|
|
"group2" => 1,
|
|
|
|
"group3" => 1,
|
|
|
|
"group4" => 1,
|
|
|
|
"group5" =>1);
|
2004-01-06 02:48:12 +00:00
|
|
|
|
|
|
|
// --- get header (field names) ---
|
2004-04-14 20:46:39 +00:00
|
|
|
$header = split($csv_delimiter, fgets($fp,1024));
|
2004-01-06 02:48:12 +00:00
|
|
|
// check for valid field names
|
|
|
|
foreach ($header as $i => $h) {
|
|
|
|
$h = trim($h); $header[$i] = $h; // remove whitespace
|
|
|
|
if (!($required[$h] or $optionalDefaults[$h] or $optional[$h])) {
|
2004-10-03 16:08:27 +00:00
|
|
|
error(get_string('invalidfieldname', 'error', $h), 'uploaduser.php?sesskey='.$USER->sesskey);
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
if ($required[$h]) {
|
|
|
|
$required[$h] = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check for required fields
|
|
|
|
foreach ($required as $key => $value) {
|
|
|
|
if ($value < 2) {
|
2004-10-03 16:08:27 +00:00
|
|
|
error(get_string('fieldrequired', 'error', $key), 'uploaduser.php?sesskey='.$USER->sesskey);
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$linenum = 2; // since header is line 1
|
|
|
|
|
|
|
|
while (!feof ($fp)) {
|
2004-01-06 20:25:12 +00:00
|
|
|
foreach ($optionalDefaults as $key => $value) {
|
2004-01-08 11:00:40 +00:00
|
|
|
$user->$key = addslashes($adminuser->$key);
|
2004-01-06 20:25:12 +00:00
|
|
|
}
|
2004-04-14 20:46:39 +00:00
|
|
|
//Note: commas within a field should be encoded as , (for comma separated csv files)
|
|
|
|
//Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
|
|
|
|
$line = split($csv_delimiter, fgets($fp,1024));
|
2004-01-06 02:48:12 +00:00
|
|
|
foreach ($line as $key => $value) {
|
2004-01-06 17:33:36 +00:00
|
|
|
//decode encoded commas
|
2004-04-14 20:46:39 +00:00
|
|
|
$record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
if ($record[$header[0]]) {
|
|
|
|
// add a new user to the database
|
|
|
|
optional_variable($newuser, "");
|
|
|
|
|
|
|
|
// add fields to object $user
|
|
|
|
foreach ($record as $name => $value) {
|
|
|
|
// check for required values
|
|
|
|
if ($required[$name] and !$value) {
|
2004-01-07 06:55:35 +00:00
|
|
|
error(get_string('missingfield', 'error', $name). " ".
|
2005-02-14 06:07:59 +00:00
|
|
|
get_string('erroronline', 'error', $linenum) .". ".
|
|
|
|
get_string('processingstops', 'error'),
|
2004-10-03 16:08:27 +00:00
|
|
|
'uploaduser.php?sesskey='.$USER->sesskey);
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
// password needs to be encrypted
|
|
|
|
else if ($name == "password") {
|
|
|
|
$user->password = md5($value);
|
|
|
|
}
|
2004-12-10 04:16:02 +00:00
|
|
|
else if ($name == "username") {
|
2004-12-12 20:43:19 +00:00
|
|
|
$user->username = addslashes(moodle_strtolower($value));
|
2004-12-10 04:16:02 +00:00
|
|
|
}
|
2004-01-06 02:48:12 +00:00
|
|
|
// normal entry
|
|
|
|
else {
|
2004-01-08 03:51:08 +00:00
|
|
|
$user->{$name} = addslashes($value);
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$user->confirmed = 1;
|
|
|
|
$user->timemodified = time();
|
|
|
|
$linenum++;
|
|
|
|
$username = $user->username;
|
|
|
|
$addcourse[0] = $user->course1;
|
|
|
|
$addcourse[1] = $user->course2;
|
|
|
|
$addcourse[2] = $user->course3;
|
|
|
|
$addcourse[3] = $user->course4;
|
|
|
|
$addcourse[4] = $user->course5;
|
2004-02-24 20:15:26 +00:00
|
|
|
$addgroup[0] = $user->group1;
|
|
|
|
$addgroup[1] = $user->group2;
|
|
|
|
$addgroup[2] = $user->group3;
|
|
|
|
$addgroup[3] = $user->group4;
|
|
|
|
$addgroup[4] = $user->group5;
|
2004-11-22 05:41:14 +00:00
|
|
|
$courses = get_courses("all",'c.sortorder','c.id,c.shortname,c.fullname,c.sortorder');
|
2004-01-06 02:48:12 +00:00
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
$courseid[$i]=0;
|
|
|
|
}
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
if ($course->shortname == $addcourse[$i]) {
|
|
|
|
$courseid[$i] = $course->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-02-14 06:07:59 +00:00
|
|
|
if (get_record("user","username",$username) || !($user->id = insert_record("user", $user))) {
|
2004-01-06 02:48:12 +00:00
|
|
|
if (!$user = get_record("user", "username", "changeme")) { // half finished user from another time
|
|
|
|
//Record not added - probably because user is already registered
|
|
|
|
//In this case, output userid from previous registration
|
|
|
|
//This can be used to obtain a list of userids for existing users
|
|
|
|
if ($user = get_record("user","username",$username)) {
|
2004-01-07 06:55:35 +00:00
|
|
|
notify("$user->id ".get_string('usernotaddedregistered', 'error', $username));
|
2004-01-06 02:48:12 +00:00
|
|
|
} else {
|
2004-01-07 06:55:35 +00:00
|
|
|
notify(get_string('usernotaddederror', 'error', $username));
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($user->username != "changeme") {
|
2004-01-07 06:55:35 +00:00
|
|
|
notify("$struser: $user->id = $user->username");
|
2004-01-06 02:48:12 +00:00
|
|
|
$numusers++;
|
|
|
|
}
|
2004-02-24 20:15:26 +00:00
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
if ($addcourse[$i] && !$courseid[$i]) {
|
|
|
|
notify(get_string('unknowncourse', 'error', $addcourse[$i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
$groupid[$i] = 0;
|
|
|
|
if ($addgroup[$i]) {
|
|
|
|
if (!$courseid[$i]) {
|
|
|
|
notify(get_string('coursegroupunknown','error',$addgroup[$i]));
|
|
|
|
} else {
|
|
|
|
if ($group = get_record("groups","courseid",$courseid[$i],"name",$addgroup[$i])) {
|
|
|
|
$groupid[$i] = $group->id;
|
|
|
|
} else {
|
|
|
|
notify(get_string('groupunknown','error',$addgroup[$i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-01-06 02:48:12 +00:00
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
if ($courseid[$i]) {
|
|
|
|
if (enrol_student($user->id, $courseid[$i])) {
|
2004-01-07 06:55:35 +00:00
|
|
|
notify('-->'. get_string('enrolledincourse', '', $addcourse[$i]));
|
2004-01-06 02:48:12 +00:00
|
|
|
} else {
|
2004-01-07 06:55:35 +00:00
|
|
|
notify('-->'.get_string('enrolledincoursenot', '', $addcourse[$i]));
|
2004-01-06 02:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 20:15:26 +00:00
|
|
|
for ($i=0; $i<5; $i++) {
|
|
|
|
if ($courseid[$i] && $groupid[$i]) {
|
|
|
|
if (record_exists("user_students","userid",$user->id,"course",$courseid[$i])) {
|
2004-02-24 22:09:43 +00:00
|
|
|
$usergroup = user_group($courseid[$i],$user->id);
|
|
|
|
if ($usergroup) {
|
|
|
|
notify('-->' . get_string('groupalready','error',$usergroup->name));
|
2004-02-24 20:15:26 +00:00
|
|
|
} else {
|
|
|
|
$group_member->groupid = $groupid[$i];
|
|
|
|
$group_member->userid = $user->id;
|
|
|
|
$group_member->timeadded = time();
|
|
|
|
if (insert_record("groups_members",$group_member)) {
|
|
|
|
notify('-->' . get_string('addedtogroup','',$addgroup[$i]));
|
|
|
|
} else {
|
|
|
|
notify('-->' . get_string('addedtogroupnot','',$addgroup[$i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notify('-->' . get_string('addedtogroupnotenrolled','',$addgroup[$i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-06 02:48:12 +00:00
|
|
|
unset ($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($fp);
|
2004-01-07 06:55:35 +00:00
|
|
|
notify("$strusersnew: $numusers");
|
2004-01-06 02:48:12 +00:00
|
|
|
|
|
|
|
echo '<hr />';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the form
|
|
|
|
print_heading_with_help($struploadusers, 'uploadusers');
|
|
|
|
|
|
|
|
$maxuploadsize = get_max_upload_file_size();
|
|
|
|
echo '<center>';
|
|
|
|
echo '<form method="post" enctype="multipart/form-data" action="uploaduser.php">'.
|
|
|
|
$strchoose.':<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'">'.
|
2004-10-03 16:08:27 +00:00
|
|
|
'<input type="hidden" name="sesskey" value="'.$USER->sesskey.'">'.
|
2004-09-12 00:21:21 +00:00
|
|
|
'<input type="file" name="userfile" size="30">'.
|
2004-01-06 02:48:12 +00:00
|
|
|
'<input type="submit" value="'.$struploadusers.'">'.
|
|
|
|
'</form></br>';
|
|
|
|
echo '</center>';
|
|
|
|
|
|
|
|
print_footer($course);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function my_file_get_contents($filename, $use_include_path = 0) {
|
|
|
|
/// Returns the file as one big long string
|
|
|
|
|
|
|
|
$data = "";
|
|
|
|
$file = @fopen($filename, "rb", $use_include_path);
|
|
|
|
if ($file) {
|
|
|
|
while (!feof($file)) {
|
|
|
|
$data .= fread($file, 1024);
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
2004-01-06 17:33:36 +00:00
|
|
|
|