weekly release 5.0dev
[moodle.git] / lib / classes / output / chooser.php
blob0f7c86df65aa4f5aa092a85813beb579c23366b5
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.
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/>.
17 /**
18 * The chooser renderable.
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
25 namespace core\output;
27 use lang_string;
28 use moodle_url;
29 use stdClass;
31 /**
32 * The chooser renderable class.
34 * @package core
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. */
40 public $actionurl;
41 /** @var lang_string The instructions to display. */
42 public $instructions;
43 /** @var string The form method. */
44 public $method = 'post';
45 /** @var string The name of the parameter for the items value. */
46 public $paramname;
47 /** @var array The list of hidden parameters. See {@link self::add_param}. */
48 public $params = [];
49 /** @var chooser_section[] The sections */
50 public $sections;
51 /** @var lang_string The chooser title. */
52 public $title;
54 /**
55 * Constructor.
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;
69 /**
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) {
77 if (!$id) {
78 $id = $name;
80 $this->params[] = [
81 'name' => $name,
82 'value' => $value,
83 'id' => $id,
87 /**
88 * Set the chooser instructions.
90 * @param lang_string $value The instructions.
92 public function set_instructions(lang_string $value) {
93 $this->instructions = $value;
96 /**
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.
109 * @return stdClass
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);
124 }, $this->sections);
126 return $data;