]> git.mjollnir.org Git - moodle.git/commitdiff
roles and capability support for backups. role mapping is in, please help test, thanks
authortoyomoyo <toyomoyo>
Mon, 2 Oct 2006 08:49:07 +0000 (08:49 +0000)
committertoyomoyo <toyomoyo>
Mon, 2 Oct 2006 08:49:07 +0000 (08:49 +0000)
backup/restore_check.html
backup/restore_form.html
backup/restorelib.php
lang/en_utf8/moodle.php
lib/accesslib.php

index 1f75e280e41d736261e888b34f228014658ab6f3..d18691835e9783edf8049aa78a95e40b167ec8d3 100644 (file)
         //add new vars to restore object 
         $restore->course_startdateoffset = $coursestartdatedateoffset;
         $restore->course_shortname = $currentcourseshortname;
-
+        
+        // create role mappings, not sure all should be here
+        if ($data2 = data_submitted()) {
+            foreach ($data2 as $tempname=>$tempdata) {
+                if (strstr($tempname, 'roles_')) {
+                    $temprole = explode('_', $tempname);
+                    $oldroleid = $temprole[1];
+                    $newroleid = $tempdata;
+                    $restore->rolesmapping[$oldroleid] = $newroleid;
+                }                     
+            }       
+        }        
+                
     } else {
         //We have the object, so check if we have a new course_id
         //passed as parammeter
         if ($course_id) {
             $restore->course_id=$course_id;
         }
-    }
+    } 
     //We have the object with data, put it in the session
     $SESSION->restore = $restore;
 
index ade43092687079dfab57fcd1d55fb266f3f6619c..20a82b99211a4b9210ff1de4f118dae3de96d0bd 100644 (file)
@@ -413,6 +413,45 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
     }
 ?>
 </table>
+
+<hr/>
+<?php
+
+print_heading(get_string('rolemappings'));
+$xml_file  = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
+
+
+$roles = restore_read_xml_roles($xml_file);
+
+$siteroles = get_records('role');
+$siterolesarray = array();
+foreach ($siteroles as $siterole) {
+    $siterolesarray[$siterole->id] = $siterole->shortname;  
+}
+
+
+echo ('<table width="100%" class="restore-form-instances">');
+echo ('<tr><td align="right"><b>'.get_string('sourcerole').'</b></td><td align="left"><b>'.get_string('targetrole').'</b></td></tr>');
+
+foreach ($roles->roles as $roleid=>$role) {
+    echo ('<tr><td align="right">');
+    echo $role->shortname;
+    echo ('</td><td align="left">');
+    
+    // see if any short name match
+    $matchrole = 0;
+    foreach ($siteroles as $siterole) {
+        if ($siterole->shortname == $role->shortname) {
+            $matchrole = $siterole->id;
+            break;  
+        }  
+    }
+    
+    choose_from_menu ($siterolesarray, "roles_".$roleid, $matchrole, 'new role', '', '0');
+    echo ('</td></tr>'); 
+}
+echo ('</table>');
+?>
 <br />
 <center>
 <input type="hidden" name="id"     value="<?php  p($id) ?>" />
index df6c183675015b6c5c1dabe6004b09fd097f446f..7d197c878df0f4199bf0f20deeda643a0ade02d2 100644 (file)
         return $info;
     }
 
-    function restore_read_xml_roles ($restore,$xml_file) {
+    function restore_read_xml_roles ($xml_file) {
         //We call the main read_xml function, with todo = ROLES
-        $info = restore_read_xml ($xml_file,"ROLES",$restore);
+        $info = restore_read_xml ($xml_file,"ROLES",false);
 
         return $info;  
     }
                             break;
                     }
                 }
+                
+                
                 if ($this->level == 7 && $this->tree[5]!="ROLE_ASSIGNMENTS" && $this->tree[5]!="ROLE_OVERRIDES") {
                     switch ($tagName) {
                         case "TYPE":
                             break;
                         default:
                             break;
+                    
                     }
                 }
