MDL-79268 questions: Show version numbers in delete confirmation

This commit is contained in:
danghieu1407 2024-12-13 13:34:38 +07:00
parent aa55f38820
commit 2b916ec563
4 changed files with 79 additions and 21 deletions

View File

@ -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) . '<br />';
} else {
$questionnames .= $questionname . '<br />';
}
$a = new \stdClass();
$a->name = $questionname;
$a->version = $questionversions[$questionid];
$questionnames .= get_string('questionnameandquestionversion', 'question', $a) . '<br />';
}
// Add the in-use message if required.

View File

@ -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) . '<br />', $message5);
$question->name) . ' v1' . '<br />', $message5);
// Verify confirmation title and confirmation message in history page with multiple question selected.
$listnameofquestionversion6 = 'Question 1<br />* Question 1<br />';
$listnameofquestionversion6 = 'Question 1 v1' . '<br />* Question 1 v2<br />';
[$title6, $message6] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionfirstversionid,
$questionsecondversionid], $deleteallversions);
$this->assertEquals(['confirmtitle' => get_string('deleteversiontitle_plural', 'question')],

View File

@ -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.
*/

View File

@ -323,6 +323,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
*