See Release Notes
Long Term Support Release
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 defined('MOODLE_INTERNAL') || die(); 18 19 use core_adminpresets\local\setting\adminpresets_setting; 20 use core_adminpresets\manager; 21 use core_adminpresets\helper; 22 23 global $CFG; 24 require_once($CFG->libdir . '/adminlib.php'); 25 26 /** 27 * Data generator for adminpresets component. 28 * 29 * @package core_adminpresets 30 * @category test 31 * @copyright 2021 Sara Arjona (sara@moodle.com) 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class core_adminpresets_generator extends \component_generator_base { 35 36 /** 37 * Create a preset. This preset will have only 3 settings and 3 plugins. 38 * Settings: 39 * - none.enablebadges = 0 40 * - none.allowemojipicker = 1 41 * - mod_lesson.mediawidth = 900 42 * - mod_lesson.maxanswers = 2 with advanced disabled. 43 * Plugins: 44 * - enrol_guest = 0 45 * - mod_glossary = 0 46 * - qtype_truefalse = 1 47 * 48 * @param array $data Preset data. Supported values: 49 * - name. To define the preset name. 50 * - comments. To change the comments field. 51 * - author. To set the author. 52 * - applypreset. Whether the preset should be applied too or not. 53 * @return int Identifier of the preset created. 54 */ 55 public function create_preset(array $data = []): int { 56 global $DB, $USER, $CFG; 57 58 if (!isset($data['name'])) { 59 $data['name'] = 'Preset default name'; 60 } 61 if (!isset($data['comments'])) { 62 $data['comments'] = 'Preset default comment'; 63 } 64 if (!isset($data['author'])) { 65 $data['author'] = 'Default author'; 66 } 67 if (!isset($data['iscore'])) { 68 $data['iscore'] = manager::NONCORE_PRESET; 69 } 70 // Validate iscore value. 71 $allowed = [manager::NONCORE_PRESET, manager::STARTER_PRESET, manager::FULL_PRESET]; 72 if (!in_array($data['iscore'], $allowed)) { 73 $data['iscore'] = manager::NONCORE_PRESET; 74 } 75 76 $preset = [ 77 'userid' => $USER->id, 78 'name' => $data['name'], 79 'comments' => $data['comments'], 80 'site' => $CFG->wwwroot, 81 'author' => $data['author'], 82 'moodleversion' => $CFG->version, 83 'moodlerelease' => $CFG->release, 84 'timecreated' => time(), 85 'timeimported' => 0, 86 'iscore' => $data['iscore'], 87 ]; 88 89 $presetid = $DB->insert_record('adminpresets', $preset); 90 $preset['id'] = $presetid; 91 92 // Setting: enablebadges = 0. 93 helper::add_item($presetid, 'enablebadges', '0'); 94 // Setting: allowemojipicker = 1. 95 helper::add_item($presetid, 'allowemojipicker', '1'); 96 // Setting: mediawidth = 900. 97 helper::add_item($presetid, 'mediawidth', '900', 'mod_lesson'); 98 // Setting: maxanswers = 2 (with advanced disabled). 99 helper::add_item($presetid, 'maxanswers', '2', 'mod_lesson', 'maxanswers_adv', 0); 100 101 // Plugin: enrol_guest = 0. 102 helper::add_plugin($presetid, 'enrol', 'guest', 0); 103 // Plugin: mod_glossary = 0. 104 helper::add_plugin($presetid, 'mod', 'glossary', 0); 105 // Plugin: qtype_truefalse. 106 helper::add_plugin($presetid, 'qtype', 'truefalse', 1); 107 108 // Check if the preset should be created as applied preset too, to fill in the rest of the tables. 109 $applypreset = isset($data['applypreset']) && $data['applypreset']; 110 if ($applypreset) { 111 $presetapp = [ 112 'adminpresetid' => $presetid, 113 'userid' => $USER->id, 114 'time' => time(), 115 ]; 116 $appid = $DB->insert_record('adminpresets_app', $presetapp); 117 118 $this->apply_setting($appid, 'enablebadges', '1', '0'); 119 // The allowemojipicker setting shouldn't be applied because the value matches the current one. 120 $this->apply_setting($appid, 'mediawidth', '640', '900', 'mod_lesson'); 121 $this->apply_setting($appid, 'maxanswers', '5', '2', 'mod_lesson'); 122 $this->apply_setting($appid, 'maxanswers_adv', '1', '0', 'mod_lesson', 'maxanswers'); 123 124 $this->apply_plugin($appid, 'enrol', 'guest', 1, 0); 125 $this->apply_plugin($appid, 'mod', 'glossary', 1, 0); 126 // The qtype_truefalse plugin shouldn't be applied because the value matches the current one. 127 } 128 129 return $presetid; 130 } 131 132 /** 133 * Helper method to create an applied setting item. 134 * 135 * @param int $appid The applied preset identifier. 136 * @param string $name The setting name. 137 * @param string $oldvalue The setting old value. 138 * @param string $newvalue The setting new value. 139 * @param string|null $plugin The setting plugin (or null if none). 140 * @param string|null $itemname Whether it should be treated as advanced item or not. 141 * 142 * @return bool|int true or new id. 143 */ 144 private function apply_setting(int $appid, string $name, string $oldvalue, string $newvalue, ?string $plugin = null, 145 ?string $itemname = null) { 146 global $DB; 147 148 set_config($name, $newvalue, $plugin); 149 $configlogid = $this->add_to_config_log($name, $oldvalue, $newvalue, $plugin); 150 $presetappitem = [ 151 'adminpresetapplyid' => $appid, 152 'configlogid' => $configlogid, 153 ]; 154 $table = 'adminpresets_app_it'; 155 if (!is_null($itemname)) { 156 $table = 'adminpresets_app_it_a'; 157 $presetappitem['itemname'] = $itemname; 158 } 159 $appitemid = $DB->insert_record($table, $presetappitem); 160 161 return $appitemid; 162 163 } 164 165 /** 166 * Helper method to create an applied plugin. 167 * 168 * @param int $appid The applied preset identifier. 169 * @param string $plugin The plugin type. 170 * @param string $name The plugin name. 171 * @param int $oldvalue The setting old value. 172 * @param int $newvalue The setting new value. 173 * 174 * @return bool|int true or new id. 175 */ 176 private function apply_plugin(int $appid, string $plugin, string $name, int $oldvalue, int $newvalue) { 177 global $DB; 178 179 // Change plugin visibility. 180 $pluginclass = \core_plugin_manager::resolve_plugininfo_class($plugin); 181 $pluginclass::enable_plugin($name, $newvalue); 182 183 // Create entry in applied plugins table. 184 $presetappplug = [ 185 'adminpresetapplyid' => $appid, 186 'plugin' => $plugin, 187 'name' => $name, 188 'value' => $newvalue, 189 'oldvalue' => $oldvalue, 190 ]; 191 $appplugid = $DB->insert_record('adminpresets_app_plug', $presetappplug); 192 193 return $appplugid; 194 } 195 196 /** 197 * Helper method to add entry in config_log. 198 * 199 * @param string $name The setting name. 200 * @param string $oldvalue The setting old value. 201 * @param string $value The setting new value. 202 * @param string|null $plugin The setting plugin (or null if the setting doesn't belong to any plugin). 203 * @return int The id of the config_log entry created. 204 */ 205 private function add_to_config_log(string $name, string $oldvalue, string $value, ?string $plugin = null): int { 206 global $DB, $USER; 207 208 $log = new stdClass(); 209 $log->userid = $USER->id; 210 $log->timemodified = time(); 211 $log->name = $name; 212 $log->oldvalue = $oldvalue; 213 $log->value = $value; 214 $log->plugin = $plugin; 215 $id = $DB->insert_record('config_log', $log); 216 217 return $id; 218 } 219 220 /** 221 * Helper method to access to a protected property. 222 * 223 * @param string|object $object The class. 224 * @param string $property The private/protected property in $object to access. 225 * @return mixed The current value of the property. 226 */ 227 public function access_protected($object, string $property) { 228 $reflection = new ReflectionClass($object); 229 $property = $reflection->getProperty($property); 230 $property->setAccessible(true); 231 return $property->getValue($object); 232 } 233 234 235 /** 236 * Given a tree category and setting name, it gets the adminpresets_setting class. 237 * 238 * @param string $category Tree category name where the setting is located. 239 * @param string $settingname Setting name to get the class. 240 * @return adminpresets_setting 241 */ 242 public function get_admin_preset_setting(string $category, string $settingname): adminpresets_setting { 243 $adminroot = admin_get_root(); 244 245 // Set method accessibility. 246 $method = new ReflectionMethod(manager::class, 'get_setting'); 247 $method->setAccessible(true); 248 249 // Get the proper adminpresets_setting instance. 250 $settingpage = $adminroot->locate($category); 251 $settingdata = $settingpage->settings->$settingname; 252 return $method->invokeArgs(new manager(), [$settingdata, '']); 253 } 254 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body