adding support for 1.6->1.7 backups

This commit is contained in:
toyomoyo
2006-10-03 08:54:46 +00:00
parent 31aab450e0
commit 5e67946d6c
5 changed files with 212 additions and 190 deletions
+1 -58
View File
@@ -51,38 +51,7 @@
//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";
@@ -191,37 +160,11 @@
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)
+12 -1
View File
@@ -130,7 +130,18 @@
$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
+66 -23
View 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) {
echo ('<tr><td align="right">');
echo $role->shortname;
echo ('</td><td align="left">');
if ($info->backup_moodle_version < 2006092801) {
// 1.6 and below backup
// see if any short name match
$matchrole = 0;
foreach ($siteroles as $siterole) {
if ($siterole->shortname == $role->shortname) {
$matchrole = $siterole->id;
break;
}
/// Editting teacher
echo ('<tr><td align="right">');
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>');
/// 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>
+131 -106
View File
@@ -1508,19 +1508,15 @@
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
@@ -1529,57 +1525,81 @@
}
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;
//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
}
//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']);
}
$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
}
//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);
//Insert data in user_students
//The structure is exactly as we need
$status = insert_record("user_students",$user->roles['student']);
}
}
if (!$is_course_user) {
//If the record (user) doesn't exists
@@ -4418,8 +4438,7 @@
break;
}
}
if ($this->level == 7 && $this->tree[5]!="ROLE_ASSIGNMENTS" && $this->tree[5]!="ROLE_OVERRIDES") {
switch ($tagName) {
case "TYPE":
@@ -5992,57 +6011,58 @@
// 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);
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;
$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);
}
}
}
}
@@ -6063,23 +6083,28 @@
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);
}
}
}
+2 -2
View File
@@ -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));
}
}
}