From 96bada849543d4b14d95539d27e39ee0d14e6085 Mon Sep 17 00:00:00 2001
From: Mark Johnson <mark.johnson@catalyst-eu.net>
Date: Fri, 17 Mar 2023 12:34:56 +0000
Subject: [PATCH] MDL-75301 quiz: Use "always latest" option for question
 previews

This will set the "alwayslatest" option when previewing a question from
the quiz according to the version setting used in the quiz slot.
---
 mod/quiz/classes/output/edit_renderer.php        |  9 ++++++---
 mod/quiz/locallib.php                            | 10 +++++++---
 .../tests/behat/quiz_question_versions.feature   | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/mod/quiz/classes/output/edit_renderer.php b/mod/quiz/classes/output/edit_renderer.php
index 5158e1e8ec0..b2e7b816e18 100644
--- a/mod/quiz/classes/output/edit_renderer.php
+++ b/mod/quiz/classes/output/edit_renderer.php
@@ -28,6 +28,7 @@ use core_question\local\bank\question_version_status;
 use mod_quiz\question\bank\qbank_helper;
 use \mod_quiz\structure;
 use \html_writer;
+use qbank_previewquestion\question_preview_options;
 use renderable;
 
 /**
@@ -837,11 +838,12 @@ class edit_renderer extends \plugin_renderer_base {
     public function get_action_icon(structure $structure, int $slot, \moodle_url $pageurl) : string {
         // Action icons.
         $qtype = $structure->get_question_type_for_slot($slot);
+        $slotinfo = $structure->get_slot_by_number($slot);
         $questionicons = '';
         if ($qtype !== 'random') {
             $questionicons .= $this->question_preview_icon($structure->get_quiz(),
                     $structure->get_question_in_slot($slot),
-                    null, null, $qtype);
+                    null, null, $slotinfo->requestedversion ?: question_preview_options::ALWAYS_LATEST);
         }
         if ($structure->can_be_edited() && $structure->has_use_capability($slot)) {
             $questionicons .= $this->question_remove_icon($structure, $slot, $pageurl);
@@ -885,16 +887,17 @@ class edit_renderer extends \plugin_renderer_base {
      *      If ->questionid is set, that is used instead of ->id.
      * @param bool $label if true, show the preview question label after the icon
      * @param int $variant which question variant to preview (optional).
+     * @param int $restartversion version to use when restarting the preview
      * @return string HTML to output.
      */
-    public function question_preview_icon($quiz, $questiondata, $label = null, $variant = null) {
+    public function question_preview_icon($quiz, $questiondata, $label = null, $variant = null, $restartversion = null) {
         $question = clone($questiondata);
         if (isset($question->questionid)) {
 
             $question->id = $question->questionid;
         }
 
-        $url = quiz_question_preview_url($quiz, $question, $variant);
+        $url = quiz_question_preview_url($quiz, $question, $variant, $restartversion);
 
         // Do we want a label?
         $strpreviewlabel = '';
diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php
index faaa83f9351..5d227bfeaf2 100644
--- a/mod/quiz/locallib.php
+++ b/mod/quiz/locallib.php
@@ -1049,9 +1049,10 @@ function quiz_question_edit_button($cmid, $question, $returnurl, $contentafteric
  * @param stdClass $quiz the quiz settings
  * @param stdClass $question the question
  * @param int $variant which question variant to preview (optional).
+ * @param int $restartversion version of the question to use when restarting the preview.
  * @return moodle_url to preview this question with the options from this quiz.
  */
-function quiz_question_preview_url($quiz, $question, $variant = null) {
+function quiz_question_preview_url($quiz, $question, $variant = null, $restartversion = null) {
     // Get the appropriate display options.
     $displayoptions = display_options::make_from_quiz($quiz,
             display_options::DURING);
@@ -1063,7 +1064,7 @@ function quiz_question_preview_url($quiz, $question, $variant = null) {
 
     // Work out the correcte preview URL.
     return \qbank_previewquestion\helper::question_preview_url($question->id, $quiz->preferredbehaviour,
-            $maxmark, $displayoptions, $variant);
+            $maxmark, $displayoptions, $variant, null, null, $restartversion);
 }
 
 /**
@@ -1079,7 +1080,10 @@ function quiz_question_preview_button($quiz, $question, $label = false, $variant
     if (!question_has_capability_on($question, 'use')) {
         return '';
     }
-    return $PAGE->get_renderer('mod_quiz', 'edit')->question_preview_icon($quiz, $question, $label, $variant, null);
+    $slotinfo = quiz_settings::create($quiz->id)->get_structure()->get_slot_by_number($question->slot);
+    return $PAGE->get_renderer('mod_quiz', 'edit')
+        ->question_preview_icon($quiz, $question, $label, $variant,
+                $slotinfo->requestedversion ?: \qbank_previewquestion\question_preview_options::ALWAYS_LATEST);
 }
 
 /**
diff --git a/mod/quiz/tests/behat/quiz_question_versions.feature b/mod/quiz/tests/behat/quiz_question_versions.feature
index 356f5ec9473..d26065a120d 100644
--- a/mod/quiz/tests/behat/quiz_question_versions.feature
+++ b/mod/quiz/tests/behat/quiz_question_versions.feature
@@ -121,3 +121,19 @@ Feature: Quiz question versioning
     And I set the field "question_status_dropdown" in the "First question" "table_row" to "Draft"
     When I am on the "Quiz 1" "mod_quiz > Edit" page
     Then I should see "This question has all versions in Draft status. The quiz will not work with this question in place."
+
+  @javascript
+  Scenario: Previewing a question set to use always latest version will set the preview to always latest version
+    When I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher"
+    And the field "version" in the "First question" "list_item" matches value "Always latest"
+    When I follow "Preview question"
+    And I expand all fieldsets
+    Then the field "Question version" matches value "Always latest"
+
+  @javascript
+  Scenario: Previewing a question set to use a specific version will set the preview to that version
+    When I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher"
+    And I set the field "version" to "v1 (latest)"
+    When I follow "Preview question"
+    And I expand all fieldsets
+    Then the field "Question version" matches value "1"