MDL-46991 Availability: Conditional dates not updated on restore

This commit is contained in:
sam marshall 2014-08-29 18:51:18 +01:00
parent 272fec367f
commit 5176504d3f
8 changed files with 158 additions and 55 deletions

View file

@ -45,6 +45,55 @@ abstract class restore_step extends base_step {
}
return $this->task->get_restoreid();
}
/**
* 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
*
* @param int $value Time value (seconds since epoch), or empty for nothing
* @return int Time value after applying the date offset, or empty for nothing
*/
public function apply_date_offset($value) {
// Empties don't offset - zeros (int and string), false and nulls return original value.
if (empty($value)) {
return $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;
$setting = 0;
if ($this->setting_exists('course_startdate')) { // Seting may not exist (MDL-25019).
$setting = $this->get_setting_value('course_startdate');
}
if (empty($original) || empty($setting)) {
// Original course has not startdate or setting doesn't exist, offset = 0.
$cache[$this->get_restoreid()] = 0;
} else if (abs($setting - $original) < 24 * 60 * 60) {
// Less than 24h of difference, offset = 0 (this avoids some problems with timezones).
$cache[$this->get_restoreid()] = 0;
} else if (!has_capability('moodle/restore:rolldates',
context_course::instance($this->get_courseid()), $this->task->get_userid())) {
// Re-enforce 'moodle/restore:rolldates' capability for the user in the course, just in case.
$cache[$this->get_restoreid()] = 0;
} else {
// Arrived here, let's calculate the real offset.
$cache[$this->get_restoreid()] = $setting - $original;
}
// Return the passed value with cached offset applied.
return $value + $cache[$this->get_restoreid()];
}
}
/*

View file

@ -245,53 +245,6 @@ abstract class restore_structure_step extends restore_step {
$this->task->add_result($resultstoadd);
}
/**
* 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
*/
public function apply_date_offset($value) {
// empties don't offset - zeros (int and string), false and nulls return original value
if (empty($value)) {
return $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;
$setting = 0;
if ($this->setting_exists('course_startdate')) { // Seting may not exist (MDL-25019)
$setting = $this->get_setting_value('course_startdate');
}
// Original course has not startdate or setting doesn't exist, offset = 0
if (empty($original) || empty($setting)) {
$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',
context_course::instance($this->get_courseid()),
$this->task->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()];
}
/**
* As far as restore structure steps are implementing restore_plugin stuff, they need to
* have the parent task available for wrapping purposes (get course/context....)