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 mod_data\manager; 20 use mod_data\preset; 21 use moodle_url; 22 use url_select; 23 24 /** 25 * Class responsible for generating the action bar elements in the database module pages. 26 * 27 * @package mod_data 28 * @copyright 2021 Mihail Geshoski <mihail@moodle.com> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class action_bar { 32 33 /** @var int $id The database module id. */ 34 private $id; 35 36 /** @var int $cmid The database course module id. */ 37 private $cmid; 38 39 /** @var moodle_url $currenturl The URL of the current page. */ 40 private $currenturl; 41 42 /** 43 * The class constructor. 44 * 45 * @param int $id The database module id. 46 * @param moodle_url $pageurl The URL of the current page. 47 */ 48 public function __construct(int $id, moodle_url $pageurl) { 49 $this->id = $id; 50 [$course, $cm] = get_course_and_cm_from_instance($this->id, 'data'); 51 $this->cmid = $cm->id; 52 $this->currenturl = $pageurl; 53 } 54 55 /** 56 * Generate the output for the action bar in the field page. 57 * 58 * @param bool $hasfieldselect Whether the field selector element should be rendered. 59 * @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore. 60 * @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore. 61 * @return string The HTML code for the action bar. 62 */ 63 public function get_fields_action_bar( 64 bool $hasfieldselect = false, 65 ?bool $unused1 = null, 66 ?bool $unused2 = null 67 ): string { 68 global $PAGE; 69 70 if ($unused1 !== null || $unused2 !== null) { 71 debugging('Deprecated argument passed to get_fields_action_bar method', DEBUG_DEVELOPER); 72 } 73 74 $renderer = $PAGE->get_renderer('mod_data'); 75 $fieldsactionbar = new fields_action_bar($this->id); 76 77 return $renderer->render_fields_action_bar($fieldsactionbar); 78 } 79 80 /** 81 * Generate the output for the action bar in the field mappings page. 82 * 83 * @return string The HTML code for the action bar. 84 */ 85 public function get_fields_mapping_action_bar(): string { 86 global $PAGE; 87 88 $renderer = $PAGE->get_renderer('mod_data'); 89 $fieldsactionbar = new fields_mappings_action_bar($this->id); 90 91 $data = $fieldsactionbar->export_for_template($renderer); 92 return $renderer->render_from_template('mod_data/fields_action_bar', $data); 93 } 94 95 /** 96 * Generate the output for the create a new field action menu. 97 * 98 * @param bool $isprimarybutton is the action trigger a primary or secondary button? 99 * @return \action_menu Action menu to create a new field 100 */ 101 public function get_create_fields(bool $isprimarybutton = false): \action_menu { 102 // Get the list of possible fields (plugins). 103 $plugins = \core_component::get_plugin_list('datafield'); 104 $menufield = []; 105 foreach ($plugins as $plugin => $fulldir) { 106 $menufield[$plugin] = get_string('pluginname', "datafield_{$plugin}"); 107 } 108 asort($menufield); 109 110 $fieldselect = new \action_menu(); 111 $triggerclasses = ['btn']; 112 $triggerclasses[] = $isprimarybutton ? 'btn-primary' : 'btn-secondary'; 113 $fieldselect->set_menu_trigger(get_string('newfield', 'mod_data'), join(' ', $triggerclasses)); 114 $fieldselectparams = ['id' => $this->cmid, 'mode' => 'new']; 115 foreach ($menufield as $fieldtype => $fieldname) { 116 $fieldselectparams['newtype'] = $fieldtype; 117 $fieldselect->add(new \action_menu_link( 118 new moodle_url('/mod/data/field.php', $fieldselectparams), 119 new \pix_icon('field/' . $fieldtype, $fieldname, 'data'), 120 $fieldname, 121 false 122 )); 123 } 124 $fieldselect->set_additional_classes('singlebutton'); 125 126 return $fieldselect; 127 } 128 129 /** 130 * Generate the output for the action selector in the view page. 131 * 132 * @param bool $hasentries Whether entries exist. 133 * @param string $mode The current view mode (list, view...). 134 * @return string The HTML code for the action selector. 135 */ 136 public function get_view_action_bar(bool $hasentries, string $mode): string { 137 global $PAGE; 138 139 $viewlistlink = new moodle_url('/mod/data/view.php', ['d' => $this->id]); 140 $viewsinglelink = new moodle_url('/mod/data/view.php', ['d' => $this->id, 'mode' => 'single']); 141 142 $menu = [ 143 $viewlistlink->out(false) => get_string('listview', 'mod_data'), 144 $viewsinglelink->out(false) => get_string('singleview', 'mod_data'), 145 ]; 146 147 $activeurl = $this->currenturl; 148 149 if ($this->currenturl->get_param('rid') || $this->currenturl->get_param('mode') == 'single') { 150 $activeurl = $viewsinglelink; 151 } 152 153 $urlselect = new url_select($menu, $activeurl->out(false), null, 'viewactionselect'); 154 $urlselect->set_label(get_string('viewnavigation', 'mod_data'), ['class' => 'sr-only']); 155 $renderer = $PAGE->get_renderer('mod_data'); 156 $viewactionbar = new view_action_bar($this->id, $urlselect, $hasentries, $mode); 157 158 return $renderer->render_view_action_bar($viewactionbar); 159 } 160 161 /** 162 * Generate the output for the action selector in the templates page. 163 * 164 * @return string The HTML code for the action selector. 165 */ 166 public function get_templates_action_bar(): string { 167 global $PAGE; 168 169 $listtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 170 'mode' => 'listtemplate']); 171 $singletemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 172 'mode' => 'singletemplate']); 173 $advancedsearchtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 174 'mode' => 'asearchtemplate']); 175 $addtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'addtemplate']); 176 $rsstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'rsstemplate']); 177 $csstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'csstemplate']); 178 $jstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'jstemplate']); 179 180 $menu = [ 181 $addtemplatelink->out(false) => get_string('addtemplate', 'mod_data'), 182 $singletemplatelink->out(false) => get_string('singletemplate', 'mod_data'), 183 $listtemplatelink->out(false) => get_string('listtemplate', 'mod_data'), 184 $advancedsearchtemplatelink->out(false) => get_string('asearchtemplate', 'mod_data'), 185 $csstemplatelink->out(false) => get_string('csstemplate', 'mod_data'), 186 $jstemplatelink->out(false) => get_string('jstemplate', 'mod_data'), 187 $rsstemplatelink->out(false) => get_string('rsstemplate', 'mod_data'), 188 ]; 189 190 $selectmenu = new \core\output\select_menu('presetsactions', $menu, $this->currenturl->out(false)); 191 $selectmenu->set_label(get_string('templatesnavigation', 'mod_data'), ['class' => 'sr-only']); 192 193 $renderer = $PAGE->get_renderer('mod_data'); 194 195 $presetsactions = $this->get_presets_actions_select(false); 196 197 // Reset single template action. 198 $resetcurrrent = new moodle_url($this->currenturl); 199 $resetcurrrent->param('action', 'resettemplate'); 200 $presetsactions->add(new \action_menu_link( 201 $resetcurrrent, 202 null, 203 get_string('resettemplate', 'mod_data'), 204 false, 205 ['data-action' => 'resettemplate', 'data-dataid' => $this->id] 206 )); 207 208 // Reset all templates action. 209 $resetallurl = new moodle_url($this->currenturl); 210 $resetallurl->params([ 211 'action' => 'resetalltemplates', 212 'sesskey' => sesskey(), 213 ]); 214 $presetsactions->add(new \action_menu_link( 215 $resetallurl, 216 null, 217 get_string('resetalltemplates', 'mod_data'), 218 false, 219 ['data-action' => 'resetalltemplates', 'data-dataid' => $this->id] 220 )); 221 222 $templatesactionbar = new templates_action_bar($this->id, $selectmenu, null, null, $presetsactions); 223 224 return $renderer->render_templates_action_bar($templatesactionbar); 225 } 226 227 /** 228 * Generate the output for the action selector in the presets page. 229 * 230 * @return string The HTML code for the action selector. 231 */ 232 public function get_presets_action_bar(): string { 233 global $PAGE; 234 235 $renderer = $PAGE->get_renderer('mod_data'); 236 $presetsactionbar = new presets_action_bar($this->cmid, $this->get_presets_actions_select(true)); 237 238 return $renderer->render_presets_action_bar($presetsactionbar); 239 } 240 241 /** 242 * Generate the output for the action selector in the presets preview page. 243 * 244 * @param manager $manager the manager instance 245 * @param string $fullname the preset fullname 246 * @param string $current the current template name 247 * @return string The HTML code for the action selector 248 */ 249 public function get_presets_preview_action_bar(manager $manager, string $fullname, string $current): string { 250 global $PAGE; 251 252 $renderer = $PAGE->get_renderer(manager::PLUGINNAME); 253 254 $cm = $manager->get_coursemodule(); 255 256 $menu = []; 257 $selected = null; 258 foreach (['listtemplate', 'singletemplate'] as $templatename) { 259 $link = new moodle_url('/mod/data/preset.php', [ 260 'd' => $this->id, 261 'template' => $templatename, 262 'fullname' => $fullname, 263 'action' => 'preview', 264 ]); 265 $menu[$link->out(false)] = get_string($templatename, manager::PLUGINNAME); 266 if (!$selected || $templatename == $current) { 267 $selected = $link->out(false); 268 } 269 } 270 $urlselect = new url_select($menu, $selected, null); 271 $urlselect->set_label(get_string('templatesnavigation', manager::PLUGINNAME), ['class' => 'sr-only']); 272 273 $data = [ 274 'title' => get_string('preview', manager::PLUGINNAME, preset::get_name_from_plugin($fullname)), 275 'hasback' => true, 276 'backtitle' => get_string('back'), 277 'backurl' => new moodle_url('/mod/data/preset.php', ['id' => $cm->id]), 278 'extraurlselect' => $urlselect->export_for_template($renderer), 279 ]; 280 return $renderer->render_from_template('mod_data/action_bar', $data); 281 } 282 283 /** 284 * Helper method to get the selector for the presets action. 285 * 286 * @param bool $hasimport Whether the Import buttons must be included or not. 287 * @return \action_menu|null The selector object used to display the presets actions. Null when the import button is not 288 * displayed and the database hasn't any fields. 289 */ 290 protected function get_presets_actions_select(bool $hasimport = false): ?\action_menu { 291 global $DB; 292 293 $hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]); 294 295 // Early return if the database has no fields and the import action won't be displayed. 296 if (!$hasfields && !$hasimport) { 297 return null; 298 } 299 300 $actionsselect = new \action_menu(); 301 $actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary'); 302 303 if ($hasimport) { 304 // Import. 305 $actionsselectparams = ['id' => $this->cmid]; 306 $actionsselect->add(new \action_menu_link( 307 new moodle_url('/mod/data/preset.php', $actionsselectparams), 308 null, 309 get_string('importpreset', 'mod_data'), 310 false, 311 ['data-action' => 'importpresets', 'data-dataid' => $this->cmid] 312 )); 313 } 314 315 // If the database has no fields, export and save as preset options shouldn't be displayed. 316 if ($hasfields) { 317 // Export. 318 $actionsselectparams = ['id' => $this->cmid, 'action' => 'export']; 319 $actionsselect->add(new \action_menu_link( 320 new moodle_url('/mod/data/preset.php', $actionsselectparams), 321 null, 322 get_string('exportpreset', 'mod_data'), 323 false 324 )); 325 // Save as preset. 326 $actionsselect->add(new \action_menu_link( 327 new moodle_url('/mod/data/preset.php', $actionsselectparams), 328 null, 329 get_string('saveaspreset', 'mod_data'), 330 false, 331 ['data-action' => 'saveaspreset', 'data-dataid' => $this->id] 332 )); 333 } 334 335 return $actionsselect; 336 } 337 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body