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_courseformat\output;
|
|
|
18 |
|
|
|
19 |
use cm_info;
|
|
|
20 |
use core_courseformat\output\local\courseformat_named_templatable;
|
|
|
21 |
use core\output\named_templatable;
|
|
|
22 |
use renderer_base;
|
|
|
23 |
use stdClass;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Base class to render an activity badge.
|
|
|
27 |
*
|
|
|
28 |
* Plugins can extend this class and override some methods to customize the content to be displayed in the activity badge.
|
|
|
29 |
*
|
|
|
30 |
* @package core_courseformat
|
|
|
31 |
* @copyright 2023 Sara Arjona <sara@moodle.com>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
abstract class activitybadge implements named_templatable, \renderable {
|
|
|
35 |
use courseformat_named_templatable;
|
|
|
36 |
|
|
|
37 |
/** @var array Badge defined styles. */
|
|
|
38 |
public const STYLES = [
|
|
|
39 |
'none' => 'badge-none',
|
|
|
40 |
'dark' => 'bg-dark text-white',
|
|
|
41 |
'danger' => 'bg-danger text-white',
|
|
|
42 |
'warning' => 'bg-warning text-dark',
|
|
|
43 |
'info' => 'bg-info text-white',
|
|
|
44 |
];
|
|
|
45 |
|
|
|
46 |
/** @var cm_info The course module information. */
|
|
|
47 |
protected $cminfo = null;
|
|
|
48 |
|
|
|
49 |
/** @var string The content to be displayed in the activity badge. */
|
|
|
50 |
protected $content = null;
|
|
|
51 |
|
|
|
52 |
/** @var string The style for the activity badge. */
|
|
|
53 |
protected $style = self::STYLES['none'];
|
|
|
54 |
|
|
|
55 |
/** @var \moodle_url An optional URL to redirect the user when the activity badge is clicked. */
|
|
|
56 |
protected $url = null;
|
|
|
57 |
|
|
|
58 |
/** @var string An optional element id in case the module wants to add some code for the activity badge (events, CSS...). */
|
|
|
59 |
protected $elementid = null;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @var array An optional array of extra HTML attributes to add to the badge element (for example, data attributes).
|
|
|
63 |
* The format for this array is [['name' => 'attr1', 'value' => 'attrval1'], ['name' => 'attr2', 'value' => 'attrval2']].
|
|
|
64 |
*/
|
|
|
65 |
protected $extraattributes = [];
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Constructor.
|
|
|
69 |
*
|
|
|
70 |
* @param cm_info $cminfo The course module information.
|
|
|
71 |
*/
|
|
|
72 |
public function __construct(cm_info $cminfo) {
|
|
|
73 |
$this->cminfo = $cminfo;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Export this data so it can be used as the context for a mustache template.
|
|
|
78 |
*
|
|
|
79 |
* @param renderer_base $output typically, the renderer that's calling this function
|
|
|
80 |
* @return stdClass data context for a mustache template
|
|
|
81 |
*/
|
|
|
82 |
final public function export_for_template(renderer_base $output): stdClass {
|
|
|
83 |
$this->update_content();
|
|
|
84 |
if (empty($this->content)) {
|
|
|
85 |
return new stdClass();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$data = (object)[
|
|
|
89 |
'badgecontent' => $this->content,
|
|
|
90 |
'badgestyle' => $this->style,
|
|
|
91 |
];
|
|
|
92 |
|
|
|
93 |
if (!empty($this->url)) {
|
|
|
94 |
$data->badgeurl = $this->url->out();
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (!empty($this->elementid)) {
|
|
|
98 |
$data->badgeelementid = $this->elementid;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
if (!empty($this->extraattributes)) {
|
|
|
102 |
$data->badgeextraattributes = $this->extraattributes;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return $data;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Creates an instance of activityclass for the given course module, in case it implements it.
|
|
|
110 |
*
|
|
|
111 |
* @param cm_info $cminfo
|
|
|
112 |
* @return self|null An instance of activityclass for the given module or null if the module doesn't implement it.
|
|
|
113 |
*/
|
|
|
114 |
final public static function create_instance(cm_info $cminfo): ?self {
|
|
|
115 |
$classname = '\mod_' . $cminfo->modname . '\output\courseformat\activitybadge';
|
|
|
116 |
if (!class_exists($classname)) {
|
|
|
117 |
return null;
|
|
|
118 |
}
|
|
|
119 |
return new $classname($cminfo);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* This method will be called before exporting the template.
|
|
|
124 |
*
|
|
|
125 |
* It should be implemented by any module extending this class and will be in charge of updating any of the class attributes
|
|
|
126 |
* with the proper information that will be displayed in the activity badge (like the content or the badge style).
|
|
|
127 |
*/
|
|
|
128 |
abstract protected function update_content(): void;
|
|
|
129 |
}
|