MDL-37012 some typos, add a new lang string, change tag 'a' for 'link', some code...
[moodle.git] / lib / form / listing.php
blob102b492571506f2ea93bd2bea08137a0844e4026
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 function MoodleQuickForm_listing($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::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
95 /**
96 * Returns HTML for listing form element.
98 * @return string the HTML.
100 function toHtml() {
101 global $CFG, $PAGE;
103 $mainhtml = html_writer::tag('div', $this->items[$this->getValue()]->mainhtml,
104 array('id' => $this->getName().'_items_main', 'class' => 'formlistingmain'));
106 // Add the main div containing the selected item (+ the caption: "More items").
107 $html = html_writer::tag('div', $mainhtml .
108 html_writer::tag('div', $this->showall,
109 array('id' => $this->getName().'_items_caption', 'class' => 'formlistingmore')),
110 array('id'=>$this->getName().'_items', 'class' => 'formlisting hide'));
112 // Add collapsible region: all the items.
113 $itemrows = '';
114 $html .= html_writer::tag('div', $itemrows,
115 array('id' => $this->getName().'_items_all', 'class' => 'formlistingall'));
117 // Add radio buttons for non javascript support.
118 $radiobuttons = '';
119 foreach ($this->items as $itemid => $item) {
120 $radioparams = array('name' => $this->getName(), 'value' => $itemid,
121 'id' => 'id_'.$itemid, 'class' => 'formlistinginputradio', 'type' => 'radio');
122 if ($itemid == $this->getValue()) {
123 $radioparams['checked'] = 'checked';
125 $radiobuttons .= html_writer::tag('div', html_writer::tag('input',
126 html_writer::tag('div', $item->rowhtml, array('class' => 'formlistingradiocontent')), $radioparams),
127 array('class' => 'formlistingradio'));
130 // Container for the hidden hidden input which will contain the selected item.
131 $html .= html_writer::tag('div', $radiobuttons,
132 array('id' => 'formlistinginputcontainer_' . $this->getName(), 'class' => 'formlistinginputcontainer'));
134 $module = array('name'=>'form_listing', 'fullpath'=>'/lib/form/yui/listing/listing.js',
135 'requires'=>array('node', 'event', 'transition', 'escape'));
137 $PAGE->requires->js_init_call('M.form_listing.init',
138 array(array(
139 'elementid' => $this->getName().'_items',
140 'hideall' => $this->hideall,
141 'showall' => $this->showall,
142 'hiddeninputid' => $this->getAttribute('id'),
143 'items' => $this->items,
144 'inputname' => $this->getName(),
145 'currentvalue' => $this->getValue())), true, $module);
147 return $html;