MDL-9367 restore: Roll only configuration dates

It was decided to roll only configuration dates and any date related to user content
such as 'timecreated' , 'timemodified' etc should not be rolled forward.
This commit is contained in:
Ankit Agarwal 2017-06-29 13:38:47 +05:30
parent d509f80c48
commit 0d14fcbc60
57 changed files with 1617 additions and 74 deletions

View file

@ -62,9 +62,10 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
$oldid = $data->id;
$data->course = $this->get_courseid();
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
// See MDL-9367.
$data->available = $this->apply_date_offset($data->available);
$data->deadline = $this->apply_date_offset($data->deadline);
$data->timemodified = $this->apply_date_offset($data->timemodified);
// The lesson->highscore code was removed in MDL-49581.
// Remove it if found in the backup file.
@ -110,8 +111,6 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
$data->lessonid = $this->get_new_parentid('lesson');
// We'll remap all the prevpageid and nextpageid at the end, once all pages have been created
$data->timemodified = $this->apply_date_offset($data->timemodified);
$data->timecreated = $this->apply_date_offset($data->timecreated);
$newitemid = $DB->insert_record('lesson_pages', $data);
$this->set_mapping('lesson_page', $oldid, $newitemid, true); // Has related fileareas
@ -124,8 +123,6 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
$data->lessonid = $this->get_new_parentid('lesson');
$data->pageid = $this->get_new_parentid('lesson_page');
$data->answer = $data->answer_text;
$data->timemodified = $this->apply_date_offset($data->timemodified);
$data->timecreated = $this->apply_date_offset($data->timecreated);
// Set a dummy mapping to get the old ID so that it can be used by get_old_parentid when
// processing attempts. It will be corrected in after_execute
@ -147,7 +144,6 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
// We use the old answerid here as the answer isn't created until after_execute
$data->answerid = $this->get_old_parentid('lesson_answer');
$data->userid = $this->get_mappingid('user', $data->userid);
$data->timeseen = $this->apply_date_offset($data->timeseen);
$newitemid = $DB->insert_record('lesson_attempts', $data);
$this->set_mapping('lesson_attempt', $oldid, $newitemid, true); // Has related fileareas.
@ -160,7 +156,6 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
$oldid = $data->id;
$data->lessonid = $this->get_new_parentid('lesson');
$data->userid = $this->get_mappingid('user', $data->userid);
$data->completed = $this->apply_date_offset($data->completed);
$newitemid = $DB->insert_record('lesson_grades', $data);
$this->set_mapping('lesson_grade', $oldid, $newitemid);
@ -174,7 +169,6 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
$data->lessonid = $this->get_new_parentid('lesson');
$data->pageid = $this->get_new_parentid('lesson_page');
$data->userid = $this->get_mappingid('user', $data->userid);
$data->timeseen = $this->apply_date_offset($data->timeseen);
$newitemid = $DB->insert_record('lesson_branch', $data);
}
@ -191,8 +185,6 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_
$oldid = $data->id;
$data->lessonid = $this->get_new_parentid('lesson');
$data->userid = $this->get_mappingid('user', $data->userid);
$data->starttime = $this->apply_date_offset($data->starttime);
$data->lessontime = $this->apply_date_offset($data->lessontime);
// Supply item that maybe missing from previous versions.
if (!isset($data->completed)) {
$data->completed = 0;

View file

@ -1120,6 +1120,8 @@ function lesson_reset_userdata($data) {
WHERE lessonid IN (SELECT id FROM {lesson} WHERE course = ?)
AND deadline <> 0", array($data->timeshift, $data->courseid));
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
// See MDL-9367.
shift_course_mod_dates('lesson', array('available', 'deadline'), $data->timeshift, $data->courseid);
$status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
}

View file

@ -0,0 +1,190 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Restore date tests.
*
* @package mod_lesson
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
/**
* Restore date tests.
*
* @package mod_lesson
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_lesson_restore_date_testcase extends restore_date_testcase {
/**
* Creates an attempt for the given userwith a correct or incorrect answer and optionally finishes it.
*
* TODO This api can be better extracted to a generator.
*
* @param stdClass $lesson Lesson object.
* @param stdClass $page page object.
* @param boolean $correct If the answer should be correct.
* @param boolean $finished If we should finish the attempt.
*
* @return array the result of the attempt creation or finalisation.
*/
protected function create_attempt($lesson, $page, $correct = true, $finished = false) {
global $DB, $USER;
// First we need to launch the lesson so the timer is on.
mod_lesson_external::launch_attempt($lesson->id);
$DB->set_field('lesson', 'feedback', 1, array('id' => $lesson->id));
$DB->set_field('lesson', 'progressbar', 1, array('id' => $lesson->id));
$DB->set_field('lesson', 'custom', 0, array('id' => $lesson->id));
$DB->set_field('lesson', 'maxattempts', 3, array('id' => $lesson->id));
$answercorrect = 0;
$answerincorrect = 0;
$p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page->id), 'id');
foreach ($p2answers as $answer) {
if ($answer->jumpto == 0) {
$answerincorrect = $answer->id;
} else {
$answercorrect = $answer->id;
}
}
$data = array(
array(
'name' => 'answerid',
'value' => $correct ? $answercorrect : $answerincorrect,
),
array(
'name' => '_qf__lesson_display_answer_form_truefalse',
'value' => 1,
)
);
$result = mod_lesson_external::process_page($lesson->id, $page->id, $data);
$result = external_api::clean_returnvalue(mod_lesson_external::process_page_returns(), $result);
// Create attempt.
$newpageattempt = [
'lessonid' => $lesson->id,
'pageid' => $page->id,
'userid' => $USER->id,
'answerid' => $answercorrect,
'retry' => 1, // First attempt is always 0.
'correct' => 1,
'useranswer' => '1',
'timeseen' => time(),
];
$DB->insert_record('lesson_attempts', (object) $newpageattempt);
if ($finished) {
$result = mod_lesson_external::finish_attempt($lesson->id);
$result = external_api::clean_returnvalue(mod_lesson_external::finish_attempt_returns(), $result);
}
return $result;
}
/**
* Test restore dates.
*/
public function test_restore_dates() {
global $DB, $USER;
// Create lesson data.
$record = ['available' => 100, 'deadline' => 100, 'timemodified' => 100];
list($course, $lesson) = $this->create_course_and_module('lesson', $record);
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
$page = $lessongenerator->create_content($lesson);
$page2 = $lessongenerator->create_question_truefalse($lesson);
$this->create_attempt($lesson, $page2, true, true);
$timer = $DB->get_record('lesson_timer', ['lessonid' => $lesson->id]);
// Lesson grade.
$timestamp = 100;
$grade = new stdClass();
$grade->lessonid = $lesson->id;
$grade->userid = $USER->id;
$grade->grade = 8.9;
$grade->completed = $timestamp;
$grade->id = $DB->insert_record('lesson_grades', $grade);
// User override.
$override = (object)[
'lessonid' => $lesson->id,
'groupid' => 0,
'userid' => $USER->id,
'sortorder' => 1,
'available' => 100,
'deadline' => 200
];
$DB->insert_record('lesson_overrides', $override);
// Set time fields to a constant for easy validation.
$DB->set_field('lesson_pages', 'timecreated', $timestamp);
$DB->set_field('lesson_pages', 'timemodified', $timestamp);
$DB->set_field('lesson_answers', 'timecreated', $timestamp);
$DB->set_field('lesson_answers', 'timemodified', $timestamp);
$DB->set_field('lesson_attempts', 'timeseen', $timestamp);
// Do backup and restore.
$newcourseid = $this->backup_and_restore($course);
$newlesson = $DB->get_record('lesson', ['course' => $newcourseid]);
$this->assertFieldsNotRolledForward($lesson, $newlesson, ['timemodified']);
$props = ['available', 'deadline'];
$this->assertFieldsRolledForward($lesson, $newlesson, $props);
$newpages = $DB->get_records('lesson_pages', ['lessonid' => $newlesson->id]);
$newanswers = $DB->get_records('lesson_answers', ['lessonid' => $newlesson->id]);
$newgrade = $DB->get_record('lesson_grades', ['lessonid' => $newlesson->id]);
$newoverride = $DB->get_record('lesson_overrides', ['lessonid' => $newlesson->id]);
$newtimer = $DB->get_record('lesson_timer', ['lessonid' => $newlesson->id]);
$newattempt = $DB->get_record('lesson_attempts', ['lessonid' => $newlesson->id]);
// Page time checks.
foreach ($newpages as $newpage) {
$this->assertEquals($timestamp, $newpage->timemodified);
$this->assertEquals($timestamp, $newpage->timecreated);
}
// Page answers time checks.
foreach ($newanswers as $newanswer) {
$this->assertEquals($timestamp, $newanswer->timemodified);
$this->assertEquals($timestamp, $newanswer->timecreated);
}
// Lesson override time checks.
$diff = $this->get_diff();
$this->assertEquals($override->available + $diff, $newoverride->available);
$this->assertEquals($override->deadline + $diff, $newoverride->deadline);
// Lesson grade time checks.
$this->assertEquals($timestamp, $newgrade->completed);
// Lesson timer time checks.
$this->assertEquals($timer->starttime, $newtimer->starttime);
$this->assertEquals($timer->lessontime, $newtimer->lessontime);
// Lesson attempt time check.
$this->assertEquals($timestamp, $newattempt->timeseen);
}
}