Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Listing form element.
  19   *
  20   * Contains HTML class for a listing form element.
  21   *
  22   * @package   core_form
  23   * @copyright 2012 Jerome Mouneyrac
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  if (!defined('MOODLE_INTERNAL')) {
  28      die('Direct access to this script is forbidden.');
  29  }
  30  
  31  require_once("HTML/QuickForm/input.php");
  32  
  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.
  38  *
  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
  45  *
  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.
  48  *
  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.
  52  *
  53  * @package   core_form
  54  * @copyright 2012 Jerome Mouneyrac
  55  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  56  */
  57  class MoodleQuickForm_listing extends HTML_QuickForm_input {
  58  
  59      /** @var array items to display. */
  60      protected $items = array();
  61  
  62      /** @var string language string for Show All. */
  63      protected $showall;
  64  
  65      /** @var string language string for Hide. */
  66      protected $hideall;
  67  
  68      /**
  69       * Constructor.
  70       *
  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.
  75       */
  76      public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=array()) {
  77  
  78         $this->_type = 'listing';
  79          if (!empty($options['items'])) {
  80              $this->items = $options['items'];
  81          }
  82          if (!empty($options['showall'])) {
  83              $this->showall = $options['showall'];
  84          } else {
  85              $this->showall = get_string('showall');
  86          }
  87          if (!empty($options['hideall'])) {
  88              $this->hideall = $options['hideall'];
  89          } else {
  90              $this->hideall = get_string('hide');
  91          }
  92          parent::__construct($elementName, $elementLabel, $attributes);
  93      }
  94  
  95      /**
  96       * Old syntax of class constructor. Deprecated in PHP7.
  97       *
  98       * @deprecated since Moodle 3.1
  99       */
 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);
 103      }
 104  
 105      /**
 106       * Returns HTML for listing form element.
 107       *
 108       * @return string the HTML.
 109       */
 110      function toHtml() {
 111          global $CFG, $PAGE;
 112  
 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'));
 117  
 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'));
 123  
 124          // Add collapsible region: all the items.
 125          $itemrows = '';
 126          $html .= html_writer::tag('div', $itemrows,
 127                  array('id' => $elementid . '_items_all', 'class' => 'formlistingall'));
 128  
 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';
 136              }
 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'));
 140          }
 141  
 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'));
 145  
 146          $module = array('name'=>'form_listing', 'fullpath'=>'/lib/form/yui/listing/listing.js',
 147              'requires'=>array('node', 'event', 'transition', 'escape'));
 148  
 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);
 158  
 159          return $html;
 160      }
 161  }