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 mod_data\manager; 22 use mod_data\preset; 23 use mod_data\template; 24 use moodle_page; 25 use moodle_url; 26 27 /** 28 * Preset preview output class. 29 * 30 * @package mod_data 31 * @copyright 2022 Ferran Recio <ferran@moodle.com> 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class preset_preview implements templatable, renderable { 35 36 /** @var manager manager instance. */ 37 private $manager; 38 39 /** @var preset the preset. */ 40 private $preset; 41 42 /** @var string the template to preview. */ 43 private $templatename; 44 45 /** 46 * The class constructor. 47 * 48 * @param manager $manager the activity instance manager 49 * @param preset $preset the preset 50 * @param string $templatename the templatename 51 */ 52 public function __construct(manager $manager, preset $preset, string $templatename) { 53 $this->manager = $manager; 54 $this->preset = $preset; 55 $this->templatename = $templatename; 56 } 57 58 /** 59 * Add the preset CSS and JS to the page. 60 * 61 * @param moodle_page $page the current page instance 62 */ 63 public function prepare_page(moodle_page $page) { 64 $instance = $this->manager->get_instance(); 65 $preset = $this->preset; 66 // Add CSS and JS. 67 $csscontent = $preset->get_template_content('csstemplate'); 68 if (!empty($csscontent)) { 69 $url = new moodle_url('/mod/data/css.php', ['d' => $instance->id, 'preset' => $preset->get_fullname()]); 70 $page->requires->css($url); 71 } 72 $jscontent = $preset->get_template_content('jstemplate'); 73 if (!empty($jscontent)) { 74 $url = new moodle_url('/mod/data/js.php', ['d' => $instance->id, 'preset' => $preset->get_fullname()]); 75 $page->requires->js($url); 76 } 77 } 78 79 /** 80 * Export the data for the mustache template. 81 * 82 * @param \renderer_base $output renderer to be used to render the action bar elements. 83 * @return array 84 */ 85 public function export_for_template(\renderer_base $output): array { 86 $coursemodule = $this->manager->get_coursemodule(); 87 $preset = $this->preset; 88 89 // Get fields for preview. 90 $count = ($this->templatename == 'listtemplate') ? 2 : 1; 91 $fields = $preset->get_fields(true); 92 $entries = $preset->get_sample_entries($count); 93 $templatecontent = $preset->get_template_content($this->templatename); 94 $useurl = new moodle_url('/mod/data/field.php'); 95 96 // Generate preview content. 97 $options = ['templatename' => $this->templatename]; 98 if ($this->templatename == 'listtemplate') { 99 $options['showmore'] = true; 100 } 101 $parser = new template($this->manager, $templatecontent, $options, $fields); 102 $content = $parser->parse_entries($entries); 103 if ($this->templatename == 'listtemplate') { 104 $listtemplateheader = $preset->get_template_content('listtemplateheader'); 105 $listtemplatefooter = $preset->get_template_content('listtemplatefooter'); 106 $content = $listtemplateheader . $content . $listtemplatefooter; 107 } 108 109 return [ 110 'cmid' => $coursemodule->id, 111 'description' => $preset->description ?? '', 112 'preview' => $content, 113 'formactionurl' => $useurl->out(), 114 'userid' => $preset->get_userid() ?? 0, 115 'shortname' => $preset->shortname, 116 ]; 117 } 118 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body