Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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;
  20  
  21  use context;
  22  use core_reportbuilder\local\models\report;
  23  use core_reportbuilder\local\report\base;
  24  
  25  /**
  26   * Factory class for creating system report instances
  27   *
  28   * @package     core_reportbuilder
  29   * @copyright   2020 Paul Holden <paulh@moodle.com>
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class system_report_factory {
  33  
  34      /**
  35       * Create and return instance of given system report source
  36       *
  37       * @param string $source Class path of system report definition
  38       * @param context $context
  39       * @param string $component
  40       * @param string $area
  41       * @param int $itemid
  42       * @param array $parameters Simple key/value pairs, accessed inside reports using $this->get_parameter()
  43       * @return system_report
  44       * @throws source_invalid_exception
  45       */
  46      public static function create(string $source, context $context, string $component = '', string $area = '',
  47              int $itemid = 0, array $parameters = []): system_report {
  48  
  49          // Exit early if source isn't a system report.
  50          if (!manager::report_source_exists($source, system_report::class)) {
  51              throw new source_invalid_exception($source);
  52          }
  53  
  54          $report = static::get_report_persistent($source, $context, $component, $area, $itemid);
  55  
  56          return manager::get_report_from_persistent($report, $parameters);
  57      }
  58  
  59      /**
  60       * Given a report source, with accompanying context information, return a persistent report instance
  61       *
  62       * @param string $source
  63       * @param context $context
  64       * @param string $component
  65       * @param string $area
  66       * @param int $itemid
  67       * @return report
  68       */
  69      private static function get_report_persistent(string $source, context $context, string $component = '', string $area = '',
  70              int $itemid = 0): report {
  71  
  72          $reportdata = [
  73              'type' => base::TYPE_SYSTEM_REPORT,
  74              'source' => $source,
  75              'contextid' => $context->id,
  76              'component' => $component,
  77              'area' => $area,
  78              'itemid' => $itemid,
  79          ];
  80  
  81          if ($report = report::get_record($reportdata)) {
  82               return $report;
  83          }
  84  
  85          return manager::create_report_persistent((object) $reportdata);
  86      }
  87  }