MDL-21432 backup - helper function for restoring course/role names and date_offset
This commit is contained in:
@@ -830,6 +830,63 @@ abstract class restore_dbops {
|
||||
}
|
||||
return $dbrec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given on courseid, fullname and shortname, calculate the correct fullname/shortname to avoid dupes
|
||||
*/
|
||||
public static function calculate_course_names($courseid, $fullname, $shortname) {
|
||||
global $CFG, $DB;
|
||||
|
||||
$currentfullname = '';
|
||||
$currentshortname = '';
|
||||
$counter = 0;
|
||||
// Iteratere while the name exists
|
||||
do {
|
||||
if ($counter) {
|
||||
$suffixfull = ' ' . get_string('copyasnoun') . ' ' . $counter;
|
||||
$suffixshort = '_' . $counter;
|
||||
} else {
|
||||
$suffixfull = '';
|
||||
$suffixshort = '';
|
||||
}
|
||||
$currentfullname = $fullname.$suffixfull;
|
||||
$currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort; // < 100cc
|
||||
$coursefull = $DB->get_record_select('course', 'fullname = ? AND id != ?', array($currentfullname, $courseid));
|
||||
$courseshort = $DB->get_record_select('course', 'shortname = ? AND id != ?', array($currentshortname, $courseid));
|
||||
$counter++;
|
||||
} while ($coursefull || $courseshort);
|
||||
|
||||
// Return results
|
||||
return array($currentfullname, $currentshortname);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the target course context, put as many custom role names as possible
|
||||
*/
|
||||
public static function set_course_role_names($restoreid, $courseid) {
|
||||
global $DB;
|
||||
|
||||
// Get the course context
|
||||
$coursectx = get_context_instance(CONTEXT_COURSE, $courseid);
|
||||
// Get all the mapped roles we have
|
||||
$rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid');
|
||||
foreach ($rs as $recrole) {
|
||||
// Get the complete temp_ids record
|
||||
$role = (object)self::get_backup_ids_record($restoreid, 'role', $recrole->itemid);
|
||||
// If it's one mapped role
|
||||
if (!empty($role->newitemid)) {
|
||||
// If role name doesn't exist, add it
|
||||
$rolename = new stdclass();
|
||||
$rolename->roleid = $role->newitemid;
|
||||
$rolename->contextid = $coursectx->id;
|
||||
if (!$DB->record_exists('role_names', (array)$rolename)) {
|
||||
$rolename->name = $role->info['nameincourse'];
|
||||
$DB->insert_record('role_names', $rolename);
|
||||
}
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -255,6 +255,49 @@ abstract class restore_structure_step extends restore_step {
|
||||
$filearea, $filesctxid, $mappingitemname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply course startdate offset based in original course startdate and course_offset_startdate setting
|
||||
* Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
|
||||
* executions in the same request
|
||||
*/
|
||||
protected function apply_date_offset($value) {
|
||||
|
||||
static $cache = array();
|
||||
// Lookup cache
|
||||
if (isset($cache[$this->get_restoreid()])) {
|
||||
return $value + $cache[$this->get_restoreid()];
|
||||
}
|
||||
// No cache, let's calculate the offset
|
||||
$original = $this->task->get_info()->original_course_startdate;
|
||||
// TODO: Get startdate from settings
|
||||
debugging ('review these lines of apply_date_offset() to start using the "course_startdate" setting', DEBUG_DEVELOPER);
|
||||
// $setting = $this->get_setting_value('course_startdate');
|
||||
// TODO: Delete this line once we are getting the var above from settings
|
||||
$setting = $original;
|
||||
|
||||
// Original course has not startdate, offset = 0
|
||||
if (empty($original)) {
|
||||
$cache[$this->get_restoreid()] = 0;
|
||||
|
||||
// Less than 24h of difference, offset = 0 (this avoids some problems with timezones)
|
||||
} else if (abs($setting - $original) < 24 * 60 * 60) {
|
||||
$cache[$this->get_restoreid()] = 0;
|
||||
|
||||
// Re-enforce 'moodle/restore:rolldates' capability for the user in the course, just in case
|
||||
} else if (!has_capability('moodle/restore:rolldates',
|
||||
get_context_instance(CONTEXT_COURSE, $this->get_courseid()),
|
||||
$this->get_userid())) {
|
||||
$cache[$this->get_restoreid()] = 0;
|
||||
|
||||
// Arrived here, let's calculate the real offset
|
||||
} else {
|
||||
$cache[$this->get_restoreid()] = $setting - $original;
|
||||
}
|
||||
|
||||
// Return the passed value with cached offset applied
|
||||
return $value + $cache[$this->get_restoreid()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that will return the structure to be processed by this restore_step.
|
||||
* Must return one array of @restore_path_element elements
|
||||
|
||||
Reference in New Issue
Block a user