1441 |
ariadna |
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 |
namespace core\output\local;
|
|
|
18 |
|
|
|
19 |
use core\output\named_templatable;
|
|
|
20 |
use core\output\renderable;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Collapsable section output.
|
|
|
24 |
*
|
|
|
25 |
* @package core
|
|
|
26 |
* @copyright 2024 Ferran Recio <ferran@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class collapsable_section implements named_templatable, renderable {
|
|
|
30 |
/**
|
|
|
31 |
* Constructor.
|
|
|
32 |
*
|
|
|
33 |
* @param string $titlecontent The content to be displayed inside the button.
|
|
|
34 |
* @param string $sectioncontent The content to be displayed inside the dialog.
|
|
|
35 |
* @param string $classes Additional CSS classes to be applied to the section.
|
|
|
36 |
* @param array $extras An attribute => value array to be added to the element.
|
|
|
37 |
* @param bool $open If the section is opened by default.
|
|
|
38 |
* @param string|null $expandlabel The label for the expand button.
|
|
|
39 |
* @param string|null $collapselabel The label for the collapse button.
|
|
|
40 |
*/
|
|
|
41 |
public function __construct(
|
|
|
42 |
/** @var string $titlecontent The content to be displayed inside the button. */
|
|
|
43 |
protected string $titlecontent,
|
|
|
44 |
/** @var string $sectioncontent The content to be displayed inside the dialog. */
|
|
|
45 |
protected string $sectioncontent,
|
|
|
46 |
/** @var string $classes Additional CSS classes to be applied to the section. */
|
|
|
47 |
protected string $classes = '',
|
|
|
48 |
/** @var array $extras A attribute => value array to be added to the element. */
|
|
|
49 |
protected array $extras = [],
|
|
|
50 |
/** @var bool $open if the section is opened by default. */
|
|
|
51 |
protected bool $open = false,
|
|
|
52 |
/** @var string|null $expandlabel The label for the expand button. */
|
|
|
53 |
protected string|null $expandlabel = null,
|
|
|
54 |
/** @var string|null $collapselabel The label for the collapse button. */
|
|
|
55 |
protected string|null $collapselabel = null,
|
|
|
56 |
) {
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Set the title content.
|
|
|
61 |
*
|
|
|
62 |
* @param string $titlecontent
|
|
|
63 |
*/
|
|
|
64 |
public function set_title_content(string $titlecontent) {
|
|
|
65 |
$this->titlecontent = $titlecontent;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Sets the content for the collapsable section.
|
|
|
70 |
*
|
|
|
71 |
* @param string $sectioncontent The content to be set for the section.
|
|
|
72 |
*/
|
|
|
73 |
public function set_section_content(string $sectioncontent) {
|
|
|
74 |
$this->sectioncontent = $sectioncontent;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Sets the CSS classes for the collapsable section.
|
|
|
79 |
*
|
|
|
80 |
* @param string $classes The CSS classes to be applied to the collapsable section.
|
|
|
81 |
*/
|
|
|
82 |
public function set_classes(string $classes) {
|
|
|
83 |
$this->classes = $classes;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Merges the provided extras array with the existing extras array.
|
|
|
88 |
*
|
|
|
89 |
* @param array $extras The array of extra attributes => extra value.
|
|
|
90 |
*/
|
|
|
91 |
public function add_extra_attributes(array $extras) {
|
|
|
92 |
$this->extras = array_merge($this->extras, $extras);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Sets the default open state of the collapsible section.
|
|
|
97 |
*
|
|
|
98 |
* @param bool $open
|
|
|
99 |
*/
|
|
|
100 |
public function set_open(bool $open) {
|
|
|
101 |
$this->open = $open;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
#[\Override]
|
|
|
105 |
public function export_for_template(\renderer_base $output): array {
|
|
|
106 |
$elementid = $this->extras['id'] ?? \html_writer::random_id('collapsableSection_');
|
|
|
107 |
|
|
|
108 |
$data = [
|
|
|
109 |
'titlecontent' => $this->titlecontent,
|
|
|
110 |
'sectioncontent' => $this->sectioncontent,
|
|
|
111 |
'classes' => $this->classes,
|
|
|
112 |
'extras' => $this->export_extras(),
|
|
|
113 |
'elementid' => $elementid,
|
|
|
114 |
];
|
|
|
115 |
if ($this->open) {
|
|
|
116 |
$data['open'] = 'true';
|
|
|
117 |
}
|
|
|
118 |
if ($this->expandlabel) {
|
|
|
119 |
$data['expandlabel'] = $this->expandlabel;
|
|
|
120 |
}
|
|
|
121 |
if ($this->collapselabel) {
|
|
|
122 |
$data['collapselabel'] = $this->collapselabel;
|
|
|
123 |
}
|
|
|
124 |
return $data;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Exports the extras as an array of attribute-value pairs.
|
|
|
129 |
*
|
|
|
130 |
* @return array An array of associative arrays, each containing 'attribute' and 'value' keys.
|
|
|
131 |
*/
|
|
|
132 |
private function export_extras(): array {
|
|
|
133 |
$extras = [];
|
|
|
134 |
foreach ($this->extras as $attribute => $value) {
|
|
|
135 |
$extras[] = [
|
|
|
136 |
'attribute' => $attribute,
|
|
|
137 |
'value' => $value,
|
|
|
138 |
];
|
|
|
139 |
}
|
|
|
140 |
return $extras;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
#[\Override]
|
|
|
144 |
public function get_template_name(\renderer_base $renderer): string {
|
|
|
145 |
return 'core/local/collapsable_section';
|
|
|
146 |
}
|
|
|
147 |
}
|