From 9be598a77fd916d3e9584b42d0741ab46adcfd89 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 16 May 2023 09:23:09 +0100 Subject: [PATCH] MDL-78260 quiz stats: fix View details for random questions --- mod/quiz/locallib.php | 16 +++-- .../report/statistics/statistics_table.php | 6 +- .../tests/behat/random_essays.feature | 38 ------------ .../tests/behat/random_questions.feature | 58 +++++++++++++++++++ 4 files changed, 70 insertions(+), 48 deletions(-) delete mode 100644 mod/quiz/report/statistics/tests/behat/random_essays.feature create mode 100644 mod/quiz/report/statistics/tests/behat/random_questions.feature diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 5d227bfeaf249..c667ca899f979 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -42,6 +42,7 @@ use mod_quiz\question\display_options; use mod_quiz\quiz_attempt; use mod_quiz\quiz_settings; +use qbank_previewquestion\question_preview_options; /** * @var int We show the countdown timer if there is less than this amount of time left before the @@ -1073,17 +1074,22 @@ function quiz_question_preview_url($quiz, $question, $variant = null, $restartve * @param bool $label if true, show the preview question label after the icon * @param int $variant which question variant to preview (optional). * @param bool $random if question is random, true. - * @return the HTML for a preview question icon. + * @return string the HTML for a preview question icon. */ function quiz_question_preview_button($quiz, $question, $label = false, $variant = null, $random = null) { global $PAGE; if (!question_has_capability_on($question, 'use')) { return ''; } - $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); + $structure = quiz_settings::create($quiz->id)->get_structure(); + if (!empty($question->slot)) { + $requestedversion = $structure->get_slot_by_number($question->slot)->requestedversion + ?? question_preview_options::ALWAYS_LATEST; + } else { + $requestedversion = question_preview_options::ALWAYS_LATEST; + } + return $PAGE->get_renderer('mod_quiz', 'edit')->question_preview_icon( + $quiz, $question, $label, $variant, $requestedversion); } /** diff --git a/mod/quiz/report/statistics/statistics_table.php b/mod/quiz/report/statistics/statistics_table.php index 4a5eacd9b2758..d4ed71ab35733 100644 --- a/mod/quiz/report/statistics/statistics_table.php +++ b/mod/quiz/report/statistics/statistics_table.php @@ -204,12 +204,8 @@ protected function col_actions($questionstat) { } else if ($questionstat->question->qtype === 'missingtype') { return ''; } else { - $random = null; - if ($questionstat->question->qtype === 'random') { - $random = true; - } return quiz_question_action_icons($this->quiz, $this->cmid, - $questionstat->question, $this->baseurl, $questionstat->variant, $random); + $questionstat->question, $this->baseurl, $questionstat->variant); } } diff --git a/mod/quiz/report/statistics/tests/behat/random_essays.feature b/mod/quiz/report/statistics/tests/behat/random_essays.feature deleted file mode 100644 index 1cc460cee6b09..0000000000000 --- a/mod/quiz/report/statistics/tests/behat/random_essays.feature +++ /dev/null @@ -1,38 +0,0 @@ -@mod @mod_quiz @quiz @quiz_statistics -Feature: Robustness of the statistics calculations with random essays - In order not to see errors - As a teacher - I need the statistics to work even if the quiz uses a random selection of essays - - Scenario: Statistics can be calculated even after a question type has been uninstalled - Given the following "users" exist: - | username | - | teacher | - | student | - And the following "courses" exist: - | fullname | shortname | - | Course 1 | C1 | - And the following "course enrolments" exist: - | user | course | role | - | teacher | C1 | editingteacher | - | student | C1 | student | - And the following "question categories" exist: - | contextlevel | reference | name | - | Course | C1 | Test questions | - And the following "questions" exist: - | questioncategory | qtype | template | name | questiontext | - | Test questions | essay | plain | Test question 1 | | - | Test questions | essay | plain | Test question 2 | | - | Test questions | random | | Random (Test questions) | 0 | - And the following "activities" exist: - | activity | name | course | idnumber | - | quiz | Quiz 1 | C1 | quiz1 | - And quiz "Quiz 1" contains the following questions: - | question | page | - | Random (Test questions) | 1 | - And user "student" has attempted "Quiz 1" with responses: - | slot | response | - | 1 | Here is my wonderful essay | - - When I am on the "Quiz 1" "mod_quiz > Statistics report" page logged in as teacher - Then I should see "No attempts have been made at this quiz, or all attempts have questions that need manual grading." diff --git a/mod/quiz/report/statistics/tests/behat/random_questions.feature b/mod/quiz/report/statistics/tests/behat/random_questions.feature new file mode 100644 index 0000000000000..89af85f91f19a --- /dev/null +++ b/mod/quiz/report/statistics/tests/behat/random_questions.feature @@ -0,0 +1,58 @@ +@mod @mod_quiz @quiz @quiz_statistics +Feature: Statistics calculations with random questions + In order to verify my quizzes are performing well + As a teacher + I need the statistics to analyse any random questions they contain + + Background: + Given the following "users" exist: + | username | + | teacher | + | student | + And the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | teacher | C1 | editingteacher | + | student | C1 | student | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + + Scenario: Statistics can be calculated for random essays + Given the following "questions" exist: + | questioncategory | qtype | template | name | questiontext | + | Test questions | essay | plain | Test question 1 | | + | Test questions | essay | plain | Test question 2 | | + | Test questions | random | | Random (Test questions) | 0 | + And the following "activities" exist: + | activity | name | course | idnumber | + | quiz | Quiz 1 | C1 | quiz1 | + And quiz "Quiz 1" contains the following questions: + | question | page | + | Random (Test questions) | 1 | + And user "student" has attempted "Quiz 1" with responses: + | slot | response | + | 1 | Here is my wonderful essay | + When I am on the "Quiz 1" "mod_quiz > Statistics report" page logged in as teacher + Then I should see "No attempts have been made at this quiz, or all attempts have questions that need manual grading." + + Scenario: View details works for random questions + Given the following "questions" exist: + | questioncategory | qtype | template | name | questiontext | + | Test questions | multichoice | one_of_four | Test question 1 | | + | Test questions | multichoice | one_of_four | Test question 2 | | + | Test questions | random | | Random (Test questions) | 0 | + And the following "activities" exist: + | activity | name | course | idnumber | + | quiz | Quiz 1 | C1 | quiz1 | + And quiz "Quiz 1" contains the following questions: + | question | page | + | Random (Test questions) | 1 | + And user "student" has attempted "Quiz 1" with responses: + | slot | response | + | 1 | One | + When I am on the "Quiz 1" "mod_quiz > Statistics report" page logged in as teacher + And I follow "View details" + Then I should see "Structural analysis for question number 1"