From 77022d9e5d36caac453ca15f96b4a1b6f5eba2a9 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 12 May 2008 23:07:42 +0000 Subject: [PATCH] Role custom names (aliases) are now preserved during backup/restore. MDL-14783 ; merged from 19_STABLE --- backup/backuplib.php | 7 +++++++ backup/restorelib.php | 35 +++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/backup/backuplib.php b/backup/backuplib.php index 56d1f2e5694..31183aa3bd0 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -642,12 +642,18 @@ $roles = backup_fetch_roles($preferences); $sitecontext = get_context_instance(CONTEXT_SYSTEM); + $coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course); foreach ($roles as $role) { fwrite ($bf,start_tag('ROLE',2,true)); fwrite ($bf,full_tag('ID', 3, false, $role->id)); fwrite ($bf,full_tag('NAME',3,false,$role->name)); fwrite ($bf,full_tag('SHORTNAME',3,false,$role->shortname)); + /// Calculate $role name in course + $nameincourse = role_get_name($role, $coursecontext); + if ($nameincourse != $role->name) { + fwrite ($bf,full_tag('NAMEINCOURSE', 3, false, $nameincourse)); + } // find and write all default capabilities fwrite ($bf,start_tag('CAPABILITIES',3,true)); // pull out all default (site context) capabilities @@ -657,6 +663,7 @@ fwrite ($bf,full_tag('NAME', 5, false, $capability)); fwrite ($bf,full_tag('PERMISSION', 5, false, $value)); // use this to pull out the other info (timemodified and modifierid) + $cap = get_record_sql("SELECT * FROM {$CFG->prefix}role_capabilities WHERE capability = '$capability' diff --git a/backup/restorelib.php b/backup/restorelib.php index 794c1ea501f..60a7ed727b3 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -4996,24 +4996,24 @@ define('RESTORE_GROUPS_GROUPINGS', 3); if ($this->tree[3] == "ROLE") { if ($this->level == 4) { switch ($tagName) { - case "NAME": - $this->info->tempname = $this->getContents(); - - break; - case "SHORTNAME": - $this->info->tempshortname = $this->getContents(); - break; case "ID": // this is the old id $this->info->tempid = $this->getContents(); + $this->info->roles[$this->info->tempid]->id = $this->info->tempid; + break; + case "NAME": + $this->info->roles[$this->info->tempid]->name = $this->getContents();; + break; + case "SHORTNAME": + $this->info->roles[$this->info->tempid]->shortname = $this->getContents();; + break; + case "NAMEINCOURSE": // custom name of the role in course + $this->info->roles[$this->info->tempid]->nameincourse = $this->getContents();; break; } } if ($this->level == 6) { switch ($tagName) { case "NAME": - $this->info->roles[$this->info->tempid]->name = $this->info->tempname; - $this->info->roles[$this->info->tempid]->shortname = $this->info->tempshortname; - $this->info->tempcapname = $this->getContents(); $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents(); break; @@ -8288,6 +8288,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); if (isset($info->roles) && $info->roles) { foreach ($info->roles as $oldroleid=>$roledata) { + if (empty($restore->rolesmapping)) { // if this is empty altogether, we came from import or there's no roles used in course at all // in this case, write the same oldid as this is the same site @@ -8346,6 +8347,20 @@ define('RESTORE_GROUPS_GROUPINGS', 3); insert_record('role_capabilities', $roleinfo); } } + /// Now, restore role nameincourse + $newrole = backup_getid($restore->backup_unique_code, 'role', $oldroleid); /// Look for target role + $coursecontext = get_context_instance(CONTEXT_COURSE, $restore->course_id); /// Look for target context + if (!empty($newrole->new_id) && !empty($coursecontext)) { + /// Check the role hasn't any custom name in context + if (!record_exists('role_names', 'roleid', $newrole->new_id, 'contextid', $coursecontext->id)) { + $rolename = new object(); + $rolename->roleid = $newrole->new_id; + $rolename->contextid = $coursecontext->id; + $rolename->name = addslashes($roledata->nameincourse); + + insert_record('role_names', $rolename); + } + } } } return true;