Differences Between: [Versions 400 and 402] [Versions 401 and 402]
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 mod_data\output; 18 19 use action_menu; 20 use action_menu_link_secondary; 21 use mod_data\manager; 22 use mod_data\preset; 23 use moodle_url; 24 use templatable; 25 use renderable; 26 use renderer_base; 27 use stdClass; 28 29 /** 30 * Renderable class for the presets table in the database activity. 31 * 32 * @package mod_data 33 * @copyright 2021 Mihail Geshoski <mihail@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class presets implements templatable, renderable { 37 38 /** @var array $presets The array containing the existing presets. */ 39 private $presets; 40 41 /** @var moodle_url $formactionurl The action url for the form. */ 42 private $formactionurl; 43 44 /** @var bool $manage Whether the manage preset options should be displayed. */ 45 private $manage; 46 47 /** @var int $id instance id */ 48 private $id; 49 50 /** @var int $cmid course module id */ 51 private $cmid; 52 53 /** 54 * The class constructor. 55 * 56 * @param manager $manager The database manager 57 * @param array $presets The array containing the existing presets 58 * @param moodle_url $formactionurl The action url for the form 59 * @param bool $manage Whether the manage preset options should be displayed 60 */ 61 public function __construct(manager $manager, array $presets, moodle_url $formactionurl, bool $manage = false) { 62 $this->id = $manager->get_instance()->id; 63 $this->cmid = $manager->get_coursemodule()->id; 64 $this->presets = $presets; 65 $this->formactionurl = $formactionurl; 66 $this->manage = $manage; 67 } 68 69 /** 70 * Export the data for the mustache template. 71 * 72 * @param renderer_base $output The renderer to be used to render the action bar elements. 73 * @return array 74 */ 75 public function export_for_template(renderer_base $output): array { 76 $presets = $this->get_presets($output); 77 return [ 78 'id' => $this->cmid, 79 'formactionurl' => $this->formactionurl->out(), 80 'showmanage' => $this->manage, 81 'presets' => $presets, 82 ]; 83 } 84 85 /** 86 * Returns the presets list with the information required to display them. 87 * 88 * @param renderer_base $output The renderer to be used to render the action bar elements. 89 * @return array Presets list. 90 */ 91 private function get_presets(renderer_base $output): array { 92 $presets = []; 93 foreach ($this->presets as $index => $preset) { 94 $presetname = $preset->name; 95 $userid = $preset instanceof preset ? $preset->get_userid() : $preset->userid; 96 if (!empty($userid)) { 97 // If the preset has the userid field, the full name of creator it will be added to the end of the name. 98 $userfieldsapi = \core_user\fields::for_name(); 99 $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects; 100 $fields = 'id, ' . $namefields; 101 $presetuser = \core_user::get_user($userid, $fields, MUST_EXIST); 102 $username = fullname($presetuser, true); 103 $presetname = "{$presetname} ({$username})"; 104 } 105 $actions = $this->get_preset_action_menu($output, $preset, $userid); 106 107 $fullname = $preset->get_fullname(); 108 $previewurl = new moodle_url( 109 '/mod/data/preset.php', 110 ['d' => $this->id, 'fullname' => $fullname, 'action' => 'preview'] 111 ); 112 113 $presets[] = [ 114 'id' => $this->id, 115 'cmid' => $this->cmid, 116 'name' => $preset->name, 117 'url' => $previewurl->out(), 118 'shortname' => $preset->shortname, 119 'fullname' => $presetname, 120 'description' => $preset->description, 121 'userid' => $userid, 122 'actions' => $actions, 123 'presetindex' => $index, 124 ]; 125 } 126 127 return $presets; 128 } 129 130 /** 131 * Return the preset action menu data. 132 * 133 * @param renderer_base $output The renderer to be used to render the action bar elements. 134 * @param preset|stdClass $preset the preset object 135 * @param int|null $userid the user id (null for plugin presets) 136 * @return stdClass the resulting action menu 137 */ 138 private function get_preset_action_menu(renderer_base $output, $preset, ?int $userid): stdClass { 139 140 $actions = new stdClass(); 141 // If we cannot manage then return an empty menu. 142 if (!$this->manage) { 143 return $actions; 144 } 145 $actionmenu = new action_menu(); 146 $actionmenu->set_kebab_trigger(); 147 $actionmenu->set_action_label(get_string('actions')); 148 $actionmenu->set_additional_classes('presets-actions'); 149 $canmanage = $preset->can_manage(); 150 151 $usepreseturl = new moodle_url('/mod/data/preset.php', [ 152 'action' => 'usepreset', 153 'cmid' => $this->cmid, 154 ]); 155 $this->add_action_menu($actionmenu, get_string('usepreset', 'mod_data'), $usepreseturl, [ 156 'data-action' => 'selectpreset', 157 'data-presetname' => $preset->get_fullname(), 158 'data-cmid' => $this->cmid, 159 ] 160 ); 161 162 // Attention: the id here is the cm->id, not d->id. 163 $previewpreseturl = new moodle_url('/mod/data/preset.php', [ 164 'fullname' => $preset->get_fullname(), 165 'action' => 'preview', 166 'id' => $this->cmid, 167 ]); 168 $this->add_action_menu($actionmenu, get_string('previewaction', 'mod_data'), $previewpreseturl, [ 169 'data-action' => 'preview', 170 ] 171 ); 172 173 // Presets saved by users can be edited or removed. 174 if (!$preset->isplugin) { 175 // Edit. 176 if ($canmanage) { 177 $editactionurl = new moodle_url('/mod/data/preset.php', [ 178 'action' => 'edit', 179 'd' => $this->id, 180 ]); 181 $this->add_action_menu($actionmenu, get_string('edit'), $editactionurl, [ 182 'data-action' => 'editpreset', 183 'data-presetname' => $preset->name, 184 'data-presetdescription' => $preset->description, 185 ]); 186 } 187 188 // Export. 189 $exporturl = new moodle_url('/mod/data/preset.php', [ 190 'presetname' => $preset->name, 191 'action' => 'export', 192 'd' => $this->id, 193 ]); 194 $this->add_action_menu($actionmenu, get_string('export', 'mod_data'), $exporturl, [ 195 'data-action' => 'exportpreset', 196 'data-presetname' => $preset->name, 197 'data-presetdescription' => $preset->description, 198 ]); 199 200 // Delete. 201 if ($canmanage) { 202 203 $deleteactionurl = new moodle_url('/mod/data/preset.php', [ 204 'action' => 'delete', 205 'd' => $this->id, 206 ]); 207 $this->add_action_menu($actionmenu, get_string('delete'), $deleteactionurl, [ 208 'data-action' => 'deletepreset', 209 'data-presetname' => $preset->name, 210 ]); 211 } 212 } 213 $actions = $actionmenu->export_for_template($output); 214 return $actions; 215 } 216 217 /** 218 * Add action to the action menu 219 * 220 * @param action_menu $actionmenu 221 * @param string $actionlabel 222 * @param moodle_url $actionurl 223 * @param array $otherattributes 224 * @return void 225 */ 226 private function add_action_menu(action_menu &$actionmenu, string $actionlabel, moodle_url $actionurl, 227 array $otherattributes) { 228 $attributes = [ 229 'data-dataid' => $this->id, 230 ]; 231 $actionmenu->add(new action_menu_link_secondary( 232 $actionurl, 233 null, 234 $actionlabel, 235 array_merge($attributes, $otherattributes), 236 )); 237 } 238 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body