ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 {
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($a_js_id, $a_may_rate, array $a_categories = null, $a_onclick = null, $a_average = false)
178 {
181
182 $ttpl = new ilTemplate("tpl.rating_details.html", true, true, "Services/Rating");
183
184 $rate_text = null;
185 if ($this->getYourRatingText() != "#") {
186 $rate_text = ($this->getYourRatingText() != "")
187 ? $this->getYourRatingText()
188 : $lng->txt("rating_your_rating");
189 }
190
191 // no categories: 1 simple rating (link)
192 if (!$a_categories) {
193 if ($a_may_rate) {
195 $this->obj_id,
196 $this->obj_type,
197 $this->sub_obj_id,
198 $this->sub_obj_type,
199 $this->getUserId(),
200 0
201 );
202
203 if ((bool) $a_average) {
204 $overall_rating = ilRating::getOverallRatingForObject(
205 $this->obj_id,
206 $this->obj_type,
207 $this->sub_obj_id,
208 $this->sub_obj_type
209 );
210 }
211
212 // user rating links
213 for ($i = 1; $i <= 5; $i++) {
214 if ((bool) $a_average &&
215 $i == $rating) {
216 $ttpl->setCurrentBlock("rating_mark_simple");
217 $ttpl->setVariable(
218 "SRC_MARK_SIMPLE",
219 ilUtil::getImagePath("icon_rate_marker.svg")
220 );
221 $ttpl->parseCurrentBlock();
222 }
223
224 $ttpl->setCurrentBlock("rating_link_simple");
225 if (stristr($a_onclick, "%rating%")) {
226 $url_save = "#";
227 } else {
228 $ilCtrl->setParameter($this, "rating", $i);
229 if (!$this->ctrl_path) {
230 $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
231 } else {
232 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
233 }
234 }
235 $ttpl->setVariable("HREF_RATING", $url_save);
236
237 if ($a_onclick) {
238 $onclick = str_replace("%rating%", $i, $a_onclick);
239 $ttpl->setVariable("ONCLICK_RATING", ' onclick="' . $onclick . '"');
240 }
241
242 if ((bool) $a_average) {
243 $ref_rating = $overall_rating["avg"];
244 } else {
245 $ref_rating = $rating;
246 }
247
248 if ($ref_rating >= $i) {
249 $ttpl->setVariable(
250 "SRC_ICON",
251 ilUtil::getImagePath("icon_rate_on.svg")
252 );
253 } else {
254 $ttpl->setVariable(
255 "SRC_ICON",
256 ilUtil::getImagePath("icon_rate_off.svg")
257 );
258 }
259 $ttpl->setVariable("ALT_ICON", "(" . $i . "/5)");
260 $ttpl->parseCurrentBlock();
261 }
262
263 // remove
264 if ($rating) {
265 $ttpl->setCurrentBlock("rating_simple_del_bl");
266 $ttpl->setVariable("CAPTION_RATING_DEL", $lng->txt("rating_remove"));
267
268 if (stristr($a_onclick, "%rating%")) {
269 $url_save = "#";
270 } else {
271 $ilCtrl->setParameter($this, "rating", 0);
272 if (!$this->ctrl_path) {
273 $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
274 } else {
275 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
276 }
277 }
278 $ttpl->setVariable("HREF_RATING_DEL", $url_save);
279
280 if ($a_onclick) {
281 $onclick = str_replace("%rating%", 0, $a_onclick);
282 $ttpl->setVariable("ONCLICK_RATING_DEL", ' onclick="' . $onclick . '"');
283 }
284
285 $ttpl->parseCurrentBlock();
286 }
287
288 if ($rate_text) {
289 $ttpl->setCurrentBlock("rating_simple_title");
290 $ttpl->setVariable("TXT_RATING_SIMPLE", $rate_text);
291 $ttpl->parseCurrentBlock();
292 }
293
294 // user rating text
295 $ttpl->setCurrentBlock("user_rating_simple");
296
297 if ((bool) $a_average &&
298 $overall_rating["cnt"]) {
299 $ttpl->setVariable("NUMBER_VOTES_SIMPLE", $overall_rating["cnt"]);
300 }
301
302 $ttpl->parseCurrentBlock();
303 }
304 }
305 // categories: overall & user (form)
306 else {
307 $has_user_rating = false;
308 foreach ($a_categories as $category) {
309 $user_rating = round(ilRating::getRatingForUserAndObject(
310 $this->obj_id,
311 $this->obj_type,
312 $this->sub_obj_id,
313 $this->sub_obj_type,
314 $this->getUserId(),
315 $category["id"]
316 ));
317
318 $overall_rating = ilRating::getOverallRatingForObject(
319 $this->obj_id,
320 $this->obj_type,
321 $this->sub_obj_id,
322 $this->sub_obj_type,
323 $category["id"]
324 );
325
326 for ($i = 1; $i <= 5; $i++) {
327 if ($a_may_rate && $i == $user_rating) {
328 $has_user_rating = true;
329
330 $ttpl->setCurrentBlock("rating_mark");
331 $ttpl->setVariable(
332 "SRC_MARK",
333 ilUtil::getImagePath("icon_rate_marker.svg")
334 );
335 $ttpl->parseCurrentBlock();
336 }
337
338 $ttpl->setCurrentBlock("user_rating_icon");
339 if ($overall_rating["avg"] >= $i) {
340 $ttpl->setVariable(
341 "SRC_ICON",
342 ilUtil::getImagePath("icon_rate_on.svg")
343 );
344 } elseif ($overall_rating["avg"] + 1 <= $i) {
345 $ttpl->setVariable(
346 "SRC_ICON",
347 ilUtil::getImagePath("icon_rate_off.svg")
348 );
349 } else {
350 $nr = round(($overall_rating["avg"] + 1 - $i) * 10);
351 $ttpl->setVariable(
352 "SRC_ICON",
353 ilUtil::getImagePath("icon_rate_$nr.svg")
354 );
355 }
356 $ttpl->setVariable("ALT_ICON", "(" . $i . "/5)");
357
358 if ($a_may_rate) {
359 $ttpl->setVariable("HREF_RATING", "il.Rating.setValue(" . $category["id"] . "," . $i . ", '" . $a_js_id . "')");
360 $ttpl->setVariable("CATEGORY_ID", $category["id"]);
361 $ttpl->setVariable("ICON_VALUE", $i);
362 $ttpl->setVariable("JS_ID", $a_js_id);
363 $ttpl->setVariable("ICON_MOUSEACTION", " onmouseover=\"il.Rating.toggleIcon(this," . $i . ")\"" .
364 " onmouseout=\"il.Rating.toggleIcon(this," . $i . ",1)\"");
365 }
366
367 $ttpl->parseCurrentBlock();
368 }
369
370 if ($a_may_rate) {
371 $ttpl->setCurrentBlock("user_rating_category_column");
372 $ttpl->setVariable("JS_ID", $a_js_id);
373 $ttpl->setVariable("CATEGORY_ID", $category["id"]);
374 $ttpl->setVariable("CATEGORY_VALUE", $user_rating);
375 $ttpl->parseCurrentBlock();
376 }
377
378 // category title
379 $ttpl->setCurrentBlock("user_rating_category");
380 $ttpl->setVariable("TXT_RATING_CATEGORY", $category["title"]);
381 $ttpl->parseCurrentBlock();
382 }
383
384 if ($overall_rating["cnt"]) {
385 $ttpl->setCurrentBlock("votes_number_bl");
386 $ttpl->setVariable("NUMBER_VOTES", sprintf($lng->txt("rating_number_votes"), $overall_rating["cnt"]));
387 $ttpl->parseCurrentBlock();
388 }
389
390 if ($a_may_rate) {
391 // remove
392 if ($has_user_rating) {
393 $ttpl->setCurrentBlock("user_rating_categories_del_bl");
394 $ttpl->setVariable("CAPTION_RATING_DEL_CAT", $lng->txt("rating_remove"));
395
396 $ilCtrl->setParameter($this, "rating", 0);
397 if (!$this->ctrl_path) {
398 $url_save = $ilCtrl->getLinkTarget($this, "resetUserRating");
399 } else {
400 $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "resetUserRating");
401 }
402 $ttpl->setVariable("HREF_RATING_DEL_CAT", $url_save);
403
404 $ttpl->parseCurrentBlock();
405 }
406
407 if (!$this->ctrl_path) {
408 $url_form = $ilCtrl->getFormAction($this, "saveRating");
409 } else {
410 $url_form = $ilCtrl->getFormActionByClass($this->ctrl_path, "saveRating");
411 }
412 $ttpl->setVariable("FORM_ACTION", $url_form);
413 $ttpl->setVariable("TXT_SUBMIT", $lng->txt("rating_overlay_submit"));
414 $ttpl->setVariable("CMD_SUBMIT", "saveRating");
415 $ttpl->touchBlock("user_rating_categories_form_out");
416
417 // overall / user title
418 /*
419 $ttpl->setCurrentBlock("user_rating_categories");
420 $ttpl->setVariable("TXT_RATING_OVERALL", $lng->txt("rating_overlay_title_overall"));
421 $ttpl->parseCurrentBlock();
422 */
423 }
424 }
425
426 return $ttpl->get();
427 }
428
438 public function getHTML($a_show_overall = true, $a_may_rate = true, $a_onclick = null, $a_additional_id = null)
439 {
441
442 $unique_id = $this->id;
443 if ($a_additional_id) {
444 $unique_id .= "_" . $a_additional_id;
445 }
446
447 $categories = array();
448 if ($this->enable_categories) {
449 $categories = ilRatingCategory::getAllForObject($this->obj_id);
450 }
451
452 $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
453 if ($may_rate && !$a_may_rate) {
454 $may_rate = false;
455 }
456
457 $has_overlay = false;
458 if ($may_rate || $categories) {
459 $has_overlay = true;
460 }
461
462 $ttpl = new ilTemplate("tpl.rating_input.html", true, true, "Services/Rating");
463
464 // user rating
465 $user_rating = 0;
466 if ($may_rate || !$a_show_overall) {
467 $user_rating = round(ilRating::getRatingForUserAndObject(
468 $this->obj_id,
469 $this->obj_type,
470 $this->sub_obj_id,
471 $this->sub_obj_type,
472 $this->getUserId()
473 ));
474 }
475
476 // (1) overall rating
477 if ($a_show_overall) {
479 $this->obj_id,
480 $this->obj_type,
481 $this->sub_obj_id,
482 $this->sub_obj_type
483 );
484 } else {
485 $rating = array("avg" => $user_rating);
486 }
487
488 for ($i = 1; $i <= 5; $i++) {
489 if ($a_show_overall &&
490 $i == $user_rating) {
491 $ttpl->setCurrentBlock("rating_mark");
492 $ttpl->setVariable(
493 "SRC_MARK",
494 ilUtil::getImagePath("icon_rate_marker.svg")
495 );
496 $ttpl->parseCurrentBlock();
497 }
498
499 $ttpl->setCurrentBlock("rating_icon");
500 if ($rating["avg"] >= $i) {
501 $ttpl->setVariable(
502 "SRC_ICON",
503 ilUtil::getImagePath("icon_rate_on.svg")
504 );
505 } elseif ($rating["avg"] + 1 <= $i) {
506 $ttpl->setVariable(
507 "SRC_ICON",
508 ilUtil::getImagePath("icon_rate_off.svg")
509 );
510 } else {
511 $nr = round(($rating["avg"] + 1 - $i) * 10);
512 $ttpl->setVariable(
513 "SRC_ICON",
514 ilUtil::getImagePath("icon_rate_$nr.svg")
515 );
516 }
517 $ttpl->setVariable("ALT_ICON", "(" . $i . "/5)");
518 $ttpl->parseCurrentBlock();
519 }
520 $ttpl->setCurrentBlock("rating_icon");
521
522 if ($a_show_overall) {
523 if ($rating["cnt"] == 0) {
524 $tt = $lng->txt("rat_not_rated_yet");
525 } elseif ($rating["cnt"] == 1) {
526 $tt = $lng->txt("rat_one_rating");
527 } else {
528 $tt = sprintf($lng->txt("rat_nr_ratings"), $rating["cnt"]);
529 }
530 include_once("./Services/UIComponent/Tooltip/classes/class.ilTooltipGUI.php");
531 ilTooltipGUI::addTooltip($unique_id . "_tt", $tt);
532
533 if ($rating["cnt"] > 0) {
534 $ttpl->setCurrentBlock("rat_nr");
535 $ttpl->setVariable("RT_NR", $rating["cnt"]);
536 $ttpl->parseCurrentBlock();
537 }
538 }
539
540 // add overlay (trigger)
541 if ($has_overlay) {
542 include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
543 $ov = new ilOverlayGUI($unique_id);
544 $ov->setTrigger("tr_" . $unique_id, "click", "tr_" . $unique_id);
545 $ov->add();
546
547 $ttpl->setCurrentBlock("act_rat_start");
548 $ttpl->setVariable("ID", $unique_id);
549 $ttpl->parseCurrentBlock();
550
551 $ttpl->touchBlock("act_rat_end");
552 }
553
554 $ttpl->parseCurrentBlock();
555
556
557 // (2) user rating
558
559 if ($has_overlay) {
560 $ttpl->setVariable(
561 "RATING_DETAILS",
562 $this->renderDetails("rtov_", $may_rate, $categories, $a_onclick)
563 );
564
565 $ttpl->setCurrentBlock("user_rating");
566 $ttpl->setVariable("ID", $unique_id);
567 $ttpl->parseCurrentBlock();
568 }
569
570 $ttpl->setVariable("TTID", $unique_id);
571
572 return $ttpl->get();
573 }
574
575 public function getBlockHTML($a_title)
576 {
577 $ui = $this->ui;
578
579 $categories = array();
580 if ($this->enable_categories) {
581 $categories = ilRatingCategory::getAllForObject($this->obj_id);
582 }
583
584 $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
585
586
587 $panel = $ui->factory()->panel()->secondary()->legacy(
588 $a_title,
589 $ui->factory()->legacy(
590 $this->renderDetails("rtsb_", $may_rate, $categories, null, true)
591 )
592 );
593
594 return $ui->renderer()->render($panel);
595 }
596
600 public function saveRating()
601 {
603
604 if (!is_array($_REQUEST["rating"])) {
605 $rating = (int) ilUtil::stripSlashes($_GET["rating"]);
606 if (!$rating) {
607 $this->resetUserRating();
608 } else {
610 $this->obj_id,
611 $this->obj_type,
612 $this->sub_obj_id,
613 $this->sub_obj_type,
614 $this->getUserId(),
615 $rating
616 );
617 }
618 } else {
619 foreach ($_POST["rating"] as $cat_id => $rating) {
621 $this->obj_id,
622 $this->obj_type,
623 $this->sub_obj_id,
624 $this->sub_obj_type,
625 $this->getUserId(),
626 $rating,
627 $cat_id
628 );
629 }
630 }
631
632 if ($this->update_callback) {
633 call_user_func(
634 $this->update_callback,
635 $this->obj_id,
636 $this->obj_type,
637 $this->sub_obj_id,
638 $this->sub_obj_type
639 );
640 }
641
642 if ($ilCtrl->isAsynch()) {
643 exit();
644 }
645 }
646
647 public function setUpdateCallback($a_callback)
648 {
649 $this->update_callback = $a_callback;
650 }
651
655 public function resetUserRating()
656 {
658 $this->obj_id,
659 $this->obj_type,
660 $this->sub_obj_id,
661 $this->sub_obj_type,
662 $this->getUserId()
663 );
664 }
665
666 public function setExportCallback($a_callback, $a_subobj_title)
667 {
668 $this->export_callback = $a_callback;
669 $this->export_subobj_title = $a_subobj_title;
670 }
671
681 public function getListGUIProperty($a_ref_id, $a_may_rate, $a_ajax_hash, $_parent_ref_id)
682 {
683 return $this->getHTML(
684 true,
685 $a_may_rate,
686 "il.Object.saveRatingFromListGUI(" . $a_ref_id . ", '" . $a_ajax_hash . "', %rating%);",
687 $_parent_ref_id
688 );
689 }
690}
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)
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)
global $ilCtrl
Definition: ilias.php:18
exit
Definition: login.php:29
$i
Definition: metadata.php:24
$ilUser
Definition: imgupload.php:18
ui()
Definition: ui.php:5
$DIC
Definition: xapitoken.php:46