Merge branch 'MDL-67830' of https://github.com/timhunt/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 5 Feb 2020 18:07:55 +0000 (19:07 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 5 Feb 2020 18:07:55 +0000 (19:07 +0100)
mod/quiz/attemptlib.php
mod/quiz/grade.php

index 0e7d128..69dd30e 100644 (file)
@@ -576,25 +576,12 @@ class quiz_attempt {
      *      of the state of each question. Else just set up the basic details of the attempt.
      */
     public function __construct($attempt, $quiz, $cm, $course, $loadquestions = true) {
-        global $DB;
-
         $this->attempt = $attempt;
         $this->quizobj = new quiz($quiz, $cm, $course);
 
-        if (!$loadquestions) {
-            return;
+        if ($loadquestions) {
+            $this->load_questions();
         }
-
-        $this->quba = question_engine::load_questions_usage_by_activity($this->attempt->uniqueid);
-        $this->slots = $DB->get_records('quiz_slots',
-                array('quizid' => $this->get_quizid()), 'slot',
-                'slot, id, requireprevious, questionid, includingsubcategories');
-        $this->sections = array_values($DB->get_records('quiz_sections',
-                array('quizid' => $this->get_quizid()), 'firstslot'));
-
-        $this->link_sections_and_slots();
-        $this->determine_layout();
-        $this->number_questions();
     }
 
     /**
@@ -643,6 +630,28 @@ class quiz_attempt {
         return quiz_attempt_state_name($state);
     }
 
+    /**
+     * This method can be called later if the object was constructed with $loadqusetions = false.
+     */
+    public function load_questions() {
+        global $DB;
+
+        if (isset($this->quba)) {
+            throw new coding_exception('This quiz attempt has already had the questions loaded.');
+        }
+
+        $this->quba = question_engine::load_questions_usage_by_activity($this->attempt->uniqueid);
+        $this->slots = $DB->get_records('quiz_slots',
+                array('quizid' => $this->get_quizid()), 'slot',
+                'slot, id, requireprevious, questionid, includingsubcategories');
+        $this->sections = array_values($DB->get_records('quiz_sections',
+                array('quizid' => $this->get_quizid()), 'firstslot'));
+
+        $this->link_sections_and_slots();
+        $this->determine_layout();
+        $this->number_questions();
+    }
+
     /**
      * Let each slot know which section it is part of.
      */
index f7ca5cb..970aa6f 100644 (file)
@@ -81,6 +81,7 @@ if ($userid) {
     if ($attempt) {
         $attemptobj = new quiz_attempt($attempt, $quiz, $cm, $course, false);
         if ($attemptobj->is_review_allowed()) {
+            $attemptobj->load_questions();
             redirect($attemptobj->review_url());
         }
     }