Merge branch 'MDL-70917-39' of git://github.com/paulholden/moodle into MOODLE_39_STABLE
[moodle.git] / lib / form / listing.php
blobdc520b74ee27afc786c998df4df11dad4c04663c
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 $this->_generateId();
114 $elementid = $this->getAttribute('id');
115 $mainhtml = html_writer::tag('div', $this->items[$this->getValue()]->mainhtml,
116 array('id' => $elementid . '_items_main', 'class' => 'formlistingmain'));
118 // Add the main div containing the selected item (+ the caption: "More items").
119 $html = html_writer::tag('div', $mainhtml .
120 html_writer::tag('div', $this->showall,
121 array('id' => $elementid . '_items_caption', 'class' => 'formlistingmore')),
122 array('id' => $elementid . '_items', 'class' => 'formlisting hide'));
124 // Add collapsible region: all the items.
125 $itemrows = '';
126 $html .= html_writer::tag('div', $itemrows,
127 array('id' => $elementid . '_items_all', 'class' => 'formlistingall'));
129 // Add radio buttons for non javascript support.
130 $radiobuttons = '';
131 foreach ($this->items as $itemid => $item) {
132 $radioparams = array('name' => $this->getName(), 'value' => $itemid,
133 'id' => 'id_'.$itemid, 'class' => 'formlistinginputradio', 'type' => 'radio');
134 if ($itemid == $this->getValue()) {
135 $radioparams['checked'] = 'checked';
137 $radiobuttons .= html_writer::tag('div', html_writer::tag('input',
138 html_writer::tag('div', $item->rowhtml, array('class' => 'formlistingradiocontent')), $radioparams),
139 array('class' => 'formlistingradio'));
142 // Container for the hidden hidden input which will contain the selected item.
143 $html .= html_writer::tag('div', $radiobuttons,
144 array('id' => 'formlistinginputcontainer_' . $elementid, 'class' => 'formlistinginputcontainer'));
146 $module = array('name'=>'form_listing', 'fullpath'=>'/lib/form/yui/listing/listing.js',
147 'requires'=>array('node', 'event', 'transition', 'escape'));
149 $PAGE->requires->js_init_call('M.form_listing.init',
150 array(array(
151 'elementid' => $elementid.'_items',
152 'hideall' => $this->hideall,
153 'showall' => $this->showall,
154 'hiddeninputid' => $this->getAttribute('id'),
155 'items' => $this->items,
156 'inputname' => $elementid,
157 'currentvalue' => $this->getValue())), true, $module);
159 return $html;