]> git.mjollnir.org Git - moodle.git/commitdiff
adding support for roles in backups
authortoyomoyo <toyomoyo>
Wed, 27 Sep 2006 08:59:27 +0000 (08:59 +0000)
committertoyomoyo <toyomoyo>
Wed, 27 Sep 2006 08:59:27 +0000 (08:59 +0000)
backup/backuplib.php
backup/restorelib.php
mod/assignment/restorelib.php

index fb259a1d74d1fe16b5184ffd3eaf178da26599b6..0f5f46d701cf1e0c65b1196547eeaca52db48540 100644 (file)
                 //Iterate over every block
                 foreach ($instances as $position) {
                     foreach ($position as $instance) {
+                      
                         //If we somehow have a block with an invalid id, skip it
                         if(empty($blocks[$instance->blockid]->name)) {
                             continue;
                         //Begin Block
                         
                         fwrite ($bf,start_tag('BLOCK',3,true));
+                        fwrite ($bf,full_tag('ID', 4, false,$instance->id));
                         fwrite ($bf,full_tag('NAME',4,false,$blocks[$instance->blockid]->name));
                         fwrite ($bf,full_tag('PAGEID',4,false,$instance->pageid));
                         fwrite ($bf,full_tag('PAGETYPE',4,false,$instance->pagetype));
      */ 
     function backup_fetch_roles($preferences) {
 
+        global $CFG;
         $contexts = array();
         $roles = array();
         
             }
         }
         
+        // add all roles assigned at user context
+        if ($preferences->backup_users) {
+            if ($users = get_records_sql("SELECT u.old_id, u.table_name,u.info
+                                            FROM {$CFG->prefix}backup_ids u
+                                            WHERE u.backup_code = '$preferences->backup_unique_code' AND
+                                            u.table_name = 'user'")) {                   
+                foreach ($users as $user) {
+                    $context = get_context_instance(CONTEXT_USER, $user->old_id);
+                    $contexts[$context->id] = $context; 
+                }                
+            }
+          
+        }
+
+        // add all roles assigned at block context
+        if ($courseblocks = get_records_sql("SELECT * 
+                                             FROM {$CFG->prefix}block_instance
+                                             WHERE pagetype = '".PAGE_COURSE_VIEW."'
+                                                   AND pageid = {$preferences->backup_course}")) {
+        
+            foreach ($courseblocks as $courseblock) {
+               
+                $context = get_context_instance(CONTEXT_COURSE, $courseblock->id);
+                 echo "<br/>adding $context->id";
+                 $contexts[$context->id] = $context;     
+            }                                         
+        }     
+        
         // foreach context, call get_roles_on_exact_context insert into array
         foreach ($contexts as $context) {
             if ($proles = get_roles_on_exact_context($context)) {
index ade2194e59379408fdf56e32b41270eb38f991a4..ff0d22a12392a384b2bb31e3a15f9655b9e7e9fa 100644 (file)
                     //Add this instance
                     $instance->blockid = $blocks[$instance->name]->id;
                     
-                    if(!insert_record('block_instance', $instance)) {
+                    if($newid = insert_record('block_instance', $instance)) {
+                        backup_putid ($restore->backup_unique_code,"block_instance",$instance->id,$newid);
+                    } else {
                         $status = false;
-                        break;
+                        break;                      
                     }
                     
                     //Get an object for the block and tell it it's been restored so it can update dates
                 }
             }
         }
-
+        
         return $status;
     }
 
                 }
                 if ($this->level == 5) {
                     switch ($tagName) {
+                        case 'ID':
+                            $this->info->tempinstance->id = $this->getContents();
                         case 'NAME':
                             $this->info->tempinstance->name = $this->getContents();
                             break;
                             break;
                     }
                 } 
-                //echo "<br/>this tree 5 is".$this->tree[5];
-//print_object($this);
+
                 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
                     if ($this->level == 7) {
                         switch ($tagName) {
                     }
                     
                     if ($this->level == 9) {
-                        echo "<br/>tagname is $tagName";
+
                         switch ($tagName) {
                             case "USERID":
                                 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
         
         //This is the endTag handler we use where we are reading the users zone (todo="USERS")
         function endElementUsers($parser, $tagName) {
+            global $CFG;  
             //Check if we are into USERS zone
             if ($this->tree[3] == "USERS") {
                 //if (trim($this->content))                                                                     //Debug
                         case "USER":
                             //Increment counter
                             $this->counter++;
-                            //Save to db
+                            //Save to db, only save if record not already exist    
+                            // if there already is an new_id for this entry, just use that new_id?
+                            $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
+                            if (isset($newuser->new_id)) {
+                                $newid = $newuser->new_id;
+                            } else {
+                                $newid = null;  
+                            }
+                            
                             backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
-                                          null,$this->info->tempuser);
-
+                                            $newid,$this->info->tempuser);
+                            
                             //Do some output   
                             if ($this->counter % 10 == 0) {
                                 if (!defined('RESTORE_SILENTLY')) {
                         case "TIMEMODIFIED": 
                             $this->info->tempuser->timemodified = $this->getContents();
                             break;
+                        default:
+                            break;
                     }
                 }
-                if ($this->level == 6) {
+                if ($this->level == 6 && $this->tree[5]!="ROLE_ASSIGNMENTS" && $this->tree[5]!="ROLE_OVERRIDES") {
                     switch ($tagName) {
                         case "ROLE":
                             //We've finalized a role, get it
                             break;
                     }
                 }
-                if ($this->level == 7) {
+                if ($this->level == 7 && $this->tree[5]!="ROLE_ASSIGNMENTS" && $this->tree[5]!="ROLE_OVERRIDES") {
                     switch ($tagName) {
                         case "TYPE":
                             $this->info->temprole->type = $this->getContents();
                         case "VALUE":
                             $this->info->tempuserpreference->value = $this->getContents();
                             break;
+                        default:
+                            break;
                     }
                 }
-            }
+                
+                
+                
+                
+                if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
+
+                    if ($this->level == 7) {
+                        switch ($tagName) {
+                            case "NAME":
+                                $this->info->tempname = $this->getContents();
+                            break;
+                            case "SHORTNAME":
+                                $this->info->tempshortname = $this->getContents();
+                            break;
+                            case "ID":
+                                $this->info->tempid = $this->getContents(); // temp roleid
+                            break;
+                        }
+                    }
+                    
+                    if ($this->level == 9) {
+
+                        switch ($tagName) {
+                            case "USERID":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
+                                
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
+                                
+                                $this->info->tempuserid = $this->getContents();
+                                
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
+                            break;
+                            case "HIDDEN":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
+                            break;
+                            case "TIMESTART":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
+                            break;
+                            case "TIMEEND":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
+                            break;
+                            case "TIMEMODIFIED":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
+                            break;
+                            case "MODIFIERID":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
+                            break;
+                            case "ENROL":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
+                            break;
+                            case "SORTORDER":
+                                $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
+                            break;
+                        
+                        }
+                    }
+                } /// ends role_assignments
+                
+                if ($this->tree[5] == "ROLES_OVERRIDES") {
+                    if ($this->level == 7) {
+                        switch ($tagName) {
+                            case "NAME":
+                                $this->info->tempname = $this->getContents();
+                            break;
+                            case "SHORTNAME":
+                                $this->info->tempshortname = $this->getContents();
+                            break;                            
+                            case "ID":
+                                $this->info->tempid = $this->getContents(); // temp roleid
+                            break;
+                        }      
+                    }
+                    
+                    if ($this->level == 9) {
+                        switch ($tagName) {
+                            case "NAME":
+                            
+                                $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
+                                $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
+                                $this->info->tempname = $this->getContents(); // change to name of capability
+                                $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
+                            break;
+                            case "PERMISSION":
+                                $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
+                            break;
+                            case "TIMEMODIFIED":
+                                $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
+                            break;
+                            case "MODIFIERID":
+                                $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
+                            break;
+                        }
+                    }
+                } /// ends role_overrides                                                          
+                
+               
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+                
+            } // closes if this->tree[3]=="users"
 
             //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
             //Speed up a lot (avoid parse all)
                 $this->finished = true;
                 $this->counter = 0;
             }
-
+            
             //Clear things
             $this->tree[$this->level] = "";
             $this->level--;
                     return false;
                 }
             }
+            
             //Now print info about the work done
             if ($status) {
                 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
         $courseassignments = $course->roleassignments;
 
         foreach ($courseassignments as $oldroleid => $courseassignment) {
-            restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
+            //restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
         }  
+        
         /*******************************************************
          * Restoring assignments from module level assignments *
          *******************************************************/     
                 }
             }
         }
