"type3" => 1,
"type4" => 1,
"type5" => 1,
+ "role1" => 1,
+ "role2" => 1,
+ "role3" => 1,
+ "role4" => 1,
+ "role5" => 1,
"password" => $createpassword,
"oldusername" => $allowrenames);
$addtype[2] = $user->type3;
$addtype[3] = $user->type4;
$addtype[4] = $user->type5;
+ $addrole[0] = $user->role1;
+ $addrole[1] = $user->role2;
+ $addrole[2] = $user->role3;
+ $addrole[3] = $user->role4;
+ $addrole[4] = $user->role5;
for ($i=0; $i<5; $i++) {
$course[$i]=NULL;
}
for ($i=0; $i<5; $i++) { /// Enrol into courses if necessary
if ($course[$i]) {
- if (isset($addtype[$i])) {
+ if (isset($addrole[$i])) {
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $course[$i]->id);
+ if (!user_can_assign($coursecontext, $addrole[$i])) {
+ notify('--> Can not eroll into course'); //TODO: localize
+ }
+ $ret = role_assign($addrole[$i], $user->id, 0, $coursecontext->id);
+ } else if (isset($addtype[$i])) {
switch ($addtype[$i]) {
case 2: // teacher
$ret = add_teacher($user->id, $course[$i]->id, 1);
}
for ($i=0; $i<5; $i++) { // Add user to groups if necessary
if ($course[$i] && $groupid[$i]) {
- if (record_exists('user_students','userid',$user->id,'course',$course[$i]->id) ||
- record_exists('user_teachers','userid',$user->id,'course',$course[$i]->id)) {
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $course[$i]->id);
+ if (count(get_user_roles($coursecontext, $user->id))) {
if (add_user_to_group($groupid[$i], $user->id)) {
notify('-->' . get_string('addedtogroup','',$addgroup[$i]));
} else {
<p><strong>Default fieldnames:</strong> these are optional - if they are not included then the values are taken from the primary admin</p>
<p><font color="#990000" face="Courier New, Courier, mono">institution, department, city, country, lang, auth, timezone</font> </p>
<p><strong>Optional fieldnames: </strong>all of these are completely optional. The course names are the "shortnames" of the courses - if present then the user will be enrolled as students in those courses. Group names must be associated to the corresponding courses, i.e. group1 to course1, etc.</p>
- <p> <font color="#990000" face="Courier New, Courier, mono">idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, course1, course2, course3, course4, course5, group1, group2, group3, group4, group5, type1, type2, type3, type4, type5</font></p>
+ <p> <font color="#990000" face="Courier New, Courier, mono">idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, course1, course2, course3, course4, course5, group1, group2, group3, group4, group5, type1, type2, type3, type4, type5, role1, role2, role3, role4, role5</font></p>
</blockquote>
</li>
<li>Commas within the data should be encoded as &#44 - the script will automatically decode these back to commas. </li>
<li>For Boolean fields, use 0 for false and 1 for true. </li>
<li>Types are used to tell Moodle whether the user is a student or a teacher if a corresponding course exists (e.g. type2 corresponds to course2). 1 = Student, 2 = Editing Teacher, and 3 = Non-editing Teacher. If type is left blank, or if no course is specified, the user is default to student. </li>
- <li>For courses, use the short name for the course</li>
+ <li>For courses use the short name; for groups use group name; for roles use id.</li>
<li>Note: If a user is already registered in the Moodle user database, this script will return the
userid number (database index) for that user, and will enrol the user as a student in any of the
specified courses WITHOUT updating the other specified data.</li>