MDL-74255 quiz: Remove un-needed method question_array_sort

This commit is contained in:
Tim Hunt 2022-03-23 14:57:03 +00:00
parent f62cd4484e
commit 37e8faf0ed
5 changed files with 19 additions and 35 deletions

View file

@ -152,8 +152,8 @@ class quiz {
if (!empty($questionids)) { if (!empty($questionids)) {
$questiondata = \mod_quiz\question\bank\qbank_helper::get_question_structure_data($this->quiz->id, $questionids, true); $questiondata = \mod_quiz\question\bank\qbank_helper::get_question_structure_data($this->quiz->id, $questionids, true);
} }
$allquestiondata = \mod_quiz\question\bank\qbank_helper::question_array_sort( $allquestiondata = \mod_quiz\question\bank\qbank_helper::question_load_random_questions(
\mod_quiz\question\bank\qbank_helper::question_load_random_questions($this->quiz->id, $questiondata), 'slot'); $this->quiz->id, $questiondata);
$this->questions = $allquestiondata; $this->questions = $allquestiondata;
} }

View file

@ -68,23 +68,6 @@ class qbank_helper {
return $DB->get_records_sql($sql, [$questionid]); return $DB->get_records_sql($sql, [$questionid]);
} }
/**
* Sort the elements of an array according to a key.
*
* @param array $arrays
* @param string $on
* @param int $order
* @return array
*/
public static function question_array_sort($arrays, $on, $order = SORT_ASC): array {
$element = [];
foreach ($arrays as $array) {
$element[$array->$on] = $array;
}
ksort($element, $order);
return $element;
}
/** /**
* Get the question id from slot id. * Get the question id from slot id.
* *
@ -204,10 +187,10 @@ class qbank_helper {
/** /**
* Get the question structure data for the given quiz or question ids. * Get the question structure data for the given quiz or question ids.
* *
* @param null $quizid * @param int $quizid ID of the quiz to load data for.
* @param array $questionids * @param array $questionids
* @param bool $attempt * @param bool $attempt if false (default) array key is slot number, else array key is question.id.
* @return array * @return array the question data, ordered by slot number.
*/ */
public static function get_question_structure_data($quizid, $questionids = [], $attempt = false) { public static function get_question_structure_data($quizid, $questionids = [], $attempt = false) {
global $DB; global $DB;
@ -245,16 +228,14 @@ class qbank_helper {
LEFT JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid LEFT JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
LEFT JOIN {question} q ON q.id = qv.questionid LEFT JOIN {question} q ON q.id = qv.questionid
WHERE slot.quizid = :quizid WHERE slot.quizid = :quizid
$condition"; $condition
ORDER BY slot.slot";
$questiondatas = $DB->get_records_sql($sql, $params); $questiondatas = $DB->get_records_sql($sql, $params);
foreach ($questiondatas as $questiondata) { foreach ($questiondatas as $questiondata) {
$questiondata->_partiallyloaded = true; $questiondata->_partiallyloaded = true;
} }
if (!empty($questiondatas)) {
return $questiondatas; return $questiondatas;
} }
return [];
}
/** /**
* Get question structure. * Get question structure.
@ -274,7 +255,9 @@ class qbank_helper {
} }
} }
return self::question_array_sort(array_merge($firstslotsets, $secondslotsets), 'slot'); $data = array_merge($firstslotsets, $secondslotsets);
ksort($data);
return $data;
} }
/** /**
@ -329,7 +312,7 @@ class qbank_helper {
} }
foreach ($randomquestions as $randomquestion) { foreach ($randomquestions as $randomquestion) {
// Should not add if there is no question found from the ramdom question loader, maybe empty category. // Should not add if there is no question found from the random question loader, maybe empty category.
if ($randomquestion->questionid === null) { if ($randomquestion->questionid === null) {
continue; continue;
} }
@ -346,6 +329,8 @@ class qbank_helper {
$questiondata[$question->id] = $question; $questiondata[$question->id] = $question;
} }
uasort($questiondata, function (\stdClass $a, \stdClass $b): int { return ($a->slot <=> $b->slot); });
return $questiondata; return $questiondata;
} }

View file

@ -114,7 +114,7 @@ function quiz_report_get_significant_questions($quiz) {
} }
$qsbyslot[$slotreport->slot] = $slotreport; $qsbyslot[$slotreport->slot] = $slotreport;
} }
$qsbyslot = \mod_quiz\question\bank\qbank_helper::question_array_sort($qsbyslot, 'slot'); ksort($qsbyslot);
$number = 1; $number = 1;
foreach ($qsbyslot as $question) { foreach ($qsbyslot as $question) {
$question->number = $number; $question->number = $number;

View file

@ -856,7 +856,8 @@ class quiz_statistics_report extends quiz_default_report {
foreach ($randomquestions as $randomquestion) { foreach ($randomquestions as $randomquestion) {
$questiondata[$randomquestion->slot] = $randomquestion; $questiondata[$randomquestion->slot] = $randomquestion;
} }
return \mod_quiz\question\bank\qbank_helper::question_array_sort($questiondata, 'slot'); ksort($questiondata);
return $questiondata;
} }
/** /**

View file

@ -80,7 +80,6 @@ class qbank_helper_test extends \advanced_testcase {
* @covers ::get_question_for_redo * @covers ::get_question_for_redo
* @covers ::get_always_latest_version_question_ids * @covers ::get_always_latest_version_question_ids
* @covers ::question_load_random_questions * @covers ::question_load_random_questions
* @covers ::question_array_sort
*/ */
public function test_reference_records() { public function test_reference_records() {
$this->resetAfterTest(); $this->resetAfterTest();
@ -130,7 +129,6 @@ class qbank_helper_test extends \advanced_testcase {
* *
* @covers ::get_question_structure * @covers ::get_question_structure
* @covers ::get_question_structure_data * @covers ::get_question_structure_data
* @covers ::question_array_sort
* @covers ::get_always_latest_version_question_ids * @covers ::get_always_latest_version_question_ids
* @covers ::question_load_random_questions * @covers ::question_load_random_questions
*/ */