[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/form/ -> listing.php (source)

   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      function MoodleQuickForm_listing($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::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  93      }
  94  
  95      /**
  96       * Returns HTML for listing form element.
  97       *
  98       * @return string the HTML.
  99       */
 100      function toHtml() {
 101          global $CFG, $PAGE;
 102  
 103          $mainhtml = html_writer::tag('div', $this->items[$this->getValue()]->mainhtml,
 104                  array('id' => $this->getName().'_items_main', 'class' => 'formlistingmain'));
 105  
 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'));
 111  
 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'));
 116  
 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';
 124              }
 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'));
 128          }
 129  
 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'));
 133  
 134          $module = array('name'=>'form_listing', 'fullpath'=>'/lib/form/yui/listing/listing.js',
 135              'requires'=>array('node', 'event', 'transition', 'escape'));
 136  
 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);
 146  
 147          return $html;
 148      }
 149  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1