]> git.mjollnir.org Git - moodle.git/commitdiff
adding support for 1.6->1.7 backups
authortoyomoyo <toyomoyo>
Tue, 3 Oct 2006 08:54:46 +0000 (08:54 +0000)
committertoyomoyo <toyomoyo>
Tue, 3 Oct 2006 08:54:46 +0000 (08:54 +0000)
backup/backuplib.php
backup/restore_check.html
backup/restore_form.html
backup/restorelib.php
lib/accesslib.php

index c7ded7a0c3a06fd52390e4af05a8cd708a02cd80..2ad2d577cb80a742aff880f0554ef8b06315caef 100644 (file)
                 //Iterate over users putting their roles
                 foreach ($backupable_users as $backupable_user) {
                     $backupable_user->info = "";
-                    /*
-                    // writing all the applicable role assignments
-                    if ($userroles = get_records_sql("SELECT DISTINCT r.* 
-                                                  FROM {$CFG->prefix}role_assignments ra,
-                                                       {$CFG->prefix}role r
-                                                  WHERE ra.userid = $backupable_user->id
-                                                        AND r.id = ra.roleid
-                                                        AND ra.contextid = $context->id")) {
-                                                                    
-                        foreach ($userroles as $userrole) {
-                            $backupable_user->info .= $userrole->shortname.",";  
-                        }
-                    }
-                    */
-                    /*
-                    if (record_exists("user_admins","userid",$backupable_user->id)) {
-                        $backupable_user->info .= "admin";
-                    }
-                    //Is Course Creator in tables (not is_coursecreator()) !!
-                    if (record_exists("user_coursecreators","userid",$backupable_user->id)) {
-                        $backupable_user->info .= "coursecreator";
-                    }
-                    //Is Teacher in tables (not is_teacher()) !!
-                    if (record_exists("user_teachers","course",$course,"userid",$backupable_user->id)) {
-                        $backupable_user->info .= "teacher";
-                    }
-                    //Is Student in tables (not is_student()) !!
-                    if (record_exists("user_students","course",$course,"userid",$backupable_user->id)) {
-                        $backupable_user->info .= "student";
-                    }
-                    */
-                    
+                                     
                     //Is needed user (exists in needed_users) 
                     if (isset($needed_users[$backupable_user->id])) {
                         $backupable_user->info .= "needed";
     function backup_get_enrolled_users ($courseid) {
 
         global $CFG;
-
-        $result = false;
               
         // get all users with moodle/course:view capability, this will include people
         // assigned at cat level, or site level
         // but it should be ok if they have no direct assignment at course, mod, block level
         return get_users_by_capability(get_context_instance(CONTEXT_COURSE, $courseid), 'moodle/course:view');
-        /*
-        //Get teachers
-        $teachers = get_records_sql("SELECT DISTINCT userid,userid
-                     FROM {$CFG->prefix}user_teachers
-                     WHERE course = '$courseid'");
-        //Get students
-        $students = get_records_sql("SELECT DISTINCT userid,userid
-                     FROM {$CFG->prefix}user_students
-                     WHERE course = '$courseid'");
-        //Add teachers
-        if ($teachers) {
-            foreach ($teachers as $teacher) {
-                $result[$teacher->userid]->id = $teacher->userid;
-            }
-        }
-        //Add students
-        if ($students) {
-            foreach ($students as $student) {
-                $result[$student->userid]->id = $student->userid;
-            }
-        }
-
-        return $result; 
-        */
     }
 
     //Returns all users (every record in users table)
index d18691835e9783edf8049aa78a95e40b167ec8d3..a45b41966c43b044bd29883815aaba7a0672cb93 100644 (file)
                     $restore->rolesmapping[$oldroleid] = $newroleid;
                 }                     
             }       
