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;
18
 
19
use core\component;
20
use core\exception\coding_exception;
21
use core\output\local\properties\iconsize;
22
use core\output\renderable;
23
use core\output\renderer_base;
24
use core\output\templatable;
25
use core\url;
26
use cm_info;
27
 
28
/**
29
 * Class activity_icon
30
 *
31
 * @package    core_course
32
 * @copyright  2025 Ferran Recio <ferran@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class activity_icon implements renderable, templatable {
36
 
37
    /** @var string optional text title */
38
    protected string $title = '';
39
 
40
    /** @var string Extra container classes. */
41
    protected string $extraclasses = '';
42
 
43
    /** @var bool Determine if the icon must be colored or not. */
44
    protected bool $colourize = true;
45
 
46
    /** @var iconsize set the icon size */
47
    protected iconsize $iconsize = iconsize::UNDEFINED;
48
 
49
    /** @var url The icon URL. */
50
    protected url $iconurl;
51
 
52
    /** @var string The module purpose */
53
    protected string $purpose;
54
 
55
    /** @var bool is branded */
56
    protected bool $isbranded;
57
 
58
    /**
59
     * Constructor.
60
     *
61
     * @param string $modname the module name
62
     */
63
    protected function __construct(
64
        /** @var string the module name */
65
        protected string $modname,
66
    ) {
67
        try {
68
            $this->isbranded = component_callback('mod_' . $this->modname, 'is_branded', [], false);
69
        } catch (coding_exception $e) {
70
            debugging($e->getMessage(), DEBUG_DEVELOPER);
71
            $this->isbranded = false;
72
        }
73
        $this->purpose = plugin_supports('mod', $this->modname, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER);
74
    }
75
 
76
    /**
77
     * Create an activity icon from a cm_info object.
78
     *
79
     * @param cm_info $cm
80
     * @return self
81
     */
82
    public static function from_cm_info(cm_info $cm): self {
83
        $result = new self($cm->modname);
84
        $result->iconurl = $cm->get_icon_url();
85
        return $result;
86
    }
87
 
88
    /**
89
     * Create an activity icon from a module name.
90
     *
91
     * @param string $modname
92
     * @return self
93
     */
94
    public static function from_modname(string $modname): self {
95
        return new self($modname);
96
    }
97
 
98
    /**
99
     * Set the title.
100
     *
101
     * @param string $title
102
     * @return self
103
     */
104
    public function set_title(string $title): self {
105
        $this->title = $title;
106
        return $this;
107
    }
108
 
109
    /**
110
     * Set the colourize icon value.
111
     *
112
     * @param bool $colourize
113
     * @return self
114
     */
115
    public function set_colourize(bool $colourize): self {
116
        $this->colourize = $colourize;
117
        return $this;
118
    }
119
 
120
    /**
121
     * Set the extra classes.
122
     *
123
     * @param string $extraclasses
124
     * @return self
125
     */
126
    public function set_extra_classes(string $extraclasses): self {
127
        $this->extraclasses = $extraclasses;
128
        return $this;
129
    }
130
 
131
    /**
132
     * Set the icon size.
133
     *
134
     * @param iconsize $iconsize
135
     * @return self
136
     */
137
    public function set_icon_size(iconsize $iconsize): self {
138
        $this->iconsize = $iconsize;
139
        return $this;
140
    }
141
 
142
    #[\Override]
143
    public function export_for_template(renderer_base $output): array {
144
        if (!isset($this->iconurl)) {
145
            $this->iconurl = $this->get_icon_url($output);
146
        }
147
        $needfiltering = $this->colourize && $this->iconurl->get_param('filtericon');
148
        $iconclass = $needfiltering ? '' : 'nofilter';
149
 
150
        $data = [
151
            'icon' => $this->iconurl,
152
            'iconclass' => $iconclass,
153
            'modname' => $this->modname,
154
            'pluginname' => get_string('pluginname', 'mod_' . $this->modname),
155
            'purpose' => $this->purpose,
156
            'branded' => $this->isbranded,
157
            'extraclasses' => $this->extraclasses . $this->iconsize->classes(),
158
        ];
159
 
160
        if (!empty($this->title)) {
161
            $data['title'] = $this->title;
162
        }
163
 
164
        return $data;
165
    }
166
 
167
    /**
168
     * Get the icon URL.
169
     *
170
     * @param renderer_base $output
171
     * @return url
172
     */
173
    public function get_icon_url(renderer_base $output): url {
174
        $icon = $output->image_url('monologo', $this->modname);
175
        // Legacy activity modules may only have an `icon` icon instead of a `monologo` icon.
176
        $ismonologo = component::has_monologo_icon('mod', $this->modname);
177
 
178
        if ($ismonologo) {
179
            // The filtericon param is used to determine if the icon should be colored or not.
180
            // The name of the param is not colorize to preserve backward compatibility.
181
            $icon->param('filtericon', 1);
182
        }
183
        return $icon;
184
    }
185
 
186
    /**
187
     * Check if the module is branded.
188
     *
189
     * @return bool
190
     */
191
    public function is_branded(): bool {
192
        return $this->isbranded;
193
    }
194
 
195
    /**
196
     * Get the colourize icon value.
197
     *
198
     * @return bool
199
     */
200
    public function get_colourize(): bool {
201
        return $this->colourize;
202
    }
203
 
204
    /**
205
     * Get the title text.
206
     *
207
     * @return string
208
     */
209
    public function get_title(): string {
210
        return $this->title;
211
    }
212
 
213
    /**
214
     * Get the extra classes.
215
     *
216
     * @return string
217
     */
218
    public function get_extra_classes(): string {
219
        return $this->extraclasses;
220
    }
221
 
222
    /**
223
     * Get the icon size.
224
     *
225
     * @return iconsize
226
     */
227
    public function get_icon_size(): iconsize {
228
        return $this->iconsize;
229
    }
230
 
231
    /**
232
     * Get the activity purpose.
233
     *
234
     * @return string
235
     */
236
    public function get_purpose(): string {
237
        return $this->purpose;
238
    }
239
}