Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * The renderable for core/checkbox-toggleall.
19
 *
20
 * @package    core
21
 * @copyright  2019 Jun Pataleta
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core\output;
26
 
27
use stdClass;
28
 
29
/**
30
 * The checkbox-toggleall renderable class.
31
 *
32
 * @package    core
33
 * @copyright  2019 Jun Pataleta
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class checkbox_toggleall implements renderable, templatable {
37
    /** @var string The name of the group of checkboxes to be toggled. */
38
    protected $togglegroup;
39
 
40
    /** @var bool $ismaster Whether we're rendering for a master checkbox or a slave checkbox. */
41
    protected $ismaster;
42
 
43
    /** @var array $options The options for the checkbox. */
44
    protected $options;
45
 
46
    /** @var bool $isbutton Whether to render this as a button. Applies to master checkboxes only. */
47
    protected $isbutton;
48
 
49
    /**
50
     * Constructor.
51
     *
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:
55
     *     <ul>
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>
69
     *     </ul>
70
     * @param bool $isbutton Whether to render this as a button. Applies to master only.
71
     */
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;
77
    }
78
 
79
    /**
80
     * Export for template.
81
     *
82
     * @param renderer_base $output The renderer.
83
     * @return stdClass
84
     */
85
    public function export_for_template(renderer_base $output) {
86
        $data = (object)[
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,
95
        ];
96
 
97
        if ($this->ismaster) {
98
            $data->selectall = $this->options['selectall'] ?? get_string('selectall');
99
            $data->deselectall = $this->options['deselectall'] ?? get_string('deselectall');
100
        }
101
 
102
        return $data;
103
    }
104
 
105
    /**
106
     * Fetches the appropriate template for the checkbox toggle all element.
107
     *
108
     * @return string
109
     */
110
    public function get_template() {
111
        if ($this->ismaster) {
112
            if ($this->isbutton) {
113
                return 'core/checkbox-toggleall-master-button';
114
            } else {
115
                return 'core/checkbox-toggleall-master';
116
            }
117
        }
118
        return 'core/checkbox-toggleall-slave';
119
    }
120
}