Score:0

Webform Conditions for a multi valued select

cn flag

I'm making a quiz utilising Webform. I would like to use a multi-valued select list for one of the questions, and want to grade the user on any combination of correct answers selected.

Using the Webform Image Select element's default Cute Kittens as an example, for the values of the select:

  1. kitten_1
  2. kitten_2
  3. kitten_3
  4. kitten_4

I'd like to be able to say a particular combination of selections is the right answer. Say kitten_1 and kitten_3, when they are selected it should show the element which says "Correct answer!".

From what I can see of conditions, they can't be applied to multiple values. I could create a 'pattern' option, but that would only tell me if either kitten_1 or kitten_3 or both is selected, not iff they are both selected.

If this isn't possible without custom code, can it be added via a plugin etc, I can't figure that out either.

I wanted to demo this to someone, so I modified the module to add it, diff below, but would very much like a better way.

diff -r /tmp/webform/js/webform.states.js web/modules/contrib/webform/js/webform.states.js
115,119d114
<     else if ('multi_value' in reference) {
<      const multiValue = reference['multi_value'].split(',');
<      const equal = (multiValue.length === value.length) && (multiValue.sort().every((v, i) => v === value[i]));
<      return equal;
<     }
diff -r /tmp/webform/src/Element/WebformElementStates.php web/modules/contrib/webform/src/Element/WebformElementStates.php
485,486d484
<           [$trigger_selector => ['value' => 'multi_value']],
<           'or',
945c943
<       elseif (in_array($trigger, ['multi_value', 'pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) {
---
>       elseif (in_array($trigger, ['pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) {
1105d1102
<       'multi_value' => t('Multi values'),
diff -r /tmp/webform/src/WebformEntityConditionsManager.php web/modules/contrib/webform/src/WebformEntityConditionsManager.php
75d74
<       'multi_value' => $this->t('has values'),
233c232
<       if (in_array($trigger_substate, ['multi_value', 'pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) {
---
>       if (in_array($trigger_substate, ['pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) {
diff -r /tmp/webform/src/WebformSubmissionConditionsValidator.php web/modules/contrib/webform/src/WebformSubmissionConditionsValidator.php
764c764
<       if (in_array($trigger_substate, ['multi_value', 'pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) {
---
>       if (in_array($trigger_substate, ['pattern', '!pattern', 'less', 'less_equal', 'greater', 'greater_equal', 'between', '!between'])) {
775,778c775
<     if ($element_plugin->hasMultipleValues($element) && $trigger === 'multi_value') {
<       $result = $this->checkConditionTrigger($trigger, $trigger_value, (array) $element_value);
<     }
<     elseif ($element_plugin->hasMultipleValues($element) && $trigger !== 'empty') {
---
>     if ($element_plugin->hasMultipleValues($element) && $trigger !== 'empty') {
832,837d828
< 
<       case 'multi_value':
<         sort($element_value);
<         $multi_values = explode(',', $trigger_value);
<         sort($multi_values);   
<         return $multi_values === $element_value;
No Sssweat avatar
ua flag
Did you consider using the [quiz module](https://www.drupal.org/project/quiz) ?
Cameron avatar
cn flag
Yes, seems pretty buggy at the moment, and also overkill for what we want.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.