-                
-                
-                
-                
+                                           
                 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
 
                     if ($this->level == 7) {
                         }
                     }
                 } /// ends role_overrides                                                          
-                
-               
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
-                
+                           
             } // closes if this->tree[3]=="users"
 
             //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
      * It isn't now, just overwriting
      */
     function restore_create_roles($restore, $xmlfile) {
-        
-        $info = restore_read_xml_roles($restore, $xmlfile);
+        if (!defined('RESTORE_SILENTLY')) {
+            echo "<li>".get_string("creatingrolesdefinitions").'</li>';
+        }
+        $info = restore_read_xml_roles($xmlfile);
 
         $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
 
         // but we could use more intelligent detection, and role mapping
         // get role mapping info from $restore
 
+        $rolemappings = $restore->rolesmapping; // an array
+
         foreach ($info->roles as $oldroleid=>$roledata) {
             
-            // code to make new role name/short name if same role name or shortname exists
-            $fullname = $roledata->name;
-            $shortname = $roledata->shortname;
-            $currentfullname = "";
-            $currentshortname = "";
-            $counter = 0;
+            /// first we check if the roles are in the mappings
+            // if so, we just do a mapping i.e. update oldids table
+            if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
+                $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
+                                     $rolemappings[$oldroleid]); // adding a new id     
+              
+            } else {
+            
+                // code to make new role name/short name if same role name or shortname exists
+                $fullname = $roledata->name;
+                $shortname = $roledata->shortname;
+                $currentfullname = "";
+                $currentshortname = "";
+                $counter = 0;
                       
-            do {
-                if ($counter) {
-                    $suffixfull = " ".get_string("copyasnoun")." ".$counter;
-                    $suffixshort = "_".$counter;
-                } else {
-                    $suffixfull = "";
-                    $suffixshort = "";
-                }
-                $currentfullname = $fullname.$suffixfull;
-                // Limit the size of shortname - database column accepts <= 15 chars
-                $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
-                $coursefull  = get_record("role","name",addslashes($currentfullname));
-                $courseshort = get_record("role","shortname",addslashes($currentshortname));
-                $counter++;
-            } while ($coursefull || $courseshort);
-
-            $roledata->name = $currentfullname;
-            $roledata->shortname= $currentshortname;           
+                do {
+                    if ($counter) {
+                        $suffixfull = " ".get_string("copyasnoun")." ".$counter;
+                        $suffixshort = "_".$counter;
+                    } else {
+                        $suffixfull = "";
+                        $suffixshort = "";
+                    }
+                    $currentfullname = $fullname.$suffixfull;
+                    // Limit the size of shortname - database column accepts <= 15 chars
+                    $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
+                    $coursefull  = get_record("role","name",addslashes($currentfullname));
+                    $courseshort = get_record("role","shortname",addslashes($currentshortname));
+                    $counter++;
+                } while ($coursefull || $courseshort);
+
+                $roledata->name = $currentfullname;
+                $roledata->shortname= $currentshortname;           
             
-            // done finding a unique name
+                // done finding a unique name
             
-            $newroleid = create_role($roledata->name,$roledata->shortname,'');
-            $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
+                $newroleid = create_role($roledata->name,$roledata->shortname,'');
+                $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
                                      $newroleid); // adding a new id
-            foreach ($roledata->capabilities as $capability) {
+                foreach ($roledata->capabilities as $capability) {
                 
-                $roleinfo = new object();
-                $roleinfo = (object)$capability;
-                $roleinfo->contextid = $sitecontext->id;
-                $roleinfo->capability = $capability->name;
-                $roleinfo->roleid = $newroleid;
+                    $roleinfo = new object();
+                    $roleinfo = (object)$capability;
+                    $roleinfo->contextid = $sitecontext->id;
+                    $roleinfo->capability = $capability->name;
+                    $roleinfo->roleid = $newroleid;
                 
-                insert_record('role_capabilities', $roleinfo);
+                    insert_record('role_capabilities', $roleinfo);
+                }
             }
         }
     }
         // data pulls from course, mod, user, and blocks
         
         /*******************************************************
-         * Restoring assignments from course level assignments *
+         * Restoring from course level assignments *
          *******************************************************/
