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.

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