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
+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';
}
}