first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
/**
* This module adds ajax display functions to the template library page.
*
* @module tool_templatelibrary/display
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("tool_templatelibrary/display",["jquery","core/ajax","core/log","core/notification","core/templates","core/config","core/str"],(function($,ajax,log,notification,templates,config,str){var findDocsSection=function(templateSource,templateName){if(!templateSource)return!1;var sections,marker="@template "+templateName,i=0;if(null!==(sections=templateSource.match(/{{!([\s\S]*?)}}/g)))for(i=0;i<sections.length;i++){var section=sections[i],start=section.indexOf(marker);if(-1!==start){var offset=start+marker.length+1;return section=section.substr(offset,section.length-2-offset)}}return!1},loadTemplate=function(templateName){var parts=templateName.split("/"),component=parts.shift(),name=parts.join("/"),promises=ajax.call([{methodname:"core_output_load_template",args:{component:component,template:name,themename:config.theme,includecomments:!0}},{methodname:"tool_templatelibrary_load_canonical_template",args:{component:component,template:name}}],!0,!1);$.when.apply($,promises).done((function(source,originalSource){!function(templateName,source,originalSource){str.get_string("templateselected","tool_templatelibrary",templateName).done((function(s){$('[data-region="displaytemplateheader"]').text(s)})).fail(notification.exception);var docs=findDocsSection(source,templateName);!1===docs&&(docs=findDocsSection(originalSource,templateName)),docs&&(source=docs),$('[data-region="displaytemplatesource"]').text(source);var example=source.match(/Example context \(json\):([\s\S]*)/),context=!1;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)}}context?templates.render(templateName,context).done((function(html,js){templates.replaceNodeContents($('[data-region="displaytemplateexample"]'),html,js)})).fail(notification.exception):str.get_string("templatehasnoexample","tool_templatelibrary").done((function(s){$('[data-region="displaytemplateexample"]').text(s)})).fail(notification.exception)}(templateName,source,originalSource)})).fail(notification.exception)};return $('[data-region="list-templates"]').on("click","[data-templatename]",(function(e){var templatename=$(this).data("templatename");e.preventDefault(),loadTemplate(templatename)})),{}}));
//# sourceMappingURL=display.min.js.map
File diff suppressed because one or more lines are too long
+10
View File
@@ -0,0 +1,10 @@
/**
* This module adds ajax search functions to the template library page.
*
* @module tool_templatelibrary/search
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("tool_templatelibrary/search",["jquery","core/ajax","core/log","core/notification","core/templates","core/config"],(function($,ajax,log,notification,templates,config){var reloadListTemplate=function(templateList){templates.render("tool_templatelibrary/search_results",{templates:templateList}).done((function(result,js){templates.replaceNode($('[data-region="searchresults"]'),result,js)})).fail(notification.exception)},refreshSearch=function(themename){var componentStr=$('[data-field="component"]').val(),searchStr=$('[data-region="list-templates"] [data-region="input"]').val();""!==searchStr?$('[data-region="list-templates"] [data-action="clearsearch"]').removeClass("d-none"):$('[data-region="list-templates"] [data-action="clearsearch"]').addClass("d-none"),ajax.call([{methodname:"tool_templatelibrary_list_templates",args:{component:componentStr,search:searchStr,themename:themename},done:reloadListTemplate,fail:notification.exception}],!0,!1)},throttle=null,changeHandler=function(){var callback,delay;callback=refreshSearch.bind(this,config.theme),delay=400,null!==throttle&&window.clearTimeout(throttle),throttle=window.setTimeout((function(){callback(),throttle=null}),delay)};return $('[data-region="list-templates"]').on("change",'[data-field="component"]',changeHandler),$('[data-region="list-templates"]').on("input",'[data-region="input"]',changeHandler),$('[data-action="clearsearch"]').on("click",(function(){$('[data-region="input"]').val(""),refreshSearch(config.theme),$(this).addClass("d-none")})),refreshSearch(config.theme),{}}));
//# sourceMappingURL=search.min.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,157 @@
// 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 <http://www.gnu.org/licenses/>.
/**
* This module adds ajax display functions to the template library page.
*
* @module tool_templatelibrary/display
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @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) {
/**
* Search through a template for a template docs comment.
*
* @param {String} templateSource The raw template
* @param {String} templateName The name of the template used to search for docs tag
* @return {String|boolean} the correct comment or false
*/
var findDocsSection = function(templateSource, templateName) {
if (!templateSource) {
return false;
}
// Find the comment section marked with @template component/template.
var marker = "@template " + templateName,
i = 0,
sections = [];
sections = templateSource.match(/{{!([\s\S]*?)}}/g);
// If no sections match - show the entire file.
if (sections !== null) {
for (i = 0; i < sections.length; i++) {
var section = sections[i];
var start = section.indexOf(marker);
if (start !== -1) {
// Remove {{! and }} from start and end.
var offset = start + marker.length + 1;
section = section.substr(offset, section.length - 2 - offset);
return section;
}
}
}
// No matching comment.
return false;
};
/**
* Handle a template loaded response.
*
* @param {String} templateName The template name
* @param {String} source The template source
* @param {String} originalSource The original template source (not theme overridden)
*/
var templateLoaded = function(templateName, source, originalSource) {
str.get_string('templateselected', 'tool_templatelibrary', templateName).done(function(s) {
$('[data-region="displaytemplateheader"]').text(s);
}).fail(notification.exception);
// Find the comment section marked with @template component/template.
var docs = findDocsSection(source, templateName);
if (docs === false) {
// Docs was not in theme template, try original.
docs = findDocsSection(originalSource, templateName);
}
// If we found a docs section, limit the template library to showing this section.
if (docs) {
source = docs;
}
$('[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) {
templates.replaceNodeContents($('[data-region="displaytemplateexample"]'), html, 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.join('/');
var promises = ajax.call([{
methodname: 'core_output_load_template',
args: {
component: component,
template: name,
themename: config.theme,
includecomments: true
}
}, {
methodname: 'tool_templatelibrary_load_canonical_template',
args: {
component: component,
template: name
}
}], true, false);
// When returns a new promise that is resolved when all the passed in promises are resolved.
// The arguments to the done become the values of each resolved promise.
$.when.apply($, promises)
.done(function(source, originalSource) {
templateLoaded(templateName, source, originalSource);
})
.fail(notification.exception);
};
// Add the event listeners.
$('[data-region="list-templates"]').on('click', '[data-templatename]', function(e) {
var templatename = $(this).data('templatename');
e.preventDefault();
loadTemplate(templatename);
});
// This module does not expose anything.
return {};
});
@@ -0,0 +1,99 @@
// 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 <http://www.gnu.org/licenses/>.
/**
* This module adds ajax search functions to the template library page.
*
* @module tool_templatelibrary/search
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @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'],
function($, ajax, log, notification, templates, config) {
/**
* The ajax call has returned with a new list of templates.
*
* @method reloadListTemplate
* @param {String[]} templateList List of template ids.
*/
var reloadListTemplate = function(templateList) {
templates.render('tool_templatelibrary/search_results', {templates: templateList})
.done(function(result, js) {
templates.replaceNode($('[data-region="searchresults"]'), result, js);
}).fail(notification.exception);
};
/**
* Get the current values for the form inputs and refresh the list of matching templates.
*
* @method refreshSearch
* @param {String} themename The naeme of the theme.
*/
var refreshSearch = function(themename) {
var componentStr = $('[data-field="component"]').val();
var searchStr = $('[data-region="list-templates"] [data-region="input"]').val();
if (searchStr !== '') {
$('[data-region="list-templates"] [data-action="clearsearch"]').removeClass('d-none');
} else {
$('[data-region="list-templates"] [data-action="clearsearch"]').addClass('d-none');
}
// Trigger the search.
ajax.call([
{methodname: 'tool_templatelibrary_list_templates',
args: {component: componentStr, search: searchStr, themename: themename},
done: reloadListTemplate,
fail: notification.exception}
], true, false);
};
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 {Number} 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.bind(this, config.theme), 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-region="input"]', changeHandler);
$('[data-action="clearsearch"]').on('click', function() {
$('[data-region="input"]').val('');
refreshSearch(config.theme);
$(this).addClass('d-none');
});
refreshSearch(config.theme);
return {};
});
+176
View File
@@ -0,0 +1,176 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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 core_component;
use core\output\mustache_template_finder;
use coding_exception;
use moodle_exception;
use required_capability_exception;
use stdClass;
/**
* 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.
* @param string $themename The name of the current theme.
* @return array[string] Where each template is in the form "component/templatename".
*/
public static function list_templates($component = '', $search = '', $themename = '') {
global $CFG, $PAGE;
if (empty($themename)) {
$themename = $PAGE->theme->name;
}
$themeconfig = \theme_config::load($themename);
$templatedirs = array();
$results = array();
if ($component !== '') {
// Just look at one component for templates.
$dirs = mustache_template_finder::get_template_directories_for_component($component, $themename);
$templatedirs[$component] = $dirs;
} else {
// Look at all the templates dirs for core.
$templatedirs['core'] = mustache_template_finder::get_template_directories_for_component('core', $themename);
// Look at all the templates dirs for subsystems.
$subsystems = core_component::get_core_subsystems();
foreach ($subsystems as $subsystem => $dir) {
if (empty($dir)) {
continue;
}
$dir .= '/templates';
if (is_dir($dir)) {
$dirs = mustache_template_finder::get_template_directories_for_component('core_' . $subsystem, $themename);
$templatedirs['core_' . $subsystem] = $dirs;
}
}
// Look at all the templates dirs for plugins.
$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 ($type == 'theme' && $plugin != $themename && !in_array($plugin, $themeconfig->parents)) {
continue;
}
if (!empty($dir) && is_dir($dir)) {
$pluginname = $type . '_' . $plugin;
$dirs = mustache_template_finder::get_template_directories_for_component($pluginname, $themename);
$templatedirs[$pluginname] = $dirs;
}
}
}
}
foreach ($templatedirs as $templatecomponent => $dirs) {
foreach ($dirs as $dir) {
if (!is_dir($dir) || !is_readable($dir)) {
continue;
}
$dir = realpath($dir);
// List it.
$directory = new \RecursiveDirectoryIterator($dir);
$files = new \RecursiveIteratorIterator($directory);
foreach ($files as $file) {
if (!$file->isFile()) {
continue;
}
$filename = substr($file->getRealpath(), strlen($dir) + 1);
if (strpos($templatecomponent, 'theme_') === 0) {
if (strpos($filename, '/') !== false && strpos($filename, 'local/') !== 0) {
// Skip any template in a sub-directory of a theme which is not in a local directory.
// These are theme overrides of core templates.
// Note: There is a rare edge case where a theme may override a template and then have additional
// dependant templates and these will not be shown.
continue;
}
}
$templatename = str_replace('.mustache', '', $filename);
$componenttemplatename = "{$templatecomponent}/{$templatename}";
if ($search == '' || strpos($componenttemplatename, $search) !== false) {
$results[$componenttemplatename] = 1;
}
}
}
}
$results = array_keys($results);
sort($results);
return $results;
}
/**
* Return a mustache template.
* Note - this function differs from the function core_output_load_template
* because it will never return a theme overridden version of a template.
*
* @param string $component The component that holds the template.
* @param string $template The name of the template.
* @return string the template or false if template doesn't exist.
*/
public static function load_canonical_template($component, $template) {
// Get the list of possible template directories.
$dirs = mustache_template_finder::get_template_directories_for_component($component);
$filename = false;
$themedir = core_component::get_plugin_types()['theme'];
foreach ($dirs as $dir) {
// Skip theme dirs - we only want the original plugin/core template.
if (strpos($dir, $themedir) === 0) {
continue;
}
$candidate = $dir . $template . '.mustache';
if (file_exists($candidate)) {
$filename = $candidate;
break;
}
}
if ($filename === false) {
// There are occasions where we don't have a core template.
return false;
}
$templatestr = file_get_contents($filename);
return $templatestr;
}
}
@@ -0,0 +1,127 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
namespace tool_templatelibrary;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_value;
/**
* 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,
''
);
$themename = new external_value(
PARAM_COMPONENT,
'The current theme',
VALUE_DEFAULT,
''
);
$params = array('component' => $component, 'search' => $search, 'themename' => $themename);
return new external_function_parameters($params);
}
/**
* Loads the list of templates.
* @param string $component Limit the search to a component.
* @param string $search The search string.
* @param string $themename The name of theme
* @return array[string]
*/
public static function list_templates($component, $search, $themename = '') {
$params = self::validate_parameters(self::list_templates_parameters(),
array(
'component' => $component,
'search' => $search,
'themename' => $themename,
));
return api::list_templates($component, $search, $themename);
}
/**
* Returns description of list_templates() result value.
*
* @return \core_external\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)'));
}
/**
* Returns description of load_canonical_template() parameters.
*
* @return external_function_parameters
*/
public static function load_canonical_template_parameters() {
return new external_function_parameters(
array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
'template' => new external_value(PARAM_SAFEPATH, 'name of the template'))
);
}
/**
* Return a mustache template.
* Note - this function differs from the function core_output_load_template
* because it will never return a theme overridden version of a template.
*
* @param string $component The component that holds the template.
* @param string $template The name of the template.
* @return string the template, false if template doesn't exist.
*/
public static function load_canonical_template($component, $template) {
$params = self::validate_parameters(self::load_canonical_template_parameters(),
array('component' => $component,
'template' => $template));
$component = $params['component'];
$template = $params['template'];
return api::load_canonical_template($component, $template);
}
/**
* Returns description of load_canonical_template() result value.
*
* @return \core_external\external_description
*/
public static function load_canonical_template_returns() {
return new external_value(PARAM_RAW, 'template');
}
}
@@ -0,0 +1,110 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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_collator;
use core_component;
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 {
/** @var string $component The currently selected component */
protected $component;
/** @var string $search The current search */
protected $search;
/**
* Template page constructor
*
* @param string $component
* @param string $search
*/
public function __construct(string $component = '', string $search = '') {
$this->component = $component;
$this->search = $search;
}
/**
* 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) {
$fulltemplatenames = api::list_templates();
$pluginmanager = core_plugin_manager::instance();
$components = [];
foreach ($fulltemplatenames as $templatename) {
[$component, ] = explode('/', $templatename, 2);
[$type, ] = core_component::normalize_component($component);
// Core sub-systems are grouped together and are denoted by a distinct lang string.
$coresubsystem = (strpos($component, 'core') === 0);
if (!array_key_exists($type, $components)) {
$typename = $coresubsystem
? get_string('core', 'tool_templatelibrary')
: $pluginmanager->plugintype_name_plural($type);
$components[$type] = (object) [
'type' => $typename,
'plugins' => [],
];
}
$pluginname = $coresubsystem
? get_string('coresubsystem', 'tool_templatelibrary', $component)
: $pluginmanager->plugin_name($component);
$components[$type]->plugins[$component] = (object) [
'name' => $pluginname,
'component' => $component,
'selected' => ($component === $this->component),
];
}
// Sort returned components according to their type, followed by name.
core_collator::asort_objects_by_property($components, 'type');
array_walk($components, function(stdClass $component) {
core_collator::asort_objects_by_property($component->plugins, 'name');
$component->plugins = array_values($component->plugins);
});
return (object) [
'allcomponents' => array_values($components),
'search' => $this->search,
];
}
}
@@ -0,0 +1,51 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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);
}
}
@@ -0,0 +1,46 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for tool_templatelibrary.
*
* @package tool_templatelibrary
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_templatelibrary\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for tool_templatelibrary implementing null_provider.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
@@ -0,0 +1,48 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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'=> '',
'ajax' => true,
'loginrequired' => false,
),
'tool_templatelibrary_load_canonical_template' => array(
'classname' => 'tool_templatelibrary\external',
'methodname' => 'load_canonical_template',
'description' => 'Load a canonical template by name (not the theme overidden one).',
'type' => 'read',
'ajax' => true,
'loginrequired' => false,
),
);
+47
View File
@@ -0,0 +1,47 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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();
@@ -0,0 +1,39 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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'] = 'Core';
$string['coresubsystem'] = 'Subsystem ({$a})';
$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}';
$string['privacy:metadata'] = 'The Template library plugin does not store any personal data.';
+33
View File
@@ -0,0 +1,33 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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;
// Template library page.
$temp = new admin_externalpage(
'tooltemplatelibrary',
get_string('pluginname', 'tool_templatelibrary'),
new moodle_url('/admin/tool/templatelibrary/index.php')
);
$ADMIN->add('development', $temp);
+5
View File
@@ -0,0 +1,5 @@
[data-region="displaytemplateexample"] {
border-radius: 4px;
border: 1px inset #e3e3e3;
padding: 1em;
}
@@ -0,0 +1,50 @@
{{!
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 <http://www.gnu.org/licenses/>.
}}
{{!
@template tool_templatelibrary/display_template
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):
{ }
}}
<div data-region="displaytemplate">
<h3 data-region="displaytemplateheader">{{#str}}notemplateselected, tool_templatelibrary{{/str}}</h3>
<div>
<h4>{{#str}}example, tool_templatelibrary{{/str}}</h4>
<div data-region="displaytemplateexample">
-
</div>
</div>
<div>
<h4>{{#str}}documentation, tool_templatelibrary{{/str}}</h4>
<pre data-region="displaytemplatesource"> - </pre>
</div>
</div>
@@ -0,0 +1,108 @@
{{!
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 <http://www.gnu.org/licenses/>.
}}
{{!
@template tool_templatelibrary/list_templates_page
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.
}}
{{!
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 <http://www.gnu.org/licenses/>.
}}
{{!
@template tool_templatelibrary/list_templates_page
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.
}}
<div data-region="list-templates">
<form class="form-horizontal">
{{< core_form/element-template }}
{{$label}}
<div class="col-form-label">{{#str}}component, tool_templatelibrary{{/str}}</div>
{{/label}}
{{$element}}
<select id="selectcomponent" name="component" class="form-control" data-field="component">
<option value="">{{#str}}all, tool_templatelibrary{{/str}}</option>
{{#allcomponents}}
<optgroup label="{{type}}">
{{#plugins}}
<option value="{{component}}" {{#selected}}selected{{/selected}}>{{name}}</option>
{{/plugins}}
</optgroup>
{{/allcomponents}}
</select>
{{/element}}
{{/ core_form/element-template }}
{{< core_form/element-template }}
{{$element}}
{{< core/search_input_auto }}
{{$label}}
{{#str}} search, tool_templatelibrary {{/str}}
{{/label}}
{{$value}}{{ search }}{{/value}}
{{/ core/search_input_auto }}
{{/element}}
{{/ core_form/element-template }}
</form>
<hr/>
{{> tool_templatelibrary/search_results }}
<hr/>
{{> tool_templatelibrary/display_template }}
</div>
{{#js}}
require(['tool_templatelibrary/search', 'tool_templatelibrary/display']);
{{/js}}
@@ -0,0 +1,46 @@
{{!
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 <http://www.gnu.org/licenses/>.
}}
{{!
@template tool_templatelibrary/search_results
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" ] }
}}
<div data-region="searchresults" aria-live="off" class="no-overflow" style="max-height: 10em">
{{^templates}}
<p class="text-warning">{{#str}}noresults, tool_templatelibrary{{/str}}</p>
{{/templates}}
<ul>
{{#templates}}
<li data-templatename="{{.}}"><a href="#">{{.}}</a></li>
{{/templates}}
</ul>
</div>
@@ -0,0 +1,85 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
namespace tool_templatelibrary;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* 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 externallib_test extends externallib_advanced_testcase {
/**
* Test list all.
*/
public function test_list_templates(): void {
$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(): void {
$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(): void {
$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");
}
public function test_load_canonical_template(): void {
global $CFG;
$originaltheme = $CFG->theme;
// Change the theme to 'base' because it overrides these templates.
$CFG->theme = 'boost';
$template = external::load_canonical_template('core', 'notification_error');
// Only the base template should contain the docs.
$this->assertStringContainsString('@template core/notification_error', $template);
// Restore the original theme.
$CFG->theme = $originaltheme;
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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 = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'tool_templatelibrary'; // Full name of the plugin (used for diagnostics).