Merge branch 'MDL-82183-main-squashed' of https://github.com/andrewnicols/moodle
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
issueNumber: MDL-82183
|
||||
notes:
|
||||
core:
|
||||
- message: >
|
||||
The following classes are now also available in the following new
|
||||
locations. They will continue to work in their old locations:
|
||||
|
||||
| Old classname | New classname |
|
||||
|
||||
| --- | --- |
|
||||
|
||||
| `\action_link` | `\core\output\action_link` |
|
||||
|
||||
| `\action_menu_filler` | `\core\output\action_menu\filler` |
|
||||
|
||||
| `\action_menu_link_primary` | `\core\output\action_menu\link_primary` |
|
||||
|
||||
| `\action_menu_link_secondary` | `\core\output\action_menu\link_secondary` |
|
||||
|
||||
| `\action_menu_link` | `\core\output\action_menu\link` |
|
||||
|
||||
| `\action_menu` | `\core\output\action_menu` |
|
||||
|
||||
| `\block_contents` | `\core_block\output\block_contents` |
|
||||
|
||||
| `\block_move_target` | `\core_block\output\block_move_target` |
|
||||
|
||||
| `\component_action` | `\core\output\actions\component_action` |
|
||||
|
||||
| `\confirm_action` | `\core\output\actions\confirm_action` |
|
||||
|
||||
| `\context_header` | `\core\output\context_header` |
|
||||
|
||||
| `\core_renderer_ajax` | `\core\output\core_renderer_ajax` |
|
||||
|
||||
| `\core_renderer_cli` | `\core\output\core_renderer_cli` |
|
||||
|
||||
| `\core_renderer_maintenance` | `\core\output\core_renderer_maintenance` |
|
||||
|
||||
| `\core_renderer` | `\core\output\core_renderer` |
|
||||
|
||||
| `\custom_menu_item` | `\core\output\custom_menu_item` |
|
||||
|
||||
| `\custom_menu` | `\core\output\custom_menu` |
|
||||
|
||||
| `\file_picker` | `\core\output\file_picker` |
|
||||
|
||||
| `\flexible_table` | `\core_table\flexible_table` |
|
||||
|
||||
| `\fragment_requirements_manager` | `\core\output\requirements\fragment_requirements_manager` |
|
||||
|
||||
| `\help_icon` | `\core\output\help_icon` |
|
||||
|
||||
| `\html_table_cell` | `\core_table\output\html_table_cell` |
|
||||
|
||||
| `\html_table_row` | `\core_table\output\html_table_row` |
|
||||
|
||||
| `\html_table` | `\core_table\output\html_table` |
|
||||
|
||||
| `\html_writer` | `\core\output\html_writer` |
|
||||
|
||||
| `\image_icon` | `\core\output\image_icon` |
|
||||
|
||||
| `\initials_bar` | `\core\output\initials_bar` |
|
||||
|
||||
| `\js_writer` | `\core\output\js_writer` |
|
||||
|
||||
| `\page_requirements_manager` | `\core\output\requirements\page_requirements_manager` |
|
||||
|
||||
| `\paging_bar` | `\core\output\paging_bar` |
|
||||
|
||||
| `\pix_emoticon` | `\core\output\pix_emoticon` |
|
||||
|
||||
| `\pix_icon_font` | `\core\output\pix_icon_font` |
|
||||
|
||||
| `\pix_icon_fontawesome` | `\core\output\pix_icon_fontawesome` |
|
||||
|
||||
| `\pix_icon` | `\core\output\pix_icon` |
|
||||
|
||||
| `\plugin_renderer_base` | `\core\output\plugin_renderer_base` |
|
||||
|
||||
| `\popup_action` | `\core\output\actions\popup_action` |
|
||||
|
||||
| `\preferences_group` | `\core\output\preferences_group` |
|
||||
|
||||
| `\preferences_groups` | `\core\output\preferences_groups` |
|
||||
|
||||
| `\progress_bar` | `\core\output\progress_bar` |
|
||||
|
||||
| `\renderable` | `\core\output\renderable` |
|
||||
|
||||
| `\renderer_base` | `\core\output\renderer_base` |
|
||||
|
||||
| `\renderer_factory_base` | `\core\output\renderer_factory\renderer_factory_base` |
|
||||
|
||||
| `\renderer_factory` | `\core\output\renderer_factory\renderer_factory_interface` |
|
||||
|
||||
| `\single_button` | `\core\output\single_button` |
|
||||
|
||||
| `\single_select` | `\core\output\single_select` |
|
||||
|
||||
| `\standard_renderer_factory` | `\core\output\renderer_factory\standard_renderer_factory` |
|
||||
|
||||
| `\table_dataformat_export_format` | `\core_table\dataformat_export_format` |
|
||||
|
||||
| `\table_default_export_format_parent` | `\core_table\base_export_format` |
|
||||
|
||||
| `\table_sql` | `\core_table\sql_table` |
|
||||
|
||||
| `\tabobject` | `\core\output\tabobject` |
|
||||
|
||||
| `\tabtree` | `\core\output\tabtree` |
|
||||
|
||||
| `\templatable` | `\core\output\templatable` |
|
||||
|
||||
| `\theme_config` | `\core\output\theme_config` |
|
||||
|
||||
| `\theme_overridden_renderer_factory` | `\core\output\renderer_factory\theme_overridden_renderer_factory` |
|
||||
|
||||
| `\url_select` | `\core\output\url_select` |
|
||||
|
||||
| `\user_picture` | `\core\output\user_picture` |
|
||||
|
||||
| `\xhtml_container_stack` | `\core\output\xhtml_container_stack` |
|
||||
|
||||
| `\YUI_config` | `\core\output\requirements\yui` |
|
||||
|
||||
type: improved
|
||||
@@ -0,0 +1,168 @@
|
||||
<?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 core_block\output;
|
||||
|
||||
/**
|
||||
* This class represents how a block appears on a page.
|
||||
*
|
||||
* During output, each block instance is asked to return a block_contents object,
|
||||
* those are then passed to the $OUTPUT->block function for display.
|
||||
*
|
||||
* contents should probably be generated using a moodle_block_..._renderer.
|
||||
*
|
||||
* Other block-like things that need to appear on the page, for example the
|
||||
* add new block UI, are also represented as block_contents objects.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core_block
|
||||
* @category output
|
||||
*/
|
||||
class block_contents {
|
||||
/** Used when the block cannot be collapsed **/
|
||||
const NOT_HIDEABLE = 0;
|
||||
|
||||
/** Used when the block can be collapsed but currently is not **/
|
||||
const VISIBLE = 1;
|
||||
|
||||
/** Used when the block has been collapsed **/
|
||||
const HIDDEN = 2;
|
||||
|
||||
/**
|
||||
* @var int Used to set $skipid.
|
||||
*/
|
||||
protected static $idcounter = 1;
|
||||
|
||||
/**
|
||||
* @var int All the blocks (or things that look like blocks) printed on
|
||||
* a page are given a unique number that can be used to construct id="" attributes.
|
||||
* This is set automatically be the {@see prepare()} method.
|
||||
* Do not try to set it manually.
|
||||
*/
|
||||
public $skipid;
|
||||
|
||||
/**
|
||||
* @var int If this is the contents of a real block, this should be set
|
||||
* to the block_instance.id. Otherwise this should be set to 0.
|
||||
*/
|
||||
public $blockinstanceid = 0;
|
||||
|
||||
/**
|
||||
* @var int If this is a real block instance, and there is a corresponding
|
||||
* block_position.id for the block on this page, this should be set to that id.
|
||||
* Otherwise it should be 0.
|
||||
*/
|
||||
public $blockpositionid = 0;
|
||||
|
||||
/**
|
||||
* @var array An array of attribute => value pairs that are put on the outer div of this
|
||||
* block. {@see $id} and {@see $classes} attributes should be set separately.
|
||||
*/
|
||||
public $attributes;
|
||||
|
||||
/**
|
||||
* @var string The title of this block. If this came from user input, it should already
|
||||
* have had format_string() processing done on it. This will be output inside
|
||||
* <h2> tags. Please do not cause invalid XHTML.
|
||||
*/
|
||||
public $title = '';
|
||||
|
||||
/**
|
||||
* @var string The label to use when the block does not, or will not have a visible title.
|
||||
* You should never set this as well as title... it will just be ignored.
|
||||
*/
|
||||
public $arialabel = '';
|
||||
|
||||
/**
|
||||
* @var string HTML for the content
|
||||
*/
|
||||
public $content = '';
|
||||
|
||||
/**
|
||||
* @var array An alternative to $content, it you want a list of things with optional icons.
|
||||
*/
|
||||
public $footer = '';
|
||||
|
||||
/**
|
||||
* @var string Any small print that should appear under the block to explain
|
||||
* to the teacher about the block, for example 'This is a sticky block that was
|
||||
* added in the system context.'
|
||||
*/
|
||||
public $annotation = '';
|
||||
|
||||
/**
|
||||
* @var int One of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
|
||||
* the user can toggle whether this block is visible.
|
||||
*/
|
||||
public $collapsible = self::NOT_HIDEABLE;
|
||||
|
||||
/**
|
||||
* Set this to true if the block is dockable.
|
||||
* @var bool
|
||||
*/
|
||||
public $dockable = false;
|
||||
|
||||
/**
|
||||
* @var array A (possibly empty) array of editing controls. Each element of
|
||||
* this array should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
|
||||
* $icon is the icon name. Fed to $OUTPUT->image_url.
|
||||
*/
|
||||
public $controls = [];
|
||||
|
||||
|
||||
/**
|
||||
* Create new instance of block content.
|
||||
*
|
||||
* @param null|array $attributes
|
||||
*/
|
||||
public function __construct(?array $attributes = null) {
|
||||
$this->skipid = self::$idcounter;
|
||||
self::$idcounter += 1;
|
||||
|
||||
if ($attributes) {
|
||||
// Standard block.
|
||||
$this->attributes = $attributes;
|
||||
} else {
|
||||
// Simple "fake" blocks used in some modules and "Add new block" block.
|
||||
$this->attributes = ['class' => 'block'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add html class to block
|
||||
*
|
||||
* @param string $class
|
||||
*/
|
||||
public function add_class($class) {
|
||||
$this->attributes['class'] .= ' ' . $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the block is a fake block.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_fake() {
|
||||
return isset($this->attributes['data-block']) && $this->attributes['data-block'] == '_fake';
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(block_contents::class, \block_contents::class);
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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 core_block\output;
|
||||
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* This class represents a target for where a block can go when it is being moved.
|
||||
*
|
||||
* This needs to be rendered as a form with the given hidden from fields, and
|
||||
* clicking anywhere in the form should submit it. The form action should be
|
||||
* $PAGE->url.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core_block
|
||||
* @category output
|
||||
*/
|
||||
class block_move_target {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param moodle_url $url
|
||||
*/
|
||||
public function __construct(
|
||||
/** @var moodle_url Move url */
|
||||
public moodle_url $url,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(block_move_target::class, \block_move_target::class);
|
||||
@@ -391,6 +391,7 @@ class core_component {
|
||||
\core\exception\moodle_exception::class,
|
||||
\core\output\bootstrap_renderer::class,
|
||||
\core\lang_string::class,
|
||||
\renderable::class,
|
||||
];
|
||||
foreach ($keyclasses as $classname) {
|
||||
if (!array_key_exists($classname, $cache['classmap'])) {
|
||||
@@ -728,14 +729,13 @@ $cache = ' . var_export($cache, true) . ';
|
||||
self::$classmap = [];
|
||||
|
||||
self::load_classes('core', "$CFG->dirroot/lib/classes");
|
||||
self::load_legacy_classes($CFG->libdir);
|
||||
self::load_legacy_classes($CFG->libdir, true);
|
||||
|
||||
foreach (self::$subsystems as $subsystem => $fulldir) {
|
||||
if (!$fulldir) {
|
||||
continue;
|
||||
}
|
||||
self::load_classes('core_' . $subsystem, "$fulldir/classes");
|
||||
self::load_legacy_classes($fulldir);
|
||||
}
|
||||
|
||||
foreach (self::$plugins as $plugintype => $plugins) {
|
||||
@@ -1449,8 +1449,12 @@ $cache = ' . var_export($cache, true) . ';
|
||||
* and the value is the path to the class file within the relative ../classes/ directory.
|
||||
*
|
||||
* @param string|null $fulldir The directory to the legacy classes.
|
||||
* @param bool $allowsubsystems Whether to allow the specification of alternative subsystems for this path.
|
||||
*/
|
||||
protected static function load_legacy_classes(?string $fulldir): void {
|
||||
protected static function load_legacy_classes(
|
||||
?string $fulldir,
|
||||
bool $allowsubsystems = false,
|
||||
): void {
|
||||
if (is_null($fulldir)) {
|
||||
return;
|
||||
}
|
||||
@@ -1461,7 +1465,32 @@ $cache = ' . var_export($cache, true) . ';
|
||||
require($file);
|
||||
if (is_array($legacyclasses)) {
|
||||
foreach ($legacyclasses as $classname => $path) {
|
||||
self::$classmap[$classname] = "{$fulldir}/classes/{$path}";
|
||||
if (is_array($path)) {
|
||||
if (!$allowsubsystems) {
|
||||
throw new \Exception(
|
||||
"Invalid legacy classes path entry for {$classname}. " .
|
||||
"Only files within the component can be specified.",
|
||||
);
|
||||
}
|
||||
if (count($path) !== 2) {
|
||||
throw new \Exception(
|
||||
"Invalid legacy classes path entry for {$classname}. " .
|
||||
"Entries must be in the format [subsystem, path].",
|
||||
);
|
||||
}
|
||||
[$subsystem, $path] = $path;
|
||||
$subsystem = substr($subsystem, 5);
|
||||
if (!array_key_exists($subsystem, self::$subsystems)) {
|
||||
throw new \Exception(
|
||||
"Unknown subsystem '{$subsystem}' for legacy classes entry of '{$classname}'",
|
||||
);
|
||||
}
|
||||
|
||||
$subsystemfulldir = self::$subsystems[$subsystem];
|
||||
self::$classmap[$classname] = "{$subsystemfulldir}/classes/{$path}";
|
||||
} else {
|
||||
self::$classmap[$classname] = "{$fulldir}/classes/{$path}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\output\actions\component_action;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Data structure describing html link with special action attached.
|
||||
*
|
||||
* @copyright 2010 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class action_link implements renderable {
|
||||
/**
|
||||
* @var moodle_url Href url
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* @var string Link text HTML fragment
|
||||
*/
|
||||
public $text;
|
||||
|
||||
/**
|
||||
* @var array HTML attributes
|
||||
*/
|
||||
public $attributes;
|
||||
|
||||
/**
|
||||
* @var array List of actions attached to link
|
||||
*/
|
||||
public $actions;
|
||||
|
||||
/**
|
||||
* @var pix_icon Optional pix icon to render with the link
|
||||
*/
|
||||
public $icon;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param moodle_url $url
|
||||
* @param string $text HTML fragment
|
||||
* @param null|component_action $action
|
||||
* @param null|array $attributes associative array of html link attributes + disabled
|
||||
* @param null|pix_icon $icon optional pix_icon to render with the link text
|
||||
*/
|
||||
public function __construct(
|
||||
moodle_url $url,
|
||||
$text,
|
||||
?component_action $action = null,
|
||||
?array $attributes = null,
|
||||
?pix_icon $icon = null
|
||||
) {
|
||||
$this->url = clone($url);
|
||||
$this->text = $text;
|
||||
if (empty($attributes['id'])) {
|
||||
$attributes['id'] = html_writer::random_id('action_link');
|
||||
}
|
||||
$this->attributes = (array)$attributes;
|
||||
if ($action) {
|
||||
$this->add_action($action);
|
||||
}
|
||||
$this->icon = $icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add action to the link.
|
||||
*
|
||||
* @param component_action $action
|
||||
*/
|
||||
public function add_action(component_action $action) {
|
||||
$this->actions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a CSS class to this action link object
|
||||
* @param string $class
|
||||
*/
|
||||
public function add_class($class) {
|
||||
if (empty($this->attributes['class'])) {
|
||||
$this->attributes['class'] = $class;
|
||||
} else {
|
||||
$this->attributes['class'] .= ' ' . $class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the specified class has been added to this link.
|
||||
* @param string $class
|
||||
* @return bool
|
||||
*/
|
||||
public function has_class($class) {
|
||||
return strpos(' ' . $this->attributes['class'] . ' ', ' ' . $class . ' ') !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the rendered HTML for the icon. Useful for rendering action links in a template.
|
||||
* @return string
|
||||
*/
|
||||
public function get_icon_html() {
|
||||
global $OUTPUT;
|
||||
if (!$this->icon) {
|
||||
return '';
|
||||
}
|
||||
return $OUTPUT->render($this->icon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$data = new stdClass();
|
||||
$attributes = $this->attributes;
|
||||
|
||||
$data->id = $attributes['id'];
|
||||
unset($attributes['id']);
|
||||
|
||||
$data->disabled = !empty($attributes['disabled']);
|
||||
unset($attributes['disabled']);
|
||||
|
||||
$data->text = $this->text instanceof renderable ? $output->render($this->text) : (string) $this->text;
|
||||
$data->url = $this->url ? $this->url->out(false) : '';
|
||||
$data->icon = $this->icon ? $this->icon->export_for_pix() : null;
|
||||
$data->classes = isset($attributes['class']) ? $attributes['class'] : '';
|
||||
unset($attributes['class']);
|
||||
|
||||
$data->attributes = array_map(function ($key, $value) {
|
||||
return [
|
||||
'name' => $key,
|
||||
'value' => $value,
|
||||
];
|
||||
}, array_keys($attributes), $attributes);
|
||||
|
||||
$data->actions = array_map(function ($action) use ($output) {
|
||||
return $action->export_for_template($output);
|
||||
}, !empty($this->actions) ? $this->actions : []);
|
||||
$data->hasactions = !empty($this->actions);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(action_link::class, \action_link::class);
|
||||
@@ -0,0 +1,681 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use core\output\local\action_menu\subpanel;
|
||||
use core\output\action_menu\link as action_menu_link;
|
||||
use core\output\action_menu\filler as action_menu_filler;
|
||||
use lang_string;
|
||||
use moodle_page;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* An action menu.
|
||||
*
|
||||
* This action menu component takes a series of primary and secondary actions.
|
||||
* The primary actions are displayed permanently and the secondary attributes are displayed within a drop
|
||||
* down menu.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2013 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class action_menu implements renderable, templatable {
|
||||
/**
|
||||
* Top right alignment.
|
||||
*/
|
||||
const TL = 1;
|
||||
|
||||
/**
|
||||
* Top right alignment.
|
||||
*/
|
||||
const TR = 2;
|
||||
|
||||
/**
|
||||
* Top right alignment.
|
||||
*/
|
||||
const BL = 3;
|
||||
|
||||
/**
|
||||
* Top right alignment.
|
||||
*/
|
||||
const BR = 4;
|
||||
|
||||
/**
|
||||
* The instance number. This is unique to this instance of the action menu.
|
||||
* @var int
|
||||
*/
|
||||
protected $instance = 0;
|
||||
|
||||
/**
|
||||
* An array of primary actions. Please use {@see action_menu::add_primary_action()} to add actions.
|
||||
* @var array
|
||||
*/
|
||||
protected $primaryactions = [];
|
||||
|
||||
/**
|
||||
* An array of secondary actions. Please use {@see action_menu::add_secondary_action()} to add actions.
|
||||
* @var array
|
||||
*/
|
||||
protected $secondaryactions = [];
|
||||
|
||||
/**
|
||||
* An array of attributes added to the container of the action menu.
|
||||
* Initialised with defaults during construction.
|
||||
* @var array
|
||||
*/
|
||||
public $attributes = [];
|
||||
/**
|
||||
* An array of attributes added to the container of the primary actions.
|
||||
* Initialised with defaults during construction.
|
||||
* @var array
|
||||
*/
|
||||
public $attributesprimary = [];
|
||||
/**
|
||||
* An array of attributes added to the container of the secondary actions.
|
||||
* Initialised with defaults during construction.
|
||||
* @var array
|
||||
*/
|
||||
public $attributessecondary = [];
|
||||
|
||||
/**
|
||||
* The string to use next to the icon for the action icon relating to the secondary (dropdown) menu.
|
||||
* @var array
|
||||
*/
|
||||
public $actiontext = null;
|
||||
|
||||
/**
|
||||
* The string to use for the accessible label for the menu.
|
||||
* @var array
|
||||
*/
|
||||
public $actionlabel = null;
|
||||
|
||||
/**
|
||||
* An icon to use for the toggling the secondary menu (dropdown).
|
||||
* @var pix_icon
|
||||
*/
|
||||
public $actionicon;
|
||||
|
||||
/**
|
||||
* Any text to use for the toggling the secondary menu (dropdown).
|
||||
* @var string
|
||||
*/
|
||||
public $menutrigger = '';
|
||||
|
||||
/**
|
||||
* An array of attributes added to the trigger element of the secondary menu.
|
||||
* @var array
|
||||
*/
|
||||
public $triggerattributes = [];
|
||||
|
||||
/**
|
||||
* Any extra classes for toggling to the secondary menu.
|
||||
* @var string
|
||||
*/
|
||||
public $triggerextraclasses = '';
|
||||
|
||||
/**
|
||||
* Place the action menu before all other actions.
|
||||
* @var bool
|
||||
*/
|
||||
public $prioritise = false;
|
||||
|
||||
/**
|
||||
* Dropdown menu alignment class.
|
||||
* @var string
|
||||
*/
|
||||
public $dropdownalignment = '';
|
||||
|
||||
/**
|
||||
* Constructs the action menu with the given items.
|
||||
*
|
||||
* @param array $actions An array of actions (action_menu_link|pix_icon|string).
|
||||
*/
|
||||
public function __construct(array $actions = []) {
|
||||
static $initialised = 0;
|
||||
$this->instance = $initialised;
|
||||
$initialised++;
|
||||
|
||||
$this->attributes = [
|
||||
'id' => 'action-menu-' . $this->instance,
|
||||
'class' => 'moodle-actionmenu',
|
||||
'data-enhance' => 'moodle-core-actionmenu',
|
||||
];
|
||||
$this->attributesprimary = [
|
||||
'id' => 'action-menu-' . $this->instance . '-menubar',
|
||||
'class' => 'menubar',
|
||||
];
|
||||
$this->attributessecondary = [
|
||||
'id' => 'action-menu-' . $this->instance . '-menu',
|
||||
'class' => 'menu',
|
||||
'data-rel' => 'menu-content',
|
||||
'aria-labelledby' => 'action-menu-toggle-' . $this->instance,
|
||||
'role' => 'menu',
|
||||
];
|
||||
$this->dropdownalignment = 'dropdown-menu-right';
|
||||
foreach ($actions as $action) {
|
||||
$this->add($action);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label for the menu trigger.
|
||||
*
|
||||
* @param string $label The text
|
||||
*/
|
||||
public function set_action_label($label) {
|
||||
$this->actionlabel = $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the menu trigger text.
|
||||
*
|
||||
* @param string $trigger The text
|
||||
* @param string $extraclasses Extra classes to style the secondary menu toggle.
|
||||
*/
|
||||
public function set_menu_trigger($trigger, $extraclasses = '') {
|
||||
$this->menutrigger = $trigger;
|
||||
$this->triggerextraclasses = $extraclasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Classes for the trigger menu
|
||||
*/
|
||||
const DEFAULT_KEBAB_TRIGGER_CLASSES = 'btn btn-icon d-flex align-items-center justify-content-center no-caret';
|
||||
|
||||
/**
|
||||
* Setup trigger as in the kebab menu.
|
||||
*
|
||||
* @param string|null $triggername
|
||||
* @param core_renderer|null $output
|
||||
* @param string|null $extraclasses extra classes for the trigger {@see self::set_menu_trigger()}
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function set_kebab_trigger(
|
||||
?string $triggername = null,
|
||||
?core_renderer $output = null,
|
||||
?string $extraclasses = ''
|
||||
) {
|
||||
global $OUTPUT;
|
||||
if (empty($output)) {
|
||||
$output = $OUTPUT;
|
||||
}
|
||||
$label = $triggername ?? get_string('actions');
|
||||
$triggerclasses = self::DEFAULT_KEBAB_TRIGGER_CLASSES . ' ' . $extraclasses;
|
||||
$icon = $output->pix_icon('i/menu', $label);
|
||||
$this->set_menu_trigger($icon, $triggerclasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is at least one visible link in the menu.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_empty() {
|
||||
return !count($this->primaryactions) && !count($this->secondaryactions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises JS required fore the action menu.
|
||||
* The JS is only required once as it manages all action menu's on the page.
|
||||
*
|
||||
* @param moodle_page $page
|
||||
*/
|
||||
public function initialise_js(moodle_page $page) {
|
||||
static $initialised = false;
|
||||
if (!$initialised) {
|
||||
$page->requires->yui_module('moodle-core-actionmenu', 'M.core.actionmenu.init');
|
||||
$initialised = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an action to this action menu.
|
||||
*
|
||||
* @param action_link|pix_icon|subpanel|string $action
|
||||
*/
|
||||
public function add($action) {
|
||||
|
||||
if ($action instanceof subpanel) {
|
||||
$this->add_secondary_subpanel($action);
|
||||
} else if ($action instanceof action_link) {
|
||||
if ($action->primary) {
|
||||
$this->add_primary_action($action);
|
||||
} else {
|
||||
$this->add_secondary_action($action);
|
||||
}
|
||||
} else if ($action instanceof pix_icon) {
|
||||
$this->add_primary_action($action);
|
||||
} else {
|
||||
$this->add_secondary_action($action);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a secondary subpanel.
|
||||
* @param subpanel $subpanel
|
||||
*/
|
||||
public function add_secondary_subpanel(subpanel $subpanel) {
|
||||
$this->secondaryactions[] = $subpanel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a primary action to the action menu.
|
||||
*
|
||||
* @param action_menu_link|action_link|pix_icon|string $action
|
||||
*/
|
||||
public function add_primary_action($action) {
|
||||
if ($action instanceof action_link || $action instanceof pix_icon) {
|
||||
$action->attributes['role'] = 'menuitem';
|
||||
$action->attributes['tabindex'] = '-1';
|
||||
if ($action instanceof action_menu_link) {
|
||||
$action->actionmenu = $this;
|
||||
}
|
||||
}
|
||||
$this->primaryactions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a secondary action to the action menu.
|
||||
*
|
||||
* @param action_link|pix_icon|string $action
|
||||
*/
|
||||
public function add_secondary_action($action) {
|
||||
if ($action instanceof action_link || $action instanceof pix_icon) {
|
||||
$action->attributes['role'] = 'menuitem';
|
||||
$action->attributes['tabindex'] = '-1';
|
||||
if ($action instanceof action_menu_link) {
|
||||
$action->actionmenu = $this;
|
||||
}
|
||||
}
|
||||
$this->secondaryactions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the primary actions ready to be rendered.
|
||||
*
|
||||
* @param null|core_renderer $output The renderer to use for getting icons.
|
||||
* @return array
|
||||
*/
|
||||
public function get_primary_actions(?core_renderer $output = null) {
|
||||
global $OUTPUT;
|
||||
if ($output === null) {
|
||||
$output = $OUTPUT;
|
||||
}
|
||||
$pixicon = $this->actionicon;
|
||||
$linkclasses = ['toggle-display'];
|
||||
|
||||
$title = '';
|
||||
if (!empty($this->menutrigger)) {
|
||||
$pixicon = '<b class="caret"></b>';
|
||||
$linkclasses[] = 'textmenu';
|
||||
} else {
|
||||
$title = new lang_string('actionsmenu', 'moodle');
|
||||
$this->actionicon = new pix_icon(
|
||||
't/edit_menu',
|
||||
'',
|
||||
'moodle',
|
||||
['class' => 'iconsmall actionmenu', 'title' => '']
|
||||
);
|
||||
$pixicon = $this->actionicon;
|
||||
}
|
||||
if ($pixicon instanceof renderable) {
|
||||
$pixicon = $output->render($pixicon);
|
||||
if ($pixicon instanceof pix_icon && isset($pixicon->attributes['alt'])) {
|
||||
$title = $pixicon->attributes['alt'];
|
||||
}
|
||||
}
|
||||
$string = '';
|
||||
if ($this->actiontext) {
|
||||
$string = $this->actiontext;
|
||||
}
|
||||
$label = '';
|
||||
if ($this->actionlabel) {
|
||||
$label = $this->actionlabel;
|
||||
} else {
|
||||
$label = $title;
|
||||
}
|
||||
$actions = $this->primaryactions;
|
||||
$attributes = [
|
||||
'class' => implode(' ', $linkclasses),
|
||||
'title' => $title,
|
||||
'aria-label' => $label,
|
||||
'id' => 'action-menu-toggle-' . $this->instance,
|
||||
'role' => 'menuitem',
|
||||
'tabindex' => '-1',
|
||||
];
|
||||
$link = html_writer::link('#', $string . $this->menutrigger . $pixicon, $attributes);
|
||||
if ($this->prioritise) {
|
||||
array_unshift($actions, $link);
|
||||
} else {
|
||||
$actions[] = $link;
|
||||
}
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the secondary actions ready to be rendered.
|
||||
* @return array
|
||||
*/
|
||||
public function get_secondary_actions() {
|
||||
return $this->secondaryactions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the selector that should be used to find the owning node of this menu.
|
||||
* @param string $selector A CSS/YUI selector to identify the owner of the menu.
|
||||
*/
|
||||
public function set_owner_selector($selector) {
|
||||
$this->attributes['data-owner'] = $selector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the alignment of the dialogue in relation to button used to toggle it.
|
||||
*
|
||||
* @deprecated since Moodle 4.0
|
||||
*
|
||||
* @param int $dialogue One of action_menu::TL, action_menu::TR, action_menu::BL, action_menu::BR.
|
||||
* @param int $button One of action_menu::TL, action_menu::TR, action_menu::BL, action_menu::BR.
|
||||
*/
|
||||
public function set_alignment($dialogue, $button) {
|
||||
debugging('The method action_menu::set_alignment() is deprecated, use action_menu::set_menu_left()', DEBUG_DEVELOPER);
|
||||
if (isset($this->attributessecondary['data-align'])) {
|
||||
// We've already got one set, lets remove the old class so as to avoid troubles.
|
||||
$class = $this->attributessecondary['class'];
|
||||
$search = 'align-' . $this->attributessecondary['data-align'];
|
||||
$this->attributessecondary['class'] = str_replace($search, '', $class);
|
||||
}
|
||||
$align = $this->get_align_string($dialogue) . '-' . $this->get_align_string($button);
|
||||
$this->attributessecondary['data-align'] = $align;
|
||||
$this->attributessecondary['class'] .= ' align-' . $align;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string to describe the alignment.
|
||||
*
|
||||
* @param int $align One of action_menu::TL, action_menu::TR, action_menu::BL, action_menu::BR.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_align_string($align) {
|
||||
switch ($align) {
|
||||
case self::TL:
|
||||
return 'tl';
|
||||
case self::TR:
|
||||
return 'tr';
|
||||
case self::BL:
|
||||
return 'bl';
|
||||
case self::BR:
|
||||
return 'br';
|
||||
default:
|
||||
return 'tl';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Aligns the left corner of the dropdown.
|
||||
*
|
||||
*/
|
||||
public function set_menu_left() {
|
||||
$this->dropdownalignment = 'dropdown-menu-left';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a constraint for the dialogue.
|
||||
*
|
||||
* The constraint is applied when the dialogue is shown and limits the display of the dialogue to within the
|
||||
* element the constraint identifies.
|
||||
*
|
||||
* This is required whenever the action menu is displayed inside any CSS element with the .no-overflow class
|
||||
* (flexible_table and any of it's child classes are a likely candidate).
|
||||
*
|
||||
* @deprecated since Moodle 4.3
|
||||
* @param string $ancestorselector A snippet of CSS used to identify the ancestor to contrain the dialogue to.
|
||||
*/
|
||||
public function set_constraint($ancestorselector) {
|
||||
debugging('The method set_constraint() is deprecated. Please use the set_boundary() method instead.', DEBUG_DEVELOPER);
|
||||
$this->set_boundary('window');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the overflow constraint boundary of the dropdown menu.
|
||||
* @see https://getbootstrap.com/docs/4.6/components/dropdowns/#options The 'boundary' option in the Bootstrap documentation
|
||||
*
|
||||
* @param string $boundary Accepts the values of 'viewport', 'window', or 'scrollParent'.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function set_boundary(string $boundary) {
|
||||
if (!in_array($boundary, ['viewport', 'window', 'scrollParent'])) {
|
||||
throw new coding_exception("HTMLElement reference boundaries are not supported." .
|
||||
"Accepted boundaries are 'viewport', 'window', or 'scrollParent'.", DEBUG_DEVELOPER);
|
||||
}
|
||||
|
||||
$this->triggerattributes['data-boundary'] = $boundary;
|
||||
}
|
||||
|
||||
/**
|
||||
* If you call this method the action menu will be displayed but will not be enhanced.
|
||||
*
|
||||
* By not displaying the menu enhanced all items will be displayed in a single row.
|
||||
*
|
||||
* @deprecated since Moodle 3.2
|
||||
*/
|
||||
public function do_not_enhance() {
|
||||
debugging('The method action_menu::do_not_enhance() is deprecated, use a list of action_icon instead.', DEBUG_DEVELOPER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this action menu will be enhanced.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function will_be_enhanced() {
|
||||
return isset($this->attributes['data-enhance']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets nowrap on items. If true menu items should not wrap lines if they are longer than the available space.
|
||||
*
|
||||
* This property can be useful when the action menu is displayed within a parent element that is either floated
|
||||
* or relatively positioned.
|
||||
* In that situation the width of the menu is determined by the width of the parent element which may not be large
|
||||
* enough for the menu items without them wrapping.
|
||||
* This disables the wrapping so that the menu takes on the width of the longest item.
|
||||
*
|
||||
* @param bool $value If true nowrap gets set, if false it gets removed. Defaults to true.
|
||||
*/
|
||||
public function set_nowrap_on_items($value = true) {
|
||||
$class = 'nowrap-items';
|
||||
if (!empty($this->attributes['class'])) {
|
||||
$pos = strpos($this->attributes['class'], $class);
|
||||
if ($value === true && $pos === false) {
|
||||
// The value is true and the class has not been set yet. Add it.
|
||||
$this->attributes['class'] .= ' ' . $class;
|
||||
} else if ($value === false && $pos !== false) {
|
||||
// The value is false and the class has been set. Remove it.
|
||||
$this->attributes['class'] = substr($this->attributes['class'], $pos, strlen($class));
|
||||
}
|
||||
} else if ($value) {
|
||||
// The value is true and the class has not been set yet. Add it.
|
||||
$this->attributes['class'] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add classes to the action menu for an easier styling.
|
||||
*
|
||||
* @param string $class The class to add to attributes.
|
||||
*/
|
||||
public function set_additional_classes(string $class = '') {
|
||||
if (!empty($this->attributes['class'])) {
|
||||
$this->attributes['class'] .= " " . $class;
|
||||
} else {
|
||||
$this->attributes['class'] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$data = new stdClass();
|
||||
// Assign a role of menubar to this action menu when:
|
||||
// - it contains 2 or more primary actions; or
|
||||
// - if it contains a primary action and secondary actions.
|
||||
if (count($this->primaryactions) > 1 || (!empty($this->primaryactions) && !empty($this->secondaryactions))) {
|
||||
$this->attributes['role'] = 'menubar';
|
||||
}
|
||||
$attributes = $this->attributes;
|
||||
|
||||
$data->instance = $this->instance;
|
||||
|
||||
$data->classes = isset($attributes['class']) ? $attributes['class'] : '';
|
||||
unset($attributes['class']);
|
||||
|
||||
$data->attributes = array_map(function ($key, $value) {
|
||||
return [ 'name' => $key, 'value' => $value ];
|
||||
}, array_keys($attributes), $attributes);
|
||||
|
||||
$data->primary = $this->export_primary_actions_for_template($output);
|
||||
$data->secondary = $this->export_secondary_actions_for_template($output);
|
||||
$data->dropdownalignment = $this->dropdownalignment;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the primary actions for the template.
|
||||
* @param renderer_base $output
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function export_primary_actions_for_template(renderer_base $output): stdClass {
|
||||
$attributes = $this->attributes;
|
||||
$attributesprimary = $this->attributesprimary;
|
||||
|
||||
$primary = new stdClass();
|
||||
$primary->title = '';
|
||||
$primary->prioritise = $this->prioritise;
|
||||
|
||||
$primary->classes = isset($attributesprimary['class']) ? $attributesprimary['class'] : '';
|
||||
unset($attributesprimary['class']);
|
||||
|
||||
$primary->attributes = array_map(function ($key, $value) {
|
||||
return ['name' => $key, 'value' => $value];
|
||||
}, array_keys($attributesprimary), $attributesprimary);
|
||||
$primary->triggerattributes = array_map(function ($key, $value) {
|
||||
return ['name' => $key, 'value' => $value];
|
||||
}, array_keys($this->triggerattributes), $this->triggerattributes);
|
||||
|
||||
$actionicon = $this->actionicon;
|
||||
if (!empty($this->menutrigger)) {
|
||||
$primary->menutrigger = $this->menutrigger;
|
||||
$primary->triggerextraclasses = $this->triggerextraclasses;
|
||||
if ($this->actionlabel) {
|
||||
$primary->title = $this->actionlabel;
|
||||
} else if ($this->actiontext) {
|
||||
$primary->title = $this->actiontext;
|
||||
} else {
|
||||
$primary->title = strip_tags($this->menutrigger);
|
||||
}
|
||||
} else {
|
||||
$primary->title = get_string('actionsmenu');
|
||||
$iconattributes = ['class' => 'iconsmall actionmenu', 'title' => $primary->title];
|
||||
$actionicon = new pix_icon('t/edit_menu', '', 'moodle', $iconattributes);
|
||||
}
|
||||
|
||||
// If the menu trigger is within the menubar, assign a role of menuitem. Otherwise, assign as a button.
|
||||
$primary->triggerrole = 'button';
|
||||
if (isset($attributes['role']) && $attributes['role'] === 'menubar') {
|
||||
$primary->triggerrole = 'menuitem';
|
||||
}
|
||||
|
||||
if ($actionicon instanceof pix_icon) {
|
||||
$primary->icon = $actionicon->export_for_pix();
|
||||
if (!empty($actionicon->attributes['alt'])) {
|
||||
$primary->title = $actionicon->attributes['alt'];
|
||||
}
|
||||
} else {
|
||||
$primary->iconraw = $actionicon ? $output->render($actionicon) : '';
|
||||
}
|
||||
|
||||
$primary->actiontext = $this->actiontext ? (string) $this->actiontext : '';
|
||||
$primary->items = array_map(function ($item) use ($output) {
|
||||
$data = (object) [];
|
||||
if ($item instanceof action_menu_link) {
|
||||
$data->actionmenulink = $item->export_for_template($output);
|
||||
} else if ($item instanceof action_menu_filler) {
|
||||
$data->actionmenufiller = $item->export_for_template($output);
|
||||
} else if ($item instanceof action_link) {
|
||||
$data->actionlink = $item->export_for_template($output);
|
||||
} else if ($item instanceof pix_icon) {
|
||||
$data->pixicon = $item->export_for_template($output);
|
||||
} else {
|
||||
$data->rawhtml = ($item instanceof renderable) ? $output->render($item) : $item;
|
||||
}
|
||||
return $data;
|
||||
}, $this->primaryactions);
|
||||
return $primary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the secondary actions for the template.
|
||||
* @param renderer_base $output
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function export_secondary_actions_for_template(renderer_base $output): stdClass {
|
||||
$attributessecondary = $this->attributessecondary;
|
||||
$secondary = new stdClass();
|
||||
$secondary->classes = isset($attributessecondary['class']) ? $attributessecondary['class'] : '';
|
||||
unset($attributessecondary['class']);
|
||||
|
||||
$secondary->attributes = array_map(function ($key, $value) {
|
||||
return ['name' => $key, 'value' => $value];
|
||||
}, array_keys($attributessecondary), $attributessecondary);
|
||||
$secondary->items = array_map(function ($item) use ($output) {
|
||||
$data = (object) [
|
||||
'simpleitem' => true,
|
||||
];
|
||||
if ($item instanceof action_menu_link) {
|
||||
$data->actionmenulink = $item->export_for_template($output);
|
||||
$data->simpleitem = false;
|
||||
} else if ($item instanceof action_menu_filler) {
|
||||
$data->actionmenufiller = $item->export_for_template($output);
|
||||
$data->simpleitem = false;
|
||||
} else if ($item instanceof subpanel) {
|
||||
$data->subpanel = $item->export_for_template($output);
|
||||
$data->simpleitem = false;
|
||||
} else if ($item instanceof action_link) {
|
||||
$data->actionlink = $item->export_for_template($output);
|
||||
} else if ($item instanceof pix_icon) {
|
||||
$data->pixicon = $item->export_for_template($output);
|
||||
} else {
|
||||
$data->rawhtml = ($item instanceof renderable) ? $output->render($item) : $item;
|
||||
}
|
||||
return $data;
|
||||
}, $this->secondaryactions);
|
||||
return $secondary;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(action_menu::class, \action_menu::class);
|
||||
@@ -0,0 +1,49 @@
|
||||
<?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 core\output\action_menu;
|
||||
|
||||
use core\output\action_link;
|
||||
use core\output\html_writer;
|
||||
use core\output\renderable;
|
||||
|
||||
/**
|
||||
* An action menu filler
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2013 Andrew Nicols
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class filler extends action_link implements renderable {
|
||||
/**
|
||||
* True if this is a primary action. False if not.
|
||||
* @var bool
|
||||
*/
|
||||
public $primary = true;
|
||||
|
||||
/**
|
||||
* Constructs the object.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->attributes['id'] = html_writer::random_id('action_link');
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(filler::class, \action_menu_filler::class);
|
||||
@@ -0,0 +1,120 @@
|
||||
<?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 core\output\action_menu;
|
||||
|
||||
use core\output\action_menu;
|
||||
use core\output\action_link;
|
||||
use core\output\pix_icon;
|
||||
use core\output\renderable;
|
||||
use core\output\renderer_base;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* An action menu action
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2013 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class link extends action_link implements renderable {
|
||||
/**
|
||||
* True if this is a primary action. False if not.
|
||||
* @var bool
|
||||
*/
|
||||
public $primary = true;
|
||||
|
||||
/**
|
||||
* The action menu this link has been added to.
|
||||
* @var action_menu
|
||||
*/
|
||||
public $actionmenu = null;
|
||||
|
||||
/**
|
||||
* The number of instances of this action menu link (and its subclasses).
|
||||
*
|
||||
* @var int
|
||||
* @deprecated since Moodle 4.4.
|
||||
*/
|
||||
protected static $instance = 1;
|
||||
|
||||
/**
|
||||
* Constructs the object.
|
||||
*
|
||||
* @param moodle_url $url The URL for the action.
|
||||
* @param pix_icon|null $icon The icon to represent the action.
|
||||
* @param string $text The text to represent the action.
|
||||
* @param bool $primary Whether this is a primary action or not.
|
||||
* @param array $attributes Any attribtues associated with the action.
|
||||
*/
|
||||
public function __construct(moodle_url $url, ?pix_icon $icon, $text, $primary = true, array $attributes = []) {
|
||||
parent::__construct($url, $text, null, $attributes, $icon);
|
||||
$this->primary = (bool)$primary;
|
||||
$this->add_class('menu-action');
|
||||
$this->attributes['role'] = 'menuitem';
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$data = parent::export_for_template($output);
|
||||
|
||||
// Ignore what the parent did with the attributes, except for ID and class.
|
||||
$data->attributes = [];
|
||||
$attributes = $this->attributes;
|
||||
unset($attributes['id']);
|
||||
unset($attributes['class']);
|
||||
|
||||
// Handle text being a renderable.
|
||||
if ($this->text instanceof renderable) {
|
||||
$data->text = $this->render($this->text);
|
||||
}
|
||||
|
||||
$data->showtext = (!$this->icon || $this->primary === false);
|
||||
|
||||
$data->icon = null;
|
||||
if ($this->icon) {
|
||||
$icon = $this->icon;
|
||||
if ($this->primary || !$this->actionmenu->will_be_enhanced()) {
|
||||
$attributes['title'] = $data->text;
|
||||
}
|
||||
$data->icon = $icon ? $icon->export_for_pix() : null;
|
||||
}
|
||||
|
||||
$data->disabled = !empty($attributes['disabled']);
|
||||
unset($attributes['disabled']);
|
||||
|
||||
$data->attributes = array_map(function ($key, $value) {
|
||||
return [
|
||||
'name' => $key,
|
||||
'value' => $value,
|
||||
];
|
||||
}, array_keys($attributes), $attributes);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(link::class, \action_menu_link::class);
|
||||
@@ -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/>.
|
||||
|
||||
namespace core\output\action_menu;
|
||||
|
||||
use core\output\pix_icon;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* A primary action menu action
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2013 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class link_primary extends link {
|
||||
/**
|
||||
* Constructs the object.
|
||||
*
|
||||
* @param moodle_url $url
|
||||
* @param pix_icon|null $icon
|
||||
* @param string $text
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(moodle_url $url, ?pix_icon $icon, $text, array $attributes = []) {
|
||||
parent::__construct($url, $icon, $text, true, $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(link_primary::class, \action_menu_link_primary::class);
|
||||
@@ -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/>.
|
||||
|
||||
namespace core\output\action_menu;
|
||||
|
||||
use core\output\pix_icon;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* A secondary action menu action
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2013 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class link_secondary extends link {
|
||||
/**
|
||||
* Constructs the object.
|
||||
*
|
||||
* @param moodle_url $url
|
||||
* @param pix_icon|null $icon
|
||||
* @param string $text
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(moodle_url $url, ?pix_icon $icon, $text, array $attributes = []) {
|
||||
parent::__construct($url, $icon, $text, false, $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(link_secondary::class, \action_menu_link_secondary::class);
|
||||
+10
-5
@@ -14,17 +14,17 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core\output\local\action_menu;
|
||||
namespace core\output\action_menu;
|
||||
|
||||
use action_link;
|
||||
use pix_icon;
|
||||
use renderable;
|
||||
use core\output\action_link;
|
||||
use core\output\pix_icon;
|
||||
use core\output\renderable;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Interface to a subpanel implementation.
|
||||
*
|
||||
* @package core_admin
|
||||
* @package core
|
||||
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
@@ -80,3 +80,8 @@ class subpanel extends action_link implements renderable {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(subpanel::class, \core\output\local\action_menu\subpanel::class);
|
||||
@@ -0,0 +1,90 @@
|
||||
<?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 core\output\actions;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use core\output\renderer_base;
|
||||
use core\output\templatable;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Helper class used by other components that involve an action on the page (URL or JS).
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class component_action implements templatable {
|
||||
/**
|
||||
* @var string $event The DOM event that will trigger this action when caught
|
||||
*/
|
||||
public $event;
|
||||
|
||||
/**
|
||||
* @var string A function name to call when the button is clicked
|
||||
* The JS function you create must have two arguments:
|
||||
* 1. The event object
|
||||
* 2. An object/array of arguments ($jsfunctionargs)
|
||||
*/
|
||||
public $jsfunction = false;
|
||||
|
||||
/**
|
||||
* @var array An array of arguments to pass to the JS function
|
||||
*/
|
||||
public $jsfunctionargs = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param string $event DOM event
|
||||
* @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
|
||||
* @param array $jsfunctionargs An array of arguments to pass to the jsfunction
|
||||
*/
|
||||
public function __construct($event, $jsfunction, $jsfunctionargs = []) {
|
||||
$this->event = $event;
|
||||
|
||||
$this->jsfunction = $jsfunction;
|
||||
$this->jsfunctionargs = $jsfunctionargs;
|
||||
|
||||
if (!empty($this->jsfunctionargs)) {
|
||||
if (empty($this->jsfunction)) {
|
||||
throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$args = !empty($this->jsfunctionargs) ? json_encode($this->jsfunctionargs) : false;
|
||||
return (object) [
|
||||
'event' => $this->event,
|
||||
'jsfunction' => $this->jsfunction,
|
||||
'jsfunctionargs' => $args,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(component_action::class, \component_action::class);
|
||||
@@ -0,0 +1,58 @@
|
||||
<?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 core\output\actions;
|
||||
|
||||
/**
|
||||
* Confirm action
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class confirm_action extends component_action {
|
||||
/**
|
||||
* Constructs the confirm action object
|
||||
*
|
||||
* @param string $message The message to display to the user when they are shown
|
||||
* the confirm dialogue.
|
||||
* @param string $callback Deprecated since 2.7
|
||||
* @param string $continuelabel The string to use for he continue button
|
||||
* @param string $cancellabel The string to use for the cancel button
|
||||
*/
|
||||
public function __construct($message, $callback = null, $continuelabel = null, $cancellabel = null) {
|
||||
if ($callback !== null) {
|
||||
debugging(
|
||||
'The callback argument to new confirm_action() has been deprecated.' .
|
||||
' If you need to use a callback, please write Javascript to use moodle-core-notification-confirmation ' .
|
||||
'and attach to the provided events.',
|
||||
DEBUG_DEVELOPER,
|
||||
);
|
||||
}
|
||||
parent::__construct('click', 'M.util.show_confirm_dialog', [
|
||||
'message' => $message,
|
||||
'continuelabel' => $continuelabel,
|
||||
'cancellabel' => $cancellabel,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(confirm_action::class, \confirm_action::class);
|
||||
@@ -0,0 +1,120 @@
|
||||
<?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 core\output\actions;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* Component action for a popup window.
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class popup_action extends component_action {
|
||||
/**
|
||||
* @var string The JS function to call for the popup
|
||||
*/
|
||||
public $jsfunction = 'openpopup';
|
||||
|
||||
/**
|
||||
* @var array An array of parameters that will be passed to the openpopup JS function
|
||||
*/
|
||||
public $params = [
|
||||
'height' => 400,
|
||||
'width' => 500,
|
||||
'top' => 0,
|
||||
'left' => 0,
|
||||
'menubar' => false,
|
||||
'location' => false,
|
||||
'scrollbars' => true,
|
||||
'resizable' => true,
|
||||
'toolbar' => true,
|
||||
'status' => true,
|
||||
'directories' => false,
|
||||
'fullscreen' => false,
|
||||
'dependent' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $event DOM event
|
||||
* @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
|
||||
* @param string $name The JS function to call for the popup (default 'popup')
|
||||
* @param array $params An array of popup parameters
|
||||
*/
|
||||
public function __construct($event, $url, $name = 'popup', $params = []) {
|
||||
global $CFG;
|
||||
|
||||
$url = new moodle_url($url);
|
||||
|
||||
if ($name) {
|
||||
$checkname = $name;
|
||||
if (($checkname = preg_replace("/\s/", '_', $checkname)) != $name) {
|
||||
throw new coding_exception(
|
||||
"The {$name} of a popup window shouldn't contain spaces - string modified. {$name} changed to {$checkname}",
|
||||
);
|
||||
$name = $checkname;
|
||||
}
|
||||
} else {
|
||||
$name = 'popup';
|
||||
}
|
||||
|
||||
foreach ($this->params as $var => $val) {
|
||||
if (array_key_exists($var, $params)) {
|
||||
$this->params[$var] = $params[$var];
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = ['url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params)];
|
||||
if (!empty($params['fullscreen'])) {
|
||||
$attributes['fullscreen'] = 1;
|
||||
}
|
||||
parent::__construct($event, $this->jsfunction, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string of concatenated option->value pairs used by JS to call the popup window,
|
||||
* based on this object's variables
|
||||
*
|
||||
* @return string String of option->value pairs for JS popup function.
|
||||
*/
|
||||
public function get_js_options() {
|
||||
$jsoptions = '';
|
||||
|
||||
foreach ($this->params as $var => $val) {
|
||||
if (is_string($val) || is_int($val)) {
|
||||
$jsoptions .= "$var=$val,";
|
||||
} else if (is_bool($val)) {
|
||||
$jsoptions .= ($val) ? "$var," : "$var=0,";
|
||||
}
|
||||
}
|
||||
|
||||
$jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);
|
||||
|
||||
return $jsoptions;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(popup_action::class, \popup_action::class);
|
||||
@@ -17,8 +17,6 @@
|
||||
namespace core\output;
|
||||
|
||||
use moodle_page;
|
||||
use renderer_base;
|
||||
use url_select;
|
||||
|
||||
/**
|
||||
* Data structure representing standard components displayed on the activity header.
|
||||
@@ -31,7 +29,7 @@ use url_select;
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class activity_header implements \renderable, \templatable {
|
||||
class activity_header implements renderable, templatable {
|
||||
/** @var moodle_page $page The current page we are looking at */
|
||||
protected $page;
|
||||
/** @var string $title The title to be displayed in the header. Defaults to activityrecord name. */
|
||||
@@ -66,8 +64,10 @@ class activity_header implements \renderable, \templatable {
|
||||
$this->title = format_string($page->activityrecord->name);
|
||||
}
|
||||
|
||||
if (empty($layoutoptions['nodescription']) && !empty($page->activityrecord->intro) &&
|
||||
trim($page->activityrecord->intro)) {
|
||||
if (
|
||||
empty($layoutoptions['nodescription']) && !empty($page->activityrecord->intro) &&
|
||||
trim($page->activityrecord->intro)
|
||||
) {
|
||||
$this->description = format_module_intro($this->page->activityname, $page->activityrecord, $page->cm->id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,12 +23,8 @@
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use renderable;
|
||||
use renderer_base;
|
||||
use stdClass;
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* The checkbox-toggleall renderable class.
|
||||
@@ -38,7 +34,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class checkbox_toggleall implements renderable, templatable {
|
||||
|
||||
/** @var string The name of the group of checkboxes to be toggled. */
|
||||
protected $togglegroup;
|
||||
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use renderable;
|
||||
use renderer_base;
|
||||
use core\output\named_templatable;
|
||||
|
||||
/**
|
||||
* A generic user choice output class.
|
||||
*
|
||||
@@ -30,8 +26,7 @@ use core\output\named_templatable;
|
||||
* @copyright 2023 Ferran Recio <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class choicelist implements renderable, named_templatable {
|
||||
|
||||
class choicelist implements named_templatable, renderable {
|
||||
/** @var object[] The user choices. */
|
||||
protected $options = [];
|
||||
|
||||
@@ -102,13 +97,13 @@ class choicelist implements renderable, named_templatable {
|
||||
* @return \stdClass[]
|
||||
*/
|
||||
public function get_selectable_options(): array {
|
||||
$selectableOptions = [];
|
||||
$selectableoptions = [];
|
||||
foreach ($this->options as $option) {
|
||||
if ($option['value'] !== $this->selected && !$option['disabled']) {
|
||||
$selectableOptions[] = (object) $option;
|
||||
$selectableoptions[] = (object) $option;
|
||||
}
|
||||
}
|
||||
return $selectableOptions;
|
||||
return $selectableoptions;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,14 +23,10 @@
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
use renderer_base;
|
||||
use renderable;
|
||||
use stdClass;
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* The chooser renderable class.
|
||||
@@ -40,7 +36,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class chooser implements renderable, templatable {
|
||||
|
||||
/** @var moodle_url The form action URL. */
|
||||
public $actionurl;
|
||||
/** @var lang_string The instructions to display. */
|
||||
@@ -85,7 +80,7 @@ class chooser implements renderable, templatable {
|
||||
$this->params[] = [
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
'id' => $id
|
||||
'id' => $id,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -124,11 +119,10 @@ class chooser implements renderable, templatable {
|
||||
$data->sesskey = sesskey();
|
||||
$data->title = (string) $this->title;
|
||||
|
||||
$data->sections = array_map(function($section) use ($output) {
|
||||
$data->sections = array_map(function ($section) use ($output) {
|
||||
return $section->export_for_template($output);
|
||||
}, $this->sections);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,13 +24,9 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use coding_exception;
|
||||
use context;
|
||||
use pix_icon;
|
||||
use renderer_base;
|
||||
use renderable;
|
||||
use core\exception\coding_exception;
|
||||
use core\context;
|
||||
use stdClass;
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* The chooser_item renderable class.
|
||||
@@ -40,7 +36,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class chooser_item implements renderable, templatable {
|
||||
|
||||
/** @var string An identifier for the item. */
|
||||
public $id;
|
||||
/** @var string The label of this item. */
|
||||
@@ -93,11 +88,17 @@ class chooser_item implements renderable, templatable {
|
||||
|
||||
$data->description = '';
|
||||
if (!empty($this->description)) {
|
||||
list($data->description) = \core_external\util::format_text((string) $this->description, FORMAT_MARKDOWN,
|
||||
$this->context->id, null, null, null, $options);
|
||||
[$data->description] = \core_external\util::format_text(
|
||||
(string) $this->description,
|
||||
FORMAT_MARKDOWN,
|
||||
$this->context->id,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,13 +23,9 @@
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use lang_string;
|
||||
use renderer_base;
|
||||
use renderable;
|
||||
use stdClass;
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* The chooser_section renderable class.
|
||||
@@ -39,7 +35,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class chooser_section implements renderable, templatable {
|
||||
|
||||
/** @var string $id An identifier for the section. */
|
||||
public $id;
|
||||
/** @var lang_string $label The label of the section. */
|
||||
@@ -70,10 +65,9 @@ class chooser_section implements renderable, templatable {
|
||||
$data = new stdClass();
|
||||
$data->id = $this->id;
|
||||
$data->label = (string) $this->label;
|
||||
$data->items = array_map(function($item) use ($output) {
|
||||
$data->items = array_map(function ($item) use ($output) {
|
||||
return $item->export_for_template($output);
|
||||
}, array_values($this->items));
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,20 +16,16 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use moodle_exception;
|
||||
use renderable;
|
||||
use renderer_base;
|
||||
use templatable;
|
||||
use core\exception\moodle_exception;
|
||||
|
||||
/**
|
||||
* Renderable class for the comboboxsearch.
|
||||
*
|
||||
* @package core_output
|
||||
* @package core
|
||||
* @copyright 2022 Mathew May <Mathew.solutions>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class comboboxsearch implements renderable, templatable {
|
||||
|
||||
/** @var bool $renderlater Should the dropdown render straightaway? We sometimes need to output the component without all of the
|
||||
* data and leave the rendering of any defaults and actual data to the caller. We will give you a basic placeholder that can
|
||||
* then be easily replaced.*/
|
||||
@@ -53,7 +49,7 @@ class comboboxsearch implements renderable, templatable {
|
||||
/** @var null|string $buttonheader If the button item in the tertiary nav needs an extra top header for context. */
|
||||
protected $buttonheader;
|
||||
|
||||
/** @var boolean $usesbutton Whether to provide a A11y button. */
|
||||
/** @var bool $usesbutton Whether to provide a A11y button. */
|
||||
protected $usesbutton;
|
||||
|
||||
/** @var null|string $label The label of the combobox. */
|
||||
@@ -99,15 +95,16 @@ class comboboxsearch implements renderable, templatable {
|
||||
throw new moodle_exception(
|
||||
'incorrectdropdownvars',
|
||||
'core',
|
||||
'', null,
|
||||
'',
|
||||
null,
|
||||
'Dropdown content must be set to render later.'
|
||||
);
|
||||
}
|
||||
|
||||
if ($usebutton && !$label) {
|
||||
debugging(
|
||||
'You have requested to use the button but have not provided a label for the combobox.',
|
||||
DEBUG_DEVELOPER
|
||||
'You have requested to use the button but have not provided a label for the combobox.',
|
||||
DEBUG_DEVELOPER
|
||||
);
|
||||
}
|
||||
|
||||
@@ -140,7 +137,7 @@ class comboboxsearch implements renderable, templatable {
|
||||
public function export_for_template(renderer_base $output): array {
|
||||
return [
|
||||
'renderlater' => $this->renderlater,
|
||||
'buttoncontent' => $this->buttoncontent ,
|
||||
'buttoncontent' => $this->buttoncontent,
|
||||
'dropdowncontent' => $this->dropdowncontent,
|
||||
'parentclasses' => $this->parentclasses,
|
||||
'buttonclasses' => $this->buttonclasses,
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Renderable for the main page header.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @since 2.9
|
||||
* @copyright 2015 Adrian Greeve <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class context_header implements renderable, templatable {
|
||||
/** @var string $heading Main heading */
|
||||
public $heading;
|
||||
|
||||
/** @var int $headinglevel Main heading 'h' tag level */
|
||||
public $headinglevel;
|
||||
|
||||
/** @var string|null $imagedata HTML code for the picture in the page header */
|
||||
public $imagedata;
|
||||
|
||||
/**
|
||||
* @var array $additionalbuttons Additional buttons for the header e.g. Messaging button for the user header.
|
||||
* array elements - title => alternate text for the image, or if no image is available the button text.
|
||||
* url => Link for the button to head to. Should be a moodle_url.
|
||||
* image => location to the image, or name of the image in /pix/t/{image name}.
|
||||
* linkattributes => additional attributes for the <a href> element.
|
||||
* page => page object. Don't include if the image is an external image.
|
||||
*/
|
||||
public $additionalbuttons;
|
||||
|
||||
/** @var string $prefix A string that is before the title */
|
||||
public $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $heading Main heading data.
|
||||
* @param int $headinglevel Main heading 'h' tag level.
|
||||
* @param string|null $imagedata HTML code for the picture in the page header.
|
||||
* @param string $additionalbuttons Buttons for the header e.g. Messaging button for the user header.
|
||||
* @param string $prefix Text that precedes the heading.
|
||||
*/
|
||||
public function __construct($heading = null, $headinglevel = 1, $imagedata = null, $additionalbuttons = null, $prefix = null) {
|
||||
$this->heading = $heading;
|
||||
$this->headinglevel = $headinglevel;
|
||||
$this->imagedata = $imagedata;
|
||||
$this->additionalbuttons = $additionalbuttons;
|
||||
// If we have buttons then format them.
|
||||
if (isset($this->additionalbuttons)) {
|
||||
$this->format_button_images();
|
||||
}
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an array element for a formatted image.
|
||||
*/
|
||||
protected function format_button_images() {
|
||||
|
||||
foreach ($this->additionalbuttons as $buttontype => $button) {
|
||||
$page = $button['page'];
|
||||
// If no image is provided then just use the title.
|
||||
if (!isset($button['image'])) {
|
||||
$this->additionalbuttons[$buttontype]['formattedimage'] = $button['title'];
|
||||
} else {
|
||||
// Check to see if this is an internal Moodle icon.
|
||||
$internalimage = $page->theme->resolve_image_location('t/' . $button['image'], 'moodle');
|
||||
if ($internalimage) {
|
||||
$this->additionalbuttons[$buttontype]['formattedimage'] = 't/' . $button['image'];
|
||||
} else {
|
||||
// Treat as an external image.
|
||||
$this->additionalbuttons[$buttontype]['formattedimage'] = $button['image'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($button['linkattributes']['class'])) {
|
||||
$class = $button['linkattributes']['class'] . ' btn';
|
||||
} else {
|
||||
$class = 'btn';
|
||||
}
|
||||
// Add the bootstrap 'btn' class for formatting.
|
||||
$this->additionalbuttons[$buttontype]['linkattributes'] = array_merge(
|
||||
$button['linkattributes'],
|
||||
['class' => $class]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output Renderer.
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_template(renderer_base $output): array {
|
||||
// Heading.
|
||||
$headingtext = isset($this->heading) ? $this->heading : $output->get_page()->heading;
|
||||
$heading = $output->heading($headingtext, $this->headinglevel, "h2 mb-0");
|
||||
|
||||
// Buttons.
|
||||
if (isset($this->additionalbuttons)) {
|
||||
$additionalbuttons = [];
|
||||
foreach ($this->additionalbuttons as $button) {
|
||||
if (!isset($button->page)) {
|
||||
// Include js for messaging.
|
||||
if ($button['buttontype'] === 'togglecontact') {
|
||||
\core_message\helper::togglecontact_requirejs();
|
||||
}
|
||||
if ($button['buttontype'] === 'message') {
|
||||
\core_message\helper::messageuser_requirejs();
|
||||
}
|
||||
}
|
||||
foreach ($button['linkattributes'] as $key => $value) {
|
||||
$button['attributes'][] = ['name' => $key, 'value' => $value];
|
||||
}
|
||||
$additionalbuttons[] = $button;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'heading' => $heading,
|
||||
'headinglevel' => $this->headinglevel,
|
||||
'imagedata' => $this->imagedata,
|
||||
'prefix' => $this->prefix,
|
||||
'hasadditionalbuttons' => !empty($additionalbuttons),
|
||||
'additionalbuttons' => $additionalbuttons ?? [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(context_header::class, \context_header::class);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,154 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core_useragent;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* A renderer that generates output for ajax scripts.
|
||||
*
|
||||
* This renderer prevents accidental sends back only json
|
||||
* encoded error messages, all other output is ignored.
|
||||
*
|
||||
* @copyright 2010 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class core_renderer_ajax extends core_renderer {
|
||||
/**
|
||||
* Returns a template fragment representing a fatal error.
|
||||
*
|
||||
* @param string $message The message to output
|
||||
* @param string $moreinfourl URL where more info can be found about the error
|
||||
* @param string $link Link for the Continue button
|
||||
* @param array $backtrace The execution backtrace
|
||||
* @param null|string $debuginfo Debugging information
|
||||
* @param string $errorcode
|
||||
* @return string A template fragment for a fatal error
|
||||
*/
|
||||
public function fatal_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = "") {
|
||||
global $CFG;
|
||||
|
||||
// Ugly hack - make sure page context is set to something, we do not want bogus warnings here.
|
||||
$this->page->set_context(null);
|
||||
|
||||
$e = new stdClass();
|
||||
$e->error = $message;
|
||||
$e->errorcode = $errorcode;
|
||||
$e->stacktrace = null;
|
||||
$e->debuginfo = null;
|
||||
$e->reproductionlink = null;
|
||||
if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) {
|
||||
$link = (string) $link;
|
||||
if ($link) {
|
||||
$e->reproductionlink = $link;
|
||||
}
|
||||
if (!empty($debuginfo)) {
|
||||
$e->debuginfo = $debuginfo;
|
||||
}
|
||||
if (!empty($backtrace)) {
|
||||
$e->stacktrace = format_backtrace($backtrace, true);
|
||||
}
|
||||
}
|
||||
$this->header();
|
||||
return json_encode($e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to display a notification.
|
||||
* For the AJAX notifications are discarded.
|
||||
*
|
||||
* @param string $message The message to print out.
|
||||
* @param string $type The type of notification. See constants on \core\output\notification.
|
||||
* @param bool $closebutton Whether to show a close icon to remove the notification (default true).
|
||||
*/
|
||||
public function notification($message, $type = null, $closebutton = true) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to display a redirection message.
|
||||
* AJAX redirections should not occur and as such redirection messages
|
||||
* are discarded.
|
||||
*
|
||||
* @param moodle_url|string $encodedurl
|
||||
* @param string $message
|
||||
* @param int $delay
|
||||
* @param bool $debugdisableredirect
|
||||
* @param string $messagetype The type of notification to show the message in.
|
||||
* See constants on \core\output\notification.
|
||||
*/
|
||||
public function redirect_message(
|
||||
$encodedurl,
|
||||
$message,
|
||||
$delay,
|
||||
$debugdisableredirect,
|
||||
$messagetype = notification::NOTIFY_INFO,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the start of an AJAX output.
|
||||
*/
|
||||
public function header() {
|
||||
// Unfortunately YUI iframe upload does not support application/json.
|
||||
if (!empty($_FILES)) {
|
||||
@header('Content-type: text/plain; charset=utf-8');
|
||||
if (!core_useragent::supports_json_contenttype()) {
|
||||
@header('X-Content-Type-Options: nosniff');
|
||||
}
|
||||
} else if (!core_useragent::supports_json_contenttype()) {
|
||||
@header('Content-type: text/plain; charset=utf-8');
|
||||
@header('X-Content-Type-Options: nosniff');
|
||||
} else {
|
||||
@header('Content-type: application/json; charset=utf-8');
|
||||
}
|
||||
|
||||
// Headers to make it not cacheable and json.
|
||||
@header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
@header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
@header('Pragma: no-cache');
|
||||
@header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
@header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
@header('Accept-Ranges: none');
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no footer for an AJAX request, however we must override the
|
||||
* footer method to prevent the default footer.
|
||||
*/
|
||||
public function footer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* No need for headers in an AJAX request... this should never happen.
|
||||
* @param string $text
|
||||
* @param int $level
|
||||
* @param string $classes
|
||||
* @param string $id
|
||||
*/
|
||||
public function heading($text, $level = 2, $classes = 'main', $id = null) {
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(core_renderer_ajax::class, \core_renderer_ajax::class);
|
||||
@@ -0,0 +1,260 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core_text;
|
||||
use core\check\result as check_result;
|
||||
|
||||
/**
|
||||
* A renderer that generates output for command-line scripts.
|
||||
*
|
||||
* The implementation of this renderer is probably incomplete.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class core_renderer_cli extends core_renderer {
|
||||
/**
|
||||
* @var array $progressmaximums stores the largest percentage for a progress bar.
|
||||
*/
|
||||
private $progressmaximums = [];
|
||||
|
||||
/**
|
||||
* Returns the page header.
|
||||
*
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public function header() {
|
||||
return $this->page->heading . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a Check API result
|
||||
*
|
||||
* To aid in CLI consistency this status is NOT translated and the visual
|
||||
* width is always exactly 10 chars.
|
||||
*
|
||||
* @param check_result $result
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
protected function render_check_result(check_result $result) {
|
||||
$status = $result->get_status();
|
||||
|
||||
$labels = [
|
||||
check_result::NA => ' ' . cli_ansi_format('<colour:darkGray>') . ' NA ',
|
||||
check_result::OK => ' ' . cli_ansi_format('<colour:green>') . ' OK ',
|
||||
check_result::INFO => ' ' . cli_ansi_format('<colour:blue>') . ' INFO ',
|
||||
check_result::UNKNOWN => ' ' . cli_ansi_format('<colour:darkGray>') . ' UNKNOWN ',
|
||||
check_result::WARNING => ' ' . cli_ansi_format('<colour:black><bgcolour:yellow>') . ' WARNING ',
|
||||
check_result::ERROR => ' ' . cli_ansi_format('<bgcolour:red>') . ' ERROR ',
|
||||
check_result::CRITICAL => '' . cli_ansi_format('<bgcolour:red>') . ' CRITICAL ',
|
||||
];
|
||||
$string = $labels[$status] . cli_ansi_format('<colour:normal>');
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a Check API result
|
||||
*
|
||||
* @param check_result $result
|
||||
* @return string fragment
|
||||
*/
|
||||
public function check_result(check_result $result) {
|
||||
return $this->render_check_result($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a progress bar.
|
||||
*
|
||||
* Do not use $OUTPUT->render($bar), instead use progress_bar::create().
|
||||
*
|
||||
* @param progress_bar $bar The bar.
|
||||
* @return string ascii fragment
|
||||
*/
|
||||
public function render_progress_bar(progress_bar $bar) {
|
||||
global $CFG;
|
||||
|
||||
$size = 55; // The width of the progress bar in chars.
|
||||
$ascii = "\n";
|
||||
|
||||
if (stream_isatty(STDOUT)) {
|
||||
require_once($CFG->libdir . '/clilib.php');
|
||||
|
||||
$ascii .= "[" . str_repeat(' ', $size) . "] 0% \n";
|
||||
return cli_ansi_format($ascii);
|
||||
}
|
||||
|
||||
$this->progressmaximums[$bar->get_id()] = 0;
|
||||
$ascii .= '[';
|
||||
return $ascii;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders an update to a progress bar.
|
||||
*
|
||||
* Note: This does not cleanly map to a renderable class and should
|
||||
* never be used directly.
|
||||
*
|
||||
* @param string $id
|
||||
* @param float $percent
|
||||
* @param string $msg Message
|
||||
* @param string $estimate time remaining message
|
||||
* @return string ascii fragment
|
||||
*/
|
||||
public function render_progress_bar_update(string $id, float $percent, string $msg, string $estimate): string {
|
||||
$size = 55; // The width of the progress bar in chars.
|
||||
$ascii = '';
|
||||
|
||||
// If we are rendering to a terminal then we can safely use ansii codes
|
||||
// to move the cursor and redraw the complete progress bar each time
|
||||
// it is updated.
|
||||
if (stream_isatty(STDOUT)) {
|
||||
$colour = $percent == 100 ? 'green' : 'blue';
|
||||
|
||||
$done = $percent * $size * 0.01;
|
||||
$whole = floor($done);
|
||||
$bar = "<colour:$colour>";
|
||||
$bar .= str_repeat('█', $whole);
|
||||
|
||||
if ($whole < $size) {
|
||||
// By using unicode chars for partial blocks we can have higher
|
||||
// precision progress bar.
|
||||
$fraction = floor(($done - $whole) * 8);
|
||||
$bar .= core_text::substr(' ▏▎▍▌▋▊▉', $fraction, 1);
|
||||
|
||||
// Fill the rest of the empty bar.
|
||||
$bar .= str_repeat(' ', $size - $whole - 1);
|
||||
}
|
||||
|
||||
$bar .= '<colour:normal>';
|
||||
|
||||
if ($estimate) {
|
||||
$estimate = "- $estimate";
|
||||
}
|
||||
|
||||
$ascii .= '<cursor:up>';
|
||||
$ascii .= '<cursor:up>';
|
||||
$ascii .= sprintf("[$bar] %3.1f%% %-22s\n", $percent, $estimate);
|
||||
$ascii .= sprintf("%-80s\n", $msg);
|
||||
return cli_ansi_format($ascii);
|
||||
}
|
||||
|
||||
// If we are not rendering to a tty, ie when piped to another command
|
||||
// or on windows we need to progressively render the progress bar
|
||||
// which can only ever go forwards.
|
||||
$done = round($percent * $size * 0.01);
|
||||
$delta = max(0, $done - $this->progressmaximums[$id]);
|
||||
|
||||
$ascii .= str_repeat('#', $delta);
|
||||
if ($percent >= 100 && $delta > 0) {
|
||||
$ascii .= sprintf("] %3.1f%%", $percent) . "\n$msg\n";
|
||||
}
|
||||
$this->progressmaximums[$id] += $delta;
|
||||
return $ascii;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a template fragment representing a Heading.
|
||||
*
|
||||
* @param string $text The text of the heading
|
||||
* @param int $level The level of importance of the heading
|
||||
* @param string $classes A space-separated list of CSS classes
|
||||
* @param string $id An optional ID
|
||||
* @return string A template fragment for a heading
|
||||
*/
|
||||
public function heading($text, $level = 2, $classes = 'main', $id = null) {
|
||||
$text .= "\n";
|
||||
switch ($level) {
|
||||
case 1:
|
||||
return '=>' . $text;
|
||||
case 2:
|
||||
return '-->' . $text;
|
||||
default:
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a template fragment representing a fatal error.
|
||||
*
|
||||
* @param string $message The message to output
|
||||
* @param string $moreinfourl URL where more info can be found about the error
|
||||
* @param string $link Link for the Continue button
|
||||
* @param array $backtrace The execution backtrace
|
||||
* @param null|string $debuginfo Debugging information
|
||||
* @param string $errorcode
|
||||
* @return string A template fragment for a fatal error
|
||||
*/
|
||||
public function fatal_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = "") {
|
||||
global $CFG;
|
||||
|
||||
$output = "!!! $message !!!\n";
|
||||
|
||||
if ($CFG->debugdeveloper) {
|
||||
if (!empty($debuginfo)) {
|
||||
$output .= $this->notification($debuginfo, 'notifytiny');
|
||||
}
|
||||
if (!empty($backtrace)) {
|
||||
$output .= $this->notification('Stack trace: ' . format_backtrace($backtrace, true), 'notifytiny');
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a template fragment representing a notification.
|
||||
*
|
||||
* @param string $message The message to print out.
|
||||
* @param string $type The type of notification. See constants on \core\output\notification.
|
||||
* @param bool $closebutton Whether to show a close icon to remove the notification (default true).
|
||||
* @return string A template fragment for a notification
|
||||
*/
|
||||
public function notification($message, $type = null, $closebutton = true) {
|
||||
$message = clean_text($message);
|
||||
if ($type === 'notifysuccess' || $type === 'success') {
|
||||
return "++ $message ++\n";
|
||||
}
|
||||
return "!! $message !!\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no footer for a cli request, however we must override the
|
||||
* footer method to prevent the default footer.
|
||||
*/
|
||||
public function footer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a notification (that is, a status message about something that has
|
||||
* just happened).
|
||||
*
|
||||
* @param notification $notification the notification to print out
|
||||
* @return string plain text output
|
||||
*/
|
||||
public function render_notification(notification $notification) {
|
||||
return $this->notification($notification->get_message(), $notification->get_message_type());
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(core_renderer_cli::class, \core_renderer_cli::class);
|
||||
@@ -0,0 +1,250 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core_block\output\block_contents;
|
||||
use core\exception\coding_exception;
|
||||
use moodle_page;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* The maintenance renderer.
|
||||
*
|
||||
* The purpose of this renderer is to block out the core renderer methods that are not usable when the site
|
||||
* is running a maintenance related task.
|
||||
* It must always extend the core_renderer as we switch from the core_renderer to this renderer in a couple of places.
|
||||
*
|
||||
* @since Moodle 2.6
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2013 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_renderer_maintenance extends core_renderer {
|
||||
/**
|
||||
* Initialises the renderer instance.
|
||||
*
|
||||
* @param moodle_page $page
|
||||
* @param string $target
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function __construct(moodle_page $page, $target) {
|
||||
if ($target !== RENDERER_TARGET_MAINTENANCE || $page->pagelayout !== 'maintenance') {
|
||||
throw new coding_exception('Invalid request for the maintenance renderer.');
|
||||
}
|
||||
parent::__construct($page, $target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce blocks.
|
||||
*
|
||||
* @param block_contents $bc
|
||||
* @param string $region
|
||||
* @return string
|
||||
*/
|
||||
public function block(block_contents $bc, $region) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce blocks.
|
||||
*
|
||||
* @param string $region
|
||||
* @param array $classes
|
||||
* @param string $tag
|
||||
* @param boolean $fakeblocksonly
|
||||
* @return string
|
||||
*/
|
||||
public function blocks($region, $classes = [], $tag = 'aside', $fakeblocksonly = false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce blocks.
|
||||
*
|
||||
* @param string $region
|
||||
* @param boolean $fakeblocksonly Output fake block only.
|
||||
* @return string
|
||||
*/
|
||||
public function blocks_for_region($region, $fakeblocksonly = false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce a course content header.
|
||||
*
|
||||
* @param bool $onlyifnotcalledbefore
|
||||
* @return string
|
||||
*/
|
||||
public function course_content_header($onlyifnotcalledbefore = false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce a course content footer.
|
||||
*
|
||||
* @param bool $onlyifnotcalledbefore
|
||||
* @return string
|
||||
*/
|
||||
public function course_content_footer($onlyifnotcalledbefore = false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce a course header.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function course_header() {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce a course footer.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function course_footer() {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce a custom menu.
|
||||
*
|
||||
* @param string $custommenuitems
|
||||
* @return string
|
||||
*/
|
||||
public function custom_menu($custommenuitems = '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce a file picker.
|
||||
*
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function file_picker($options) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden confirm message for upgrades.
|
||||
*
|
||||
* @param string $message The question to ask the user
|
||||
* @param single_button|moodle_url|string $continue The single_button component representing the Continue answer.
|
||||
* @param single_button|moodle_url|string $cancel The single_button component representing the Cancel answer.
|
||||
* @param array $displayoptions optional extra display options
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public function confirm($message, $continue, $cancel, array $displayoptions = []) {
|
||||
// We need plain styling of confirm boxes on upgrade because we don't know which stylesheet we have (it could be
|
||||
// from any previous version of Moodle).
|
||||
if ($continue instanceof single_button) {
|
||||
$continue->type = single_button::BUTTON_PRIMARY;
|
||||
} else if (is_string($continue)) {
|
||||
$continue = new single_button(
|
||||
new moodle_url($continue),
|
||||
get_string('continue'),
|
||||
'post',
|
||||
$displayoptions['type'] ?? single_button::BUTTON_PRIMARY
|
||||
);
|
||||
} else if ($continue instanceof moodle_url) {
|
||||
$continue = new single_button(
|
||||
$continue,
|
||||
get_string('continue'),
|
||||
'post',
|
||||
$displayoptions['type'] ?? single_button::BUTTON_PRIMARY
|
||||
);
|
||||
} else {
|
||||
throw new coding_exception('The continue param to $OUTPUT->confirm() must be either a URL' .
|
||||
' (string/moodle_url) or a single_button instance.');
|
||||
}
|
||||
|
||||
if ($cancel instanceof single_button) {
|
||||
$output = '';
|
||||
} else if (is_string($cancel)) {
|
||||
$cancel = new single_button(new moodle_url($cancel), get_string('cancel'), 'get');
|
||||
} else if ($cancel instanceof moodle_url) {
|
||||
$cancel = new single_button($cancel, get_string('cancel'), 'get');
|
||||
} else {
|
||||
throw new coding_exception('The cancel param to $OUTPUT->confirm() must be either a URL' .
|
||||
' (string/moodle_url) or a single_button instance.');
|
||||
}
|
||||
|
||||
$output = $this->box_start('generalbox', 'notice');
|
||||
$output .= html_writer::tag('h4', get_string('confirm'));
|
||||
$output .= html_writer::tag('p', $message);
|
||||
$output .= html_writer::tag('div', $this->render($cancel) . $this->render($continue), ['class' => 'buttons']);
|
||||
$output .= $this->box_end();
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer does not support JS.
|
||||
*
|
||||
* @param block_contents $bc
|
||||
*/
|
||||
public function init_block_hider_js(block_contents $bc) {
|
||||
// Does nothing.
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce language menus.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function lang_menu() {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer has no need for login information.
|
||||
*
|
||||
* @param mixed $withlinks
|
||||
* @return string
|
||||
*/
|
||||
public function login_info($withlinks = null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Secure login info.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function secure_login_info() {
|
||||
return $this->login_info(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. The maintenance renderer cannot produce user pictures.
|
||||
*
|
||||
* @param stdClass $user
|
||||
* @param null|array $options
|
||||
* @return string
|
||||
*/
|
||||
public function user_picture(stdClass $user, ?array $options = null) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(core_renderer_maintenance::class, \core_renderer_maintenance::class);
|
||||
@@ -0,0 +1,188 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\moodle_exception;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* Custom menu class
|
||||
*
|
||||
* This class is used to operate a custom menu that can be rendered for the page.
|
||||
* The custom menu is built using $CFG->custommenuitems and is a structured collection
|
||||
* of custom_menu_item nodes that can be rendered by the core renderer.
|
||||
*
|
||||
* To configure the custom menu:
|
||||
* Settings: Administration > Appearance > Advanced theme settings
|
||||
*
|
||||
* @copyright 2010 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class custom_menu extends custom_menu_item {
|
||||
/**
|
||||
* @var string The language we should render for, null disables multilang support.
|
||||
*/
|
||||
protected $currentlanguage = null;
|
||||
|
||||
/**
|
||||
* Creates the custom menu
|
||||
*
|
||||
* @param string $definition the menu items definition in syntax required by {@see convert_text_to_menu_nodes()}
|
||||
* @param string $currentlanguage the current language code, null disables multilang support
|
||||
*/
|
||||
public function __construct($definition = '', $currentlanguage = null) {
|
||||
$this->currentlanguage = $currentlanguage;
|
||||
parent::__construct('root'); // create virtual root element of the menu
|
||||
if (!empty($definition)) {
|
||||
$this->override_children(self::convert_text_to_menu_nodes($definition, $currentlanguage));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the children of this custom menu. Useful when getting children
|
||||
* from $CFG->custommenuitems
|
||||
*
|
||||
* @param array $children
|
||||
*/
|
||||
public function override_children(array $children) {
|
||||
$this->children = [];
|
||||
foreach ($children as $child) {
|
||||
if ($child instanceof custom_menu_item) {
|
||||
$this->children[] = $child;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string into a structured array of custom_menu_items which can
|
||||
* then be added to a custom menu.
|
||||
*
|
||||
* Structure:
|
||||
* text|url|title|langs
|
||||
* The number of hyphens at the start determines the depth of the item. The
|
||||
* languages are optional, comma separated list of languages the line is for.
|
||||
*
|
||||
* Example structure:
|
||||
* First level first item|http://www.moodle.com/
|
||||
* -Second level first item|http://www.moodle.com/partners/
|
||||
* -Second level second item|http://www.moodle.com/hq/
|
||||
* --Third level first item|http://www.moodle.com/jobs/
|
||||
* -Second level third item|http://www.moodle.com/development/
|
||||
* First level second item|http://www.moodle.com/feedback/
|
||||
* First level third item
|
||||
* English only|http://moodle.com|English only item|en
|
||||
* German only|http://moodle.de|Deutsch|de,de_du,de_kids
|
||||
*
|
||||
*
|
||||
* @static
|
||||
* @param string $text the menu items definition
|
||||
* @param string $language the language code, null disables multilang support
|
||||
* @return array
|
||||
*/
|
||||
public static function convert_text_to_menu_nodes($text, $language = null) {
|
||||
$root = new custom_menu();
|
||||
$lastitem = $root;
|
||||
$lastdepth = 0;
|
||||
$hiddenitems = [];
|
||||
$lines = explode("\n", $text);
|
||||
foreach ($lines as $linenumber => $line) {
|
||||
$line = trim($line);
|
||||
if (strlen($line) == 0) {
|
||||
continue;
|
||||
}
|
||||
// Parse item settings.
|
||||
$itemtext = null;
|
||||
$itemurl = null;
|
||||
$itemtitle = null;
|
||||
$itemvisible = true;
|
||||
$settings = explode('|', $line);
|
||||
foreach ($settings as $i => $setting) {
|
||||
$setting = trim($setting);
|
||||
if ($setting !== '') {
|
||||
switch ($i) {
|
||||
case 0: // Menu text.
|
||||
$itemtext = ltrim($setting, '-');
|
||||
break;
|
||||
case 1: // URL.
|
||||
try {
|
||||
$itemurl = new moodle_url($setting);
|
||||
} catch (moodle_exception $exception) {
|
||||
// We're not actually worried about this, we don't want to mess up the display
|
||||
// just for a wrongly entered URL.
|
||||
$itemurl = null;
|
||||
}
|
||||
break;
|
||||
case 2: // Title attribute.
|
||||
$itemtitle = $setting;
|
||||
break;
|
||||
case 3: // Language.
|
||||
if (!empty($language)) {
|
||||
$itemlanguages = array_map('trim', explode(',', $setting));
|
||||
$itemvisible &= in_array($language, $itemlanguages);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Get depth of new item.
|
||||
preg_match('/^(\-*)/', $line, $match);
|
||||
$itemdepth = strlen($match[1]) + 1;
|
||||
// Find parent item for new item.
|
||||
while (($lastdepth - $itemdepth) >= 0) {
|
||||
$lastitem = $lastitem->get_parent();
|
||||
$lastdepth--;
|
||||
}
|
||||
$lastitem = $lastitem->add($itemtext, $itemurl, $itemtitle, $linenumber + 1);
|
||||
$lastdepth++;
|
||||
if (!$itemvisible) {
|
||||
$hiddenitems[] = $lastitem;
|
||||
}
|
||||
}
|
||||
foreach ($hiddenitems as $item) {
|
||||
$item->parent->remove_child($item);
|
||||
}
|
||||
return $root->get_children();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts two custom menu items
|
||||
*
|
||||
* This function is designed to be used with the usort method
|
||||
* usort($this->children, array('custom_menu','sort_custom_menu_items'));
|
||||
*
|
||||
* @static
|
||||
* @param custom_menu_item $itema
|
||||
* @param custom_menu_item $itemb
|
||||
* @return int
|
||||
*/
|
||||
public static function sort_custom_menu_items(custom_menu_item $itema, custom_menu_item $itemb) {
|
||||
$itema = $itema->get_sort_order();
|
||||
$itemb = $itemb->get_sort_order();
|
||||
if ($itema == $itemb) {
|
||||
return 0;
|
||||
}
|
||||
return ($itema > $itemb) ? +1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(custom_menu::class, \custom_menu::class);
|
||||
@@ -0,0 +1,272 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\context\system as context_system;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Custom menu item
|
||||
*
|
||||
* This class is used to represent one item within a custom menu that may or may
|
||||
* not have children.
|
||||
*
|
||||
* @copyright 2010 Sam Hemelryk
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class custom_menu_item implements renderable, templatable {
|
||||
/**
|
||||
* @var string The text to show for the item
|
||||
*/
|
||||
protected $text;
|
||||
|
||||
/**
|
||||
* @var string A title to apply to the item. By default the text
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* @var int A sort order for the item, not necessary if you order things in
|
||||
* the CFG var.
|
||||
*/
|
||||
protected $sort;
|
||||
|
||||
/**
|
||||
* @var array A array in which to store children this item has.
|
||||
*/
|
||||
protected $children = [];
|
||||
|
||||
/**
|
||||
* @var int A reference to the sort var of the last child that was added
|
||||
*/
|
||||
protected $lastsort = 0;
|
||||
|
||||
/**
|
||||
* Constructs the new custom menu item
|
||||
*
|
||||
* @param string $text
|
||||
* @param null|moodle_url $url A moodle url to apply as the link for this item [Optional]
|
||||
* @param string $title A title to apply to this item [Optional]
|
||||
* @param int $sort A sort or to use if we need to sort differently [Optional]
|
||||
* @param null|custom_menu_item $parent A reference to the parent custom_menu_item this child
|
||||
* belongs to, only if the child has a parent. [Optional]
|
||||
* @param array $attributes Array of other HTML attributes for the custom menu item.
|
||||
*/
|
||||
public function __construct(
|
||||
$text,
|
||||
/** @var moodle_url The link to give the icon if it has no children */
|
||||
protected ?moodle_url $url = null,
|
||||
$title = null,
|
||||
$sort = null,
|
||||
/**
|
||||
* @var custom_menu_item A reference to the parent for this item or NULL if
|
||||
* it is a top level item
|
||||
*/
|
||||
protected ?custom_menu_item $parent = null,
|
||||
/** @var array Array of other HTML attributes for the custom menu item. */
|
||||
protected array $attributes = [],
|
||||
) {
|
||||
|
||||
// Use class setter method for text to ensure it's always a string type.
|
||||
$this->set_text($text);
|
||||
|
||||
$this->title = $title;
|
||||
$this->sort = (int)$sort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom menu item as a child of this node given its properties.
|
||||
*
|
||||
* @param string $text
|
||||
* @param null|moodle_url $url
|
||||
* @param string $title
|
||||
* @param int $sort
|
||||
* @param array $attributes Array of other HTML attributes for the custom menu item.
|
||||
* @return custom_menu_item
|
||||
*/
|
||||
public function add(
|
||||
$text,
|
||||
?moodle_url $url = null,
|
||||
$title = null,
|
||||
$sort = null,
|
||||
$attributes = [],
|
||||
) {
|
||||
$key = count($this->children);
|
||||
if (empty($sort)) {
|
||||
$sort = $this->lastsort + 1;
|
||||
}
|
||||
$this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this, $attributes);
|
||||
$this->lastsort = (int)$sort;
|
||||
return $this->children[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a custom menu item that is a child or descendant to the current menu.
|
||||
*
|
||||
* Returns true if child was found and removed.
|
||||
*
|
||||
* @param custom_menu_item $menuitem
|
||||
* @return bool
|
||||
*/
|
||||
public function remove_child(custom_menu_item $menuitem) {
|
||||
$removed = false;
|
||||
if (($key = array_search($menuitem, $this->children)) !== false) {
|
||||
unset($this->children[$key]);
|
||||
$this->children = array_values($this->children);
|
||||
$removed = true;
|
||||
} else {
|
||||
foreach ($this->children as $child) {
|
||||
if ($removed = $child->remove_child($menuitem)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text for this item
|
||||
* @return string
|
||||
*/
|
||||
public function get_text() {
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the url for this item
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the title for this item
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts and returns the children for this item
|
||||
* @return array
|
||||
*/
|
||||
public function get_children() {
|
||||
$this->sort();
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the sort order for this child
|
||||
* @return int
|
||||
*/
|
||||
public function get_sort_order() {
|
||||
return $this->sort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the parent this child belong to
|
||||
* @return custom_menu_item
|
||||
*/
|
||||
public function get_parent() {
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the children this item has
|
||||
*/
|
||||
public function sort() {
|
||||
usort($this->children, ['custom_menu', 'sort_custom_menu_items']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this item has any children
|
||||
* @return bool
|
||||
*/
|
||||
public function has_children() {
|
||||
return (count($this->children) > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text for the node
|
||||
* @param string $text
|
||||
*/
|
||||
public function set_text($text) {
|
||||
$this->text = (string)$text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the title for the node
|
||||
* @param string $title
|
||||
*/
|
||||
public function set_title($title) {
|
||||
$this->title = (string)$title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the url for the node
|
||||
* @param moodle_url $url
|
||||
*/
|
||||
public function set_url(moodle_url $url) {
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export this data so it can be used as the context for a mustache template.
|
||||
*
|
||||
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$syscontext = context_system::instance();
|
||||
|
||||
$context = new stdClass();
|
||||
$context->moremenuid = uniqid();
|
||||
$context->text = \core_external\util::format_string($this->text, $syscontext->id);
|
||||
$context->url = $this->url ? $this->url->out() : null;
|
||||
// No need for the title if it's the same with text.
|
||||
if ($this->text !== $this->title) {
|
||||
// Show the title attribute only if it's different from the text.
|
||||
$context->title = \core_external\util::format_string($this->title, $syscontext->id);
|
||||
}
|
||||
$context->sort = $this->sort;
|
||||
if (!empty($this->attributes)) {
|
||||
$context->attributes = $this->attributes;
|
||||
}
|
||||
$context->children = [];
|
||||
if (preg_match("/^#+$/", $this->text)) {
|
||||
$context->divider = true;
|
||||
}
|
||||
$context->haschildren = !empty($this->children) && (count($this->children) > 0);
|
||||
foreach ($this->children as $child) {
|
||||
$child = $child->export_for_template($output);
|
||||
array_push($context->children, $child);
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(custom_menu_item::class, \custom_menu_item::class);
|
||||
@@ -16,10 +16,8 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use context;
|
||||
use renderable;
|
||||
use core\context;
|
||||
use stdClass;
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* The filter renderable class.
|
||||
@@ -30,7 +28,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class datafilter implements renderable, templatable {
|
||||
|
||||
/** @var int None of the following match */
|
||||
public const JOINTYPE_NONE = 0;
|
||||
|
||||
@@ -110,7 +107,7 @@ abstract class datafilter implements renderable, templatable {
|
||||
'values' => $values,
|
||||
'filteroptions' => $filteroptions,
|
||||
'required' => $required,
|
||||
'joinlist' => json_encode($joinlist)
|
||||
'joinlist' => json_encode($joinlist),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ declare(strict_types=1);
|
||||
namespace core\output;
|
||||
|
||||
use core\output\dynamic_tabs\base;
|
||||
use renderer_base;
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* Class dynamic tabs
|
||||
@@ -30,7 +28,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class dynamic_tabs implements templatable {
|
||||
|
||||
/** @var base[] */
|
||||
protected $tabs = [];
|
||||
|
||||
@@ -62,7 +59,7 @@ class dynamic_tabs implements templatable {
|
||||
*/
|
||||
public function export_for_template(renderer_base $output): array {
|
||||
$data = [
|
||||
'tabs' => []
|
||||
'tabs' => [],
|
||||
];
|
||||
|
||||
foreach ($this->tabs as $tab) {
|
||||
|
||||
@@ -18,8 +18,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace core\output\dynamic_tabs;
|
||||
|
||||
use moodle_exception;
|
||||
use templatable;
|
||||
use core\exception\moodle_exception;
|
||||
use core\output\templatable;
|
||||
|
||||
/**
|
||||
* Class tab_base
|
||||
@@ -29,7 +29,6 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class base implements templatable {
|
||||
|
||||
/** @var array */
|
||||
protected $data;
|
||||
|
||||
|
||||
@@ -39,13 +39,12 @@ class external extends external_api {
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function load_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'),
|
||||
'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
|
||||
'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false)
|
||||
)
|
||||
);
|
||||
return new external_function_parameters([
|
||||
'component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
|
||||
'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
|
||||
'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
|
||||
'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,11 +59,15 @@ class external extends external_api {
|
||||
global $DB, $CFG, $PAGE;
|
||||
|
||||
$PAGE->set_context(context_system::instance());
|
||||
$params = self::validate_parameters(self::load_template_parameters(),
|
||||
array('component' => $component,
|
||||
'template' => $template,
|
||||
'themename' => $themename,
|
||||
'includecomments' => $includecomments));
|
||||
$params = self::validate_parameters(
|
||||
self::load_template_parameters(),
|
||||
[
|
||||
'component' => $component,
|
||||
'template' => $template,
|
||||
'themename' => $themename,
|
||||
'includecomments' => $includecomments,
|
||||
],
|
||||
);
|
||||
|
||||
$loader = new mustache_template_source_loader();
|
||||
// Will throw exceptions if the template does not exist.
|
||||
@@ -72,7 +75,7 @@ class external extends external_api {
|
||||
$params['component'],
|
||||
$params['template'],
|
||||
$params['themename'],
|
||||
$params['includecomments']
|
||||
$params['includecomments'],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -108,7 +111,7 @@ class external extends external_api {
|
||||
* @param string $themename The name of the current theme.
|
||||
* @param bool $includecomments Whether to strip comments from the template source.
|
||||
* @param string $lang moodle translation language, null means use current.
|
||||
* @return string the template
|
||||
* @return array the template
|
||||
*/
|
||||
public static function load_template_with_dependencies(
|
||||
string $component,
|
||||
@@ -126,7 +129,7 @@ class external extends external_api {
|
||||
'template' => $template,
|
||||
'themename' => $themename,
|
||||
'includecomments' => $includecomments,
|
||||
'lang' => $lang
|
||||
'lang' => $lang,
|
||||
]
|
||||
);
|
||||
|
||||
@@ -141,14 +144,14 @@ class external extends external_api {
|
||||
[],
|
||||
$params['lang']
|
||||
);
|
||||
$formatdependencies = function($dependency) {
|
||||
$formatdependencies = function ($dependency) {
|
||||
$results = [];
|
||||
foreach ($dependency as $dependencycomponent => $dependencyvalues) {
|
||||
foreach ($dependencyvalues as $dependencyname => $dependencyvalue) {
|
||||
array_push($results, [
|
||||
'component' => $dependencycomponent,
|
||||
'name' => $dependencyname,
|
||||
'value' => $dependencyvalue
|
||||
'value' => $dependencyvalue,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -159,7 +162,7 @@ class external extends external_api {
|
||||
// by external functions (because they don't support dynamic keys).
|
||||
return [
|
||||
'templates' => $formatdependencies($dependencies['templates']),
|
||||
'strings' => $formatdependencies($dependencies['strings'])
|
||||
'strings' => $formatdependencies($dependencies['strings']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -172,12 +175,12 @@ class external extends external_api {
|
||||
$resourcestructure = new external_single_structure([
|
||||
'component' => new external_value(PARAM_COMPONENT, 'component containing the resource'),
|
||||
'name' => new external_value(PARAM_TEXT, 'name of the resource'),
|
||||
'value' => new external_value(PARAM_RAW, 'resource value')
|
||||
'value' => new external_value(PARAM_RAW, 'resource value'),
|
||||
]);
|
||||
|
||||
return new external_single_structure([
|
||||
'templates' => new external_multiple_structure($resourcestructure),
|
||||
'strings' => new external_multiple_structure($resourcestructure)
|
||||
'strings' => new external_multiple_structure($resourcestructure),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\context\user as context_user;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Data structure representing a file picker.
|
||||
*
|
||||
* @copyright 2010 Dongsheng Cai
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class file_picker implements renderable {
|
||||
/**
|
||||
* @var stdClass An object containing options for the file picker
|
||||
*/
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* Constructs a file picker object.
|
||||
*
|
||||
* The following are possible options for the filepicker:
|
||||
* - accepted_types (*)
|
||||
* - return_types (FILE_INTERNAL)
|
||||
* - env (filepicker)
|
||||
* - client_id (uniqid)
|
||||
* - itemid (0)
|
||||
* - maxbytes (-1)
|
||||
* - maxfiles (1)
|
||||
* - buttonname (false)
|
||||
*
|
||||
* @param stdClass $options An object containing options for the file picker.
|
||||
*/
|
||||
public function __construct(stdClass $options) {
|
||||
global $CFG, $USER, $PAGE;
|
||||
require_once($CFG->dirroot . '/repository/lib.php');
|
||||
$defaults = [
|
||||
'accepted_types' => '*',
|
||||
'return_types' => FILE_INTERNAL,
|
||||
'env' => 'filepicker',
|
||||
'client_id' => uniqid(),
|
||||
'itemid' => 0,
|
||||
'maxbytes' => -1,
|
||||
'maxfiles' => 1,
|
||||
'buttonname' => false,
|
||||
];
|
||||
foreach ($defaults as $key => $value) {
|
||||
if (empty($options->$key)) {
|
||||
$options->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$options->currentfile = '';
|
||||
if (!empty($options->itemid)) {
|
||||
$fs = get_file_storage();
|
||||
$usercontext = context_user::instance($USER->id);
|
||||
if (empty($options->filename)) {
|
||||
if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) {
|
||||
$file = reset($files);
|
||||
}
|
||||
} else {
|
||||
$file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename);
|
||||
}
|
||||
if (!empty($file)) {
|
||||
$options->currentfile = html_writer::link(moodle_url::make_draftfile_url(
|
||||
$file->get_itemid(),
|
||||
$file->get_filepath(),
|
||||
$file->get_filename(),
|
||||
), $file->get_filename());
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise options, getting files in root path.
|
||||
$this->options = initialise_filepicker($options);
|
||||
|
||||
// Copying other options.
|
||||
foreach ($options as $name => $value) {
|
||||
if (!isset($this->options->$name)) {
|
||||
$this->options->$name = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(file_picker::class, \file_picker::class);
|
||||
@@ -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 core\output;
|
||||
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Data structure representing a help icon.
|
||||
*
|
||||
* @copyright 2010 Petr Skoda ([email protected])
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class help_icon implements renderable, templatable {
|
||||
/**
|
||||
* @var string lang pack identifier (without the "_help" suffix),
|
||||
* both get_string($identifier, $component) and get_string($identifier.'_help', $component)
|
||||
* must exist.
|
||||
*/
|
||||
public $identifier;
|
||||
|
||||
/**
|
||||
* @var string Component name, the same as in get_string()
|
||||
*/
|
||||
public $component;
|
||||
|
||||
/**
|
||||
* @var string Extra descriptive text next to the icon
|
||||
*/
|
||||
public $linktext = null;
|
||||
|
||||
/**
|
||||
* @var mixed An object, string or number that can be used within translation strings
|
||||
*/
|
||||
public $a = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $identifier string for help page title,
|
||||
* string with _help suffix is used for the actual help text.
|
||||
* string with _link suffix is used to create a link to further info (if it exists)
|
||||
* @param string $component
|
||||
* @param string|object|array|int $a An object, string or number that can be used
|
||||
* within translation strings
|
||||
*/
|
||||
public function __construct($identifier, $component, $a = null) {
|
||||
$this->identifier = $identifier;
|
||||
$this->component = $component;
|
||||
$this->a = $a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that both help strings exists, shows debug warnings if not
|
||||
*/
|
||||
public function diag_strings() {
|
||||
$sm = get_string_manager();
|
||||
if (!$sm->string_exists($this->identifier, $this->component)) {
|
||||
debugging("Help title string does not exist: [$this->identifier, $this->component]");
|
||||
}
|
||||
if (!$sm->string_exists($this->identifier . '_help', $this->component)) {
|
||||
debugging("Help contents string does not exist: [{$this->identifier}_help, $this->component]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export this data so it can be used as the context for a mustache template.
|
||||
*
|
||||
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
global $CFG;
|
||||
|
||||
$title = get_string($this->identifier, $this->component, $this->a);
|
||||
|
||||
if (empty($this->linktext)) {
|
||||
$alt = get_string('helpprefix2', '', trim($title, ". \t"));
|
||||
} else {
|
||||
$alt = get_string('helpwiththis');
|
||||
}
|
||||
|
||||
$data = get_formatted_help_string($this->identifier, $this->component, false, $this->a);
|
||||
|
||||
$data->alt = $alt;
|
||||
$data->icon = (new pix_icon('help', $alt, 'core', ['class' => 'iconhelp']))->export_for_template($output);
|
||||
$data->linktext = $this->linktext;
|
||||
$data->title = get_string('helpprefix2', '', trim($title, ". \t"));
|
||||
|
||||
$options = [
|
||||
'component' => $this->component,
|
||||
'identifier' => $this->identifier,
|
||||
'lang' => current_language(),
|
||||
];
|
||||
|
||||
// Debugging feature lets you display string identifier and component.
|
||||
if (isset($CFG->debugstringids) && $CFG->debugstringids && optional_param('strings', 0, PARAM_INT)) {
|
||||
$options['strings'] = 1;
|
||||
}
|
||||
|
||||
$data->url = (new moodle_url('/help.php', $options))->out(false);
|
||||
$data->ltr = !right_to_left();
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(help_icon::class, \help_icon::class);
|
||||
@@ -0,0 +1,891 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use core_table\output\html_table;
|
||||
use core_table\output\html_table_cell;
|
||||
use core_table\output\html_table_row;
|
||||
use core_text;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* Simple html output class
|
||||
*
|
||||
* @copyright 2009 Tim Hunt, 2010 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class html_writer {
|
||||
/**
|
||||
* Outputs a tag with attributes and contents
|
||||
*
|
||||
* @param string $tagname The name of tag ('a', 'img', 'span' etc.)
|
||||
* @param string $contents What goes between the opening and closing tags
|
||||
* @param null|array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function tag($tagname, $contents, ?array $attributes = null) {
|
||||
return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs an opening tag with attributes
|
||||
*
|
||||
* @param string $tagname The name of tag ('a', 'img', 'span' etc.)
|
||||
* @param null|array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function start_tag($tagname, ?array $attributes = null) {
|
||||
return '<' . $tagname . self::attributes($attributes) . '>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a closing tag
|
||||
*
|
||||
* @param string $tagname The name of tag ('a', 'img', 'span' etc.)
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function end_tag($tagname) {
|
||||
return '</' . $tagname . '>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs an empty tag with attributes
|
||||
*
|
||||
* @param string $tagname The name of tag ('input', 'img', 'br' etc.)
|
||||
* @param null|array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function empty_tag($tagname, ?array $attributes = null) {
|
||||
return '<' . $tagname . self::attributes($attributes) . ' />';
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a tag, but only if the contents are not empty
|
||||
*
|
||||
* @param string $tagname The name of tag ('a', 'img', 'span' etc.)
|
||||
* @param string $contents What goes between the opening and closing tags
|
||||
* @param null|array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function nonempty_tag($tagname, $contents, ?array $attributes = null) {
|
||||
if ($contents === '' || is_null($contents)) {
|
||||
return '';
|
||||
}
|
||||
return self::tag($tagname, $contents, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a HTML attribute and value
|
||||
*
|
||||
* @param string $name The name of the attribute ('src', 'href', 'class' etc.)
|
||||
* @param string $value The value of the attribute. The value will be escaped with {@see s()}
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function attribute($name, $value) {
|
||||
if ($value instanceof moodle_url) {
|
||||
return ' ' . $name . '="' . $value->out() . '"';
|
||||
}
|
||||
|
||||
// Special case, we do not want these in output.
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// No sloppy trimming here!
|
||||
return ' ' . $name . '="' . s($value) . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a list of HTML attributes and values
|
||||
*
|
||||
* @param null|array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
||||
* The values will be escaped with {@see s()}
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function attributes(?array $attributes = null) {
|
||||
$attributes = (array)$attributes;
|
||||
$output = '';
|
||||
foreach ($attributes as $name => $value) {
|
||||
$output .= self::attribute($name, $value);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a simple image tag with attributes.
|
||||
*
|
||||
* @param string $src The source of image
|
||||
* @param string $alt The alternate text for image
|
||||
* @param null|array $attributes The tag attributes (array('height' => $max_height, 'class' => 'class1') etc.)
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function img($src, $alt, ?array $attributes = null) {
|
||||
$attributes = (array)$attributes;
|
||||
$attributes['src'] = $src;
|
||||
// In case a null alt text is provided, set it to an empty string.
|
||||
$attributes['alt'] = $alt ?? '';
|
||||
if (array_key_exists('role', $attributes) && core_text::strtolower($attributes['role']) === 'presentation') {
|
||||
// A presentation role is not necessary for the img tag.
|
||||
// If a non-empty alt text is provided, the presentation role will conflict with the alt text.
|
||||
// An empty alt text denotes a decorative image. The presence of a presentation role is redundant.
|
||||
unset($attributes['role']);
|
||||
debugging('The presentation role is not necessary for an img tag.', DEBUG_DEVELOPER);
|
||||
}
|
||||
|
||||
return self::empty_tag('img', $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates random html element id.
|
||||
*
|
||||
* @staticvar int $counter
|
||||
* @staticvar string $uniq
|
||||
* @param string $base A string fragment that will be included in the random ID.
|
||||
* @return string A unique ID
|
||||
*/
|
||||
public static function random_id($base = 'random') {
|
||||
static $counter = 0;
|
||||
static $uniq;
|
||||
|
||||
if (!isset($uniq)) {
|
||||
$uniq = uniqid();
|
||||
}
|
||||
|
||||
$counter++;
|
||||
return $base . $uniq . $counter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a simple html link
|
||||
*
|
||||
* @param string|moodle_url $url The URL
|
||||
* @param string $text The text
|
||||
* @param null|array $attributes HTML attributes
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function link($url, $text, ?array $attributes = null) {
|
||||
$attributes = (array)$attributes;
|
||||
$attributes['href'] = $url;
|
||||
return self::tag('a', $text, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a simple checkbox with optional label
|
||||
*
|
||||
* @param string $name The name of the checkbox
|
||||
* @param string $value The value of the checkbox
|
||||
* @param bool $checked Whether the checkbox is checked
|
||||
* @param string $label The label for the checkbox
|
||||
* @param null|array $attributes Any attributes to apply to the checkbox
|
||||
* @param null|array $labelattributes Any attributes to apply to the label, if present
|
||||
* @return string html fragment
|
||||
*/
|
||||
public static function checkbox(
|
||||
$name,
|
||||
$value,
|
||||
$checked = true,
|
||||
$label = '',
|
||||
?array $attributes = null,
|
||||
?array $labelattributes = null,
|
||||
) {
|
||||
$attributes = (array) $attributes;
|
||||
$output = '';
|
||||
|
||||
if ($label !== '' && !is_null($label)) {
|
||||
if (empty($attributes['id'])) {
|
||||
$attributes['id'] = self::random_id('checkbox_');
|
||||
}
|
||||
}
|
||||
$attributes['type'] = 'checkbox';
|
||||
$attributes['value'] = $value;
|
||||
$attributes['name'] = $name;
|
||||
$attributes['checked'] = $checked ? 'checked' : null;
|
||||
|
||||
$output .= self::empty_tag('input', $attributes);
|
||||
|
||||
if ($label !== '' && !is_null($label)) {
|
||||
$labelattributes = (array) $labelattributes;
|
||||
$labelattributes['for'] = $attributes['id'];
|
||||
$output .= self::tag('label', $label, $labelattributes);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a simple select yes/no form field
|
||||
*
|
||||
* @param string $name name of select element
|
||||
* @param bool $selected
|
||||
* @param null|array $attributes - html select element attributes
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function select_yes_no($name, $selected = true, ?array $attributes = null) {
|
||||
$options = ['1' => get_string('yes'), '0' => get_string('no')];
|
||||
return self::select($options, $name, $selected, null, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a simple select form field
|
||||
*
|
||||
* Note this function does HTML escaping on the optgroup labels, but not on the choice labels.
|
||||
*
|
||||
* @param array $options associative array value=>label ex.:
|
||||
* array(1=>'One, 2=>Two)
|
||||
* it is also possible to specify optgroup as complex label array ex.:
|
||||
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
|
||||
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
|
||||
* @param string $name name of select element
|
||||
* @param string|array $selected value or array of values depending on multiple attribute
|
||||
* @param array|bool|null $nothing add nothing selected option, or false of not added
|
||||
* @param null|array $attributes html select element attributes
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function select(
|
||||
array $options,
|
||||
$name,
|
||||
$selected = '',
|
||||
$nothing = ['' => 'choosedots'],
|
||||
?array $attributes = null,
|
||||
) {
|
||||
$attributes = (array)$attributes;
|
||||
if (is_array($nothing)) {
|
||||
foreach ($nothing as $k => $v) {
|
||||
if ($v === 'choose' || $v === 'choosedots') {
|
||||
$nothing[$k] = get_string('choosedots');
|
||||
}
|
||||
}
|
||||
$options = $nothing + $options; // Keep keys, do not override.
|
||||
} else if (is_string($nothing) && $nothing !== '') {
|
||||
// BC.
|
||||
$options = ['' => $nothing] + $options;
|
||||
}
|
||||
|
||||
// We may accept more values if multiple attribute specified.
|
||||
$selected = (array)$selected;
|
||||
foreach ($selected as $k => $v) {
|
||||
$selected[$k] = (string)$v;
|
||||
}
|
||||
|
||||
if (!isset($attributes['id'])) {
|
||||
$id = 'menu' . $name;
|
||||
// Name may contain [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading.
|
||||
$id = str_replace('[', '', $id);
|
||||
$id = str_replace(']', '', $id);
|
||||
$attributes['id'] = $id;
|
||||
}
|
||||
|
||||
if (!isset($attributes['class'])) {
|
||||
$class = 'menu' . $name;
|
||||
// Name may contain [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading.
|
||||
$class = str_replace('[', '', $class);
|
||||
$class = str_replace(']', '', $class);
|
||||
$attributes['class'] = $class;
|
||||
}
|
||||
$attributes['class'] = 'select custom-select ' . $attributes['class']; // Add 'select' selector always.
|
||||
|
||||
$attributes['name'] = $name;
|
||||
|
||||
if (!empty($attributes['disabled'])) {
|
||||
$attributes['disabled'] = 'disabled';
|
||||
} else {
|
||||
unset($attributes['disabled']);
|
||||
}
|
||||
|
||||
$output = '';
|
||||
foreach ($options as $value => $label) {
|
||||
if (is_array($label)) {
|
||||
// Ignore key, it just has to be unique.
|
||||
$output .= self::select_optgroup(key($label), current($label), $selected);
|
||||
} else {
|
||||
$output .= self::select_option($label, $value, $selected);
|
||||
}
|
||||
}
|
||||
return self::tag('select', $output, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTML to display a select box option.
|
||||
*
|
||||
* @param string $label The label to display as the option.
|
||||
* @param string|int $value The value the option represents
|
||||
* @param array $selected An array of selected options
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
private static function select_option($label, $value, array $selected) {
|
||||
$attributes = [];
|
||||
$value = (string)$value;
|
||||
if (in_array($value, $selected, true)) {
|
||||
$attributes['selected'] = 'selected';
|
||||
}
|
||||
$attributes['value'] = $value;
|
||||
return self::tag('option', $label, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTML to display a select box option group.
|
||||
*
|
||||
* @param string $groupname The label to use for the group
|
||||
* @param array $options The options in the group
|
||||
* @param array $selected An array of selected values.
|
||||
* @return string HTML fragment.
|
||||
*/
|
||||
private static function select_optgroup($groupname, $options, array $selected) {
|
||||
if (empty($options)) {
|
||||
return '';
|
||||
}
|
||||
$attributes = ['label' => $groupname];
|
||||
$output = '';
|
||||
foreach ($options as $value => $label) {
|
||||
$output .= self::select_option($label, $value, $selected);
|
||||
}
|
||||
return self::tag('optgroup', $output, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a shortcut for making an hour selector menu.
|
||||
*
|
||||
* @param string $type The type of selector (years, months, days, hours, minutes)
|
||||
* @param string $name fieldname
|
||||
* @param int $currenttime A default timestamp in GMT
|
||||
* @param int $step minute spacing
|
||||
* @param null|array $attributes - html select element attributes
|
||||
* @param float|int|string $timezone the timezone to use to calculate the time
|
||||
* {@link https://moodledev.io/docs/apis/subsystems/time#timezone}
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function select_time($type, $name, $currenttime = 0, $step = 5, ?array $attributes = null, $timezone = 99) {
|
||||
global $OUTPUT;
|
||||
|
||||
if (!$currenttime) {
|
||||
$currenttime = time();
|
||||
}
|
||||
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
||||
$currentdate = $calendartype->timestamp_to_date_array($currenttime, $timezone);
|
||||
|
||||
$userdatetype = $type;
|
||||
$timeunits = [];
|
||||
|
||||
switch ($type) {
|
||||
case 'years':
|
||||
$timeunits = $calendartype->get_years();
|
||||
$userdatetype = 'year';
|
||||
break;
|
||||
case 'months':
|
||||
$timeunits = $calendartype->get_months();
|
||||
$userdatetype = 'month';
|
||||
$currentdate['month'] = (int)$currentdate['mon'];
|
||||
break;
|
||||
case 'days':
|
||||
$timeunits = $calendartype->get_days();
|
||||
$userdatetype = 'mday';
|
||||
break;
|
||||
case 'hours':
|
||||
for ($i = 0; $i <= 23; $i++) {
|
||||
$timeunits[$i] = sprintf("%02d", $i);
|
||||
}
|
||||
break;
|
||||
case 'minutes':
|
||||
if ($step != 1) {
|
||||
$currentdate['minutes'] = ceil($currentdate['minutes'] / $step) * $step;
|
||||
}
|
||||
|
||||
for ($i = 0; $i <= 59; $i += $step) {
|
||||
$timeunits[$i] = sprintf("%02d", $i);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
|
||||
}
|
||||
|
||||
$attributes = (array) $attributes;
|
||||
$data = (object) [
|
||||
'name' => $name,
|
||||
'id' => !empty($attributes['id']) ? $attributes['id'] : self::random_id('ts_'),
|
||||
'label' => get_string(substr($type, 0, -1), 'form'),
|
||||
'options' => array_map(function ($value) use ($timeunits, $currentdate, $userdatetype) {
|
||||
return [
|
||||
'name' => $timeunits[$value],
|
||||
'value' => $value,
|
||||
'selected' => $currentdate[$userdatetype] == $value,
|
||||
];
|
||||
}, array_keys($timeunits)),
|
||||
];
|
||||
|
||||
unset($attributes['id']);
|
||||
unset($attributes['name']);
|
||||
$data->attributes = array_map(function ($name) use ($attributes) {
|
||||
return [
|
||||
'name' => $name,
|
||||
'value' => $attributes[$name],
|
||||
];
|
||||
}, array_keys($attributes));
|
||||
|
||||
return $OUTPUT->render_from_template('core/select_time', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for quick making of lists
|
||||
*
|
||||
* Note: 'list' is a reserved keyword ;-)
|
||||
*
|
||||
* @param array $items
|
||||
* @param null|array $attributes
|
||||
* @param string $tag ul or ol
|
||||
* @return string
|
||||
*/
|
||||
public static function alist(array $items, ?array $attributes = null, $tag = 'ul') {
|
||||
$output = self::start_tag($tag, $attributes) . "\n";
|
||||
foreach ($items as $item) {
|
||||
$output .= self::tag('li', $item) . "\n";
|
||||
}
|
||||
$output .= self::end_tag($tag);
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns hidden input fields created from url parameters.
|
||||
*
|
||||
* @param moodle_url $url
|
||||
* @param null|array $exclude list of excluded parameters
|
||||
* @return string HTML fragment
|
||||
*/
|
||||
public static function input_hidden_params(moodle_url $url, ?array $exclude = null) {
|
||||
$exclude = (array)$exclude;
|
||||
$params = $url->params();
|
||||
foreach ($exclude as $key) {
|
||||
unset($params[$key]);
|
||||
}
|
||||
|
||||
$output = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$attributes = ['type' => 'hidden', 'name' => $key, 'value' => $value];
|
||||
$output .= self::empty_tag('input', $attributes) . "\n";
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a script tag containing the the specified code.
|
||||
*
|
||||
* @param string $jscode the JavaScript code
|
||||
* @param moodle_url|string $url optional url of the external script, $code ignored if specified
|
||||
* @return string HTML, the code wrapped in <script> tags.
|
||||
*/
|
||||
public static function script($jscode, $url = null) {
|
||||
if ($jscode) {
|
||||
return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n") . "\n";
|
||||
} else if ($url) {
|
||||
return self::tag('script', '', ['src' => $url]) . "\n";
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders HTML table
|
||||
*
|
||||
* This method may modify the passed instance by adding some default properties if they are not set yet.
|
||||
* If this is not what you want, you should make a full clone of your data before passing them to this
|
||||
* method. In most cases this is not an issue at all so we do not clone by default for performance
|
||||
* and memory consumption reasons.
|
||||
*
|
||||
* @param html_table $table data to be rendered
|
||||
* @return string HTML code
|
||||
*/
|
||||
public static function table(html_table $table) {
|
||||
// Prepare table data and populate missing properties with reasonable defaults.
|
||||
if (!empty($table->align)) {
|
||||
foreach ($table->align as $key => $aa) {
|
||||
if ($aa) {
|
||||
$table->align[$key] = 'text-align:' . fix_align_rtl($aa) . ';'; // Fix for RTL languages.
|
||||
} else {
|
||||
$table->align[$key] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($table->size)) {
|
||||
foreach ($table->size as $key => $ss) {
|
||||
if ($ss) {
|
||||
$table->size[$key] = 'width:' . $ss . ';';
|
||||
} else {
|
||||
$table->size[$key] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($table->wrap)) {
|
||||
foreach ($table->wrap as $key => $ww) {
|
||||
if ($ww) {
|
||||
$table->wrap[$key] = 'white-space:nowrap;';
|
||||
} else {
|
||||
$table->wrap[$key] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($table->head)) {
|
||||
foreach ($table->head as $key => $val) {
|
||||
if (!isset($table->align[$key])) {
|
||||
$table->align[$key] = null;
|
||||
}
|
||||
if (!isset($table->size[$key])) {
|
||||
$table->size[$key] = null;
|
||||
}
|
||||
if (!isset($table->wrap[$key])) {
|
||||
$table->wrap[$key] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($table->attributes['class'])) {
|
||||
$table->attributes['class'] = 'generaltable';
|
||||
}
|
||||
if (!empty($table->tablealign)) {
|
||||
$table->attributes['class'] .= ' boxalign' . $table->tablealign;
|
||||
}
|
||||
|
||||
// Explicitly assigned properties override those defined via $table->attributes.
|
||||
$table->attributes['class'] = trim($table->attributes['class']);
|
||||
$attributes = array_merge($table->attributes, [
|
||||
'id' => $table->id,
|
||||
'width' => $table->width,
|
||||
'summary' => $table->summary,
|
||||
'cellpadding' => $table->cellpadding,
|
||||
'cellspacing' => $table->cellspacing,
|
||||
]);
|
||||
$output = self::start_tag('table', $attributes) . "\n";
|
||||
|
||||
$countcols = 0;
|
||||
|
||||
// Output a caption if present.
|
||||
if (!empty($table->caption)) {
|
||||
$captionattributes = [];
|
||||
if ($table->captionhide) {
|
||||
$captionattributes['class'] = 'accesshide';
|
||||
}
|
||||
$output .= self::tag(
|
||||
'caption',
|
||||
$table->caption,
|
||||
$captionattributes
|
||||
);
|
||||
}
|
||||
|
||||
if (!empty($table->head)) {
|
||||
$countcols = count($table->head);
|
||||
|
||||
$output .= self::start_tag('thead', []) . "\n";
|
||||
$output .= self::start_tag('tr', []) . "\n";
|
||||
$keys = array_keys($table->head);
|
||||
$lastkey = end($keys);
|
||||
|
||||
foreach ($table->head as $key => $heading) {
|
||||
// Convert plain string headings into html_table_cell objects.
|
||||
if (!($heading instanceof html_table_cell)) {
|
||||
$headingtext = $heading;
|
||||
$heading = new html_table_cell();
|
||||
$heading->text = $headingtext;
|
||||
$heading->header = true;
|
||||
}
|
||||
|
||||
if ($heading->header !== false) {
|
||||
$heading->header = true;
|
||||
}
|
||||
|
||||
$tagtype = 'td';
|
||||
if ($heading->header && (string)$heading->text != '') {
|
||||
$tagtype = 'th';
|
||||
}
|
||||
|
||||
$heading->attributes['class'] .= ' header c' . $key;
|
||||
if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
|
||||
$heading->colspan = $table->headspan[$key];
|
||||
$countcols += $table->headspan[$key] - 1;
|
||||
}
|
||||
|
||||
if ($key == $lastkey) {
|
||||
$heading->attributes['class'] .= ' lastcol';
|
||||
}
|
||||
if (isset($table->colclasses[$key])) {
|
||||
$heading->attributes['class'] .= ' ' . $table->colclasses[$key];
|
||||
}
|
||||
$heading->attributes['class'] = trim($heading->attributes['class']);
|
||||
$attributes = array_merge($heading->attributes, [
|
||||
'style' => $table->align[$key] . $table->size[$key] . $heading->style,
|
||||
'colspan' => $heading->colspan,
|
||||
]);
|
||||
|
||||
if ($tagtype == 'th') {
|
||||
$attributes['scope'] = !empty($heading->scope) ? $heading->scope : 'col';
|
||||
}
|
||||
|
||||
$output .= self::tag($tagtype, $heading->text, $attributes) . "\n";
|
||||
}
|
||||
$output .= self::end_tag('tr') . "\n";
|
||||
$output .= self::end_tag('thead') . "\n";
|
||||
|
||||
if (empty($table->data)) {
|
||||
// For valid XHTML strict every table must contain either a valid tr
|
||||
// or a valid tbody... both of which must contain a valid td.
|
||||
$output .= self::start_tag('tbody', ['class' => 'empty']);
|
||||
$output .= self::tag('tr', self::tag('td', '', ['colspan' => count($table->head)]));
|
||||
$output .= self::end_tag('tbody');
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($table->data)) {
|
||||
$keys = array_keys($table->data);
|
||||
$lastrowkey = end($keys);
|
||||
$output .= self::start_tag('tbody', []);
|
||||
|
||||
foreach ($table->data as $key => $row) {
|
||||
if (($row === 'hr') && ($countcols)) {
|
||||
$output .= self::tag('td', self::tag('div', '', ['class' => 'tabledivider']), ['colspan' => $countcols]);
|
||||
} else {
|
||||
// Convert array rows to html_table_rows and cell strings to html_table_cell objects.
|
||||
if (!($row instanceof html_table_row)) {
|
||||
$newrow = new html_table_row();
|
||||
|
||||
foreach ($row as $cell) {
|
||||
if (!($cell instanceof html_table_cell)) {
|
||||
$cell = new html_table_cell($cell);
|
||||
}
|
||||
$newrow->cells[] = $cell;
|
||||
}
|
||||
$row = $newrow;
|
||||
}
|
||||
|
||||
if (isset($table->rowclasses[$key])) {
|
||||
$row->attributes['class'] .= ' ' . $table->rowclasses[$key];
|
||||
}
|
||||
|
||||
if ($key == $lastrowkey) {
|
||||
$row->attributes['class'] .= ' lastrow';
|
||||
}
|
||||
|
||||
// Explicitly assigned properties should override those defined in the attributes.
|
||||
$row->attributes['class'] = trim($row->attributes['class']);
|
||||
$trattributes = array_merge($row->attributes, [
|
||||
'id' => $row->id,
|
||||
'style' => $row->style,
|
||||
]);
|
||||
$output .= self::start_tag('tr', $trattributes) . "\n";
|
||||
$keys2 = array_keys($row->cells);
|
||||
$lastkey = end($keys2);
|
||||
|
||||
$gotlastkey = false; // Flag for sanity checking.
|
||||
foreach ($row->cells as $key => $cell) {
|
||||
if ($gotlastkey) {
|
||||
// This should never happen. Why do we have a cell after the last cell?
|
||||
mtrace("A cell with key ($key) was found after the last key ($lastkey)");
|
||||
}
|
||||
|
||||
if (!($cell instanceof html_table_cell)) {
|
||||
$mycell = new html_table_cell();
|
||||
$mycell->text = $cell;
|
||||
$cell = $mycell;
|
||||
}
|
||||
|
||||
if (($cell->header === true) && empty($cell->scope)) {
|
||||
$cell->scope = 'row';
|
||||
}
|
||||
|
||||
if (isset($table->colclasses[$key])) {
|
||||
$cell->attributes['class'] .= ' ' . $table->colclasses[$key];
|
||||
}
|
||||
|
||||
$cell->attributes['class'] .= ' cell c' . $key;
|
||||
if ($key == $lastkey) {
|
||||
$cell->attributes['class'] .= ' lastcol';
|
||||
$gotlastkey = true;
|
||||
}
|
||||
$tdstyle = '';
|
||||
$tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
|
||||
$tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
|
||||
$tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
|
||||
$cell->attributes['class'] = trim($cell->attributes['class']);
|
||||
$tdattributes = array_merge($cell->attributes, [
|
||||
'style' => $tdstyle . $cell->style,
|
||||
'colspan' => $cell->colspan,
|
||||
'rowspan' => $cell->rowspan,
|
||||
'id' => $cell->id,
|
||||
'abbr' => $cell->abbr,
|
||||
'scope' => $cell->scope,
|
||||
]);
|
||||
$tagtype = 'td';
|
||||
if ($cell->header === true) {
|
||||
$tagtype = 'th';
|
||||
}
|
||||
$output .= self::tag($tagtype, $cell->text, $tdattributes) . "\n";
|
||||
}
|
||||
}
|
||||
$output .= self::end_tag('tr') . "\n";
|
||||
}
|
||||
$output .= self::end_tag('tbody') . "\n";
|
||||
}
|
||||
$output .= self::end_tag('table') . "\n";
|
||||
|
||||
if ($table->responsive) {
|
||||
return self::div($output, 'table-responsive');
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders form element label
|
||||
*
|
||||
* By default, the label is suffixed with a label separator defined in the
|
||||
* current language pack (colon by default in the English lang pack).
|
||||
* Adding the colon can be explicitly disabled if needed. Label separators
|
||||
* are put outside the label tag itself so they are not read by
|
||||
* screenreaders (accessibility).
|
||||
*
|
||||
* Parameter $for explicitly associates the label with a form control. When
|
||||
* set, the value of this attribute must be the same as the value of
|
||||
* the id attribute of the form control in the same document. When null,
|
||||
* the label being defined is associated with the control inside the label
|
||||
* element.
|
||||
*
|
||||
* @param string $text content of the label tag
|
||||
* @param string|null $for id of the element this label is associated with, null for no association
|
||||
* @param bool $colonize add label separator (colon) to the label text, if it is not there yet
|
||||
* @param array $attributes to be inserted in the tab, for example array('accesskey' => 'a')
|
||||
* @return string HTML of the label element
|
||||
*/
|
||||
public static function label($text, $for, $colonize = true, array $attributes = []) {
|
||||
if (!is_null($for)) {
|
||||
$attributes = array_merge($attributes, ['for' => $for]);
|
||||
}
|
||||
$text = trim($text ?? '');
|
||||
$label = self::tag('label', $text, $attributes);
|
||||
|
||||
// TODO MDL-12192 $colonize disabled for now yet
|
||||
// if (!empty($text) and $colonize) {
|
||||
// the $text may end with the colon already, though it is bad string definition style
|
||||
// $colon = get_string('labelsep', 'langconfig');
|
||||
// if (!empty($colon)) {
|
||||
// $trimmed = trim($colon);
|
||||
// if ((substr($text, -strlen($trimmed)) == $trimmed) or (substr($text, -1) == ':')) {
|
||||
// debugging('The label text should not end with colon or other label separator,
|
||||
// please fix the string definition.', DEBUG_DEVELOPER);
|
||||
// } else {
|
||||
// $label .= $colon;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines a class parameter with other attributes. Aids in code reduction
|
||||
* because the class parameter is very frequently used.
|
||||
*
|
||||
* If the class attribute is specified both in the attributes and in the
|
||||
* class parameter, the two values are combined with a space between.
|
||||
*
|
||||
* @param string $class Optional CSS class (or classes as space-separated list)
|
||||
* @param null|array $attributes Optional other attributes as array
|
||||
* @return array Attributes (or null if still none)
|
||||
*/
|
||||
private static function add_class($class = '', ?array $attributes = null) {
|
||||
if ($class !== '') {
|
||||
$classattribute = ['class' => $class];
|
||||
if ($attributes) {
|
||||
if (array_key_exists('class', $attributes)) {
|
||||
$attributes['class'] = trim($attributes['class'] . ' ' . $class);
|
||||
} else {
|
||||
$attributes = $classattribute + $attributes;
|
||||
}
|
||||
} else {
|
||||
$attributes = $classattribute;
|
||||
}
|
||||
}
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a <div> tag. (Shortcut function.)
|
||||
*
|
||||
* @param string $content HTML content of tag
|
||||
* @param string $class Optional CSS class (or classes as space-separated list)
|
||||
* @param null|array $attributes Optional other attributes as array
|
||||
* @return string HTML code for div
|
||||
*/
|
||||
public static function div($content, $class = '', ?array $attributes = null) {
|
||||
return self::tag('div', $content, self::add_class($class, $attributes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a <div> tag. (Shortcut function.)
|
||||
*
|
||||
* @param string $class Optional CSS class (or classes as space-separated list)
|
||||
* @param null|array $attributes Optional other attributes as array
|
||||
* @return string HTML code for open div tag
|
||||
*/
|
||||
public static function start_div($class = '', ?array $attributes = null) {
|
||||
return self::start_tag('div', self::add_class($class, $attributes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a <div> tag. (Shortcut function.)
|
||||
*
|
||||
* @return string HTML code for close div tag
|
||||
*/
|
||||
public static function end_div() {
|
||||
return self::end_tag('div');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a <span> tag. (Shortcut function.)
|
||||
*
|
||||
* @param string $content HTML content of tag
|
||||
* @param string $class Optional CSS class (or classes as space-separated list)
|
||||
* @param null|array $attributes Optional other attributes as array
|
||||
* @return string HTML code for span
|
||||
*/
|
||||
public static function span($content, $class = '', ?array $attributes = null) {
|
||||
return self::tag('span', $content, self::add_class($class, $attributes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a <span> tag. (Shortcut function.)
|
||||
*
|
||||
* @param string $class Optional CSS class (or classes as space-separated list)
|
||||
* @param null|array $attributes Optional other attributes as array
|
||||
* @return string HTML code for open span tag
|
||||
*/
|
||||
public static function start_span($class = '', ?array $attributes = null) {
|
||||
return self::start_tag('span', self::add_class($class, $attributes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a <span> tag. (Shortcut function.)
|
||||
*
|
||||
* @return string HTML code for close span tag
|
||||
*/
|
||||
public static function end_span() {
|
||||
return self::end_tag('span');
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(html_writer::class, \html_writer::class);
|
||||
@@ -14,22 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Contains class \core\output\icon_system
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use renderer_base;
|
||||
use pix_icon;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class allowing different systems for mapping and rendering icons.
|
||||
*
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class allowing different systems for mapping and rendering icons.
|
||||
*
|
||||
@@ -41,6 +39,4 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class icon_system_font extends icon_system {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -14,22 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Contains class \core\output\icon_system
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use renderer_base;
|
||||
use pix_icon;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class allowing different systems for mapping and rendering icons.
|
||||
*
|
||||
@@ -44,7 +30,6 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class icon_system_fontawesome extends icon_system_font {
|
||||
|
||||
/**
|
||||
* @var array $map Cached map of moodle icon names to font awesome icon names.
|
||||
*/
|
||||
@@ -473,7 +458,7 @@ class icon_system_fontawesome extends icon_system_font {
|
||||
|
||||
// Create different mapping keys for different icon system classes, there may be several different
|
||||
// themes on the same site.
|
||||
$mapkey = 'mapping_'.preg_replace('/[^a-zA-Z0-9_]/', '_', get_class($this));
|
||||
$mapkey = 'mapping_' . preg_replace('/[^a-zA-Z0-9_]/', '_', get_class($this));
|
||||
$this->map = $cache->get($mapkey);
|
||||
|
||||
if (empty($this->map)) {
|
||||
@@ -490,16 +475,16 @@ class icon_system_fontawesome extends icon_system_font {
|
||||
}
|
||||
$cache->set($mapkey, $this->map);
|
||||
}
|
||||
|
||||
}
|
||||
return $this->map;
|
||||
}
|
||||
|
||||
|
||||
#[\Override]
|
||||
public function get_amd_name() {
|
||||
return 'core/icon_system_fontawesome';
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function render_pix_icon(renderer_base $output, pix_icon $icon) {
|
||||
$subtype = 'pix_icon_fontawesome';
|
||||
$subpix = new $subtype($icon);
|
||||
@@ -521,5 +506,4 @@ class icon_system_fontawesome extends icon_system_font {
|
||||
|
||||
return $output->render_from_template('core/pix_icon_fontawesome', $data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,22 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Contains class \core\output\icon_system_standard
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use renderer_base;
|
||||
use pix_icon;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Standard icon rendering. No mapping - img tags used.
|
||||
*
|
||||
@@ -39,15 +25,14 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class icon_system_standard extends icon_system {
|
||||
|
||||
#[\Override]
|
||||
public function render_pix_icon(renderer_base $output, pix_icon $icon) {
|
||||
$data = $icon->export_for_template($output);
|
||||
return $output->render_from_template('core/pix_icon', $data);
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function get_amd_name() {
|
||||
return 'core/icon_system_standard';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Data structure representing an activity icon.
|
||||
*
|
||||
* The difference is that activity icons will always render with the standard icon system (no font icons).
|
||||
*
|
||||
* @copyright 2017 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
*/
|
||||
class image_icon extends pix_icon {
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(image_icon::class, \image_icon::class);
|
||||
@@ -0,0 +1,157 @@
|
||||
<?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 core\output;
|
||||
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Component representing initials bar.
|
||||
*
|
||||
* @copyright 2017 Ilya Tregubov
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.3
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class initials_bar implements renderable, templatable {
|
||||
/**
|
||||
* @var string Currently selected letter.
|
||||
*/
|
||||
public $current;
|
||||
|
||||
/**
|
||||
* @var string Class name to add to this initial bar.
|
||||
*/
|
||||
public $class;
|
||||
|
||||
/**
|
||||
* @var string The name to put in front of this initial bar.
|
||||
*/
|
||||
public $title;
|
||||
|
||||
/**
|
||||
* @var string URL parameter name for this initial.
|
||||
*/
|
||||
public $urlvar;
|
||||
|
||||
/**
|
||||
* @var moodle_url URL object.
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* @var array An array of letters in the alphabet.
|
||||
*/
|
||||
public $alpha;
|
||||
|
||||
/**
|
||||
* @var bool Omit links if we are doing a mini render.
|
||||
*/
|
||||
public $minirender;
|
||||
|
||||
/**
|
||||
* Constructor initials_bar with only the required params.
|
||||
*
|
||||
* @param string $current the currently selected letter.
|
||||
* @param string $class class name to add to this initial bar.
|
||||
* @param string $title the name to put in front of this initial bar.
|
||||
* @param string $urlvar URL parameter name for this initial.
|
||||
* @param string $url URL object.
|
||||
* @param array $alpha of letters in the alphabet.
|
||||
* @param bool $minirender Return a trimmed down view of the initials bar.
|
||||
*/
|
||||
public function __construct($current, $class, $title, $urlvar, $url, $alpha = null, bool $minirender = false) {
|
||||
$this->current = $current;
|
||||
$this->class = $class;
|
||||
$this->title = $title;
|
||||
$this->urlvar = $urlvar;
|
||||
$this->url = $url;
|
||||
$this->alpha = $alpha;
|
||||
$this->minirender = $minirender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$data = new stdClass();
|
||||
|
||||
if ($this->alpha == null) {
|
||||
$this->alpha = explode(',', get_string('alphabet', 'langconfig'));
|
||||
}
|
||||
|
||||
if ($this->current == 'all') {
|
||||
$this->current = '';
|
||||
}
|
||||
|
||||
// We want to find a letter grouping size which suits the language so
|
||||
// find the largest group size which is less than 15 chars.
|
||||
// The choice of 15 chars is the largest number of chars that reasonably
|
||||
// fits on the smallest supported screen size. By always using a max number
|
||||
// of groups which is a factor of 2, we always get nice wrapping, and the
|
||||
// last row is always the shortest.
|
||||
$groupsize = count($this->alpha);
|
||||
$groups = 1;
|
||||
while ($groupsize > 15) {
|
||||
$groups *= 2;
|
||||
$groupsize = ceil(count($this->alpha) / $groups);
|
||||
}
|
||||
|
||||
$groupsizelimit = 0;
|
||||
$groupnumber = 0;
|
||||
foreach ($this->alpha as $letter) {
|
||||
if ($groupsizelimit++ > 0 && $groupsizelimit % $groupsize == 1) {
|
||||
$groupnumber++;
|
||||
}
|
||||
$groupletter = new stdClass();
|
||||
$groupletter->name = $letter;
|
||||
if (!$this->minirender) {
|
||||
$groupletter->url = $this->url->out(false, [$this->urlvar => $letter]);
|
||||
} else {
|
||||
$groupletter->input = $letter;
|
||||
}
|
||||
if ($letter == $this->current) {
|
||||
$groupletter->selected = $this->current;
|
||||
}
|
||||
if (!isset($data->group[$groupnumber])) {
|
||||
$data->group[$groupnumber] = new stdClass();
|
||||
}
|
||||
$data->group[$groupnumber]->letter[] = $groupletter;
|
||||
}
|
||||
|
||||
$data->class = $this->class;
|
||||
$data->title = $this->title;
|
||||
if (!$this->minirender) {
|
||||
$data->url = $this->url->out(false, [$this->urlvar => '']);
|
||||
} else {
|
||||
$data->input = 'ALL';
|
||||
}
|
||||
$data->current = $this->current;
|
||||
$data->all = get_string('all');
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(initials_bar::class, \initials_bar::class);
|
||||
@@ -14,21 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Contains class \core\output\inplace_editable
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use templatable;
|
||||
use renderable;
|
||||
use lang_string;
|
||||
use pix_icon;
|
||||
|
||||
/**
|
||||
* Class allowing to quick edit a title inline
|
||||
@@ -52,8 +40,7 @@ use pix_icon;
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class inplace_editable implements templatable, renderable {
|
||||
|
||||
class inplace_editable implements renderable, templatable {
|
||||
/**
|
||||
* @var string component responsible for diplsying/updating
|
||||
*/
|
||||
@@ -114,7 +101,8 @@ class inplace_editable implements templatable, renderable {
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $component name of the component or plugin responsible for the updating of the value (must declare callback)
|
||||
* @param string $itemtype type of the item inside the component - each component/plugin may implement multiple inplace-editable elements
|
||||
* @param string $itemtype type of the item inside the component
|
||||
* Each component/plugin may implement multiple inplace-editable elements.
|
||||
* @param int $itemid identifier of the item that can be edited in-place
|
||||
* @param bool $editable whether this value is editable (check capabilities and editing mode), if false, only "displayvalue"
|
||||
* will be displayed without anything else
|
||||
@@ -125,8 +113,17 @@ class inplace_editable implements templatable, renderable {
|
||||
* @param lang_string|string $editlabel label for the input element in the editing mode (for screenreaders)
|
||||
* @param pix_icon|null $editicon icon to use to toggle editing
|
||||
*/
|
||||
public function __construct($component, $itemtype, $itemid, $editable,
|
||||
$displayvalue, $value = null, $edithint = null, $editlabel = null, ?pix_icon $editicon = null) {
|
||||
public function __construct(
|
||||
$component,
|
||||
$itemtype,
|
||||
$itemid,
|
||||
$editable,
|
||||
$displayvalue,
|
||||
$value = null,
|
||||
$edithint = null,
|
||||
$editlabel = null,
|
||||
?pix_icon $editicon = null
|
||||
) {
|
||||
$this->component = $component;
|
||||
$this->itemtype = $itemtype;
|
||||
$this->itemid = $itemid;
|
||||
@@ -151,7 +148,7 @@ class inplace_editable implements templatable, renderable {
|
||||
*/
|
||||
public function set_type_toggle($options = null) {
|
||||
if ($options === null) {
|
||||
$options = array(0, 1);
|
||||
$options = [0, 1];
|
||||
}
|
||||
$options = array_values($options);
|
||||
$idx = array_search($this->value, $options, true);
|
||||
@@ -248,16 +245,16 @@ class inplace_editable implements templatable, renderable {
|
||||
*/
|
||||
public function export_for_template(\renderer_base $output) {
|
||||
if (!$this->editable) {
|
||||
return array(
|
||||
'displayvalue' => (string)$this->displayvalue
|
||||
);
|
||||
return [
|
||||
'displayvalue' => (string)$this->displayvalue,
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->editicon === null) {
|
||||
$this->editicon = new pix_icon('t/editstring', (string) $this->edithint);
|
||||
}
|
||||
|
||||
return array(
|
||||
return [
|
||||
'component' => $this->component,
|
||||
'itemtype' => $this->itemtype,
|
||||
'itemid' => $this->itemid,
|
||||
@@ -269,7 +266,7 @@ class inplace_editable implements templatable, renderable {
|
||||
'type' => $this->type,
|
||||
'options' => $this->options,
|
||||
'linkeverything' => $this->get_linkeverything() ? 1 : 0,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Simple javascript output class
|
||||
*
|
||||
* @copyright 2010 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class js_writer {
|
||||
/**
|
||||
* Returns javascript code calling the function
|
||||
*
|
||||
* @param string $function function name, can be complex like Y.Event.purgeElement
|
||||
* @param null|array $arguments parameters
|
||||
* @param int $delay execution delay in seconds
|
||||
* @return string JS code fragment
|
||||
*/
|
||||
public static function function_call($function, ?array $arguments = null, $delay = 0) {
|
||||
if ($arguments) {
|
||||
$arguments = array_map('json_encode', convert_to_array($arguments));
|
||||
$arguments = implode(', ', $arguments);
|
||||
} else {
|
||||
$arguments = '';
|
||||
}
|
||||
$js = "$function($arguments);";
|
||||
|
||||
if ($delay) {
|
||||
$delay = $delay * 1000; // Delay in miliseconds.
|
||||
$js = "setTimeout(function() { $js }, $delay);";
|
||||
}
|
||||
return $js . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Special function which adds Y as first argument of function call.
|
||||
*
|
||||
* @param string $function The function to call
|
||||
* @param null|array $extraarguments Any arguments to pass to it
|
||||
* @return string Some JS code
|
||||
*/
|
||||
public static function function_call_with_y($function, ?array $extraarguments = null) {
|
||||
if ($extraarguments) {
|
||||
$extraarguments = array_map('json_encode', convert_to_array($extraarguments));
|
||||
$arguments = 'Y, ' . implode(', ', $extraarguments);
|
||||
} else {
|
||||
$arguments = 'Y';
|
||||
}
|
||||
return "$function($arguments);\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns JavaScript code to initialise a new object
|
||||
*
|
||||
* @param string $var If it is null then no var is assigned the new object.
|
||||
* @param string $class The class to initialise an object for.
|
||||
* @param null|array $arguments An array of args to pass to the init method.
|
||||
* @param null|array $requirements Any modules required for this class.
|
||||
* @param int $delay The delay before initialisation. 0 = no delay.
|
||||
* @return string Some JS code
|
||||
*/
|
||||
public static function object_init($var, $class, ?array $arguments = null, ?array $requirements = null, $delay = 0) {
|
||||
if (is_array($arguments)) {
|
||||
$arguments = array_map('json_encode', convert_to_array($arguments));
|
||||
$arguments = implode(', ', $arguments);
|
||||
}
|
||||
|
||||
if ($var === null) {
|
||||
$js = "new $class(Y, $arguments);";
|
||||
} else if (strpos($var, '.') !== false) {
|
||||
$js = "$var = new $class(Y, $arguments);";
|
||||
} else {
|
||||
$js = "var $var = new $class(Y, $arguments);";
|
||||
}
|
||||
|
||||
if ($delay) {
|
||||
$delay = $delay * 1000; // Delay in miliseconds.
|
||||
$js = "setTimeout(function() { $js }, $delay);";
|
||||
}
|
||||
|
||||
if (count($requirements) > 0) {
|
||||
$requirements = implode("', '", $requirements);
|
||||
$js = "Y.use('$requirements', function(Y){ $js });";
|
||||
}
|
||||
return $js . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns code setting value to variable
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value json serialised value
|
||||
* @param bool $usevar add var definition, ignored for nested properties
|
||||
* @return string JS code fragment
|
||||
*/
|
||||
public static function set_variable($name, $value, $usevar = true) {
|
||||
$output = '';
|
||||
|
||||
if ($usevar) {
|
||||
if (strpos($name, '.')) {
|
||||
$output .= '';
|
||||
} else {
|
||||
$output .= 'var ';
|
||||
}
|
||||
}
|
||||
|
||||
$output .= "$name = " . json_encode($value) . ";";
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes event handler attaching code
|
||||
*
|
||||
* @param array|string $selector standard YUI selector for elements, may be
|
||||
* array or string, element id is in the form "#idvalue"
|
||||
* @param string $event A valid DOM event (click, mousedown, change etc.)
|
||||
* @param string $function The name of the function to call
|
||||
* @param null|array $arguments An optional array of argument parameters to pass to the function
|
||||
* @return string JS code fragment
|
||||
*/
|
||||
public static function event_handler($selector, $event, $function, ?array $arguments = null) {
|
||||
$selector = json_encode($selector);
|
||||
$output = "Y.on('$event', $function, $selector, null";
|
||||
if (!empty($arguments)) {
|
||||
$output .= ', ' . json_encode($arguments);
|
||||
}
|
||||
return $output . ");\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(js_writer::class, \js_writer::class);
|
||||
@@ -33,8 +33,7 @@ namespace core\output;
|
||||
* @copyright 2021 Adrian Greeve <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class language_menu implements \renderable, \templatable {
|
||||
|
||||
class language_menu implements renderable, templatable {
|
||||
/** @var \moodle_page $page the moodle page that the languague menu belongs to */
|
||||
protected $page;
|
||||
|
||||
@@ -67,7 +66,7 @@ class language_menu implements \renderable, \templatable {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->page->course != SITEID and !empty($this->page->course->lang)) {
|
||||
if ($this->page->course != SITEID && !empty($this->page->course->lang)) {
|
||||
// Do not show lang menu if language forced.
|
||||
return false;
|
||||
}
|
||||
@@ -175,5 +174,4 @@ class language_menu implements \renderable, \templatable {
|
||||
$singleselect->class = 'langmenu';
|
||||
return $singleselect->export_for_template($output);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
namespace core\output\local\dropdown;
|
||||
|
||||
use core\output\named_templatable;
|
||||
use renderable;
|
||||
use core\output\renderable;
|
||||
|
||||
/**
|
||||
* Class to render a dropdown dialog element.
|
||||
|
||||
@@ -30,7 +30,6 @@ use core\output\choicelist;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class status extends dialog {
|
||||
|
||||
/**
|
||||
* @var choicelist content of dialog.
|
||||
*/
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Mustache helper to load strings from string_manager and perform HTML escaping on them.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2021 Shamim Rezaie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
@@ -30,12 +21,12 @@ use Mustache_LambdaHelper;
|
||||
/**
|
||||
* This class will load language strings in a template.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2021 Shamim Rezaie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 4.0
|
||||
*/
|
||||
class mustache_clean_string_helper {
|
||||
|
||||
/** @var mustache_string_helper A string helper instance that is being used internally for fetching strings */
|
||||
private $stringhelper;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
*
|
||||
* @copyright 2019 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
@@ -28,6 +29,7 @@ namespace core\output;
|
||||
*
|
||||
* @copyright 2019 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
*/
|
||||
class mustache_engine extends \Mustache_Engine {
|
||||
/**
|
||||
@@ -68,12 +70,11 @@ class mustache_engine extends \Mustache_Engine {
|
||||
/**
|
||||
* Get the current set of Mustache helpers.
|
||||
*
|
||||
* @see Mustache_Engine::setHelpers
|
||||
* @see \Mustache_Engine::setHelpers
|
||||
*
|
||||
* @return \Mustache_HelperCollection
|
||||
*/
|
||||
public function getHelpers()
|
||||
{
|
||||
public function gethelpers() {
|
||||
if (!isset($this->helpers)) {
|
||||
$this->helpers = new mustache_helper_collection(null, $this->disallowednestedhelpers);
|
||||
}
|
||||
|
||||
@@ -14,28 +14,17 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Perform some custom name mapping for template file names (strip leading component/).
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use coding_exception;
|
||||
|
||||
/**
|
||||
* Perform some custom name mapping for template file names.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 2.9
|
||||
*/
|
||||
class mustache_filesystem_loader extends \Mustache_Loader_FilesystemLoader {
|
||||
|
||||
/**
|
||||
* Provide a default no-args constructor (we don't really need anything).
|
||||
*/
|
||||
@@ -49,7 +38,7 @@ class mustache_filesystem_loader extends \Mustache_Loader_FilesystemLoader {
|
||||
* @param string $name
|
||||
* @return string Template file name
|
||||
*/
|
||||
protected function getFileName($name) {
|
||||
protected function getfilename($name) {
|
||||
// Call the Moodle template finder.
|
||||
return mustache_template_finder::get_template_filepath($name);
|
||||
}
|
||||
@@ -62,7 +51,7 @@ class mustache_filesystem_loader extends \Mustache_Loader_FilesystemLoader {
|
||||
*
|
||||
* @return bool Whether to check `is_dir` and `file_exists`
|
||||
*/
|
||||
protected function shouldCheckPath() {
|
||||
protected function shouldcheckpath() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,23 +14,16 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Custom Moodle helper collection for mustache.
|
||||
*
|
||||
* @copyright 2019 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
/**
|
||||
* Custom Moodle helper collection for mustache.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2019 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mustache_helper_collection extends \Mustache_HelperCollection {
|
||||
|
||||
/**
|
||||
* @var string[] Names of helpers that aren't allowed to be called within other helpers.
|
||||
*/
|
||||
@@ -73,7 +66,7 @@ class mustache_helper_collection extends \Mustache_HelperCollection {
|
||||
$disallowedlist = $this->disallowednestedhelpers;
|
||||
|
||||
if (is_callable($helper) && !empty($disallowedlist)) {
|
||||
$helper = function($source, \Mustache_LambdaHelper $lambdahelper) use ($helper, $disallowedlist) {
|
||||
$helper = function ($source, \Mustache_LambdaHelper $lambdahelper) use ($helper, $disallowedlist) {
|
||||
|
||||
// Temporarily override the disallowed helpers to return nothing
|
||||
// so that they can't be executed from within other helpers.
|
||||
@@ -111,7 +104,7 @@ class mustache_helper_collection extends \Mustache_HelperCollection {
|
||||
$function = $this->get($name);
|
||||
// Null out the helper. Must call parent::add here to avoid
|
||||
// a recursion problem.
|
||||
parent::add($name, function() {
|
||||
parent::add($name, function () {
|
||||
return '';
|
||||
});
|
||||
|
||||
@@ -155,11 +148,11 @@ class mustache_helper_collection extends \Mustache_HelperCollection {
|
||||
$endtoken = '\/';
|
||||
}
|
||||
|
||||
$regexes = array_map(function($name) use ($starttoken, $endtoken) {
|
||||
$regexes = array_map(function ($name) use ($starttoken, $endtoken) {
|
||||
// We only strip out the name of the helper (excluding delimiters)
|
||||
// the user is able to change the delimeters on a per template
|
||||
// basis so they may not be curly braces.
|
||||
return '/\s*' . $starttoken . '\s*'. $name . '\W+.*' . $endtoken . '\s*' . $name . '\s*/';
|
||||
return '/\s*' . $starttoken . '\s*' . $name . '\W+.*' . $endtoken . '\s*' . $name . '\s*/';
|
||||
}, $disallowedlist);
|
||||
|
||||
// This will strip out unwanted helpers from the $source string
|
||||
@@ -169,7 +162,7 @@ class mustache_helper_collection extends \Mustache_HelperCollection {
|
||||
// "core, move, {{#js}} some nasty JS hack {{/js}}"
|
||||
// After:
|
||||
// "core, move, {{}}".
|
||||
return preg_replace_callback($regexes, function() {
|
||||
return preg_replace_callback($regexes, function () {
|
||||
return '';
|
||||
}, $string);
|
||||
}
|
||||
|
||||
@@ -19,12 +19,12 @@ namespace core\output;
|
||||
/**
|
||||
* Store a list of JS calls to insert at the end of the page.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 2.9
|
||||
*/
|
||||
class mustache_javascript_helper {
|
||||
|
||||
/** @var \moodle_page $page - Page used to get requirement manager */
|
||||
private $page = null;
|
||||
|
||||
|
||||
@@ -14,29 +14,18 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Mustache helper render pix icons.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
use renderer_base;
|
||||
|
||||
/**
|
||||
* This class will call pix_icon with the section content.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 2.9
|
||||
*/
|
||||
class mustache_pix_helper {
|
||||
|
||||
/** @var renderer_base $renderer A reference to the renderer in use */
|
||||
private $renderer;
|
||||
|
||||
@@ -80,4 +69,3 @@ class mustache_pix_helper {
|
||||
return trim($this->renderer->pix_icon($key, $text, $component));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ namespace core\output;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mustache_quote_helper {
|
||||
|
||||
/**
|
||||
* Wrap content in quotes, and escape all special JSON characters used.
|
||||
*
|
||||
|
||||
@@ -14,30 +14,18 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Mustache helper shorten text.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2017 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
use renderer_base;
|
||||
|
||||
/**
|
||||
* This class will call shorten_text with the section content.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2017 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mustache_shorten_text_helper {
|
||||
|
||||
/**
|
||||
* Read a length and text component from the string.
|
||||
*
|
||||
@@ -51,7 +39,7 @@ class mustache_shorten_text_helper {
|
||||
*/
|
||||
public function shorten($args, Mustache_LambdaHelper $helper) {
|
||||
// Split the text into an array of variables.
|
||||
list($length, $text) = explode(',', $args, 2);
|
||||
[$length, $text] = explode(',', $args, 2);
|
||||
$length = trim($length);
|
||||
$text = trim($text);
|
||||
|
||||
@@ -62,4 +50,3 @@ class mustache_shorten_text_helper {
|
||||
return shorten_text($text, $length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Mustache helper to load strings from string_manager.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
@@ -34,9 +25,9 @@ use stdClass;
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 2.9
|
||||
* @package core
|
||||
*/
|
||||
class mustache_string_helper {
|
||||
|
||||
/**
|
||||
* Read a lang string from a template and get it from get_string.
|
||||
*
|
||||
|
||||
@@ -14,21 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* List the valid locations to search for a template with a given name.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use coding_exception;
|
||||
use moodle_exception;
|
||||
use core\exception\coding_exception;
|
||||
use core\exception\moodle_exception;
|
||||
use core_component;
|
||||
use theme_config;
|
||||
|
||||
/**
|
||||
* Get information about valid locations for mustache templates.
|
||||
@@ -36,9 +26,9 @@ use theme_config;
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since 2.9
|
||||
* @package core
|
||||
*/
|
||||
class mustache_template_finder {
|
||||
|
||||
/**
|
||||
* Helper function for getting a list of valid template directories for a specific component.
|
||||
*
|
||||
@@ -59,14 +49,14 @@ class mustache_template_finder {
|
||||
$themename = clean_param($themename, PARAM_COMPONENT);
|
||||
|
||||
// Validate the component.
|
||||
$dirs = array();
|
||||
$dirs = [];
|
||||
$compdirectory = core_component::get_component_directory($component);
|
||||
if (!$compdirectory) {
|
||||
throw new coding_exception("Component was not valid: " . s($component));
|
||||
}
|
||||
|
||||
// Find the parent themes.
|
||||
$parents = array();
|
||||
$parents = [];
|
||||
if ($themename === $PAGE->theme->name) {
|
||||
$parents = $PAGE->theme->parents;
|
||||
} else {
|
||||
@@ -108,7 +98,7 @@ class mustache_template_finder {
|
||||
' (' . s($name) . ' requested) ');
|
||||
}
|
||||
|
||||
list($component, $templatename) = explode('/', $name, 2);
|
||||
[$component, $templatename] = explode('/', $name, 2);
|
||||
$component = clean_param($component, PARAM_COMPONENT);
|
||||
|
||||
$dirs = self::get_template_directories_for_component($component, $themename);
|
||||
|
||||
@@ -14,29 +14,18 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Load template source strings.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2018 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use \Mustache_Tokenizer;
|
||||
use Mustache_Tokenizer;
|
||||
|
||||
/**
|
||||
* Load template source strings.
|
||||
*
|
||||
* @copyright 2018 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
*/
|
||||
class mustache_template_source_loader {
|
||||
|
||||
/** @var $gettemplatesource Callback function to load the template source from full name */
|
||||
private $gettemplatesource = null;
|
||||
|
||||
@@ -55,7 +44,7 @@ class mustache_template_source_loader {
|
||||
$this->gettemplatesource = $gettemplatesource;
|
||||
} else {
|
||||
// By default we will pull the template from disk.
|
||||
$this->gettemplatesource = function($component, $name, $themename) {
|
||||
$this->gettemplatesource = function ($component, $name, $themename) {
|
||||
$fulltemplatename = $component . '/' . $name;
|
||||
$filename = mustache_template_finder::get_template_filepath($fulltemplatename, $themename);
|
||||
return file_get_contents($filename);
|
||||
@@ -161,7 +150,7 @@ class mustache_template_source_loader {
|
||||
// Get the requested template source.
|
||||
$templatesource = $this->load($templatecomponent, $templatename, $themename, $includecomments);
|
||||
// This is a helper function to save a value in one of the result arrays (either $templates or $strings).
|
||||
$save = function(array $results, array $seenlist, string $component, string $id, $value) use ($lang) {
|
||||
$save = function (array $results, array $seenlist, string $component, string $id, $value) use ($lang) {
|
||||
if (!isset($results[$component])) {
|
||||
// If the results list doesn't already contain this component then initialise it.
|
||||
$results[$component] = [];
|
||||
@@ -176,7 +165,7 @@ class mustache_template_source_loader {
|
||||
};
|
||||
// This is a helper function for processing a dependency. Does stuff like ignore duplicate processing,
|
||||
// common result formatting etc.
|
||||
$handler = function(array $dependency, array $ignorelist, callable $processcallback) use ($lang) {
|
||||
$handler = function (array $dependency, array $ignorelist, callable $processcallback) use ($lang) {
|
||||
foreach ($dependency as $component => $ids) {
|
||||
foreach ($ids as $id) {
|
||||
$dependencyid = "$component/$id";
|
||||
@@ -193,7 +182,7 @@ class mustache_template_source_loader {
|
||||
};
|
||||
|
||||
// Save this template as the first result in the $templates result array.
|
||||
list($templates, $seentemplates) = $save($templates, $seentemplates, $templatecomponent, $templatename, $templatesource);
|
||||
[$templates, $seentemplates] = $save($templates, $seentemplates, $templatecomponent, $templatename, $templatesource);
|
||||
|
||||
// Check the template for any dependencies that need to be loaded.
|
||||
$dependencies = $this->scan_template_source_for_dependencies($templatesource);
|
||||
@@ -205,10 +194,10 @@ class mustache_template_source_loader {
|
||||
$seenstrings,
|
||||
// Include $strings and $seenstrings by reference so that their values can be updated
|
||||
// outside of this anonymous function.
|
||||
function($component, $id) use ($save, &$strings, &$seenstrings, $lang) {
|
||||
function ($component, $id) use ($save, &$strings, &$seenstrings, $lang) {
|
||||
$string = get_string_manager()->get_string($id, $component, null, $lang);
|
||||
// Save the string in the $strings results array.
|
||||
list($strings, $seenstrings) = $save($strings, $seenstrings, $component, $id, $string);
|
||||
[$strings, $seenstrings] = $save($strings, $seenstrings, $component, $id, $string);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -219,7 +208,10 @@ class mustache_template_source_loader {
|
||||
$seentemplates,
|
||||
// Include $strings, $seenstrings, $templates, and $seentemplates by reference so that their values can be updated
|
||||
// outside of this anonymous function.
|
||||
function($component, $id) use (
|
||||
function (
|
||||
$component,
|
||||
$id
|
||||
) use (
|
||||
$themename,
|
||||
$includecomments,
|
||||
&$seentemplates,
|
||||
@@ -243,14 +235,14 @@ class mustache_template_source_loader {
|
||||
foreach ($subdependencies['templates'] as $component => $ids) {
|
||||
foreach ($ids as $id => $value) {
|
||||
// Include the child themes in our results.
|
||||
list($templates, $seentemplates) = $save($templates, $seentemplates, $component, $id, $value);
|
||||
[$templates, $seentemplates] = $save($templates, $seentemplates, $component, $id, $value);
|
||||
}
|
||||
};
|
||||
|
||||
foreach ($subdependencies['strings'] as $component => $ids) {
|
||||
foreach ($ids as $id => $value) {
|
||||
// Include any strings that the child templates need in our results.
|
||||
list($strings, $seenstrings) = $save($strings, $seenstrings, $component, $id, $value);
|
||||
[$strings, $seenstrings] = $save($strings, $seenstrings, $component, $id, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -258,7 +250,7 @@ class mustache_template_source_loader {
|
||||
|
||||
return [
|
||||
'templates' => $templates,
|
||||
'strings' => $strings
|
||||
'strings' => $strings,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -291,7 +283,7 @@ class mustache_template_source_loader {
|
||||
$tokens = $tokenizer->scan($source);
|
||||
$templates = [];
|
||||
$strings = [];
|
||||
$addtodependencies = function($dependencies, $component, $id) {
|
||||
$addtodependencies = function ($dependencies, $component, $id) {
|
||||
$id = trim($id);
|
||||
$component = trim($component);
|
||||
|
||||
@@ -313,16 +305,16 @@ class mustache_template_source_loader {
|
||||
if ($name) {
|
||||
switch ($type) {
|
||||
case Mustache_Tokenizer::T_PARTIAL:
|
||||
list($component, $id) = explode('/', $name, 2);
|
||||
[$component, $id] = explode('/', $name, 2);
|
||||
$templates = $addtodependencies($templates, $component, $id);
|
||||
break;
|
||||
case Mustache_Tokenizer::T_PARENT:
|
||||
list($component, $id) = explode('/', $name, 2);
|
||||
[$component, $id] = explode('/', $name, 2);
|
||||
$templates = $addtodependencies($templates, $component, $id);
|
||||
break;
|
||||
case Mustache_Tokenizer::T_SECTION:
|
||||
if ($name == 'str') {
|
||||
list($id, $component) = $this->get_string_identifiers($tokens, $index);
|
||||
[$id, $component] = $this->get_string_identifiers($tokens, $index);
|
||||
|
||||
if ($id) {
|
||||
$strings = $addtodependencies($strings, $component, $id);
|
||||
@@ -335,7 +327,7 @@ class mustache_template_source_loader {
|
||||
|
||||
return [
|
||||
'templates' => $templates,
|
||||
'strings' => $strings
|
||||
'strings' => $strings,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ namespace core\output;
|
||||
* @since 2.9
|
||||
*/
|
||||
class mustache_uniqid_helper {
|
||||
|
||||
/** @var string $uniqid The unique id */
|
||||
private $uniqid = null;
|
||||
|
||||
|
||||
@@ -14,19 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Mustache helper that will convert a timestamp to a date string.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2017 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use Mustache_LambdaHelper;
|
||||
|
||||
/**
|
||||
@@ -34,9 +23,9 @@ use Mustache_LambdaHelper;
|
||||
*
|
||||
* @copyright 2017 Ryan Wyllie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
*/
|
||||
class mustache_user_date_helper {
|
||||
|
||||
/**
|
||||
* Read a timestamp and format from the string.
|
||||
*
|
||||
@@ -52,7 +41,7 @@ class mustache_user_date_helper {
|
||||
*/
|
||||
public function transform($args, Mustache_LambdaHelper $helper) {
|
||||
// Split the text into an array of variables.
|
||||
list($timestamp, $format) = explode(',', $args, 2);
|
||||
[$timestamp, $format] = explode(',', $args, 2);
|
||||
$timestamp = trim($timestamp);
|
||||
$format = trim($format);
|
||||
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use templatable;
|
||||
|
||||
/**
|
||||
* A subset of templatable which provides the name of the template to use.
|
||||
*
|
||||
@@ -26,12 +24,11 @@ use templatable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
interface named_templatable extends templatable {
|
||||
|
||||
/**
|
||||
* Get the name of the template to use for this templatable.
|
||||
*
|
||||
* @param \renderer_base $renderer The renderer requesting the template name
|
||||
* @param renderer_base $renderer The renderer requesting the template name
|
||||
* @return string
|
||||
*/
|
||||
public function get_template_name(\renderer_base $renderer): string;
|
||||
public function get_template_name(renderer_base $renderer): string;
|
||||
}
|
||||
|
||||
@@ -33,8 +33,7 @@ namespace core\output;
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class notification implements \renderable, \templatable {
|
||||
|
||||
class notification implements renderable, templatable {
|
||||
/**
|
||||
* A notification of level 'success'.
|
||||
*/
|
||||
@@ -78,7 +77,7 @@ class notification implements \renderable, \templatable {
|
||||
/**
|
||||
* @var array $extraclasses A list of any extra classes that may be required.
|
||||
*/
|
||||
protected $extraclasses = array();
|
||||
protected $extraclasses = [];
|
||||
|
||||
/**
|
||||
* Notification constructor.
|
||||
@@ -129,7 +128,7 @@ class notification implements \renderable, \templatable {
|
||||
* @param array $classes
|
||||
* @return $this
|
||||
*/
|
||||
public function set_extra_classes($classes = array()) {
|
||||
public function set_extra_classes($classes = []) {
|
||||
$this->extraclasses = $classes;
|
||||
|
||||
return $this;
|
||||
@@ -160,7 +159,7 @@ class notification implements \renderable, \templatable {
|
||||
* @return array data context for a mustache template
|
||||
*/
|
||||
public function export_for_template(\renderer_base $output) {
|
||||
return array(
|
||||
return [
|
||||
'message' => clean_text($this->message),
|
||||
'extraclasses' => implode(' ', $this->extraclasses),
|
||||
'announce' => $this->announce,
|
||||
@@ -169,9 +168,14 @@ class notification implements \renderable, \templatable {
|
||||
'isinfo' => $this->messagetype === 'info',
|
||||
'iswarning' => $this->messagetype === 'warning',
|
||||
'iserror' => $this->messagetype === 'error',
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the template to use for this templatable.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_template_name() {
|
||||
$templatemappings = [
|
||||
// Current types mapped to template names.
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use moodle_page;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Component representing a paging bar.
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class paging_bar implements renderable, templatable {
|
||||
/**
|
||||
* @var int The maximum number of pagelinks to display.
|
||||
*/
|
||||
public $maxdisplay = 18;
|
||||
|
||||
/**
|
||||
* @var int The total number of entries to be pages through..
|
||||
*/
|
||||
public $totalcount;
|
||||
|
||||
/**
|
||||
* @var int The page you are currently viewing.
|
||||
*/
|
||||
public $page;
|
||||
|
||||
/**
|
||||
* @var int The number of entries that should be shown per page.
|
||||
*/
|
||||
public $perpage;
|
||||
|
||||
/**
|
||||
* @var string|moodle_url If this is a string then it is the url which will be appended with $pagevar,
|
||||
* an equals sign and the page number.
|
||||
* If this is a moodle_url object then the pagevar param will be replaced by
|
||||
* the page no, for each page.
|
||||
*/
|
||||
public $baseurl;
|
||||
|
||||
/**
|
||||
* @var string This is the variable name that you use for the pagenumber in your
|
||||
* code (ie. 'tablepage', 'blogpage', etc)
|
||||
*/
|
||||
public $pagevar;
|
||||
|
||||
/**
|
||||
* @var string A HTML link representing the "previous" page.
|
||||
*/
|
||||
public $previouslink = null;
|
||||
|
||||
/**
|
||||
* @var string A HTML link representing the "next" page.
|
||||
*/
|
||||
public $nextlink = null;
|
||||
|
||||
/**
|
||||
* @var string A HTML link representing the first page.
|
||||
*/
|
||||
public $firstlink = null;
|
||||
|
||||
/**
|
||||
* @var string A HTML link representing the last page.
|
||||
*/
|
||||
public $lastlink = null;
|
||||
|
||||
/**
|
||||
* @var array An array of strings. One of them is just a string: the current page
|
||||
*/
|
||||
public $pagelinks = [];
|
||||
|
||||
/**
|
||||
* Constructor paging_bar with only the required params.
|
||||
*
|
||||
* @param int $totalcount The total number of entries available to be paged through
|
||||
* @param int $page The page you are currently viewing
|
||||
* @param int $perpage The number of entries that should be shown per page
|
||||
* @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
|
||||
* @param string $pagevar name of page parameter that holds the page number
|
||||
*/
|
||||
public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
|
||||
$this->totalcount = $totalcount;
|
||||
$this->page = $page;
|
||||
$this->perpage = $perpage;
|
||||
$this->baseurl = $baseurl;
|
||||
$this->pagevar = $pagevar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the paging bar for output.
|
||||
*
|
||||
* This method validates the arguments set up for the paging bar and then
|
||||
* produces fragments of HTML to assist display later on.
|
||||
*
|
||||
* @param renderer_base $output
|
||||
* @param moodle_page $page
|
||||
* @param string $target
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function prepare(renderer_base $output, moodle_page $page, $target) {
|
||||
if (!isset($this->totalcount) || is_null($this->totalcount)) {
|
||||
throw new coding_exception('paging_bar requires a totalcount value.');
|
||||
}
|
||||
if (!isset($this->page) || is_null($this->page)) {
|
||||
throw new coding_exception('paging_bar requires a page value.');
|
||||
}
|
||||
if (empty($this->perpage)) {
|
||||
throw new coding_exception('paging_bar requires a perpage value.');
|
||||
}
|
||||
if (empty($this->baseurl)) {
|
||||
throw new coding_exception('paging_bar requires a baseurl value.');
|
||||
}
|
||||
|
||||
if ($this->totalcount > $this->perpage) {
|
||||
$pagenum = $this->page - 1;
|
||||
|
||||
if ($this->page > 0) {
|
||||
$this->previouslink = html_writer::link(
|
||||
new moodle_url($this->baseurl, [$this->pagevar => $pagenum]),
|
||||
get_string('previous'),
|
||||
['class' => 'previous'],
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->perpage > 0) {
|
||||
$lastpage = ceil($this->totalcount / $this->perpage);
|
||||
} else {
|
||||
$lastpage = 1;
|
||||
}
|
||||
|
||||
if ($this->page > round(($this->maxdisplay / 3) * 2)) {
|
||||
$currpage = $this->page - round($this->maxdisplay / 3);
|
||||
|
||||
$this->firstlink = html_writer::link(
|
||||
new moodle_url($this->baseurl, [$this->pagevar => 0]),
|
||||
'1',
|
||||
['class' => 'first'],
|
||||
);
|
||||
} else {
|
||||
$currpage = 0;
|
||||
}
|
||||
|
||||
$displaycount = $displaypage = 0;
|
||||
|
||||
while ($displaycount < $this->maxdisplay && $currpage < $lastpage) {
|
||||
$displaypage = $currpage + 1;
|
||||
|
||||
if ($this->page == $currpage) {
|
||||
$this->pagelinks[] = html_writer::span($displaypage, 'current-page');
|
||||
} else {
|
||||
$pagelink = html_writer::link(new moodle_url($this->baseurl, [$this->pagevar => $currpage]), $displaypage);
|
||||
$this->pagelinks[] = $pagelink;
|
||||
}
|
||||
|
||||
$displaycount++;
|
||||
$currpage++;
|
||||
}
|
||||
|
||||
if ($currpage < $lastpage) {
|
||||
$lastpageactual = $lastpage - 1;
|
||||
$this->lastlink = html_writer::link(
|
||||
new moodle_url($this->baseurl, [$this->pagevar => $lastpageactual]),
|
||||
$lastpage,
|
||||
['class' => 'last'],
|
||||
);
|
||||
}
|
||||
|
||||
$pagenum = $this->page + 1;
|
||||
|
||||
if ($pagenum != $lastpage) {
|
||||
$this->nextlink = html_writer::link(
|
||||
new moodle_url($this->baseurl, [$this->pagevar => $pagenum]),
|
||||
get_string('next'),
|
||||
['class' => 'next'],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$data = new stdClass();
|
||||
$data->previous = null;
|
||||
$data->next = null;
|
||||
$data->first = null;
|
||||
$data->last = null;
|
||||
$data->label = get_string('page');
|
||||
$data->pages = [];
|
||||
$data->haspages = $this->totalcount > $this->perpage;
|
||||
$data->pagesize = $this->perpage;
|
||||
|
||||
if (!$data->haspages) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($this->page > 0) {
|
||||
$data->previous = [
|
||||
'page' => $this->page,
|
||||
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page - 1]))->out(false),
|
||||
];
|
||||
}
|
||||
|
||||
$currpage = 0;
|
||||
if ($this->page > round(($this->maxdisplay / 3) * 2)) {
|
||||
$currpage = $this->page - round($this->maxdisplay / 3);
|
||||
$data->first = [
|
||||
'page' => 1,
|
||||
'url' => (new moodle_url($this->baseurl, [$this->pagevar => 0]))->out(false),
|
||||
];
|
||||
}
|
||||
|
||||
$lastpage = 1;
|
||||
if ($this->perpage > 0) {
|
||||
$lastpage = ceil($this->totalcount / $this->perpage);
|
||||
}
|
||||
|
||||
$displaycount = 0;
|
||||
$displaypage = 0;
|
||||
while ($displaycount < $this->maxdisplay && $currpage < $lastpage) {
|
||||
$displaypage = $currpage + 1;
|
||||
|
||||
$iscurrent = $this->page == $currpage;
|
||||
$link = new moodle_url($this->baseurl, [$this->pagevar => $currpage]);
|
||||
|
||||
$data->pages[] = [
|
||||
'page' => $displaypage,
|
||||
'active' => $iscurrent,
|
||||
'url' => $iscurrent ? null : $link->out(false),
|
||||
];
|
||||
|
||||
$displaycount++;
|
||||
$currpage++;
|
||||
}
|
||||
|
||||
if ($currpage < $lastpage) {
|
||||
$data->last = [
|
||||
'page' => $lastpage,
|
||||
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $lastpage - 1]))->out(false),
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->page + 1 != $lastpage) {
|
||||
$data->next = [
|
||||
'page' => $this->page + 2,
|
||||
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page + 1]))->out(false),
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(paging_bar::class, \paging_bar::class);
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use context_course;
|
||||
use core\context\course as context_course;
|
||||
use moodle_page;
|
||||
use navigation_node;
|
||||
use moodle_url;
|
||||
@@ -28,7 +28,7 @@ use moodle_url;
|
||||
* @copyright 2021 Peter Dias
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class participants_action_bar implements \renderable {
|
||||
class participants_action_bar implements renderable {
|
||||
/** @var object $course The course we are dealing with. */
|
||||
private $course;
|
||||
/** @var moodle_page $page The current page. */
|
||||
@@ -72,7 +72,7 @@ class participants_action_bar implements \renderable {
|
||||
'renameroles',
|
||||
],
|
||||
'groups:group' => [
|
||||
'groups'
|
||||
'groups',
|
||||
],
|
||||
'permissions:role' => [
|
||||
'override',
|
||||
@@ -98,8 +98,10 @@ class participants_action_bar implements \renderable {
|
||||
|
||||
$formattedcontent = [];
|
||||
$enrolmentsheading = get_string('enrolments', 'enrol');
|
||||
if ($this->page->context->contextlevel != CONTEXT_MODULE &&
|
||||
$this->page->context->contextlevel != CONTEXT_COURSECAT) {
|
||||
if (
|
||||
$this->page->context->contextlevel != CONTEXT_MODULE &&
|
||||
$this->page->context->contextlevel != CONTEXT_COURSECAT
|
||||
) {
|
||||
// Pre-populate the formatted tertiary nav items with the "Enrolled users" node if user can view the participants page.
|
||||
$coursecontext = context_course::instance($this->course->id);
|
||||
$canviewparticipants = course_can_view_participants($coursecontext);
|
||||
@@ -108,14 +110,14 @@ class participants_action_bar implements \renderable {
|
||||
$formattedcontent[] = [
|
||||
$enrolmentsheading => [
|
||||
$participantsurl => get_string('enrolledusers', 'enrol'),
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$nodes = $this->get_ordered_nodes();
|
||||
foreach ($nodes as $description => $content) {
|
||||
list($stringid, $location) = explode(':', $description);
|
||||
[$stringid, $location] = explode(':', $description);
|
||||
$heading = get_string($stringid, $location);
|
||||
$items = [];
|
||||
foreach ($content as $key) {
|
||||
@@ -129,7 +131,7 @@ class participants_action_bar implements \renderable {
|
||||
$params = ['id' => $this->course->id];
|
||||
$items += [
|
||||
(new moodle_url('/group/groupings.php', $params))->out() => get_string('groupings', 'group'),
|
||||
(new moodle_url('/group/overview.php', $params))->out() => get_string('overview', 'group')
|
||||
(new moodle_url('/group/overview.php', $params))->out() => get_string('overview', 'group'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -145,8 +147,10 @@ class participants_action_bar implements \renderable {
|
||||
}
|
||||
|
||||
// If we are accessing a page from a module/category context additional nodes will not be visible.
|
||||
if ($this->page->context->contextlevel != CONTEXT_MODULE &&
|
||||
$this->page->context->contextlevel != CONTEXT_COURSECAT) {
|
||||
if (
|
||||
$this->page->context->contextlevel != CONTEXT_MODULE &&
|
||||
$this->page->context->contextlevel != CONTEXT_COURSECAT
|
||||
) {
|
||||
// Need to do some funky code here to find out if we have added third party navigation nodes.
|
||||
$thirdpartynodearray = $this->get_thirdparty_node_array() ?: [];
|
||||
$formattedcontent = array_merge($formattedcontent, $thirdpartynodearray);
|
||||
|
||||
@@ -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/>.
|
||||
|
||||
namespace core\output;
|
||||
|
||||
/**
|
||||
* Data structure representing an emoticon image
|
||||
*
|
||||
* @copyright 2010 David Mudrak
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class pix_emoticon extends pix_icon implements renderable {
|
||||
/**
|
||||
* Constructor
|
||||
* @param string $pix short icon name
|
||||
* @param string $alt alternative text
|
||||
* @param string $component emoticon image provider
|
||||
* @param array $attributes explicit HTML attributes
|
||||
*/
|
||||
public function __construct($pix, $alt, $component = 'moodle', array $attributes = []) {
|
||||
if (empty($attributes['class'])) {
|
||||
$attributes['class'] = 'emoticon';
|
||||
}
|
||||
parent::__construct($pix, $alt, $component, $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(pix_emoticon::class, \pix_emoticon::class);
|
||||
@@ -0,0 +1,154 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Data structure representing an icon.
|
||||
*
|
||||
* @copyright 2010 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class pix_icon implements renderable, templatable {
|
||||
/**
|
||||
* @var string The icon name
|
||||
*/
|
||||
public $pix;
|
||||
|
||||
/**
|
||||
* @var string The component the icon belongs to.
|
||||
*/
|
||||
public $component;
|
||||
|
||||
/**
|
||||
* @var array An array of attributes to use on the icon
|
||||
*/
|
||||
public $attributes = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $pix short icon name
|
||||
* @param string $alt The alt text to use for the icon
|
||||
* @param string $component component name
|
||||
* @param null|array $attributes html attributes
|
||||
*/
|
||||
public function __construct(
|
||||
$pix,
|
||||
$alt,
|
||||
$component = 'moodle',
|
||||
?array $attributes = null,
|
||||
) {
|
||||
global $PAGE;
|
||||
|
||||
$this->pix = $pix;
|
||||
$this->component = $component;
|
||||
$this->attributes = (array)$attributes;
|
||||
|
||||
if (empty($this->attributes['class'])) {
|
||||
$this->attributes['class'] = '';
|
||||
}
|
||||
|
||||
// Set an additional class for big icons so that they can be styled properly.
|
||||
if (substr($pix, 0, 2) === 'b/') {
|
||||
$this->attributes['class'] .= ' iconsize-big';
|
||||
}
|
||||
|
||||
// If the alt is empty, don't place it in the attributes, otherwise it will override parent alt text.
|
||||
if (!is_null($alt)) {
|
||||
$this->attributes['alt'] = $alt;
|
||||
|
||||
// If there is no title, set it to the attribute.
|
||||
if (!isset($this->attributes['title'])) {
|
||||
$this->attributes['title'] = $this->attributes['alt'];
|
||||
}
|
||||
} else {
|
||||
unset($this->attributes['alt']);
|
||||
}
|
||||
|
||||
if (empty($this->attributes['title'])) {
|
||||
// Remove the title attribute if empty, we probably want to use the parent node's title
|
||||
// and some browsers might overwrite it with an empty title.
|
||||
unset($this->attributes['title']);
|
||||
}
|
||||
|
||||
// Hide icons from screen readers that have no alt.
|
||||
if (empty($this->attributes['alt'])) {
|
||||
$this->attributes['aria-hidden'] = 'true';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export this data so it can be used as the context for a mustache template.
|
||||
*
|
||||
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$attributes = $this->attributes;
|
||||
$extraclasses = '';
|
||||
|
||||
foreach ($attributes as $key => $item) {
|
||||
if ($key == 'class') {
|
||||
$extraclasses = $item;
|
||||
unset($attributes[$key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$attributes['src'] = $output->image_url($this->pix, $this->component)->out(false);
|
||||
$templatecontext = [];
|
||||
foreach ($attributes as $name => $value) {
|
||||
$templatecontext[] = ['name' => $name, 'value' => $value];
|
||||
}
|
||||
$title = isset($attributes['title']) ? $attributes['title'] : '';
|
||||
if (empty($title)) {
|
||||
$title = isset($attributes['alt']) ? $attributes['alt'] : '';
|
||||
}
|
||||
$data = [
|
||||
'attributes' => $templatecontext,
|
||||
'extraclasses' => $extraclasses,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Much simpler version of export that will produce the data required to render this pix with the
|
||||
* pix helper in a mustache tag.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_pix() {
|
||||
$title = isset($this->attributes['title']) ? $this->attributes['title'] : '';
|
||||
if (empty($title)) {
|
||||
$title = isset($this->attributes['alt']) ? $this->attributes['alt'] : '';
|
||||
}
|
||||
return [
|
||||
'key' => $this->pix,
|
||||
'component' => $this->component,
|
||||
'title' => (string) $title,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(pix_icon::class, \pix_icon::class);
|
||||
@@ -0,0 +1,99 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Data structure representing an icon font.
|
||||
*
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class pix_icon_font implements templatable {
|
||||
/**
|
||||
* @var pix_icon $pixicon The original icon.
|
||||
*/
|
||||
private $pixicon = null;
|
||||
|
||||
/**
|
||||
* @var string $key The mapped key.
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* @var bool $mapped The icon could not be mapped.
|
||||
*/
|
||||
private $mapped;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param pix_icon $pixicon The original icon
|
||||
*/
|
||||
public function __construct(pix_icon $pixicon) {
|
||||
global $PAGE;
|
||||
|
||||
$this->pixicon = $pixicon;
|
||||
$this->mapped = false;
|
||||
$iconsystem = icon_system::instance();
|
||||
|
||||
$this->key = $iconsystem->remap_icon_name($pixicon->pix, $pixicon->component);
|
||||
if (!empty($this->key)) {
|
||||
$this->mapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this pix_icon was successfully mapped to an icon font.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_mapped() {
|
||||
return $this->mapped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export this data so it can be used as the context for a mustache template.
|
||||
*
|
||||
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
|
||||
$pixdata = $this->pixicon->export_for_template($output);
|
||||
|
||||
$title = isset($this->pixicon->attributes['title']) ? $this->pixicon->attributes['title'] : '';
|
||||
$alt = isset($this->pixicon->attributes['alt']) ? $this->pixicon->attributes['alt'] : '';
|
||||
if (empty($title)) {
|
||||
$title = $alt;
|
||||
}
|
||||
$data = [
|
||||
'extraclasses' => $pixdata['extraclasses'],
|
||||
'title' => $title,
|
||||
'alt' => $alt,
|
||||
'key' => $this->key,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(pix_icon_font::class, \pix_icon_font::class);
|
||||
@@ -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/>.
|
||||
|
||||
namespace core\output;
|
||||
|
||||
/**
|
||||
* Data structure representing an icon subtype.
|
||||
*
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class pix_icon_fontawesome extends pix_icon_font {
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(pix_icon_fontawesome::class, \pix_icon_fontawesome::class);
|
||||
@@ -0,0 +1,140 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use moodle_page;
|
||||
|
||||
/**
|
||||
* Basis for all plugin renderers.
|
||||
*
|
||||
* @copyright Petr Skoda (skodak)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class plugin_renderer_base extends renderer_base {
|
||||
/**
|
||||
* @var renderer_base|core_renderer A reference to the current renderer.
|
||||
* The renderer provided here will be determined by the page but will in 90%
|
||||
* of cases by the {@see core_renderer}
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* Constructor method, calls the parent constructor
|
||||
*
|
||||
* @param moodle_page $page
|
||||
* @param string $target one of rendering target constants
|
||||
*/
|
||||
public function __construct(moodle_page $page, $target) {
|
||||
if (empty($target) && $page->pagelayout === 'maintenance') {
|
||||
// If the page is using the maintenance layout then we're going to force the target to maintenance.
|
||||
// This way we'll get a special maintenance renderer that is designed to block access to API's that are likely
|
||||
// unavailable for this page layout.
|
||||
$target = RENDERER_TARGET_MAINTENANCE;
|
||||
}
|
||||
$this->output = $page->get_renderer('core', null, $target);
|
||||
parent::__construct($page, $target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the provided widget and returns the HTML to display it.
|
||||
*
|
||||
* @param renderable $widget instance with renderable interface
|
||||
* @return string
|
||||
*/
|
||||
public function render(renderable $widget) {
|
||||
$classname = get_class($widget);
|
||||
|
||||
// Strip namespaces.
|
||||
$classname = preg_replace('/^.*\\\/', '', $classname);
|
||||
|
||||
// Keep a copy at this point, we may need to look for a deprecated method.
|
||||
$deprecatedmethod = "render_{$classname}";
|
||||
|
||||
// Remove _renderable suffixes.
|
||||
$classname = preg_replace('/_renderable$/', '', $classname);
|
||||
$rendermethod = "render_{$classname}";
|
||||
|
||||
if (method_exists($this, $rendermethod)) {
|
||||
// Call the render_[widget_name] function.
|
||||
// Note: This has a higher priority than the named_templatable to allow the theme to override the template.
|
||||
return $this->$rendermethod($widget);
|
||||
}
|
||||
|
||||
if ($widget instanceof named_templatable) {
|
||||
// This is a named templatable.
|
||||
// Fetch the template name from the get_template_name function instead.
|
||||
// Note: This has higher priority than the deprecated method which is not overridable by themes anyway.
|
||||
return $this->render_from_template(
|
||||
$widget->get_template_name($this),
|
||||
$widget->export_for_template($this)
|
||||
);
|
||||
}
|
||||
|
||||
if ($rendermethod !== $deprecatedmethod && method_exists($this, $deprecatedmethod)) {
|
||||
// This is exactly where we don't want to be.
|
||||
// If you have arrived here you have a renderable component within your plugin that has the name
|
||||
// blah_renderable, and you have a render method render_blah_renderable on your plugin.
|
||||
// In 2.8 we revamped output, as part of this change we changed slightly how renderables got rendered
|
||||
// and the _renderable suffix now gets removed when looking for a render method.
|
||||
// You need to change your renderers render_blah_renderable to render_blah.
|
||||
// Until you do this it will not be possible for a theme to override the renderer to override your method.
|
||||
// Please do it ASAP.
|
||||
static $debugged = [];
|
||||
if (!isset($debugged[$deprecatedmethod])) {
|
||||
debugging(sprintf(
|
||||
'Deprecated call. Please rename your renderables render method from %s to %s.',
|
||||
$deprecatedmethod,
|
||||
$rendermethod
|
||||
), DEBUG_DEVELOPER);
|
||||
$debugged[$deprecatedmethod] = true;
|
||||
}
|
||||
return $this->$deprecatedmethod($widget);
|
||||
}
|
||||
|
||||
// Pass to core renderer if method not found here.
|
||||
// Note: this is not a parent. This is _new_ renderer which respects the requested format, and output type.
|
||||
return $this->output->render($widget);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method used to pass calls otherwise meant for the standard renderer
|
||||
* to it to ensure we don't go causing unnecessary grief.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $arguments) {
|
||||
if (method_exists('renderer_base', $method)) {
|
||||
throw new coding_exception('Protected method called against ' . get_class($this) . ' :: ' . $method);
|
||||
}
|
||||
if (method_exists($this->output, $method)) {
|
||||
return call_user_func_array([$this->output, $method], $arguments);
|
||||
} else {
|
||||
throw new coding_exception('Unknown method called against ' . get_class($this) . ' :: ' . $method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(plugin_renderer_base::class, \plugin_renderer_base::class);
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Represents a group of preferences page link.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2015 Frédéric Massart - FMCorz.net
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class preferences_group implements renderable {
|
||||
/**
|
||||
* Title of the group.
|
||||
* @var string
|
||||
*/
|
||||
public $title;
|
||||
|
||||
/**
|
||||
* Array of navigation_node.
|
||||
* @var array
|
||||
*/
|
||||
public $nodes;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param string $title The title.
|
||||
* @param array $nodes of navigation_node.
|
||||
*/
|
||||
public function __construct($title, $nodes) {
|
||||
$this->title = $title;
|
||||
$this->nodes = $nodes;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(preferences_group::class, \preferences_group::class);
|
||||
@@ -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/>.
|
||||
|
||||
namespace core\output;
|
||||
|
||||
/**
|
||||
* Represents a set of preferences groups.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2015 Frédéric Massart - FMCorz.net
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class preferences_groups implements renderable {
|
||||
/**
|
||||
* Array of preferences_group.
|
||||
* @var array
|
||||
*/
|
||||
public $groups;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param array $groups of preferences_group
|
||||
*/
|
||||
public function __construct($groups) {
|
||||
$this->groups = $groups;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(preferences_groups::class, \preferences_groups::class);
|
||||
@@ -0,0 +1,213 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
|
||||
/**
|
||||
* Progress bar class.
|
||||
*
|
||||
* Manages the display of a progress bar.
|
||||
*
|
||||
* To use this class.
|
||||
* - construct
|
||||
* - call create (or use the 3rd param to the constructor)
|
||||
* - call update or update_full() or update() repeatedly
|
||||
*
|
||||
* @copyright 2008 jamiesensei
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class progress_bar implements renderable, templatable {
|
||||
/** @var string html id */
|
||||
private $htmlid;
|
||||
/** @var int total width */
|
||||
private $width;
|
||||
/** @var int last percentage printed */
|
||||
private $percent = 0;
|
||||
/** @var int time when last printed */
|
||||
private $lastupdate = 0;
|
||||
/** @var int when did we start printing this */
|
||||
private $timestart = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Prints JS code if $autostart true.
|
||||
*
|
||||
* @param string $htmlid The container ID.
|
||||
* @param int $width The suggested width.
|
||||
* @param bool $autostart Whether to start the progress bar right away.
|
||||
*/
|
||||
public function __construct($htmlid = '', $width = 500, $autostart = false) {
|
||||
if (!CLI_SCRIPT && !NO_OUTPUT_BUFFERING) {
|
||||
debugging('progress_bar used in a non-CLI script without setting NO_OUTPUT_BUFFERING.', DEBUG_DEVELOPER);
|
||||
}
|
||||
|
||||
if (!empty($htmlid)) {
|
||||
$this->htmlid = $htmlid;
|
||||
} else {
|
||||
$this->htmlid = 'pbar_' . uniqid();
|
||||
}
|
||||
|
||||
$this->width = $width;
|
||||
|
||||
if ($autostart) {
|
||||
$this->create();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for ID
|
||||
* @return string id
|
||||
*/
|
||||
public function get_id(): string {
|
||||
return $this->htmlid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new progress bar, this function will output html.
|
||||
*
|
||||
* @return void Echo's output
|
||||
*/
|
||||
public function create() {
|
||||
global $OUTPUT;
|
||||
|
||||
$this->timestart = microtime(true);
|
||||
|
||||
flush();
|
||||
echo $OUTPUT->render($this);
|
||||
flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the progress bar.
|
||||
*
|
||||
* @param int $percent From 1-100.
|
||||
* @param string $msg The message.
|
||||
* @return void Echo's output
|
||||
* @throws coding_exception
|
||||
*/
|
||||
private function update_raw($percent, $msg) {
|
||||
global $OUTPUT;
|
||||
|
||||
if (empty($this->timestart)) {
|
||||
throw new coding_exception('You must call create() (or use the $autostart ' .
|
||||
'argument to the constructor) before you try updating the progress bar.');
|
||||
}
|
||||
|
||||
$estimate = $this->estimate($percent);
|
||||
|
||||
if ($estimate === null) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
|
||||
// Always do the first and last updates.
|
||||
} else if ($estimate == 0) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
|
||||
// Always do the last updates.
|
||||
} else if ($this->lastupdate + 20 < time()) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
|
||||
// We must update otherwise browser would time out.
|
||||
} else if (round($this->percent, 2) === round($percent, 2)) {
|
||||
// No significant change, no need to update anything.
|
||||
return;
|
||||
}
|
||||
|
||||
$estimatemsg = '';
|
||||
if ($estimate != 0 && is_numeric($estimate)) {
|
||||
// Err on the conservative side and also avoid showing 'now' as the estimate.
|
||||
$estimatemsg = format_time(ceil($estimate));
|
||||
}
|
||||
|
||||
$this->percent = $percent;
|
||||
$this->lastupdate = microtime(true);
|
||||
|
||||
echo $OUTPUT->render_progress_bar_update($this->htmlid, $this->percent, $msg, $estimatemsg);
|
||||
flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate how much time it is going to take.
|
||||
*
|
||||
* @param int $pt From 1-100.
|
||||
* @return mixed Null (unknown), or int.
|
||||
*/
|
||||
private function estimate($pt) {
|
||||
if ($this->lastupdate == 0) {
|
||||
return null;
|
||||
}
|
||||
if ($pt < 0.00001) {
|
||||
return null; // We do not know yet how long it will take.
|
||||
}
|
||||
if ($pt > 99.99999) {
|
||||
return 0; // Nearly done, right?
|
||||
}
|
||||
$consumed = microtime(true) - $this->timestart;
|
||||
if ($consumed < 0.001) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (100 - $pt) * ($consumed / $pt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update progress bar according percent.
|
||||
*
|
||||
* @param int $percent From 1-100.
|
||||
* @param string $msg The message needed to be shown.
|
||||
*/
|
||||
public function update_full($percent, $msg) {
|
||||
$percent = max(min($percent, 100), 0);
|
||||
$this->update_raw($percent, $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update progress bar according the number of tasks.
|
||||
*
|
||||
* @param int $cur Current task number.
|
||||
* @param int $total Total task number.
|
||||
* @param string $msg The message needed to be shown.
|
||||
*/
|
||||
public function update($cur, $total, $msg) {
|
||||
$percent = ($cur / $total) * 100;
|
||||
$this->update_full($percent, $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart the progress bar.
|
||||
*/
|
||||
public function restart() {
|
||||
$this->percent = 0;
|
||||
$this->lastupdate = 0;
|
||||
$this->timestart = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return array
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
return [
|
||||
'id' => $this->htmlid,
|
||||
'width' => $this->width,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(progress_bar::class, \progress_bar::class);
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Interface marking other classes as suitable for renderer_base::render()
|
||||
*
|
||||
* @copyright 2010 Petr Skoda (skodak) [email protected]
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
interface renderable {
|
||||
// Intentionally empty.
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(renderable::class, \renderable::class);
|
||||
@@ -0,0 +1,432 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\context\system as context_system;
|
||||
use core\exception\moodle_exception;
|
||||
use core\exception\coding_exception;
|
||||
use core\output\actions\component_action;
|
||||
use moodle_page;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
use Mustache_Exception_UnknownTemplateException;
|
||||
|
||||
/**
|
||||
* Simple base class for Moodle renderers.
|
||||
*
|
||||
* Tracks the xhtml_container_stack to use, which is passed in in the constructor.
|
||||
*
|
||||
* Also has methods to facilitate generating HTML output.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class renderer_base {
|
||||
/**
|
||||
* @var xhtml_container_stack The xhtml_container_stack to use.
|
||||
*/
|
||||
protected $opencontainers;
|
||||
|
||||
/**
|
||||
* @var moodle_page The Moodle page the renderer has been created to assist with.
|
||||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* @var string The requested rendering target.
|
||||
*/
|
||||
protected $target;
|
||||
|
||||
/**
|
||||
* @var \Mustache_Engine The mustache template compiler
|
||||
*/
|
||||
private $mustache;
|
||||
|
||||
/**
|
||||
* @var array $templatecache The mustache template cache.
|
||||
*/
|
||||
protected $templatecache = [];
|
||||
|
||||
/**
|
||||
* Return an instance of the mustache class.
|
||||
*
|
||||
* @since 2.9
|
||||
* @return \Mustache_Engine
|
||||
*/
|
||||
protected function get_mustache() {
|
||||
global $CFG;
|
||||
|
||||
if ($this->mustache === null) {
|
||||
require_once("{$CFG->libdir}/filelib.php");
|
||||
|
||||
$themename = $this->page->theme->name;
|
||||
$themerev = theme_get_revision();
|
||||
|
||||
// Create new localcache directory.
|
||||
$cachedir = make_localcache_directory("mustache/$themerev/$themename");
|
||||
|
||||
// Remove old localcache directories.
|
||||
$mustachecachedirs = glob("{$CFG->localcachedir}/mustache/*", GLOB_ONLYDIR);
|
||||
foreach ($mustachecachedirs as $localcachedir) {
|
||||
$cachedrev = [];
|
||||
preg_match("/\/mustache\/([0-9]+)$/", $localcachedir, $cachedrev);
|
||||
$cachedrev = isset($cachedrev[1]) ? intval($cachedrev[1]) : 0;
|
||||
if ($cachedrev > 0 && $cachedrev < $themerev) {
|
||||
fulldelete($localcachedir);
|
||||
}
|
||||
}
|
||||
|
||||
$loader = new mustache_filesystem_loader();
|
||||
$stringhelper = new mustache_string_helper();
|
||||
$cleanstringhelper = new mustache_clean_string_helper();
|
||||
$quotehelper = new mustache_quote_helper();
|
||||
$jshelper = new mustache_javascript_helper($this->page);
|
||||
$pixhelper = new mustache_pix_helper($this);
|
||||
$shortentexthelper = new mustache_shorten_text_helper();
|
||||
$userdatehelper = new mustache_user_date_helper();
|
||||
|
||||
// We only expose the variables that are exposed to JS templates.
|
||||
$safeconfig = $this->page->requires->get_config_for_javascript($this->page, $this);
|
||||
|
||||
$helpers = ['config' => $safeconfig,
|
||||
'str' => [$stringhelper, 'str'],
|
||||
'cleanstr' => [$cleanstringhelper, 'cleanstr'],
|
||||
'quote' => [$quotehelper, 'quote'],
|
||||
'js' => [$jshelper, 'help'],
|
||||
'pix' => [$pixhelper, 'pix'],
|
||||
'shortentext' => [$shortentexthelper, 'shorten'],
|
||||
'userdate' => [$userdatehelper, 'transform'],
|
||||
];
|
||||
|
||||
$this->mustache = new mustache_engine([
|
||||
'cache' => $cachedir,
|
||||
'escape' => 's',
|
||||
'loader' => $loader,
|
||||
'helpers' => $helpers,
|
||||
'pragmas' => [\Mustache_Engine::PRAGMA_BLOCKS],
|
||||
// Don't allow the JavaScript helper to be executed from within another
|
||||
// helper. If it's allowed it can be used by users to inject malicious
|
||||
// JS into the page.
|
||||
'disallowednestedhelpers' => ['js'],
|
||||
// Disable lambda rendering - content in helpers is already rendered, no need to render it again.
|
||||
'disable_lambda_rendering' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->mustache;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* The constructor takes two arguments. The first is the page that the renderer
|
||||
* has been created to assist with, and the second is the target.
|
||||
* The target is an additional identifier that can be used to load different
|
||||
* renderers for different options.
|
||||
*
|
||||
* @param moodle_page $page the page we are doing output for.
|
||||
* @param string $target one of rendering target constants
|
||||
*/
|
||||
public function __construct(moodle_page $page, $target) {
|
||||
$this->opencontainers = $page->opencontainers;
|
||||
$this->page = $page;
|
||||
$this->target = $target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a template by name with the given context.
|
||||
*
|
||||
* The provided data needs to be array/stdClass made up of only simple types.
|
||||
* Simple types are array,stdClass,bool,int,float,string
|
||||
*
|
||||
* @since 2.9
|
||||
* @param string $templatename The template to render
|
||||
* @param array|stdClass $context Context containing data for the template.
|
||||
* @return string|boolean
|
||||
*/
|
||||
public function render_from_template($templatename, $context) {
|
||||
$mustache = $this->get_mustache();
|
||||
|
||||
if ($mustache->hasHelper('uniqid')) {
|
||||
// Grab a copy of the existing helper to be restored later.
|
||||
$uniqidhelper = $mustache->getHelper('uniqid');
|
||||
} else {
|
||||
// Helper doesn't exist.
|
||||
$uniqidhelper = null;
|
||||
}
|
||||
|
||||
// Provide 1 random value that will not change within a template
|
||||
// but will be different from template to template. This is useful for
|
||||
// e.g. aria attributes that only work with id attributes and must be
|
||||
// unique in a page.
|
||||
$mustache->addHelper('uniqid', new mustache_uniqid_helper());
|
||||
if (isset($this->templatecache[$templatename])) {
|
||||
$template = $this->templatecache[$templatename];
|
||||
} else {
|
||||
try {
|
||||
$template = $mustache->loadTemplate($templatename);
|
||||
$this->templatecache[$templatename] = $template;
|
||||
} catch (Mustache_Exception_UnknownTemplateException $e) {
|
||||
throw new moodle_exception('Unknown template: ' . $templatename);
|
||||
}
|
||||
}
|
||||
|
||||
$renderedtemplate = trim($template->render($context));
|
||||
|
||||
// If we had an existing uniqid helper then we need to restore it to allow
|
||||
// handle nested calls of render_from_template.
|
||||
if ($uniqidhelper) {
|
||||
$mustache->addHelper('uniqid', $uniqidhelper);
|
||||
}
|
||||
|
||||
return $renderedtemplate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns rendered widget.
|
||||
*
|
||||
* The provided widget needs to be an object that extends the renderable
|
||||
* interface.
|
||||
* If will then be rendered by a method based upon the classname for the widget.
|
||||
* For instance a widget of class `crazywidget` will be rendered by a protected
|
||||
* render_crazywidget method of this renderer.
|
||||
* If no render_crazywidget method exists and crazywidget implements templatable,
|
||||
* look for the 'crazywidget' template in the same component and render that.
|
||||
*
|
||||
* @param renderable $widget instance with renderable interface
|
||||
* @return string
|
||||
*/
|
||||
public function render(renderable $widget) {
|
||||
$classparts = explode('\\', get_class($widget));
|
||||
// Strip namespaces.
|
||||
$classname = array_pop($classparts);
|
||||
// Remove _renderable suffixes.
|
||||
$classname = preg_replace('/_renderable$/', '', $classname);
|
||||
|
||||
$rendermethod = "render_{$classname}";
|
||||
if (method_exists($this, $rendermethod)) {
|
||||
// Call the render_[widget_name] function.
|
||||
// Note: This has a higher priority than the named_templatable to allow the theme to override the template.
|
||||
return $this->$rendermethod($widget);
|
||||
}
|
||||
|
||||
if ($widget instanceof named_templatable) {
|
||||
// This is a named templatable.
|
||||
// Fetch the template name from the get_template_name function instead.
|
||||
// Note: This has higher priority than the guessed template name.
|
||||
return $this->render_from_template(
|
||||
$widget->get_template_name($this),
|
||||
$widget->export_for_template($this)
|
||||
);
|
||||
}
|
||||
|
||||
if ($widget instanceof templatable) {
|
||||
// Guess the templat ename based on the class name.
|
||||
// Note: There's no benefit to moving this aboved the named_templatable and this approach is more costly.
|
||||
$component = array_shift($classparts);
|
||||
if (!$component) {
|
||||
$component = 'core';
|
||||
}
|
||||
$template = $component . '/' . $classname;
|
||||
$context = $widget->export_for_template($this);
|
||||
return $this->render_from_template($template, $context);
|
||||
}
|
||||
throw new coding_exception("Can not render widget, renderer method ('{$rendermethod}') not found.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a JS action for the element with the provided id.
|
||||
*
|
||||
* This method adds a JS event for the provided component action to the page
|
||||
* and then returns the id that the event has been attached to.
|
||||
* If no id has been provided then a new ID is generated by {@see html_writer::random_id()}
|
||||
*
|
||||
* @param component_action $action
|
||||
* @param string $id
|
||||
* @return string id of element, either original submitted or random new if not supplied
|
||||
*/
|
||||
public function add_action_handler(component_action $action, $id = null) {
|
||||
if (!$id) {
|
||||
$id = html_writer::random_id($action->event);
|
||||
}
|
||||
$this->page->requires->event_handler("#$id", $action->event, $action->jsfunction, $action->jsfunctionargs);
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true is output has already started, and false if not.
|
||||
*
|
||||
* @return boolean true if the header has been printed.
|
||||
*/
|
||||
public function has_started() {
|
||||
return $this->page->state >= moodle_page::STATE_IN_BODY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an array or space-separated list of classes, prepares and returns the HTML class attribute value
|
||||
*
|
||||
* @param mixed $classes Space-separated string or array of classes
|
||||
* @return string HTML class attribute value
|
||||
*/
|
||||
public static function prepare_classes($classes) {
|
||||
if (is_array($classes)) {
|
||||
return implode(' ', array_unique($classes));
|
||||
}
|
||||
return $classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the direct URL for an image from the pix folder.
|
||||
*
|
||||
* Use this function sparingly and never for icons. For icons use pix_icon or the pix helper in a mustache template.
|
||||
*
|
||||
* @deprecated since Moodle 3.3
|
||||
* @param string $imagename the name of the icon.
|
||||
* @param string $component specification of one plugin like in get_string()
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function pix_url($imagename, $component = 'moodle') {
|
||||
debugging('pix_url is deprecated. Use image_url for images and pix_icon for icons.', DEBUG_DEVELOPER);
|
||||
return $this->page->theme->image_url($imagename, $component);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the moodle_url for an image.
|
||||
*
|
||||
* The exact image location and extension is determined
|
||||
* automatically by searching for gif|png|jpg|jpeg, please
|
||||
* note there can not be diferent images with the different
|
||||
* extension. The imagename is for historical reasons
|
||||
* a relative path name, it may be changed later for core
|
||||
* images. It is recommended to not use subdirectories
|
||||
* in plugin and theme pix directories.
|
||||
*
|
||||
* There are three types of images:
|
||||
* 1/ theme images - stored in theme/mytheme/pix/,
|
||||
* use component 'theme'
|
||||
* 2/ core images - stored in /pix/,
|
||||
* overridden via theme/mytheme/pix_core/
|
||||
* 3/ plugin images - stored in mod/mymodule/pix,
|
||||
* overridden via theme/mytheme/pix_plugins/mod/mymodule/,
|
||||
* example: image_url('comment', 'mod_glossary')
|
||||
*
|
||||
* @param string $imagename the pathname of the image
|
||||
* @param string $component full plugin name (aka component) or 'theme'
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function image_url($imagename, $component = 'moodle') {
|
||||
return $this->page->theme->image_url($imagename, $component);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the site's logo URL, if any.
|
||||
*
|
||||
* @param int $maxwidth The maximum width, or null when the maximum width does not matter.
|
||||
* @param int $maxheight The maximum height, or null when the maximum height does not matter.
|
||||
* @return moodle_url|false
|
||||
*/
|
||||
public function get_logo_url($maxwidth = null, $maxheight = 200) {
|
||||
global $CFG;
|
||||
$logo = get_config('core_admin', 'logo');
|
||||
if (empty($logo)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 200px high is the default image size which should be displayed at 100px in the page to account for retina displays.
|
||||
// It's not worth the overhead of detecting and serving 2 different images based on the device.
|
||||
|
||||
// Hide the requested size in the file path.
|
||||
$filepath = ((int) $maxwidth . 'x' . (int) $maxheight) . '/';
|
||||
|
||||
// Use $CFG->themerev to prevent browser caching when the file changes.
|
||||
return moodle_url::make_pluginfile_url(
|
||||
context_system::instance()->id,
|
||||
'core_admin',
|
||||
'logo',
|
||||
$filepath,
|
||||
theme_get_revision(),
|
||||
$logo
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the site's compact logo URL, if any.
|
||||
*
|
||||
* @param int $maxwidth The maximum width, or null when the maximum width does not matter.
|
||||
* @param int $maxheight The maximum height, or null when the maximum height does not matter.
|
||||
* @return moodle_url|false
|
||||
*/
|
||||
public function get_compact_logo_url($maxwidth = 300, $maxheight = 300) {
|
||||
global $CFG;
|
||||
$logo = get_config('core_admin', 'logocompact');
|
||||
if (empty($logo)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Hide the requested size in the file path.
|
||||
$filepath = ((int) $maxwidth . 'x' . (int) $maxheight) . '/';
|
||||
|
||||
// Use $CFG->themerev to prevent browser caching when the file changes.
|
||||
return moodle_url::make_pluginfile_url(
|
||||
context_system::instance()->id,
|
||||
'core_admin',
|
||||
'logocompact',
|
||||
$filepath,
|
||||
theme_get_revision(),
|
||||
$logo
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we should display the logo in the navbar.
|
||||
*
|
||||
* We will when there are no main logos, and we have compact logo.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_display_navbar_logo() {
|
||||
$logo = $this->get_compact_logo_url();
|
||||
return !empty($logo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since Moodle 4.0
|
||||
*/
|
||||
#[\core\attribute\deprecated(null, reason: 'It is no longer used', since: '4.0', final: true)]
|
||||
public function should_display_main_logo() {
|
||||
\core\deprecation::emit_deprecation_if_present([self::class, __FUNCTION__]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the moodle page object.
|
||||
*
|
||||
* @return moodle_page
|
||||
*/
|
||||
public function get_page(): moodle_page {
|
||||
return $this->page;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(renderer_base::class, \renderer_base::class);
|
||||
@@ -0,0 +1,216 @@
|
||||
<?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 core\output\renderer_factory;
|
||||
|
||||
use core_component;
|
||||
use core\exception\coding_exception;
|
||||
use core\output\theme_config;
|
||||
|
||||
/**
|
||||
* This is a base class to help you implement the renderer_factory interface.
|
||||
*
|
||||
* It keeps a cache of renderers that have been constructed, so you only need
|
||||
* to construct each one once in you subclass.
|
||||
*
|
||||
* It also has a method to get the name of, and include the renderer.php with
|
||||
* the definition of, the standard renderer class for a given module.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
abstract class renderer_factory_base implements renderer_factory_interface {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param theme_config $theme the theme we belong to.
|
||||
*/
|
||||
public function __construct(
|
||||
/** @var theme_config The theme we belong to */
|
||||
protected theme_config $theme,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns suffix of renderer class expected for given target.
|
||||
*
|
||||
* @param string $target one of the renderer target constants, target is guessed if null used
|
||||
* @return array two element array, first element is target, second the target suffix string
|
||||
*/
|
||||
protected function get_target_suffix($target) {
|
||||
if (empty($target) || $target === RENDERER_TARGET_MAINTENANCE) {
|
||||
// If the target hasn't been specified we need to guess the defaults.
|
||||
// We also override the target with the default if the maintenance target has been provided.
|
||||
// This ensures we don't use the maintenance renderer if we are processing a special target.
|
||||
if (defined('PREFERRED_RENDERER_TARGET')) {
|
||||
$target = PREFERRED_RENDERER_TARGET;
|
||||
} else if (CLI_SCRIPT) {
|
||||
$target = RENDERER_TARGET_CLI;
|
||||
} else if (AJAX_SCRIPT) {
|
||||
$target = RENDERER_TARGET_AJAX;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($target) {
|
||||
case RENDERER_TARGET_CLI:
|
||||
$suffix = '_cli';
|
||||
break;
|
||||
case RENDERER_TARGET_AJAX:
|
||||
$suffix = '_ajax';
|
||||
break;
|
||||
case RENDERER_TARGET_TEXTEMAIL:
|
||||
$suffix = '_textemail';
|
||||
break;
|
||||
case RENDERER_TARGET_HTMLEMAIL:
|
||||
$suffix = '_htmlemail';
|
||||
break;
|
||||
case RENDERER_TARGET_MAINTENANCE:
|
||||
$suffix = '_maintenance';
|
||||
break;
|
||||
default:
|
||||
$target = RENDERER_TARGET_GENERAL;
|
||||
$suffix = '';
|
||||
}
|
||||
|
||||
return [$target, $suffix];
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given module name, return the possible class names
|
||||
* that defines the renderer interface for that module.
|
||||
*
|
||||
* Newer auto-loaded class names are returned as well as the old style _renderable classnames.
|
||||
*
|
||||
* Also, if it exists, include the renderer.php file for that module, so
|
||||
* the class definition of the default renderer has been loaded.
|
||||
*
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to:
|
||||
* '\mod_forum\output\news_renderer'
|
||||
* or '\mod_forum\output\news\renderer'
|
||||
* or non-autoloaded 'mod_forum_news'
|
||||
* @return array[] Each element of the array is an array with keys:
|
||||
* classname - The class name to search
|
||||
* autoloaded - Does this classname assume autoloading?
|
||||
* validwithprefix - Is this class name valid when a prefix is added to it?
|
||||
* validwithoutprefix - Is this class name valid when no prefix is added to it?
|
||||
* @throws coding_exception
|
||||
*/
|
||||
protected function standard_renderer_classnames($component, $subtype = null) {
|
||||
global $CFG; // Needed in included files.
|
||||
$classnames = [];
|
||||
|
||||
// Standardize component name ala frankenstyle.
|
||||
[$plugin, $type] = core_component::normalize_component($component);
|
||||
if ($type === null) {
|
||||
$component = $plugin;
|
||||
} else {
|
||||
$component = $plugin . '_' . $type;
|
||||
}
|
||||
|
||||
if ($component !== 'core') {
|
||||
// Renderers are stored in renderer.php files.
|
||||
if (!$compdirectory = core_component::get_component_directory($component)) {
|
||||
throw new coding_exception('Invalid component specified in renderer request', $component);
|
||||
}
|
||||
$rendererfile = $compdirectory . '/renderer.php';
|
||||
if (file_exists($rendererfile)) {
|
||||
include_once($rendererfile);
|
||||
}
|
||||
} else if (!empty($subtype)) {
|
||||
$coresubsystems = core_component::get_core_subsystems();
|
||||
if (!array_key_exists($subtype, $coresubsystems)) { // There may be nulls.
|
||||
throw new coding_exception('Invalid core subtype "' . $subtype . '" in renderer request', $subtype);
|
||||
}
|
||||
if ($coresubsystems[$subtype]) {
|
||||
$rendererfile = $coresubsystems[$subtype] . '/renderer.php';
|
||||
if (file_exists($rendererfile)) {
|
||||
include_once($rendererfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($subtype)) {
|
||||
// Theme specific auto-loaded name (only valid when prefixed with the theme name).
|
||||
$classnames[] = [
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => false,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\output\\' . $component . '_renderer',
|
||||
];
|
||||
|
||||
// Standard autoloaded plugin name (not valid with a prefix).
|
||||
$classnames[] = [
|
||||
'validwithprefix' => false,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\' . $component . '\\output\\renderer',
|
||||
];
|
||||
// Legacy class name - (valid with or without a prefix).
|
||||
$classnames[] = [
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => false,
|
||||
'classname' => $component . '_renderer',
|
||||
];
|
||||
} else {
|
||||
// Theme specific auto-loaded name (only valid when prefixed with the theme name).
|
||||
$classnames[] = [
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => false,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\output\\' . $component . '\\' . $subtype . '_renderer',
|
||||
];
|
||||
// Version of the above with subtype being a namespace level on it's own.
|
||||
$classnames[] = [
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => false,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\output\\' . $component . '\\' . $subtype . '\\renderer',
|
||||
];
|
||||
// Standard autoloaded plugin name (not valid with a prefix).
|
||||
$classnames[] = [
|
||||
'validwithprefix' => false,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\' . $component . '\\output\\' . $subtype . '_renderer',
|
||||
];
|
||||
// Version of the above with subtype being a namespace level on it's own.
|
||||
$classnames[] = [
|
||||
'validwithprefix' => false,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\' . $component . '\\output\\' . $subtype . '\\renderer',
|
||||
];
|
||||
// Legacy class name - (valid with or without a prefix).
|
||||
$classnames[] = [
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => false,
|
||||
'classname' => $component . '_' . $subtype . '_renderer',
|
||||
];
|
||||
}
|
||||
return $classnames;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(renderer_factory_base::class, \renderer_factory_base::class);
|
||||
@@ -0,0 +1,70 @@
|
||||
<?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 core\output\renderer_factory;
|
||||
|
||||
use moodle_page;
|
||||
use core\output\renderer_base;
|
||||
|
||||
/**
|
||||
* A renderer factory is just responsible for creating an appropriate renderer
|
||||
* for any given part of Moodle.
|
||||
*
|
||||
* Which renderer factory to use is chose by the current theme, and an instance
|
||||
* if created automatically when the theme is set up.
|
||||
*
|
||||
* A renderer factory must also have a constructor that takes a theme_config object.
|
||||
* (See {@see renderer_factory_base::__construct} for an example.)
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
interface renderer_factory_interface {
|
||||
/**
|
||||
* Return the renderer for a particular part of Moodle.
|
||||
*
|
||||
* The renderer interfaces are defined by classes called {plugin}_renderer
|
||||
* where {plugin} is the name of the component. The renderers for core Moodle are
|
||||
* defined in lib/renderer.php. For plugins, they will be defined in a file
|
||||
* called renderer.php inside the plugin.
|
||||
*
|
||||
* Renderers will normally want to subclass the renderer_base class.
|
||||
* (However, if you really know what you are doing, you don't have to do that.)
|
||||
*
|
||||
* There is no separate interface definition for renderers. The default
|
||||
* {plugin}_renderer implementation also serves to define the API for
|
||||
* other implementations of the interface, whether or not they subclass it.
|
||||
*
|
||||
* A particular plugin can define multiple renderers if it wishes, using the
|
||||
* $subtype parameter. For example workshop_renderer,
|
||||
* workshop_allocation_manual_renderer etc.
|
||||
*
|
||||
* @param moodle_page $page the page the renderer is outputting content for.
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
|
||||
* @param string $target one of rendering target constants
|
||||
* @return renderer_base an object implementing the requested renderer interface.
|
||||
*/
|
||||
public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null);
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(renderer_factory_interface::class, \renderer_factory::class);
|
||||
@@ -0,0 +1,90 @@
|
||||
<?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 core\output\renderer_factory;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use core\output\renderer_base;
|
||||
use moodle_page;
|
||||
|
||||
/**
|
||||
* This is the default renderer factory for Moodle.
|
||||
*
|
||||
* It simply returns an instance of the appropriate standard renderer class.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class standard_renderer_factory extends renderer_factory_base {
|
||||
/**
|
||||
* Implement the subclass method
|
||||
*
|
||||
* @param moodle_page $page the page the renderer is outputting content for.
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
|
||||
* @param string $target one of rendering target constants
|
||||
* @return renderer_base an object implementing the requested renderer interface.
|
||||
*/
|
||||
public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
|
||||
$classnames = $this->standard_renderer_classnames($component, $subtype);
|
||||
$classname = '';
|
||||
|
||||
[$target, $suffix] = $this->get_target_suffix($target);
|
||||
// First look for a version with a suffix.
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'] . $suffix;
|
||||
if (class_exists($newclassname)) {
|
||||
$classname = $newclassname;
|
||||
break;
|
||||
} else {
|
||||
$newclassname = $classnamedetails['classname'];
|
||||
if (class_exists($newclassname)) {
|
||||
$classname = $newclassname;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now look for a non-suffixed version.
|
||||
if (empty($classname)) {
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'];
|
||||
if (class_exists($newclassname)) {
|
||||
$classname = $newclassname;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($classname)) {
|
||||
// Standard renderer must always exist.
|
||||
throw new coding_exception('Request for an unknown renderer class. Searched for: ' . var_export($classnames, true));
|
||||
}
|
||||
|
||||
return new $classname($page, $target);
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(standard_renderer_factory::class, \standard_renderer_factory::class);
|
||||
@@ -0,0 +1,130 @@
|
||||
<?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 core\output\renderer_factory;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use core\output\renderer_base;
|
||||
use core\output\theme_config;
|
||||
use moodle_page;
|
||||
|
||||
/**
|
||||
* This is renderer factory allows themes to override the standard renderers using php code.
|
||||
*
|
||||
* It will load any code from theme/mytheme/renderers.php and
|
||||
* theme/parenttheme/renderers.php, if then exist. Then whenever you ask for
|
||||
* a renderer for 'component', it will create a mytheme_component_renderer or a
|
||||
* parenttheme_component_renderer, instead of a component_renderer,
|
||||
* if either of those classes exist.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class theme_overridden_renderer_factory extends renderer_factory_base {
|
||||
/**
|
||||
* @var array An array of renderer prefixes
|
||||
*/
|
||||
protected $prefixes = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param theme_config $theme the theme we are rendering for.
|
||||
*/
|
||||
public function __construct(theme_config $theme) {
|
||||
parent::__construct($theme);
|
||||
// Initialise $this->prefixes.
|
||||
$this->prefixes = $theme->renderer_prefixes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement the subclass method
|
||||
*
|
||||
* @param moodle_page $page the page the renderer is outputting content for.
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
|
||||
* @param string $target one of rendering target constants
|
||||
* @return renderer_base an object implementing the requested renderer interface.
|
||||
*/
|
||||
public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
|
||||
$classnames = $this->standard_renderer_classnames($component, $subtype);
|
||||
|
||||
[$target, $suffix] = $this->get_target_suffix($target);
|
||||
|
||||
// Theme lib.php and renderers.php files are loaded automatically
|
||||
// when loading the theme configs.
|
||||
|
||||
// First try the renderers with correct suffix.
|
||||
foreach ($this->prefixes as $prefix) {
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithprefix']) {
|
||||
if ($classnamedetails['autoloaded']) {
|
||||
$newclassname = $prefix . $classnamedetails['classname'] . $suffix;
|
||||
} else {
|
||||
$newclassname = $prefix . '_' . $classnamedetails['classname'] . $suffix;
|
||||
}
|
||||
if (class_exists($newclassname)) {
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'] . $suffix;
|
||||
if (class_exists($newclassname)) {
|
||||
// Use the specialised renderer for given target, default renderer might also decide
|
||||
// to implement support for more targets.
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then try general renderer.
|
||||
foreach ($this->prefixes as $prefix) {
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithprefix']) {
|
||||
if ($classnamedetails['autoloaded']) {
|
||||
$newclassname = $prefix . $classnamedetails['classname'];
|
||||
} else {
|
||||
$newclassname = $prefix . '_' . $classnamedetails['classname'];
|
||||
}
|
||||
if (class_exists($newclassname)) {
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final attempt - no prefix or suffix.
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'];
|
||||
if (class_exists($newclassname)) {
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new coding_exception('Request for an unknown renderer ' . $component . ', ' . $subtype . ', ' . $target);
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(theme_overridden_renderer_factory::class, \theme_overridden_renderer_factory::class);
|
||||
@@ -0,0 +1,126 @@
|
||||
<?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 core\output\requirements;
|
||||
|
||||
use html_writer;
|
||||
use js_writer;
|
||||
|
||||
/**
|
||||
* This requirements manager captures the appropriate html for creating a fragment to
|
||||
* be inserted elsewhere.
|
||||
*
|
||||
* @copyright 2016 Adrian Greeve <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.1
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class fragment_requirements_manager extends page_requirements_manager {
|
||||
/**
|
||||
* Page fragment constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
// As this is a fragment the header should already be done.
|
||||
$this->headdone = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns js code to load amd module loader, then insert inline script tags
|
||||
* that contain require() calls using RequireJS.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_amd_footercode() {
|
||||
global $CFG;
|
||||
$output = '';
|
||||
|
||||
// First include must be to a module with no dependencies, this prevents multiple requests.
|
||||
$prefix = 'M.util.js_pending("core/first");';
|
||||
$prefix .= "require(['core/first'], function() {\n";
|
||||
$suffix = "\n});";
|
||||
$suffix .= 'M.util.js_complete("core/first");';
|
||||
$output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate any HTML that needs to go at the end of the page.
|
||||
*
|
||||
* @return string the HTML code to to at the end of the page.
|
||||
*/
|
||||
public function get_end_code() {
|
||||
global $CFG;
|
||||
|
||||
$output = '';
|
||||
|
||||
// Call amd init functions.
|
||||
$output .= $this->get_amd_footercode();
|
||||
|
||||
// Add other requested modules.
|
||||
$output .= $this->get_extra_modules_code();
|
||||
|
||||
// All the other linked scripts - there should be as few as possible.
|
||||
if ($this->jsincludes['footer']) {
|
||||
foreach ($this->jsincludes['footer'] as $url) {
|
||||
$output .= html_writer::script('', $url);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->stringsforjs)) {
|
||||
// Add all needed strings.
|
||||
$strings = [];
|
||||
foreach ($this->stringsforjs as $component => $v) {
|
||||
foreach ($v as $indentifier => $langstring) {
|
||||
$strings[$component][$indentifier] = $langstring->out();
|
||||
}
|
||||
}
|
||||
// Append don't overwrite.
|
||||
$output .= html_writer::script('require(["jquery"], function($) {
|
||||
M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
|
||||
});');
|
||||
}
|
||||
|
||||
// Add variables.
|
||||
if ($this->jsinitvariables['footer']) {
|
||||
$js = '';
|
||||
foreach ($this->jsinitvariables['footer'] as $data) {
|
||||
[$var, $value] = $data;
|
||||
$js .= js_writer::set_variable($var, $value, true);
|
||||
}
|
||||
$output .= html_writer::script($js);
|
||||
}
|
||||
|
||||
$inyuijs = $this->get_javascript_code(false);
|
||||
$ondomreadyjs = $this->get_javascript_code(true);
|
||||
// See if this is still needed when we get to the ajax page.
|
||||
$jsinit = $this->get_javascript_init_code();
|
||||
$handlersjs = $this->get_event_handler_code();
|
||||
|
||||
// There is a global Y, make sure it is available in your scope.
|
||||
$js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
|
||||
|
||||
$output .= html_writer::script($js);
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(fragment_requirements_manager::class, \fragment_requirements_manager::class);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,379 @@
|
||||
<?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 core\output\requirements;
|
||||
|
||||
use cache;
|
||||
use core_component;
|
||||
use core_minify;
|
||||
use core\exception\coding_exception;
|
||||
use DirectoryIterator;
|
||||
|
||||
/**
|
||||
* This class represents the YUI configuration.
|
||||
*
|
||||
* @copyright 2013 Andrew Nicols
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.5
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class yui {
|
||||
/**
|
||||
* These settings must be public so that when the object is converted to json they are exposed.
|
||||
* Note: Some of these are camelCase because YUI uses camelCase variable names.
|
||||
*
|
||||
* The settings are described and documented in the YUI API at:
|
||||
* - http://yuilibrary.com/yui/docs/api/classes/config.html
|
||||
* - http://yuilibrary.com/yui/docs/api/classes/Loader.html
|
||||
*/
|
||||
public $debug = false;
|
||||
public $base;
|
||||
public $comboBase;
|
||||
public $combine;
|
||||
public $filter = null;
|
||||
public $insertBefore = 'firstthemesheet';
|
||||
public $groups = [];
|
||||
public $modules = [];
|
||||
/** @var array The log sources that should be not be logged. */
|
||||
public $logInclude = [];
|
||||
/** @var array Tog sources that should be logged. */
|
||||
public $logExclude = [];
|
||||
/** @var string The minimum log level for YUI logging statements. */
|
||||
public $logLevel;
|
||||
|
||||
/**
|
||||
* @var array List of functions used by the YUI Loader group pattern recognition.
|
||||
*/
|
||||
protected $jsconfigfunctions = [];
|
||||
|
||||
/**
|
||||
* Create a new group within the YUI_config system.
|
||||
*
|
||||
* @param string $name The name of the group. This must be unique and
|
||||
* not previously used.
|
||||
* @param array $config The configuration for this group.
|
||||
* @return void
|
||||
*/
|
||||
public function add_group($name, $config) {
|
||||
if (isset($this->groups[$name])) {
|
||||
throw new coding_exception(
|
||||
"A YUI configuration group for '{$name}' already exists. " .
|
||||
'To make changes to this group use YUI_config->update_group().',
|
||||
);
|
||||
}
|
||||
$this->groups[$name] = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing group configuration
|
||||
*
|
||||
* Note, any existing configuration for that group will be wiped out.
|
||||
* This includes module configuration.
|
||||
*
|
||||
* @param string $name The name of the group. This must be unique and
|
||||
* not previously used.
|
||||
* @param array $config The configuration for this group.
|
||||
* @return void
|
||||
*/
|
||||
public function update_group($name, $config) {
|
||||
if (!isset($this->groups[$name])) {
|
||||
throw new coding_exception(
|
||||
'The Moodle YUI module does not exist. ' .
|
||||
'You must define the moodle module config using YUI_config->add_module_config first.',
|
||||
);
|
||||
}
|
||||
$this->groups[$name] = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a configuration function used by the YUI Loader's pattern testing.
|
||||
*
|
||||
* Only the body of the function should be passed, and not the whole function wrapper.
|
||||
*
|
||||
* The JS function your write will be passed a single argument 'name' containing the
|
||||
* name of the module being loaded.
|
||||
*
|
||||
* @param string $function String the body of the JavaScript function. This should be used i
|
||||
* @return string the name of the function to use in the group pattern configuration.
|
||||
*/
|
||||
public function set_config_function($function) {
|
||||
$configname = 'yui' . (count($this->jsconfigfunctions) + 1) . 'ConfigFn';
|
||||
if (isset($this->jsconfigfunctions[$configname])) {
|
||||
throw new coding_exception(
|
||||
"A YUI config function with this name already exists. Config function names must be unique.",
|
||||
);
|
||||
}
|
||||
$this->jsconfigfunctions[$configname] = $function;
|
||||
return '@' . $configname . '@';
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow setting of the config function described in {@see set_config_function} from a file.
|
||||
* The contents of this file are then passed to set_config_function.
|
||||
*
|
||||
* When jsrev is positive, the function is minified and stored in a MUC cache for subsequent uses.
|
||||
*
|
||||
* @param string $file The path to the JavaScript function used for YUI configuration.
|
||||
* @return string the name of the function to use in the group pattern configuration.
|
||||
*/
|
||||
public function set_config_source($file) {
|
||||
global $CFG;
|
||||
$cache = cache::make('core', 'yuimodules');
|
||||
|
||||
// Attempt to get the metadata from the cache.
|
||||
$keyname = 'configfn_' . $file;
|
||||
$fullpath = $CFG->dirroot . '/' . $file;
|
||||
if (!isset($CFG->jsrev) || $CFG->jsrev == -1) {
|
||||
$cache->delete($keyname);
|
||||
$configfn = file_get_contents($fullpath);
|
||||
} else {
|
||||
$configfn = $cache->get($keyname);
|
||||
if ($configfn === false) {
|
||||
require_once($CFG->libdir . '/jslib.php');
|
||||
$configfn = core_minify::js_files([$fullpath]);
|
||||
$cache->set($keyname, $configfn);
|
||||
}
|
||||
}
|
||||
return $this->set_config_function($configfn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of JavaScript functions for YUI_config groups.
|
||||
*
|
||||
* @return string The complete set of config functions
|
||||
*/
|
||||
public function get_config_functions() {
|
||||
$configfunctions = '';
|
||||
foreach ($this->jsconfigfunctions as $functionname => $function) {
|
||||
$configfunctions .= "var {$functionname} = function(me) {";
|
||||
$configfunctions .= $function;
|
||||
$configfunctions .= "};\n";
|
||||
}
|
||||
return $configfunctions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the header JavaScript with any required modification for the YUI Loader.
|
||||
*
|
||||
* @param string $js String The JavaScript to manipulate.
|
||||
* @return string the modified JS string.
|
||||
*/
|
||||
public function update_header_js($js) {
|
||||
// Update the names of the the configFn variables.
|
||||
// The PHP json_encode function cannot handle literal names so we have to wrap
|
||||
// them in @ and then replace them with literals of the same function name.
|
||||
foreach ($this->jsconfigfunctions as $functionname => $function) {
|
||||
$js = str_replace('"@' . $functionname . '@"', $functionname, $js);
|
||||
}
|
||||
return $js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configuration for a specific module.
|
||||
*
|
||||
* @param string $name The name of the module to add configuration for.
|
||||
* @param array $config The configuration for the specified module.
|
||||
* @param string $group The name of the group to add configuration for.
|
||||
* If not specified, then this module is added to the global
|
||||
* configuration.
|
||||
* @return void
|
||||
*/
|
||||
public function add_module_config($name, $config, $group = null) {
|
||||
if ($group) {
|
||||
if (!isset($this->groups[$name])) {
|
||||
throw new coding_exception(
|
||||
'The Moodle YUI module does not exist. ' .
|
||||
'You must define the moodle module config using YUI_config->add_module_config first.',
|
||||
);
|
||||
}
|
||||
if (!isset($this->groups[$group]['modules'])) {
|
||||
$this->groups[$group]['modules'] = [];
|
||||
}
|
||||
$modules = &$this->groups[$group]['modules'];
|
||||
} else {
|
||||
$modules = &$this->modules;
|
||||
}
|
||||
$modules[$name] = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the moodle YUI module metadata for the moodle group to the YUI_config instance.
|
||||
*
|
||||
* If js caching is disabled, metadata will not be served causing YUI to calculate
|
||||
* module dependencies as each module is loaded.
|
||||
*
|
||||
* If metadata does not exist it will be created and stored in a MUC entry.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_moodle_metadata() {
|
||||
global $CFG;
|
||||
if (!isset($this->groups['moodle'])) {
|
||||
throw new coding_exception(
|
||||
'The Moodle YUI module does not exist. ' .
|
||||
'You must define the moodle module config using YUI_config->add_module_config first.',
|
||||
);
|
||||
}
|
||||
|
||||
if (!isset($this->groups['moodle']['modules'])) {
|
||||
$this->groups['moodle']['modules'] = [];
|
||||
}
|
||||
|
||||
$cache = cache::make('core', 'yuimodules');
|
||||
if (!isset($CFG->jsrev) || $CFG->jsrev == -1) {
|
||||
$metadata = [];
|
||||
$metadata = $this->get_moodle_metadata();
|
||||
$cache->delete('metadata');
|
||||
} else {
|
||||
// Attempt to get the metadata from the cache.
|
||||
if (!$metadata = $cache->get('metadata')) {
|
||||
$metadata = $this->get_moodle_metadata();
|
||||
$cache->set('metadata', $metadata);
|
||||
}
|
||||
}
|
||||
|
||||
// Merge with any metadata added specific to this page which was added manually.
|
||||
$this->groups['moodle']['modules'] = array_merge(
|
||||
$this->groups['moodle']['modules'],
|
||||
$metadata
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the module metadata for all moodle YUI modules.
|
||||
*
|
||||
* This works through all modules capable of serving YUI modules, and attempts to get
|
||||
* metadata for each of those modules.
|
||||
*
|
||||
* @return array of module metadata
|
||||
*/
|
||||
private function get_moodle_metadata() {
|
||||
$moodlemodules = [];
|
||||
// Core isn't a plugin type or subsystem - handle it seperately.
|
||||
if ($module = $this->get_moodle_path_metadata(core_component::get_component_directory('core'))) {
|
||||
$moodlemodules = array_merge($moodlemodules, $module);
|
||||
}
|
||||
|
||||
// Handle other core subsystems.
|
||||
$subsystems = core_component::get_core_subsystems();
|
||||
foreach ($subsystems as $subsystem => $path) {
|
||||
if (is_null($path)) {
|
||||
continue;
|
||||
}
|
||||
if ($module = $this->get_moodle_path_metadata($path)) {
|
||||
$moodlemodules = array_merge($moodlemodules, $module);
|
||||
}
|
||||
}
|
||||
|
||||
// And finally the plugins.
|
||||
$plugintypes = core_component::get_plugin_types();
|
||||
foreach ($plugintypes as $plugintype => $pathroot) {
|
||||
$pluginlist = core_component::get_plugin_list($plugintype);
|
||||
foreach ($pluginlist as $plugin => $path) {
|
||||
if ($module = $this->get_moodle_path_metadata($path)) {
|
||||
$moodlemodules = array_merge($moodlemodules, $module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $moodlemodules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function process and return the YUI metadata for all of the modules under the specified path.
|
||||
*
|
||||
* @param string $path the UNC path to the YUI src directory.
|
||||
* @return array the complete array for frankenstyle directory.
|
||||
*/
|
||||
private function get_moodle_path_metadata($path) {
|
||||
// Add module metadata is stored in frankenstyle_modname/yui/src/yui_modname/meta/yui_modname.json.
|
||||
$baseyui = $path . '/yui/src';
|
||||
$modules = [];
|
||||
if (is_dir($baseyui)) {
|
||||
$items = new DirectoryIterator($baseyui);
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDot() || !$item->isDir()) {
|
||||
continue;
|
||||
}
|
||||
$metafile = realpath($baseyui . '/' . $item . '/meta/' . $item . '.json');
|
||||
if (!is_readable($metafile)) {
|
||||
continue;
|
||||
}
|
||||
$metadata = file_get_contents($metafile);
|
||||
$modules = array_merge($modules, (array) json_decode($metadata));
|
||||
}
|
||||
}
|
||||
return $modules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define YUI modules which we have been required to patch between releases.
|
||||
*
|
||||
* We must do this because we aggressively cache content on the browser, and we must also override use of the
|
||||
* external CDN which will serve the true authoritative copy of the code without our patches.
|
||||
*
|
||||
* @param string $combobase The local combobase
|
||||
* @param string $yuiversion The current YUI version
|
||||
* @param int $patchlevel The patch level we're working to for YUI
|
||||
* @param array $patchedmodules An array containing the names of the patched modules
|
||||
* @return void
|
||||
*/
|
||||
public function define_patched_core_modules($combobase, $yuiversion, $patchlevel, $patchedmodules) {
|
||||
// The version we use is suffixed with a patchlevel so that we can get additional revisions between YUI releases.
|
||||
$subversion = $yuiversion . '_' . $patchlevel;
|
||||
|
||||
if ($this->comboBase == $combobase) {
|
||||
// If we are using the local combobase in the loader, we can add a group and still make use of the combo
|
||||
// loader. We just need to specify a different root which includes a slightly different YUI version number
|
||||
// to include our patchlevel.
|
||||
$patterns = [];
|
||||
$modules = [];
|
||||
foreach ($patchedmodules as $modulename) {
|
||||
// We must define the pattern and module here so that the loader uses our group configuration instead of
|
||||
// the standard module definition. We may lose some metadata provided by upstream but this will be
|
||||
// loaded when the module is loaded anyway.
|
||||
$patterns[$modulename] = [
|
||||
'group' => 'yui-patched',
|
||||
];
|
||||
$modules[$modulename] = [];
|
||||
}
|
||||
|
||||
// Actually add the patch group here.
|
||||
$this->add_group('yui-patched', [
|
||||
'combine' => true,
|
||||
'root' => $subversion . '/',
|
||||
'patterns' => $patterns,
|
||||
'modules' => $modules,
|
||||
]);
|
||||
} else {
|
||||
// The CDN is in use - we need to instead use the local combobase for this module and override the modules
|
||||
// definition. We cannot use the local base - we must use the combobase because we cannot invalidate the
|
||||
// local base in browser caches.
|
||||
$fullpathbase = $combobase . $subversion . '/';
|
||||
foreach ($patchedmodules as $modulename) {
|
||||
$this->modules[$modulename] = [
|
||||
'fullpath' => $fullpathbase . $modulename . '/' . $modulename . '-min.js',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(yui::class, \YUI_config::class);
|
||||
@@ -18,8 +18,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use renderer_base;
|
||||
|
||||
/**
|
||||
* A single-select combobox widget that is functionally similar to an HTML select element.
|
||||
*
|
||||
@@ -28,7 +26,7 @@ use renderer_base;
|
||||
* @copyright 2022 Shamim Rezaie <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class select_menu implements \renderable, \templatable {
|
||||
class select_menu implements renderable, templatable {
|
||||
/** @var array List of options. */
|
||||
protected $options;
|
||||
|
||||
@@ -85,7 +83,7 @@ class select_menu implements \renderable, \templatable {
|
||||
'name' => $groupname,
|
||||
'isgroup' => true,
|
||||
'id' => \html_writer::random_id('select-menu-group'),
|
||||
'options' => []
|
||||
'options' => [],
|
||||
];
|
||||
}
|
||||
foreach ($optoptions as $optvalue => $optoption) {
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\output\actions\confirm_action;
|
||||
use core\output\actions\component_action;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Data structure representing a simple form with only one button.
|
||||
*
|
||||
* @copyright 2009 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class single_button implements renderable {
|
||||
/**
|
||||
* Possible button types. From boostrap.
|
||||
*/
|
||||
const BUTTON_TYPES = [
|
||||
self::BUTTON_PRIMARY,
|
||||
self::BUTTON_SECONDARY,
|
||||
self::BUTTON_SUCCESS,
|
||||
self::BUTTON_DANGER,
|
||||
self::BUTTON_WARNING,
|
||||
self::BUTTON_INFO,
|
||||
];
|
||||
|
||||
/**
|
||||
* Possible button types - Primary.
|
||||
*/
|
||||
const BUTTON_PRIMARY = 'primary';
|
||||
/**
|
||||
* Possible button types - Secondary.
|
||||
*/
|
||||
const BUTTON_SECONDARY = 'secondary';
|
||||
/**
|
||||
* Possible button types - Danger.
|
||||
*/
|
||||
const BUTTON_DANGER = 'danger';
|
||||
/**
|
||||
* Possible button types - Success.
|
||||
*/
|
||||
const BUTTON_SUCCESS = 'success';
|
||||
/**
|
||||
* Possible button types - Warning.
|
||||
*/
|
||||
const BUTTON_WARNING = 'warning';
|
||||
/**
|
||||
* Possible button types - Info.
|
||||
*/
|
||||
const BUTTON_INFO = 'info';
|
||||
|
||||
/**
|
||||
* @var moodle_url Target url
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* @var string Button label
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* @var string Form submit method post or get
|
||||
*/
|
||||
public $method = 'post';
|
||||
|
||||
/**
|
||||
* @var string Wrapping div class
|
||||
*/
|
||||
public $class = 'singlebutton';
|
||||
|
||||
/**
|
||||
* @var string Type of button (from defined types). Used for styling.
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @var bool True if button is primary button. Used for styling.
|
||||
* @deprecated since Moodle 4.2
|
||||
*/
|
||||
private $primary = false;
|
||||
|
||||
/**
|
||||
* @var bool True if button disabled, false if normal
|
||||
*/
|
||||
public $disabled = false;
|
||||
|
||||
/**
|
||||
* @var string Button tooltip
|
||||
*/
|
||||
public $tooltip = null;
|
||||
|
||||
/**
|
||||
* @var string Form id
|
||||
*/
|
||||
public $formid;
|
||||
|
||||
/**
|
||||
* @var array List of attached actions
|
||||
*/
|
||||
public $actions = [];
|
||||
|
||||
/**
|
||||
* @var array $params URL Params
|
||||
*/
|
||||
public $params;
|
||||
|
||||
/**
|
||||
* @var string Action id
|
||||
*/
|
||||
public $actionid;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param moodle_url $url
|
||||
* @param string $label button text
|
||||
* @param string $method get or post submit method
|
||||
* @param string $type whether this is a primary button or another type, used for styling
|
||||
* @param array $attributes Attributes for the HTML button tag
|
||||
*/
|
||||
public function __construct(
|
||||
moodle_url $url,
|
||||
$label,
|
||||
$method = 'post',
|
||||
$type = self::BUTTON_SECONDARY,
|
||||
$attributes = []
|
||||
) {
|
||||
if (is_bool($type)) {
|
||||
debugging('The boolean $primary is deprecated and replaced by $type,
|
||||
use single_button::BUTTON_PRIMARY or self::BUTTON_SECONDARY instead');
|
||||
$type = $type ? self::BUTTON_PRIMARY : self::BUTTON_SECONDARY;
|
||||
}
|
||||
$this->url = clone($url);
|
||||
$this->label = $label;
|
||||
$this->method = $method;
|
||||
$this->type = $type;
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for adding a JS confirm dialog when the button is clicked.
|
||||
* The message must be a yes/no question.
|
||||
*
|
||||
* @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
|
||||
*/
|
||||
public function add_confirm_action($confirmmessage) {
|
||||
$this->add_action(new confirm_action($confirmmessage));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add action to the button.
|
||||
* @param component_action $action
|
||||
*/
|
||||
public function add_action(component_action $action) {
|
||||
$this->actions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an attribute for the HTML button tag.
|
||||
*
|
||||
* @param string $name The attribute name
|
||||
* @param mixed $value The value
|
||||
* @return null
|
||||
*/
|
||||
public function set_attribute($name, $value) {
|
||||
$this->attributes[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic setter method.
|
||||
*
|
||||
* This method manages access to some properties and will display deprecation message when accessing 'primary' property.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __set($name, $value) {
|
||||
switch ($name) {
|
||||
case 'primary':
|
||||
debugging('The primary field is deprecated, use the type field instead');
|
||||
// Here just in case we modified the primary field from outside {@see \mod_quiz_renderer::summary_page_controls}.
|
||||
$this->type = $value ? self::BUTTON_PRIMARY : self::BUTTON_SECONDARY;
|
||||
break;
|
||||
case 'type':
|
||||
$this->type = in_array($value, self::BUTTON_TYPES) ? $value : self::BUTTON_SECONDARY;
|
||||
break;
|
||||
default:
|
||||
$this->$name = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method getter.
|
||||
*
|
||||
* This method manages access to some properties and will display deprecation message when accessing 'primary' property.
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name) {
|
||||
switch ($name) {
|
||||
case 'primary':
|
||||
debugging('The primary field is deprecated, use type field instead');
|
||||
return $this->type == self::BUTTON_PRIMARY;
|
||||
case 'type':
|
||||
return $this->type;
|
||||
default:
|
||||
return $this->$name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export data.
|
||||
*
|
||||
* @param renderer_base $output Renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$url = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
|
||||
|
||||
$data = new stdClass();
|
||||
$data->id = html_writer::random_id('single_button');
|
||||
$data->formid = $this->formid;
|
||||
$data->method = $this->method;
|
||||
$data->url = $url === '' ? '#' : $url;
|
||||
$data->label = $this->label;
|
||||
$data->classes = $this->class;
|
||||
$data->disabled = $this->disabled;
|
||||
$data->tooltip = $this->tooltip;
|
||||
$data->type = $this->type;
|
||||
$data->attributes = [];
|
||||
foreach ($this->attributes as $key => $value) {
|
||||
$data->attributes[] = ['name' => $key, 'value' => $value];
|
||||
}
|
||||
|
||||
// Form parameters.
|
||||
$actionurl = new moodle_url($this->url);
|
||||
if ($this->method === 'post') {
|
||||
$actionurl->param('sesskey', sesskey());
|
||||
}
|
||||
$data->params = $actionurl->export_params_for_template();
|
||||
|
||||
// Button actions.
|
||||
$actions = $this->actions;
|
||||
$data->actions = array_map(function ($action) use ($output) {
|
||||
return $action->export_for_template($output);
|
||||
}, $actions);
|
||||
$data->hasactions = !empty($data->actions);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(single_button::class, \single_button::class);
|
||||
@@ -0,0 +1,306 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use core\output\actions\component_action;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Simple form with just one select field that gets submitted automatically.
|
||||
*
|
||||
* If JS not enabled small go button is printed too.
|
||||
*
|
||||
* @copyright 2009 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class single_select implements renderable, templatable {
|
||||
/**
|
||||
* @var moodle_url Target url - includes hidden fields
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* @var string Name of the select element.
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var array $options associative array value=>label ex.: array(1=>'One, 2=>Two)
|
||||
* it is also possible to specify optgroup as complex label array ex.:
|
||||
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
|
||||
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
|
||||
*/
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* @var string Selected option
|
||||
*/
|
||||
public $selected;
|
||||
|
||||
/**
|
||||
* @var array Nothing selected
|
||||
*/
|
||||
public $nothing;
|
||||
|
||||
/**
|
||||
* @var array Extra select field attributes
|
||||
*/
|
||||
public $attributes = [];
|
||||
|
||||
/**
|
||||
* @var string Button label
|
||||
*/
|
||||
public $label = '';
|
||||
|
||||
/**
|
||||
* @var array Button label's attributes
|
||||
*/
|
||||
public $labelattributes = [];
|
||||
|
||||
/**
|
||||
* @var string Form submit method post or get
|
||||
*/
|
||||
public $method = 'get';
|
||||
|
||||
/**
|
||||
* @var string Wrapping div class
|
||||
*/
|
||||
public $class = 'singleselect';
|
||||
|
||||
/**
|
||||
* @var bool True if button disabled, false if normal
|
||||
*/
|
||||
public $disabled = false;
|
||||
|
||||
/**
|
||||
* @var string Button tooltip
|
||||
*/
|
||||
public $tooltip = null;
|
||||
|
||||
/**
|
||||
* @var string Form id
|
||||
*/
|
||||
public $formid = null;
|
||||
|
||||
/**
|
||||
* @var help_icon The help icon for this element.
|
||||
*/
|
||||
public $helpicon = null;
|
||||
|
||||
/** @var component_action[] component action. */
|
||||
public $actions = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param moodle_url $url form action target, includes hidden fields
|
||||
* @param string $name name of selection field - the changing parameter in url
|
||||
* @param array $options list of options
|
||||
* @param string $selected selected element
|
||||
* @param ?array $nothing
|
||||
* @param string $formid
|
||||
*/
|
||||
public function __construct(
|
||||
moodle_url $url,
|
||||
$name,
|
||||
array $options,
|
||||
$selected = '',
|
||||
$nothing = ['' => 'choosedots'],
|
||||
$formid = null,
|
||||
) {
|
||||
$this->url = $url;
|
||||
$this->name = $name;
|
||||
$this->options = $options;
|
||||
$this->selected = $selected;
|
||||
$this->nothing = $nothing;
|
||||
$this->formid = $formid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for adding a JS confirm dialog when the button is clicked.
|
||||
* The message must be a yes/no question.
|
||||
*
|
||||
* @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
|
||||
*/
|
||||
public function add_confirm_action($confirmmessage) {
|
||||
$this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', ['message' => $confirmmessage]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add action to the button.
|
||||
*
|
||||
* @param component_action $action
|
||||
*/
|
||||
public function add_action(component_action $action) {
|
||||
$this->actions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds help icon.
|
||||
*
|
||||
* @deprecated since Moodle 2.0
|
||||
*/
|
||||
public function set_old_help_icon($helppage, $title, $component = 'moodle') {
|
||||
throw new coding_exception('set_old_help_icon() can not be used any more, please see set_help_icon().');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds help icon.
|
||||
*
|
||||
* @param string $identifier The keyword that defines a help page
|
||||
* @param string $component
|
||||
*/
|
||||
public function set_help_icon($identifier, $component = 'moodle') {
|
||||
$this->helpicon = new help_icon($identifier, $component);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets select's label
|
||||
*
|
||||
* @param string $label
|
||||
* @param array $attributes (optional)
|
||||
*/
|
||||
public function set_label($label, $attributes = []) {
|
||||
$this->label = $label;
|
||||
$this->labelattributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export data.
|
||||
*
|
||||
* @param renderer_base $output Renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$attributes = $this->attributes;
|
||||
|
||||
$data = new stdClass();
|
||||
$data->name = $this->name;
|
||||
$data->method = $this->method;
|
||||
$data->action = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
|
||||
$data->classes = $this->class;
|
||||
$data->label = $this->label;
|
||||
$data->disabled = $this->disabled;
|
||||
$data->title = $this->tooltip;
|
||||
$data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('single_select_f');
|
||||
$data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('single_select');
|
||||
|
||||
// Select element attributes.
|
||||
// Unset attributes that are already predefined in the template.
|
||||
unset($attributes['id']);
|
||||
unset($attributes['class']);
|
||||
unset($attributes['name']);
|
||||
unset($attributes['title']);
|
||||
unset($attributes['disabled']);
|
||||
|
||||
// Map the attributes.
|
||||
$data->attributes = array_map(function ($key) use ($attributes) {
|
||||
return ['name' => $key, 'value' => $attributes[$key]];
|
||||
}, array_keys($attributes));
|
||||
|
||||
// Form parameters.
|
||||
$actionurl = new moodle_url($this->url);
|
||||
if ($this->method === 'post') {
|
||||
$actionurl->param('sesskey', sesskey());
|
||||
}
|
||||
$data->params = $actionurl->export_params_for_template();
|
||||
|
||||
// Select options.
|
||||
$hasnothing = false;
|
||||
if (is_string($this->nothing) && $this->nothing !== '') {
|
||||
$nothing = ['' => $this->nothing];
|
||||
$hasnothing = true;
|
||||
$nothingkey = '';
|
||||
} else if (is_array($this->nothing)) {
|
||||
$nothingvalue = reset($this->nothing);
|
||||
if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') {
|
||||
$nothing = [key($this->nothing) => get_string('choosedots')];
|
||||
} else {
|
||||
$nothing = $this->nothing;
|
||||
}
|
||||
$hasnothing = true;
|
||||
$nothingkey = key($this->nothing);
|
||||
}
|
||||
if ($hasnothing) {
|
||||
$options = $nothing + $this->options;
|
||||
} else {
|
||||
$options = $this->options;
|
||||
}
|
||||
|
||||
foreach ($options as $value => $name) {
|
||||
if (is_array($options[$value])) {
|
||||
foreach ($options[$value] as $optgroupname => $optgroupvalues) {
|
||||
$sublist = [];
|
||||
foreach ($optgroupvalues as $optvalue => $optname) {
|
||||
$option = [
|
||||
'value' => $optvalue,
|
||||
'name' => $optname,
|
||||
'selected' => strval($this->selected) === strval($optvalue),
|
||||
];
|
||||
|
||||
if ($hasnothing && $nothingkey === $optvalue) {
|
||||
$option['ignore'] = 'data-ignore';
|
||||
}
|
||||
|
||||
$sublist[] = $option;
|
||||
}
|
||||
$data->options[] = [
|
||||
'name' => $optgroupname,
|
||||
'optgroup' => true,
|
||||
'options' => $sublist,
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$option = [
|
||||
'value' => $value,
|
||||
'name' => $options[$value],
|
||||
'selected' => strval($this->selected) === strval($value),
|
||||
'optgroup' => false,
|
||||
];
|
||||
|
||||
if ($hasnothing && $nothingkey === $value) {
|
||||
$option['ignore'] = 'data-ignore';
|
||||
}
|
||||
|
||||
$data->options[] = $option;
|
||||
}
|
||||
}
|
||||
|
||||
// Label attributes.
|
||||
$data->labelattributes = [];
|
||||
// Unset label attributes that are already in the template.
|
||||
unset($this->labelattributes['for']);
|
||||
// Map the label attributes.
|
||||
foreach ($this->labelattributes as $key => $value) {
|
||||
$data->labelattributes[] = ['name' => $key, 'value' => $value];
|
||||
}
|
||||
|
||||
// Help icon.
|
||||
$data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(single_select::class, \single_select::class);
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
use renderable;
|
||||
|
||||
/**
|
||||
* Class to render a sticky footer element.
|
||||
*
|
||||
@@ -35,7 +33,6 @@ use renderable;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class sticky_footer implements named_templatable, renderable {
|
||||
|
||||
/**
|
||||
* @var string content of the sticky footer.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
<?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 core\output;
|
||||
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* Stores one tab
|
||||
*
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @copyright Marina Glancy
|
||||
*/
|
||||
class tabobject implements renderable, templatable {
|
||||
/** @var string unique id of the tab in this tree, it is used to find selected and/or inactive tabs */
|
||||
public $id;
|
||||
/** @var moodle_url|string link */
|
||||
public $link;
|
||||
/** @var string text on the tab */
|
||||
public $text;
|
||||
/** @var string title under the link, by defaul equals to text */
|
||||
public $title;
|
||||
/** @var bool whether to display a link under the tab name when it's selected */
|
||||
public $linkedwhenselected = false;
|
||||
/** @var bool whether the tab is inactive */
|
||||
public $inactive = false;
|
||||
/** @var bool indicates that this tab's child is selected */
|
||||
public $activated = false;
|
||||
/** @var bool indicates that this tab is selected */
|
||||
public $selected = false;
|
||||
/** @var array stores children tabobjects */
|
||||
public $subtree = [];
|
||||
/** @var int level of tab in the tree, 0 for root (instance of tabtree), 1 for the first row of tabs */
|
||||
public $level = 1;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $id unique id of the tab in this tree, it is used to find selected and/or inactive tabs
|
||||
* @param string|moodle_url $link
|
||||
* @param string $text text on the tab
|
||||
* @param string $title title under the link, by defaul equals to text
|
||||
* @param bool $linkedwhenselected whether to display a link under the tab name when it's selected
|
||||
*/
|
||||
public function __construct($id, $link = null, $text = '', $title = '', $linkedwhenselected = false) {
|
||||
$this->id = $id;
|
||||
$this->link = $link;
|
||||
$this->text = $text;
|
||||
$this->title = $title ? $title : $text;
|
||||
$this->linkedwhenselected = $linkedwhenselected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Travels through tree and finds the tab to mark as selected, all parents are automatically marked as activated
|
||||
*
|
||||
* @param string $selected the id of the selected tab (whatever row it's on),
|
||||
* if null marks all tabs as unselected
|
||||
* @return bool whether this tab is selected or contains selected tab in its subtree
|
||||
*/
|
||||
protected function set_selected($selected) {
|
||||
if ((string)$selected === (string)$this->id) {
|
||||
$this->selected = true;
|
||||
// This tab is selected. No need to travel through subtree.
|
||||
return true;
|
||||
}
|
||||
foreach ($this->subtree as $subitem) {
|
||||
if ($subitem->set_selected($selected)) {
|
||||
// This tab has child that is selected. Mark it as activated. No need to check other children.
|
||||
$this->activated = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Travels through tree and finds a tab with specified id
|
||||
*
|
||||
* @param string $id
|
||||
* @return tabtree|null
|
||||
*/
|
||||
public function find($id) {
|
||||
if ((string)$this->id === (string)$id) {
|
||||
return $this;
|
||||
}
|
||||
foreach ($this->subtree as $tab) {
|
||||
if ($obj = $tab->find($id)) {
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to mark each tab's level in the tree before rendering.
|
||||
*
|
||||
* @param int $level
|
||||
*/
|
||||
protected function set_level($level) {
|
||||
$this->level = $level;
|
||||
foreach ($this->subtree as $tab) {
|
||||
$tab->set_level($level + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output Renderer.
|
||||
* @return object
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
if ($this->inactive || ($this->selected && !$this->linkedwhenselected) || $this->activated) {
|
||||
$link = null;
|
||||
} else {
|
||||
$link = $this->link;
|
||||
}
|
||||
$active = $this->activated || $this->selected;
|
||||
|
||||
return (object) [
|
||||
'id' => $this->id,
|
||||
'link' => is_object($link) ? $link->out(false) : $link,
|
||||
'text' => $this->text,
|
||||
'title' => $this->title,
|
||||
'inactive' => !$active && $this->inactive,
|
||||
'active' => $active,
|
||||
'level' => $this->level,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(tabobject::class, \tabobject::class);
|
||||
@@ -0,0 +1,98 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Stores tabs list
|
||||
*
|
||||
* Example how to print a single line tabs:
|
||||
* $rows = array(
|
||||
* new tabobject(...),
|
||||
* new tabobject(...)
|
||||
* );
|
||||
* echo $OUTPUT->tabtree($rows, $selectedid);
|
||||
*
|
||||
* Multiple row tabs may not look good on some devices but if you want to use them
|
||||
* you can specify ->subtree for the active tabobject.
|
||||
*
|
||||
* @copyright 2013 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.5
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class tabtree extends tabobject {
|
||||
/**
|
||||
* Constuctor
|
||||
*
|
||||
* It is highly recommended to call constructor when list of tabs is already
|
||||
* populated, this way you ensure that selected and inactive tabs are located
|
||||
* and attribute level is set correctly.
|
||||
*
|
||||
* @param array $tabs array of tabs, each of them may have it's own ->subtree
|
||||
* @param string|null $selected which tab to mark as selected, all parent tabs will
|
||||
* automatically be marked as activated
|
||||
* @param array|string|null $inactive list of ids of inactive tabs, regardless of
|
||||
* their level. Note that you can as weel specify tabobject::$inactive for separate instances
|
||||
*/
|
||||
public function __construct($tabs, $selected = null, $inactive = null) {
|
||||
$this->subtree = $tabs;
|
||||
if ($selected !== null) {
|
||||
$this->set_selected($selected);
|
||||
}
|
||||
if ($inactive !== null) {
|
||||
if (is_array($inactive)) {
|
||||
foreach ($inactive as $id) {
|
||||
if ($tab = $this->find($id)) {
|
||||
$tab->inactive = true;
|
||||
}
|
||||
}
|
||||
} else if ($tab = $this->find($inactive)) {
|
||||
$tab->inactive = true;
|
||||
}
|
||||
}
|
||||
$this->set_level(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output Renderer.
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$tabs = [];
|
||||
$secondrow = false;
|
||||
|
||||
foreach ($this->subtree as $tab) {
|
||||
$tabs[] = $tab->export_for_template($output);
|
||||
if (!empty($tab->subtree) && ($tab->level == 0 || $tab->selected || $tab->activated)) {
|
||||
$secondrow = new tabtree($tab->subtree);
|
||||
}
|
||||
}
|
||||
|
||||
return (object) [
|
||||
'tabs' => $tabs,
|
||||
'secondrow' => $secondrow ? $secondrow->export_for_template($output) : false,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(tabtree::class, \tabtree::class);
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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 core\output;
|
||||
|
||||
/**
|
||||
* Interface marking other classes having the ability to export their data for use by templates.
|
||||
*
|
||||
* @copyright 2015 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
* @since 2.9
|
||||
*/
|
||||
interface templatable {
|
||||
/**
|
||||
* Function to export the renderer data in a format that is suitable for a
|
||||
* mustache template. This means:
|
||||
* 1. No complex types - only stdClass, array, int, string, float, bool
|
||||
* 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
|
||||
*
|
||||
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
|
||||
* @return \stdClass|array
|
||||
*/
|
||||
public function export_for_template(renderer_base $output);
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(templatable::class, \templatable::class);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -25,7 +25,6 @@ namespace core\output;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class theme_usage {
|
||||
|
||||
/** @var string The theme usage type for users. */
|
||||
public const THEME_USAGE_TYPE_USER = 'user';
|
||||
|
||||
@@ -61,7 +60,6 @@ class theme_usage {
|
||||
$isused = $cache->get($themename);
|
||||
|
||||
if ($isused === false) {
|
||||
|
||||
$sqlunions = [];
|
||||
|
||||
// For each context, check if the config is enabled and there is at least one use.
|
||||
@@ -119,7 +117,6 @@ class theme_usage {
|
||||
// Cache the result so we don't have to keep checking for this theme.
|
||||
$cache->set($themename, $isused);
|
||||
return $isused;
|
||||
|
||||
} else {
|
||||
return $isused;
|
||||
}
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
|
||||
namespace core\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* URL rewriter interface
|
||||
*
|
||||
@@ -36,7 +34,6 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
interface url_rewriter {
|
||||
|
||||
/**
|
||||
* Rewrite moodle_urls into another form.
|
||||
*
|
||||
@@ -52,7 +49,4 @@ interface url_rewriter {
|
||||
* @return void
|
||||
*/
|
||||
public static function html_head_setup();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\exception\coding_exception;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Simple URL selection widget description.
|
||||
*
|
||||
* @copyright 2009 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class url_select implements renderable, templatable {
|
||||
/**
|
||||
* @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two)
|
||||
* it is also possible to specify optgroup as complex label array ex.:
|
||||
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
|
||||
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
|
||||
*/
|
||||
public $urls;
|
||||
|
||||
/**
|
||||
* @var string Selected option
|
||||
*/
|
||||
public $selected;
|
||||
|
||||
/**
|
||||
* @var array Nothing selected
|
||||
*/
|
||||
public $nothing;
|
||||
|
||||
/**
|
||||
* @var array Extra select field attributes
|
||||
*/
|
||||
public $attributes = [];
|
||||
|
||||
/**
|
||||
* @var string Button label
|
||||
*/
|
||||
public $label = '';
|
||||
|
||||
/**
|
||||
* @var array Button label's attributes
|
||||
*/
|
||||
public $labelattributes = [];
|
||||
|
||||
/**
|
||||
* @var string Wrapping div class
|
||||
*/
|
||||
public $class = 'urlselect';
|
||||
|
||||
/**
|
||||
* @var bool True if button disabled, false if normal
|
||||
*/
|
||||
public $disabled = false;
|
||||
|
||||
/**
|
||||
* @var string Button tooltip
|
||||
*/
|
||||
public $tooltip = null;
|
||||
|
||||
/**
|
||||
* @var string Form id
|
||||
*/
|
||||
public $formid = null;
|
||||
|
||||
/**
|
||||
* @var help_icon The help icon for this element.
|
||||
*/
|
||||
public $helpicon = null;
|
||||
|
||||
/**
|
||||
* @var string If set, makes button visible with given name for button
|
||||
*/
|
||||
public $showbutton = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param array $urls list of options
|
||||
* @param string $selected selected element
|
||||
* @param array $nothing
|
||||
* @param string $formid
|
||||
* @param string $showbutton Set to text of button if it should be visible
|
||||
* or null if it should be hidden (hidden version always has text 'go')
|
||||
*/
|
||||
public function __construct(array $urls, $selected = '', $nothing = ['' => 'choosedots'], $formid = null, $showbutton = null) {
|
||||
$this->urls = $urls;
|
||||
$this->selected = $selected;
|
||||
$this->nothing = $nothing;
|
||||
$this->formid = $formid;
|
||||
$this->showbutton = $showbutton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds help icon.
|
||||
*
|
||||
* @deprecated since Moodle 2.0
|
||||
*/
|
||||
public function set_old_help_icon($helppage, $title, $component = 'moodle') {
|
||||
throw new coding_exception('set_old_help_icon() can not be used any more, please see set_help_icon().');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds help icon.
|
||||
*
|
||||
* @param string $identifier The keyword that defines a help page
|
||||
* @param string $component
|
||||
*/
|
||||
public function set_help_icon($identifier, $component = 'moodle') {
|
||||
$this->helpicon = new help_icon($identifier, $component);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets select's label
|
||||
*
|
||||
* @param string $label
|
||||
* @param array $attributes (optional)
|
||||
*/
|
||||
public function set_label($label, $attributes = []) {
|
||||
$this->label = $label;
|
||||
$this->labelattributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean a URL.
|
||||
*
|
||||
* @param string $value The URL.
|
||||
* @return string The cleaned URL.
|
||||
*/
|
||||
protected function clean_url($value) {
|
||||
global $CFG;
|
||||
|
||||
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
|
||||
if (empty($value)) {
|
||||
// Nothing.
|
||||
} else if (strpos($value, $CFG->wwwroot . '/') === 0) {
|
||||
$value = str_replace($CFG->wwwroot, '', $value);
|
||||
} else if (strpos($value, '/') !== 0) {
|
||||
debugging("Invalid url_select urls parameter: url '$value' is not local relative url!", DEBUG_DEVELOPER);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten the options for Mustache.
|
||||
*
|
||||
* This also cleans the URLs.
|
||||
*
|
||||
* @param array $options The options.
|
||||
* @param array $nothing The nothing option.
|
||||
* @return array
|
||||
*/
|
||||
protected function flatten_options($options, $nothing) {
|
||||
$flattened = [];
|
||||
|
||||
foreach ($options as $value => $option) {
|
||||
if (is_array($option)) {
|
||||
foreach ($option as $groupname => $optoptions) {
|
||||
if (!isset($flattened[$groupname])) {
|
||||
$flattened[$groupname] = [
|
||||
'name' => $groupname,
|
||||
'isgroup' => true,
|
||||
'options' => [],
|
||||
];
|
||||
}
|
||||
foreach ($optoptions as $optvalue => $optoption) {
|
||||
$cleanedvalue = $this->clean_url($optvalue);
|
||||
$flattened[$groupname]['options'][$cleanedvalue] = [
|
||||
'name' => $optoption,
|
||||
'value' => $cleanedvalue,
|
||||
'selected' => $this->selected == $optvalue,
|
||||
];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$cleanedvalue = $this->clean_url($value);
|
||||
$flattened[$cleanedvalue] = [
|
||||
'name' => $option,
|
||||
'value' => $cleanedvalue,
|
||||
'selected' => $this->selected == $value,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($nothing)) {
|
||||
$value = key($nothing);
|
||||
$name = reset($nothing);
|
||||
$flattened = [
|
||||
$value => ['name' => $name, 'value' => $value, 'selected' => $this->selected == $value],
|
||||
] + $flattened;
|
||||
}
|
||||
|
||||
// Make non-associative array.
|
||||
foreach ($flattened as $key => $value) {
|
||||
if (!empty($value['options'])) {
|
||||
$flattened[$key]['options'] = array_values($value['options']);
|
||||
}
|
||||
}
|
||||
$flattened = array_values($flattened);
|
||||
|
||||
return $flattened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output Renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$attributes = $this->attributes;
|
||||
|
||||
$data = new stdClass();
|
||||
$data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('url_select_f');
|
||||
$data->classes = $this->class;
|
||||
$data->label = $this->label;
|
||||
$data->disabled = $this->disabled;
|
||||
$data->title = $this->tooltip;
|
||||
$data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('url_select');
|
||||
$data->sesskey = sesskey();
|
||||
$data->action = (new moodle_url('/course/jumpto.php'))->out(false);
|
||||
|
||||
// Remove attributes passed as property directly.
|
||||
unset($attributes['class']);
|
||||
unset($attributes['id']);
|
||||
unset($attributes['name']);
|
||||
unset($attributes['title']);
|
||||
unset($attributes['disabled']);
|
||||
|
||||
$data->showbutton = $this->showbutton;
|
||||
|
||||
// Select options.
|
||||
$nothing = false;
|
||||
if (is_string($this->nothing) && $this->nothing !== '') {
|
||||
$nothing = ['' => $this->nothing];
|
||||
} else if (is_array($this->nothing)) {
|
||||
$nothingvalue = reset($this->nothing);
|
||||
if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') {
|
||||
$nothing = [key($this->nothing) => get_string('choosedots')];
|
||||
} else {
|
||||
$nothing = $this->nothing;
|
||||
}
|
||||
}
|
||||
$data->options = $this->flatten_options($this->urls, $nothing);
|
||||
|
||||
// Label attributes.
|
||||
$data->labelattributes = [];
|
||||
// Unset label attributes that are already in the template.
|
||||
unset($this->labelattributes['for']);
|
||||
// Map the label attributes.
|
||||
foreach ($this->labelattributes as $key => $value) {
|
||||
$data->labelattributes[] = ['name' => $key, 'value' => $value];
|
||||
}
|
||||
|
||||
// Help icon.
|
||||
$data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
|
||||
|
||||
// Finally all the remaining attributes.
|
||||
$data->attributes = [];
|
||||
foreach ($attributes as $key => $value) {
|
||||
$data->attributes[] = ['name' => $key, 'value' => $value];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(url_select::class, \url_select::class);
|
||||
@@ -0,0 +1,346 @@
|
||||
<?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 core\output;
|
||||
|
||||
use core\context\user as context_user;
|
||||
use core\exception\coding_exception;
|
||||
use moodle_page;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Data structure representing a user picture.
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault, 2010 Petr Skoda
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Modle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class user_picture implements renderable {
|
||||
/**
|
||||
* @var stdClass A user object with at least fields all columns specified
|
||||
* in $fields array constant set.
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* @var int The course id. Used when constructing the link to the user's
|
||||
* profile, page course id used if not specified.
|
||||
*/
|
||||
public $courseid;
|
||||
|
||||
/**
|
||||
* @var bool Add course profile link to image
|
||||
*/
|
||||
public $link = true;
|
||||
|
||||
/**
|
||||
* @var int Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatibility.
|
||||
* Recommended values (supporting user initials too): 16, 35, 64 and 100.
|
||||
*/
|
||||
public $size = 35;
|
||||
|
||||
/**
|
||||
* @var bool Add non-blank alt-text to the image.
|
||||
* Default true, set to false when image alt just duplicates text in screenreaders.
|
||||
*/
|
||||
public $alttext = true;
|
||||
|
||||
/**
|
||||
* @var bool Whether or not to open the link in a popup window.
|
||||
*/
|
||||
public $popup = false;
|
||||
|
||||
/**
|
||||
* @var string Image class attribute
|
||||
*/
|
||||
public $class = 'userpicture';
|
||||
|
||||
/**
|
||||
* @var bool Whether to be visible to screen readers.
|
||||
*/
|
||||
public $visibletoscreenreaders = true;
|
||||
|
||||
/**
|
||||
* @var bool Whether to include the fullname in the user picture link.
|
||||
*/
|
||||
public $includefullname = false;
|
||||
|
||||
/**
|
||||
* @var mixed Include user authentication token. True indicates to generate a token for current user, and integer value
|
||||
* indicates to generate a token for the user whose id is the value indicated.
|
||||
*/
|
||||
public $includetoken = false;
|
||||
|
||||
/**
|
||||
* User picture constructor.
|
||||
*
|
||||
* @param stdClass $user user record with at least id, picture, imagealt, firstname and lastname set.
|
||||
* It is recommended to add also contextid of the user for performance reasons.
|
||||
*/
|
||||
public function __construct(stdClass $user) {
|
||||
global $DB;
|
||||
|
||||
if (empty($user->id)) {
|
||||
throw new coding_exception('User id is required when printing user avatar image.');
|
||||
}
|
||||
|
||||
// Only touch the DB if we are missing data and complain loudly...
|
||||
$needrec = false;
|
||||
foreach (\core_user\fields::get_picture_fields() as $field) {
|
||||
if (!property_exists($user, $field)) {
|
||||
$needrec = true;
|
||||
debugging(
|
||||
"Missing {$field} property in {$user} object, "
|
||||
. "this is a performance problem that needs to be fixed by a developer. "
|
||||
. 'Please use the \core_user\fields API to get the full list of required fields.',
|
||||
DEBUG_DEVELOPER,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($needrec) {
|
||||
$this->user = $DB->get_record(
|
||||
'user',
|
||||
['id' => $user->id],
|
||||
implode(',', \core_user\fields::get_picture_fields()),
|
||||
MUST_EXIST
|
||||
);
|
||||
} else {
|
||||
$this->user = clone($user);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of required user fields, useful when fetching required user info from db.
|
||||
*
|
||||
* In some cases we have to fetch the user data together with some other information,
|
||||
* the idalias is useful there because the id would otherwise override the main
|
||||
* id of the result record. Please note it has to be converted back to id before rendering.
|
||||
*
|
||||
* @param string $tableprefix name of database table prefix in query
|
||||
* @param null|array $extrafields extra fields to be included in result
|
||||
* Do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE.
|
||||
* @param string $idalias alias of id field
|
||||
* @param string $fieldprefix prefix to add to all columns in their aliases, does not apply to 'id'
|
||||
* @return string
|
||||
* @deprecated since Moodle 3.11 MDL-45242
|
||||
* @see \core_user\fields
|
||||
*/
|
||||
public static function fields(
|
||||
$tableprefix = '',
|
||||
?array $extrafields = null,
|
||||
$idalias = 'id',
|
||||
$fieldprefix = '',
|
||||
) {
|
||||
debugging('user_picture::fields() is deprecated. Please use the \core_user\fields API instead.', DEBUG_DEVELOPER);
|
||||
$userfields = \core_user\fields::for_userpic();
|
||||
if ($extrafields) {
|
||||
$userfields->including(...$extrafields);
|
||||
}
|
||||
$selects = $userfields->get_sql($tableprefix, false, $fieldprefix, $idalias, false)->selects;
|
||||
if ($tableprefix === '') {
|
||||
// If no table alias is specified, don't add {user}. in front of fields.
|
||||
$selects = str_replace('{user}.', '', $selects);
|
||||
}
|
||||
// Maintain legacy behaviour where the field list was done with 'implode' and no spaces.
|
||||
$selects = str_replace(', ', ',', $selects);
|
||||
return $selects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the aliased user fields from a given record
|
||||
*
|
||||
* Given a record that was previously obtained using {@see self::fields()} with aliases,
|
||||
* this method extracts user related unaliased fields.
|
||||
*
|
||||
* @param stdClass $record containing user picture fields
|
||||
* @param null|array $extrafields extra fields included in the $record
|
||||
* @param string $idalias alias of the id field
|
||||
* @param string $fieldprefix prefix added to all columns in their aliases, does not apply to 'id'
|
||||
* @return stdClass object with unaliased user fields
|
||||
*/
|
||||
public static function unalias(
|
||||
stdClass $record,
|
||||
?array $extrafields = null,
|
||||
$idalias = 'id',
|
||||
$fieldprefix = '',
|
||||
) {
|
||||
if (empty($idalias)) {
|
||||
$idalias = 'id';
|
||||
}
|
||||
|
||||
$return = new stdClass();
|
||||
|
||||
foreach (\core_user\fields::get_picture_fields() as $field) {
|
||||
if ($field === 'id') {
|
||||
if (property_exists($record, $idalias)) {
|
||||
$return->id = $record->{$idalias};
|
||||
}
|
||||
} else {
|
||||
if (property_exists($record, $fieldprefix . $field)) {
|
||||
$return->{$field} = $record->{$fieldprefix . $field};
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add extra fields if not already there.
|
||||
if ($extrafields) {
|
||||
foreach ($extrafields as $e) {
|
||||
if ($e === 'id' || property_exists($return, $e)) {
|
||||
continue;
|
||||
}
|
||||
$return->{$e} = $record->{$fieldprefix . $e};
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Works out the URL for the users picture.
|
||||
*
|
||||
* This method is recommended as it avoids costly redirects of user pictures
|
||||
* if requests are made for non-existent files etc.
|
||||
*
|
||||
* @param moodle_page $page
|
||||
* @param null|renderer_base $renderer
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url(
|
||||
moodle_page $page,
|
||||
?renderer_base $renderer = null,
|
||||
) {
|
||||
global $CFG;
|
||||
|
||||
if (is_null($renderer)) {
|
||||
$renderer = $page->get_renderer('core');
|
||||
}
|
||||
|
||||
// Sort out the filename and size. Size is only required for the gravatar
|
||||
// implementation presently.
|
||||
if (empty($this->size)) {
|
||||
$filename = 'f2';
|
||||
$size = 35;
|
||||
} else if ($this->size === true || $this->size == 1) {
|
||||
$filename = 'f1';
|
||||
$size = 100;
|
||||
} else if ($this->size > 100) {
|
||||
$filename = 'f3';
|
||||
$size = (int)$this->size;
|
||||
} else if ($this->size >= 50) {
|
||||
$filename = 'f1';
|
||||
$size = (int)$this->size;
|
||||
} else {
|
||||
$filename = 'f2';
|
||||
$size = (int)$this->size;
|
||||
}
|
||||
|
||||
$defaulturl = $renderer->image_url('u/' . $filename); // Default image.
|
||||
|
||||
if (
|
||||
(!empty($CFG->forcelogin) && !isloggedin()) ||
|
||||
(!empty($CFG->forceloginforprofileimage) && (!isloggedin() || isguestuser()))
|
||||
) {
|
||||
// Protect images if login required and not logged in;
|
||||
// also if login is required for profile images and is not logged in or guest
|
||||
// do not use require_login() because it is expensive and not suitable here anyway.
|
||||
return $defaulturl;
|
||||
}
|
||||
|
||||
// First try to detect deleted users - but do not read from database for performance reasons!
|
||||
if (!empty($this->user->deleted) || !str_contains($this->user->email, '@')) {
|
||||
// All deleted users should have email replaced by md5 hash,
|
||||
// all active users are expected to have valid email.
|
||||
return $defaulturl;
|
||||
}
|
||||
|
||||
// Did the user upload a picture?
|
||||
if ($this->user->picture > 0) {
|
||||
if (!empty($this->user->contextid)) {
|
||||
$contextid = $this->user->contextid;
|
||||
} else {
|
||||
$context = context_user::instance($this->user->id, IGNORE_MISSING);
|
||||
if (!$context) {
|
||||
// This must be an incorrectly deleted user, all other users have context.
|
||||
return $defaulturl;
|
||||
}
|
||||
$contextid = $context->id;
|
||||
}
|
||||
|
||||
$path = '/';
|
||||
if (clean_param($page->theme->name, PARAM_THEME) == $page->theme->name) {
|
||||
// We append the theme name to the file path if we have it so that
|
||||
// in the circumstance that the profile picture is not available
|
||||
// when the user actually requests it they still get the profile
|
||||
// picture for the correct theme.
|
||||
$path .= $page->theme->name . '/';
|
||||
}
|
||||
// Set the image URL to the URL for the uploaded file and return.
|
||||
$url = moodle_url::make_pluginfile_url(
|
||||
$contextid,
|
||||
'user',
|
||||
'icon',
|
||||
null,
|
||||
$path,
|
||||
$filename,
|
||||
false,
|
||||
$this->includetoken
|
||||
);
|
||||
$url->param('rev', $this->user->picture);
|
||||
return $url;
|
||||
}
|
||||
|
||||
if ($this->user->picture == 0 && !empty($CFG->enablegravatar)) {
|
||||
// Normalise the size variable to acceptable bounds.
|
||||
if ($size < 1 || $size > 512) {
|
||||
$size = 35;
|
||||
}
|
||||
// Hash the users email address.
|
||||
$md5 = md5(strtolower(trim($this->user->email)));
|
||||
// Build a gravatar URL with what we know.
|
||||
|
||||
// Find the best default image URL we can (MDL-35669).
|
||||
if (empty($CFG->gravatardefaulturl)) {
|
||||
$absoluteimagepath = $page->theme->resolve_image_location('u/' . $filename, 'core');
|
||||
if (strpos($absoluteimagepath, $CFG->dirroot) === 0) {
|
||||
$gravatardefault = $CFG->wwwroot . substr($absoluteimagepath, strlen($CFG->dirroot));
|
||||
} else {
|
||||
$gravatardefault = $CFG->wwwroot . '/pix/u/' . $filename . '.png';
|
||||
}
|
||||
} else {
|
||||
$gravatardefault = $CFG->gravatardefaulturl;
|
||||
}
|
||||
|
||||
// If the currently requested page is https then we'll return an
|
||||
// https gravatar page.
|
||||
if (is_https()) {
|
||||
return new moodle_url("https://secure.gravatar.com/avatar/{$md5}", ['s' => $size, 'd' => $gravatardefault]);
|
||||
} else {
|
||||
return new moodle_url("http://www.gravatar.com/avatar/{$md5}", ['s' => $size, 'd' => $gravatardefault]);
|
||||
}
|
||||
}
|
||||
|
||||
return $defaulturl;
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(user_picture::class, \user_picture::class);
|
||||
@@ -0,0 +1,162 @@
|
||||
<?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 core\output;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* This class keeps track of which HTML tags are currently open.
|
||||
*
|
||||
* This makes it much easier to always generate well formed XHTML output, even
|
||||
* if execution terminates abruptly. Any time you output some opening HTML
|
||||
* without the matching closing HTML, you should push the necessary close tags
|
||||
* onto the stack.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class xhtml_container_stack {
|
||||
/**
|
||||
* @var array Stores the list of open containers.
|
||||
*/
|
||||
protected $opencontainers = [];
|
||||
|
||||
/**
|
||||
* @var array In developer debug mode, stores a stack trace of all opens and
|
||||
* closes, so we can output helpful error messages when there is a mismatch.
|
||||
*/
|
||||
protected $log = [];
|
||||
|
||||
/**
|
||||
* @var bool Store whether we are developer debug mode. We need this in
|
||||
* several places including in the destructor where we may not have access to $CFG.
|
||||
*/
|
||||
protected $isdebugging;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
global $CFG;
|
||||
$this->isdebugging = $CFG->debugdeveloper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push the close HTML for a recently opened container onto the stack.
|
||||
*
|
||||
* @param string $type The type of container. This is checked when {@see pop()}
|
||||
* is called and must match, otherwise a developer debug warning is output.
|
||||
* @param string $closehtml The HTML required to close the container.
|
||||
*/
|
||||
public function push($type, $closehtml) {
|
||||
$container = new stdClass();
|
||||
$container->type = $type;
|
||||
$container->closehtml = $closehtml;
|
||||
if ($this->isdebugging) {
|
||||
$this->log('Open', $type);
|
||||
}
|
||||
array_push($this->opencontainers, $container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop the HTML for the next closing container from the stack. The $type
|
||||
* must match the type passed when the container was opened, otherwise a
|
||||
* warning will be output.
|
||||
*
|
||||
* @param string $type The type of container.
|
||||
* @return ?string the HTML required to close the container.
|
||||
*/
|
||||
public function pop($type) {
|
||||
if (empty($this->opencontainers)) {
|
||||
debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
|
||||
$this->output_log(), DEBUG_DEVELOPER);
|
||||
return;
|
||||
}
|
||||
|
||||
$container = array_pop($this->opencontainers);
|
||||
if ($container->type != $type) {
|
||||
debugging('<p>The type of container to be closed (' . $container->type .
|
||||
') does not match the type of the next open container (' . $type .
|
||||
'). This suggests there is a nesting problem.</p>' .
|
||||
$this->output_log(), DEBUG_DEVELOPER);
|
||||
}
|
||||
if ($this->isdebugging) {
|
||||
$this->log('Close', $type);
|
||||
}
|
||||
return $container->closehtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all but the last open container. This is useful in places like error
|
||||
* handling, where you want to close all the open containers (apart from <body>)
|
||||
* before outputting the error message.
|
||||
*
|
||||
* @param bool $shouldbenone assert that the stack should be empty now - causes a
|
||||
* developer debug warning if it isn't.
|
||||
* @return string the HTML required to close any open containers inside <body>.
|
||||
*/
|
||||
public function pop_all_but_last($shouldbenone = false) {
|
||||
if ($shouldbenone && count($this->opencontainers) != 1) {
|
||||
debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
|
||||
$this->output_log(), DEBUG_DEVELOPER);
|
||||
}
|
||||
$output = '';
|
||||
while (count($this->opencontainers) > 1) {
|
||||
$container = array_pop($this->opencontainers);
|
||||
$output .= $container->closehtml;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* You can call this function if you want to throw away an instance of this
|
||||
* class without properly emptying the stack (for example, in a unit test).
|
||||
* Calling this method stops the destruct method from outputting a developer
|
||||
* debug warning. After calling this method, the instance can no longer be used.
|
||||
*/
|
||||
public function discard() {
|
||||
$this->opencontainers = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an entry to the log.
|
||||
*
|
||||
* @param string $action The name of the action
|
||||
* @param string $type The type of action
|
||||
*/
|
||||
protected function log($action, $type) {
|
||||
$this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
|
||||
format_backtrace(debug_backtrace()) . '</li>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the log's contents as a HTML list.
|
||||
*
|
||||
* @return string HTML list of the log
|
||||
*/
|
||||
protected function output_log() {
|
||||
return '<ul>' . implode("\n", $this->log) . '</ul>';
|
||||
}
|
||||
}
|
||||
|
||||
// Alias this class to the old name.
|
||||
// This file will be autoloaded by the legacyclasses autoload system.
|
||||
// In future all uses of this class will be corrected and the legacy references will be removed.
|
||||
class_alias(xhtml_container_stack::class, \xhtml_container_stack::class);
|
||||
@@ -30,6 +30,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
// The old class name is the key, the path to the file containing the class is the value.
|
||||
// The array must be called $legacyclasses.
|
||||
$legacyclasses = [
|
||||
// Exception API.
|
||||
\bootstrap_renderer::class => 'output/bootstrap_renderer.php',
|
||||
|
||||
// Core API.
|
||||
@@ -48,4 +49,103 @@ $legacyclasses = [
|
||||
\require_login_session_timeout_exception::class => 'exception/require_login_session_timeout_exception.php',
|
||||
\required_capability_exception::class => 'exception/required_capability_exception.php',
|
||||
\webservice_parameter_exception::class => 'exception/webservice_parameter_exception.php',
|
||||
|
||||
// Output API.
|
||||
\theme_config::class => 'output/theme_config.php',
|
||||
\xhtml_container_stack::class => 'output/xhtml_container_stack.php',
|
||||
|
||||
\renderable::class => 'output/renderable.php',
|
||||
\templatable::class => 'output/templatable.php',
|
||||
|
||||
// Output API: Renderer Factories.
|
||||
\renderer_factory::class => 'output/renderer_factory/renderer_factory_interface.php',
|
||||
\renderer_factory_base::class => 'output/renderer_factory/renderer_factory_base.php',
|
||||
\standard_renderer_factory::class => 'output/renderer_factory/standard_renderer_factory.php',
|
||||
\theme_overridden_renderer_factory::class => 'output/renderer_factory/theme_overridden_renderer_factory.php',
|
||||
|
||||
// Output API: Renderers.
|
||||
\renderer_base::class => 'output/renderer_base.php',
|
||||
\plugin_renderer_base::class => 'output/plugin_renderer_base.php',
|
||||
\core_renderer::class => 'output/core_renderer.php',
|
||||
\core_renderer_cli::class => 'output/core_renderer_cli.php',
|
||||
\core_renderer_ajax::class => 'output/core_renderer_ajax.php',
|
||||
\core_renderer_maintenance::class => 'output/core_renderer_maintenance.php',
|
||||
\page_requirements_manager::class => 'output/requirements/page_requirements_manager.php',
|
||||
\YUI_config::class => 'output/requirements/yui.php',
|
||||
\fragment_requirements_manager::class => 'output/requirements/fragment_requirements_manager.php',
|
||||
|
||||
// Output API: components.
|
||||
\file_picker::class => 'output/file_picker.php',
|
||||
\user_picture::class => 'output/user_picture.php',
|
||||
\help_icon::class => 'output/help_icon.php',
|
||||
\pix_icon_font::class => 'output/pix_icon_font.php',
|
||||
\pix_icon_fontawesome::class => 'output/pix_icon_fontawesome.php',
|
||||
\pix_icon::class => 'output/pix_icon.php',
|
||||
\image_icon::class => 'output/image_icon.php',
|
||||
\pix_emoticon::class => 'output/pix_emoticon.php',
|
||||
\single_button::class => 'output/single_button.php',
|
||||
\single_select::class => 'output/single_select.php',
|
||||
\url_select::class => 'output/url_select.php',
|
||||
\action_link::class => 'output/action_link.php',
|
||||
\html_writer::class => 'output/html_writer.php',
|
||||
\js_writer::class => 'output/js_writer.php',
|
||||
\paging_bar::class => 'output/paging_bar.php',
|
||||
\initials_bar::class => 'output/initials_bar.php',
|
||||
\custom_menu_item::class => 'output/custom_menu_item.php',
|
||||
\custom_menu::class => 'output/custom_menu.php',
|
||||
\tabobject::class => 'output/tabobject.php',
|
||||
\context_header::class => 'output/context_header.php',
|
||||
\tabtree::class => 'output/tabtree.php',
|
||||
\action_menu::class => 'output/action_menu.php',
|
||||
\action_menu_filler::class => 'output/action_menu/filler.php',
|
||||
\action_menu_link::class => 'output/action_menu/link.php',
|
||||
\action_menu_link_primary::class => 'output/action_menu/link_primary.php',
|
||||
\action_menu_link_secondary::class => 'output/action_menu/link_secondary.php',
|
||||
\core\output\local\action_menu\subpanel::class => 'output/action_menu/subpanel.php',
|
||||
\preferences_groups::class => 'output/preferences_groups.php',
|
||||
\preferences_group::class => 'output/preferences_group.php',
|
||||
\progress_bar::class => 'output/progress_bar.php',
|
||||
\component_action::class => 'output/actions/component_action.php',
|
||||
\confirm_action::class => 'output/actions/confirm_action.php',
|
||||
\popup_action::class => 'output/actions/popup_action.php',
|
||||
|
||||
// Block Subsystem.
|
||||
\block_contents::class => [
|
||||
'core_block',
|
||||
'output/block_contents.php',
|
||||
],
|
||||
\block_move_target::class => [
|
||||
'core_block',
|
||||
'output/block_move_target.php',
|
||||
],
|
||||
|
||||
// Table Subsystem.
|
||||
\html_table::class => [
|
||||
'core_table',
|
||||
'output/html_table.php',
|
||||
],
|
||||
\html_table_row::class => [
|
||||
'core_table',
|
||||
'output/html_table_row.php',
|
||||
],
|
||||
\html_table_cell::class => [
|
||||
'core_table',
|
||||
'output/html_table_cell.php',
|
||||
],
|
||||
\flexible_table::class => [
|
||||
'core_table',
|
||||
'flexible_table.php',
|
||||
],
|
||||
\table_sql::class => [
|
||||
'core_table',
|
||||
'sql_table.php',
|
||||
],
|
||||
\table_default_export_format_parent::class => [
|
||||
'core_table',
|
||||
'base_export_format.php',
|
||||
],
|
||||
\table_dataformat_export_format::class => [
|
||||
'core_table',
|
||||
'dataformat_export_format.php',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
*/
|
||||
|
||||
require_once("HTML/QuickForm/button.php");
|
||||
require_once(__DIR__ . '/../outputcomponents.php');
|
||||
require_once('templatable_form_element.php');
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,18 +21,6 @@
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
// Some form elements are used before $CFG is created - do not rely on it here.
|
||||
require_once(__DIR__ . '/../outputcomponents.php');
|
||||
|
||||
/**
|
||||
* templatable_form_element trait.
|
||||
*
|
||||
* @package core_form
|
||||
* @copyright 2016 Damyon Wiese
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
trait templatable_form_element {
|
||||
|
||||
/**
|
||||
|
||||
+5
-202
@@ -14,207 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Classes representing JS event handlers, used by output components.
|
||||
*
|
||||
* Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
|
||||
* for an overview.
|
||||
*
|
||||
* @package core
|
||||
* @category output
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Helper class used by other components that involve an action on the page (URL or JS).
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class component_action implements templatable {
|
||||
|
||||
/**
|
||||
* @var string $event The DOM event that will trigger this action when caught
|
||||
*/
|
||||
public $event;
|
||||
|
||||
/**
|
||||
* @var string A function name to call when the button is clicked
|
||||
* The JS function you create must have two arguments:
|
||||
* 1. The event object
|
||||
* 2. An object/array of arguments ($jsfunctionargs)
|
||||
*/
|
||||
public $jsfunction = false;
|
||||
|
||||
/**
|
||||
* @var array An array of arguments to pass to the JS function
|
||||
*/
|
||||
public $jsfunctionargs = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param string $event DOM event
|
||||
* @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
|
||||
* @param array $jsfunctionargs An array of arguments to pass to the jsfunction
|
||||
*/
|
||||
public function __construct($event, $jsfunction, $jsfunctionargs=array()) {
|
||||
$this->event = $event;
|
||||
|
||||
$this->jsfunction = $jsfunction;
|
||||
$this->jsfunctionargs = $jsfunctionargs;
|
||||
|
||||
if (!empty($this->jsfunctionargs)) {
|
||||
if (empty($this->jsfunction)) {
|
||||
throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export for template.
|
||||
*
|
||||
* @param renderer_base $output The renderer.
|
||||
* @return stdClass
|
||||
*/
|
||||
public function export_for_template(renderer_base $output) {
|
||||
$args = !empty($this->jsfunctionargs) ? json_encode($this->jsfunctionargs) : false;
|
||||
return (object) [
|
||||
'event' => $this->event,
|
||||
'jsfunction' => $this->jsfunction,
|
||||
'jsfunctionargs' => $args,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Confirm action
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class confirm_action extends component_action {
|
||||
/**
|
||||
* Constructs the confirm action object
|
||||
*
|
||||
* @param string $message The message to display to the user when they are shown
|
||||
* the confirm dialogue.
|
||||
* @param string $callback Deprecated since 2.7
|
||||
* @param string $continuelabel The string to use for he continue button
|
||||
* @param string $cancellabel The string to use for the cancel button
|
||||
*/
|
||||
public function __construct($message, $callback = null, $continuelabel = null, $cancellabel = null) {
|
||||
if ($callback !== null) {
|
||||
debugging('The callback argument to new confirm_action() has been deprecated.' .
|
||||
' If you need to use a callback, please write Javascript to use moodle-core-notification-confirmation ' .
|
||||
'and attach to the provided events.',
|
||||
DEBUG_DEVELOPER);
|
||||
}
|
||||
parent::__construct('click', 'M.util.show_confirm_dialog', array(
|
||||
'message' => $message,
|
||||
'continuelabel' => $continuelabel, 'cancellabel' => $cancellabel));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Component action for a popup window.
|
||||
*
|
||||
* @copyright 2009 Nicolas Connault
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class popup_action extends component_action {
|
||||
|
||||
/**
|
||||
* @var string The JS function to call for the popup
|
||||
*/
|
||||
public $jsfunction = 'openpopup';
|
||||
|
||||
/**
|
||||
* @var array An array of parameters that will be passed to the openpopup JS function
|
||||
*/
|
||||
public $params = array(
|
||||
'height' => 400,
|
||||
'width' => 500,
|
||||
'top' => 0,
|
||||
'left' => 0,
|
||||
'menubar' => false,
|
||||
'location' => false,
|
||||
'scrollbars' => true,
|
||||
'resizable' => true,
|
||||
'toolbar' => true,
|
||||
'status' => true,
|
||||
'directories' => false,
|
||||
'fullscreen' => false,
|
||||
'dependent' => true);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $event DOM event
|
||||
* @param moodle_url|string $url A moodle_url object, required if no jsfunction is given
|
||||
* @param string $name The JS function to call for the popup (default 'popup')
|
||||
* @param array $params An array of popup parameters
|
||||
*/
|
||||
public function __construct($event, $url, $name='popup', $params=array()) {
|
||||
global $CFG;
|
||||
|
||||
$url = new moodle_url($url);
|
||||
|
||||
if ($name) {
|
||||
$_name = $name;
|
||||
if (($_name = preg_replace("/\s/", '_', $_name)) != $name) {
|
||||
throw new coding_exception('The $name of a popup window shouldn\'t contain spaces - string modified. '. $name .' changed to '. $_name);
|
||||
$name = $_name;
|
||||
}
|
||||
} else {
|
||||
$name = 'popup';
|
||||
}
|
||||
|
||||
foreach ($this->params as $var => $val) {
|
||||
if (array_key_exists($var, $params)) {
|
||||
$this->params[$var] = $params[$var];
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = array('url' => $url->out(false), 'name' => $name, 'options' => $this->get_js_options($params));
|
||||
if (!empty($params['fullscreen'])) {
|
||||
$attributes['fullscreen'] = 1;
|
||||
}
|
||||
parent::__construct($event, $this->jsfunction, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string of concatenated option->value pairs used by JS to call the popup window,
|
||||
* based on this object's variables
|
||||
*
|
||||
* @return string String of option->value pairs for JS popup function.
|
||||
*/
|
||||
public function get_js_options() {
|
||||
$jsoptions = '';
|
||||
|
||||
foreach ($this->params as $var => $val) {
|
||||
if (is_string($val) || is_int($val)) {
|
||||
$jsoptions .= "$var=$val,";
|
||||
} elseif (is_bool($val)) {
|
||||
$jsoptions .= ($val) ? "$var," : "$var=0,";
|
||||
}
|
||||
}
|
||||
|
||||
$jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);
|
||||
|
||||
return $jsoptions;
|
||||
}
|
||||
}
|
||||
// This file is deprecated, but it should never have been manually included by anything outside of lib/outputlib.php.
|
||||
// Throwing an exception here should be fine because removing the manual inclusion should have no impact.
|
||||
throw new \core\exception\coding_exception(
|
||||
'This file should not be manually included by any component.',
|
||||
);
|
||||
|
||||
+5
-5348
File diff suppressed because it is too large
Load Diff
+5
-431
@@ -14,436 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Interface and classes for creating appropriate renderers for various parts of Moodle.
|
||||
*
|
||||
* Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
|
||||
* for an overview.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/** General rendering target, usually normal browser page */
|
||||
define('RENDERER_TARGET_GENERAL', 'general');
|
||||
|
||||
/** General rendering target, usually normal browser page, but with limited capacity to avoid API use */
|
||||
define('RENDERER_TARGET_MAINTENANCE', 'maintenance');
|
||||
|
||||
/** Plain text rendering for CLI scripts and cron */
|
||||
define('RENDERER_TARGET_CLI', 'cli');
|
||||
|
||||
/** Plain text rendering for Ajax scripts*/
|
||||
define('RENDERER_TARGET_AJAX', 'ajax');
|
||||
|
||||
/** Plain text rendering intended for sending via email */
|
||||
define('RENDERER_TARGET_TEXTEMAIL', 'textemail');
|
||||
|
||||
/** Rich text html rendering intended for sending via email */
|
||||
define('RENDERER_TARGET_HTMLEMAIL', 'htmlemail');
|
||||
|
||||
// note: maybe we could define portfolio export target too
|
||||
|
||||
|
||||
/**
|
||||
* A renderer factory is just responsible for creating an appropriate renderer
|
||||
* for any given part of Moodle.
|
||||
*
|
||||
* Which renderer factory to use is chose by the current theme, and an instance
|
||||
* if created automatically when the theme is set up.
|
||||
*
|
||||
* A renderer factory must also have a constructor that takes a theme_config object.
|
||||
* (See {@link renderer_factory_base::__construct} for an example.)
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
interface renderer_factory {
|
||||
|
||||
/**
|
||||
* Return the renderer for a particular part of Moodle.
|
||||
*
|
||||
* The renderer interfaces are defined by classes called {plugin}_renderer
|
||||
* where {plugin} is the name of the component. The renderers for core Moodle are
|
||||
* defined in lib/renderer.php. For plugins, they will be defined in a file
|
||||
* called renderer.php inside the plugin.
|
||||
*
|
||||
* Renderers will normally want to subclass the renderer_base class.
|
||||
* (However, if you really know what you are doing, you don't have to do that.)
|
||||
*
|
||||
* There is no separate interface definition for renderers. The default
|
||||
* {plugin}_renderer implementation also serves to define the API for
|
||||
* other implementations of the interface, whether or not they subclass it.
|
||||
*
|
||||
* A particular plugin can define multiple renderers if it wishes, using the
|
||||
* $subtype parameter. For example workshop_renderer,
|
||||
* workshop_allocation_manual_renderer etc.
|
||||
*
|
||||
* @param moodle_page $page the page the renderer is outputting content for.
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
|
||||
* @param string $target one of rendering target constants
|
||||
* @return renderer_base an object implementing the requested renderer interface.
|
||||
*/
|
||||
public function get_renderer(moodle_page $page, $component, $subtype=null, $target=null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This is a base class to help you implement the renderer_factory interface.
|
||||
*
|
||||
* It keeps a cache of renderers that have been constructed, so you only need
|
||||
* to construct each one once in you subclass.
|
||||
*
|
||||
* It also has a method to get the name of, and include the renderer.php with
|
||||
* the definition of, the standard renderer class for a given module.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
abstract class renderer_factory_base implements renderer_factory {
|
||||
/**
|
||||
* @var theme_config The theme we belong to.
|
||||
*/
|
||||
protected $theme;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param theme_config $theme the theme we belong to.
|
||||
*/
|
||||
public function __construct(theme_config $theme) {
|
||||
$this->theme = $theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns suffix of renderer class expected for given target.
|
||||
*
|
||||
* @param string $target one of the renderer target constants, target is guessed if null used
|
||||
* @return array two element array, first element is target, second the target suffix string
|
||||
*/
|
||||
protected function get_target_suffix($target) {
|
||||
if (empty($target) || $target === RENDERER_TARGET_MAINTENANCE) {
|
||||
// If the target hasn't been specified we need to guess the defaults.
|
||||
// We also override the target with the default if the maintenance target has been provided.
|
||||
// This ensures we don't use the maintenance renderer if we are processing a special target.
|
||||
if (defined('PREFERRED_RENDERER_TARGET')) {
|
||||
$target = PREFERRED_RENDERER_TARGET;
|
||||
} else if (CLI_SCRIPT) {
|
||||
$target = RENDERER_TARGET_CLI;
|
||||
} else if (AJAX_SCRIPT) {
|
||||
$target = RENDERER_TARGET_AJAX;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($target) {
|
||||
case RENDERER_TARGET_CLI: $suffix = '_cli'; break;
|
||||
case RENDERER_TARGET_AJAX: $suffix = '_ajax'; break;
|
||||
case RENDERER_TARGET_TEXTEMAIL: $suffix = '_textemail'; break;
|
||||
case RENDERER_TARGET_HTMLEMAIL: $suffix = '_htmlemail'; break;
|
||||
case RENDERER_TARGET_MAINTENANCE: $suffix = '_maintenance'; break;
|
||||
default: $target = RENDERER_TARGET_GENERAL; $suffix = '';
|
||||
}
|
||||
|
||||
return array($target, $suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* For a given module name, return the possible class names
|
||||
* that defines the renderer interface for that module.
|
||||
*
|
||||
* Newer auto-loaded class names are returned as well as the old style _renderable classnames.
|
||||
*
|
||||
* Also, if it exists, include the renderer.php file for that module, so
|
||||
* the class definition of the default renderer has been loaded.
|
||||
*
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to:
|
||||
* '\mod_forum\output\news_renderer'
|
||||
* or '\mod_forum\output\news\renderer'
|
||||
* or non-autoloaded 'mod_forum_news'
|
||||
* @return array[] Each element of the array is an array with keys:
|
||||
* classname - The class name to search
|
||||
* autoloaded - Does this classname assume autoloading?
|
||||
* validwithprefix - Is this class name valid when a prefix is added to it?
|
||||
* validwithoutprefix - Is this class name valid when no prefix is added to it?
|
||||
* @throws coding_exception
|
||||
*/
|
||||
protected function standard_renderer_classnames($component, $subtype = null) {
|
||||
global $CFG; // Needed in included files.
|
||||
$classnames = array();
|
||||
|
||||
// Standardize component name ala frankenstyle.
|
||||
list($plugin, $type) = core_component::normalize_component($component);
|
||||
if ($type === null) {
|
||||
$component = $plugin;
|
||||
} else {
|
||||
$component = $plugin.'_'.$type;
|
||||
}
|
||||
|
||||
if ($component !== 'core') {
|
||||
// Renderers are stored in renderer.php files.
|
||||
if (!$compdirectory = core_component::get_component_directory($component)) {
|
||||
throw new coding_exception('Invalid component specified in renderer request', $component);
|
||||
}
|
||||
$rendererfile = $compdirectory . '/renderer.php';
|
||||
if (file_exists($rendererfile)) {
|
||||
include_once($rendererfile);
|
||||
}
|
||||
|
||||
} else if (!empty($subtype)) {
|
||||
$coresubsystems = core_component::get_core_subsystems();
|
||||
if (!array_key_exists($subtype, $coresubsystems)) { // There may be nulls.
|
||||
throw new coding_exception('Invalid core subtype "' . $subtype . '" in renderer request', $subtype);
|
||||
}
|
||||
if ($coresubsystems[$subtype]) {
|
||||
$rendererfile = $coresubsystems[$subtype] . '/renderer.php';
|
||||
if (file_exists($rendererfile)) {
|
||||
include_once($rendererfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($subtype)) {
|
||||
// Theme specific auto-loaded name (only valid when prefixed with the theme name).
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => false,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\output\\' . $component . '_renderer'
|
||||
);
|
||||
|
||||
// Standard autoloaded plugin name (not valid with a prefix).
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => false,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\' . $component . '\\output\\renderer'
|
||||
);
|
||||
// Legacy class name - (valid with or without a prefix).
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => false,
|
||||
'classname' => $component . '_renderer'
|
||||
);
|
||||
} else {
|
||||
// Theme specific auto-loaded name (only valid when prefixed with the theme name).
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => false,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\output\\' . $component . '\\' . $subtype . '_renderer'
|
||||
);
|
||||
// Version of the above with subtype being a namespace level on it's own.
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => false,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\output\\' . $component . '\\' . $subtype . '\\renderer'
|
||||
);
|
||||
// Standard autoloaded plugin name (not valid with a prefix).
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => false,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\' . $component . '\\output\\' . $subtype . '_renderer'
|
||||
);
|
||||
// Version of the above with subtype being a namespace level on it's own.
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => false,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => true,
|
||||
'classname' => '\\' . $component . '\\output\\' . $subtype . '\\renderer'
|
||||
);
|
||||
// Legacy class name - (valid with or without a prefix).
|
||||
$classnames[] = array(
|
||||
'validwithprefix' => true,
|
||||
'validwithoutprefix' => true,
|
||||
'autoloaded' => false,
|
||||
'classname' => $component . '_' . $subtype . '_renderer'
|
||||
);
|
||||
}
|
||||
return $classnames;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the default renderer factory for Moodle.
|
||||
*
|
||||
* It simply returns an instance of the appropriate standard renderer class.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class standard_renderer_factory extends renderer_factory_base {
|
||||
|
||||
/**
|
||||
* Implement the subclass method
|
||||
*
|
||||
* @param moodle_page $page the page the renderer is outputting content for.
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
|
||||
* @param string $target one of rendering target constants
|
||||
* @return renderer_base an object implementing the requested renderer interface.
|
||||
*/
|
||||
public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
|
||||
$classnames = $this->standard_renderer_classnames($component, $subtype);
|
||||
$classname = '';
|
||||
|
||||
list($target, $suffix) = $this->get_target_suffix($target);
|
||||
// First look for a version with a suffix.
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'] . $suffix;
|
||||
if (class_exists($newclassname)) {
|
||||
$classname = $newclassname;
|
||||
break;
|
||||
} else {
|
||||
$newclassname = $classnamedetails['classname'];
|
||||
if (class_exists($newclassname)) {
|
||||
$classname = $newclassname;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now look for a non-suffixed version.
|
||||
if (empty($classname)) {
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'];
|
||||
if (class_exists($newclassname)) {
|
||||
$classname = $newclassname;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($classname)) {
|
||||
// Standard renderer must always exist.
|
||||
throw new coding_exception('Request for an unknown renderer class. Searched for: ' . var_export($classnames, true));
|
||||
}
|
||||
|
||||
return new $classname($page, $target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This is renderer factory allows themes to override the standard renderers using php code.
|
||||
*
|
||||
* It will load any code from theme/mytheme/renderers.php and
|
||||
* theme/parenttheme/renderers.php, if then exist. Then whenever you ask for
|
||||
* a renderer for 'component', it will create a mytheme_component_renderer or a
|
||||
* parenttheme_component_renderer, instead of a component_renderer,
|
||||
* if either of those classes exist.
|
||||
*
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 2.0
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class theme_overridden_renderer_factory extends renderer_factory_base {
|
||||
|
||||
/**
|
||||
* @var array An array of renderer prefixes
|
||||
*/
|
||||
protected $prefixes = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param theme_config $theme the theme we are rendering for.
|
||||
*/
|
||||
public function __construct(theme_config $theme) {
|
||||
parent::__construct($theme);
|
||||
// Initialise $this->prefixes.
|
||||
$this->prefixes = $theme->renderer_prefixes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement the subclass method
|
||||
*
|
||||
* @param moodle_page $page the page the renderer is outputting content for.
|
||||
* @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
|
||||
* @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
|
||||
* @param string $target one of rendering target constants
|
||||
* @return renderer_base an object implementing the requested renderer interface.
|
||||
*/
|
||||
public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
|
||||
$classnames = $this->standard_renderer_classnames($component, $subtype);
|
||||
|
||||
list($target, $suffix) = $this->get_target_suffix($target);
|
||||
|
||||
// Theme lib.php and renderers.php files are loaded automatically
|
||||
// when loading the theme configs.
|
||||
|
||||
// First try the renderers with correct suffix.
|
||||
foreach ($this->prefixes as $prefix) {
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithprefix']) {
|
||||
if ($classnamedetails['autoloaded']) {
|
||||
$newclassname = $prefix . $classnamedetails['classname'] . $suffix;
|
||||
} else {
|
||||
$newclassname = $prefix . '_' . $classnamedetails['classname'] . $suffix;
|
||||
}
|
||||
if (class_exists($newclassname)) {
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'] . $suffix;
|
||||
if (class_exists($newclassname)) {
|
||||
// Use the specialised renderer for given target, default renderer might also decide
|
||||
// to implement support for more targets.
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then try general renderer.
|
||||
foreach ($this->prefixes as $prefix) {
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithprefix']) {
|
||||
if ($classnamedetails['autoloaded']) {
|
||||
$newclassname = $prefix . $classnamedetails['classname'];
|
||||
} else {
|
||||
$newclassname = $prefix . '_' . $classnamedetails['classname'];
|
||||
}
|
||||
if (class_exists($newclassname)) {
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final attempt - no prefix or suffix.
|
||||
foreach ($classnames as $classnamedetails) {
|
||||
if ($classnamedetails['validwithoutprefix']) {
|
||||
$newclassname = $classnamedetails['classname'];
|
||||
if (class_exists($newclassname)) {
|
||||
return new $newclassname($page, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new coding_exception('Request for an unknown renderer ' . $component . ', ' . $subtype . ', ' . $target);
|
||||
}
|
||||
}
|
||||
// This file is deprecated, but it should never have been manually included by anything outside of lib/outputlib.php.
|
||||
// Throwing an exception here should be fine because removing the manual inclusion should have no impact.
|
||||
throw new \core\exception\coding_exception(
|
||||
'This file should not be manually included by any component.',
|
||||
);
|
||||
|
||||
@@ -14,116 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Library functions to facilitate the use of JavaScript in Moodle.
|
||||
*
|
||||
* @copyright 2016 Adrian Greeve <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* This requirements manager captures the appropriate html for creating a fragment to
|
||||
* be inserted elsewhere.
|
||||
*
|
||||
* @copyright 2016 Adrian Greeve <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.1
|
||||
* @package core
|
||||
* @category output
|
||||
*/
|
||||
class fragment_requirements_manager extends page_requirements_manager {
|
||||
|
||||
/**
|
||||
* Page fragment constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
// As this is a fragment the header should already be done.
|
||||
$this->headdone = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns js code to load amd module loader, then insert inline script tags
|
||||
* that contain require() calls using RequireJS.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_amd_footercode() {
|
||||
global $CFG;
|
||||
$output = '';
|
||||
|
||||
// First include must be to a module with no dependencies, this prevents multiple requests.
|
||||
$prefix = 'M.util.js_pending("core/first");';
|
||||
$prefix .= "require(['core/first'], function() {\n";
|
||||
$suffix = "\n});";
|
||||
$suffix .= 'M.util.js_complete("core/first");';
|
||||
$output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate any HTML that needs to go at the end of the page.
|
||||
*
|
||||
* @return string the HTML code to to at the end of the page.
|
||||
*/
|
||||
public function get_end_code() {
|
||||
global $CFG;
|
||||
|
||||
$output = '';
|
||||
|
||||
// Call amd init functions.
|
||||
$output .= $this->get_amd_footercode();
|
||||
|
||||
// Add other requested modules.
|
||||
$output .= $this->get_extra_modules_code();
|
||||
|
||||
// All the other linked scripts - there should be as few as possible.
|
||||
if ($this->jsincludes['footer']) {
|
||||
foreach ($this->jsincludes['footer'] as $url) {
|
||||
$output .= html_writer::script('', $url);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->stringsforjs)) {
|
||||
// Add all needed strings.
|
||||
$strings = array();
|
||||
foreach ($this->stringsforjs as $component => $v) {
|
||||
foreach ($v as $indentifier => $langstring) {
|
||||
$strings[$component][$indentifier] = $langstring->out();
|
||||
}
|
||||
}
|
||||
// Append don't overwrite.
|
||||
$output .= html_writer::script('require(["jquery"], function($) {
|
||||
M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
|
||||
});');
|
||||
}
|
||||
|
||||
// Add variables.
|
||||
if ($this->jsinitvariables['footer']) {
|
||||
$js = '';
|
||||
foreach ($this->jsinitvariables['footer'] as $data) {
|
||||
list($var, $value) = $data;
|
||||
$js .= js_writer::set_variable($var, $value, true);
|
||||
}
|
||||
$output .= html_writer::script($js);
|
||||
}
|
||||
|
||||
$inyuijs = $this->get_javascript_code(false);
|
||||
$ondomreadyjs = $this->get_javascript_code(true);
|
||||
// See if this is still needed when we get to the ajax page.
|
||||
$jsinit = $this->get_javascript_init_code();
|
||||
$handlersjs = $this->get_event_handler_code();
|
||||
|
||||
// There is a global Y, make sure it is available in your scope.
|
||||
$js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
|
||||
|
||||
$output .= html_writer::script($js);
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
// This file is deprecated, but it should never have been manually included by anything outside of lib/outputlib.php.
|
||||
// Throwing an exception here should be fine because removing the manual inclusion should have no impact.
|
||||
throw new \core\exception\coding_exception(
|
||||
'This file should not be manually included by any component.',
|
||||
);
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user