Skip to content
Snippets Groups Projects
Commit 1fd728ac authored by Leon's avatar Leon
Browse files

Fix Radio Buttons in Single choice questions; Shuffle Answers

parent d4d81814
No related branches found
No related tags found
No related merge requests found
......@@ -171,6 +171,9 @@ function constructQuestion(data) {
input.setAttribute('type', isScq ? 'radio' : 'checkbox');
input.setAttribute('id', `answer_${i}`);
input.setAttribute('answerId', `${data['answers'][i]['id']}`);
if (isScq) {
input.setAttribute('name', 'scq-answer');
}
label.setAttribute('for', `answer_${i}`);
label.innerText = data['answers'][i]['label'];
......
......@@ -36,33 +36,37 @@ class QuestionService
$target = $questions[array_rand($questions)];
if ($target->isSingleChoice() || $target->isMultipleChoiceRandomWrongs()) {
/** @var Answer[] $additionalAnswers */
$additionalAnswers = $this->answerRepository
->findAllByTopicAndContentType($topic, $target->getContentType(), $target->getId())
;
if (count($additionalAnswers) === 0) {
throw new NotFoundHttpException('Additional answers not found');
}
if ($target->getAnswers()->count() < 5) {
/** @var Answer[] $additionalAnswers */
$additionalAnswers = $this->answerRepository
->findAllByTopicAndContentType($topic, $target->getContentType(), $target->getId());
if (count($additionalAnswers) === 0) {
throw new NotFoundHttpException('Additional answers not found');
}
for ($i = 0; $i < (6 - $target->getAnswers()->count()); $i++) {
/** @var int $rnd */
$rnd = array_rand($additionalAnswers);
$target->addAnswer($additionalAnswers[$rnd]);
unset($additionalAnswers[$rnd]);
for ($i = 0; $i < (6 - $target->getAnswers()->count()); $i++) {
/** @var int $rnd */
$rnd = array_rand($additionalAnswers);
$target->addAnswer($additionalAnswers[$rnd]);
unset($additionalAnswers[$rnd]);
}
}
}
$answers = array_map(function ($answer) {
/** @var Answer $answer */
return [
'id' => $answer->getId(),
'label' => $answer->getLabel()
];
}, $target->getAnswers()->getValues());
shuffle($answers);
return [
'id' => $target->getId(),
'type' => $target->getStructureType(),
'label' => $target->getLabel(),
'answers' => array_map(function ($answer) {
/** @var Answer $answer */
return [
'id' => $answer->getId(),
'label' => $answer->getLabel()
];
}, $target->getAnswers()->getValues())
'answers' => $answers
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment