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_courseformat\local\overview;
18
 
19
use core\output\renderable;
20
use core\output\renderer_base;
21
use core\output\local\properties\text_align;
22
 
23
/**
24
 * Class overviewitem
25
 *
26
 * @package    core_courseformat
27
 * @copyright  2025 Ferran Recio <ferran@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class overviewitem {
31
    /**
32
     * Overview item constructor.
33
     *
34
     * @param string $name The name of the activity.
35
     * @param int|string|bool|null $value The section name.
36
     * @param string|renderable|null $content The item content.
37
     * @param text_align $textalign The preferred text alignment.
38
     * @param int $alertcount an optional numeric indicator for alerts (zero means no alerts).
39
     * @param string $alertlabel the meaning to show with the alert count.
40
     */
41
    public function __construct(
42
        /** @var string the name of the activity */
43
        protected string $name,
44
        /** @var string the section name */
45
        protected int|string|bool|null $value,
46
        /** @var string the item content */
47
        protected string|renderable|null $content = null,
48
        /** @var text_align the preferred text alignment. */
49
        protected text_align $textalign = text_align::START,
50
        /** @var int an optional numeric indicator for alerts (zero means no alerts). */
51
        protected int $alertcount = 0,
52
        /** @var string the badge label an optional label for the badge. */
53
        protected string $alertlabel = '',
54
    ) {
55
    }
56
 
57
    /**
58
     * Retrieves the name of the overview item.
59
     *
60
     * @return string
61
     */
62
    public function get_name(): string {
63
        return $this->name;
64
    }
65
 
66
    /**
67
     * Retrieves the value of the overview item.
68
     *
69
     * @return int|string|bool|null
70
     */
71
    public function get_value(): int|string|bool|null {
72
        return $this->value;
73
    }
74
 
75
    /**
76
     * Gets the content for this item.
77
     *
78
     * Items can utilize either a renderable object or a pre-rendered string as their content.
79
     *
80
     * - For simple items, a plain string is sufficient and can be used in any context.
81
     * - For more complex items, a renderable object is preferable. This allows the item
82
     *   to be rendered differently depending on the context, providing greater flexibility.
83
     *
84
     * @return string|\core\output\renderable|null
85
     */
86
    public function get_content(): string|renderable|null {
87
        return $this->content ?? (string) $this->value ?? null;
88
    }
89
 
90
    /**
91
     * Gets the rendered content for this item.
92
     *
93
     * This method is used when the context does not have any specific requirements
94
     * and could use the default item content rendering.
95
     *
96
     * @param \core\output\renderer_base $output
97
     * @return string
98
     */
99
    public function get_rendered_content(renderer_base $output): string {
100
        if ($this->content instanceof renderable) {
101
            return $output->render($this->content);
102
        }
103
        return $this->get_content() ?? '';
104
    }
105
 
106
    /**
107
     * Gets the preferred text alignment of the item.
108
     *
109
     * @return text_align The text alignment.
110
     */
111
    public function get_text_align(): text_align {
112
        return $this->textalign;
113
    }
114
 
115
    /**
116
     * Gets the alert count for the item.
117
     *
118
     * Alert count is an optional numeric indicator for alerts used for filtering,
119
     * highlighting, or the mobile APP badge display.
120
     *
121
     * @return int The alert count.
122
     */
123
    public function get_alert_count(): int {
124
        return $this->alertcount;
125
    }
126
 
127
    /**
128
     * Gets the alert label for the item.
129
     *
130
     * @return string The alert label.
131
     */
132
    public function get_alert_label(): string {
133
        return $this->alertlabel;
134
    }
135
 
136
    /**
137
     * Sets the content for this item.
138
     *
139
     * Items can utilize either a renderable object or a pre-rendered string as their content.
140
     *
141
     * @param string|renderable|null $content
142
     * @return $this
143
     */
144
    public function set_content(string|renderable|null $content): static {
145
        $this->content = $content;
146
        return $this;
147
    }
148
 
149
    /**
150
     * Sets the preferred text alignment of the item.
151
     *
152
     * @param text_align $textalign
153
     * @return $this
154
     */
155
    public function set_text_align(text_align $textalign): static {
156
        $this->textalign = $textalign;
157
        return $this;
158
    }
159
 
160
    /**
161
     * Sets the value of the overview item.
162
     *
163
     * @param int|string|bool|null $value
164
     * @return $this
165
     */
166
    public function set_value(int|string|bool|null $value): static {
167
        $this->value = $value;
168
        return $this;
169
    }
170
 
171
    /**
172
     * Sets the name of the overview item.
173
     *
174
     * @param string $name
175
     * @return $this
176
     */
177
    public function set_name(string $name): static {
178
        $this->name = $name;
179
        return $this;
180
    }
181
 
182
    /**
183
     * Sets the alert count and alert label for the item.
184
     *
185
     * @param int $alertcount
186
     * @param string $alertlabel
187
     * @return $this
188
     */
189
    public function set_alert(int $alertcount, string $alertlabel): static {
190
        $this->alertcount = $alertcount;
191
        $this->alertlabel = $alertlabel;
192
        return $this;
193
    }
194
}