Skip to content

Commit

Permalink
MDL-31589 Sort order of lesson answers before restoring them
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Robert Nicols authored and Sam Hemelryk committed Feb 20, 2012
1 parent 22f94b8 commit 9ef38ab
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions mod/lesson/backup/moodle2/restore_lesson_stepslib.php
Expand Up @@ -30,6 +30,9 @@
* Structure step to restore one lesson activity
*/
class restore_lesson_activity_structure_step extends restore_activity_structure_step {
// Store the answers as they're received but only process them at the
// end of the lesson
protected $answers = array();

protected function define_structure() {

Expand Down Expand Up @@ -94,15 +97,15 @@ protected function process_lesson_answer($data) {
global $DB;

$data = (object)$data;
$oldid = $data->id;
$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);

$newitemid = $DB->insert_record('lesson_answers', $data);
$this->set_mapping('lesson_answer', $oldid, $newitemid);
// Answers need to be processed in order, so we store them in an
// instance variable and insert them in the after_execute stage
$this->answers[$data->id] = $data;
}

protected function process_lesson_attempt($data) {
Expand Down Expand Up @@ -173,6 +176,13 @@ protected function process_lesson_timer($data) {
protected function after_execute() {
global $DB;

// Answers must be sorted by id to ensure that they're shown correctly
ksort($this->answers);
foreach ($this->answers as $answer) {
$newitemid = $DB->insert_record('lesson_answers', $answer);
$this->set_mapping('lesson_answer', $answer->id, $newitemid);
}

// Add lesson mediafile, no need to match by itemname (just internally handled context)
$this->add_related_files('mod_lesson', 'mediafile', null);
// Add lesson page files, by lesson_page itemname
Expand Down

0 comments on commit 9ef38ab

Please sign in to comment.