Skip to content

Commit

Permalink
Merge branch 'MDL-74007' of https://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Mar 9, 2022
2 parents 87da563 + 61f11f8 commit 558f852
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
4 changes: 3 additions & 1 deletion backup/tests/quiz_restore_decode_links_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function test_restore_quiz_decode_links() {

// Add to the quiz.
quiz_add_quiz_question($question->id, $quiz);
mod_quiz\external\submit_question_version::execute(
$DB->get_field('quiz_slots', 'id', ['quizid' => $quiz->id, 'slot' => 1]), 1);

$questiondata = question_bank::load_question_data($question->id);

Expand All @@ -81,7 +83,7 @@ public function test_restore_quiz_decode_links() {
$quizquestions = \mod_quiz\question\bank\qbank_helper::get_question_structure_data($newcm->instance);
$questionids = [];
foreach ($quizquestions as $quizquestion) {
$questionids [] = $quizquestion->id;
$questionids[] = $quizquestion->id;
}
list($condition, $param) = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED, 'questionid');
$condition = 'WHERE qa.question ' . $condition;
Expand Down
7 changes: 2 additions & 5 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2396,9 +2396,7 @@ function quiz_add_quiz_question($questionid, $quiz, $page = 0, $maxmark = null)
$questionreferences->questionarea = 'slot';
$questionreferences->itemid = $slotid;
$questionreferences->questionbankentryid = get_question_bank_entry($questionid)->id;
$version = get_question_version($questionid);
$questionreferences->version = $version[array_key_first($version)]->version;

$questionreferences->version = null; // Always latest.
$DB->insert_record('question_references', $questionreferences);

} else if ($qreferenceitem->itemid === 0 || $qreferenceitem->itemid === null) {
Expand All @@ -2414,8 +2412,7 @@ function quiz_add_quiz_question($questionid, $quiz, $page = 0, $maxmark = null)
$questionreferences->questionarea = 'slot';
$questionreferences->itemid = $slotid;
$questionreferences->questionbankentryid = get_question_bank_entry($questionid)->id;
$version = get_question_version($questionid);
$questionreferences->version = $version[array_key_first($version)]->version;
$questionreferences->version = null; // Always latest.
$DB->insert_record('question_references', $questionreferences);
}

Expand Down
24 changes: 24 additions & 0 deletions mod/quiz/tests/behat/quiz_question_versions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,27 @@ Feature: Quiz question versioning
And I press "id_saveupdate"
And I click on "finish" "button"
Then I should see "The correct answer is 'False'."

@javascript
Scenario: Creating a new question should have always latest in the version selection
When I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1"
And I click on "Add" "link"
And I follow "a new question"
And I set the field "item_qtype_essay" to "1"
And I press "submitbutton"
Then I should see "Adding an Essay question"
And I set the field "Question name" to "Essay 01 new"
And I set the field "Question text" to "Please write 200 words about Essay 01"
And I press "id_submitbutton"
And I should see "Essay 01 new" on quiz page "1"
And I should see "Always latest" on quiz page "1"

@javascript
Scenario: Adding a question from question bank should have always latest in the version selection
When I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1"
And I click on "Add" "link"
And I follow "from question bank"
And I set the field with xpath "//input[@type='checkbox' and @id='qbheadercheckbox']" to "1"
And I press "Add selected questions to the quiz"
And I should see "First question" on quiz page "1"
And I should see "Always latest" on quiz page "1"
2 changes: 2 additions & 0 deletions mod/quiz/tests/qbank_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public function test_get_question_version_info() {
* @covers ::get_specific_version_question_ids
*/
public function test_get_specific_version_question_ids() {
global $DB;
$this->resetAfterTest();
$quiz = $this->create_test_quiz($this->course);
// Test for questions from a different context.
Expand All @@ -217,6 +218,7 @@ public function test_get_specific_version_question_ids() {
$questiongenerator->update_question($numq, null, ['name' => 'This is the second version']);
$questiongenerator->update_question($numq, null, ['name' => 'This is the third version']);
quiz_add_quiz_question($numq->id, $quiz);
submit_question_version::execute($DB->get_field('quiz_slots', 'id', ['quizid' => $quiz->id, 'slot' => 1]), 3);
$specificversionquestionid = qbank_helper::get_specific_version_question_ids($quiz->id);
$specificversionquestionid = reset($specificversionquestionid);
$this->assertEquals($numq->id, $specificversionquestionid);
Expand Down
31 changes: 15 additions & 16 deletions mod/quiz/tests/quiz_question_version_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,30 @@ public function test_quiz_questions_for_changed_versions() {
$structure = \mod_quiz\structure::create_for_quiz($quizobj);
$slots = $structure->get_slots();
$slot = reset($slots);
// Test that the version added is the latest version, as there are three created.
// Test that the version added is 'always latest'.
$this->assertEquals(3, $slot->version);
$quizobj->preload_questions();
$quizobj->load_questions();
$questions = $quizobj->get_questions();
$question = reset($questions);
$this->assertEquals(3, $question->version);
$this->assertEquals('This is the third version', $question->name);
// Create another version.
$questiongenerator->update_question($numq, null, ['name' => 'This is the latest version']);
// Check that 'Always latest is working'.
$quizobj->preload_questions();
$quizobj->load_questions();
$questions = $quizobj->get_questions();
$question = reset($questions);
$this->assertEquals(4, $question->version);
$this->assertEquals('This is the latest version', $question->name);
$structure = \mod_quiz\structure::create_for_quiz($quizobj);
$slots = $structure->get_slots();
$slot = reset($slots);
$this->assertEquals(4, $slot->version);
// Now change the version using the external service.
$versions = qbank_helper::get_version_options($slot->questionid);
// We dont want the current version.
// We don't want the current version.
$selectversions = [];
foreach ($versions as $version) {
if ($version->version === $slot->version) {
Expand Down Expand Up @@ -114,20 +127,6 @@ public function test_quiz_questions_for_changed_versions() {
$slots = $structure->get_slots();
$slot = reset($slots);
$this->assertEquals(2, $slot->version);
// Create another version.
$questiongenerator->update_question($numq, null, ['name' => 'This is the latest version']);
// Change to always latest.
submit_question_version::execute($slot->id, 0);
$quizobj->preload_questions();
$quizobj->load_questions();
$questions = $quizobj->get_questions();
$question = reset($questions);
$this->assertEquals(4, $question->version);
$this->assertEquals('This is the latest version', $question->name);
$structure = \mod_quiz\structure::create_for_quiz($quizobj);
$slots = $structure->get_slots();
$slot = reset($slots);
$this->assertEquals(4, $slot->version);
}

/**
Expand Down

0 comments on commit 558f852

Please sign in to comment.