Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  
   7  class Sum
   8  {
   9      /**
  10       * SUM, ignoring non-numeric non-error strings. This is eventually used by SUMIF.
  11       *
  12       * SUM computes the sum of all the values and cells referenced in the argument list.
  13       *
  14       * Excel Function:
  15       *        SUM(value1[,value2[, ...]])
  16       *
  17       * @param mixed ...$args Data values
  18       *
  19       * @return float|string
  20       */
  21      public static function sumIgnoringStrings(...$args)
  22      {
  23          $returnValue = 0;
  24  
  25          // Loop through the arguments
  26          foreach (Functions::flattenArray($args) as $arg) {
  27              // Is it a numeric value?
  28              if (is_numeric($arg)) {
  29                  $returnValue += $arg;
  30              } elseif (Functions::isError($arg)) {
  31                  return $arg;
  32              }
  33          }
  34  
  35          return $returnValue;
  36      }
  37  
  38      /**
  39       * SUM, returning error for non-numeric strings. This is used by Excel SUM function.
  40       *
  41       * SUM computes the sum of all the values and cells referenced in the argument list.
  42       *
  43       * Excel Function:
  44       *        SUM(value1[,value2[, ...]])
  45       *
  46       * @param mixed ...$args Data values
  47       *
  48       * @return float|string
  49       */
  50      public static function sumErroringStrings(...$args)
  51      {
  52          $returnValue = 0;
  53          // Loop through the arguments
  54          $aArgs = Functions::flattenArrayIndexed($args);
  55          foreach ($aArgs as $k => $arg) {
  56              // Is it a numeric value?
  57              if (is_numeric($arg) || empty($arg)) {
  58                  if (is_string($arg)) {
  59                      $arg = (int) $arg;
  60                  }
  61                  $returnValue += $arg;
  62              } elseif (is_bool($arg)) {
  63                  $returnValue += (int) $arg;
  64              } elseif (Functions::isError($arg)) {
  65                  return $arg;
  66              // ignore non-numerics from cell, but fail as literals (except null)
  67              } elseif ($arg !== null && !Functions::isCellValue($k)) {
  68                  return Functions::VALUE();
  69              }
  70          }
  71  
  72          return $returnValue;
  73      }
  74  
  75      /**
  76       * SUMPRODUCT.
  77       *
  78       * Excel Function:
  79       *        SUMPRODUCT(value1[,value2[, ...]])
  80       *
  81       * @param mixed ...$args Data values
  82       *
  83       * @return float|string The result, or a string containing an error
  84       */
  85      public static function product(...$args)
  86      {
  87          $arrayList = $args;
  88  
  89          $wrkArray = Functions::flattenArray(array_shift($arrayList));
  90          $wrkCellCount = count($wrkArray);
  91  
  92          for ($i = 0; $i < $wrkCellCount; ++$i) {
  93              if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
  94                  $wrkArray[$i] = 0;
  95              }
  96          }
  97  
  98          foreach ($arrayList as $matrixData) {
  99              $array2 = Functions::flattenArray($matrixData);
 100              $count = count($array2);
 101              if ($wrkCellCount != $count) {
 102                  return Functions::VALUE();
 103              }
 104  
 105              foreach ($array2 as $i => $val) {
 106                  if ((!is_numeric($val)) || (is_string($val))) {
 107                      $val = 0;
 108                  }
 109                  $wrkArray[$i] *= $val;
 110              }
 111          }
 112  
 113          return array_sum($wrkArray);
 114      }
 115  }