diff --git a/question/engine/bank.php b/question/engine/bank.php index 92f3cc25707c5..8fc5337d0a947 100644 --- a/question/engine/bank.php +++ b/question/engine/bank.php @@ -528,8 +528,8 @@ public function load_question_data($questionid) { /** * Get the ids of all the questions in a list of categories. - * @param array $categoryids either a categoryid, or a comma-separated list - * category ids, or an array of them. + * @param array $categoryids either a category id, or a comma-separated list + * of category ids, or an array of them. * @param string $extraconditions extra conditions to AND with the rest of * the where clause. Must use named parameters. * @param array $extraparams any parameters used by $extraconditions. @@ -545,6 +545,7 @@ public function get_questions_from_categories($categoryids, $extraconditions, $extraconditions = ' AND (' . $extraconditions . ')'; } $qcparams['readystatus'] = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY; + $qcparams['readystatusqv'] = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY; $sql = "SELECT q.id, q.id AS id2 FROM {question} q JOIN {question_versions} qv ON qv.questionid = q.id @@ -552,6 +553,12 @@ public function get_questions_from_categories($categoryids, $extraconditions, WHERE qbe.questioncategoryid {$qcsql} AND q.parent = 0 AND qv.status = :readystatus + AND qv.version = (SELECT MAX(v.version) + FROM {question_versions} v + JOIN {question_bank_entries} be + ON be.id = v.questionbankentryid + WHERE be.id = qbe.id + AND v.status = :readystatusqv) {$extraconditions}"; return $DB->get_records_sql_menu($sql, $qcparams + $extraparams); diff --git a/question/engine/tests/questionbank_test.php b/question/engine/tests/questionbank_test.php index a81aac2a5434e..ac95340379fdb 100644 --- a/question/engine/tests/questionbank_test.php +++ b/question/engine/tests/questionbank_test.php @@ -16,6 +16,7 @@ namespace core_question; +use core_question\local\bank\question_version_status; use qubaid_list; use question_bank; use question_engine; @@ -124,4 +125,46 @@ public function test_load_many_for_cache_missing_id() { $this->expectException(\dml_missing_record_exception::class); question_finder::get_instance()->load_many_for_cache([-1]); } + + /** + * Test get_questions_from_categories. + * + * @covers \question_finder::get_questions_from_categories + * + * @return void + */ + public function test_get_questions_from_categories(): void { + $this->resetAfterTest(); + + /** @var core_question_generator $questiongenerator */ + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + + // Create three questions in a question bank category, each with three versions. + // The first question has all three versions in status ready. + $cat = $questiongenerator->create_question_category(); + $q1v1 = $questiongenerator->create_question('truefalse', null, ['name' => 'Q1V1', 'category' => $cat->id]); + $q1v2 = $questiongenerator->update_question($q1v1, null, ['name' => 'Q1V2']); + $q1v3 = $questiongenerator->update_question($q1v2, null, ['name' => 'Q1V3']); + // The second question has the first version in status draft, the second version in status ready, + // and third version in status draft. + $q2v1 = $questiongenerator->create_question('numerical', null, ['name' => 'Q2V2', 'category' => $cat->id, + 'status' => question_version_status::QUESTION_STATUS_DRAFT, ]); + $q2v2 = $questiongenerator->update_question($q2v1, null, ['name' => 'Q2V2', + 'status' => question_version_status::QUESTION_STATUS_READY, ]); + $q2v3 = $questiongenerator->update_question($q2v2, null, + ['name' => 'Q2V3', 'status' => question_version_status::QUESTION_STATUS_DRAFT]); + // The third question has all three version in status draft. + $q3v1 = $questiongenerator->create_question('shortanswer', null, ['name' => 'Q3V1', 'category' => $cat->id, + 'status' => question_version_status::QUESTION_STATUS_DRAFT, ]); + $q3v2 = $questiongenerator->update_question($q3v1, null, ['name' => 'Q3V2', + 'status' => question_version_status::QUESTION_STATUS_DRAFT, ]); + $q3v3 = $questiongenerator->update_question($q3v2, null, ['name' => 'Q3V3', + 'status' => question_version_status::QUESTION_STATUS_DRAFT]); + + // Test the returned array of questions in that category is the desired one with version three of the first + // question, version two of the second question, and the third question omitted completely since there are + // only draft versions. + $this->assertEquals([$q1v3->id => $q1v3->id, $q2v2->id => $q2v2->id], + question_bank::get_finder()->get_questions_from_categories([$cat->id], "")); + } } diff --git a/question/type/randomsamatch/question.php b/question/type/randomsamatch/question.php index f773b9efd9e2e..a47f747e94851 100644 --- a/question/type/randomsamatch/question.php +++ b/question/type/randomsamatch/question.php @@ -37,6 +37,12 @@ class qtype_randomsamatch_question extends qtype_match_question { /** @var qtype_randomsamatch_question_loader helper for loading the shortanswer questions. */ public $questionsloader; + /** @var int the number of subquestions to randomly create. */ + public $choose; + + /** @var bool whether to include questions from subcategories when making the random selection. */ + public $subcats; + public function start_attempt(question_attempt_step $step, $variant) { $saquestions = $this->questionsloader->load_questions(); foreach ($saquestions as $wrappedquestion) { diff --git a/question/type/randomsamatch/tests/behat/add.feature b/question/type/randomsamatch/tests/behat/add.feature new file mode 100644 index 0000000000000..8f842397f5ae4 --- /dev/null +++ b/question/type/randomsamatch/tests/behat/add.feature @@ -0,0 +1,79 @@ +@qtype @qtype_randomsamatch +Feature: Test creating a Random short-answer matching question + As a teacher + In order to test my students + I need to be able to create a Random short-answer matching question + + Background: + Given the following "users" exist: + | username | + | teacher | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher | C1 | editingteacher | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Category 1 | + And the following "questions" exist: + | questioncategory | qtype | name | template | + | Category 1 | shortanswer | Short answer question A version 1 | frogtoad | + | Category 1 | shortanswer | Short answer question B version 1 | frogtoad | + | Category 1 | shortanswer | Short answer question C version 1 | frogtoad | + + Scenario: Create a too large size of options Random short-answer matching question + When I am on the "Course 1" "core_question > course question bank" page logged in as teacher + And I add a "Random short-answer matching" question filling the form with: + | Category | Category 1 | + | Question name | Random short-answer matching | + | Question text | Random short-answer matching | + | Default mark | 1 | + | Number of questions to select | 4 | + Then I should see "There is/are only 3 short answer questions in the category" + + Scenario: Create a Random short-answer matching question + When I am on the "Course 1" "core_question > course question bank" page logged in as teacher + # Edit the first Short answer question so a version 2 is created. + And I am on the "Short answer question A version 1" "core_question > edit" page logged in as teacher + And I set the following fields to these values: + | Question name | Short answer question A version 2 | + | Question text | Short answer question A version 2 | + | id_answer_0 | Short answer Question A Version 2 Answer 1 | + | id_answer_1 | Short answer Question A Version 2 Answer 2 | + | id_answer_2 | Short answer Question A Version 2 Answer 3 | + And I press "id_submitbutton" + And I should see "Short answer question A version 2" + # Edit the second Short answer question so a version 2 is created. + And I am on the "Short answer question B version 1" "core_question > edit" page logged in as teacher + And I set the following fields to these values: + | Question name | Short answer question B version 2 | + | Question text | Short answer question B version 2 | + | id_answer_0 | Short answer Question B Version 2 Answer 1 | + | id_answer_1 | Short answer Question B Version 2 Answer 2 | + | id_answer_2 | Short answer Question B Version 2 Answer 3 | + And I press "id_submitbutton" + And I should see "Short answer question B version 2" + # Edit the third Short answer question so a version 2 is created. + And I am on the "Short answer question C version 1" "core_question > edit" page logged in as teacher + And I set the following fields to these values: + | Question name | Short answer question C version 2 | + | Question text | Short answer question C version 2 | + | id_answer_0 | Short answer Question C Version 2 Answer 1 | + | id_answer_1 | Short answer Question C Version 2 Answer 2 | + | id_answer_2 | Short answer Question C Version 2 Answer 3 | + And I press "id_submitbutton" + And I should see "Short answer question C version 2" + # Create the Random short-answer question. + And I am on the "Course 1" "core_question > course question bank" page logged in as teacher + And I add a "Random short-answer matching" question filling the form with: + | Category | Category 1 | + | Question name | Random short-answer matching | + | Question text | Random short-answer matching | + | Default mark | 1 | + | Number of questions to select | 3 | + And I should see "Random short-answer matching" + And I am on the "Random short-answer matching" "core_question > preview" page logged in as teacher + Then I should not see "Name an amphibian:" + And I should see "Version 2"