ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilRatingGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Rating/classes/class.ilRating.php");
5include_once("./Services/Rating/classes/class.ilRatingCategory.php");
6
18{
22 protected $lng;
23
27 protected $ctrl;
28
32 protected $user;
33
34 protected $id = "rtg_";
37 protected $ctrl_path;
38
42 protected $ui;
43
44 public function __construct()
45 {
46 global $DIC;
47
48 $this->lng = $DIC->language();
49 $this->ctrl = $DIC->ctrl();
50 $this->user = $DIC->user();
51 $lng = $DIC->language();
52
53 $this->ui = $DIC->ui();
54
55 $lng->loadLanguageModule("rating");
56 }
57
61 public function executeCommand()
62 {
63 $ilCtrl = $this->ctrl;
64
65 $next_class = $ilCtrl->getNextClass($this);
66 $cmd = $ilCtrl->getCmd();
67
68 switch ($next_class) {
69 case "ilratingcategorygui":
70 include_once("./Services/Rating/classes/class.ilRatingCategoryGUI.php");
71 $gui = new ilRatingCategoryGUI($this->obj_id, $this->export_callback, $this->export_subobj_title);
72 $ilCtrl->forwardCommand($gui);
73 break;
74
75 default:
76 return $this->$cmd();
77 break;
78 }
79 }
80
89 public function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
90 {
92
93 if (!trim($a_sub_obj_type)) {
94 $a_sub_obj_type = "-";
95 }
96
97 $this->obj_id = $a_obj_id;
98 $this->obj_type = $a_obj_type;
99 $this->sub_obj_id = $a_sub_obj_id;
100 $this->sub_obj_type = $a_sub_obj_type;
101 $this->id = "rtg_" . $this->obj_id . "_" . $this->obj_type . "_" . $this->sub_obj_id . "_" .
102 $this->sub_obj_type;
103
104 $this->setUserId($ilUser->getId());
105 }
106
112 public function setUserId($a_userid)
113 {
114 $this->userid = $a_userid;
115 }
116
122 public function getUserId()
123 {
124 return $this->userid;
125 }
126
132 public function setYourRatingText($a_val)
133 {
134 $this->your_rating_text = $a_val;
135 }
136
142 public function getYourRatingText()
143 {
144 return $this->your_rating_text;
145 }
146
152 public function enableCategories($a_value)
153 {
154 $this->enable_categories = (bool) $a_value;
155 }
156
162 public function setCtrlPath(array $a_value)
163 {
164 $this->ctrl_path = $a_value;
165 }
166
177 protected function renderDetails(
178 $a_js_id,
179 $a_may_rate,
180 array $a_categories = null,
181 $a_onclick = null,
182 $a_average = false,
183 bool $add_tooltip = false
184 )
185 {
187 $ilCtrl = $this->ctrl;
188
189 $ttpl = new ilTemplate("tpl.rating_details.html", true, true, "Services/Rating");
190
191 $rate_text = null;
192 if ($this->getYourRatingText() != "#") {
193 $rate_text = ($this->getYourRatingText() != "")
194 ? $this->getYourRatingText()
195 : $lng->txt("rating_your_rating");
196 }
197
198 // no categories: 1 simple rating (link)
199 if (!$a_categories) {
200 if ($a_may_rate) {
202 $this->obj_id,
203 $this->obj_type,
204 $this->sub_obj_id,
205 $this->sub_obj_type,
206 $this->getUserId(),
207 0
208 );
209
210 if ((bool) $a_average) {
211 $overall_rating = ilRating::getOverallRatingForObject(
212 $this->obj_id,
213 $this->obj_type,
214 $this->sub_obj_id,
215 $this->sub_obj_type
216 );
217 }
218
219 // user rating links
220 for ($i = 1; $i <= 5; $i++) {
221 if ((bool) $a_average &&
222 $i == $rating) {
223 $ttpl->setCurrentBlock("rating_mark_simple");
224 $ttpl->setVariable(
225 "SRC_MARK_SIMPLE",
226 ilUtil::getImagePath("icon_rate_marker.svg")
227 );
228 $ttpl->parseCurrentBlock();
229 }
230
231 $ttpl->setCurrentBlock("rating_link_simple");
232 if (stristr($a_onclick, "%rating%")) {
233 $url_save = "#";
234 } else {
235 $ilCtrl->setParameter($this, "rating", $i);
236 if (!$this->ctrl_path) {
237 $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
238 } else {
239 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
240 }
241 }
242 $ttpl->setVariable("HREF_RATING", $url_save);
243
244 if ($a_onclick) {
245 $onclick = str_replace("%rating%", $i, $a_onclick);
246 $ttpl->setVariable("ONCLICK_RATING", ' onclick="' . $onclick . '"');
247 }
248
249 if ((bool) $a_average) {
250 $ref_rating = $overall_rating["avg"];
251 } else {
252 $ref_rating = $rating;
253 }
254
255 if ($ref_rating >= $i) {
256 $ttpl->setVariable(
257 "SRC_ICON",
258 ilUtil::getImagePath("icon_rate_on.svg")
259 );
260 } else {
261 $ttpl->setVariable(
262 "SRC_ICON",
263 ilUtil::getImagePath("icon_rate_off.svg")
264 );
265 }
266 $ttpl->setVariable("ALT_ICON",
267 sprintf($lng->txt("rating_rate_x_of_5"), $i)
268 );
269 $ttpl->parseCurrentBlock();
270 }
271
272 // remove
273 if ($rating) {
274 $ttpl->setCurrentBlock("rating_simple_del_bl");
275 $ttpl->setVariable("CAPTION_RATING_DEL", $lng->txt("rating_remove"));
276
277 if (stristr($a_onclick, "%rating%")) {
278 $url_save = "#";
279 } else {
280 $ilCtrl->setParameter($this, "rating", 0);
281 if (!$this->ctrl_path) {
282 $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
283 } else {
284 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
285 }
286 }
287 $ttpl->setVariable("HREF_RATING_DEL", $url_save);
288
289 if ($a_onclick) {
290 $onclick = str_replace("%rating%", 0, $a_onclick);
291 $ttpl->setVariable("ONCLICK_RATING_DEL", ' onclick="' . $onclick . '"');
292 }
293
294 $ttpl->parseCurrentBlock();
295 }
296
297 if ($rate_text) {
298 $ttpl->setCurrentBlock("rating_simple_title");
299 $ttpl->setVariable("TXT_RATING_SIMPLE", $rate_text);
300 $ttpl->parseCurrentBlock();
301 }
302
303
304 if ((bool) $a_average &&
305 $overall_rating["cnt"]) {
306 $ttpl->setCurrentBlock("number_votes_simple");
307 $ttpl->setVariable("NUMBER_VOTES_SIMPLE", $overall_rating["cnt"]);
308 $ttpl->parseCurrentBlock();
309 }
310
311 if ($add_tooltip) {
312 $unique_id = $this->id . "_block";
313 $ttpl->setVariable("TTID", $unique_id);
314 $this->addTooltip(
315 $unique_id,
316 (int) $overall_rating["cnt"] ?? 0,
317 (float) $overall_rating["avg"] ?? 0,
318 (int) $rating ?? 0
319 );
320 }
321
322 // user rating text
323 $ttpl->setCurrentBlock("user_rating_simple");
324 $ttpl->parseCurrentBlock();
325 }
326 }
327 // categories: overall & user (form)
328 else {
329 $has_user_rating = false;
330 foreach ($a_categories as $category) {
331 $user_rating = round(ilRating::getRatingForUserAndObject(
332 $this->obj_id,
333 $this->obj_type,
334 $this->sub_obj_id,
335 $this->sub_obj_type,
336 $this->getUserId(),
337 $category["id"]
338 ));
339
340 $overall_rating = ilRating::getOverallRatingForObject(
341 $this->obj_id,
342 $this->obj_type,
343 $this->sub_obj_id,
344 $this->sub_obj_type,
345 $category["id"]
346 );
347
348 for ($i = 1; $i <= 5; $i++) {
349 if ($a_may_rate && $i == $user_rating) {
350 $has_user_rating = true;
351
352 $ttpl->setCurrentBlock("rating_mark");
353 $ttpl->setVariable(
354 "SRC_MARK",
355 ilUtil::getImagePath("icon_rate_marker.svg")
356 );
357 $ttpl->parseCurrentBlock();
358 }
359
360 $ttpl->setCurrentBlock("user_rating_icon");
361 if ($overall_rating["avg"] >= $i) {
362 $ttpl->setVariable(
363 "SRC_ICON",
364 ilUtil::getImagePath("icon_rate_on.svg")
365 );
366 } elseif ($overall_rating["avg"] + 1 <= $i) {
367 $ttpl->setVariable(
368 "SRC_ICON",
369 ilUtil::getImagePath("icon_rate_off.svg")
370 );
371 } else {
372 $nr = round(($overall_rating["avg"] + 1 - $i) * 10);
373 $ttpl->setVariable(
374 "SRC_ICON",
375 ilUtil::getImagePath("icon_rate_$nr.svg")
376 );
377 }
378 $ttpl->setVariable(
379 "ALT_ICON",
380 sprintf($lng->txt("rating_rate_x_of_5"), $i)
381 );
382
383 if ($a_may_rate) {
384 $ttpl->setVariable("HREF_RATING", "il.Rating.setValue(" . $category["id"] . "," . $i . ", '" . $a_js_id . "')");
385 $ttpl->setVariable("CATEGORY_ID", $category["id"]);
386 $ttpl->setVariable("ICON_VALUE", $i);
387 $ttpl->setVariable("JS_ID", $a_js_id);
388 $ttpl->setVariable("ICON_MOUSEACTION", " onmouseover=\"il.Rating.toggleIcon(this," . $i . ")\"" .
389 " onmouseout=\"il.Rating.toggleIcon(this," . $i . ",1)\"");
390 }
391
392 $ttpl->parseCurrentBlock();
393 }
394
395 if ($a_may_rate) {
396 $ttpl->setCurrentBlock("user_rating_category_column");
397 $ttpl->setVariable("JS_ID", $a_js_id);
398 $ttpl->setVariable("CATEGORY_ID", $category["id"]);
399 $ttpl->setVariable("CATEGORY_VALUE", $user_rating);
400 if ($add_tooltip) {
401 $unique_id = $this->id . "_block_" . $category["id"];
402 $ttpl->setVariable("CAT_TTID", $unique_id);
403 $this->addTooltip(
404 $unique_id,
405 (int) $overall_rating["cnt"] ?? 0,
406 (float) $overall_rating["avg"] ?? 0,
407 (int) $user_rating ?? 0
408 );
409 }
410 $ttpl->parseCurrentBlock();
411 }
412
413
414 // category title
415 $ttpl->setCurrentBlock("user_rating_category");
416 $ttpl->setVariable("TXT_RATING_CATEGORY", $category["title"]);
417 $ttpl->parseCurrentBlock();
418 }
419
420 if ($overall_rating["cnt"]) {
421 $ttpl->setCurrentBlock("votes_number_bl");
422 $ttpl->setVariable("NUMBER_VOTES", sprintf($lng->txt("rating_number_votes"), $overall_rating["cnt"]));
423 $ttpl->parseCurrentBlock();
424 }
425
426 if ($a_may_rate) {
427 // remove
428 if ($has_user_rating) {
429 $ttpl->setCurrentBlock("user_rating_categories_del_bl");
430 $ttpl->setVariable("CAPTION_RATING_DEL_CAT", $lng->txt("rating_remove"));
431
432 $ilCtrl->setParameter($this, "rating", 0);
433 if (!$this->ctrl_path) {
434 $url_save = $ilCtrl->getLinkTarget($this, "resetUserRating");
435 } else {
436 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "resetUserRating");
437 }
438 $ttpl->setVariable("HREF_RATING_DEL_CAT", $url_save);
439
440 $ttpl->parseCurrentBlock();
441 }
442
443 if (!$this->ctrl_path) {
444 $url_form = $ilCtrl->getFormAction($this, "saveRating");
445 } else {
446 $url_form = $ilCtrl->getFormActionByClass($this->ctrl_path, "saveRating");
447 }
448 $ttpl->setVariable("FORM_ACTION", $url_form);
449 $ttpl->setVariable("TXT_SUBMIT", $lng->txt("rating_overlay_submit"));
450 $ttpl->setVariable("CMD_SUBMIT", "saveRating");
451 $ttpl->touchBlock("user_rating_categories_form_out");
452
453 // overall / user title
454 /*
455 $ttpl->setCurrentBlock("user_rating_categories");
456 $ttpl->setVariable("TXT_RATING_OVERALL", $lng->txt("rating_overlay_title_overall"));
457 $ttpl->parseCurrentBlock();
458 */
459 }
460 }
461
462 return $ttpl->get();
463 }
464
474 public function getHTML($a_show_overall = true, $a_may_rate = true, $a_onclick = null, $a_additional_id = null)
475 {
477 $unique_id = $this->id;
478 if ($a_additional_id) {
479 $unique_id .= "_" . $a_additional_id;
480 }
481
482 $categories = array();
483 if ($this->enable_categories) {
484 $categories = ilRatingCategory::getAllForObject($this->obj_id);
485 }
486
487 $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
488 if ($may_rate && !$a_may_rate) {
489 $may_rate = false;
490 }
491
492 $has_overlay = false;
493 if ($may_rate || $categories) {
494 $has_overlay = true;
495 }
496
497 $ttpl = new ilTemplate("tpl.rating_input.html", true, true, "Services/Rating");
498
499 // user rating
500 $user_rating = 0;
501 if ($may_rate || !$a_show_overall) {
502 $user_rating = round(ilRating::getRatingForUserAndObject(
503 $this->obj_id,
504 $this->obj_type,
505 $this->sub_obj_id,
506 $this->sub_obj_type,
507 $this->getUserId()
508 ));
509 }
510
511 // (1) overall rating
512 if ($a_show_overall) {
514 $this->obj_id,
515 $this->obj_type,
516 $this->sub_obj_id,
517 $this->sub_obj_type
518 );
519 } else {
520 $rating = array("avg" => $user_rating);
521 }
522
523 for ($i = 1; $i <= 5; $i++) {
524 if ($a_show_overall &&
525 $i == $user_rating) {
526 $ttpl->setCurrentBlock("rating_mark");
527 $ttpl->setVariable(
528 "SRC_MARK",
529 ilUtil::getImagePath("icon_rate_marker.svg")
530 );
531 $ttpl->parseCurrentBlock();
532 }
533
534 $ttpl->setCurrentBlock("rating_icon");
535 if ($rating["avg"] >= $i) {
536 $ttpl->setVariable(
537 "SRC_ICON",
538 ilUtil::getImagePath("icon_rate_on.svg")
539 );
540 } elseif ($rating["avg"] + 1 <= $i) {
541 $ttpl->setVariable(
542 "SRC_ICON",
543 ilUtil::getImagePath("icon_rate_off.svg")
544 );
545 } else {
546 $nr = round(($rating["avg"] + 1 - $i) * 10);
547 $ttpl->setVariable(
548 "SRC_ICON",
549 ilUtil::getImagePath("icon_rate_$nr.svg")
550 );
551 }
552 $ttpl->setVariable("ALT_ICON", "");
553 $ttpl->parseCurrentBlock();
554 }
555 $ttpl->setCurrentBlock("rating_icon");
556
557 if ($a_show_overall) {
558 if ($rating["cnt"] > 0) {
559 $ttpl->setCurrentBlock("rat_nr");
560 $ttpl->setVariable("RT_NR", $rating["cnt"]);
561 $ttpl->parseCurrentBlock();
562 }
563 }
564
565 $this->addTooltip(
566 $unique_id . "_tt",
567 (int) $rating["cnt"] ?? 0,
568 (float) $rating["avg"] ?? 0,
569 (int) $user_rating ?? 0
570 );
571
572 // add overlay (trigger)
573 if ($has_overlay) {
574 include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
575 $ov = new ilOverlayGUI($unique_id);
576 $ov->setTrigger("tr_" . $unique_id, "click", "tr_" . $unique_id);
577 $ov->add();
578
579 $ttpl->setCurrentBlock("act_rat_start");
580 $ttpl->setVariable("ID", $unique_id);
581 $ttpl->setVariable("TXT_OPEN_DIALOG", $lng->txt("rating_open_dialog"));
582 $ttpl->parseCurrentBlock();
583
584 $ttpl->touchBlock("act_rat_end");
585 }
586
587 $ttpl->parseCurrentBlock();
588
589
590 // (2) user rating
591
592 if ($has_overlay) {
593 $ttpl->setVariable(
594 "RATING_DETAILS",
595 $this->renderDetails("rtov_", $may_rate, $categories, $a_onclick)
596 );
597
598 $ttpl->setCurrentBlock("user_rating");
599 $ttpl->setVariable("ID", $unique_id);
600 $ttpl->parseCurrentBlock();
601 }
602
603 $ttpl->setVariable("TTID", $unique_id);
604
605 return $ttpl->get();
606 }
607
608 protected function addTooltip(
609 string $id,
610 int $cnt = 0,
611 float $avg = 0,
612 int $user = 0
613 ) : void
614 {
616
617 $tt = "";
618 if ($cnt == 0) {
619 $tt = $lng->txt("rat_not_rated_yet");
620 } else {
621 if ($cnt == 1) {
622 $tt = $lng->txt("rat_one_rating");
623 } else {
624 $tt = sprintf($lng->txt("rat_nr_ratings"), $cnt);
625 }
626 $tt.= "<br>".$lng->txt("rating_avg_rating").": ".round($avg, 1);
627 }
628
629 if ($user > 0) {
630 $tt.= "<br>".$lng->txt("rating_personal_rating").": ".$user;
631 }
632 if ($tt !== "") {
634 $id,
635 $tt,
636 "",
637 "bottom center",
638 "top center",
639 false
640 );
641 }
642 }
643
644 public function getBlockHTML($a_title)
645 {
646 $ui = $this->ui;
647
648 $categories = array();
649 if ($this->enable_categories) {
650 $categories = ilRatingCategory::getAllForObject($this->obj_id);
651 }
652
653 $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
654
655
656 $panel = $ui->factory()->panel()->secondary()->legacy(
657 $a_title,
658 $ui->factory()->legacy(
659 $this->renderDetails("rtsb_", $may_rate, $categories, null, true, true)
660 )
661 );
662
663 return $ui->renderer()->render($panel);
664 }
665
669 public function saveRating()
670 {
671 $ilCtrl = $this->ctrl;
672
673 if (!is_array($_REQUEST["rating"])) {
674 $rating = (int) ilUtil::stripSlashes($_GET["rating"]);
675 if (!$rating) {
676 $this->resetUserRating();
677 } else {
679 $this->obj_id,
680 $this->obj_type,
681 $this->sub_obj_id,
682 $this->sub_obj_type,
683 $this->getUserId(),
684 $rating
685 );
686 }
687 } else {
688 foreach ($_POST["rating"] as $cat_id => $rating) {
690 $this->obj_id,
691 $this->obj_type,
692 $this->sub_obj_id,
693 $this->sub_obj_type,
694 $this->getUserId(),
695 $rating,
696 $cat_id
697 );
698 }
699 }
700
701 if ($this->update_callback) {
702 call_user_func(
703 $this->update_callback,
704 $this->obj_id,
705 $this->obj_type,
706 $this->sub_obj_id,
707 $this->sub_obj_type
708 );
709 }
710
711 if ($ilCtrl->isAsynch()) {
712 exit();
713 }
714 }
715
716 public function setUpdateCallback($a_callback)
717 {
718 $this->update_callback = $a_callback;
719 }
720
724 public function resetUserRating()
725 {
727 $this->obj_id,
728 $this->obj_type,
729 $this->sub_obj_id,
730 $this->sub_obj_type,
731 $this->getUserId()
732 );
733 }
734
735 public function setExportCallback($a_callback, $a_subobj_title)
736 {
737 $this->export_callback = $a_callback;
738 $this->export_subobj_title = $a_subobj_title;
739 }
740
750 public function getListGUIProperty($a_ref_id, $a_may_rate, $a_ajax_hash, $_parent_ref_id)
751 {
752 return $this->getHTML(
753 true,
754 $a_may_rate,
755 "il.Object.saveRatingFromListGUI(" . $a_ref_id . ", '" . $a_ajax_hash . "', %rating%);",
756 $_parent_ref_id
757 );
758 }
759}
user()
Definition: user.php:4
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
This is a utility class for the yui overlays.
Class ilRatingCategoryGUI.
static getAllForObject($a_parent_obj_id)
Get all categories for object.
Class ilRatingGUI.
enableCategories($a_value)
Toggle categories status.
renderDetails( $a_js_id, $a_may_rate, array $a_categories=null, $a_onclick=null, $a_average=false, bool $add_tooltip=false)
Render rating details.
setUserId($a_userid)
Set User ID.
resetUserRating()
Reset Rating.
setCtrlPath(array $a_value)
ilCtrl path
executeCommand()
execute command
getUserId()
Get User ID.
setUpdateCallback($a_callback)
setObject($a_obj_id, $a_obj_type, $a_sub_obj_id=0, $a_sub_obj_type="")
Set Object.
getBlockHTML($a_title)
saveRating()
Save Rating.
setExportCallback($a_callback, $a_subobj_title)
getListGUIProperty($a_ref_id, $a_may_rate, $a_ajax_hash, $_parent_ref_id)
Build list gui property for object.
setYourRatingText($a_val)
Set "Your Rating" text.
getHTML($a_show_overall=true, $a_may_rate=true, $a_onclick=null, $a_additional_id=null)
Get HTML for rating of an object (and a user)
addTooltip(string $id, int $cnt=0, float $avg=0, int $user=0)
getYourRatingText()
Get "Your Rating" text.
static resetRatingForUserAndObject( $a_obj_id, $a_obj_type, $a_sub_obj_id, $a_sub_obj_type, $a_user_id)
Reset rating for a user and an object.
static writeRatingForUserAndObject( $a_obj_id, $a_obj_type, $a_sub_obj_id, $a_sub_obj_type, $a_user_id, $a_rating, $a_category_id=0)
Write rating for a user and an object.
static getRatingForUserAndObject( $a_obj_id, $a_obj_type, $a_sub_obj_id, $a_sub_obj_type, $a_user_id, $a_category_id=null)
Get rating for a user and an object.
static getOverallRatingForObject($a_obj_id, $a_obj_type, $a_sub_obj_id=null, $a_sub_obj_type=null, $a_category_id=null)
Get overall rating for an object.
special template class to simplify handling of ITX/PEAR
static addTooltip( $a_el_id, $a_text, $a_container="", $a_my="bottom center", $a_at="top center", $a_use_htmlspecialchars=true)
Adds a tooltip to an HTML element.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
const ANONYMOUS_USER_ID
Definition: constants.php:25
global $DIC
Definition: goto.php:24
if(isset($_FILES['img_file']) &&is_array($_FILES['img_file'])) $panel
Definition: imgupload.php:138
$ilUser
Definition: imgupload.php:18
exit
Definition: login.php:29
$i
Definition: metadata.php:24
ui()
Definition: ui.php:5