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  /**
  18   * Customfield catecory controller class
  19   *
  20   * @package   core_customfield
  21   * @copyright 2018 Toni Barbera <toni@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_customfield;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Class category
  31   *
  32   * @package core_customfield
  33   * @copyright 2018 Toni Barbera <toni@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class category_controller {
  37  
  38      /**
  39       * Category persistent
  40       *
  41       * @var category
  42       */
  43      protected $category;
  44  
  45      /**
  46       * @var field_controller[]
  47       */
  48      protected $fields = [];
  49  
  50      /** @var handler */
  51      protected $handler;
  52  
  53      /**
  54       * category constructor.
  55       *
  56       * This class is not abstract, however the constructor was made protected to be consistent with
  57       * field_controller and data_controller
  58       *
  59       * @param int $id
  60       * @param \stdClass|null $record
  61       */
  62      protected function __construct(int $id = 0, \stdClass $record = null) {
  63          $this->category = new category($id, $record);
  64      }
  65  
  66      /**
  67       * Creates an instance of category_controller
  68       *
  69       * Either $id or $record or $handler need to be specified
  70       * If handler is known pass it to constructor to avoid retrieving it later
  71       * Component, area and itemid must not conflict with the ones in handler
  72       *
  73       * @param int $id
  74       * @param \stdClass|null $record
  75       * @param handler|null $handler
  76       * @return category_controller
  77       * @throws \moodle_exception
  78       * @throws \coding_exception
  79       */
  80      public static function create(int $id, \stdClass $record = null, handler $handler = null) : category_controller {
  81          global $DB;
  82          if ($id && $record) {
  83              // This warning really should be in persistent as well.
  84              debugging('Too many parameters, either id need to be specified or a record, but not both.',
  85                  DEBUG_DEVELOPER);
  86          }
  87          if ($id) {
  88              if (!$record = $DB->get_record(category::TABLE, array('id' => $id), '*', IGNORE_MISSING)) {
  89                  throw new \moodle_exception('categorynotfound', 'core_customfield');
  90              }
  91          }
  92          if (empty($record->component)) {
  93              if (!$handler) {
  94                  throw new \coding_exception('Not enough parameters to initialise category_controller - unknown component');
  95              }
  96              $record->component = $handler->get_component();
  97          }
  98          if (empty($record->area)) {
  99              if (!$handler) {
 100                  throw new \coding_exception('Not enough parameters to initialise category_controller - unknown area');
 101              }
 102              $record->area = $handler->get_area();
 103          }
 104          if (!isset($record->itemid)) {
 105              if (!$handler) {
 106                  throw new \coding_exception('Not enough parameters to initialise category_controller - unknown itemid');
 107              }
 108              $record->itemid = $handler->get_itemid();
 109          }
 110          $category = new self(0, $record);
 111          if (!$category->get('contextid')) {
 112              // If contextid was not present in the record we can find it out from the handler.
 113              $handlernew = $handler ?? $category->get_handler();
 114              $category->set('contextid', $handlernew->get_configuration_context()->id);
 115          }
 116          if ($handler) {
 117              $category->set_handler($handler);
 118          }
 119          return $category;
 120      }
 121  
 122      /**
 123       * Persistent getter parser.
 124       *
 125       * @param string $property
 126       * @return mixed
 127       */
 128      final public function get($property) {
 129          return $this->category->get($property);
 130      }
 131  
 132      /**
 133       * Persistent setter parser.
 134       *
 135       * @param string $property
 136       * @param mixed $value
 137       */
 138      final public function set($property, $value) {
 139          return $this->category->set($property, $value);
 140      }
 141  
 142      /**
 143       * Persistent delete parser.
 144       *
 145       * @return bool
 146       */
 147      final public function delete() {
 148          return $this->category->delete();
 149      }
 150  
 151      /**
 152       * Persistent save parser.
 153       *
 154       * @return void
 155       */
 156      final public function save() {
 157          $this->category->save();
 158      }
 159  
 160      /**
 161       * Return an array of field objects associated with this category.
 162       *
 163       * @return field_controller[]
 164       */
 165      public function get_fields() {
 166          return $this->fields;
 167      }
 168  
 169      /**
 170       * Adds a child field
 171       *
 172       * @param field_controller $field
 173       */
 174      public function add_field(field_controller $field) {
 175          $this->fields[$field->get('id')] = $field;
 176      }
 177  
 178      /**
 179       * Gets a handler, if not known retrieve it
 180       *
 181       * @return handler
 182       */
 183      public function get_handler() : handler {
 184          if ($this->handler === null) {
 185              $this->handler = handler::get_handler($this->get('component'), $this->get('area'), $this->get('itemid'));
 186          }
 187          return $this->handler;
 188      }
 189  
 190      /**
 191       * Allows to set handler so we don't need to retrieve it later
 192       *
 193       * @param handler $handler
 194       * @throws \coding_exception
 195       */
 196      public function set_handler(handler $handler) {
 197          // Make sure there are no conflicts.
 198          if ($this->get('component') !== $handler->get_component()) {
 199              throw new \coding_exception('Component of the handler does not match the one from the record');
 200          }
 201          if ($this->get('area') !== $handler->get_area()) {
 202              throw new \coding_exception('Area of the handler does not match the one from the record');
 203          }
 204          if ($this->get('itemid') != $handler->get_itemid()) {
 205              throw new \coding_exception('Itemid of the handler does not match the one from the record');
 206          }
 207          if ($this->get('contextid') != $handler->get_configuration_context()->id) {
 208              throw new \coding_exception('Context of the handler does not match the one from the record');
 209          }
 210          $this->handler = $handler;
 211      }
 212  
 213      /**
 214       * Persistent to_record parser.
 215       *
 216       * @return \stdClass
 217       */
 218      final public function to_record() {
 219          return $this->category->to_record();
 220      }
 221  
 222      /**
 223       * Returns the category name formatted according to configuration context.
 224       *
 225       * @return string
 226       */
 227      public function get_formatted_name() : string {
 228          $context = $this->get_handler()->get_configuration_context();
 229          return format_string($this->get('name'), true, ['context' => $context]);
 230      }
 231  }