diff --git a/question/bank/deletequestion/classes/helper.php b/question/bank/deletequestion/classes/helper.php
index 29488989b07..0e58d0cf9db 100644
--- a/question/bank/deletequestion/classes/helper.php
+++ b/question/bank/deletequestion/classes/helper.php
@@ -40,17 +40,18 @@ class helper {
$questionversions = [];
$countselectedquestion = count($questionids);
- if ($deleteallversions) {
- $versionsofeachquestionbankentry = \question_bank::get_all_versions_of_questions($questionids);
- foreach ($versionsofeachquestionbankentry as $entryid => $versions) {
- // Re-order to oldest first.
- $versionsofeachquestionbankentry[$entryid] = array_reverse($versions, true);
- // Flip the array to list question by question id. [ qid => version ].
- $questionversions += array_flip($versions);
- }
- // Flatten an array.
- $questionids = array_merge(...$versionsofeachquestionbankentry);
+ $versionsofeachquestionbankentry = $deleteallversions
+ ? \question_bank::get_all_versions_of_questions($questionids)
+ : \question_bank::get_version_of_questions($questionids);
+
+ foreach ($versionsofeachquestionbankentry as $entryid => $versions) {
+ // Re-order to the oldest first.
+ $versionsofeachquestionbankentry[$entryid] = array_reverse($versions, true);
+ // Flip the array to list question by question id. [ qid => version ].
+ $questionversions += array_flip($versions);
}
+ // Flatten an array.
+ $questionids = array_merge(...$versionsofeachquestionbankentry);
// Get the names of all the questions.
$questions = $DB->get_records_list('question', 'id', $questionids, '', 'id, name');
@@ -62,15 +63,11 @@ class helper {
$inuse = true;
}
$questionname = format_string($questions[$questionid]->name);
- if (isset($questionversions[$questionid])) {
- $a = new \stdClass();
- $a->name = $questionname;
- $a->version = $questionversions[$questionid];
- $questionnames .= get_string('questionnameandquestionversion',
- 'question', $a) . '
';
- } else {
- $questionnames .= $questionname . '
';
- }
+
+ $a = new \stdClass();
+ $a->name = $questionname;
+ $a->version = $questionversions[$questionid];
+ $questionnames .= get_string('questionnameandquestionversion', 'question', $a) . '
';
}
// Add the in-use message if required.
diff --git a/question/bank/deletequestion/tests/helper_test.php b/question/bank/deletequestion/tests/helper_test.php
index 4333d1c9bb6..4b686d95490 100644
--- a/question/bank/deletequestion/tests/helper_test.php
+++ b/question/bank/deletequestion/tests/helper_test.php
@@ -125,10 +125,10 @@ final class helper_test extends \advanced_testcase {
$this->assertEquals(['confirmtitle' => get_string('deleteversiontitle', 'question')],
$title5);
$this->assertEquals(get_string('deleteselectedquestioncheck', 'question',
- $question->name) . '
', $message5);
+ $question->name) . ' v1' . '
', $message5);
// Verify confirmation title and confirmation message in history page with multiple question selected.
- $listnameofquestionversion6 = 'Question 1
* Question 1
';
+ $listnameofquestionversion6 = 'Question 1 v1' . '
* Question 1 v2
';
[$title6, $message6] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionfirstversionid,
$questionsecondversionid], $deleteallversions);
$this->assertEquals(['confirmtitle' => get_string('deleteversiontitle_plural', 'question')],
diff --git a/question/engine/bank.php b/question/engine/bank.php
index 84e86211d11..76329f9159a 100644
--- a/question/engine/bank.php
+++ b/question/engine/bank.php
@@ -342,6 +342,31 @@ abstract class question_bank {
return $result;
}
+ /**
+ * Retrieves version information for a list of questions.
+ *
+ * @param array $questionids Array of question ids.
+ * @return array An array question_bank_entries.id => version number => question.id.
+ */
+ public static function get_version_of_questions(array $questionids): array {
+ global $DB;
+
+ [$listquestionid, $params] = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED);
+ $sql = "SELECT qv.questionid, qv.version, qv.questionbankentryid
+ FROM {question_versions} qv
+ JOIN {question_bank_entries} qbe ON qv.questionbankentryid = qbe.id
+ WHERE qv.questionid $listquestionid
+ ORDER BY qv.version DESC";
+
+ $rows = $DB->get_recordset_sql($sql, $params);
+ $result = [];
+ foreach ($rows as $row) {
+ $result[$row->questionbankentryid][$row->version] = $row->questionid;
+ }
+
+ return $result;
+ }
+
/**
* @return question_finder a question finder.
*/
diff --git a/question/tests/version_test.php b/question/tests/version_test.php
index cc96cdd5292..eaea4126fea 100644
--- a/question/tests/version_test.php
+++ b/question/tests/version_test.php
@@ -324,6 +324,42 @@ final class version_test extends \advanced_testcase {
$this->assertEquals($questionversions, $questionversionsofquestions[$questionbankentryids]);
}
+ /**
+ * Test the get_version_of_questions function.
+ *
+ * @covers ::get_version_of_questions
+ */
+ public function test_get_version_of_questions(): void {
+ global $DB;
+
+ $qcategory = $this->qgenerator->create_question_category(['contextid' => $this->context->id]);
+ $question = $this->qgenerator->create_question('shortanswer', null, ['category' => $qcategory->id]);
+
+ // Update the question to create new versions.
+ $question = $this->qgenerator->update_question($question, null, ['name' => 'Version 2']);
+
+ // Get the current version of the question.
+ $currentversions = question_bank::get_version_of_questions([$question->id]);
+
+ // Get questionbankentryid for assertion.
+ $questionbankentryid = $DB->get_field('question_versions', 'questionbankentryid',
+ ['questionid' => $question->id]);
+
+ // Assert that the structure matches.
+ $this->assertArrayHasKey($questionbankentryid, $currentversions);
+ $this->assertArrayHasKey(2, $currentversions[$questionbankentryid]);
+ $this->assertEquals($question->id, $currentversions[$questionbankentryid][2]);
+
+ // Update the question to create new versions.
+ $question = $this->qgenerator->update_question($question, null, ['name' => 'Version 3']);
+ $currentversions = question_bank::get_version_of_questions([$question->id]);
+
+ // Assert the updated version.
+ $this->assertArrayHasKey($questionbankentryid, $currentversions);
+ $this->assertArrayHasKey(3, $currentversions[$questionbankentryid]);
+ $this->assertEquals($question->id, $currentversions[$questionbankentryid][3]);
+ }
+
/**
* Test population of latestversion field in question_definition objects
*