2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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;
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
{
46 * @var field_controller[]
48 protected $fields = [];
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
60 * @param \stdClass|null $record
62 protected function __construct(int $id = 0, \stdClass
$record = null) {
63 $this->category
= new category($id, $record);
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
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
{
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.',
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
)) {
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
)) {
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
)) {
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
);
117 $category->set_handler($handler);
123 * Persistent getter parser.
125 * @param string $property
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.
147 final public function delete() {
148 return $this->category
->delete();
152 * Persistent save parser.
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
;
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
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.
218 final public function to_record() {
219 return $this->category
->to_record();
223 * Returns the category name formatted according to configuration context.
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]);