-        }        
+        }
+        
+        // default role mapping for moodle < 1.7
+        if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
+            $restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit; 
+        }
+        if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
+            $restore->rolesmapping['defaultteacher'] = $defaultteacher; 
+        }
+        if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
+            $restore->rolesmapping['defaultstudent'] = $defaultstudent; 
+        }
                 
     } else {
         //We have the object, so check if we have a new course_id
index 20a82b99211a4b9210ff1de4f118dae3de96d0bd..8e50ac97efdf41f43cdc665dfb145175bd3b2c6f 100644 (file)
@@ -420,37 +420,80 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
 print_heading(get_string('rolemappings'));
 $xml_file  = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
 
+$info = restore_read_xml_info($xml_file);
 
-$roles = restore_read_xml_roles($xml_file);
-
-$siteroles = get_records('role');
-$siterolesarray = array();
-foreach ($siteroles as $siterole) {
-    $siterolesarray[$siterole->id] = $siterole->shortname;  
-}
-
+$siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course_header->course_id), "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) {
+if ($info->backup_moodle_version < 2006092801) {
+    // 1.6 and below backup
+    
+    /// Editting teacher
     echo ('<tr><td align="right">');
-    echo $role->shortname;
-    echo ('</td><td align="left">');
+    print_string('teacheredit');
+    echo ('</td><td algin="left">');
+    
+    // get the first teacheredit legacy
+    $roles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
+    
+    $editteacher = array_shift($roles);
+    choose_from_menu ($siterolesarray, "defaultteacheredit", $editteacher->id);
+    echo ('</td></tr>');
     
-    // see if any short name match
-    $matchrole = 0;
-    foreach ($siteroles as $siterole) {
-        if ($siterole->shortname == $role->shortname) {
-            $matchrole = $siterole->id;
-            break;  
-        }  
+    /// Non-editting teacher
+    echo ('<tr><td align="right">');
+    print_string('teacher');
+    echo ('</td><td algin="left">');
+    
+    // get the first teacheredit legacy
+    $roles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
+    $teacher = array_shift($roles);
+    
+    choose_from_menu ($siterolesarray, "defaultteacher", $teacher->id);
+    echo ('</td></tr>');
+     
+    
+    /// Student
+    echo ('<tr><td align="right">');
+    print_string('student');
+    echo ('</td><td algin="left">');
+    
+    // get the first teacheredit legacy
+    $roles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID));
+    $studentrole = array_shift($roles);
+    
+    choose_from_menu ($siterolesarray, "defaultstudent", $studentrole->id);
+    echo ('</td></tr>');
+  
+} else {
+    // 1.7 and above backup
+    
+    $roles = restore_read_xml_roles($xml_file);
+
+    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 ($siterolesarray as $siteroleid=>$siteroleshortname) {
+            if ($siteroleshortname == $role->shortname) {
+                $matchrole = $siteroleid;
+                break;  
+            }  
+        }
+    
+        choose_from_menu ($siterolesarray, "roles_".$roleid, $matchrole, 'new role', '', '0');
+        echo ('</td></tr>'); 
     }
     
-    choose_from_menu ($siterolesarray, "roles_".$roleid, $matchrole, 'new role', '', '0');
-    echo ('</td></tr>'); 
-}
-echo ('</table>');
+} // end else
+
+echo ('</table>'); // end of role mappings table
+
 ?>
 <br />
 <center>
