diff -Naur moodle.orig/admin/tool/uploaduser/index.php moodle/admin/tool/uploaduser/index.php --- moodle.orig/admin/tool/uploaduser/index.php 2015-04-03 06:45:27.000000000 +0100 +++ moodle/admin/tool/uploaduser/index.php 2015-04-03 06:46:37.000000000 +0100 @@ -68,6 +68,9 @@ $strcannotassignrole = get_string('cannotassignrole', 'error'); +$strassignedsysrole = get_string('eventroleassigned', 'role') . ' [System,'; +$strunassignedsysrole = get_string('eventroleunassigned', 'role') . ' [System,'; + $struserauthunsupported = get_string('userauthunsupported', 'error'); $stremailduplicate = get_string('useremailduplicate', 'error'); @@ -188,6 +191,7 @@ $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway! $cohorts = array(); $rolecache = uu_allowed_roles_cache(); // roles lookup cache + $sysrolecache = uu_allowed_sysroles_cache(); // roles lookup cache $manualcache = array(); // cache of used manual enrol plugins in each course $supportedauths = uu_supported_auths(); // officially supported plugins that are enabled @@ -889,6 +893,44 @@ // find course enrolments, groups, roles/types and enrol periods // this is again a special case, we always do this for any updated or created users foreach ($filecolumns as $column) { + if (preg_match('/^sysrole\d+$/', $column)) { + $i = substr($column, 7); + + if (!empty($user->{'sysrole'.$i})) { + $addrole = $user->{'sysrole'.$i}; + if ($addrole[0]=='-') + { + $sub = TRUE; + $addrole = substr($addrole,1); + } + else + $sub = FALSE; + + if (array_key_exists($addrole, $sysrolecache)) { + $rid = $sysrolecache[$addrole]->id; + } else { + $upt->track('enrolments', get_string('unknownrole', 'error', s($addrole)), 'error'); + continue; + } + + if ($sub) { + if (user_has_role_assignment($user->id, $rid, SYSCONTEXTID)) { + role_unassign($rid, $user->id, SYSCONTEXTID); + // Bit of a cheat to avoid need for more localised strings: + $upt->track('enrolments', $strunassignedsysrole . $sysrolecache[$rid]->name . ']'); + } + } else { + if (!user_has_role_assignment($user->id, $rid, SYSCONTEXTID)) { + role_assign($rid, $user->id, SYSCONTEXTID); + $upt->track('enrolments', $strassignedsysrole . $sysrolecache[$rid]->name . ']'); + } + } + + } + + continue; + + } if (!preg_match('/^course\d+$/', $column)) { continue; } diff -Naur moodle.orig/admin/tool/uploaduser/locallib.php moodle/admin/tool/uploaduser/locallib.php --- moodle.orig/admin/tool/uploaduser/locallib.php 2015-04-03 06:45:27.000000000 +0100 +++ moodle/admin/tool/uploaduser/locallib.php 2015-04-03 06:55:14.000000000 +0100 @@ -196,7 +196,7 @@ // hack: somebody wrote uppercase in csv file, but the system knows only lowercase profile field $newfield = $lcfield; - } else if (preg_match('/^(cohort|course|group|type|role|enrolperiod|enrolstatus)\d+$/', $lcfield)) { + } else if (preg_match('/^(sysrole|cohort|course|group|type|role|enrolperiod|enrolstatus)\d+$/', $lcfield)) { // special fields for enrolments $newfield = $lcfield; @@ -366,6 +366,25 @@ } /** + * Returns mapping of all system roles using short role name as index. + * @return array + */ +function uu_allowed_sysroles_cache() { + $allowedroles = get_assignable_roles(context_system::instance(), ROLENAME_SHORT); + foreach ($allowedroles as $rid=>$rname) { + $rolecache[$rid] = new stdClass(); + $rolecache[$rid]->id = $rid; + $rolecache[$rid]->name = $rname; + if (!is_numeric($rname)) { // only non-numeric shortnames are supported!!! + $rolecache[$rname] = new stdClass(); + $rolecache[$rname]->id = $rid; + $rolecache[$rname]->name = $rname; + } + } + return $rolecache; +} + +/** * Pre process custom profile data, and update it with corrected value * * @param stdClass $data user profile data @@ -422,4 +441,4 @@ } } return $noerror; -} \ No newline at end of file +}