diff --git a/question/engine/bank.php b/question/engine/bank.php index 8605aabe1af7e..6122433718456 100644 --- a/question/engine/bank.php +++ b/question/engine/bank.php @@ -532,8 +532,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. @@ -549,6 +549,7 @@ public function get_questions_from_categories($categoryids, $extraconditions, $extraconditions = ' AND (' . $extraconditions . ')'; } $qcparams['readystatus'] = question_version_status::QUESTION_STATUS_READY; + $qcparams['readystatusqv'] = 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 @@ -556,6 +557,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 dea9e7f704a7a..c21c45841a171 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; @@ -97,4 +98,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..587b88e6d932d 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 subactegories 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) {