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 * Grouping 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 grouping_handler extends handler { 37 38 /** 39 * @var grouping_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/grouping_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 if ($instanceid > 0) { 125 $grouping = $DB->get_record('groupings', ['id' => $instanceid], '*', MUST_EXIST); 126 return context_course::instance($grouping->courseid); 127 } else if (!empty($COURSE->id)) { 128 return context_course::instance($COURSE->id); 129 } else { 130 return context_system::instance(); 131 } 132 } 133 134 /** 135 * Get raw data associated with all fields current user can view or edit 136 * 137 * @param int $instanceid 138 * @return array 139 */ 140 public function get_instance_data_for_backup(int $instanceid): array { 141 $finalfields = []; 142 $instancedata = $this->get_instance_data($instanceid, true); 143 foreach ($instancedata as $data) { 144 if ($data->get('id') && $this->can_backup($data->get_field(), $instanceid)) { 145 $finalfields[] = [ 146 'id' => $data->get('id'), 147 'shortname' => $data->get_field()->get('shortname'), 148 'type' => $data->get_field()->get('type'), 149 'value' => $data->get_value(), 150 'valueformat' => $data->get('valueformat'), 151 'groupingid' => $data->get('instanceid'), 152 ]; 153 } 154 } 155 return $finalfields; 156 } 157 158 /** 159 * Creates or updates custom field data for a instanceid from backup data. 160 * 161 * The handlers have to override it if they support backup 162 * 163 * @param restore_task $task 164 * @param array $data 165 */ 166 public function restore_instance_data_from_backup(restore_task $task, array $data) { 167 $instanceid = $data['groupingid']; 168 $context = $this->get_instance_context($instanceid); 169 $editablefields = $this->get_editable_fields($instanceid); 170 $records = api::get_instance_fields_data($editablefields, $instanceid); 171 172 foreach ($records as $d) { 173 $field = $d->get_field(); 174 if ($field->get('shortname') === $data['shortname'] && $field->get('type') === $data['type']) { 175 if (!$d->get('id')) { 176 $d->set($d->datafield(), $data['value']); 177 $d->set('value', $data['value']); 178 $d->set('valueformat', $data['valueformat']); 179 $d->set('contextid', $context->id); 180 $d->save(); 181 } 182 return; 183 } 184 } 185 } 186 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body