MDL-77136 core_question: Newest versions get_questions_from_categories

This commit is contained in:
Luca Bösch 2023-12-05 11:49:51 +01:00
parent 76ff7600e4
commit bbb8622464
3 changed files with 58 additions and 2 deletions

View File

@ -528,8 +528,8 @@ class question_finder implements cache_data_source {
/**
* 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 @@ class question_finder implements cache_data_source {
$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 @@ class question_finder implements cache_data_source {
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);

View File

@ -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 @@ class questionbank_test extends \advanced_testcase {
$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], ""));
}
}

View File

@ -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) {