Commit 950eb515 authored by Hemant Gupta's avatar Hemant Gupta Committed by Nigel Cunningham
Browse files

Issue #3083916 by guptahemant: Max score from a set plugin should support multivalued options

parent 407717c1
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\webform_score\Plugin\WebformScore;

use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TraversableTypedDataInterface;

/**
 * Score is based on the maximum value from a set of scores.
@@ -31,12 +32,21 @@ class Maximum extends WebformScoreAggregateBase {
  /**
   * {@inheritdoc}
   */
  public function score(TypedDataInterface $answer) {
  public function score(TypedDataInterface $answers) {
    $scores = [];
    $plugins = $this->createPlugins();

    foreach ($this->createPlugins() as $plugin) {
    if (!($answers instanceof TraversableTypedDataInterface)) {
      // If the answers are not in traversable format, convert them in an array
      // so that further logic can work correctly.
      $answers = [$answers];
    }

    foreach ($answers as $answer) {
      foreach ($plugins as $plugin) {
        $scores[] = $plugin->score($answer);
      }
    }

    return max($scores);
  }