- Issue created by @lv46gl
Thank you for your work on developing the module!
In the lms_answer_plugins submodule, using the "Multiple answer select", "Multiple answer select with feedback", "Single answer select with feedback", and "Single answer select" types, I noticed that when I check any of the checkboxes, the first option is always selected.
The error is caused by the current use of checkboxes. The description of Checkboxes.php states:
/**
* Provides a form element for a set of checkboxes.
*
* Properties:
* - #options: An associative array whose keys are the values returned for each
* checkbox, and whose values are the labels next to each checkbox. The
* #options array cannot have a 0 key, as it would not be possible to discern
* checked and unchecked states.
However, the following is included in the lms_answer_plugin/src/Plugin/SelectBase.php class (the $delta is a number, started 0) :
/**
* {@inheritdoc}
*/
public function answeringForm(array &$form, FormStateInterface $form_state, Answer $answer): void {
$activity = $answer->getActivity();
$data = $answer->getData();
$options = [];
foreach ($activity->get('answers') as $delta => $answer_item) {
$options[$delta] = $answer_item->get('answer')->getValue();
}
I suggest something like this:
...
/**
* {@inheritdoc}
*/
public function getScore(Answer $answer): float {
$answers = $answer->getActivity()->get('answers');
$data = $answer->getData()['answer'];
if (!\is_array($data)) {
$data = [$data];
}
$data = \array_filter($data, static fn($item) => $item !== 0);
$score = 0;
$max_score = 0;
foreach ($answers as $delta => $answer_item) {
// if (\in_array((string) $delta, $data, TRUE)) {
if (\in_array('o-' . $delta, $data, TRUE)) {
$answer = TRUE;
}
else {
$answer = FALSE;
}
...
/**
* {@inheritdoc}
*/
public function answeringForm(array &$form, FormStateInterface $form_state, Answer $answer): void {
$activity = $answer->getActivity();
$data = $answer->getData();
$options = [];
foreach ($activity->get('answers') as $delta => $answer_item) {
// $options[$delta] = $answer_item->get('answer')->getValue();
$options['o-' . $delta] = $answer_item->get('answer')->getValue();
}
...
/**
* {@inheritdoc}
*/
protected function getAnswerRenderable(Answer $answer): array {
$data = $answer->getData();
if (!\array_key_exists('answer', $data)) {
return [];
}
...
foreach ($answer->getActivity()->get('answers') as $delta => $answer_item) {
// $renderable['#items'][] = $answer_item->get('answer')->getValue() . ' - ' . (\array_key_exists($delta, $data['answer']) ? $this->t('Selected') : $this->t('Not selected'));
$renderable['#items'][] = $answer_item->get('answer')->getValue() . ' - ' . (\array_key_exists('o-' . $delta, $data['answer']) ? $this->t('Selected') : $this->t('Not selected'));
}
...
Active
1.1
Activities and answers