1 |
efrain |
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 |
namespace core_group\customfield;
|
|
|
18 |
|
|
|
19 |
use context;
|
|
|
20 |
use context_course;
|
|
|
21 |
use context_system;
|
|
|
22 |
use core_customfield\api;
|
|
|
23 |
use core_customfield\handler;
|
|
|
24 |
use core_customfield\field_controller;
|
|
|
25 |
use moodle_url;
|
|
|
26 |
use restore_task;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Group handler for custom fields.
|
|
|
30 |
*
|
|
|
31 |
* @package core_group
|
|
|
32 |
* @author Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
|
|
|
33 |
* @copyright 2023 Catalyst IT Pty Ltd
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class group_handler extends handler {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @var group_handler
|
|
|
40 |
*/
|
|
|
41 |
static protected $singleton;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Returns a singleton.
|
|
|
45 |
*
|
|
|
46 |
* @param int $itemid
|
|
|
47 |
* @return \core_customfield\handler
|
|
|
48 |
*/
|
|
|
49 |
public static function create(int $itemid = 0): handler {
|
|
|
50 |
if (static::$singleton === null) {
|
|
|
51 |
self::$singleton = new static(0);
|
|
|
52 |
}
|
|
|
53 |
return self::$singleton;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Run reset code after unit tests to reset the singleton usage.
|
|
|
58 |
*/
|
|
|
59 |
public static function reset_caches(): void {
|
|
|
60 |
if (!PHPUNIT_TEST) {
|
|
|
61 |
throw new \coding_exception('This feature is only intended for use in unit tests');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
static::$singleton = null;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* The current user can configure custom fields on this component.
|
|
|
69 |
*
|
|
|
70 |
* @return bool true if the current can configure custom fields, false otherwise
|
|
|
71 |
*/
|
|
|
72 |
public function can_configure(): bool {
|
|
|
73 |
return has_capability('moodle/group:configurecustomfields', $this->get_configuration_context());
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* The current user can edit custom fields on the given group.
|
|
|
78 |
*
|
|
|
79 |
* @param field_controller $field
|
|
|
80 |
* @param int $instanceid id of the group to test edit permission
|
|
|
81 |
* @return bool true if the current can edit custom field, false otherwise
|
|
|
82 |
*/
|
|
|
83 |
public function can_edit(field_controller $field, int $instanceid = 0): bool {
|
|
|
84 |
return has_capability('moodle/course:managegroups', $this->get_instance_context($instanceid));
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* The current user can view custom fields on the given group.
|
|
|
89 |
*
|
|
|
90 |
* @param field_controller $field
|
|
|
91 |
* @param int $instanceid id of the group to test edit permission
|
|
|
92 |
* @return bool true if the current can view custom field, false otherwise
|
|
|
93 |
*/
|
|
|
94 |
public function can_view(field_controller $field, int $instanceid): bool {
|
|
|
95 |
return has_any_capability(['moodle/course:managegroups', 'moodle/course:view'], $this->get_instance_context($instanceid));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Context that should be used for new categories created by this handler.
|
|
|
100 |
*
|
|
|
101 |
* @return context the context for configuration
|
|
|
102 |
*/
|
|
|
103 |
public function get_configuration_context(): context {
|
|
|
104 |
return context_system::instance();
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* URL for configuration of the fields on this handler.
|
|
|
109 |
*
|
|
|
110 |
* @return moodle_url The URL to configure custom fields for this component
|
|
|
111 |
*/
|
|
|
112 |
public function get_configuration_url(): moodle_url {
|
|
|
113 |
return new moodle_url('/group/customfield.php');
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Returns the context for the data associated with the given instanceid.
|
|
|
118 |
*
|
|
|
119 |
* @param int $instanceid id of the record to get the context for
|
|
|
120 |
* @return context the context for the given record
|
|
|
121 |
*/
|
|
|
122 |
public function get_instance_context(int $instanceid = 0): \context {
|
|
|
123 |
global $COURSE, $DB;
|
|
|
124 |
|
|
|
125 |
if ($instanceid > 0) {
|
|
|
126 |
$group = $DB->get_record('groups', ['id' => $instanceid], '*', MUST_EXIST);
|
|
|
127 |
return context_course::instance($group->courseid);
|
|
|
128 |
} else if (!empty($COURSE->id)) {
|
|
|
129 |
return context_course::instance($COURSE->id);
|
|
|
130 |
} else {
|
|
|
131 |
return context_system::instance();
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Get raw data associated with all fields current user can view or edit
|
|
|
137 |
*
|
|
|
138 |
* @param int $instanceid
|
|
|
139 |
* @return array
|
|
|
140 |
*/
|
|
|
141 |
public function get_instance_data_for_backup(int $instanceid): array {
|
|
|
142 |
$finalfields = [];
|
|
|
143 |
$instancedata = $this->get_instance_data($instanceid, true);
|
|
|
144 |
foreach ($instancedata as $data) {
|
|
|
145 |
if ($data->get('id') && $this->can_backup($data->get_field(), $instanceid)) {
|
|
|
146 |
$finalfields[] = [
|
|
|
147 |
'id' => $data->get('id'),
|
|
|
148 |
'shortname' => $data->get_field()->get('shortname'),
|
|
|
149 |
'type' => $data->get_field()->get('type'),
|
|
|
150 |
'value' => $data->get_value(),
|
|
|
151 |
'valueformat' => $data->get('valueformat'),
|
|
|
152 |
'valuetrust' => $data->get('valuetrust'),
|
|
|
153 |
'groupid' => $data->get('instanceid'),
|
|
|
154 |
];
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
return $finalfields;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Creates or updates custom field data.
|
|
|
162 |
*
|
|
|
163 |
* @param restore_task $task
|
|
|
164 |
* @param array $data
|
|
|
165 |
*
|
|
|
166 |
* @return int|void Conditionally returns the ID of the created or updated record.
|
|
|
167 |
*/
|
|
|
168 |
public function restore_instance_data_from_backup(restore_task $task, array $data) {
|
|
|
169 |
$instanceid = $data['groupid'];
|
|
|
170 |
$context = $this->get_instance_context($instanceid);
|
|
|
171 |
$editablefields = $this->get_editable_fields($instanceid);
|
|
|
172 |
$records = api::get_instance_fields_data($editablefields, $instanceid);
|
|
|
173 |
|
|
|
174 |
foreach ($records as $d) {
|
|
|
175 |
$field = $d->get_field();
|
|
|
176 |
if ($field->get('shortname') === $data['shortname'] && $field->get('type') === $data['type']) {
|
|
|
177 |
if (!$d->get('id')) {
|
|
|
178 |
$d->set($d->datafield(), $data['value']);
|
|
|
179 |
$d->set('value', $data['value']);
|
|
|
180 |
$d->set('valueformat', $data['valueformat']);
|
|
|
181 |
$d->set('valuetrust', !empty($data['valuetrust']));
|
|
|
182 |
$d->set('contextid', $context->id);
|
|
|
183 |
$d->save();
|
|
|
184 |
}
|
|
|
185 |
return $d->get('id');
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
}
|