Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / output / chooser.php
blobd63831fe4d004c9534840d69205f65165f8017c8
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;
26 defined('MOODLE_INTERNAL') || die();
28 use lang_string;
29 use moodle_url;
30 use renderer_base;
31 use renderable;
32 use stdClass;
33 use templatable;
35 /**
36 * The chooser renderable class.
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
42 class chooser implements renderable, templatable {
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;
59 /**
60 * Constructor.
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;
74 /**
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) {
82 if (!$id) {
83 $id = $name;
85 $this->params[] = [
86 'name' => $name,
87 'value' => $value,
88 'id' => $id
92 /**
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.
114 * @return stdClass
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);
129 }, $this->sections);
131 return $data;