/lib/outputcomponents.php
PHP | 5038 lines | 2308 code | 563 blank | 2167 comment | 393 complexity | f477a455f86c0ed8bbb300edae5fcfda MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?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/>.
- /**
- * Classes representing HTML elements, used by $OUTPUT methods
- *
- * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
- * for an overview.
- *
- * @package core
- * @category output
- * @copyright 2009 Tim Hunt
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- defined('MOODLE_INTERNAL') || die();
- /**
- * Interface marking other classes as suitable for renderer_base::render()
- *
- * @copyright 2010 Petr Skoda (skodak) info@skodak.org
- * @package core
- * @category output
- */
- interface renderable {
- // intentionally empty
- }
- /**
- * Interface marking other classes having the ability to export their data for use by templates.
- *
- * @copyright 2015 Damyon Wiese
- * @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);
- }
- /**
- * 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 = array(
- '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());
- }
- }
- // initilise 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;
- }
- }
- }
- }
- /**
- * 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.
- */
- 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', array('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 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 {@link self::fields()} with aliases,
- * this method extracts user related unaliased fields.
- *
- * @param stdClass $record containing user picture fields
- * @param 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' or 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 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 or $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) and !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) or strpos($this->user->email, '@') === false) {
- // 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 and !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}", array('s' => $size, 'd' => $gravatardefault));
- } else {
- return new moodle_url("http://www.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault));
- }
- }
- return $defaulturl;
- }
- }
- /**
- * Data structure representing a help icon.
- *
- * @copyright 2010 Petr Skoda (info@skodak.org)
- * @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;
- /**
- * 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
- */
- public function __construct($identifier, $component) {
- $this->identifier = $identifier;
- $this->component = $component;
- }
- /**
- * 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 array
- */
- public function export_for_template(renderer_base $output) {
- global $CFG;
- $title = get_string($this->identifier, $this->component);
- 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);
- $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;
- }
- }
- /**
- * 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 = \core\output\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 = array(
- 'extraclasses' => $pixdata['extraclasses'],
- 'title' => $title,
- 'alt' => $alt,
- 'key' => $this->key
- );
- return $data;
- }
- }
- /**
- * 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 {
- }
- /**
- * 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
- */
- var $pix;
- /**
- * @var string The component the icon belongs to.
- */
- var $component;
- /**
- * @var array An array of attributes to use on the icon
- */
- var $attributes = array();
- /**
- * Constructor
- *
- * @param string $pix short icon name
- * @param string $alt The alt text to use for the icon
- * @param string $component component name
- * @param 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 = array();
- foreach ($attributes as $name => $value) {
- $templatecontext[] = array('name' => $name, 'value' => $value);
- }
- $title = isset($attributes['title']) ? $attributes['title'] : '';
- if (empty($title)) {
- $title = isset($attributes['alt']) ? $attributes['alt'] : '';
- }
- $data = array(
- '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,
- ];
- }
- }
- /**
- * 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 {
- }
- /**
- * 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 = array()) {
- if (empty($attributes['class'])) {
- $attributes['class'] = 'emoticon';
- }
- parent::__construct($pix, $alt, $component, $attributes);
- }
- }
- /**
- * 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 {
- /**
- * @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 bool True if button is primary button. Used for styling.
- */
- public $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 = array();
- /**
- * @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 bool $primary whether this is a primary button, used for styling
- * @param array $attributes Attributes for the HTML button tag
- */
- public function __construct(moodle_url $url, $label, $method='post', $primary=false, $attributes = []) {
- $this->url = clone($url);
- $this->label = $label;
- $this->method = $method;
- $this->primary = $primary;
- $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;
- }
- /**
- * 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->primary = $this->primary;
- $data->attributes = [];
- foreach ($this->attributes as $key => $value) {
- $data->attributes[] = ['name' => $key, 'value' => $value];
- }
- // Form parameters.
- $params = $this->url->params();
- if ($this->method === 'post') {
- $params['sesskey'] = sesskey();
- }
- $data->params = array_map(function($key) use ($params) {
- return ['name' => $key, 'value' => $params[$key]];
- }, array_keys($params));
- // 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;
- }
- }
- /**
- * 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
- */
- var $url;
- /**
- * @var string Name of the select element.
- */
- var $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')))
- */
- var $options;
- /**
- * @var string Selected option
- */
- var $selected;
- /**
- * @var array Nothing selected
- */
- var $nothing;
- /**
- * @var array Extra select field attributes
- */
- var $attributes = array();
- /**
- * @var string Button label
- */
- var $label = '';
- /**
- * @var array Button label's attributes
- */
- var $labelattributes = array();
- /**
- * @var string Form submit method post or get
- */
- var $method = 'get';
- /**
- * @var string Wrapping div class
- */
- var $class = 'singleselect';
- /**
- * @var bool True if button disabled, false if normal
- */
- var $disabled = false;
- /**
- * @var string Button tooltip
- */
- var $tooltip = null;
- /**
- * @var string Form id
- */
- var $formid = null;
- /**
- * @var help_icon The help icon for this element.
- */
- var $helpicon = null;
- /**
- * 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 = array('' => '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', array('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 = array()) {
- $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.
- $params = $this->url->params();
- if ($this->method === 'post') {
- $params['sesskey'] = sesskey();
- }
- $data->params = array_map(function($key) use ($params) {
- return ['name' => $key, 'value' => $params[$key]];
- }, array_keys($params));
- // 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;
- }
- }
- /**
- * 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')))
- */
- var $urls;
- /**
- * @var string Selected option
- */
- var $selected;
- /**
- * @var array Nothing selected
- */
- var $nothing;
- /**
- * @var array Extra select field attributes
- */
- var $attributes = array();
- /**
- * @var string Button label
- */
- var $label = '';
- /**
- * @var array Button label's attributes
- */
- var $labelattributes = array();
- /**
- * @var string Wrapping div class
- */
- var $class = 'urlselect';
- /**
- * @var bool True if button disabled, false if normal
- */
- var $disabled = false;
- /**
- * @var string Button tooltip
- */
- var $tooltip = null;
- /**
- * @var string Form id
- */
- var $formid = null;
- /**
- * @var help_icon The help icon for this element.
- */
- var $helpicon = null;
- /**
- * @var string If set, makes button visible with given name for button
- */
- var $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 = array('' => '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 = array()) {
- $this->label = $label;
- $this->labelattributes = $attributes;
- }
- /**
- * Clean a URL.
- *
- * @param string $value The URL.
- * @return The cleaned URL.
- */
- protected function clean_url($value) {
- global $CFG;
- 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;
- }
- }
- /**
- * 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 component_action $action
- * @param array $attributes associative array of html link attributes + disabled
- * @param 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;
- $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 rendereā¦
Large files files are truncated, but you can click here to view the full file