Differences Between: [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 templatable; 20 use renderable; 21 use core_tag_tag; 22 use mod_data\manager; 23 24 /** 25 * Renderable class for template editor tools. 26 * 27 * @package mod_data 28 * @copyright 2022 Ferran Recio <ferran@moodle.com> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class template_editor_tools implements templatable, renderable { 32 33 /** @var manager manager instance. */ 34 private $manager; 35 36 /** @var string the template name. */ 37 private $templatename; 38 39 /** 40 * The class constructor. 41 * 42 * @param manager $manager the activity instance manager 43 * @param string $templatename the template to edit 44 */ 45 public function __construct(manager $manager, string $templatename) { 46 $this->manager = $manager; 47 $this->templatename = $templatename; 48 } 49 50 /** 51 * Export the data for the mustache template. 52 * 53 * @param \renderer_base $output renderer to be used to render the action bar elements. 54 * @return array 55 */ 56 public function export_for_template(\renderer_base $output): array { 57 $tools = [ 58 $this->get_field_tags($this->templatename), 59 $this->get_field_info_tags($this->templatename), 60 $this->get_action_tags($this->templatename), 61 $this->get_other_tags($this->templatename), 62 ]; 63 $tools = array_filter($tools, static function ($value) { 64 return !empty($value['tags']); 65 }); 66 return [ 67 'toolshelp' => $output->help_icon('availabletags', 'data'), 68 'hastools' => !empty($tools), 69 'tools' => array_values($tools), 70 ]; 71 } 72 73 /** 74 * Return the field template tags. 75 * 76 * @param string $templatename the template name 77 * @return array|null array of tags. 78 */ 79 protected function get_field_tags(string $templatename): array { 80 $name = get_string('fields', 'data'); 81 if ($templatename == 'csstemplate' || $templatename == 'jstemplate') { 82 return $this->get_optgroup_data($name, []); 83 } 84 $taglist = []; 85 $fields = $this->manager->get_fields(); 86 foreach ($fields as $field) { 87 if ($field->type === 'unknown') { 88 continue; 89 } 90 $fieldname = $field->get_name(); 91 $taglist["[[$fieldname]]"] = $fieldname; 92 } 93 $taglist['##otherfields##'] = get_string('otherfields', 'data'); 94 return $this->get_optgroup_data($name, $taglist); 95 } 96 97 /** 98 * Return the field information template tags. 99 * 100 * @param string $templatename the template name 101 * @return array|null array of tags. 102 */ 103 protected function get_field_info_tags(string $templatename): array { 104 $name = get_string('fieldsinformationtags', 'data'); 105 $taglist = []; 106 $fields = $this->manager->get_fields(); 107 foreach ($fields as $field) { 108 if ($field->type === 'unknown') { 109 continue; 110 } 111 $fieldname = $field->get_name(); 112 if ($templatename == 'addtemplate') { 113 $taglist["[[$fieldname#id]]"] = get_string('fieldtagid', 'mod_data', $fieldname); 114 } 115 $taglist["[[$fieldname#name]]"] = get_string('fieldtagname', 'mod_data', $fieldname); 116 $taglist["[[$fieldname#description]]"] = get_string('fieldtagdescription', 'mod_data', $fieldname); 117 } 118 return $this->get_optgroup_data($name, $taglist); 119 } 120 121 /** 122 * Return the field action tags. 123 * 124 * @param string $templatename the template name 125 * @return array|null array of tags. 126 */ 127 protected function get_action_tags(string $templatename): array { 128 $name = get_string('actions'); 129 if ($templatename == 'addtemplate' || $templatename == 'asearchtemplate') { 130 return $this->get_optgroup_data($name, []); 131 } 132 $taglist = [ 133 '##actionsmenu##' => get_string('actionsmenu', 'data'), 134 '##edit##' => get_string('edit', 'data'), 135 '##delete##' => get_string('delete', 'data'), 136 '##approve##' => get_string('approve', 'data'), 137 '##disapprove##' => get_string('disapprove', 'data'), 138 ]; 139 if ($templatename != 'rsstemplate') { 140 $taglist['##export##'] = get_string('export', 'data'); 141 } 142 if ($templatename != 'singletemplate') { 143 $taglist['##more##'] = get_string('more', 'data'); 144 $taglist['##moreurl##'] = get_string('moreurl', 'data'); 145 $taglist['##delcheck##'] = get_string('delcheck', 'data'); 146 } 147 return $this->get_optgroup_data($name, $taglist); 148 } 149 150 /** 151 * Return the available other tags 152 * 153 * @param string $templatename the template name 154 * @return array associative array of tags => tag name 155 */ 156 protected function get_other_tags(string $templatename): array { 157 $name = get_string('other', 'data'); 158 $taglist = []; 159 if ($templatename == 'asearchtemplate') { 160 $taglist['##firstname##'] = get_string('firstname'); 161 $taglist['##lastname##'] = get_string('lastname'); 162 return $this->get_optgroup_data($name, $taglist); 163 } 164 if (core_tag_tag::is_enabled('mod_data', 'data_records')) { 165 $taglist['##tags##'] = get_string('tags'); 166 } 167 if ($templatename == 'addtemplate') { 168 return $this->get_optgroup_data($name, $taglist); 169 } 170 $taglist['##timeadded##'] = get_string('timeadded', 'data'); 171 $taglist['##timemodified##'] = get_string('timemodified', 'data'); 172 $taglist['##user##'] = get_string('user'); 173 $taglist['##userpicture##'] = get_string('userpic'); 174 $taglist['##approvalstatus##'] = get_string('approvalstatus', 'data'); 175 $taglist['##id##'] = get_string('id', 'data'); 176 177 if ($templatename == 'singletemplate') { 178 return $this->get_optgroup_data($name, $taglist); 179 } 180 181 $taglist['##comments##'] = get_string('comments', 'data'); 182 183 return $this->get_optgroup_data($name, $taglist); 184 } 185 186 /** 187 * Generate a valid optgroup data. 188 * 189 * @param string $name the optgroup name 190 * @param array $taglist the indexed array of taglists ($tag => $tagname) 191 * @return array of optgroup data 192 */ 193 protected function get_optgroup_data (string $name, array $taglist): array { 194 $tags = []; 195 foreach ($taglist as $tag => $tagname) { 196 $tags[] = [ 197 'tag' => "$tag", 198 'tagname' => $tagname . ' - ' . $tag, 199 ]; 200 } 201 return [ 202 'name' => $name, 203 'tags' => $tags, 204 ]; 205 } 206 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body