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
namespace core\output\local\dropdown;
18
 
19
use core\output\choicelist;
20
 
21
/**
22
 * Class to render a dropdown dialog element.
23
 *
24
 * A dropdown dialog allows to render any arbitrary HTML into a dropdown elements triggered
25
 * by a button.
26
 *
27
 * @package    core
28
 * @category   output
29
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class status extends dialog {
33
    /**
34
     * @var choicelist content of dialog.
35
     */
36
    protected $choices = null;
37
 
38
    /**
39
     * Constructor.
40
     *
41
     * The definition object could contain the following keys:
42
     * - classes: component CSS classes.
43
     * - buttonclasses: the button CSS classes.
44
     * - dialogwidth: the dropdown width.
45
     * - extras: extra HTML attributes (attribute => value).
46
     * - buttonsync: if the button should be synced with the selected value.
47
     * - updatestatus: if component must update the status and trigger a change event when clicked.
48
     *
49
     * @param string $buttoncontent the button content
50
     * @param choicelist $choices the choice object
51
     * @param array $definition an optional array of the element definition
52
     */
53
    public function __construct(string $buttoncontent, choicelist $choices, array $definition = []) {
54
        parent::__construct($buttoncontent, '', $definition);
55
        $this->set_choice($choices);
56
        if ($definition['buttonsync'] ?? false) {
57
            $this->extras['data-button-sync'] = 'true';
58
        }
59
        if ($definition['updatestatus'] ?? false) {
60
            $this->extras['data-update-status'] = 'true';
61
        }
62
    }
63
 
64
    /**
65
     * Set the dialog contents.
66
     *
67
     * @param choicelist $choices
68
     */
69
    public function set_choice(choicelist $choices) {
70
        $this->choices = $choices;
71
        $description = $choices->get_description();
72
        if (!empty($description)) {
73
            $this->set_content($description);
74
        }
75
    }
76
 
77
    /**
78
     * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
79
     *
80
     * @param \renderer_base $output typically, the renderer that's calling this function
81
     * @return array data context for a mustache template
82
     */
83
    public function export_for_template(\renderer_base $output): array {
84
        $data = parent::export_for_template($output);
85
        if ($this->choices !== null) {
86
            $data['choices'] = $this->choices->export_for_template($output);
87
        }
88
        $selectedvalue = $this->choices->get_selected_value();
89
        if ($selectedvalue !== null) {
90
            $data['extras'][] = (object)[
91
                'attribute' => 'data-value',
92
                'value' => $selectedvalue,
93
            ];
94
        }
95
        return $data;
96
    }
97
 
98
    /**
99
     * Get the name of the template to use for this templatable.
100
     *
101
     * @param \renderer_base $renderer The renderer requesting the template name
102
     * @return string the template name
103
     */
104
    public function get_template_name(\renderer_base $renderer): string {
105
        return 'core/local/dropdown/status';
106
    }
107
}