Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / form / listing.php
blobcd9477cb33ade032aaf518dc78469711dd5973ba
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Listing form element.
20 * Contains HTML class for a listing form element.
22 * @package core_form
23 * @copyright 2012 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.');
31 require_once("HTML/QuickForm/input.php");
33 /**
34 * The listing element is a simple customizable "select" without the input type=select.
35 * One main div contains the "large" html of an item.
36 * A show/hide div shows a hidden div containing the list of all items.
37 * This list is composed by the "small" html of each item.
39 * How to use it:
40 * The options parameter is an array containing:
41 * - items => array of object: the key is the value of the form input
42 * $item->rowhtml => small html
43 * $item->mainhtml => large html
44 * - showall/hideall => string for the Show/Hide button
46 * WARNINGS: The form lets you display HTML. So it is subject to CROSS-SCRIPTING if you send it uncleaned HTML.
47 * Don't forget to escape your HTML as soon as one string comes from an input/external source.
49 * How to customize it:
50 * You can change the css in core.css. For example if you remove float:left; from .formlistingrow,
51 * then the item list is not display as tabs but as rows.
53 * @package core_form
54 * @copyright 2012 Jerome Mouneyrac
55 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
57 class MoodleQuickForm_listing extends HTML_QuickForm_input {
59 /** @var array items to display. */
60 protected $items = array();
62 /** @var string language string for Show All. */
63 protected $showall;
65 /** @var string language string for Hide. */
66 protected $hideall;
68 /**
69 * Constructor.
71 * @param string $elementName (optional) name of the listing.
72 * @param string $elementLabel (optional) listing label.
73 * @param array $attributes (optional) Either a typical HTML attribute string or an associative array.
74 * @param array $options set of options to initalize listing.
76 public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=array()) {
78 $this->_type = 'listing';
79 if (!empty($options['items'])) {
80 $this->items = $options['items'];
82 if (!empty($options['showall'])) {
83 $this->showall = $options['showall'];
84 } else {
85 $this->showall = get_string('showall');
87 if (!empty($options['hideall'])) {
88 $this->hideall = $options['hideall'];
89 } else {
90 $this->hideall = get_string('hide');
92 parent::__construct($elementName, $elementLabel, $attributes);
95 /**
96 * Old syntax of class constructor. Deprecated in PHP7.
98 * @deprecated since Moodle 3.1
100 public function MoodleQuickForm_listing($elementName=null, $elementLabel=null, $attributes=null, $options=array()) {
101 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
102 self::__construct($elementName, $elementLabel, $attributes, $options);
106 * Returns HTML for listing form element.
108 * @return string the HTML.
110 function toHtml() {
111 global $CFG, $PAGE;
113 $mainhtml = html_writer::tag('div', $this->items[$this->getValue()]->mainhtml,
114 array('id' => $this->getName().'_items_main', 'class' => 'formlistingmain'));
116 // Add the main div containing the selected item (+ the caption: "More items").
117 $html = html_writer::tag('div', $mainhtml .
118 html_writer::tag('div', $this->showall,
119 array('id' => $this->getName().'_items_caption', 'class' => 'formlistingmore')),
120 array('id'=>$this->getName().'_items', 'class' => 'formlisting hide'));
122 // Add collapsible region: all the items.
123 $itemrows = '';
124 $html .= html_writer::tag('div', $itemrows,
125 array('id' => $this->getName().'_items_all', 'class' => 'formlistingall'));
127 // Add radio buttons for non javascript support.
128 $radiobuttons = '';
129 foreach ($this->items as $itemid => $item) {
130 $radioparams = array('name' => $this->getName(), 'value' => $itemid,
131 'id' => 'id_'.$itemid, 'class' => 'formlistinginputradio', 'type' => 'radio');
132 if ($itemid == $this->getValue()) {
133 $radioparams['checked'] = 'checked';
135 $radiobuttons .= html_writer::tag('div', html_writer::tag('input',
136 html_writer::tag('div', $item->rowhtml, array('class' => 'formlistingradiocontent')), $radioparams),
137 array('class' => 'formlistingradio'));
140 // Container for the hidden hidden input which will contain the selected item.
141 $html .= html_writer::tag('div', $radiobuttons,
142 array('id' => 'formlistinginputcontainer_' . $this->getName(), 'class' => 'formlistinginputcontainer'));
144 $module = array('name'=>'form_listing', 'fullpath'=>'/lib/form/yui/listing/listing.js',
145 'requires'=>array('node', 'event', 'transition', 'escape'));
147 $PAGE->requires->js_init_call('M.form_listing.init',
148 array(array(
149 'elementid' => $this->getName().'_items',
150 'hideall' => $this->hideall,
151 'showall' => $this->showall,
152 'hiddeninputid' => $this->getAttribute('id'),
153 'items' => $this->items,
154 'inputname' => $this->getName(),
155 'currentvalue' => $this->getValue())), true, $module);
157 return $html;