0

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;
2
  • Did you consider using the quiz module ?
    – No Sssweat
    May 4, 2022 at 8:06
  • Yes, seems pretty buggy at the moment, and also overkill for what we want.
    – Cameron
    May 5, 2022 at 8:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.