-<?PHP // $Id$\r
-\r
-/// Bulk user registration script from a comma separated file\r
-/// Returns list of users with their user ids\r
-\r
- require_once("../config.php");\r
-\r
- optional_variable($numusers, 0);\r
-\r
- require_login();\r
-\r
- if (!isadmin()) {\r
- error("You must be an administrator to edit users this way.");\r
- }\r
-\r
- if (! $site = get_site()) {\r
- error("Could not find site-level course");\r
- }\r
-\r
- $streditmyprofile = get_string("editmyprofile");\r
- $strnewuser = get_string("newuser");\r
- $userfullname = $strnewuser;\r
- $straddnewuser = get_string("importuser");\r
- $stradministration = get_string("administration");\r
- $strusers = get_string("users");\r
- $strchoose = get_string("choose");\r
- $struploadusers = get_string("uploadusers");\r
- $strusersnew = get_string("usersnew");\r
-\r
-/// Print the header\r
-\r
- print_header("$site->shortname: $struploadusers", $site->fullname, \r
- "<a href=\"index.php\">$stradministration</a> -> \r
- <a href=\"users.php\">$strusers</a> -> $struploadusers");\r
-\r
-\r
-/// If a file has been uploaded, then process it\r
-\r
- if ($filename = valid_uploaded_file($_FILES['userfile'])) {\r
- $notifytext = '';\r
-\r
- //Fix mac/dos newlines\r
- $text = my_file_get_contents($filename);\r
- $text = preg_replace('!\r\n?!',"\n",$text);\r
- $fp = fopen($filename, "w");\r
- fwrite($fp,$text);\r
- fclose($fp);\r
-\r
- $fp = fopen($filename, "r");\r
-\r
- // make arrays of valid fields for error checking\r
- $required = array("username" => 1, \r
- "password" => 1, \r
- "firstname" => 1, \r
- "lastname" => 1,\r
- "email" => 1);\r
- $optionalDefaults = array("institution" => 1, \r
- "department" => 1, \r
- "city" => 1, \r
- "country" => 1,\r
- "lang" => 1, \r
- "timezone" => 1);\r
- $optional = array("idnumber" => 1, \r
- "icq" => 1, \r
- "phone1" => 1, \r
- "phone2" => 1,\r
- "address" => 1, \r
- "url" => 1,\r
- "description" => 1, \r
- "mailformat" => 1, \r
- "htmleditor" => 1, \r
- "autosubscribe" => 1,\r
- "idnumber" => 1, \r
- "icq" => 1, \r
- "course1" => 1, \r
- "course2" => 1,\r
- "course3" => 1, \r
- "course4" => 1, \r
- "course5" => 1);\r
-\r
- // --- get header (field names) ---\r
- $header = split("\,", fgets($fp,1024));\r
- // check for valid field names\r
- foreach ($header as $i => $h) {\r
- $h = trim($h); $header[$i] = $h; // remove whitespace\r
- if (!($required[$h] or $optionalDefaults[$h] or $optional[$h])) {\r
- error("\"$h\" is not a valid field name.", 'uploaduser.php');\r
- }\r
- if ($required[$h]) {\r
- $required[$h] = 2;\r
- }\r
- }\r
- // check for required fields\r
- foreach ($required as $key => $value) {\r
- if ($value < 2) {\r
- error("\"$key\" is a required field.", 'uploaduser.php');\r
- }\r
- }\r
- $linenum = 2; // since header is line 1\r
-\r
- while (!feof ($fp)) {\r
- //Note: commas within a field should be encoded as ,\r
- //Last field, courseid, is optional. If present it should be the Moodle\r
- //course id number for the course in which student should be initially enroled\r
- $line = split("\,", fgets($fp,1024));\r
- foreach ($line as $key => $value) {\r
- $record[$header[$key]] = trim($value);\r
- }\r
- if ($record[$header[0]]) {\r
- // add a new user to the database\r
- optional_variable($newuser, ""); \r
-\r
- // add fields to object $user\r
- foreach ($record as $name => $value) {\r
- // check for required values\r
- if ($required[$name] and !$value) {\r
- error("Missing \"$name\" on line $linenum.", 'uploaduser.php');\r
- }\r
- // password needs to be encrypted\r
- else if ($name == "password") {\r
- $user->password = md5($value);\r
- }\r
- // normal entry\r
- else {\r
- $user->{$name} = $value;\r
- }\r
- }\r
- $user->confirmed = 1;\r
- $user->timemodified = time();\r
- $linenum++;\r
- $username = $user->username;\r
- $addcourse[0] = $user->course1;\r
- $addcourse[1] = $user->course2;\r
- $addcourse[2] = $user->course3;\r
- $addcourse[3] = $user->course4;\r
- $addcourse[4] = $user->course5;\r
- $courses = get_courses("all");\r
- for ($i=0; $i<5; $i++) {$courseid[$i]=0;}\r
- foreach ($courses as $course) {\r
- for ($i=0; $i<5; $i++) {\r
- if ($course->shortname == $addcourse[$i]) {$courseid[$i] = $course->id;}\r
- }\r
- }\r
- for ($i=0; $i<5; $i++) {\r
- if ($addcourse[$i] && !$courseid[$i]) {\r
- $notifytext .= "-1," . $addcourse[$i] . " unknown course<br>\n";\r
- }\r
- }\r
- if (! $user->id = insert_record("user", $user)) {\r
- if (!$user = get_record("user", "username", "changeme")) { // half finished user from another time\r
- //Record not added - probably because user is already registered\r
- //In this case, output userid from previous registration\r
- //This can be used to obtain a list of userids for existing users\r
- $error_uid = -1;\r
- if ($user = get_record("user","username",$username)) {\r
- $error_uid = $user->id;\r
- }\r
- if ($error_uid != -1) {\r
- $notifytext .= $error_uid . "," . $username . ",user not added - already registered";\r
- } else {\r
- $notifytext .= $error_uid . ",failed to add user " . $username . " unknown error";\r
- } \r
- }\r
- } elseif ($user->username != "changeme") {\r
- $notifytext .= $user->id . "," . $user->username . ",";\r
- $numusers++;\r
- }\r
- $lbreak = 1;\r
- for ($i=0; $i<5; $i++) {\r
- if ($courseid[$i]) {\r
- if (enrol_student($user->id, $courseid[$i])) {\r
- $lbreak = 0;\r
- $notifytext .= ",enroled in course $addcourse[$i]<br>\n";\r
- } else {\r
- $notifytext .= ",error: enrolment in course $addcourse[$i] failed<br>\n";\r
- }\r
- }\r
- }\r
- if ($lbreak) {$notifytext .= "<br>\n";}\r
- unset ($user);\r
- }\r
- }\r
- fclose($fp);\r
- notify("$strusersnew: $numusers");\r
-\r
- echo '<hr />';\r
- }\r
-\r
-/// Print the form\r
- print_heading_with_help($struploadusers, 'uploadusers');\r
-\r
- $maxuploadsize = get_max_upload_file_size();\r
- echo '<center>';\r
- echo '<form METHOD="post" enctype="multipart/form-data" action="uploaduser.php">'.\r
- $strchoose.':<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'">'.\r
- '<input type="file" name="userfile" size=30>'.\r
- '<input type="submit" value="'.$struploadusers.'">'.\r
- '</form></br>';\r
- echo '</center>';\r
-\r
- print_footer($course);\r
-\r
-\r
-\r
-function my_file_get_contents($filename, $use_include_path = 0) {\r
- $data = ""; // just to be safe. Dunno, if this is really needed\r
- $file = @fopen($filename, "rb", $use_include_path);\r
- if ($file) {\r
- while (!feof($file)) $data .= fread($file, 1024);\r
- fclose($file);\r
- }\r
- return $data;\r
-}\r
-\r
-?>\r
+<?PHP // $Id$
+
+/// 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");
+ }
+
+ $streditmyprofile = get_string("editmyprofile");
+ $strnewuser = get_string("newuser");
+ $userfullname = $strnewuser;
+ $straddnewuser = get_string("importuser");
+ $stradministration = get_string("administration");
+ $strusers = get_string("users");
+ $strchoose = get_string("choose");
+ $struploadusers = get_string("uploadusers");
+ $strusersnew = get_string("usersnew");
+
+/// 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
+
+ if ($filename = valid_uploaded_file($_FILES['userfile'])) {
+ $notifytext = '';
+
+ //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,
+ "lang" => 1,
+ "timezone" => 1);
+ $optional = array("idnumber" => 1,
+ "icq" => 1,
+ "phone1" => 1,
+ "phone2" => 1,
+ "address" => 1,
+ "url" => 1,
+ "description" => 1,
+ "mailformat" => 1,
+ "htmleditor" => 1,
+ "autosubscribe" => 1,
+ "idnumber" => 1,
+ "icq" => 1,
+ "course1" => 1,
+ "course2" => 1,
+ "course3" => 1,
+ "course4" => 1,
+ "course5" => 1);
+
+ // --- get header (field names) ---
+ $header = split("\,", fgets($fp,1024));
+ // 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])) {
+ error("\"$h\" is not a valid field name.", 'uploaduser.php');
+ }
+ if ($required[$h]) {
+ $required[$h] = 2;
+ }
+ }
+ // check for required fields
+ foreach ($required as $key => $value) {
+ if ($value < 2) {
+ error("\"$key\" is a required field.", 'uploaduser.php');
+ }
+ }
+ $linenum = 2; // since header is line 1
+
+ while (!feof ($fp)) {
+ //Note: commas within a field should be encoded as ,
+ //Last field, courseid, is optional. If present it should be the Moodle
+ //course id number for the course in which student should be initially enroled
+ $line = split("\,", fgets($fp,1024));
+ foreach ($line as $key => $value) {
+ $record[$header[$key]] = trim($value);
+ }
+ 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) {
+ error("Missing \"$name\" on line $linenum.", 'uploaduser.php');
+ }
+ // password needs to be encrypted
+ else if ($name == "password") {
+ $user->password = md5($value);
+ }
+ // normal entry
+ else {
+ $user->{$name} = $value;
+ }
+ }
+ $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;
+ $courses = get_courses("all");
+ 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;
+ }
+ }
+ }
+ for ($i=0; $i<5; $i++) {
+ if ($addcourse[$i] && !$courseid[$i]) {
+ $notifytext .= "-1," . $addcourse[$i] . " unknown course<br \>\n";
+ }
+ }
+ if (! $user->id = insert_record("user", $user)) {
+ 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
+ $error_uid = -1;
+ if ($user = get_record("user","username",$username)) {
+ $error_uid = $user->id;
+ }
+ if ($error_uid != -1) {
+ $notifytext .= $error_uid . "," . $username . ",user not added - already registered";
+ } else {
+ $notifytext .= $error_uid . ",failed to add user " . $username . " unknown error";
+ }
+ }
+ } else if ($user->username != "changeme") {
+ $notifytext .= $user->id . "," . $user->username . ",";
+ $numusers++;
+ }
+ $lbreak = 1;
+ for ($i=0; $i<5; $i++) {
+ if ($courseid[$i]) {
+ if (enrol_student($user->id, $courseid[$i])) {
+ $lbreak = 0;
+ $notifytext .= ",enroled in course $addcourse[$i]<br \>\n";
+ } else {
+ $notifytext .= ",error: enrolment in course $addcourse[$i] failed<br \>\n";
+ }
+ }
+ }
+ if ($lbreak) {
+ $notifytext .= "<br \>\n";
+ }
+ unset ($user);
+ }
+ }
+ fclose($fp);
+ notify("$strusersnew: $numusers");
+
+ 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.'">'.
+ '<input type="file" name="userfile" size=30>'.
+ '<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;
+}
+
+?>