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  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\local\models;
  20  
  21  use context;
  22  use core_reportbuilder\event\schedule_created;
  23  use core_reportbuilder\event\schedule_deleted;
  24  use core_reportbuilder\event\schedule_updated;
  25  use lang_string;
  26  use core\persistent;
  27  
  28  /**
  29   * Persistent class to represent a report schedule
  30   *
  31   * @package     core_reportbuilder
  32   * @copyright   2021 Paul Holden <paulh@moodle.com>
  33   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class schedule extends persistent {
  36  
  37      /** @var string Table name */
  38      public const TABLE = 'reportbuilder_schedule';
  39  
  40      /** @var int Send report schedule as viewed by recipient */
  41      public const REPORT_VIEWAS_RECIPIENT = -1;
  42  
  43      /** @var int Send report schedule as viewed by creator */
  44      public const REPORT_VIEWAS_CREATOR = 0;
  45  
  46      /** @var int Send report schedule as viewed by specific user */
  47      public const REPORT_VIEWAS_USER = 1;
  48  
  49      /** @var int No recurrence */
  50      public const RECURRENCE_NONE = 0;
  51  
  52      /** @var int Daily recurrence */
  53      public const RECURRENCE_DAILY = 1;
  54  
  55      /** @var int Daily recurrence for week days only */
  56      public const RECURRENCE_WEEKDAYS = 2;
  57  
  58      /** @var int Weekly recurrence */
  59      public const RECURRENCE_WEEKLY = 3;
  60  
  61      /** @var int Monthly recurrence */
  62      public const RECURRENCE_MONTHLY = 4;
  63  
  64      /** @var int Annual recurrence */
  65      public const RECURRENCE_ANNUALLY = 5;
  66  
  67      /** @var int Send schedule with empty report */
  68      public const REPORT_EMPTY_SEND_EMPTY = 0;
  69  
  70      /** @var int Send schedule without report */
  71      public const REPORT_EMPTY_SEND_WITHOUT = 1;
  72  
  73      /** @var int Don't send schedule if report is empty */
  74      public const REPORT_EMPTY_DONT_SEND = 2;
  75  
  76      /**
  77       * Return the definition of the properties of this model.
  78       *
  79       * @return array
  80       */
  81      protected static function define_properties() : array {
  82          return [
  83              'reportid' => [
  84                  'type' => PARAM_INT,
  85              ],
  86              'name' => [
  87                  'type' => PARAM_TEXT,
  88              ],
  89              'enabled' => [
  90                  'type' => PARAM_BOOL,
  91                  'default' => true,
  92              ],
  93              'audiences' => [
  94                  'type' => PARAM_RAW,
  95                  'default' => '[]',
  96              ],
  97              'format' => [
  98                  'type' => PARAM_PLUGIN,
  99              ],
 100              'subject' => [
 101                  'type' => PARAM_TEXT,
 102              ],
 103              'message' => [
 104                  'type' => PARAM_CLEANHTML,
 105              ],
 106              'messageformat' => [
 107                  'type' => PARAM_INT,
 108                  'default' => FORMAT_HTML,
 109                  'choices' => [
 110                      FORMAT_MOODLE,
 111                      FORMAT_HTML,
 112                      FORMAT_PLAIN,
 113                      FORMAT_MARKDOWN,
 114                  ],
 115              ],
 116              'userviewas' => [
 117                  'type' => PARAM_INT,
 118                  'default' => self::REPORT_VIEWAS_CREATOR,
 119              ],
 120              'timescheduled' => [
 121                  'type' => PARAM_INT,
 122              ],
 123              'recurrence' => [
 124                  'type' => PARAM_INT,
 125                  'default' => self::RECURRENCE_NONE,
 126                  'choices' => [
 127                      self::RECURRENCE_NONE,
 128                      self::RECURRENCE_DAILY,
 129                      self::RECURRENCE_WEEKDAYS,
 130                      self::RECURRENCE_WEEKLY,
 131                      self::RECURRENCE_MONTHLY,
 132                      self::RECURRENCE_ANNUALLY,
 133                  ],
 134              ],
 135              'reportempty' => [
 136                  'type' => PARAM_INT,
 137                  'default' => self::REPORT_EMPTY_SEND_EMPTY,
 138                  'choices' => [
 139                      self::REPORT_EMPTY_SEND_EMPTY,
 140                      self::REPORT_EMPTY_SEND_WITHOUT,
 141                      self::REPORT_EMPTY_DONT_SEND,
 142                  ],
 143              ],
 144              'timelastsent' => [
 145                  'type' => PARAM_INT,
 146                  'default' => 0,
 147              ],
 148              'timenextsend' => [
 149                  'type' => PARAM_INT,
 150                  'default' => 0,
 151              ],
 152              'usercreated' => [
 153                  'type' => PARAM_INT,
 154                  'default' => static function(): int {
 155                      global $USER;
 156                      return (int) $USER->id;
 157                  },
 158              ],
 159          ];
 160      }
 161  
 162      /**
 163       * Validate reportid property
 164       *
 165       * @param int $reportid
 166       * @return bool|lang_string
 167       */
 168      protected function validate_reportid(int $reportid) {
 169          if (!report::record_exists($reportid)) {
 170              return new lang_string('invaliddata', 'error');
 171          }
 172  
 173          return true;
 174      }
 175  
 176      /**
 177       * Return the report this schedule belongs to
 178       *
 179       * @return report
 180       */
 181      public function get_report(): report {
 182          return new report($this->get('reportid'));
 183      }
 184  
 185      /**
 186       * Return formatted schedule name
 187       *
 188       * @param context|null $context If the context of the report is already known, it should be passed here
 189       * @return string
 190       */
 191      public function get_formatted_name(?context $context = null): string {
 192          if ($context === null) {
 193              $context = $this->get_report()->get_context();
 194          }
 195  
 196          return format_string($this->raw_get('name'), true, ['context' => $context]);
 197      }
 198  
 199      /**
 200       * Hook to execute after creation
 201       */
 202      protected function after_create(): void {
 203          schedule_created::create_from_object($this)->trigger();
 204      }
 205  
 206      /**
 207       * Hook to execute after update
 208       *
 209       * @param bool $result
 210       */
 211      protected function after_update($result): void {
 212          if ($result) {
 213              schedule_updated::create_from_object($this)->trigger();
 214          }
 215      }
 216  
 217      /**
 218       * Hook to execute after deletion
 219       *
 220       * @param bool $result
 221       */
 222      protected function after_delete($result): void {
 223          if ($result) {
 224              schedule_deleted::create_from_object($this)->trigger();
 225          }
 226      }
 227  }