Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_course\output\actionbar;
18
 
19
use core\output\comboboxsearch;
20
use stdClass;
21
 
22
/**
23
 * Renderable class for the initial selector element in the action bar.
24
 *
25
 * @package    core_course
26
 * @copyright  2024 Kevin Percy <kevin.percy@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class initials_selector extends comboboxsearch {
30
 
31
    /**
32
     * The class constructor.
33
     *
34
     * @param stdClass $course The course object.
35
     * @param string $targeturl The target URL to send the form to.
36
     * @param string $firstinitial The selected first initial.
37
     * @param string $lastinitial The selected last initial.
38
     * @param string $firstinitialparam The parameter name for the first initial.
39
     * @param string $lastinitialparam The parameter name for the last initial.
40
     * @param array $additionalparams Any additional parameters required for the form submission URL.
41
     */
42
    public function __construct(
43
        protected stdClass $course,
44
        protected string $targeturl,
45
        protected string $firstinitial = '',
46
        protected string $lastinitial = '',
47
        protected string $firstinitialparam = 'sifirst',
48
        protected string $lastinitialparam = 'silast',
49
        protected array $additionalparams = []
50
    ) {
51
        // The second and third arguments (buttoncontent and dropdowncontent) need to be rendered here, since the comboboxsearch
52
        // template expects HTML in its respective context properties. Ideally, children of comboboxsearch would leverage Mustache's
53
        // blocks pragma, meaning a child template could extend the comboboxsearch, allowing rendering of the child component,
54
        // instead of needing to inject the child's content HTML as part of rendering the comboboxsearch parent, as is the case
55
        // here. Achieving this, however, requires a refactor of comboboxsearch. For now, this must be pre-rendered and injected.
56
        $filterstatestring = $this->get_current_filter_state_string();
57
        parent::__construct(
58
            false,
59
            $filterstatestring !== '' ? $filterstatestring : get_string('filterbyname', 'course'),
60
            $this->render_initials_dropdown_form(),
61
            'initials-selector',
62
            'initialswidget',
63
            'initialsdropdown',
64
            $filterstatestring !== '' ? get_string('name') : null,
65
            true,
66
            get_string('filterbyname', 'course'),
67
            'nameinitials',
68
            json_encode([
69
                'first' => $firstinitial,
70
                'last' => $lastinitial,
71
            ])
72
        );
73
    }
74
 
75
    /**
76
     * Method to generate the current filter string for the initial selector label.
77
     *
78
     * @return string the HTML string representing the current initials filter state. E.g. "First (A)", or empty if none selected.
79
     */
80
    private function get_current_filter_state_string(): string {
81
        if ($this->firstinitial !== '' && $this->lastinitial !== '') {
82
            return get_string('filterbothactive', 'course', ['first' => $this->firstinitial, 'last' => $this->lastinitial]);
83
        } else if ($this->firstinitial !== '') {
84
            return get_string('filterfirstactive', 'course', ['first' => $this->firstinitial]);
85
        } else if ($this->lastinitial !== '') {
86
            return get_string('filterlastactive', 'course', ['last' => $this->lastinitial]);
87
        }
88
 
89
        return '';
90
    }
91
 
92
    /**
93
     * Method to generate the output for the initial selector.
94
     *
95
     * @return string the rendered HTML content.
96
     */
97
    private function render_initials_dropdown_form(): string {
98
        global $PAGE;
99
 
100
        $initialsdropdownform = new initials_dropdown_form(
101
            $this->course,
102
            $this->targeturl,
103
            $this->firstinitial,
104
            $this->lastinitial,
105
            $this->firstinitialparam,
106
            $this->lastinitialparam,
107
            $this->additionalparams
108
        );
109
        return $PAGE->get_renderer('core', 'course')->render($initialsdropdownform);
110
    }
111
}