ILIAS  release_8 Revision v8.24
class.ilLikeGUI.php
Go to the documentation of this file.
1<?php
2
20
26{
27 protected ilLikeData $data;
28 protected \ILIAS\DI\UIServices $ui;
31 protected ilLanguage $lng;
32 protected ilCtrl $ctrl;
33 protected ilObjUser $user;
34 protected int $obj_id;
35 protected string $obj_type;
36 protected int $sub_obj_id;
37 protected string $sub_obj_type;
38 protected int $news_id;
39 protected string $dom_id;
40
41 public function __construct(
43 ?\ilTemplate $main_tpl = null
44 ) {
45 global $DIC;
46
47 $this->main_tpl = ($main_tpl == null)
48 ? $DIC->ui()->mainTemplate()
49 : $main_tpl;
50
51 $this->lng = $DIC->language();
52 $this->ctrl = $DIC->ctrl();
53 $this->user = $DIC->user();
54 $this->ui = $DIC->ui();
55
56 $this->data = $data;
57
58 $this->lng->loadLanguageModule("like");
59 $this->request = new StandardGUIRequest(
60 $DIC->http(),
61 $DIC->refinery()
62 );
63
64 $this->initJavascript();
65 }
66
67 protected function initJavascript(): void
68 {
69 $this->main_tpl->addJavaScript("./Services/Like/js/Like.js");
70 }
71
72 public function setObject(
73 int $a_obj_id,
74 string $a_obj_type,
75 int $a_sub_obj_id = 0,
76 string $a_sub_obj_type = "",
77 int $a_news_id = 0
78 ): void {
79 $this->obj_id = $a_obj_id;
80 $this->obj_type = $a_obj_type;
81 $this->sub_obj_id = $a_sub_obj_id;
82 $this->sub_obj_type = $a_sub_obj_type;
83 $this->news_id = $a_news_id;
84 $this->dom_id = "like_" . $this->obj_id . "_" . $this->obj_type . "_" . $this->sub_obj_id . "_" .
85 $this->sub_obj_type . "_" . $this->news_id;
86 }
87
88 public function executeCommand(): string
89 {
90 $ilCtrl = $this->ctrl;
91
92 $next_class = $ilCtrl->getNextClass($this);
93 $cmd = $ilCtrl->getCmd("getHTML");
94
95 switch ($next_class) {
96 default:
97 if (in_array($cmd, array("getHTML", "renderEmoticons", "renderModal", "saveExpression"))) {
98 return $this->$cmd();
99 }
100 break;
101 }
102 return "";
103 }
104
105 public function getHTML(): string
106 {
107 $f = $this->ui->factory();
108 $r = $this->ui->renderer();
109 $ctrl = $this->ctrl;
111
112 $tpl = new ilTemplate("tpl.like.html", true, true, "Services/Like");
113
114 // modal
115 $modal_asyn_url = $ctrl->getLinkTarget($this, "renderModal", "", true, false);
116 $modal = $f->modal()->roundtrip('', $f->legacy(""))
117 ->withAsyncRenderUrl($modal_asyn_url);
118
119 $modal_show_sig_id = $modal->getShowSignal()->getId();
120 $this->ctrl->setParameter($this, "modal_show_sig_id", $modal_show_sig_id);
121 $emo_counters = $this->renderEmoCounters($modal->getShowSignal());
122 $tpl->setVariable("EMO_COUNTERS", $emo_counters . $r->render($modal));
123
124
125
126 // emoticon popover
127 $popover = $f->popover()->standard($f->legacy(''))->withTitle('');
128 $ctrl->setParameter($this, "repl_sig", $popover->getReplaceContentSignal()->getId());
129 $asyn_url = $ctrl->getLinkTarget($this, "renderEmoticons", "", true, false);
130 $popover = $popover->withAsyncContentUrl($asyn_url);
131 $button = $f->button()->shy($lng->txt("like"), '#')
132 ->withOnClick($popover->getShowSignal());
133
134 $tpl->setVariable("LIKE", $r->render([$popover, $button]));
135
136 return $tpl->get();
137 }
138
143 protected function renderEmoCounters(
144 ILIAS\UI\Component\Signal $modal_signal = null,
145 bool $unavailable = false
146 ): string {
147 $ilCtrl = $this->ctrl;
148
149 $tpl = new ilTemplate("tpl.emo_counters.html", true, true, "Services/Like");
150 $f = $this->ui->factory();
151 $r = $this->ui->renderer();
152
153 $cnts = $this->data->getExpressionCounts(
154 $this->obj_id,
155 $this->obj_type,
156 $this->sub_obj_id,
157 $this->sub_obj_type,
158 $this->news_id
159 );
160 $comps = array();
161 foreach ($this->data->getExpressionTypes() as $k => $txt) {
162 if ($cnts[$k] > 0) {
163 $glyph = $this->getGlyphForConst($k, $unavailable);
164 if ($modal_signal !== null) {
165 $glyph = $glyph->withOnClick($modal_signal);
166 }
167 $comps[] = $glyph->withCounter($f->counter()->status($cnts[$k]));
168 }
169 }
170
171 if ($ilCtrl->isAsynch()) {
172 $tpl->setVariable("MODAL_TRIGGER", $r->renderAsync($comps));
173 } else {
174 $tpl->setVariable("MODAL_TRIGGER", $r->render($comps));
175 }
176 if ($modal_signal !== null) {
177 $tpl->setVariable("ID", $this->dom_id);
178 }
179
180 if (count($comps) > 0 && $modal_signal !== null) {
181 $tpl->setVariable("SEP", $r->render($f->divider()->vertical()));
182 }
183
184 return $tpl->get();
185 }
186
187 protected function getGlyphForConst(
188 int $a_const,
189 bool $unavailable = false
190 ): ?\ILIAS\UI\Component\Symbol\Glyph\Glyph {
191 $f = $this->ui->factory();
192 $like = null;
193 switch ($a_const) {
194 case ilLikeData::TYPE_LIKE: $like = $f->symbol()->glyph()->like(); break;
195 case ilLikeData::TYPE_DISLIKE: $like = $f->symbol()->glyph()->dislike(); break;
196 case ilLikeData::TYPE_LOVE: $like = $f->symbol()->glyph()->love(); break;
197 case ilLikeData::TYPE_LAUGH: $like = $f->symbol()->glyph()->laugh(); break;
198 case ilLikeData::TYPE_ASTOUNDED: $like = $f->symbol()->glyph()->astounded(); break;
199 case ilLikeData::TYPE_SAD: $like = $f->symbol()->glyph()->sad(); break;
200 case ilLikeData::TYPE_ANGRY: $like = $f->symbol()->glyph()->angry(); break;
201 }
202 if ($unavailable) {
203 $like = $like->withUnavailableAction();
204 }
205 return $like;
206 }
207
211 public function renderEmoticons(): void
212 {
213 $ilCtrl = $this->ctrl;
214 $r = $this->ui->renderer();
215 $glyphs = [];
216
217 $ilCtrl->saveParameter($this, "modal_show_sig_id");
218
219 $tpl = new ilTemplate("tpl.emoticons.html", true, true, "Services/Like");
220 $tpl->setVariable("ID", $this->dom_id);
221
222 $url = $ilCtrl->getLinkTarget($this, "", "", true);
223 foreach ($this->data->getExpressionTypes() as $k => $txt) {
224 $g = $this->getGlyphForConst($k);
225
226 if ($this->data->isExpressionSet(
227 $this->user->getId(),
228 $k,
229 $this->obj_id,
230 $this->obj_type,
231 $this->sub_obj_id,
232 $this->sub_obj_type,
233 $this->news_id
234 )) {
235 $g = $g->withHighlight();
236 }
237
238 $g = $g->withAdditionalOnLoadCode(function ($id) use ($k, $url) {
239 return
240 "$('#" . $id . "').click(function() { il.Like.toggle('" . $url . "','" . $id . "','" . $this->dom_id . "'," . $k . ");});";
241 });
242 $glyphs[] = $g;
243 }
244
245 $tpl->setVariable("GLYPHS", $r->renderAsync($glyphs));
246
247 echo $tpl->get();
248 exit;
249 }
250
255 protected function saveExpression(): void
256 {
257 $exp_key = $this->request->getExpressionKey();
258 $exp_val = $this->request->getValue();
259 $modal_show_sig_id = $this->request->getModalSignalId();
260 $show_signal = new \ILIAS\UI\Implementation\Component\Signal($modal_show_sig_id);
261
262 if ($exp_val) {
263 $this->data->addExpression(
264 $this->user->getId(),
265 $exp_key,
266 $this->obj_id,
267 $this->obj_type,
268 $this->sub_obj_id,
269 $this->sub_obj_type,
270 $this->news_id
271 );
272 } else {
273 $this->data->removeExpression(
274 $this->user->getId(),
275 $exp_key,
276 $this->obj_id,
277 $this->obj_type,
278 $this->sub_obj_id,
279 $this->sub_obj_type,
280 $this->news_id
281 );
282 }
283 echo $this->renderEmoCounters($show_signal);
284 exit;
285 }
286
287
294 public function renderModal(): void
295 {
296 $user = $this->user;
297
298 $f = $this->ui->factory();
299 $r = $this->ui->renderer();
300
301
302 $list_items = [];
303
304 $glyph_renderings = [];
305 foreach ($this->data->getExpressionEntries(
306 $this->obj_id,
307 $this->obj_type,
308 $this->sub_obj_id,
309 $this->sub_obj_type,
310 $this->news_id
311 ) as $exp) {
312 $name = ilUserUtil::getNamePresentation($exp["user_id"]);
313
314 $image = $f->image()->responsive(
315 ilObjUser::_getPersonalPicturePath($exp["user_id"]),
316 $name
317 );
318
319 $g = $this->getGlyphForConst($exp["expression"], true);
320 $placeholder = "###" . $exp["expression"] . "###";
321 $glyph_renderings[$placeholder] = $r->render($g);
322
323 $list_items[] = $f->item()->standard($name)
324 ->withDescription($placeholder . " " .
326 ->withLeadImage($image);
327 }
328
329 $std_list = $f->panel()->listing()->standard("", array(
330 $f->item()->group("", $list_items)
331 ));
332
333 $header = $f->legacy($this->renderEmoCounters(null, true));
334 //$header = $f->legacy("---");
335
336 $modal = $f->modal()->roundtrip('', [$header, $std_list]);
337 $html = $r->render($modal);
338 foreach ($glyph_renderings as $pl => $gl) {
339 $html = str_replace($pl, $gl, $html);
340 }
341 echo $html;
342 exit;
343 }
344
348 public static function getExpressionText(
349 int $a_const
350 ): string {
351 global $DIC;
352
353 $lng = $DIC->language();
354
355 switch ($a_const) {
356 case ilLikeData::TYPE_LIKE: return $lng->txt("like");
357 case ilLikeData::TYPE_DISLIKE: return $lng->txt("dislike");
358 case ilLikeData::TYPE_LOVE: return $lng->txt("love");
359 case ilLikeData::TYPE_LAUGH: return $lng->txt("laugh");
360 case ilLikeData::TYPE_ASTOUNDED: return $lng->txt("astounded");
361 case ilLikeData::TYPE_SAD: return $lng->txt("sad");
362 case ilLikeData::TYPE_ANGRY: return $lng->txt("angry");
363 }
364 return "";
365 }
366}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_DATETIME
Class ilCtrl provides processing control methods.
setParameter(object $a_gui_obj, string $a_parameter, $a_value)
@inheritDoc
getLinkTarget(object $a_gui_obj, string $a_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@classDescription Date and time handling
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const TYPE_ASTOUNDED
User interface for like feature.
renderEmoCounters(ILIAS\UI\Component\Signal $modal_signal=null, bool $unavailable=false)
Render emo counters.
ilLikeData $data
ilObjUser $user
StandardGUIRequest $request
saveExpression()
Save expression (asynch)
setObject(int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
string $sub_obj_type
string $dom_id
getGlyphForConst(int $a_const, bool $unavailable=false)
static getExpressionText(int $a_const)
Get expression text for const.
ilGlobalTemplateInterface $main_tpl
renderModal()
Render modal (asynch)
__construct(\ilLikeData $data, ?\ilTemplate $main_tpl=null)
ilLanguage $lng
string $obj_type
ILIAS DI UIServices $ui
renderEmoticons()
Render emoticons (asynch)
User class.
static _getPersonalPicturePath(int $a_usr_id, string $a_size="small", bool $a_force_pic=false, bool $a_prevent_no_photo_image=false, bool $html_export=false)
special template class to simplify handling of ITX/PEAR
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link="", bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
$txt
Definition: error.php:13
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
exit
Definition: login.php:28
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.
$url
$lng