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
;
32 * The chooser renderable class.
35 * @copyright 2016 Frédéric Massart - FMCorz.net
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class chooser
implements renderable
, templatable
{
39 /** @var moodle_url The form action URL. */
41 /** @var lang_string The instructions to display. */
43 /** @var string The form method. */
44 public $method = 'post';
45 /** @var string The name of the parameter for the items value. */
47 /** @var array The list of hidden parameters. See {@link self::add_param}. */
49 /** @var chooser_section[] The sections */
51 /** @var lang_string The chooser title. */
57 * @param moodle_url $actionurl The form action URL.
58 * @param lang_string $title The title of the chooser.
59 * @param chooser_section[] $sections The sections.
60 * @param string $paramname The name of the parameter for the items value.
62 public function __construct(moodle_url
$actionurl, lang_string
$title, array $sections, $paramname) {
63 $this->actionurl
= $actionurl;
64 $this->title
= $title;
65 $this->sections
= $sections;
66 $this->paramname
= $paramname;
70 * Add a parameter to submit with the form.
72 * @param string $name The parameter name.
73 * @param string $value The parameter value.
74 * @param string $id The parameter ID.
76 public function add_param($name, $value, $id = null) {
88 * Set the chooser instructions.
90 * @param lang_string $value The instructions.
92 public function set_instructions(lang_string
$value) {
93 $this->instructions
= $value;
97 * Set the form method.
99 * @param string $value The method.
101 public function set_method($value) {
102 $this->method
= $value;
106 * Export for template.
108 * @param renderer_base The renderer.
111 public function export_for_template(renderer_base
$output) {
112 $data = new stdClass();
114 $data->actionurl
= $this->actionurl
->out(false);
115 $data->instructions
= (string) $this->instructions
;
116 $data->method
= $this->method
;
117 $data->paramname
= $this->paramname
;
118 $data->params
= $this->params
;
119 $data->sesskey
= sesskey();
120 $data->title
= (string) $this->title
;
122 $data->sections
= array_map(function ($section) use ($output) {
123 return $section->export_for_template($output);