Commit 3d86c8dc authored by Hemant Gupta's avatar Hemant Gupta Committed by Nigel Cunningham
Browse files

Issue #3083062 by guptahemant: Sum score from a set plugin should support addition of options

parent 6c0e4ceb
Loading
Loading
Loading
Loading
+14 −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 sub of scores from a set of answers.
@@ -31,12 +32,22 @@ class Sum extends WebformScoreAggregateBase {
  /**
   * {@inheritdoc}
   */
  public function score(TypedDataInterface $answer) {
  public function score(TypedDataInterface $answers) {
    $score = 0;
    $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.
      $temp[] = $answers;
      $answers = $temp;
    }

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

    return $score;
  }