From 274d79c9d5700e718d1a514485fbd7745632c293 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 25 Mar 2015 00:05:06 +0800 Subject: [PATCH] MDL-49650 tool_templatelibrary: New tool for browsing mustache templates --- .../templatelibrary/amd/build/display.min.js | 1 + .../templatelibrary/amd/build/search.min.js | 1 + admin/tool/templatelibrary/amd/src/display.js | 114 ++++++++++++++++++ admin/tool/templatelibrary/amd/src/search.js | 89 ++++++++++++++ admin/tool/templatelibrary/classes/api.php | 92 ++++++++++++++ .../tool/templatelibrary/classes/external.php | 98 +++++++++++++++ .../classes/output/list_templates_page.php | 72 +++++++++++ .../classes/output/renderer.php | 51 ++++++++ admin/tool/templatelibrary/db/services.php | 37 ++++++ admin/tool/templatelibrary/index.php | 47 ++++++++ .../lang/en/tool_templatelibrary.php | 37 ++++++ admin/tool/templatelibrary/settings.php | 33 +++++ admin/tool/templatelibrary/styles.css | 5 + .../templates/display_template.mustache | 48 ++++++++ .../templates/list_templates_page.mustache | 61 ++++++++++ .../templates/search_results.mustache | 44 +++++++ .../tests/externallib_test.php | 69 +++++++++++ admin/tool/templatelibrary/version.php | 26 ++++ lib/classes/plugin_manager.php | 2 +- lib/templates/pix_icon.mustache | 9 ++ 20 files changed, 935 insertions(+), 1 deletion(-) create mode 100644 admin/tool/templatelibrary/amd/build/display.min.js create mode 100644 admin/tool/templatelibrary/amd/build/search.min.js create mode 100644 admin/tool/templatelibrary/amd/src/display.js create mode 100644 admin/tool/templatelibrary/amd/src/search.js create mode 100644 admin/tool/templatelibrary/classes/api.php create mode 100644 admin/tool/templatelibrary/classes/external.php create mode 100644 admin/tool/templatelibrary/classes/output/list_templates_page.php create mode 100644 admin/tool/templatelibrary/classes/output/renderer.php create mode 100644 admin/tool/templatelibrary/db/services.php create mode 100644 admin/tool/templatelibrary/index.php create mode 100644 admin/tool/templatelibrary/lang/en/tool_templatelibrary.php create mode 100644 admin/tool/templatelibrary/settings.php create mode 100644 admin/tool/templatelibrary/styles.css create mode 100644 admin/tool/templatelibrary/templates/display_template.mustache create mode 100644 admin/tool/templatelibrary/templates/list_templates_page.mustache create mode 100644 admin/tool/templatelibrary/templates/search_results.mustache create mode 100644 admin/tool/templatelibrary/tests/externallib_test.php create mode 100644 admin/tool/templatelibrary/version.php diff --git a/admin/tool/templatelibrary/amd/build/display.min.js b/admin/tool/templatelibrary/amd/build/display.min.js new file mode 100644 index 00000000000..1c067570a41 --- /dev/null +++ b/admin/tool/templatelibrary/amd/build/display.min.js @@ -0,0 +1 @@ +define(["jquery","core/ajax","core/log","core/notification","core/templates","core/config","core/str"],function(a,b,c,d,e,f,g){var h=function(b,f){g.get_string("templateselected","tool_templatelibrary",b).done(function(b){a('[data-region="displaytemplateheader"]').text(b)}).fail(d.exception);var h=f.match(/{{!([\s\S]*?)}}/g),i=0;if(null!==h)for(i=0;i. + +/** + * This module adds ajax display functions to the template library page. + * + * @module tool_templatelibrary/display + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config', 'core/str'], + function($, ajax, log, notification, templates, config, str) { + + /** + * Handle a template loaded response. + * + * @param {String} templateName The template name + * @param {String} source The template source + */ + var templateLoaded = function(templateName, source) { + str.get_string('templateselected', 'tool_templatelibrary', templateName).done(function(s) { + $('[data-region="displaytemplateheader"]').text(s); + }).fail(notification.exception); + + // Remove the GPL from the start of the template. + + var sections = source.match(/{{!([\s\S]*?)}}/g); + var i = 0; + + // Find the first non-empty comment that is not the GPL. + // If no sections match - show the entire file. + if (sections !== null) { + for (i = 0; i < sections.length; i++) { + var section = sections[i]; + if ((section.trim() !== '') && (section.indexOf('GNU General Public License') === -1)) { + // Remove {{! and }} from start and end. + section = section.substr(3, section.length - 5); + source = section; + break; + } + } + } + + $('[data-region="displaytemplatesource"]').text(source); + + // Now search the text for a json example. + + var example = source.match(/Example context \(json\):([\s\S]*)/); + var context = false; + if (example) { + var rawJSON = example[1].trim(); + try { + context = $.parseJSON(rawJSON); + } catch (e) { + log.debug('Could not parse json example context for template.'); + log.debug(e); + } + } + if (context) { + templates.render(templateName, context).done(function(html, js) { + $('[data-region="displaytemplateexample"]').empty(); + $('[data-region="displaytemplateexample"]').append(html); + templates.runTemplateJS(js); + }).fail(notification.exception); + } else { + str.get_string('templatehasnoexample', 'tool_templatelibrary').done(function(s) { + $('[data-region="displaytemplateexample"]').text(s); + }).fail(notification.exception); + } + }; + + /** + * Load the a template source from Moodle. + * @param {String} templateName + */ + var loadTemplate = function(templateName) { + var parts = templateName.split('/'); + var component = parts.shift(); + var name = parts.shift(); + + ajax.call([{ + methodname: 'core_output_load_template', + args:{ + component: component, + template: name, + themename: config.theme + }, + done: function(source) { templateLoaded(templateName, source); }, + fail: notification.exception + }]); + }; + + // Add the event listeners. + $('[data-region="list-templates"]').on('click', '[data-templatename]', function() { + var templatename = $(this).data('templatename'); + loadTemplate(templatename); + }); + + // This module does not expose anything. + return {}; +}); diff --git a/admin/tool/templatelibrary/amd/src/search.js b/admin/tool/templatelibrary/amd/src/search.js new file mode 100644 index 00000000000..d4eefe66c3d --- /dev/null +++ b/admin/tool/templatelibrary/amd/src/search.js @@ -0,0 +1,89 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * This module adds ajax search functions to the template library page. + * + * @module tool_templatelibrary/search + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'], + function($, ajax, log, notification, templates) { + + /** + * The ajax call has returned with a new list of templates. + * + * @method reloadListTemplate + * @param String[] templates List of template ids. + */ + var reloadListTemplate = function(templateList) { + templates.render('tool_templatelibrary/search_results', { templates: templateList }) + .done(function (result) { + $('[data-region="searchresults"]').replaceWith(result); + }).fail(notification.exception); + }; + + /** + * Get the current values for the form inputs and refresh the list of matching templates. + * + * @method refreshSearch + */ + var refreshSearch = function() { + var componentStr = $('[data-field="component"]').val(); + var searchStr = $('[data-field="search"]').val(); + + // Trigger the search. + + ajax.call([ + { methodname: 'tool_templatelibrary_list_templates', + args: { component: componentStr, search: searchStr }, + done: reloadListTemplate, + fail: notification.exception } + ]); + }; + + var throttle = null; + + /** + * Call the specified function after a delay. If this function is called again before the function is executed, + * the function will only be executed once. + * + * @method queueRefresh + * @param function callback + * @param int delay The time in milliseconds to delay. + */ + var queueRefresh = function(callback, delay) { + if (throttle !== null) { + window.clearTimeout(throttle); + } + + throttle = window.setTimeout(function() { + callback(); + throttle = null; + }, delay); + }; + + var changeHandler = function() { + queueRefresh(refreshSearch, 400); + }; + // Add change handlers to refresh the list. + $('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler); + $('[data-region="list-templates"]').on('input', '[data-field="search"]', changeHandler); + + refreshSearch(); + return {}; +}); diff --git a/admin/tool/templatelibrary/classes/api.php b/admin/tool/templatelibrary/classes/api.php new file mode 100644 index 00000000000..c818c866317 --- /dev/null +++ b/admin/tool/templatelibrary/classes/api.php @@ -0,0 +1,92 @@ +. + +/** + * Class for listing mustache templates. + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_templatelibrary; + +use stdClass; +use core_component; +use coding_exception; +use required_capability_exception; + +/** + * API exposed by tool_templatelibrary + * + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class api { + + /** + * Return a list of details about installed templates. + * + * @param string $component Filter the list to a single component. + * @param string $search Search string to optionally filter the list of templates. + * @return array[string] Where each template is in the form "component/templatename". + */ + public static function list_templates($component = '', $search = '') { + global $CFG; + + $templatedirs = array(); + $results = array(); + + if ($component != '') { + // Just look at one component for templates. + $dir = core_component::get_component_directory($component); + if (!$dir) { + return $templatedirs; + } + + $templatedirs[$component] = $dir . '/templates'; + } else { + + // Look at all the templates dirs for all installed plugins. + $dir = $CFG->libdir . '/templates'; + if (!empty($dir) && is_dir($dir)) { + $templatedirs['core'] = $dir; + } + $plugintypes = core_component::get_plugin_types(); + foreach ($plugintypes as $type => $dir) { + $plugins = core_component::get_plugin_list_with_file($type, 'templates', false); + foreach ($plugins as $plugin => $dir) { + if (!empty($dir) && is_dir($dir)) { + $templatedirs[$type . '_' . $plugin] = $dir; + } + } + } + } + + foreach ($templatedirs as $templatecomponent => $dir) { + // List it. + $files = glob($dir . '/*.mustache'); + + foreach ($files as $file) { + $templatename = basename($file, '.mustache'); + if ($search == '' || strpos($templatename, $search) !== false) { + $results[] = $templatecomponent . '/' . $templatename; + } + } + } + return $results; + } + +} diff --git a/admin/tool/templatelibrary/classes/external.php b/admin/tool/templatelibrary/classes/external.php new file mode 100644 index 00000000000..630f0400b30 --- /dev/null +++ b/admin/tool/templatelibrary/classes/external.php @@ -0,0 +1,98 @@ +. + +/** + * This is the external API for this tool. + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_templatelibrary; + +require_once("$CFG->libdir/externallib.php"); + +use external_api; +use external_function_parameters; +use external_value; +use external_format_value; +use external_single_structure; +use external_multiple_structure; +use invalid_parameter_exception; + +/** + * This is the external API for this tool. + * + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class external extends external_api { + + /** + * Returns description of list_templates() parameters. + * + * @return external_function_parameters + */ + public static function list_templates_parameters() { + $component = new external_value( + PARAM_COMPONENT, + 'The component to search', + VALUE_DEFAULT, + '' + ); + $search = new external_value( + PARAM_RAW, + 'The search string', + VALUE_DEFAULT, + '' + ); + $params = array('component' => $component, 'search' => $search); + return new external_function_parameters($params); + } + + /** + * Expose to AJAX + * @return boolean + */ + public static function list_templates_is_allowed_from_ajax() { + return true; + } + + /** + * Loads the list of templates. + * @param string $component Limit the search to a component. + * @param string $search The search string. + * @return array[string] + */ + public static function list_templates($component, $search) { + $params = self::validate_parameters(self::list_templates_parameters(), + array( + 'component' => $component, + 'search' => $search, + )); + + return api::list_templates($component, $search); + } + + /** + * Returns description of list_templates() result value. + * + * @return external_description + */ + public static function list_templates_returns() { + return new external_multiple_structure(new external_value(PARAM_RAW, 'The template name (format is component/templatename)')); + } +} diff --git a/admin/tool/templatelibrary/classes/output/list_templates_page.php b/admin/tool/templatelibrary/classes/output/list_templates_page.php new file mode 100644 index 00000000000..8377e8bccb8 --- /dev/null +++ b/admin/tool/templatelibrary/classes/output/list_templates_page.php @@ -0,0 +1,72 @@ +. + +/** + * Class containing data for list_templates page + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_templatelibrary\output; + +use renderable; +use templatable; +use renderer_base; +use stdClass; +use core_plugin_manager; +use tool_templatelibrary\api; + +/** + * Class containing data for list_templates page + * + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class list_templates_page implements renderable, templatable { + + /** + * Export this data so it can be used as the context for a mustache template. + * + * @return stdClass + */ + public function export_for_template(renderer_base $output) { + $data = new stdClass(); + $data->allcomponents = array(); + $fulltemplatenames = api::list_templates(); + $pluginmanager = core_plugin_manager::instance(); + $components = array(); + + foreach ($fulltemplatenames as $templatename) { + list($component, $templatename) = explode('/', $templatename, 2); + $components[$component] = 1; + } + + $components = array_keys($components); + foreach ($components as $component) { + $info = new stdClass(); + $info->component = $component; + if ($component == 'core') { + $info->name = get_string('core_component', 'tool_templatelibrary'); + } else { + $info->name = $pluginmanager->plugin_name($component); + } + $data->allcomponents[] = $info; + } + + return $data; + } +} diff --git a/admin/tool/templatelibrary/classes/output/renderer.php b/admin/tool/templatelibrary/classes/output/renderer.php new file mode 100644 index 00000000000..1fc0735ec2b --- /dev/null +++ b/admin/tool/templatelibrary/classes/output/renderer.php @@ -0,0 +1,51 @@ +. + +/** + * Renderer class for template library. + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_templatelibrary\output; + +defined('MOODLE_INTERNAL') || die; + +use plugin_renderer_base; + +/** + * Renderer class for template library. + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class renderer extends plugin_renderer_base { + + /** + * Defer to template. + * + * @param list_templates_page $page + * + * @return string html for the page + */ + public function render_list_templates_page($page) { + $data = $page->export_for_template($this); + return parent::render_from_template('tool_templatelibrary/list_templates_page', $data); + } + +} diff --git a/admin/tool/templatelibrary/db/services.php b/admin/tool/templatelibrary/db/services.php new file mode 100644 index 00000000000..fdecc01cac5 --- /dev/null +++ b/admin/tool/templatelibrary/db/services.php @@ -0,0 +1,37 @@ +. + +/** + * Template library webservice definitions. + * + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$functions = array( + + 'tool_templatelibrary_list_templates' => array( + 'classname' => 'tool_templatelibrary\external', + 'methodname' => 'list_templates', + 'classpath' => '', + 'description' => 'List/search templates by component.', + 'type' => 'read', + 'capabilities'=> '', + ), +); + diff --git a/admin/tool/templatelibrary/index.php b/admin/tool/templatelibrary/index.php new file mode 100644 index 00000000000..b7703bacebe --- /dev/null +++ b/admin/tool/templatelibrary/index.php @@ -0,0 +1,47 @@ +. + +/** + * This page lets users to manage site wide competencies. + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../../../config.php'); +require_once($CFG->libdir.'/adminlib.php'); + +admin_externalpage_setup('tooltemplatelibrary'); + +$component = optional_param('component', '', PARAM_COMPONENT); +$search = optional_param('search', '', PARAM_RAW); + +$title = get_string('templates', 'tool_templatelibrary'); +$pagetitle = get_string('searchtemplates', 'tool_templatelibrary'); +// Set up the page. +$url = new moodle_url("/admin/tool/templatelibrary/index.php", array('component' => $component, 'search' => $search)); +$PAGE->set_url($url); +$PAGE->set_title($title); +$PAGE->set_heading($title); +$output = $PAGE->get_renderer('tool_templatelibrary'); +echo $output->header(); +echo $output->heading($pagetitle); + +$page = new \tool_templatelibrary\output\list_templates_page($component, $search); +echo $output->render($page); + +echo $output->footer(); diff --git a/admin/tool/templatelibrary/lang/en/tool_templatelibrary.php b/admin/tool/templatelibrary/lang/en/tool_templatelibrary.php new file mode 100644 index 00000000000..87b8ea0303c --- /dev/null +++ b/admin/tool/templatelibrary/lang/en/tool_templatelibrary.php @@ -0,0 +1,37 @@ +. + +/** + * Strings for component 'tool_templatelibrary', language 'en' + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['all'] = 'All components'; +$string['component'] = 'Component'; +$string['core_component'] = 'Moodle core'; +$string['documentation'] = 'Documentation'; +$string['example'] = 'Example'; +$string['noresults'] = 'No results'; +$string['notemplateselected'] = 'No template selected'; +$string['pluginname'] = 'Template library'; +$string['search'] = 'Search'; +$string['searchtemplates'] = 'Search templates'; +$string['templatehasnoexample'] = 'This template has no example context, so it cannot be rendered here. To add an example context to this template, insert in a Mustache comment "Example context (json):", followed by the json encoded sample context for the template.'; +$string['templates'] = 'Templates'; +$string['templateselected'] = 'Template: {$a}'; diff --git a/admin/tool/templatelibrary/settings.php b/admin/tool/templatelibrary/settings.php new file mode 100644 index 00000000000..1b3f8828c0b --- /dev/null +++ b/admin/tool/templatelibrary/settings.php @@ -0,0 +1,33 @@ +. + +/** + * Links and settings + * + * This file contains links and settings used by tool_templatelibrary + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die; +// Manage competency frameworks page. +$temp = new admin_externalpage( + 'tooltemplatelibrary', + get_string('pluginname', 'tool_templatelibrary'), + new moodle_url('/admin/tool/templatelibrary/index.php') +); +$ADMIN->add('development', $temp); diff --git a/admin/tool/templatelibrary/styles.css b/admin/tool/templatelibrary/styles.css new file mode 100644 index 00000000000..5dad93604b6 --- /dev/null +++ b/admin/tool/templatelibrary/styles.css @@ -0,0 +1,5 @@ +[data-region="displaytemplateexample"] { + border-radius: 4px; + border: 1px inset #e3e3e3; + padding: 1em; +} diff --git a/admin/tool/templatelibrary/templates/display_template.mustache b/admin/tool/templatelibrary/templates/display_template.mustache new file mode 100644 index 00000000000..75a9469330a --- /dev/null +++ b/admin/tool/templatelibrary/templates/display_template.mustache @@ -0,0 +1,48 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + Moodle template to display another template. + + The purpose of this template is to put scafolding in the page, so the javascript can fetch templates and + insert them into this part of the page. + + Classes required for JS: + * none + + Data attributes required for JS: + * data-region + + Context variables required for this template: + * none + + Example context (json): + { } + +}} +
+

{{#str}}notemplateselected, tool_templatelibrary{{/str}}

+
+

{{#str}}example, tool_templatelibrary{{/str}}

+
+ - +
+
+
+

{{#str}}documentation, tool_templatelibrary{{/str}}

+
 - 
+
+
diff --git a/admin/tool/templatelibrary/templates/list_templates_page.mustache b/admin/tool/templatelibrary/templates/list_templates_page.mustache new file mode 100644 index 00000000000..5584bb45f82 --- /dev/null +++ b/admin/tool/templatelibrary/templates/list_templates_page.mustache @@ -0,0 +1,61 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + Moodle template to the template library + + The purpose of this template is build the entire page for the template library (by including smaller templates). + + Classes required for JS: + * none + + Data attributes required for JS: + * data-region, data-field + + Context variables required for this template: + * allcomponents - array of components containing templates. Each component has a name and a component attribute. + +}} +
+ +
+ {{> tool_templatelibrary/search_results }} + +
+ {{> tool_templatelibrary/display_template }} + +
+{{#js}} + require(['tool_templatelibrary/search', 'tool_templatelibrary/display']); +{{/js}} diff --git a/admin/tool/templatelibrary/templates/search_results.mustache b/admin/tool/templatelibrary/templates/search_results.mustache new file mode 100644 index 00000000000..1710cfd1420 --- /dev/null +++ b/admin/tool/templatelibrary/templates/search_results.mustache @@ -0,0 +1,44 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + Moodle template to display results of template search. + + This template gets rendered by javascript when it has searched for templates. + + Classes required for JS: + * none + + Data attributes required for JS: + * data-region, data-templatename + + Context variables required for this template: + * templates - And array of template names. + + Example context (json): + { "templates" : [ "core/pix_icon", "tool_templatelibrary/display_template" ] } + +}} +
+{{^templates}} +

{{#str}}noresults, tool_templatelibrary{{/str}}

+{{/templates}} +
    +{{#templates}} +
  • {{.}}
  • +{{/templates}} +
+
diff --git a/admin/tool/templatelibrary/tests/externallib_test.php b/admin/tool/templatelibrary/tests/externallib_test.php new file mode 100644 index 00000000000..db9d4df59bb --- /dev/null +++ b/admin/tool/templatelibrary/tests/externallib_test.php @@ -0,0 +1,69 @@ +. + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); + +use tool_templatelibrary\external; + +/** + * External learning plans webservice API tests. + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_templatelibrary_external_testcase extends externallib_advanced_testcase { + + /** + * Test list all. + */ + public function test_list_templates() { + $result = external::list_templates('', ''); + $count = count($result); + // We have 3 templates in this tool - and there must be more else where. + $this->assertGreaterThan(3, $count); + } + + /** + * Test we can filter by component. + */ + public function test_list_templates_for_component() { + $result = external::list_templates('tool_templatelibrary', ''); + $count = count($result); + $this->assertEquals(3, $count); + + $this->assertContains("tool_templatelibrary/display_template", $result); + $this->assertContains("tool_templatelibrary/search_results", $result); + $this->assertContains("tool_templatelibrary/list_templates_page", $result); + } + + /** + * Test we can filter by a string. + */ + public function test_list_templates_with_filter() { + $result = external::list_templates('tool_templatelibrary', 'page'); + $count = count($result); + // Should be only one matching template. + $this->assertEquals(1, $count); + $this->assertEquals($result[0], "tool_templatelibrary/list_templates_page"); + } + + +} diff --git a/admin/tool/templatelibrary/version.php b/admin/tool/templatelibrary/version.php new file mode 100644 index 00000000000..085d3477608 --- /dev/null +++ b/admin/tool/templatelibrary/version.php @@ -0,0 +1,26 @@ +. +/** + * Plugin version info + * + * @package tool_templatelibrary + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); +$plugin->version = 2015021623; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2014110400; // Requires this Moodle version. +$plugin->component = 'tool_templatelibrary'; // Full name of the plugin (used for diagnostics). diff --git a/lib/classes/plugin_manager.php b/lib/classes/plugin_manager.php index 040b39cf8d4..99d17f4dd6e 100644 --- a/lib/classes/plugin_manager.php +++ b/lib/classes/plugin_manager.php @@ -1143,7 +1143,7 @@ class core_plugin_manager { 'assignmentupgrade', 'availabilityconditions', 'behat', 'capability', 'customlang', 'dbtransfer', 'filetypes', 'generator', 'health', 'innodb', 'installaddon', 'langimport', 'log', 'messageinbound', 'multilangupgrade', 'monitor', 'phpunit', 'profiling', - 'replace', 'spamcleaner', 'task', + 'replace', 'spamcleaner', 'task', 'templatelibrary', 'unittest', 'uploadcourse', 'uploaduser', 'unsuproles', 'xmldb' ), diff --git a/lib/templates/pix_icon.mustache b/lib/templates/pix_icon.mustache index 8b7710469d4..3b62be79c85 100644 --- a/lib/templates/pix_icon.mustache +++ b/lib/templates/pix_icon.mustache @@ -27,5 +27,14 @@ Context variables required for this template: * attributes Array of name / value pairs. + + Example context (json): + { + "attributes": [ + { "name": "src", "value": "http://moodle.com/wp-content/themes/moodle/images/logo-hat2.png" }, + { "name": "class", "value": "iconsmall" } + ] + } + }}