Problem/Motivation

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.

Proposed resolution

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'));
    }

...

Issue fork lms-3540959

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

lv46gl created an issue. See original summary.

liampower made their first commit to this issue’s fork.

liampower’s picture

Version: 1.1.0-beta1 » 1.2.x-dev
Status: Active » Needs review

Was facing the same issue. Have submitted a MR

graber’s picture

Status: Needs review » Needs work

Not happy with the way this is tested as it complicates the current tests that are already complex enough. Let's try some simple Unit test instead.

graber’s picture

Status: Needs work » Needs review

Converted.. I know it's not perfect but functionalJS tests could already use a bit cleanup. Will merge if all green. Thanks for working on this!

  • graber committed 9e3ae43b on 1.2.x authored by liampower
    #3540959 Make sure only the previously selected answers are selected...
graber’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.