Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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   * The chooser renderable.
  19   *
  20   * @package    core
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use lang_string;
  29  use moodle_url;
  30  use renderer_base;
  31  use renderable;
  32  use stdClass;
  33  use templatable;
  34  
  35  /**
  36   * The chooser renderable class.
  37   *
  38   * @package    core
  39   * @copyright  2016 Frédéric Massart - FMCorz.net
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class chooser implements renderable, templatable {
  43  
  44      /** @var moodle_url The form action URL. */
  45      public $actionurl;
  46      /** @var lang_string The instructions to display. */
  47      public $instructions;
  48      /** @var string The form method. */
  49      public $method = 'post';
  50      /** @var string The name of the parameter for the items value. */
  51      public $paramname;
  52      /** @var array The list of hidden parameters. See {@link self::add_param}. */
  53      public $params = [];
  54      /** @var chooser_section[] The sections */
  55      public $sections;
  56      /** @var lang_string The chooser title. */
  57      public $title;
  58  
  59      /**
  60       * Constructor.
  61       *
  62       * @param moodle_url $actionurl The form action URL.
  63       * @param lang_string $title The title of the chooser.
  64       * @param chooser_section[] $sections The sections.
  65       * @param string $paramname The name of the parameter for the items value.
  66       */
  67      public function __construct(moodle_url $actionurl, lang_string $title, array $sections, $paramname) {
  68          $this->actionurl = $actionurl;
  69          $this->title = $title;
  70          $this->sections = $sections;
  71          $this->paramname = $paramname;
  72      }
  73  
  74      /**
  75       * Add a parameter to submit with the form.
  76       *
  77       * @param string $name The parameter name.
  78       * @param string $value The parameter value.
  79       * @param string $id The parameter ID.
  80       */
  81      public function add_param($name, $value, $id = null) {
  82          if (!$id) {
  83              $id = $name;
  84          }
  85          $this->params[] = [
  86              'name' => $name,
  87              'value' => $value,
  88              'id' => $id
  89          ];
  90      }
  91  
  92      /**
  93       * Set the chooser instructions.
  94       *
  95       * @param lang_string $value The instructions.
  96       */
  97      public function set_instructions(lang_string $value) {
  98          $this->instructions = $value;
  99      }
 100  
 101      /**
 102       * Set the form method.
 103       *
 104       * @param string $value The method.
 105       */
 106      public function set_method($value) {
 107          $this->method = $value;
 108      }
 109  
 110      /**
 111       * Export for template.
 112       *
 113       * @param renderer_base The renderer.
 114       * @return stdClass
 115       */
 116      public function export_for_template(renderer_base $output) {
 117          $data = new stdClass();
 118  
 119          $data->actionurl = $this->actionurl->out(false);
 120          $data->instructions = (string) $this->instructions;
 121          $data->method = $this->method;
 122          $data->paramname = $this->paramname;
 123          $data->params = $this->params;
 124          $data->sesskey = sesskey();
 125          $data->title = (string) $this->title;
 126  
 127          $data->sections = array_map(function($section) use ($output) {
 128              return $section->export_for_template($output);
 129          }, $this->sections);
 130  
 131          return $data;
 132      }
 133  
 134  }