]> git.mjollnir.org Git - moodle.git/commitdiff
Role custom names (aliases) are now preserved during backup/restore. MDL-14783 ;...
authorstronk7 <stronk7>
Mon, 12 May 2008 23:07:42 +0000 (23:07 +0000)
committerstronk7 <stronk7>
Mon, 12 May 2008 23:07:42 +0000 (23:07 +0000)
backup/backuplib.php
backup/restorelib.php

index 56d1f2e569433e8a4efe89e8532f4f06abd64024..31183aa3bd0ef64ba62723d6de6b6e219b444e14 100644 (file)
         $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
                     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'
index 794c1ea501fb255c8ba8c547729c5b4baea317d6..60a7ed727b30ff28ac5e0758c9dce44992befbd5 100644 (file)
@@ -4996,24 +4996,24 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                 if ($this->tree[3] == "ROLE") {
                     if ($this->level == 4) {
                         switch ($tagName) {
+                            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->tempname = $this->getContents();
-
+                                $this->info->roles[$this->info->tempid]->name = $this->getContents();;
                                 break;
                             case "SHORTNAME":
-                                $this->info->tempshortname = $this->getContents();
+                                $this->info->roles[$this->info->tempid]->shortname = $this->getContents();;
                                 break;
-                            case "ID": // this is the old id
-                                $this->info->tempid = $this->getContents();
+                            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;