-         //print_object($sections);
+        
         /*******************************************************
          * Restoring assignments from blocks level assignments *
          *******************************************************/ 
         $blocks = restore_read_xml_blocks($xmlfile);
-        print_object($blocks);  
+        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);                  
+                      
+                    }      
+                }    
+            }          
+        }
         /*******************************************************
          * Restoring assignments from userid level assignments *
-         *******************************************************/
+         *******************************************************/     
+
+        $info = restore_read_xml_users($restore, $xmlfile);
+        if (!empty($info->users)) {
+            //For each user, take its info from backup_ids
+            foreach ($info->users as $userid) {
+                $rec = backup_getid($restore->backup_unique_code,"user",$userid);   
+                if (isset($rec->info->roleassignments)) {
+                    foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
+                       restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);  
+                    }
+                }
+            }
+        }
     }
     
     function restore_override_roles($restore, $xmlfile) {
     
     // auxillary function to write role assignments read from xml to db
     function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
-        
+        echo "context level is $contextlevel";
         $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
         
         foreach ($assignments as $assignment) {
-                
             $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
             $assignment->userid = $olduser->new_id; // new userid here
             $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
             // new course id
             $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
             $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
-            $assignment->contextid = $newcontext->id;// new context id                
-
+            $assignment->contextid = $newcontext->id;// new context id                 
             insert_record('role_assignments', $assignment);   
         }  
-  
-    }
-    
-    
-    
-    
-    
+    }  
 ?>
\ No newline at end of file
index 4cc0412cf7659461504da5dfe759d9181b609fc5..3307f95d78ae6743b893d21eab680da706e216b8 100644 (file)
                 $submission->submissioncomment = backup_todb($sub_info['#']['COMMENT']['0']['#']);
             } else {
                 $submission->submissioncomment = backup_todb($sub_info['#']['SUBMISSIONCOMMENT']['0']['#']);
-            }
+            }  
             $submission->format = backup_todb($sub_info['#']['FORMAT']['0']['#']);
             $submission->teacher = backup_todb($sub_info['#']['TEACHER']['0']['#']);
             $submission->timemarked = backup_todb($sub_info['#']['TIMEMARKED']['0']['#']);