2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * The chooser renderable.
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core\output
;
26 defined('MOODLE_INTERNAL') ||
die();
36 * The chooser renderable class.
39 * @copyright 2016 Frédéric Massart - FMCorz.net
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class chooser
implements renderable
, templatable
{
44 /** @var moodle_url The form action URL. */
46 /** @var lang_string The instructions to display. */
48 /** @var string The form method. */
49 public $method = 'post';
50 /** @var string The name of the parameter for the items value. */
52 /** @var array The list of hidden parameters. See {@link self::add_param}. */
54 /** @var chooser_section[] The sections */
56 /** @var lang_string The chooser title. */
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.
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;
75 * Add a parameter to submit with the form.
77 * @param string $name The parameter name.
78 * @param string $value The parameter value.
79 * @param string $id The parameter ID.
81 public function add_param($name, $value, $id = null) {
93 * Set the chooser instructions.
95 * @param lang_string $value The instructions.
97 public function set_instructions(lang_string
$value) {
98 $this->instructions
= $value;
102 * Set the form method.
104 * @param string $value The method.
106 public function set_method($value) {
107 $this->method
= $value;
111 * Export for template.
113 * @param renderer_base The renderer.
116 public function export_for_template(renderer_base
$output) {
117 $data = new stdClass();
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
;
127 $data->sections
= array_map(function($section) use ($output) {
128 return $section->export_for_template($output);