2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Contains event class for displaying a calendar event's icon.
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_calendar\external
;
27 defined('MOODLE_INTERNAL') ||
die();
29 use \core\external\exporter
;
30 use \core_calendar\local\event\entities\event_interface
;
33 * Class for displaying a calendar event's icon.
35 * @package core_calendar
36 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class event_icon_exporter
extends exporter
{
44 * @param event_interface $event
45 * @param array $related The related data.
47 public function __construct(event_interface
$event, $related = []) {
49 $coursemodule = $event->get_course_module();
50 $category = $event->get_category();
51 $categoryid = $category ?
$category->get('id') : null;
52 $course = $event->get_course();
53 $courseid = $course ?
$course->get('id') : null;
54 $group = $event->get_group();
55 $groupid = $group ?
$group->get('id') : null;
56 $user = $event->get_user();
57 $userid = $user ?
$user->get('id') : null;
58 $isactivityevent = !empty($coursemodule);
59 $issiteevent = ($course && $courseid == SITEID
);
60 $iscategoryevent = ($category && !empty($categoryid));
61 $iscourseevent = ($course && !empty($courseid) && $courseid != SITEID
&& empty($groupid));
62 $isgroupevent = ($group && !empty($groupid));
63 $isuserevent = ($user && !empty($userid));
67 if ($isactivityevent) {
69 $component = $coursemodule->get('modname');
71 $iconurl = get_fast_modinfo($courseid)->get_cm($coursemodule->get('id'))->get_icon_url();
72 $iconclass = $iconurl->get_param('filtericon') ?
'' : 'nofilter';
73 $iconurl = $iconurl->out(false);
74 if (get_string_manager()->string_exists($event->get_type(), $component)) {
75 $alttext = get_string($event->get_type(), $component);
77 $alttext = get_string('activityevent', 'calendar');
79 } else if ($event->get_component()) {
80 // Guess the icon and the title for the component event. By default display calendar icon and the
81 // plugin name as the alttext.
82 if ($PAGE->theme
->resolve_image_location($event->get_type(), $event->get_component())) {
83 $key = $event->get_type();
84 $component = $event->get_component();
86 $key = 'i/otherevent';
90 if (get_string_manager()->string_exists($event->get_type(), $event->get_component())) {
91 $alttext = get_string($event->get_type(), $event->get_component());
93 $alttext = get_string('pluginname', $event->get_component());
95 } else if ($issiteevent) {
98 $alttext = get_string('typesite', 'calendar');
99 } else if ($iscategoryevent) {
100 $key = 'i/categoryevent';
102 $alttext = get_string('typecategory', 'calendar');
103 } else if ($iscourseevent) {
104 $key = 'i/courseevent';
106 $alttext = get_string('typecourse', 'calendar');
107 } else if ($isgroupevent) {
108 $key = 'i/groupevent';
110 $alttext = get_string('typegroup', 'calendar');
111 } else if ($isuserevent) {
112 $key = 'i/userevent';
114 $alttext = get_string('typeuser', 'calendar');
116 // Default to site event icon?
117 $key = 'i/siteevent';
119 $alttext = get_string('typesite', 'calendar');
122 $data = new \
stdClass();
124 $data->component
= $component;
125 $data->alttext
= $alttext;
126 $data->iconurl
= $iconurl;
127 $data->iconclass
= $iconclass;
129 parent
::__construct($data, $related);
133 * Return the list of properties.
137 protected static function define_properties() {
139 'key' => ['type' => PARAM_TEXT
],
140 'component' => ['type' => PARAM_TEXT
],
141 'alttext' => ['type' => PARAM_TEXT
],
142 'iconurl' => ['type' => PARAM_TEXT
],
143 'iconclass' => ['type' => PARAM_TEXT
],
148 * Returns a list of objects that are related.
152 protected static function define_related() {
154 'context' => 'context',