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;
|
|
|
18 |
|
|
|
19 |
use pix_emoticon;
|
|
|
20 |
use stdClass;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Provides core support for plugins that have to deal with emoticons (like HTML editor or emoticon filter).
|
|
|
24 |
*
|
|
|
25 |
* Whenever this manager mentiones 'emoticon object', the following data structure is expected:
|
|
|
26 |
* stdClass with properties text, imagename, imagecomponent, altidentifier and altcomponent
|
|
|
27 |
*
|
|
|
28 |
* @see \admin_setting_emoticons
|
|
|
29 |
*
|
|
|
30 |
* @package core
|
|
|
31 |
* @copyright 2010 David Mudrak
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class emoticon_manager {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Returns the currently enabled emoticons
|
|
|
38 |
*
|
|
|
39 |
* @param bool $selectable If true, only return emoticons that should be selectable from a list
|
|
|
40 |
* @return stdClass[] array of emoticon objects
|
|
|
41 |
*/
|
|
|
42 |
public function get_emoticons(
|
|
|
43 |
bool $selectable = false,
|
|
|
44 |
): array {
|
|
|
45 |
global $CFG;
|
|
|
46 |
|
|
|
47 |
$notselectable = ['martin', 'egg'];
|
|
|
48 |
|
|
|
49 |
if (empty($CFG->emoticons)) {
|
|
|
50 |
return [];
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$emoticons = $this->decode_stored_config($CFG->emoticons);
|
|
|
54 |
|
|
|
55 |
if (!is_array($emoticons)) {
|
|
|
56 |
// Something is wrong with the format of stored setting.
|
|
|
57 |
debugging('Invalid format of emoticons setting, please resave the emoticons settings form', DEBUG_NORMAL);
|
|
|
58 |
return [];
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
if ($selectable) {
|
|
|
62 |
foreach ($emoticons as $index => $emote) {
|
|
|
63 |
if (in_array($emote->altidentifier, $notselectable)) {
|
|
|
64 |
// Skip this one.
|
|
|
65 |
unset($emoticons[$index]);
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return $emoticons;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Converts emoticon object into renderable pix_emoticon object
|
|
|
75 |
*
|
|
|
76 |
* @param stdClass $emoticon emoticon object
|
|
|
77 |
* @param array $attributes explicit HTML attributes to set
|
|
|
78 |
* @return pix_emoticon
|
|
|
79 |
*/
|
|
|
80 |
public function prepare_renderable_emoticon(
|
|
|
81 |
stdClass $emoticon,
|
|
|
82 |
array $attributes = [],
|
|
|
83 |
): pix_emoticon {
|
|
|
84 |
$stringmanager = get_string_manager();
|
|
|
85 |
if ($stringmanager->string_exists($emoticon->altidentifier, $emoticon->altcomponent)) {
|
|
|
86 |
$alt = get_string($emoticon->altidentifier, $emoticon->altcomponent);
|
|
|
87 |
} else {
|
|
|
88 |
$alt = s($emoticon->text);
|
|
|
89 |
}
|
|
|
90 |
return new pix_emoticon($emoticon->imagename, $alt, $emoticon->imagecomponent, $attributes);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Encodes the array of emoticon objects into a string storable in config table
|
|
|
95 |
*
|
|
|
96 |
* @param stdClass[] $emoticons array of emoticon objects
|
|
|
97 |
* @return string
|
|
|
98 |
*/
|
|
|
99 |
public function encode_stored_config(
|
|
|
100 |
array $emoticons,
|
|
|
101 |
): string {
|
|
|
102 |
return json_encode($emoticons);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Decodes the string into an array of emoticon objects
|
|
|
107 |
*
|
|
|
108 |
* @param string $encoded
|
|
|
109 |
* @return ?array
|
|
|
110 |
*/
|
|
|
111 |
public function decode_stored_config(
|
|
|
112 |
string $encoded,
|
|
|
113 |
): ?array {
|
|
|
114 |
$decoded = json_decode($encoded);
|
|
|
115 |
if (!is_array($decoded)) {
|
|
|
116 |
return null;
|
|
|
117 |
}
|
|
|
118 |
return $decoded;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Returns default set of emoticons supported by Moodle
|
|
|
123 |
*
|
|
|
124 |
* @return stdClass[] array of emoticon objects
|
|
|
125 |
*/
|
|
|
126 |
public function default_emoticons(): array {
|
|
|
127 |
return [
|
|
|
128 |
$this->prepare_emoticon_object(":-)", 's/smiley', 'smiley'),
|
|
|
129 |
$this->prepare_emoticon_object(":)", 's/smiley', 'smiley'),
|
|
|
130 |
$this->prepare_emoticon_object(":-D", 's/biggrin', 'biggrin'),
|
|
|
131 |
$this->prepare_emoticon_object(";-)", 's/wink', 'wink'),
|
|
|
132 |
$this->prepare_emoticon_object(":-/", 's/mixed', 'mixed'),
|
|
|
133 |
$this->prepare_emoticon_object("V-.", 's/thoughtful', 'thoughtful'),
|
|
|
134 |
$this->prepare_emoticon_object(":-P", 's/tongueout', 'tongueout'),
|
|
|
135 |
$this->prepare_emoticon_object(":-p", 's/tongueout', 'tongueout'),
|
|
|
136 |
$this->prepare_emoticon_object("B-)", 's/cool', 'cool'),
|
|
|
137 |
$this->prepare_emoticon_object("^-)", 's/approve', 'approve'),
|
|
|
138 |
$this->prepare_emoticon_object("8-)", 's/wideeyes', 'wideeyes'),
|
|
|
139 |
$this->prepare_emoticon_object(":o)", 's/clown', 'clown'),
|
|
|
140 |
$this->prepare_emoticon_object(":-(", 's/sad', 'sad'),
|
|
|
141 |
$this->prepare_emoticon_object(":(", 's/sad', 'sad'),
|
|
|
142 |
$this->prepare_emoticon_object("8-.", 's/shy', 'shy'),
|
|
|
143 |
$this->prepare_emoticon_object(":-I", 's/blush', 'blush'),
|
|
|
144 |
$this->prepare_emoticon_object(":-X", 's/kiss', 'kiss'),
|
|
|
145 |
$this->prepare_emoticon_object("8-o", 's/surprise', 'surprise'),
|
|
|
146 |
$this->prepare_emoticon_object("P-|", 's/blackeye', 'blackeye'),
|
|
|
147 |
$this->prepare_emoticon_object("8-[", 's/angry', 'angry'),
|
|
|
148 |
$this->prepare_emoticon_object("(grr)", 's/angry', 'angry'),
|
|
|
149 |
$this->prepare_emoticon_object("xx-P", 's/dead', 'dead'),
|
|
|
150 |
$this->prepare_emoticon_object("|-.", 's/sleepy', 'sleepy'),
|
|
|
151 |
$this->prepare_emoticon_object("}-]", 's/evil', 'evil'),
|
|
|
152 |
$this->prepare_emoticon_object("(h)", 's/heart', 'heart'),
|
|
|
153 |
$this->prepare_emoticon_object("(heart)", 's/heart', 'heart'),
|
|
|
154 |
$this->prepare_emoticon_object("(y)", 's/yes', 'yes', 'core'),
|
|
|
155 |
$this->prepare_emoticon_object("(n)", 's/no', 'no', 'core'),
|
|
|
156 |
$this->prepare_emoticon_object("(martin)", 's/martin', 'martin'),
|
|
|
157 |
$this->prepare_emoticon_object("( )", 's/egg', 'egg'),
|
|
|
158 |
];
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Helper method preparing an emoticon object
|
|
|
163 |
*
|
|
|
164 |
* @param string|string[] $text
|
|
|
165 |
* @param string $imagename to be used by {@see pix_emoticon}
|
|
|
166 |
* @param ?string $altidentifier alternative string identifier, null for no alt
|
|
|
167 |
* @param string $altcomponent where the alternative string is defined
|
|
|
168 |
* @param string $imagecomponent to be used by {@see pix_emoticon}
|
|
|
169 |
* @return stdClass
|
|
|
170 |
*/
|
|
|
171 |
protected function prepare_emoticon_object(
|
|
|
172 |
string|array $text,
|
|
|
173 |
string $imagename,
|
|
|
174 |
?string $altidentifier = null,
|
|
|
175 |
string $altcomponent = 'core_pix',
|
|
|
176 |
string $imagecomponent = 'core',
|
|
|
177 |
): stdClass {
|
|
|
178 |
return (object) [
|
|
|
179 |
'text' => $text,
|
|
|
180 |
'imagename' => $imagename,
|
|
|
181 |
'altidentifier' => $altidentifier,
|
|
|
182 |
'altcomponent' => $altcomponent,
|
|
|
183 |
'imagecomponent' => $imagecomponent,
|
|
|
184 |
];
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// Alias this class to the old name.
|
|
|
189 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
190 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
191 |
class_alias(emoticon_manager::class, \emoticon_manager::class);
|