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  // 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  use core_reportbuilder\local\helpers\report as helper;
  20  use core_reportbuilder\local\helpers\schedule as schedule_helper;
  21  use core_reportbuilder\local\models\column;
  22  use core_reportbuilder\local\models\filter;
  23  use core_reportbuilder\local\models\report;
  24  use core_reportbuilder\local\models\schedule;
  25  use core_reportbuilder\local\audiences\base as audience_base;
  26  
  27  /**
  28   * Report builder test generator
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_reportbuilder_generator extends component_generator_base {
  35  
  36      /**
  37       * Create report
  38       *
  39       * @param array|stdClass $record
  40       * @return report
  41       * @throws coding_exception
  42       */
  43      public function create_report($record): report {
  44          $record = (array) $record;
  45  
  46          if (!array_key_exists('name', $record)) {
  47              throw new coding_exception('Record must contain \'name\' property');
  48          }
  49          if (!array_key_exists('source', $record)) {
  50              throw new coding_exception('Record must contain \'source\' property');
  51          }
  52  
  53          // Include default setup unless specifically disabled in passed record.
  54          $default = (bool) ($record['default'] ?? true);
  55  
  56          return helper::create_report((object) $record, $default);
  57      }
  58  
  59      /**
  60       * Create report column
  61       *
  62       * @param array|stdClass $record
  63       * @return column
  64       * @throws coding_exception
  65       */
  66      public function create_column($record): column {
  67          $record = (array) $record;
  68  
  69          if (!array_key_exists('reportid', $record)) {
  70              throw new coding_exception('Record must contain \'reportid\' property');
  71          }
  72          if (!array_key_exists('uniqueidentifier', $record)) {
  73              throw new coding_exception('Record must contain \'uniqueidentifier\' property');
  74          }
  75  
  76          $column = helper::add_report_column($record['reportid'], $record['uniqueidentifier']);
  77  
  78          // Update additional record properties.
  79          unset($record['reportid'], $record['uniqueidentifier']);
  80          if ($properties = array_intersect_key($record, column::properties_definition())) {
  81              $column->set_many($properties)->update();
  82          }
  83  
  84          return $column;
  85      }
  86  
  87      /**
  88       * Create report filter
  89       *
  90       * @param array|stdClass $record
  91       * @return filter
  92       * @throws coding_exception
  93       */
  94      public function create_filter($record): filter {
  95          $record = (array) $record;
  96  
  97          if (!array_key_exists('reportid', $record)) {
  98              throw new coding_exception('Record must contain \'reportid\' property');
  99          }
 100          if (!array_key_exists('uniqueidentifier', $record)) {
 101              throw new coding_exception('Record must contain \'uniqueidentifier\' property');
 102          }
 103  
 104          $filter = helper::add_report_filter($record['reportid'], $record['uniqueidentifier']);
 105  
 106          // Update additional record properties.
 107          unset($record['reportid'], $record['uniqueidentifier']);
 108          if ($properties = array_intersect_key($record, filter::properties_definition())) {
 109              $filter->set_many($properties)->update();
 110          }
 111  
 112          return $filter;
 113      }
 114  
 115      /**
 116       * Create report condition
 117       *
 118       * @param array|stdClass $record
 119       * @return filter
 120       * @throws coding_exception
 121       */
 122      public function create_condition($record): filter {
 123          $record = (array) $record;
 124  
 125          if (!array_key_exists('reportid', $record)) {
 126              throw new coding_exception('Record must contain \'reportid\' property');
 127          }
 128          if (!array_key_exists('uniqueidentifier', $record)) {
 129              throw new coding_exception('Record must contain \'uniqueidentifier\' property');
 130          }
 131  
 132          $condition = helper::add_report_condition($record['reportid'], $record['uniqueidentifier']);
 133  
 134          // Update additional record properties.
 135          unset($record['reportid'], $record['uniqueidentifier']);
 136          if ($properties = array_intersect_key($record, filter::properties_definition())) {
 137              $condition->set_many($properties)->update();
 138          }
 139  
 140          return $condition;
 141      }
 142  
 143      /**
 144       * Create report audience
 145       *
 146       * @param array|stdClass $record
 147       * @return audience_base
 148       * @throws coding_exception
 149       */
 150      public function create_audience($record): audience_base {
 151          $record = (array) $record;
 152  
 153          // Required properties.
 154          if (!array_key_exists('reportid', $record)) {
 155              throw new coding_exception('Record must contain \'reportid\' property');
 156          }
 157          if (!array_key_exists('configdata', $record)) {
 158              throw new coding_exception('Record must contain \'configdata\' property');
 159          }
 160  
 161          // Default to all users if not specified, for convenience.
 162          /** @var audience_base $classname */
 163          $classname = $record['classname'] ??
 164              \core_reportbuilder\reportbuilder\audience\allusers::class;
 165  
 166          return ($classname)::create($record['reportid'], $record['configdata']);
 167      }
 168  
 169      /**
 170       * Create report schedule
 171       *
 172       * @param array|stdClass $record
 173       * @return schedule
 174       * @throws coding_exception
 175       */
 176      public function create_schedule($record): schedule {
 177          $record = (array) $record;
 178  
 179          // Required properties.
 180          if (!array_key_exists('reportid', $record)) {
 181              throw new coding_exception('Record must contain \'reportid\' property');
 182          }
 183          if (!array_key_exists('name', $record)) {
 184              throw new coding_exception('Record must contain \'name\' property');
 185          }
 186  
 187          // Optional properties.
 188          if (!array_key_exists('format', $record)) {
 189              $record['format'] = 'csv';
 190          }
 191          if (!array_key_exists('subject', $record)) {
 192              $record['subject'] = $record['name'] . ' subject';
 193          }
 194          if (!array_key_exists('message', $record)) {
 195              $record['message'] = $record['name'] . ' message';
 196          }
 197          if (!array_key_exists('timescheduled', $record)) {
 198              $record['timescheduled'] = usergetmidnight(time() + DAYSECS);
 199          }
 200  
 201          // Time to use as comparison against current date (null means current time).
 202          $timenow = $record['timenow'] ?? null;
 203  
 204          return schedule_helper::create_schedule((object) $record, $timenow);
 205      }
 206  }