Skip to content

Commit

Permalink
MDL-77136 core_question: Newest versions get_questions_from_categories
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Dec 6, 2023
1 parent abba174 commit b53ab7e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
11 changes: 9 additions & 2 deletions question/engine/bank.php
Expand Up @@ -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.
Expand All @@ -549,13 +549,20 @@ 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
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
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);
Expand Down
43 changes: 43 additions & 0 deletions question/engine/tests/questionbank_test.php
Expand Up @@ -16,6 +16,7 @@

namespace core_question;

use core_question\local\bank\question_version_status;
use qubaid_list;
use question_bank;
use question_engine;
Expand Down Expand Up @@ -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], ""));
}
}
6 changes: 6 additions & 0 deletions question/type/randomsamatch/question.php
Expand Up @@ -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) {
Expand Down

0 comments on commit b53ab7e

Please sign in to comment.