See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
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 manager $manager The database module manager. */ 39 private $manager; 40 41 /** @var array $presets The array containing the existing presets. */ 42 private $presets; 43 44 /** @var moodle_url $formactionurl The the action url for the form. */ 45 private $formactionurl; 46 47 /** @var bool $manage Whether the manage preset options should be displayed. */ 48 private $manage; 49 50 /** 51 * The class constructor. 52 * 53 * @param manager $manager The database manager 54 * @param array $presets The array containing the existing presets 55 * @param moodle_url $formactionurl The action url for the form 56 * @param bool $manage Whether the manage preset options should be displayed 57 */ 58 public function __construct(manager $manager, array $presets, moodle_url $formactionurl, bool $manage = false) { 59 $this->manager = $manager; 60 $this->presets = $presets; 61 $this->formactionurl = $formactionurl; 62 $this->manage = $manage; 63 } 64 65 /** 66 * Export the data for the mustache template. 67 * 68 * @param renderer_base $output The renderer to be used to render the action bar elements. 69 * @return array 70 */ 71 public function export_for_template(renderer_base $output): array { 72 $presets = $this->get_presets($output); 73 return [ 74 'id' => $this->manager->get_coursemodule()->id, 75 'formactionurl' => $this->formactionurl->out(), 76 'showmanage' => $this->manage, 77 'presets' => $presets, 78 ]; 79 } 80 81 /** 82 * Returns the presets list with the information required to display them. 83 * 84 * @param renderer_base $output The renderer to be used to render the action bar elements. 85 * @return array Presets list. 86 */ 87 private function get_presets(renderer_base $output): array { 88 $presets = []; 89 foreach ($this->presets as $index => $preset) { 90 $presetname = $preset->name; 91 $userid = $preset instanceof preset ? $preset->get_userid() : $preset->userid; 92 if (!empty($userid)) { 93 // If the preset has the userid field, the full name of creator it will be added to the end of the name. 94 $userfieldsapi = \core_user\fields::for_name(); 95 $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects; 96 $fields = 'id, ' . $namefields; 97 $presetuser = \core_user::get_user($userid, $fields, MUST_EXIST); 98 $username = fullname($presetuser, true); 99 $presetname = "{$presetname} ({$username})"; 100 } 101 $actions = $this->get_preset_action_menu($output, $preset, $userid); 102 103 $fullname = $preset->get_fullname(); 104 $id = $this->manager->get_instance()->id; 105 $cmid = $this->manager->get_coursemodule()->id; 106 $previewurl = new moodle_url( 107 '/mod/data/preset.php', 108 ['d' => $id, 'fullname' => $fullname, 'action' => 'preview'] 109 ); 110 111 $presets[] = [ 112 'id' => $id, 113 'cmid' => $cmid, 114 'name' => $preset->name, 115 'url' => $previewurl->out(), 116 'shortname' => $preset->shortname, 117 'fullname' => $presetname, 118 'description' => $preset->description, 119 'userid' => $userid, 120 'actions' => $actions, 121 'presetindex' => $index, 122 ]; 123 } 124 125 return $presets; 126 } 127 128 /** 129 * Return the preset action menu data. 130 * 131 * @param renderer_base $output The renderer to be used to render the action bar elements. 132 * @param preset|stdClass $preset the preset object 133 * @param int|null $userid the user id (null for plugin presets) 134 * @return stdClass the resulting action menu 135 */ 136 private function get_preset_action_menu(renderer_base $output, $preset, ?int $userid): stdClass { 137 138 $actions = new stdClass(); 139 $actionmenu = null; 140 $id = $this->manager->get_instance()->id; 141 // Only presets saved by users can be edited or removed (so the datapreset plugins shouldn't display these buttons). 142 if ($this->manage && !$preset->isplugin) { 143 $actionmenu = new action_menu(); 144 $icon = $output->pix_icon('i/menu', get_string('actions')); 145 $actionmenu->set_menu_trigger($icon, 'btn btn-icon d-flex align-items-center justify-content-center'); 146 $actionmenu->set_action_label(get_string('actions')); 147 $actionmenu->attributes['class'] .= ' presets-actions'; 148 149 $canmanage = $preset->can_manage(); 150 // Edit. 151 if ($canmanage) { 152 $params = [ 153 'd' => $id, 154 'action' => 'edit', 155 ]; 156 $editactionurl = new moodle_url('/mod/data/preset.php', $params); 157 $attributes = [ 158 'data-action' => 'editpreset', 159 'data-dataid' => $id, 160 "data-presetname" => $preset->name, 161 "data-presetdescription" => $preset->description, 162 ]; 163 $actionmenu->add(new action_menu_link_secondary( 164 $editactionurl, 165 null, 166 get_string('edit'), 167 $attributes 168 )); 169 170 } 171 172 // Export. 173 $params = [ 174 'd' => $id, 175 'presetname' => $preset->name, 176 'action' => 'export', 177 ]; 178 $exporturl = new moodle_url('/mod/data/preset.php', $params); 179 $actionmenu->add(new action_menu_link_secondary( 180 $exporturl, 181 null, 182 get_string('export', 'mod_data'), 183 )); 184 185 // Delete. 186 if ($canmanage) { 187 $params = [ 188 'd' => $id, 189 'action' => 'delete', 190 ]; 191 $deleteactionurl = new moodle_url('/mod/data/preset.php', $params); 192 $attributes = [ 193 'data-action' => 'deletepreset', 194 'data-dataid' => $id, 195 "data-presetname" => $preset->name, 196 ]; 197 $actionmenu->add(new action_menu_link_secondary( 198 $deleteactionurl, 199 null, 200 get_string('delete'), 201 $attributes, 202 )); 203 } 204 } 205 206 if (!is_null($actionmenu)) { 207 $actions = $actionmenu->export_for_template($output); 208 } 209 210 return $actions; 211 } 212 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body