Skip to content

Commit

Permalink
MDL-75301 quiz: Use "always latest" option for question previews
Browse files Browse the repository at this point in the history
This will set the "alwayslatest" option when previewing a question from
the quiz according to the version setting used in the quiz slot.
  • Loading branch information
marxjohnson committed Apr 5, 2023
1 parent 23630ec commit 96bada8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
9 changes: 6 additions & 3 deletions mod/quiz/classes/output/edit_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use mod_quiz\question\bank\qbank_helper;
use \mod_quiz\structure;
use \html_writer;
use qbank_previewquestion\question_preview_options;
use renderable;

/**
Expand Down Expand Up @@ -837,11 +838,12 @@ public function get_question_name_for_slot(structure $structure, int $slot, \moo
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);
Expand Down Expand Up @@ -885,16 +887,17 @@ public function question_number($number) {
* 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 = '';
Expand Down
10 changes: 7 additions & 3 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions mod/quiz/tests/behat/quiz_question_versions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 96bada8

Please sign in to comment.