index 7d197c878df0f4199bf0f20deeda643a0ade02d2..3a93c36beff369947d08d4efed51d9f39def5d00 100644 (file)
                     
                     if ($is_admin) {
                         //If the record (user_admins) doesn't exists
-                        if (!record_exists("user_admins","userid",$newid)) {
-                            //Only put status in backup_ids
-                            $currinfo = $currinfo."admin,";
-                            $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
-                        }
+                        //Only put status in backup_ids
+                        $currinfo = $currinfo."admin,";
+                        $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
                     } 
                     if ($is_coursecreator) {
                         //If the record (user_coursecreators) doesn't exists
-                        if (!record_exists("user_coursecreators","userid",$newid)) {
-                            //Only put status in backup_ids
-                            $currinfo = $currinfo."coursecreator,";
-                            $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
-                        }
+                        //Only put status in backup_ids
+                        $currinfo = $currinfo."coursecreator,";
+                        $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
                     } 
                     if ($is_needed) {
                         //Only put status in backup_ids
                     }
                     if ($is_teacher) {
                         //If the record (teacher) doesn't exists
-                        if (!record_exists("user_teachers","userid",$newid,"course", $restore->course_id)) {
-                            //Put status in backup_ids 
-                            $currinfo = $currinfo."teacher,";
-                            $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
-                            //Set course and user
-                            $user->roles['teacher']->course = $restore->course_id;
-                            $user->roles['teacher']->userid = $newid;
-
-                            //Need to analyse the enrol field
-                            //    - if it isn't set, set it to $CFG->enrol
-                            //    - if we are in a different server (by wwwroot), set it to $CFG->enrol
-                            //    - if we are in the same server (by wwwroot), maintain it unmodified.
-                            if (empty($user->roles['teacher']->enrol)) {
-                                $user->roles['teacher']->enrol = $CFG->enrol;
-                            } else if ($restore->original_wwwroot != $CFG->wwwroot) {
-                                $user->roles['teacher']->enrol = $CFG->enrol;
-                            } else {
-                                //Nothing to do. Leave it unmodified
-                            }    
-
-                            //Insert data in user_teachers
-                            //The structure is exactly as we need
-                            $status = insert_record("user_teachers",$user->roles['teacher']);
-                        }
+                        //Put status in backup_ids 
+                        $currinfo = $currinfo."teacher,";
+                        $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
+                        //Set course and user
+                        $user->roles['teacher']->course = $restore->course_id;
+                        $user->roles['teacher']->userid = $newid;
+
+                        //Need to analyse the enrol field
+                        //    - if it isn't set, set it to $CFG->enrol
+                        //    - if we are in a different server (by wwwroot), set it to $CFG->enrol
+                        //    - if we are in the same server (by wwwroot), maintain it unmodified.
+                        if (empty($user->roles['teacher']->enrol)) {
+                            $user->roles['teacher']->enrol = $CFG->enrol;
+                        } else if ($restore->original_wwwroot != $CFG->wwwroot) {
+                            $user->roles['teacher']->enrol = $CFG->enrol;
+                        } else {
+                            //Nothing to do. Leave it unmodified
+                        }    
+
+                        $rolesmapping = $restore->rolesmapping;
+                        $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
+                        if ($user->roles['teacher']->editall) {
+                            role_assign($rolesmapping['defaultteacheredit'],
+                                        $newid, 
+                                        0, 
+                                        $context->id, 
+                                        $user->roles['teacher']->timestart, 
+                                        $user->roles['teacher']->timeend, 
+                                        0, 
+                                        $user->roles['teacher']->enrol);
+                            // editting teacher  
+                        } else {
+                            // non editting teacher
+                            role_assign($rolesmapping['defaultteacher'],
+                                        $newid, 
+                                        0, 
+                                        $context->id, 
+                                        $user->roles['teacher']->timestart, 
+                                        $user->roles['teacher']->timeend, 
+                                        0, 
+                                        $user->roles['teacher']->enrol);
+                        }         
                     } 
                     if ($is_student) {
-                        //If the record (student) doesn't exists
-                        if (!record_exists("user_students","userid",$newid,"course", $restore->course_id)) {
-                            //Put status in backup_ids
-                            $currinfo = $currinfo."student,";
-                            $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
-                            //Set course and user
-                            $user->roles['student']->course = $restore->course_id;
-                            $user->roles['student']->userid = $newid;
-
-                            //Need to analyse the enrol field
-                            //    - if it isn't set, set it to $CFG->enrol
-                            //    - if we are in a different server (by wwwroot), set it to $CFG->enrol
-                            //    - if we are in the same server (by wwwroot), maintain it unmodified.
-                            if (empty($user->roles['student']->enrol)) {
-                                $user->roles['student']->enrol = $CFG->enrol;
-                            } else if ($restore->original_wwwroot != $CFG->wwwroot) {
-                                $user->roles['student']->enrol = $CFG->enrol;
-                            } else {
-                                //Nothing to do. Leave it unmodified
-                            }    
 
-                            //Insert data in user_students
-                            //The structure is exactly as we need
-                            $status = insert_record("user_students",$user->roles['student']);
-                        }
+                        //Put status in backup_ids
+                        $currinfo = $currinfo."student,";
+                        $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
+                        //Set course and user
+                        $user->roles['student']->course = $restore->course_id;
+                        $user->roles['student']->userid = $newid;
+
+                        //Need to analyse the enrol field
+                        //    - if it isn't set, set it to $CFG->enrol
+                        //    - if we are in a different server (by wwwroot), set it to $CFG->enrol
+                        //    - if we are in the same server (by wwwroot), maintain it unmodified.
+                        if (empty($user->roles['student']->enrol)) {
+                            $user->roles['student']->enrol = $CFG->enrol;
+                        } else if ($restore->original_wwwroot != $CFG->wwwroot) {
+                            $user->roles['student']->enrol = $CFG->enrol;
+                        } else {
+                            //Nothing to do. Leave it unmodified
+                        }    
+                        $rolesmapping = $restore->rolesmapping;
+                        $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
+                            
+                        role_assign($rolesmapping['defaultstudent'],
+                                    $newid, 
+                                    0, 
+                                    $context->id, 
+                                    $user->roles['student']->timestart, 
+                                    $user->roles['student']->timeend, 
+                                    0, 
+                                    $user->roles['student']->enrol);
+
                     }
                     if (!$is_course_user) {
                         //If the record (user) doesn't exists
                             break;
                     }
                 }
