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 renderable for core/checkbox-toggleall.
21 * @copyright 2019 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core\output
;
30 * The checkbox-toggleall renderable class.
33 * @copyright 2019 Jun Pataleta
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class checkbox_toggleall
implements renderable
, templatable
{
37 /** @var string The name of the group of checkboxes to be toggled. */
38 protected $togglegroup;
40 /** @var bool $ismaster Whether we're rendering for a master checkbox or a slave checkbox. */
43 /** @var array $options The options for the checkbox. */
46 /** @var bool $isbutton Whether to render this as a button. Applies to master checkboxes only. */
52 * @param string $togglegroup The name of the group of checkboxes to be toggled.
53 * @param bool $ismaster Whether we're rendering for a master checkbox or a slave checkbox.
54 * @param array $options The options for the checkbox. Valid options are:
56 * <li><b>id </b> string - The element ID.</li>
57 * <li><b>name </b> string - The element name.</li>
58 * <li><b>classes </b> string - CSS classes that you want to add for your checkbox or toggle controls.
59 * For button type master toggle controls, this could be any Bootstrap 4 btn classes
60 * that you might want to add. Defaults to "btn-secondary".</li>
61 * <li><b>value </b> string|int - The element's value.</li>
62 * <li><b>checked </b> boolean - Whether to render this initially as checked.</li>
63 * <li><b>label </b> string - The label for the checkbox element.</li>
64 * <li><b>labelclasses</b> string - CSS classes that you want to add for your label.</li>
65 * <li><b>selectall </b> string - Master only. The language string that will be used to indicate that clicking on
66 * the master will select all of the slave checkboxes. Defaults to "Select all".</li>
67 * <li><b>deselectall </b> string - Master only. The language string that will be used to indicate that clicking on
68 * the master will select all of the slave checkboxes. Defaults to "Deselect all".</li>
70 * @param bool $isbutton Whether to render this as a button. Applies to master only.
72 public function __construct(string $togglegroup, bool $ismaster, $options = [], $isbutton = false) {
73 $this->togglegroup
= $togglegroup;
74 $this->ismaster
= $ismaster;
75 $this->options
= $options;
76 $this->isbutton
= $ismaster && $isbutton;
80 * Export for template.
82 * @param renderer_base $output The renderer.
85 public function export_for_template(renderer_base
$output) {
87 'togglegroup' => $this->togglegroup
,
88 'id' => $this->options
['id'] ??
null,
89 'name' => $this->options
['name'] ??
null,
90 'value' => $this->options
['value'] ??
null,
91 'classes' => $this->options
['classes'] ??
null,
92 'label' => $this->options
['label'] ??
null,
93 'labelclasses' => $this->options
['labelclasses'] ??
null,
94 'checked' => $this->options
['checked'] ??
false,
97 if ($this->ismaster
) {
98 $data->selectall
= $this->options
['selectall'] ??
get_string('selectall');
99 $data->deselectall
= $this->options
['deselectall'] ??
get_string('deselectall');
106 * Fetches the appropriate template for the checkbox toggle all element.
110 public function get_template() {
111 if ($this->ismaster
) {
112 if ($this->isbutton
) {
113 return 'core/checkbox-toggleall-master-button';
115 return 'core/checkbox-toggleall-master';
118 return 'core/checkbox-toggleall-slave';