2009-08-10 06:22:04 +00:00
|
|
|
<?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 moodlecore
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2010-01-13 17:13:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface marking other classes as suitable for renderer_base::render()
|
|
|
|
* @author 2010 Petr Skoda (skodak) info@skodak.org
|
|
|
|
*/
|
|
|
|
interface renderable {
|
|
|
|
// intentionally empty
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-13 18:50:28 +00:00
|
|
|
* Data structure representing a user picture.
|
2010-01-13 17:13:52 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Nicolas Connault, 2010 Petr Skoda
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
|
|
|
class user_picture implements renderable {
|
|
|
|
/**
|
|
|
|
* List of mandatory fields in user record here.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const FIELDS = 'id,picture,firstname,lastname,imagealt';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var object $user A user object with at least fields id, picture, imagealt, firstname and lastname set.
|
|
|
|
*/
|
|
|
|
public $user;
|
|
|
|
/**
|
|
|
|
* @var int $courseid The course id. Used when constructing the link to the user's profile,
|
|
|
|
* page course id used if not specified.
|
|
|
|
*/
|
|
|
|
public $courseid;
|
|
|
|
/**
|
|
|
|
* @var bool $link add course profile link to image
|
|
|
|
*/
|
|
|
|
public $link = true;
|
|
|
|
/**
|
|
|
|
* @var int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatibility
|
|
|
|
*/
|
|
|
|
public $size = 35;
|
|
|
|
/**
|
|
|
|
* @var boolean $alttext 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 boolean $popup Whether or not to open the link in a popup window.
|
|
|
|
*/
|
|
|
|
public $popup = false;
|
|
|
|
/**
|
|
|
|
* @var string Image class attribute
|
|
|
|
*/
|
|
|
|
public $class = 'userpicture';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User picture constructor.
|
|
|
|
*
|
|
|
|
* @param object $user user record with at least id, picture, imagealt, firstname and lastname set.
|
|
|
|
* @param array $options such as link, size, link, ...
|
|
|
|
*/
|
|
|
|
public function __construct(stdClass $user) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
static $fields = null;
|
|
|
|
if (is_null($fields)) {
|
|
|
|
$fields = explode(',', self::FIELDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ($fields as $field) {
|
|
|
|
if (!array_key_exists($field, $user)) {
|
|
|
|
$needrec = true;
|
|
|
|
debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
|
|
|
|
.'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($needrec) {
|
|
|
|
$this->user = $DB->get_record('user', array('id'=>$user->id), self::FIELDS, MUST_EXIST);
|
|
|
|
} else {
|
|
|
|
$this->user = clone($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of required user fields, usefull when fetching required user info from db.
|
|
|
|
* @param string $tableprefix name of database table prefix in query
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function fields($tableprefix = '') {
|
|
|
|
if ($tableprefix === '') {
|
|
|
|
return self::FIELDS;
|
|
|
|
} else {
|
|
|
|
return "$tableprefix." . str_replace(',', ",$tableprefix.", self::FIELDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 18:50:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data structure representing a help icon.
|
|
|
|
*
|
|
|
|
* @copyright 2009 Nicolas Connault, 2010 Petr Skoda
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
|
|
|
class help_icon implements renderable {
|
|
|
|
/**
|
|
|
|
* @var string $page name of help page
|
|
|
|
*/
|
|
|
|
public $helppage;
|
|
|
|
/**
|
|
|
|
* @var string $title A descriptive text for title tooltip
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $title = null;
|
2010-01-13 18:50:28 +00:00
|
|
|
/**
|
|
|
|
* @var string $component Component name, the same as in get_string()
|
|
|
|
*/
|
|
|
|
public $component = 'moodle';
|
|
|
|
/**
|
|
|
|
* @var string $linktext Extra descriptive text next to the icon
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $linktext = null;
|
2010-01-13 18:50:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor: sets up the other components in case they are needed
|
|
|
|
* @param string $page The keyword that defines a help page
|
|
|
|
* @param string $title A descriptive text for accesibility only
|
|
|
|
* @param string $component
|
|
|
|
* @param bool $linktext add extra text to icon
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($helppage, $title, $component = 'moodle') {
|
|
|
|
if (empty($title)) {
|
|
|
|
throw new coding_exception('A help_icon object requires a $text parameter');
|
|
|
|
}
|
|
|
|
if (empty($helppage)) {
|
|
|
|
throw new coding_exception('A help_icon object requires a $helppage parameter');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->helppage = $helppage;
|
|
|
|
$this->title = $title;
|
|
|
|
$this->component = $component;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-11 14:59:00 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class pix_icon implements renderable {
|
|
|
|
var $pix;
|
|
|
|
var $component;
|
|
|
|
var $attributes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param string $pix short icon name
|
|
|
|
* @param string $component component name
|
|
|
|
* @param array $attributes html attributes
|
|
|
|
*/
|
|
|
|
public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
|
2010-02-11 15:44:06 +00:00
|
|
|
$this->pix = $pix;
|
|
|
|
$this->component = $component;
|
2010-02-11 14:59:00 +00:00
|
|
|
$this->attributes = (array)$attributes;
|
|
|
|
|
|
|
|
$this->attributes['alt'] = $alt;
|
|
|
|
if (empty($this->attributes['class'])) {
|
|
|
|
$this->attributes['class'] = 'smallicon';
|
|
|
|
}
|
|
|
|
if (!isset($this->attributes['title'])) {
|
|
|
|
$this->attributes['title'] = $this->attributes['alt'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-14 19:18:04 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class single_button implements renderable {
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Target url
|
|
|
|
* @var moodle_url
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
var $url;
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Button label
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
var $label;
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Form submit method
|
|
|
|
* @var string post or get
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
var $method = 'post';
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Wrapping div class
|
|
|
|
* @var string
|
|
|
|
* */
|
2010-01-14 19:18:04 +00:00
|
|
|
var $class = 'singlebutton';
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* True if button disabled, false if normal
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
var $disabled = false;
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Button tooltip
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
var $tooltip = null;
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Form id
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
var $formid;
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* List of attached actions
|
|
|
|
* @var array of component_action
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
var $actions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2010-01-14 22:40:33 +00:00
|
|
|
* @param string|moodle_url $url
|
2010-01-14 19:18:04 +00:00
|
|
|
* @param string $label button text
|
|
|
|
* @param string $method get or post submit method
|
|
|
|
*/
|
|
|
|
public function __construct(moodle_url $url, $label, $method='post') {
|
|
|
|
$this->url = clone($url);
|
|
|
|
$this->label = $label;
|
|
|
|
$this->method = $method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-14 22:40:33 +00:00
|
|
|
* Shortcut for adding a JS confirm dialog when the button is clicked.
|
2010-01-14 19:18:04 +00:00
|
|
|
* The message must be a yes/no question.
|
|
|
|
* @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_confirm_action($confirmmessage) {
|
2010-02-06 17:35:11 +00:00
|
|
|
$this->add_action(new component_action('click', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
|
2010-01-14 19:18:04 +00:00
|
|
|
}
|
|
|
|
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* Add action to the button.
|
|
|
|
* @param component_action $action
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-01-14 19:18:04 +00:00
|
|
|
public function add_action(component_action $action) {
|
|
|
|
$this->actions[] = $action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-09 17:39:13 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class single_select implements renderable {
|
|
|
|
/**
|
|
|
|
* Target url - includes hidden fields
|
|
|
|
* @var moodle_url
|
|
|
|
*/
|
|
|
|
var $url;
|
|
|
|
/**
|
|
|
|
* Name of the select element.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
/**
|
|
|
|
* Selected option
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $selected;
|
|
|
|
/**
|
|
|
|
* Nothing selected
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $nothing;
|
|
|
|
/**
|
|
|
|
* Extra select field attributes
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $attributes = array();
|
|
|
|
/**
|
|
|
|
* Button label
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $label = '';
|
|
|
|
/**
|
|
|
|
* Form submit method
|
|
|
|
* @var string post or get
|
|
|
|
*/
|
|
|
|
var $method = 'get';
|
|
|
|
/**
|
|
|
|
* Wrapping div class
|
|
|
|
* @var string
|
|
|
|
* */
|
|
|
|
var $class = 'singleselect';
|
|
|
|
/**
|
|
|
|
* True if button disabled, false if normal
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $disabled = false;
|
|
|
|
/**
|
|
|
|
* Button tooltip
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $tooltip = null;
|
|
|
|
/**
|
|
|
|
* Form id
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $formid = null;
|
|
|
|
/**
|
|
|
|
* List of attached actions
|
|
|
|
* @var array of component_action
|
|
|
|
*/
|
|
|
|
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
|
2010-02-10 09:37:50 +00:00
|
|
|
* @param string $formid
|
2010-02-09 17:39:13 +00:00
|
|
|
*/
|
2010-02-10 09:37:50 +00:00
|
|
|
public function __construct(moodle_url $url, $name, array $options, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
|
2010-02-09 17:39:13 +00:00
|
|
|
$this->url = $url;
|
|
|
|
$this->name = $name;
|
|
|
|
$this->options = $options;
|
|
|
|
$this->selected = $selected;
|
|
|
|
$this->nothing = $nothing;
|
2010-02-10 09:37:50 +00:00
|
|
|
$this->formid = $formid;
|
2010-02-09 17:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for adding a JS confirm dialog when the button is clicked.
|
|
|
|
* The message must be a yes/no question.
|
|
|
|
* @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_action(component_action $action) {
|
|
|
|
$this->actions[] = $action;
|
|
|
|
}
|
2010-02-10 09:37:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor: sets up the other components in case they are needed
|
|
|
|
* @param string $page The keyword that defines a help page
|
|
|
|
* @param string $title A descriptive text for accesibility only
|
|
|
|
* @param string $component
|
|
|
|
* @param bool $linktext add extra text to icon
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_help_icon($helppage, $title, $component = 'moodle') {
|
|
|
|
$this->helpicon = new help_icon($helppage, $title, $component);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set's select lable
|
|
|
|
* @param string $label
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_label($label) {
|
|
|
|
$this->label = $label;
|
|
|
|
}
|
2010-02-09 17:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-10 10:22:25 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class url_select implements renderable {
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
/**
|
|
|
|
* Selected option
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $selected;
|
|
|
|
/**
|
|
|
|
* Nothing selected
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $nothing;
|
|
|
|
/**
|
|
|
|
* Extra select field attributes
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $attributes = array();
|
|
|
|
/**
|
|
|
|
* Button label
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $label = '';
|
|
|
|
/**
|
|
|
|
* Wrapping div class
|
|
|
|
* @var string
|
|
|
|
* */
|
|
|
|
var $class = 'urlselect';
|
|
|
|
/**
|
|
|
|
* True if button disabled, false if normal
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $disabled = false;
|
|
|
|
/**
|
|
|
|
* Button tooltip
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $tooltip = null;
|
|
|
|
/**
|
|
|
|
* Form id
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $formid = null;
|
|
|
|
/**
|
|
|
|
* List of attached actions
|
|
|
|
* @var array of component_action
|
|
|
|
*/
|
|
|
|
var $helpicon = null;
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param array $urls list of options
|
|
|
|
* @param string $selected selected element
|
|
|
|
* @param array $nothing
|
|
|
|
* @param string $formid
|
|
|
|
*/
|
|
|
|
public function __construct(array $urls, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
|
|
|
|
$this->urls = $urls;
|
|
|
|
$this->selected = $selected;
|
|
|
|
$this->nothing = $nothing;
|
|
|
|
$this->formid = $formid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor: sets up the other components in case they are needed
|
|
|
|
* @param string $page The keyword that defines a help page
|
|
|
|
* @param string $title A descriptive text for accesibility only
|
|
|
|
* @param string $component
|
|
|
|
* @param bool $linktext add extra text to icon
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_help_icon($helppage, $title, $component = 'moodle') {
|
|
|
|
$this->helpicon = new help_icon($helppage, $title, $component);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set's select lable
|
|
|
|
* @param string $label
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_label($label) {
|
|
|
|
$this->label = $label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-14 22:40:33 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class action_link implements renderable {
|
|
|
|
/**
|
|
|
|
* Href url
|
|
|
|
* @var moodle_url
|
|
|
|
*/
|
|
|
|
var $url;
|
|
|
|
/**
|
|
|
|
* Link text
|
|
|
|
* @var string HTML fragment
|
|
|
|
*/
|
|
|
|
var $text;
|
|
|
|
/**
|
|
|
|
* HTML attributes
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $attributes;
|
|
|
|
/**
|
|
|
|
* List of actions attached to link
|
|
|
|
* @var array of component_action
|
|
|
|
*/
|
|
|
|
var $actions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param string|moodle_url $url
|
|
|
|
* @param string $text HTML fragment
|
|
|
|
* @param component_action $action
|
2010-01-14 22:51:10 +00:00
|
|
|
* @param array $attributes associative array of html link attributes + disabled
|
2010-01-14 22:40:33 +00:00
|
|
|
*/
|
|
|
|
public function __construct(moodle_url $url, $text, component_action $action=null, array $attributes=null) {
|
|
|
|
$this->url = clone($url);
|
|
|
|
$this->text = $text;
|
2010-02-24 06:11:45 +00:00
|
|
|
$this->attributes = (array)$attributes;
|
2010-01-15 09:01:59 +00:00
|
|
|
if ($action) {
|
2010-01-14 22:40:33 +00:00
|
|
|
$this->add_action($action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add action to the link.
|
|
|
|
* @param component_action $action
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_action(component_action $action) {
|
|
|
|
$this->actions[] = $action;
|
|
|
|
}
|
2010-02-16 16:24:49 +00:00
|
|
|
|
|
|
|
public function add_class($class) {
|
|
|
|
if (empty($this->atribbutes['class'])) {
|
|
|
|
$this->atribbutes['class'] = $class;
|
|
|
|
} else {
|
|
|
|
$this->atribbutes['class'] = $this->atribbutes['class'].' '.$class;
|
|
|
|
}
|
|
|
|
}
|
2010-01-14 22:40:33 +00:00
|
|
|
}
|
2010-01-14 19:18:04 +00:00
|
|
|
|
2010-01-13 17:13:52 +00:00
|
|
|
|
2010-01-20 20:01:24 +00:00
|
|
|
// ==== HTML writer and helper classes, will be probably moved elsewhere ======
|
2010-01-13 17:13:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple html output class
|
|
|
|
* @copyright 2009 Tim Hunt, 2010 Petr Skoda
|
|
|
|
*/
|
|
|
|
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
|
2010-02-18 18:15:56 +00:00
|
|
|
* @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
2010-01-13 17:13:52 +00:00
|
|
|
* @return string HTML fragment
|
|
|
|
*/
|
2010-02-18 18:15:56 +00:00
|
|
|
public static function tag($tagname, $contents, array $attributes = null) {
|
2010-01-13 17:13:52 +00:00
|
|
|
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 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 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 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 {@link s()}
|
|
|
|
* @return string HTML fragment
|
|
|
|
*/
|
|
|
|
public static function attribute($name, $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER);
|
|
|
|
}
|
2010-01-13 18:50:28 +00:00
|
|
|
if ($value instanceof moodle_url) {
|
|
|
|
return ' ' . $name . '="' . $value->out() . '"';
|
|
|
|
}
|
2010-02-03 16:27:52 +00:00
|
|
|
|
|
|
|
// special case, we do not want these in output
|
|
|
|
if ($value === null) {
|
|
|
|
return '';
|
2010-01-13 17:13:52 +00:00
|
|
|
}
|
2010-02-03 16:27:52 +00:00
|
|
|
|
|
|
|
// no sloppy trimming here!
|
|
|
|
return ' ' . $name . '="' . s($value) . '"';
|
2010-01-13 17:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs a list of HTML attributes and values
|
|
|
|
* @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
|
|
|
|
* The values will be escaped with {@link 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 random html element id.
|
|
|
|
* @param string $base
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function random_id($base='random') {
|
|
|
|
return uniqid($base);
|
|
|
|
}
|
2010-01-14 22:10:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a simple html link
|
|
|
|
* @param string|moodle_url $url
|
|
|
|
* @param string $text link txt
|
|
|
|
* @param array $attributes extra html attributes
|
|
|
|
* @return string HTML fragment
|
|
|
|
*/
|
|
|
|
public static function link($url, $text, array $attributes = null) {
|
|
|
|
$attributes = (array)$attributes;
|
|
|
|
$attributes['href'] = $url;
|
2010-02-18 18:15:56 +00:00
|
|
|
return self::tag('a', $text, $attributes);
|
2010-01-14 22:10:20 +00:00
|
|
|
}
|
2010-01-16 17:00:22 +00:00
|
|
|
|
2010-02-09 18:04:16 +00:00
|
|
|
/**
|
|
|
|
* generates a simple checkbox with optional label
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
* @param bool $checked
|
|
|
|
* @param string $label
|
|
|
|
* @param array $attributes
|
|
|
|
* @return string html fragment
|
|
|
|
*/
|
|
|
|
public static function checkbox($name, $value, $checked = true, $label = '', array $attributes = null) {
|
|
|
|
$attributes = (array)$attributes;
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
if ($label !== '' and !is_null($label)) {
|
|
|
|
if (empty($attributes['id'])) {
|
|
|
|
$attributes['id'] = self::random_id('checkbox_');
|
|
|
|
}
|
|
|
|
}
|
2010-02-09 18:16:14 +00:00
|
|
|
$attributes['type'] = 'checkbox';
|
|
|
|
$attributes['value'] = $value;
|
|
|
|
$attributes['name'] = $name;
|
2010-02-09 18:04:16 +00:00
|
|
|
$attributes['checked'] = $checked ? 'selected' : null;
|
2010-02-09 18:16:14 +00:00
|
|
|
|
2010-02-09 18:04:16 +00:00
|
|
|
$output .= self::empty_tag('input', $attributes);
|
|
|
|
|
|
|
|
if ($label !== '' and !is_null($label)) {
|
2010-02-18 18:15:56 +00:00
|
|
|
$output .= self::tag('label', $label, array('for'=>$attributes['id']));
|
2010-02-09 18:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2010-02-10 14:05:07 +00:00
|
|
|
/**
|
|
|
|
* Generates a simple select yes/no form field
|
|
|
|
* @param string $name name of select element
|
|
|
|
* @param bool $selected
|
|
|
|
* @param array $attributes - html select element attributes
|
|
|
|
* @return string HRML fragment
|
|
|
|
*/
|
2010-02-19 13:10:15 +00:00
|
|
|
public static function select_yes_no($name, $selected=true, array $attributes = null) {
|
2010-02-10 14:05:07 +00:00
|
|
|
$options = array('1'=>get_string('yes'), '0'=>get_string('no'));
|
|
|
|
return self::select($options, $name, $selected, null, $attributes);
|
|
|
|
}
|
|
|
|
|
2010-01-16 17:00:22 +00:00
|
|
|
/**
|
|
|
|
* Generates a simple select form field
|
2010-01-16 19:48:01 +00:00
|
|
|
* @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.:
|
2010-01-17 09:08:34 +00:00
|
|
|
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
|
2010-01-16 19:48:01 +00:00
|
|
|
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
|
2010-01-16 17:00:22 +00:00
|
|
|
* @param string $name name of select element
|
|
|
|
* @param string|array $selected value or arary of values depending on multiple attribute
|
|
|
|
* @param array|bool $nothing, add nothing selected option, or false of not added
|
|
|
|
* @param array $attributes - html select element attributes
|
2010-02-10 14:05:07 +00:00
|
|
|
* @return string HTML fragment
|
2010-01-16 17:00:22 +00:00
|
|
|
*/
|
2010-02-03 16:49:40 +00:00
|
|
|
public static function select(array $options, $name, $selected = '', $nothing = array(''=>'choosedots'), array $attributes = null) {
|
2010-01-16 17:00:22 +00:00
|
|
|
$attributes = (array)$attributes;
|
|
|
|
if (is_array($nothing)) {
|
|
|
|
foreach ($nothing as $k=>$v) {
|
2010-01-16 18:25:51 +00:00
|
|
|
if ($v === 'choose' or $v === 'choosedots') {
|
2010-01-16 17:00:22 +00:00
|
|
|
$nothing[$k] = get_string('choosedots');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$options = $nothing + $options; // keep keys, do not override
|
2010-01-16 17:39:50 +00:00
|
|
|
|
|
|
|
} else if (is_string($nothing) and $nothing !== '') {
|
|
|
|
// BC
|
|
|
|
$options = array(''=>$nothing) + $options;
|
2010-01-17 09:08:34 +00:00
|
|
|
}
|
2010-01-16 17:00:22 +00:00
|
|
|
|
|
|
|
// 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 contaion [], 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 contaion [], 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 ' . $attributes['class']; /// Add 'select' selector always
|
|
|
|
|
|
|
|
$attributes['name'] = $name;
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
foreach ($options as $value=>$label) {
|
2010-01-16 19:48:01 +00:00
|
|
|
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);
|
2010-01-16 17:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-18 18:15:56 +00:00
|
|
|
return self::tag('select', $output, $attributes);
|
2010-01-16 17:00:22 +00:00
|
|
|
}
|
2010-01-16 19:48:01 +00:00
|
|
|
|
|
|
|
private static function select_option($label, $value, array $selected) {
|
|
|
|
$attributes = array();
|
|
|
|
$value = (string)$value;
|
|
|
|
if (in_array($value, $selected, true)) {
|
|
|
|
$attributes['selected'] = 'selected';
|
|
|
|
}
|
|
|
|
$attributes['value'] = $value;
|
2010-02-18 18:15:56 +00:00
|
|
|
return self::tag('option', $label, $attributes);
|
2010-01-16 19:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function select_optgroup($groupname, $options, array $selected) {
|
|
|
|
if (empty($options)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$attributes = array('label'=>$groupname);
|
|
|
|
$output = '';
|
|
|
|
foreach ($options as $value=>$label) {
|
|
|
|
$output .= self::select_option($label, $value, $selected);
|
|
|
|
}
|
2010-02-18 18:15:56 +00:00
|
|
|
return self::tag('optgroup', $output, $attributes);
|
2010-01-16 19:48:01 +00:00
|
|
|
}
|
2010-01-17 09:06:55 +00:00
|
|
|
|
2010-02-10 14:27:03 +00:00
|
|
|
/**
|
|
|
|
* 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 array $attributes - html select element attributes
|
|
|
|
* @return HTML fragment
|
|
|
|
*/
|
|
|
|
public static function select_time($type, $name, $currenttime=0, $step=5, array $attributes=null) {
|
|
|
|
if (!$currenttime) {
|
|
|
|
$currenttime = time();
|
|
|
|
}
|
|
|
|
$currentdate = usergetdate($currenttime);
|
|
|
|
$userdatetype = $type;
|
|
|
|
$timeunits = array();
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'years':
|
|
|
|
for ($i=1970; $i<=2020; $i++) {
|
|
|
|
$timeunits[$i] = $i;
|
|
|
|
}
|
|
|
|
$userdatetype = 'year';
|
|
|
|
break;
|
|
|
|
case 'months':
|
|
|
|
for ($i=1; $i<=12; $i++) {
|
|
|
|
$timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
|
|
|
|
}
|
|
|
|
$userdatetype = 'month';
|
|
|
|
$currentdate['month'] = $currentdate['mon'];
|
|
|
|
break;
|
|
|
|
case 'days':
|
|
|
|
for ($i=1; $i<=31; $i++) {
|
|
|
|
$timeunits[$i] = $i;
|
|
|
|
}
|
|
|
|
$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().");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($attributes['id'])) {
|
|
|
|
$attributes['id'] = self::random_id('ts_');
|
|
|
|
}
|
|
|
|
$timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, array('id'=>$attributes['id']));
|
2010-02-18 18:15:56 +00:00
|
|
|
$label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide'));
|
2010-02-10 14:27:03 +00:00
|
|
|
|
|
|
|
return $label.$timerselector;
|
|
|
|
}
|
|
|
|
|
2010-02-17 17:56:26 +00:00
|
|
|
/**
|
|
|
|
* Shortcut for quick making of lists
|
|
|
|
* @param array $items
|
|
|
|
* @param string $tag ul or ol
|
|
|
|
* @param array $attributes
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function alist(array $items, array $attributes = null, $tag = 'ul') {
|
|
|
|
//note: 'list' is a reserved keyword ;-)
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$output .= html_writer::start_tag('li') . "\n";
|
|
|
|
$output .= $item . "\n";
|
|
|
|
$output .= html_writer::end_tag('li') . "\n";
|
|
|
|
}
|
|
|
|
|
2010-02-18 18:15:56 +00:00
|
|
|
return html_writer::tag($tag, $output, $attributes);
|
2010-02-17 17:56:26 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 09:06:55 +00:00
|
|
|
/**
|
|
|
|
* Returns hidden input fields created from url parameters.
|
|
|
|
* @param moodle_url $url
|
|
|
|
* @param 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 = '';
|
2010-01-17 09:08:34 +00:00
|
|
|
foreach ($params as $key => $value) {
|
2010-01-17 09:06:55 +00:00
|
|
|
$attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value);
|
|
|
|
$output .= self::empty_tag('input', $attributes)."\n";
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2010-01-18 20:17:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a script tag containing the the specified code.
|
|
|
|
*
|
|
|
|
* @param string $js the JavaScript code
|
2010-01-21 20:55:58 +00:00
|
|
|
* @param moodle_url|string optional url of the external script, $code ignored if specified
|
2010-01-18 20:17:43 +00:00
|
|
|
* @return string HTML, the code wrapped in <script> tags.
|
|
|
|
*/
|
2010-01-21 20:55:58 +00:00
|
|
|
public static function script($jscode, $url=null) {
|
2010-01-18 20:17:43 +00:00
|
|
|
if ($jscode) {
|
2010-01-21 20:55:58 +00:00
|
|
|
$attributes = array('type'=>'text/javascript');
|
2010-02-18 18:15:56 +00:00
|
|
|
return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n", $attributes) . "\n";
|
2010-01-21 20:55:58 +00:00
|
|
|
|
|
|
|
} else if ($url) {
|
|
|
|
$attributes = array('type'=>'text/javascript', 'src'=>$url);
|
2010-02-18 18:15:56 +00:00
|
|
|
return self::tag('script', '', $attributes) . "\n";
|
2010-02-09 17:39:13 +00:00
|
|
|
|
2010-01-18 20:17:43 +00:00
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 17:13:52 +00:00
|
|
|
}
|
|
|
|
|
2010-01-20 20:01:24 +00:00
|
|
|
// ==== JS writer and helper classes, will be probably moved elsewhere ======
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple javascript output class
|
|
|
|
* @copyright 2010 Petr Skoda
|
|
|
|
*/
|
|
|
|
class js_writer {
|
|
|
|
/**
|
|
|
|
* Returns javascript code calling the function
|
|
|
|
* @param string $function function name, can be complex lin Y.Event.purgeElement
|
|
|
|
* @param array $arguments parameters
|
|
|
|
* @param int $delay execution delay in seconds
|
|
|
|
* @return string JS code fragment
|
|
|
|
*/
|
|
|
|
public function function_call($function, array $arguments = null, $delay=0) {
|
2010-01-24 19:34:44 +00:00
|
|
|
if ($arguments) {
|
|
|
|
$arguments = array_map('json_encode', $arguments);
|
|
|
|
$arguments = implode(', ', $arguments);
|
|
|
|
} else {
|
|
|
|
$arguments = '';
|
|
|
|
}
|
2010-01-20 20:01:24 +00:00
|
|
|
$js = "$function($arguments);";
|
|
|
|
|
|
|
|
if ($delay) {
|
|
|
|
$delay = $delay * 1000; // in miliseconds
|
|
|
|
$js = "setTimeout(function() { $js }, $delay);";
|
|
|
|
}
|
|
|
|
return $js . "\n";
|
|
|
|
}
|
|
|
|
|
2010-01-23 17:08:18 +00:00
|
|
|
/**
|
|
|
|
* Special function which adds Y as first argument of fucntion call.
|
|
|
|
* @param string $function
|
|
|
|
* @param array $extraarguments
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function function_call_with_Y($function, array $extraarguments = null) {
|
|
|
|
if ($extraarguments) {
|
|
|
|
$extraarguments = array_map('json_encode', $extraarguments);
|
|
|
|
$arguments = 'Y, ' . implode(', ', $extraarguments);
|
|
|
|
} else {
|
|
|
|
$arguments = 'Y';
|
|
|
|
}
|
|
|
|
return "$function($arguments);\n";
|
|
|
|
}
|
|
|
|
|
2010-01-21 08:38:50 +00:00
|
|
|
/**
|
|
|
|
* Returns JavaScript code to initialise a new object
|
|
|
|
* @param string|null $var If it is null then no var is assigned the new object
|
|
|
|
* @param string $class
|
|
|
|
* @param array $arguments
|
|
|
|
* @param array $requirements
|
|
|
|
* @param int $delay
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
|
|
|
|
if (is_array($arguments)) {
|
|
|
|
$arguments = array_map('json_encode', $arguments);
|
|
|
|
$arguments = implode(', ', $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($var === null) {
|
2010-02-05 06:36:09 +00:00
|
|
|
$js = "new $class(Y, $arguments);";
|
2010-01-21 08:38:50 +00:00
|
|
|
} else if (strpos($var, '.')!==false) {
|
2010-02-05 06:36:09 +00:00
|
|
|
$js = "$var = new $class(Y, $arguments);";
|
2010-01-21 08:38:50 +00:00
|
|
|
} else {
|
2010-02-05 06:36:09 +00:00
|
|
|
$js = "var $var = new $class(Y, $arguments);";
|
2010-01-21 08:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($delay) {
|
|
|
|
$delay = $delay * 1000; // in miliseconds
|
|
|
|
$js = "setTimeout(function() { $js }, $delay);";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($requirements) > 0) {
|
|
|
|
$requirements = implode("', '", $requirements);
|
2010-02-05 06:36:09 +00:00
|
|
|
$js = "Y.use('$requirements', function(Y){ $js });";
|
2010-01-21 08:38:50 +00:00
|
|
|
}
|
|
|
|
return $js."\n";
|
|
|
|
}
|
|
|
|
|
2010-01-20 20:01:24 +00:00
|
|
|
/**
|
|
|
|
* 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 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 mixed $selector standard YUI selector for elemnts, 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 array $arguments An optional array of argument parameters to pass to the function
|
|
|
|
* @return string JS code fragment
|
|
|
|
*/
|
|
|
|
public 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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 17:13:52 +00:00
|
|
|
|
|
|
|
// ===============================================================================================
|
2010-02-17 19:33:24 +00:00
|
|
|
// TODO: Following HTML components still need some refactoring
|
2010-01-13 17:13:52 +00:00
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2010-02-11 10:32:39 +00:00
|
|
|
* Base class for classes representing HTML elements.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
|
|
|
* Handles the id and class attributes.
|
|
|
|
*
|
|
|
|
* @copyright 2009 Tim Hunt
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2009-12-27 19:50:15 +00:00
|
|
|
class html_component {
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
|
|
|
* @var string value to use for the id attribute of this HTML tag.
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $id = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
|
|
|
* @var string $alt value to use for the alt attribute of this HTML tag.
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $alt = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
|
|
|
* @var string $style value to use for the style attribute of this HTML tag.
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $style = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
|
|
|
* @var array class names to add to this HTML element.
|
|
|
|
*/
|
|
|
|
public $classes = array();
|
|
|
|
/**
|
|
|
|
* @var string $title The title attributes applicable to any XHTML element
|
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $title = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
|
|
|
* An optional array of component_action objects handling the action part of this component.
|
|
|
|
* @var array $actions
|
|
|
|
*/
|
|
|
|
protected $actions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure some class names are an array.
|
|
|
|
* @param mixed $classes either an array of class names or a space-separated
|
|
|
|
* string containing class names.
|
|
|
|
* @return array the class names as an array.
|
|
|
|
*/
|
|
|
|
public static function clean_classes($classes) {
|
2009-08-20 08:50:50 +00:00
|
|
|
if (empty($classes)) {
|
2009-08-20 13:18:08 +00:00
|
|
|
return array();
|
2009-08-20 08:50:50 +00:00
|
|
|
} else if (is_array($classes)) {
|
2009-08-10 06:22:04 +00:00
|
|
|
return $classes;
|
|
|
|
} else {
|
|
|
|
return explode(' ', trim($classes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the class name array.
|
|
|
|
* @param mixed $classes either an array of class names or a space-separated
|
|
|
|
* string containing class names.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_classes($classes) {
|
|
|
|
$this->classes = self::clean_classes($classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a class name to the class names array.
|
|
|
|
* @param string $class the new class name to add.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_class($class) {
|
|
|
|
$this->classes[] = $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a whole lot of class names to the class names array.
|
|
|
|
* @param mixed $classes either an array of class names or a space-separated
|
|
|
|
* string containing class names.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_classes($classes) {
|
2009-09-04 21:53:24 +00:00
|
|
|
$this->classes = array_merge($this->classes, self::clean_classes($classes));
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the class names as a string.
|
|
|
|
* @return string the class names as a space-separated string. Ready to be put in the class="" attribute.
|
|
|
|
*/
|
|
|
|
public function get_classes_string() {
|
|
|
|
return implode(' ', $this->classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform any cleanup or final processing that should be done before an
|
2009-12-27 12:58:29 +00:00
|
|
|
* instance of this class is output. This method is supposed to be called
|
|
|
|
* only from renderers.
|
2009-08-10 06:22:04 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-02-17 19:33:24 +00:00
|
|
|
public function prepare() {
|
2009-08-10 06:22:04 +00:00
|
|
|
$this->classes = array_unique(self::clean_classes($this->classes));
|
|
|
|
}
|
2009-08-13 01:15:58 +00:00
|
|
|
}
|
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 08:38:45 +00:00
|
|
|
/**
|
|
|
|
* Holds all the information required to render a <table> by
|
2009-12-16 18:00:58 +00:00
|
|
|
* {@see core_renderer::table()} or by an overridden version of that
|
2009-08-10 08:38:45 +00:00
|
|
|
* method in a subclass.
|
|
|
|
*
|
|
|
|
* Example of usage:
|
|
|
|
* $t = new html_table();
|
|
|
|
* ... // set various properties of the object $t as described below
|
|
|
|
* echo $OUTPUT->table($t);
|
|
|
|
*
|
|
|
|
* @copyright 2009 David Mudrak <david.mudrak@gmail.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2010-02-16 17:35:26 +00:00
|
|
|
class html_table extends html_component {
|
2009-08-10 08:38:45 +00:00
|
|
|
/**
|
2009-09-09 03:28:26 +00:00
|
|
|
* For more control over the rendering of the headers, an array of html_table_cell objects
|
2009-08-25 07:31:13 +00:00
|
|
|
* can be passed instead of an array of strings.
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var array of headings. The n-th array item is used as a heading of the n-th column.
|
|
|
|
*
|
|
|
|
* Example of usage:
|
|
|
|
* $t->head = array('Student', 'Grade');
|
|
|
|
*/
|
|
|
|
public $head;
|
|
|
|
/**
|
|
|
|
* @var array can be used to make a heading span multiple columns
|
|
|
|
*
|
|
|
|
* Example of usage:
|
|
|
|
* $t->headspan = array(2,1);
|
|
|
|
*
|
|
|
|
* In this example, {@see html_table:$data} is supposed to have three columns. For the first two columns,
|
|
|
|
* the same heading is used. Therefore, {@see html_table::$head} should consist of two items.
|
|
|
|
*/
|
|
|
|
public $headspan;
|
|
|
|
/**
|
|
|
|
* @var array of column alignments. The value is used as CSS 'text-align' property. Therefore, possible
|
|
|
|
* values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
|
|
|
|
* of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
|
|
|
|
*
|
2009-08-10 06:37:29 +00:00
|
|
|
* Examples of usage:
|
|
|
|
* $t->align = array(null, 'right');
|
|
|
|
* or
|
|
|
|
* $t->align[1] = 'right';
|
|
|
|
*
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $align;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var array of column sizes. The value is used as CSS 'size' property.
|
|
|
|
*
|
|
|
|
* Examples of usage:
|
|
|
|
* $t->size = array('50%', '50%');
|
|
|
|
* or
|
|
|
|
* $t->size[1] = '120px';
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $size;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var array of wrapping information. The only possible value is 'nowrap' that sets the
|
|
|
|
* CSS property 'white-space' to the value 'nowrap' in the given column.
|
|
|
|
*
|
|
|
|
* Example of usage:
|
|
|
|
* $t->wrap = array(null, 'nowrap');
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $wrap;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var array of arrays or html_table_row objects containing the data. Alternatively, if you have
|
|
|
|
* $head specified, the string 'hr' (for horizontal ruler) can be used
|
|
|
|
* instead of an array of cells data resulting in a divider rendered.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
2009-08-10 06:37:29 +00:00
|
|
|
* Example of usage with array of arrays:
|
|
|
|
* $row1 = array('Harry Potter', '76 %');
|
|
|
|
* $row2 = array('Hermione Granger', '100 %');
|
|
|
|
* $t->data = array($row1, $row2);
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
2009-08-10 06:37:29 +00:00
|
|
|
* Example with array of html_table_row objects: (used for more fine-grained control)
|
|
|
|
* $cell1 = new html_table_cell();
|
|
|
|
* $cell1->text = 'Harry Potter';
|
|
|
|
* $cell1->colspan = 2;
|
|
|
|
* $row1 = new html_table_row();
|
|
|
|
* $row1->cells[] = $cell1;
|
|
|
|
* $cell2 = new html_table_cell();
|
|
|
|
* $cell2->text = 'Hermione Granger';
|
|
|
|
* $cell3 = new html_table_cell();
|
|
|
|
* $cell3->text = '100 %';
|
|
|
|
* $row2 = new html_table_row();
|
|
|
|
* $row2->cells = array($cell2, $cell3);
|
|
|
|
* $t->data = array($row1, $row2);
|
|
|
|
*/
|
|
|
|
public $data;
|
|
|
|
/**
|
|
|
|
* @var string width of the table, percentage of the page preferred. Defaults to 80% of the page width.
|
|
|
|
* @deprecated since Moodle 2.0. Styling should be in the CSS.
|
|
|
|
*/
|
|
|
|
public $width = null;
|
|
|
|
/**
|
|
|
|
* @var string alignment the whole table. Can be 'right', 'left' or 'center' (default).
|
|
|
|
* @deprecated since Moodle 2.0. Styling should be in the CSS.
|
|
|
|
*/
|
|
|
|
public $tablealign = null;
|
|
|
|
/**
|
|
|
|
* @var int padding on each cell, in pixels
|
|
|
|
* @deprecated since Moodle 2.0. Styling should be in the CSS.
|
|
|
|
*/
|
|
|
|
public $cellpadding = null;
|
|
|
|
/**
|
|
|
|
* @var int spacing between cells, in pixels
|
|
|
|
* @deprecated since Moodle 2.0. Styling should be in the CSS.
|
|
|
|
*/
|
|
|
|
public $cellspacing = null;
|
|
|
|
/**
|
|
|
|
* @var array classes to add to particular rows, space-separated string.
|
|
|
|
* Classes 'r0' or 'r1' are added automatically for every odd or even row,
|
|
|
|
* respectively. Class 'lastrow' is added automatically for the last row
|
|
|
|
* in the table.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
2009-08-10 06:37:29 +00:00
|
|
|
* Example of usage:
|
|
|
|
* $t->rowclasses[9] = 'tenth'
|
|
|
|
*/
|
|
|
|
public $rowclasses;
|
|
|
|
/**
|
|
|
|
* @var array classes to add to every cell in a particular column,
|
|
|
|
* space-separated string. Class 'cell' is added automatically by the renderer.
|
|
|
|
* Classes 'c0' or 'c1' are added automatically for every odd or even column,
|
|
|
|
* respectively. Class 'lastcol' is added automatically for all last cells
|
|
|
|
* in a row.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
2009-08-10 06:37:29 +00:00
|
|
|
* Example of usage:
|
|
|
|
* $t->colclasses = array(null, 'grade');
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $colclasses;
|
|
|
|
/**
|
|
|
|
* @var string description of the contents for screen readers.
|
|
|
|
*/
|
|
|
|
public $summary;
|
|
|
|
/**
|
|
|
|
* @var bool true causes the contents of the heading cells to be rotated 90 degrees.
|
|
|
|
*/
|
|
|
|
public $rotateheaders = false;
|
2009-08-28 05:29:05 +00:00
|
|
|
/**
|
|
|
|
* @var array $headclasses Array of CSS classes to apply to the table's thead.
|
|
|
|
*/
|
|
|
|
public $headclasses = array();
|
|
|
|
/**
|
|
|
|
* @var array $bodyclasses Array of CSS classes to apply to the table's tbody.
|
|
|
|
*/
|
|
|
|
public $bodyclasses = array();
|
|
|
|
/**
|
|
|
|
* @var array $footclasses Array of CSS classes to apply to the table's tfoot.
|
|
|
|
*/
|
|
|
|
public $footclasses = array();
|
2009-09-09 03:28:26 +00:00
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
|
|
|
|
/**
|
2009-12-27 19:50:15 +00:00
|
|
|
* @see html_component::prepare()
|
2009-08-10 06:37:29 +00:00
|
|
|
* @return void
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-17 19:33:24 +00:00
|
|
|
public function prepare() {
|
2009-08-10 06:37:29 +00:00
|
|
|
if (!empty($this->align)) {
|
|
|
|
foreach ($this->align as $key => $aa) {
|
|
|
|
if ($aa) {
|
|
|
|
$this->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
|
|
|
|
} else {
|
2010-02-03 16:27:52 +00:00
|
|
|
$this->align[$key] = null;
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
2009-08-10 06:37:29 +00:00
|
|
|
if (!empty($this->size)) {
|
|
|
|
foreach ($this->size as $key => $ss) {
|
|
|
|
if ($ss) {
|
|
|
|
$this->size[$key] = 'width:'. $ss .';';
|
|
|
|
} else {
|
2010-02-03 16:27:52 +00:00
|
|
|
$this->size[$key] = null;
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
2009-08-10 06:37:29 +00:00
|
|
|
if (!empty($this->wrap)) {
|
|
|
|
foreach ($this->wrap as $key => $ww) {
|
|
|
|
if ($ww) {
|
|
|
|
$this->wrap[$key] = 'white-space:nowrap;';
|
|
|
|
} else {
|
|
|
|
$this->wrap[$key] = '';
|
|
|
|
}
|
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
2009-08-10 06:37:29 +00:00
|
|
|
if (!empty($this->head)) {
|
|
|
|
foreach ($this->head as $key => $val) {
|
|
|
|
if (!isset($this->align[$key])) {
|
2010-02-03 16:27:52 +00:00
|
|
|
$this->align[$key] = null;
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
if (!isset($this->size[$key])) {
|
2010-02-03 16:27:52 +00:00
|
|
|
$this->size[$key] = null;
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
if (!isset($this->wrap[$key])) {
|
2010-02-03 16:27:52 +00:00
|
|
|
$this->wrap[$key] = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
if (empty($this->classes)) { // must be done before align
|
|
|
|
$this->set_classes(array('generaltable'));
|
|
|
|
}
|
|
|
|
if (!empty($this->tablealign)) {
|
|
|
|
$this->add_class('boxalign' . $this->tablealign);
|
|
|
|
}
|
|
|
|
if (!empty($this->rotateheaders)) {
|
|
|
|
$this->add_class('rotateheaders');
|
2009-08-10 06:22:04 +00:00
|
|
|
} else {
|
2009-08-10 06:37:29 +00:00
|
|
|
$this->rotateheaders = false; // Makes life easier later.
|
|
|
|
}
|
2010-02-17 19:33:24 +00:00
|
|
|
parent::prepare();
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* Component representing a table row.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Nicolas Connault
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2009-12-27 19:50:15 +00:00
|
|
|
class html_table_row extends html_component {
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var array $cells Array of html_table_cell objects
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 08:38:45 +00:00
|
|
|
public $cells = array();
|
2009-08-10 06:22:04 +00:00
|
|
|
|
2009-08-10 06:37:29 +00:00
|
|
|
/**
|
2009-12-27 19:50:15 +00:00
|
|
|
* @see lib/html_component#prepare()
|
2009-08-10 06:37:29 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-02-17 19:33:24 +00:00
|
|
|
public function prepare() {
|
|
|
|
parent::prepare();
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
2009-09-09 03:28:26 +00:00
|
|
|
|
2009-08-25 07:31:13 +00:00
|
|
|
/**
|
2010-02-17 19:33:24 +00:00
|
|
|
* Constructor
|
2009-08-25 07:31:13 +00:00
|
|
|
* @param array $cells
|
2010-02-17 19:33:24 +00:00
|
|
|
*/
|
|
|
|
public function __construct(array $cells=null) {
|
|
|
|
$cells = (array)$cells;
|
|
|
|
foreach ($cells as $cell) {
|
|
|
|
if ($cell instanceof html_table_cell) {
|
|
|
|
$this->cells[] = $cell;
|
2009-08-28 11:38:33 +00:00
|
|
|
} else {
|
2010-02-17 19:33:24 +00:00
|
|
|
$this->cells[] = new html_table_cell($cell);
|
2009-08-28 11:38:33 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-25 07:31:13 +00:00
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* Component representing a table cell.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Nicolas Connault
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2009-12-27 19:50:15 +00:00
|
|
|
class html_table_cell extends html_component {
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var string $text The contents of the cell
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 08:38:45 +00:00
|
|
|
public $text;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var string $abbr Abbreviated version of the contents of the cell
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $abbr = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var int $colspan Number of columns this cell should span
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $colspan = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var int $rowspan Number of rows this cell should span
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $rowspan = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 08:38:45 +00:00
|
|
|
* @var string $scope Defines a way to associate header cells and data cells in a table
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-03 16:27:52 +00:00
|
|
|
public $scope = null;
|
2009-08-11 07:05:46 +00:00
|
|
|
/**
|
|
|
|
* @var boolean $header Whether or not this cell is a header cell
|
|
|
|
*/
|
2009-09-03 23:31:49 +00:00
|
|
|
public $header = null;
|
2009-08-10 06:22:04 +00:00
|
|
|
|
|
|
|
/**
|
2009-12-27 19:50:15 +00:00
|
|
|
* @see lib/html_component#prepare()
|
2009-08-10 06:22:04 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-02-17 19:33:24 +00:00
|
|
|
public function prepare() {
|
2009-08-25 07:31:13 +00:00
|
|
|
if ($this->header && empty($this->scope)) {
|
|
|
|
$this->scope = 'col';
|
|
|
|
}
|
2010-02-17 19:33:24 +00:00
|
|
|
parent::prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct($text = null) {
|
|
|
|
$this->text = $text;
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 08:38:45 +00:00
|
|
|
/// Complex components aggregating simpler components
|
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* Component representing a paging bar.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
|
|
|
* @copyright 2009 Nicolas Connault
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2010-02-17 16:59:41 +00:00
|
|
|
class paging_bar implements renderable {
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var int $maxdisplay The maximum number of pagelinks to display
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $maxdisplay = 18;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var int $totalcount post or get
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $totalcount;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var int $page The page you are currently viewing
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-17 16:59:41 +00:00
|
|
|
public $page;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var int $perpage The number of entries that should be shown per page
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $perpage;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var string $baseurl 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.
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $baseurl;
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc)
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-17 16:59:41 +00:00
|
|
|
public $pagevar;
|
2009-08-10 06:37:29 +00:00
|
|
|
/**
|
2010-02-16 17:23:49 +00:00
|
|
|
* @var string $previouslink A HTML link representing the "previous" page
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
|
|
|
public $previouslink = null;
|
|
|
|
/**
|
2010-02-16 17:23:49 +00:00
|
|
|
* @var tring $nextlink A HTML link representing the "next" page
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
|
|
|
public $nextlink = null;
|
|
|
|
/**
|
2010-02-16 17:23:49 +00:00
|
|
|
* @var tring $firstlink A HTML link representing the first page
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
|
|
|
public $firstlink = null;
|
|
|
|
/**
|
2010-02-16 17:23:49 +00:00
|
|
|
* @var tring $lastlink A HTML link representing the last page
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
|
|
|
public $lastlink = null;
|
|
|
|
/**
|
2010-02-16 17:23:49 +00:00
|
|
|
* @var array $pagelinks An array of strings. One of them is just a string: the current page
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
|
|
|
public $pagelinks = array();
|
2009-08-10 06:22:04 +00:00
|
|
|
|
2010-02-17 16:59:41 +00:00
|
|
|
/**
|
|
|
|
* Constructor paging_bar with only the required params.
|
|
|
|
*
|
|
|
|
* @param int $totalcount Thetotal 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;
|
|
|
|
}
|
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-12-27 19:50:15 +00:00
|
|
|
* @see lib/html_component#prepare()
|
2009-08-10 06:22:04 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2009-12-27 12:58:29 +00:00
|
|
|
public function prepare(renderer_base $output, moodle_page $page, $target) {
|
2009-08-24 15:10:36 +00:00
|
|
|
if (!isset($this->totalcount) || is_null($this->totalcount)) {
|
2010-02-17 16:59:41 +00:00
|
|
|
throw new coding_exception('paging_bar requires a totalcount value.');
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
if (!isset($this->page) || is_null($this->page)) {
|
2010-02-17 16:59:41 +00:00
|
|
|
throw new coding_exception('paging_bar requires a page value.');
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
if (empty($this->perpage)) {
|
2010-02-17 16:59:41 +00:00
|
|
|
throw new coding_exception('paging_bar requires a perpage value.');
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
if (empty($this->baseurl)) {
|
2010-02-17 16:59:41 +00:00
|
|
|
throw new coding_exception('paging_bar requires a baseurl value.');
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
|
2009-08-10 06:37:29 +00:00
|
|
|
if ($this->totalcount > $this->perpage) {
|
|
|
|
$pagenum = $this->page - 1;
|
2009-08-10 06:22:04 +00:00
|
|
|
|
2009-08-10 06:37:29 +00:00
|
|
|
if ($this->page > 0) {
|
2010-02-17 16:59:41 +00:00
|
|
|
$this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous'));
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
|
2009-08-10 06:37:29 +00:00
|
|
|
if ($this->perpage > 0) {
|
|
|
|
$lastpage = ceil($this->totalcount / $this->perpage);
|
|
|
|
} else {
|
|
|
|
$lastpage = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->page > 15) {
|
|
|
|
$startpage = $this->page - 10;
|
|
|
|
|
2010-02-17 16:59:41 +00:00
|
|
|
$this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first'));
|
2009-08-10 06:37:29 +00:00
|
|
|
} else {
|
|
|
|
$startpage = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$currpage = $startpage;
|
|
|
|
$displaycount = $displaypage = 0;
|
|
|
|
|
|
|
|
while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
|
|
|
|
$displaypage = $currpage + 1;
|
|
|
|
|
2009-09-14 07:26:54 +00:00
|
|
|
if ($this->page == $currpage) {
|
2009-08-10 06:37:29 +00:00
|
|
|
$this->pagelinks[] = $displaypage;
|
|
|
|
} else {
|
2010-02-16 17:23:49 +00:00
|
|
|
$pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage);
|
2009-08-10 06:37:29 +00:00
|
|
|
$this->pagelinks[] = $pagelink;
|
|
|
|
}
|
|
|
|
|
|
|
|
$displaycount++;
|
|
|
|
$currpage++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($currpage < $lastpage) {
|
|
|
|
$lastpageactual = $lastpage - 1;
|
2010-02-17 03:53:28 +00:00
|
|
|
$this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last'));
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$pagenum = $this->page + 1;
|
|
|
|
|
|
|
|
if ($pagenum != $displaypage) {
|
2010-02-17 03:53:28 +00:00
|
|
|
$this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next'));
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* This class represents how a block appears on a page.
|
2009-08-10 06:22:04 +00:00
|
|
|
*
|
2009-08-10 06:37:29 +00:00
|
|
|
* During output, each block instance is asked to return a block_contents object,
|
|
|
|
* those are then passed to the $OUTPUT->block function for display.
|
|
|
|
*
|
|
|
|
* {@link $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
|
2009-08-10 06:22:04 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*/
|
2010-02-17 18:36:26 +00:00
|
|
|
class block_contents {
|
2009-08-10 06:37:29 +00:00
|
|
|
/** @var int used to set $skipid. */
|
|
|
|
protected static $idcounter = 1;
|
|
|
|
|
|
|
|
const NOT_HIDEABLE = 0;
|
|
|
|
const VISIBLE = 1;
|
|
|
|
const HIDDEN = 2;
|
|
|
|
|
2009-08-10 06:22:04 +00:00
|
|
|
/**
|
2010-02-17 18:36:26 +00:00
|
|
|
* @var integer $skipid All the blocks (or things that look like blocks)
|
2009-08-10 06:37:29 +00:00
|
|
|
* printed on a page are given a unique number that can be used to construct
|
|
|
|
* id="" attributes. This is set automatically be the {@link prepare()} method.
|
|
|
|
* Do not try to set it manually.
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2009-08-10 06:37:29 +00:00
|
|
|
public $skipid;
|
2009-08-10 06:22:04 +00:00
|
|
|
|
|
|
|
/**
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var integer 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 integer 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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $attributes an array of attribute => value pairs that are put on the
|
|
|
|
* outer div of this block. {@link $id} and {@link $classes} attributes should be set separately.
|
|
|
|
*/
|
2010-02-17 18:36:26 +00:00
|
|
|
public $attributes;
|
2009-08-10 06:37:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $title 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 = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $content HTML for the content
|
|
|
|
*/
|
|
|
|
public $content = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $list an alternative to $content, it you want a list of things with optional icons.
|
|
|
|
*/
|
|
|
|
public $footer = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.'
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $annotation = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer one of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
|
|
|
|
* the user can toggle whether this block is visible.
|
|
|
|
*/
|
|
|
|
public $collapsible = self::NOT_HIDEABLE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A (possibly empty) array of editing controls. Each element of this array
|
|
|
|
* should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
|
2009-12-16 21:50:45 +00:00
|
|
|
* $icon is the icon name. Fed to $OUTPUT->pix_url.
|
2009-08-10 06:37:29 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $controls = array();
|
|
|
|
|
2010-02-17 18:36:26 +00:00
|
|
|
|
2009-08-10 06:37:29 +00:00
|
|
|
/**
|
2010-02-17 18:36:26 +00:00
|
|
|
* Create new instance of block content
|
|
|
|
* @param array $attributes
|
2009-08-10 06:22:04 +00:00
|
|
|
*/
|
2010-02-17 18:36:26 +00:00
|
|
|
public function __construct(array $attributes=null) {
|
2009-08-10 06:37:29 +00:00
|
|
|
$this->skipid = self::$idcounter;
|
|
|
|
self::$idcounter += 1;
|
2010-02-17 18:36:26 +00:00
|
|
|
|
|
|
|
if ($attributes) {
|
|
|
|
// standard block
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
} else {
|
|
|
|
// simple "fake" blocks used in some modules and "Add new block" block
|
|
|
|
$this->attributes = array('class'=>'sideblock');
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|
2010-02-17 18:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add html class to block
|
|
|
|
* @param string $class
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_class($class) {
|
|
|
|
$this->attributes['class'] .= ' '.$class;
|
2009-08-10 06:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-10 06:37:29 +00:00
|
|
|
|
2009-12-27 12:58:29 +00:00
|
|
|
|
2009-08-10 06:37:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2010-02-17 18:36:26 +00:00
|
|
|
class block_move_target {
|
2009-08-10 06:37:29 +00:00
|
|
|
/**
|
2010-02-17 18:36:26 +00:00
|
|
|
* Move url
|
|
|
|
* @var moodle_url
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
2010-02-17 18:36:26 +00:00
|
|
|
public $url;
|
2009-08-10 06:37:29 +00:00
|
|
|
/**
|
2010-02-17 18:36:26 +00:00
|
|
|
* label
|
|
|
|
* @var string
|
2009-08-10 06:37:29 +00:00
|
|
|
*/
|
2010-02-17 18:36:26 +00:00
|
|
|
public $text;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cosntructor
|
|
|
|
* @param string $text
|
|
|
|
* @param moodle_url $url
|
|
|
|
*/
|
|
|
|
public function __construct($text, moodle_url $url) {
|
|
|
|
$this->text = $text;
|
|
|
|
$this->url = $url;
|
|
|
|
}
|
2009-08-10 06:37:29 +00:00
|
|
|
}
|