Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception;
   6  use PhpOffice\PhpSpreadsheet\Style\Conditional;
   7  
   8  /**
   9   * @method DateValue yesterday()
  10   * @method DateValue today()
  11   * @method DateValue tomorrow()
  12   * @method DateValue lastSevenDays()
  13   * @method DateValue lastWeek()
  14   * @method DateValue thisWeek()
  15   * @method DateValue nextWeek()
  16   * @method DateValue lastMonth()
  17   * @method DateValue thisMonth()
  18   * @method DateValue nextMonth()
  19   */
  20  class DateValue extends WizardAbstract implements WizardInterface
  21  {
  22      protected const MAGIC_OPERATIONS = [
  23          'yesterday' => Conditional::TIMEPERIOD_YESTERDAY,
  24          'today' => Conditional::TIMEPERIOD_TODAY,
  25          'tomorrow' => Conditional::TIMEPERIOD_TOMORROW,
  26          'lastSevenDays' => Conditional::TIMEPERIOD_LAST_7_DAYS,
  27          'last7Days' => Conditional::TIMEPERIOD_LAST_7_DAYS,
  28          'lastWeek' => Conditional::TIMEPERIOD_LAST_WEEK,
  29          'thisWeek' => Conditional::TIMEPERIOD_THIS_WEEK,
  30          'nextWeek' => Conditional::TIMEPERIOD_NEXT_WEEK,
  31          'lastMonth' => Conditional::TIMEPERIOD_LAST_MONTH,
  32          'thisMonth' => Conditional::TIMEPERIOD_THIS_MONTH,
  33          'nextMonth' => Conditional::TIMEPERIOD_NEXT_MONTH,
  34      ];
  35  
  36      protected const EXPRESSIONS = [
  37          Conditional::TIMEPERIOD_YESTERDAY => 'FLOOR(%s,1)=TODAY()-1',
  38          Conditional::TIMEPERIOD_TODAY => 'FLOOR(%s,1)=TODAY()',
  39          Conditional::TIMEPERIOD_TOMORROW => 'FLOOR(%s,1)=TODAY()+1',
  40          Conditional::TIMEPERIOD_LAST_7_DAYS => 'AND(TODAY()-FLOOR(%s,1)<=6,FLOOR(%s,1)<=TODAY())',
  41          Conditional::TIMEPERIOD_LAST_WEEK => 'AND(TODAY()-ROUNDDOWN(%s,0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(%s,0)<(WEEKDAY(TODAY())+7))',
  42          Conditional::TIMEPERIOD_THIS_WEEK => 'AND(TODAY()-ROUNDDOWN(%s,0)<=WEEKDAY(TODAY())-1,ROUNDDOWN(%s,0)-TODAY()<=7-WEEKDAY(TODAY()))',
  43          Conditional::TIMEPERIOD_NEXT_WEEK => 'AND(ROUNDDOWN(%s,0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN(%s,0)-TODAY()<(15-WEEKDAY(TODAY())))',
  44          Conditional::TIMEPERIOD_LAST_MONTH => 'AND(MONTH(%s)=MONTH(EDATE(TODAY(),0-1)),YEAR(%s)=YEAR(EDATE(TODAY(),0-1)))',
  45          Conditional::TIMEPERIOD_THIS_MONTH => 'AND(MONTH(%s)=MONTH(TODAY()),YEAR(%s)=YEAR(TODAY()))',
  46          Conditional::TIMEPERIOD_NEXT_MONTH => 'AND(MONTH(%s)=MONTH(EDATE(TODAY(),0+1)),YEAR(%s)=YEAR(EDATE(TODAY(),0+1)))',
  47      ];
  48  
  49      /** @var string */
  50      protected $operator;
  51  
  52      public function __construct(string $cellRange)
  53      {
  54          parent::__construct($cellRange);
  55      }
  56  
  57      protected function operator(string $operator): void
  58      {
  59          $this->operator = $operator;
  60      }
  61  
  62      protected function setExpression(): void
  63      {
  64          $referenceCount = substr_count(self::EXPRESSIONS[$this->operator], '%s');
  65          $references = array_fill(0, $referenceCount, $this->referenceCell);
  66          $this->expression = sprintf(self::EXPRESSIONS[$this->operator], ...$references);
  67      }
  68  
  69      public function getConditional(): Conditional
  70      {
  71          $this->setExpression();
  72  
  73          $conditional = new Conditional();
  74          $conditional->setConditionType(Conditional::CONDITION_TIMEPERIOD);
  75          $conditional->setText($this->operator);
  76          $conditional->setConditions([$this->expression]);
  77          $conditional->setStyle($this->getStyle());
  78          $conditional->setStopIfTrue($this->getStopIfTrue());
  79  
  80          return $conditional;
  81      }
  82  
  83      public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
  84      {
  85          if ($conditional->getConditionType() !== Conditional::CONDITION_TIMEPERIOD) {
  86              throw new Exception('Conditional is not a Date Value CF Rule conditional');
  87          }
  88  
  89          $wizard = new self($cellRange);
  90          $wizard->style = $conditional->getStyle();
  91          $wizard->stopIfTrue = $conditional->getStopIfTrue();
  92          $wizard->operator = $conditional->getText();
  93  
  94          return $wizard;
  95      }
  96  
  97      /**
  98       * @param string $methodName
  99       * @param mixed[] $arguments
 100       */
 101      public function __call($methodName, $arguments): self
 102      {
 103          if (!isset(self::MAGIC_OPERATIONS[$methodName])) {
 104              throw new Exception('Invalid Operation for Date Value CF Rule Wizard');
 105          }
 106  
 107          $this->operator(self::MAGIC_OPERATIONS[$methodName]);
 108  
 109          return $this;
 110      }
 111  }