+        if (!defined('RESTORE_SILENTLY')) {
+            echo "<li>".get_string("creatingcourseroles").'</li>';
+        }
         $course = restore_read_xml_course_header($xmlfile);
         $courseassignments = $course->roleassignments;
 
         foreach ($courseassignments as $oldroleid => $courseassignment) {          
             restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
         }
-         
+
         /*****************************************************
-         * Restoring assignments from course level overrides *
-         *****************************************************/       
+         * Restoring from course level overrides *
+         *****************************************************/     
         $courseoverrides = $course->roleoverrides;
+        $rolemappings = $restore->rolesmapping;
         foreach ($courseoverrides as $oldroleid => $courseoverride) {
-            restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
+            
+            // if not importing into exiting course, or creating new role, we are ok
+            // local course overrides to be respected (i.e. restored course overrides ignored)
+            if ($restore->restoreto != 1 || empty($rolemappings[$oldroleid])) {
+                restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
+            }
         }
-        
+      
         /*******************************************************
          * Restoring role assignments/overrdies                *
          * from module level assignments                       *
          *******************************************************/     
+        
+        if (!defined('RESTORE_SILENTLY')) {
+            echo "<li>".get_string("creatingmodroles").'</li>';
+        }
         $sections = restore_read_xml_sections($xmlfile);
         $secs = $sections->sections;
          
          * Restoring assignments from blocks level       *
          * role assignments/overrides                    *
          *************************************************/ 
-        $blocks = restore_read_xml_blocks($xmlfile);
-        if (isset($blocks->instances)) {
-            foreach ($blocks->instances as $instance) {
-                if (isset($instance->roleassignments)) {
-                    foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
-                        restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
+
+        if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
+            if (!defined('RESTORE_SILENTLY')) {
+                echo "<li>".get_string("creatingblocksroles").'</li>';
+            }
+            $blocks = restore_read_xml_blocks($xmlfile);
+            if (isset($blocks->instances)) {
+                foreach ($blocks->instances as $instance) {
+                    if (isset($instance->roleassignments)) {
+                        foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
+                            restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
                       
+                        }
                     }
-                }
-                if (isset($instance->roleoverrides)) {
-                    foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
-                        restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
+                    if (isset($instance->roleoverrides)) {
+                        foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
+                            restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
+                        }
                     }
                 }
             }
          * Restoring assignments from userid level      *
          * role assignments/overrides                   *
          ************************************************/     
-
+        if (!defined('RESTORE_SILENTLY')) {
+            echo "<li>".get_string("creatinguserroles").'</li>';
+        }
         $info = restore_read_xml_users($restore, $xmlfile);
         if (!empty($info->users)) {
             //For each user, take its info from backup_ids
             $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
             $assignment->modifierid = $oldmodifier->new_id?$oldmodifier->new_id:0; // new modifier id here
             $assignment->roleid = $role->new_id; // restored new role id
-            $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
+
+            // hack to make the correct contextid for course level imports
+            if ($contextlevel == CONTEXT_COURSE) {
+                $oldinstance->new_id = $restore->course_id;
+            } else {
+                $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
+            }
+
             $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
             $assignment->contextid = $newcontext->id; // new context id
-                         
-            insert_record('role_assignments', $assignment);   
+            // might already have same assignment           
+            
+            role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol);
+            
         }  
     }
     
             $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
             $override->modifierid = $oldmodifier->new_id?$oldmodifier->new_id:0; // new modifier id here
             $override->roleid = $role->new_id; // restored new role id
-            $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
+            
+            // hack to make the correct contextid for course level imports
+            if ($contextlevel == CONTEXT_COURSE) {
+                $oldinstance->new_id = $restore->course_id;
+            } else {
+                $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
+            }
+            
             $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
             $override->contextid = $newcontext->id; // new context id                 
