From 461af59ac18210fddc1397f5abad8ebd80049dbc Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Tue, 18 Sep 2007 07:00:57 +0000 Subject: [PATCH] MDL-11234, if role definitions matches, restore role mapping should be allowed with or without role assign capabilities --- backup/restore_form.html | 86 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/backup/restore_form.html b/backup/restore_form.html index 7a18c3029a..9c8c0806d2 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -516,6 +516,7 @@ $info = restore_read_xml_info($xml_file); // fix for MDL-9068, front page course is just a normal course $siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname"); $siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name"); +$allroles = get_records('role'); echo (''); echo (''); @@ -570,20 +571,33 @@ if ($info->backup_moodle_version < 2006092801) { } foreach ($roles->roles as $roleid=>$role) { + + $mappableroles = $siteroleschoicearray; + echo (''); } } @@ -603,3 +617,59 @@ echo ('
'.get_string('sourcerole').''.get_string('targetrole').'
'); echo $role->name." (".($role->shortname).")"; echo (''); - // see if any short name match - $matchrole = 0; - foreach ($siterolesarray as $siteroleid=>$siteroleshortname) { - if ($siteroleshortname == $role->shortname) { - $matchrole = $siteroleid; - break; + /// first, we see if any exact role definition is found + /// if found, that is the only option of restoring to + + if ($samerole = restore_samerole($roleid, $role)) { + $matchrole = $samerole->id; + // if an exact role is found, it does not matter whether this user can assign this role or not, + // this will be presented as a valid option regardless + $mappableroles[$samerole->id] = $allroles[$samerole->id]->name." (". $allroles[$samerole->id]->shortname.")"; + } else { + // no exact role found, let's try to match shortname + // this is useful in situations where basic roles differ slightly in definition + $matchrole = 0; + foreach ($siterolesarray as $siteroleid=>$siteroleshortname) { + if ($siteroleshortname == $role->shortname) { + $matchrole = $siteroleid; + break; + } } } - - choose_from_menu ($siteroleschoicearray, "roles_".$roleid, $matchrole, 'new role', '', '0'); + choose_from_menu ($mappableroles, "roles_".$roleid, $matchrole, 'new role', '', '0'); echo ('
'); // end of role mappings table + +prefix}role_capabilities + WHERE roleid = $roleid")) { + + if (restore_is_samerole($trole, $role)) { + $firstcap = array_shift($trole); + return get_record('role', 'id', $firstcap->roleid); + } + } + + // unfortunately then we have to loop all roles with the same number of capabiltiies + if ($proles = get_records_sql("SELECT roleid, roleid + FROM {$CFG->prefix}role_capabilities + GROUP BY roleid + HAVING COUNT(capability) = ".count($role->capabilities))) { + foreach ($proles as $proleid=>$proleobj) { + if ($prole = get_records('role_capabilities', 'roleid', $proleid)) { + if (restore_is_samerole($prole, $role)) { + return get_record('role', 'id', $proleid); + } + } + } + } + + return false; +} + +/* + * rolea from db, roleb from xml + */ +function restore_is_samerole($rolea, $roleb) { + + if (count($rolea) != count($roleb->capabilities)) { + return false; + } + + foreach ($rolea as $cap) { + if ($cap->permission != $roleb->capabilities[$cap->capability]->permission) { + return false; + } + } + return true; +} +?> \ No newline at end of file -- 2.39.5