Merge branch 'MDL-76185-311' of https://github.com/mihailges/moodle into MOODLE_311_S...
[moodle.git] / customfield / classes / category_controller.php
blob601b10616aba7d6f924ebe8cf8f1d51379ef8048
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.
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/>.
17 /**
18 * Customfield catecory controller class
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
25 namespace core_customfield;
27 defined('MOODLE_INTERNAL') || die;
29 /**
30 * Class category
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
36 class category_controller {
38 /**
39 * Category persistent
41 * @var category
43 protected $category;
45 /**
46 * @var field_controller[]
48 protected $fields = [];
50 /** @var handler */
51 protected $handler;
53 /**
54 * category constructor.
56 * This class is not abstract, however the constructor was made protected to be consistent with
57 * field_controller and data_controller
59 * @param int $id
60 * @param \stdClass|null $record
62 protected function __construct(int $id = 0, \stdClass $record = null) {
63 $this->category = new category($id, $record);
66 /**
67 * Creates an instance of category_controller
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
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
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);
87 if ($id) {
88 if (!$record = $DB->get_record(category::TABLE, array('id' => $id), '*', IGNORE_MISSING)) {
89 throw new \moodle_exception('categorynotfound', 'core_customfield');
92 if (empty($record->component)) {
93 if (!$handler) {
94 throw new \coding_exception('Not enough parameters to initialise category_controller - unknown component');
96 $record->component = $handler->get_component();
98 if (empty($record->area)) {
99 if (!$handler) {
100 throw new \coding_exception('Not enough parameters to initialise category_controller - unknown area');
102 $record->area = $handler->get_area();
104 if (!isset($record->itemid)) {
105 if (!$handler) {
106 throw new \coding_exception('Not enough parameters to initialise category_controller - unknown itemid');
108 $record->itemid = $handler->get_itemid();
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);
116 if ($handler) {
117 $category->set_handler($handler);
119 return $category;
123 * Persistent getter parser.
125 * @param string $property
126 * @return mixed
128 final public function get($property) {
129 return $this->category->get($property);
133 * Persistent setter parser.
135 * @param string $property
136 * @param mixed $value
138 final public function set($property, $value) {
139 return $this->category->set($property, $value);
143 * Persistent delete parser.
145 * @return bool
147 final public function delete() {
148 return $this->category->delete();
152 * Persistent save parser.
154 * @return void
156 final public function save() {
157 $this->category->save();
161 * Return an array of field objects associated with this category.
163 * @return field_controller[]
165 public function get_fields() {
166 return $this->fields;
170 * Adds a child field
172 * @param field_controller $field
174 public function add_field(field_controller $field) {
175 $this->fields[$field->get('id')] = $field;
179 * Gets a handler, if not known retrieve it
181 * @return handler
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'));
187 return $this->handler;
191 * Allows to set handler so we don't need to retrieve it later
193 * @param handler $handler
194 * @throws \coding_exception
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');
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');
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');
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');
210 $this->handler = $handler;
214 * Persistent to_record parser.
216 * @return \stdClass
218 final public function to_record() {
219 return $this->category->to_record();
223 * Returns the category name formatted according to configuration context.
225 * @return string
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]);