-            insert_record('role_capabilities', $override);   
+            // might already have same override
+            if (!get_record('role_capabilities', 'capability', $override->capability, 'roleid', $override->roleid, 'contextid', $override->contextid)) {
+                insert_record('role_capabilities', $override);
+            }
         }  
     }  
 ?>
\ No newline at end of file
index 40eb49b91cc434489df353d648497988ce0ade56..3275d60e5ddebfed0b416a4423d0ae863e4d331d 100644 (file)
@@ -278,19 +278,24 @@ $string['createfolder'] = 'Create a folder in $a';
 $string['createuserandpass'] = 'Create a new username and password to log in with';
 $string['createziparchive'] = 'Create zip archive';
 $string['creatingblocks'] = 'Creating blocks';
+$string['creatingblocksroles'] = 'Creating block level role assignments and overrides';
 $string['creatingcategoriesandquestions'] = 'Creating categories and questions';
 $string['creatingcoursemodules'] = 'Creating course modules';
+$string['creatingcourseroles'] = 'Creating course level role assignments and overrides';
 $string['creatingevents'] = 'Creating events';
 $string['creatinggradebook'] = 'Creating gradebook';
 $string['creatinggroups'] = 'Creating groups';
 $string['creatinglogentries'] = 'Creating log entries';
 $string['creatingmessagesinfo'] = 'Creating messages info';
 $string['creatingmetacoursedata'] = 'Creating metacourse info';
+$string['creatingmodroles'] = 'Creating module level role assignments and overrides';
 $string['creatingnewcourse'] = 'Creating new course';
+$string['creatingrolesdefinitions'] = 'Creating roles definitions';
 $string['creatingscales'] = 'Creating scales';
 $string['creatingsections'] = 'Creating sections';
 $string['creatingtemporarystructures'] = 'Creating temporary structures';
 $string['creatingusers'] = 'Creating users';
+$string['creatinguserroles'] = 'Creating user level role assignments and overrides';
 $string['creatingxmlfile'] = 'Creating XML file';
 $string['currency'] = 'Currency';
 $string['currentcourseadding'] = 'Current course, adding data to it';
@@ -1117,6 +1122,7 @@ $string['restrictmodules'] = 'Restrict activity modules?';
 $string['returningtosite'] = 'Returning to this web site?';
 $string['revert'] = 'Revert';
 $string['role'] = 'Role';
+$string['rolemappings'] = 'Role mappings';
 $string['roles'] = 'Roles';
 $string['rss'] = 'RSS';
 $string['rssarticles'] = 'Number of RSS recent articles';
@@ -1211,6 +1217,7 @@ $string['socialheadline'] = 'Social forum - latest topics';
 $string['someallowguest'] = 'Some courses may allow guest access';
 $string['someerrorswerefound'] = 'Some information was missing or incorrect. Look below for details.';
 $string['sortby'] = 'Sort by';
+$string['sourcerole'] = 'Source role';
 $string['specifyname'] = 'You must specify a name.';
 $string['startdate'] = 'Course start date';
 $string['starpending'] = '([*] = course pending approval)';
@@ -1286,6 +1293,7 @@ $string['switchroleto'] = 'Switch role to...';
 $string['tag'] = 'Tag';
 $string['tagmanagement'] = 'Add/delete tags ...';
 $string['tags'] = 'Tags';
+$string['targetrole'] = 'Target role';
 $string['teacheronly'] = 'for the $a only';
 $string['teacherroles'] = '$a roles';
 $string['teachers'] = 'Teachers';
index 8a55d7766ee605b5ba89b9d2513ec4bf79fe122b..a9a241889c737e98f364b5caad018acb5e4cd472 100755 (executable)
@@ -2081,7 +2081,9 @@ function fetch_context_capabilities($context) {
         break;
 
         case CONTEXT_USER:
-            $SQL = "select * from {$CFG->prefix}capabilities where contextlevel = ".CONTEXT_USER;
+            $SQL = "SELECT * 
+                    FROM {$CFG->prefix}capabilities 
+                    WHERE contextlevel = ".CONTEXT_USER;
         break;
 
         case CONTEXT_COURSECAT: // all