aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/AdminCourseOptionsWidget.php
blob: 9de5b71e472c1caefa0c37185893cca1a676fa9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
 * This is a special widget class for use with the admin course page.
 * It will connect to the Vue app and use it's method to change the filters.
 *
 * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @since Stud.IP 5.4
 */
class AdminCourseOptionsWidget extends ListWidget
{
    /**
     * @var string|null
     */
    protected $position_in_sidebar = null;

    public function __construct(string $title)
    {
        parent::__construct();

        $this->addCSSClass('widget-options admin-courses-options');
        $this->title = $title;
    }

    /**
     * Adds a checkbox to the widget.
     *
     * @param string $label
     * @param string $filter_name
     * @param bool   $checked
     * @param        $true_value
     * @param        $false_value
     * @param array  $attributes
     *
     * @return ButtonElement
     */
    public function addCheckbox(
        string $label,
        string $filter_name,
        bool $checked = false,
        $true_value = 1,
        $false_value = null,
        array $attributes = []
    ): ButtonElement {
        if (!isset($attributes['onclick'])) {
            $attributes['onclick'] = implode('', [
                sprintf(
                    'STUDIP.AdminCourses.App.changeFilter({%s: $(this).is(".options-checked") ? %s : %s});',
                    json_encode($filter_name),
                    json_encode($false_value),
                    json_encode($true_value)
                ),
                "return false;",
            ]);
        }

        $attributes['class'] = trim(($attributes['class'] ?? '') . ' options-checkbox options-' . ($checked ? 'checked' : 'unchecked'));

        return $this->addElement(
            new ButtonElement($label, null, $attributes + [
                'aria-checked' => $checked ? 'true' : 'false',
                'role'         => 'checkbox',
            ])
        );
    }

    /**
     * Adds a radio button to the widget.
     *
     * @param string $label
     * @param string $filter_name
     * @param        $value
     * @param bool   $checked
     * @param array  $attributes
     *
     * @return ButtonElement
     */
    public function addRadioButton(
        string $label,
        string $filter_name,
        $value,
        bool $checked = false,
        array $attributes = []
    ): ButtonElement {
        if (!isset($attributes['onclick'])) {
            $attributes['onclick'] = implode('', [
                sprintf(
                    'STUDIP.AdminCourses.App.changeFilter({%s: %s});',
                    json_encode($filter_name),
                    json_encode($value)
                ),
                "return false;",
            ]);
        }

        $attributes['class'] = trim(($attributes['class'] ?? '') . ' options-radio options-' . ($checked ? 'checked' : 'unchecked'));

        return $this->addElement(
            new ButtonElement($label, null, $attributes + [
                'aria-checked'     => $checked ? 'true' : 'false',
                'data-filter-name' => $filter_name,
                'role'             => 'radio',
            ])
        );
    }

    /**
     * Adds a select list to the widget.
     *
     * @param string $label
     * @param string $filter_name
     * @param array  $options
     * @param        $selected_value
     * @param array  $attributes
     */
    public function addSelect(
        string $label,
        string $filter_name,
        array $options,
        $selected_value = null,
        array $attributes = []
    ): SelectListElement {
        if (!isset($attributes['onchange'])) {
            $attributes['onfocus'] = 'this.classList.remove("submit-upon-select");';

            $attributes['onchange'] = sprintf(
                'STUDIP.AdminCourses.App.changeFilter({%s: this.value});',
                json_encode($filter_name)
            );
        }

        return $this->addElement(
            new SelectListElement($label, $filter_name, $options, $selected_value, $attributes)
        );
    }

    /**
     * Sets the position where the widget should be inserted in the sidebar.
     */
    public function setPositionInSidebar(?string $position): void
    {
        $this->position_in_sidebar = $position;
    }

    /**
     * Returns the position where the widget should be inserted in the sidebar.
     *
     * @return string|null
     */
    public function getPositionInSidebar(): ?string
    {
        return $this->position_in_sidebar;
    }
}