Permalink
Browse files

MDL-31589 Sort order of lesson answers before restoring them

  • Loading branch information...
andrewnicols authored and Sam Hemelryk committed Feb 10, 2012
1 parent 42f5a20 commit b32b1013800f90f88194a54c57a1c8517ef65261
Showing with 13 additions and 3 deletions.
  1. +13 −3 mod/lesson/backup/moodle2/restore_lesson_stepslib.php
@@ -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() {
@@ -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) {
@@ -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

0 comments on commit b32b101

Please sign in to comment.