Preserve randomized question order in test results
This commit is contained in:
@@ -25,6 +25,26 @@ class HtmlView extends BaseHtmlView
|
||||
$this->items = $this->get('Items');
|
||||
$this->sets = $this->get('Sets');
|
||||
$input = Factory::getApplication()->getInput();
|
||||
if ($input->getCmd('task') === 'check' && $input->getMethod() === 'POST' && $this->items) {
|
||||
$submittedQuestionOrder = array_map('intval', (array) $input->post->get('question_order', [], 'array'));
|
||||
$expectedQuestionIds = array_map(static function ($item) {
|
||||
return (int) $item->id;
|
||||
}, $this->items);
|
||||
$sortedSubmittedOrder = $submittedQuestionOrder;
|
||||
$sortedExpectedIds = $expectedQuestionIds;
|
||||
sort($sortedSubmittedOrder);
|
||||
sort($sortedExpectedIds);
|
||||
|
||||
if ($sortedSubmittedOrder === $sortedExpectedIds) {
|
||||
$itemsById = [];
|
||||
foreach ($this->items as $item) {
|
||||
$itemsById[(int) $item->id] = $item;
|
||||
}
|
||||
$this->items = array_values(array_map(static function ($questionId) use ($itemsById) {
|
||||
return $itemsById[$questionId];
|
||||
}, $submittedQuestionOrder));
|
||||
}
|
||||
}
|
||||
$this->started = $input->getMethod() === 'POST'
|
||||
&& $input->post->getString('participant_name', '') !== ''
|
||||
&& $input->post->getInt('set_id', 0) > 0;
|
||||
|
||||
Reference in New Issue
Block a user