-                
-                
+                              
                 if ($this->level == 7 && $this->tree[5]!="ROLE_ASSIGNMENTS" && $this->tree[5]!="ROLE_OVERRIDES") {
                     switch ($tagName) {
                         case "TYPE":
         // get role mapping info from $restore
 
         $rolemappings = $restore->rolesmapping; // an array
-
-        foreach ($info->roles as $oldroleid=>$roledata) {
+        if ($info->roles) {
+            foreach ($info->roles as $oldroleid=>$roledata) {
             
             /// 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,
+                if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
+                    $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
                                      $rolemappings[$oldroleid]); // adding a new id     
               
-            } else {
+                } 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;
+                    // 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);
+                    }
                 }
             }
         }
             echo "<li>".get_string("creatingcourseroles").'</li>';
         }
         $course = restore_read_xml_course_header($xmlfile);
-        $courseassignments = $course->roleassignments;
+        
+        if (!empty($course->roleassignments)) {
+            $courseassignments = $course->roleassignments;
 
-        foreach ($courseassignments as $oldroleid => $courseassignment) {          
-            restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
+            foreach ($courseassignments as $oldroleid => $courseassignment) {          
+                restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
+            }
         }
-
         /*****************************************************
          * Restoring from course level overrides *
          *****************************************************/     
-        $courseoverrides = $course->roleoverrides;
-        $rolemappings = $restore->rolesmapping;
-        foreach ($courseoverrides as $oldroleid => $courseoverride) {
+        
+        if (!empty($course->$course->roleoverrides)) {
+            $courseoverrides = $course->roleoverrides;
+            $rolemappings = $restore->rolesmapping;
+            foreach ($courseoverrides as $oldroleid => $courseoverride) {
             
-            // 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);
+                // 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);
+                }
             }
         }
       
index f45a01adcd90851c443d8b33c26858f1e866e7c6..d68e480e6756a6f8dc4879d37fab3f406f720154 100755 (executable)
@@ -2613,14 +2613,14 @@ function allow_assign($sroleid, $troleid) {
  * @param object $context
  * @return array
  */
-function get_assignable_roles ($context) {
+function get_assignable_roles ($context, $field="name") {
 
     $options = array();
 
     if ($roles = get_all_roles()) {
         foreach ($roles as $role) {
             if (user_can_assign($context, $role->id)) {
-                $options[$role->id] = strip_tags(format_string($role->name, true));
+                $options[$role->id] = strip_tags(format_string($role->{$field}, true));
             }
         }
     }