MDL-22164 backup - clean important parts of old code. Leaving rest for reference
This commit is contained in:
@@ -20,31 +20,6 @@
|
||||
//Detect if we are coming from the restore form
|
||||
$fromform = optional_param ('fromform', 0, PARAM_INT);
|
||||
|
||||
if ($form1 = data_submitted()) {
|
||||
$currentcourseshortname = $course_header->course_shortname; //"store_ShortName";
|
||||
$course_header->course_shortname = !empty($form1->shortname) ? $form1->shortname : ''; //"update_ShortName";
|
||||
$course_header->course_fullname = !empty($form1->fullname) ? $form1->fullname : ''; //"update_FullName";
|
||||
/// Roll dates only if the backup course has a start date
|
||||
/// (some formats like main page, social..., haven't it and rolling dates
|
||||
/// from 0 produces crazy dates. MDL-10125 and we have passed some custom startyear/month/day. MDL-12922
|
||||
if ($course_header->course_startdate && !empty($form1->startyear)) {
|
||||
$form1->startdate = make_timestamp($form1->startyear, $form1->startmonth, $form1->startday);
|
||||
$currentcoursestartdate = $course_header->course_startdate;
|
||||
$coursestartdatedateoffset = $form1->startdate - $currentcoursestartdate;
|
||||
$restore->course_startdateoffset = $coursestartdatedateoffset; //change to restore
|
||||
// Only apply rolling of dates if differences are bigger than one day
|
||||
// that should solve current problems with daylight changes between
|
||||
// backup and restore
|
||||
if (abs($restore->course_startdateoffset) < 24 * 60 * 60) {
|
||||
$coursestartdatedateoffset = 0;
|
||||
$restore->course_startdateoffset = 0;
|
||||
}
|
||||
} else { // don't roll if the course hasn't start date
|
||||
$coursestartdatedateoffset = 0;
|
||||
$restore->course_startdateoffset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
|
||||
$SESSION->course_header = $course_header;
|
||||
|
||||
@@ -277,26 +252,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
// If we have selected to roll dates on restore and the user is missing
|
||||
// such capability (moodle/restore:rolldates) in the target course/category,
|
||||
// disable roll of dates and warn
|
||||
if ($restore->course_startdateoffset != 0) {
|
||||
$canrolldates = false; // init to false
|
||||
|
||||
// if we know the target course, check the capability on it
|
||||
if (!empty($restore->course_id)) {
|
||||
$canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSE, $restore->course_id));
|
||||
// if we know the target category, check capability on it
|
||||
} else if (!empty($restore->restore_restorecatto)) {
|
||||
$canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto));
|
||||
}
|
||||
|
||||
if (!$canrolldates) {
|
||||
$messages[] = get_string ("noteusercannotrolldatesoncontext");
|
||||
$restore->course_startdateoffset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Check if we've selected any mod's user info and restore->users
|
||||
//is set to none. Change it to course and inform.
|
||||
if ($restore->users == 2) {
|
||||
|
||||
@@ -572,233 +572,3 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
|
||||
?>
|
||||
</table>
|
||||
|
||||
<hr />
|
||||
<?php
|
||||
// Only show the roles mapping if restore of user info is allowed
|
||||
if ($restoreuserinfo) {
|
||||
echo $OUTPUT->heading(get_string('rolemappings'));
|
||||
$xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
|
||||
|
||||
$info = restore_read_xml_info($xml_file);
|
||||
|
||||
// fix for MDL-9068, front page course is just a normal course
|
||||
$siterolesarray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "shortname", ROLENAME_ORIGINAL);
|
||||
$siterolesnamearray = get_assignable_roles (get_context_instance(CONTEXT_COURSE, $course->id), "name", ROLENAME_ORIGINAL);
|
||||
$allroles = get_records('role');
|
||||
|
||||
echo ('<table width="100%" class="restore-form-instances">');
|
||||
|
||||
if ($info->backup_moodle_version < 2006092801) {
|
||||
echo ('<tr><td align="right" style="width:50%"><b>'.get_string('sourcerole').'</b></td><td align="left" style="width:50%"><b>'.get_string('targetrole').'</b></td></tr>');
|
||||
|
||||
// 1.6 and below backup
|
||||
|
||||
/// Editting teacher
|
||||
echo ('<tr><td align="right">');
|
||||
echo '<label for="menudefaultteacheredit">'.get_string ('defaultcourseteacher').'</label>';
|
||||
echo ('</td><td algin="left">');
|
||||
|
||||
// get the first teacheredit legacy
|
||||
$roles = get_archetype_roles('editingteacher');
|
||||
|
||||
$editteacher = reset($roles);
|
||||
echo html_writer::select($siterolesarray, "defaultteacheredit", $editteacher->id, array(''=>'new role'));
|
||||
echo ('</td></tr>');
|
||||
|
||||
/// Non-editting teacher
|
||||
echo ('<tr><td align="right">');
|
||||
echo '<label for="menudefaultteacher">'.get_string ('noneditingteacher').'</label>';
|
||||
print_string('noneditingteacher');
|
||||
echo ('</td><td algin="left">');
|
||||
|
||||
// get the first teacheredit legacy
|
||||
$roles = get_archetype_roles('teacher');
|
||||
$teacher = reset($roles);
|
||||
|
||||
echo html_writer::select($siterolesarray, "defaultteacher", $teacher->id, array(''=>'new role'));
|
||||
echo ('</td></tr>');
|
||||
|
||||
|
||||
/// Student
|
||||
echo ('<tr><td align="right">');
|
||||
echo '<label for="menudefaultstudent">'.get_string ('defaultcoursestudent').'</label>';
|
||||
echo ('</td><td algin="left">');
|
||||
|
||||
// get the first teacheredit legacy
|
||||
$roles = get_archetype_roles('student');
|
||||
$studentrole = array_shift($roles);
|
||||
|
||||
echo html_writer::select($siterolesarray, "defaultstudent", $studentrole->id, array(''=>'new role'));
|
||||
echo ('</td></tr>');
|
||||
|
||||
} else {
|
||||
// 1.7 and above backup
|
||||
$roles = restore_read_xml_roles($xml_file);
|
||||
|
||||
// Calculate if any role can be mapped
|
||||
$str_new = '';
|
||||
$canmaproles = false;
|
||||
if (has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
$str_new = 'new role';
|
||||
$canmaproles = true;
|
||||
} else if (!empty($siterolesarray)) {
|
||||
$canmaproles = true;
|
||||
}
|
||||
|
||||
if ($canmaproles) {
|
||||
echo ('<tr><td align="right" style="width:50%"><b>'.get_string('sourcerole').'</b></td><td align="left" style="width:50%"><b>'.get_string('targetrole').'</b></td></tr>');
|
||||
}
|
||||
|
||||
if (!empty($roles->roles)) { // possible to have course with no roles
|
||||
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
|
||||
$siteroleschoicearray[$siteroleid] = $siterolesnamearray[$siteroleid]." (". $siterolesarray[$siteroleid].")";
|
||||
}
|
||||
|
||||
foreach ($roles->roles as $roleid=>$role) {
|
||||
|
||||
if (!$canmaproles) { // User cannot map roles at all, add hidden element and continue
|
||||
echo "<tr><td colspan=\"2\"><input type=\"hidden\" name=\"roles_{$roleid}\" value=\"0\" /></td></tr>";
|
||||
continue;
|
||||
}
|
||||
|
||||
$mappableroles = !empty($siteroleschoicearray) ? $siteroleschoicearray : array();
|
||||
|
||||
echo ('<tr><td align="right">');
|
||||
echo '<label for="menuroles_'.$roleid.'">'.format_string($role->name)." (".($role->shortname).")".'</label>';
|
||||
echo ('</td><td align="left">');
|
||||
|
||||
/// first, we see if any exact role definition is found
|
||||
/// if found, that is the only option of restoring to
|
||||
|
||||
if ($samerole = restore_samerole($roleid, $role)) {
|
||||
$matchrole = $samerole->id;
|
||||
// if an exact role is found, it does not matter whether this user can assign this role or not,
|
||||
// this will be presented as a valid option regardless
|
||||
$mappableroles[$samerole->id] = format_string($allroles[$samerole->id]->name)." (". $allroles[$samerole->id]->shortname.")";
|
||||
} else {
|
||||
// no exact role found, let's try to match shortname
|
||||
// this is useful in situations where basic roles differ slightly in definition
|
||||
$matchrole = 0;
|
||||
foreach ($siterolesarray as $siteroleid=>$siteroleshortname) {
|
||||
if ($siteroleshortname == $role->shortname) {
|
||||
$matchrole = $siteroleid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Hack a bit, so, if role continues unmatched, and was "editingteacher", and "teacher" is available
|
||||
// preselect it as fallback matching (can happen a lot)
|
||||
if ($matchrole == 0 && $role->shortname == 'editingteacher' && in_array('teacher', $siterolesarray)) {
|
||||
$matchrole = array_search('teacher', $siterolesarray);
|
||||
}
|
||||
|
||||
// If arrived here, role continues unmatched and the user lacks the 'moodle/role:manage' capability
|
||||
// perform automatic matching to site defaultcourseroleid (only if available in the list)
|
||||
if ($matchrole == 0 && !has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
if (array_key_exists($CFG->defaultcourseroleid, $siterolesarray)) {
|
||||
$matchrole = $CFG->defaultcourseroleid;
|
||||
}
|
||||
}
|
||||
}
|
||||
echo html_writer::select($mappableroles, "roles_".$roleid, $matchrole, array('' => $str_new));
|
||||
echo ('</td></tr>');
|
||||
}
|
||||
}
|
||||
|
||||
} // end else
|
||||
echo ('</table>'); // end of role mappings table
|
||||
}
|
||||
|
||||
?>
|
||||
<br />
|
||||
<div style="text-align:center">
|
||||
<?php
|
||||
/// Print captured hidden options, now that we have closed the table
|
||||
echo $hidden_options;
|
||||
?>
|
||||
<input type="hidden" name="id" value="<?php p($id) ?>" />
|
||||
<input type="hidden" name="launch" value="check" />
|
||||
<input type="hidden" name="fromform" value="1" />
|
||||
<input type="submit" value="<?php print_string("continue") ?>" />
|
||||
<input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* @param integer $roleid the id that the role in the backup files had on the old server.
|
||||
* @param object $role the rest of the definition of the role from the backup file.
|
||||
*/
|
||||
function restore_samerole($roleid, $rolefromxml) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// First we try some intelligent guesses, then, if none of those work, we do a more extensive
|
||||
// search.
|
||||
|
||||
// First guess, try let's use the id
|
||||
if (restore_is_samerole($roleid, $rolefromxml)) {
|
||||
return $DB->get_record('role', array('id'=>$roleid));
|
||||
}
|
||||
|
||||
// Second guess, try the shortname
|
||||
$testroleid = $DB->get_field('role', 'id', array('shortname'=>$rolefromxml->shortname));
|
||||
if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
|
||||
return $DB->get_record('role', array('id'=>$testroleid));
|
||||
}
|
||||
|
||||
// Finally, search all other roles. In orter to speed things up, we exclude the ones we have
|
||||
// already tested, and we only search roles with the same number of capabilities set in their
|
||||
// definition.
|
||||
$extracondition = '';
|
||||
if ($testroleid) {
|
||||
$extracondition = "AND roleid <> $testroleid";
|
||||
}
|
||||
$candidateroleids = $DB->get_records_sql(
|
||||
"SELECT roleid
|
||||
FROM {role_capabilities}
|
||||
WHERE roleid <> $roleid $extracondition
|
||||
GROUP BY roleid
|
||||
HAVING COUNT(capability) = ".count($rolefromxml->capabilities));
|
||||
if (!empty($candidateroleids)) {
|
||||
foreach ($candidateroleids as $testroleid => $notused) {
|
||||
if (restore_is_samerole($testroleid, $rolefromxml)) {
|
||||
return $DB->get_record('role', array('id'=>$testroleid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare a role in the database with one loaded from the backup file, and determine whether
|
||||
* they have identical permissions for each capability.
|
||||
* @param integer $testroleid the id of the role from the database to test against.
|
||||
* @param object $rolefromxml the role definition loaded from the backup file.
|
||||
* @return boolean true if the two roles are identical.
|
||||
*/
|
||||
function restore_is_samerole($testroleid, $rolefromxml) {
|
||||
global $DB;
|
||||
|
||||
// Load the role definition from the databse.
|
||||
$rolefromdb = $DB->get_records('role_capabilities', array('roleid'=>$testroleid), '', 'capability,permission');
|
||||
if (!$rolefromdb) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Quick check, do they have the permissions on the same number of capabilities?
|
||||
if (count($rolefromdb) != count($rolefromxml->capabilities)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If they do, check each one.
|
||||
foreach ($rolefromdb as $capability => $permissions) {
|
||||
if (!isset($rolefromxml->capabilities[$capability]) ||
|
||||
$permissions->permission != $rolefromxml->capabilities[$capability]->permission) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user