ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
39 public function __construct()
40 {
41 global $DIC;
42
43 $this->lng = $DIC->language();
44 $this->ctrl = $DIC->ctrl();
45 $this->user = $DIC->user();
46 $lng = $DIC->language();
47
48 $lng->loadLanguageModule("rating");
49 }
50
54 public function executeCommand()
55 {
57
58 $next_class = $ilCtrl->getNextClass($this);
59 $cmd = $ilCtrl->getCmd();
60
61 switch ($next_class) {
62 case "ilratingcategorygui":
63 include_once("./Services/Rating/classes/class.ilRatingCategoryGUI.php");
64 $gui = new ilRatingCategoryGUI($this->obj_id, $this->export_callback, $this->export_subobj_title);
65 $ilCtrl->forwardCommand($gui);
66 break;
67
68 default:
69 return $this->$cmd();
70 break;
71 }
72 }
73
82 public function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
83 {
85
86 if (!trim($a_sub_obj_type)) {
87 $a_sub_obj_type = "-";
88 }
89
90 $this->obj_id = $a_obj_id;
91 $this->obj_type = $a_obj_type;
92 $this->sub_obj_id = $a_sub_obj_id;
93 $this->sub_obj_type = $a_sub_obj_type;
94 $this->id = "rtg_" . $this->obj_id . "_" . $this->obj_type . "_" . $this->sub_obj_id . "_" .
95 $this->sub_obj_type;
96
97 $this->setUserId($ilUser->getId());
98 }
99
105 public function setUserId($a_userid)
106 {
107 $this->userid = $a_userid;
108 }
109
115 public function getUserId()
116 {
117 return $this->userid;
118 }
119
125 public function setYourRatingText($a_val)
126 {
127 $this->your_rating_text = $a_val;
128 }
129
135 public function getYourRatingText()
136 {
137 return $this->your_rating_text;
138 }
139
145 public function enableCategories($a_value)
146 {
147 $this->enable_categories = (bool) $a_value;
148 }
149
155 public function setCtrlPath(array $a_value)
156 {
157 $this->ctrl_path = $a_value;
158 }
159
170 protected function renderDetails($a_js_id, $a_may_rate, array $a_categories = null, $a_onclick = null, $a_average = false)
171 {
174
175 $ttpl = new ilTemplate("tpl.rating_details.html", true, true, "Services/Rating");
176
177 $rate_text = null;
178 if ($this->getYourRatingText() != "#") {
179 $rate_text = ($this->getYourRatingText() != "")
180 ? $this->getYourRatingText()
181 : $lng->txt("rating_your_rating");
182 }
183
184 // no categories: 1 simple rating (link)
185 if (!$a_categories) {
186 if ($a_may_rate) {
188 $this->obj_id,
189 $this->obj_type,
190 $this->sub_obj_id,
191 $this->sub_obj_type,
192 $this->getUserId(),
193 0
194 );
195
196 if ((bool) $a_average) {
197 $overall_rating = ilRating::getOverallRatingForObject(
198 $this->obj_id,
199 $this->obj_type,
200 $this->sub_obj_id,
201 $this->sub_obj_type
202 );
203 }
204
205 // user rating links
206 for ($i = 1; $i <= 5; $i++) {
207 if ((bool) $a_average &&
208 $i == $rating) {
209 $ttpl->setCurrentBlock("rating_mark_simple");
210 $ttpl->setVariable(
211 "SRC_MARK_SIMPLE",
212 ilUtil::getImagePath("icon_rate_marker.svg")
213 );
214 $ttpl->parseCurrentBlock();
215 }
216
217 $ttpl->setCurrentBlock("rating_link_simple");
218 if (stristr($a_onclick, "%rating%")) {
219 $url_save = "#";
220 } else {
221 $ilCtrl->setParameter($this, "rating", $i);
222 if (!$this->ctrl_path) {
223 $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
224 } else {
225 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
226 }
227 }
228 $ttpl->setVariable("HREF_RATING", $url_save);
229
230 if ($a_onclick) {
231 $onclick = str_replace("%rating%", $i, $a_onclick);
232 $ttpl->setVariable("ONCLICK_RATING", ' onclick="' . $onclick . '"');
233 }
234
235 if ((bool) $a_average) {
236 $ref_rating = $overall_rating["avg"];
237 } else {
238 $ref_rating = $rating;
239 }
240
241 if ($ref_rating >= $i) {
242 $ttpl->setVariable(
243 "SRC_ICON",
244 ilUtil::getImagePath("icon_rate_on.svg")
245 );
246 } else {
247 $ttpl->setVariable(
248 "SRC_ICON",
249 ilUtil::getImagePath("icon_rate_off.svg")
250 );
251 }
252 $ttpl->setVariable("ALT_ICON", "(" . $i . "/5)");
253 $ttpl->parseCurrentBlock();
254 }
255
256 // remove
257 if ($rating) {
258 $ttpl->setCurrentBlock("rating_simple_del_bl");
259 $ttpl->setVariable("CAPTION_RATING_DEL", $lng->txt("rating_remove"));
260
261 if (stristr($a_onclick, "%rating%")) {
262 $url_save = "#";
263 } else {
264 $ilCtrl->setParameter($this, "rating", 0);
265 if (!$this->ctrl_path) {
266 $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
267 } else {
268 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
269 }
270 }
271 $ttpl->setVariable("HREF_RATING_DEL", $url_save);
272
273 if ($a_onclick) {
274 $onclick = str_replace("%rating%", 0, $a_onclick);
275 $ttpl->setVariable("ONCLICK_RATING_DEL", ' onclick="' . $onclick . '"');
276 }
277
278 $ttpl->parseCurrentBlock();
279 }
280
281 if ($rate_text) {
282 $ttpl->setCurrentBlock("rating_simple_title");
283 $ttpl->setVariable("TXT_RATING_SIMPLE", $rate_text);
284 $ttpl->parseCurrentBlock();
285 }
286
287 // user rating text
288 $ttpl->setCurrentBlock("user_rating_simple");
289
290 if ((bool) $a_average &&
291 $overall_rating["cnt"]) {
292 $ttpl->setVariable("NUMBER_VOTES_SIMPLE", $overall_rating["cnt"]);
293 }
294
295 $ttpl->parseCurrentBlock();
296 }
297 }
298 // categories: overall & user (form)
299 else {
300 $has_user_rating = false;
301 foreach ($a_categories as $category) {
302 $user_rating = round(ilRating::getRatingForUserAndObject(
303 $this->obj_id,
304 $this->obj_type,
305 $this->sub_obj_id,
306 $this->sub_obj_type,
307 $this->getUserId(),
308 $category["id"]
309 ));
310
311 $overall_rating = ilRating::getOverallRatingForObject(
312 $this->obj_id,
313 $this->obj_type,
314 $this->sub_obj_id,
315 $this->sub_obj_type,
316 $category["id"]
317 );
318
319 for ($i = 1; $i <= 5; $i++) {
320 if ($a_may_rate && $i == $user_rating) {
321 $has_user_rating = true;
322
323 $ttpl->setCurrentBlock("rating_mark");
324 $ttpl->setVariable(
325 "SRC_MARK",
326 ilUtil::getImagePath("icon_rate_marker.svg")
327 );
328 $ttpl->parseCurrentBlock();
329 }
330
331 $ttpl->setCurrentBlock("user_rating_icon");
332 if ($overall_rating["avg"] >= $i) {
333 $ttpl->setVariable(
334 "SRC_ICON",
335 ilUtil::getImagePath("icon_rate_on.svg")
336 );
337 } elseif ($overall_rating["avg"] + 1 <= $i) {
338 $ttpl->setVariable(
339 "SRC_ICON",
340 ilUtil::getImagePath("icon_rate_off.svg")
341 );
342 } else {
343 $nr = round(($overall_rating["avg"] + 1 - $i) * 10);
344 $ttpl->setVariable(
345 "SRC_ICON",
346 ilUtil::getImagePath("icon_rate_$nr.svg")
347 );
348 }
349 $ttpl->setVariable("ALT_ICON", "(" . $i . "/5)");
350
351 if ($a_may_rate) {
352 $ttpl->setVariable("HREF_RATING", "il.Rating.setValue(" . $category["id"] . "," . $i . ", '" . $a_js_id . "')");
353 $ttpl->setVariable("CATEGORY_ID", $category["id"]);
354 $ttpl->setVariable("ICON_VALUE", $i);
355 $ttpl->setVariable("JS_ID", $a_js_id);
356 $ttpl->setVariable("ICON_MOUSEACTION", " onmouseover=\"il.Rating.toggleIcon(this," . $i . ")\"" .
357 " onmouseout=\"il.Rating.toggleIcon(this," . $i . ",1)\"");
358 }
359
360 $ttpl->parseCurrentBlock();
361 }
362
363 if ($a_may_rate) {
364 $ttpl->setCurrentBlock("user_rating_category_column");
365 $ttpl->setVariable("JS_ID", $a_js_id);
366 $ttpl->setVariable("CATEGORY_ID", $category["id"]);
367 $ttpl->setVariable("CATEGORY_VALUE", $user_rating);
368 $ttpl->parseCurrentBlock();
369 }
370
371 // category title
372 $ttpl->setCurrentBlock("user_rating_category");
373 $ttpl->setVariable("TXT_RATING_CATEGORY", $category["title"]);
374 $ttpl->parseCurrentBlock();
375 }
376
377 if ($overall_rating["cnt"]) {
378 $ttpl->setCurrentBlock("votes_number_bl");
379 $ttpl->setVariable("NUMBER_VOTES", sprintf($lng->txt("rating_number_votes"), $overall_rating["cnt"]));
380 $ttpl->parseCurrentBlock();
381 }
382
383 if ($a_may_rate) {
384 // remove
385 if ($has_user_rating) {
386 $ttpl->setCurrentBlock("user_rating_categories_del_bl");
387 $ttpl->setVariable("CAPTION_RATING_DEL_CAT", $lng->txt("rating_remove"));
388
389 $ilCtrl->setParameter($this, "rating", 0);
390 if (!$this->ctrl_path) {
391 $url_save = $ilCtrl->getLinkTarget($this, "resetUserRating");
392 } else {
393 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "resetUserRating");
394 }
395 $ttpl->setVariable("HREF_RATING_DEL_CAT", $url_save);
396
397 $ttpl->parseCurrentBlock();
398 }
399
400 if (!$this->ctrl_path) {
401 $url_form = $ilCtrl->getFormAction($this, "saveRating");
402 } else {
403 $url_form = $ilCtrl->getFormActionByClass($this->ctrl_path, "saveRating");
404 }
405 $ttpl->setVariable("FORM_ACTION", $url_form);
406 $ttpl->setVariable("TXT_SUBMIT", $lng->txt("rating_overlay_submit"));
407 $ttpl->setVariable("CMD_SUBMIT", "saveRating");
408 $ttpl->touchBlock("user_rating_categories_form_out");
409
410 // overall / user title
411 /*
412 $ttpl->setCurrentBlock("user_rating_categories");
413 $ttpl->setVariable("TXT_RATING_OVERALL", $lng->txt("rating_overlay_title_overall"));
414 $ttpl->parseCurrentBlock();
415 */
416 }
417 }
418
419 return $ttpl->get();
420 }
421
431 public function getHTML($a_show_overall = true, $a_may_rate = true, $a_onclick = null, $a_additional_id = null)
432 {
434
435 $unique_id = $this->id;
436 if ($a_additional_id) {
437 $unique_id .= "_" . $a_additional_id;
438 }
439
440 $categories = array();
441 if ($this->enable_categories) {
442 $categories = ilRatingCategory::getAllForObject($this->obj_id);
443 }
444
445 $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
446 if ($may_rate && !$a_may_rate) {
447 $may_rate = false;
448 }
449
450 $has_overlay = false;
451 if ($may_rate || $categories) {
452 $has_overlay = true;
453 }
454
455 $ttpl = new ilTemplate("tpl.rating_input.html", true, true, "Services/Rating");
456
457 // user rating
458 $user_rating = 0;
459 if ($may_rate || !$a_show_overall) {
460 $user_rating = round(ilRating::getRatingForUserAndObject(
461 $this->obj_id,
462 $this->obj_type,
463 $this->sub_obj_id,
464 $this->sub_obj_type,
465 $this->getUserId()
466 ));
467 }
468
469 // (1) overall rating
470 if ($a_show_overall) {
472 $this->obj_id,
473 $this->obj_type,
474 $this->sub_obj_id,
475 $this->sub_obj_type
476 );
477 } else {
478 $rating = array("avg" => $user_rating);
479 }
480
481 for ($i = 1; $i <= 5; $i++) {
482 if ($a_show_overall &&
483 $i == $user_rating) {
484 $ttpl->setCurrentBlock("rating_mark");
485 $ttpl->setVariable(
486 "SRC_MARK",
487 ilUtil::getImagePath("icon_rate_marker.svg")
488 );
489 $ttpl->parseCurrentBlock();
490 }
491
492 $ttpl->setCurrentBlock("rating_icon");
493 if ($rating["avg"] >= $i) {
494 $ttpl->setVariable(
495 "SRC_ICON",
496 ilUtil::getImagePath("icon_rate_on.svg")
497 );
498 } elseif ($rating["avg"] + 1 <= $i) {
499 $ttpl->setVariable(
500 "SRC_ICON",
501 ilUtil::getImagePath("icon_rate_off.svg")
502 );
503 } else {
504 $nr = round(($rating["avg"] + 1 - $i) * 10);
505 $ttpl->setVariable(
506 "SRC_ICON",
507 ilUtil::getImagePath("icon_rate_$nr.svg")
508 );
509 }
510 $ttpl->setVariable("ALT_ICON", "(" . $i . "/5)");
511 $ttpl->parseCurrentBlock();
512 }
513 $ttpl->setCurrentBlock("rating_icon");
514
515 if ($a_show_overall) {
516 if ($rating["cnt"] == 0) {
517 $tt = $lng->txt("rat_not_rated_yet");
518 } elseif ($rating["cnt"] == 1) {
519 $tt = $lng->txt("rat_one_rating");
520 } else {
521 $tt = sprintf($lng->txt("rat_nr_ratings"), $rating["cnt"]);
522 }
523 include_once("./Services/UIComponent/Tooltip/classes/class.ilTooltipGUI.php");
524 ilTooltipGUI::addTooltip($unique_id . "_tt", $tt);
525
526 if ($rating["cnt"] > 0) {
527 $ttpl->setCurrentBlock("rat_nr");
528 $ttpl->setVariable("RT_NR", $rating["cnt"]);
529 $ttpl->parseCurrentBlock();
530 }
531 }
532
533 // add overlay (trigger)
534 if ($has_overlay) {
535 include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
536 $ov = new ilOverlayGUI($unique_id);
537 $ov->setTrigger("tr_" . $unique_id, "click", "tr_" . $unique_id);
538 $ov->add();
539
540 $ttpl->setCurrentBlock("act_rat_start");
541 $ttpl->setVariable("ID", $unique_id);
542 $ttpl->parseCurrentBlock();
543
544 $ttpl->touchBlock("act_rat_end");
545 }
546
547 $ttpl->parseCurrentBlock();
548
549
550 // (2) user rating
551
552 if ($has_overlay) {
553 $ttpl->setVariable(
554 "RATING_DETAILS",
555 $this->renderDetails("rtov_", $may_rate, $categories, $a_onclick)
556 );
557
558 $ttpl->setCurrentBlock("user_rating");
559 $ttpl->setVariable("ID", $unique_id);
560 $ttpl->parseCurrentBlock();
561 }
562
563 $ttpl->setVariable("TTID", $unique_id);
564
565 return $ttpl->get();
566 }
567
568 public function getBlockHTML($a_title)
569 {
570 $categories = array();
571 if ($this->enable_categories) {
572 $categories = ilRatingCategory::getAllForObject($this->obj_id);
573 }
574
575 $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
576
577 $ttpl = new ilTemplate("tpl.rating_block.html", true, true, "Services/Rating");
578
579 $ttpl->setVariable("TITLE", $a_title);
580
581 $ttpl->setVariable(
582 "RATING_DETAILS",
583 $this->renderDetails("rtsb_", $may_rate, $categories, null, true)
584 );
585
586 return $ttpl->get();
587 }
588
592 public function saveRating()
593 {
595
596 if (!is_array($_REQUEST["rating"])) {
597 $rating = (int) ilUtil::stripSlashes($_GET["rating"]);
598 if (!$rating) {
599 $this->resetUserRating();
600 } else {
602 $this->obj_id,
603 $this->obj_type,
604 $this->sub_obj_id,
605 $this->sub_obj_type,
606 $this->getUserId(),
607 $rating
608 );
609 }
610 } else {
611 foreach ($_POST["rating"] as $cat_id => $rating) {
613 $this->obj_id,
614 $this->obj_type,
615 $this->sub_obj_id,
616 $this->sub_obj_type,
617 $this->getUserId(),
618 $rating,
619 $cat_id
620 );
621 }
622 }
623
624 if ($this->update_callback) {
625 call_user_func(
626 $this->update_callback,
627 $this->obj_id,
628 $this->obj_type,
629 $this->sub_obj_id,
630 $this->sub_obj_type
631 );
632 }
633
634 if ($ilCtrl->isAsynch()) {
635 exit();
636 }
637 }
638
639 public function setUpdateCallback($a_callback)
640 {
641 $this->update_callback = $a_callback;
642 }
643
647 public function resetUserRating()
648 {
650 $this->obj_id,
651 $this->obj_type,
652 $this->sub_obj_id,
653 $this->sub_obj_type,
654 $this->getUserId()
655 );
656 }
657
658 public function setExportCallback($a_callback, $a_subobj_title)
659 {
660 $this->export_callback = $a_callback;
661 $this->export_subobj_title = $a_subobj_title;
662 }
663
673 public function getListGUIProperty($a_ref_id, $a_may_rate, $a_ajax_hash, $_parent_ref_id)
674 {
675 return $this->getHTML(
676 true,
677 $a_may_rate,
678 "il.Object.saveRatingFromListGUI(" . $a_ref_id . ", '" . $a_ajax_hash . "', %rating%);",
679 $_parent_ref_id
680 );
681 }
682}
user()
Definition: user.php:4
exit
Definition: backend.php:16
$_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)
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)
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)
if(empty($userids)) $userid
$i
Definition: disco.tpl.php:19
global $ilCtrl
Definition: ilias.php:18
global $DIC
Definition: saml.php:7
$ilUser
Definition: imgupload.php:18