ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
4 include_once("./Services/Rating/classes/class.ilRating.php");
5 include_once("./Services/Rating/classes/class.ilRatingCategory.php");
6 
18 {
19  protected $id = "rtg_";
20  protected $export_callback;
22  protected $ctrl_path;
23 
24  function __construct()
25  {
26  global $lng;
27 
28  $lng->loadLanguageModule("rating");
29  }
30 
34  function &executeCommand()
35  {
36  global $ilCtrl;
37 
38  $next_class = $ilCtrl->getNextClass($this);
39  $cmd = $ilCtrl->getCmd();
40 
41  switch($next_class)
42  {
43  case "ilratingcategorygui":
44  include_once("./Services/Rating/classes/class.ilRatingCategoryGUI.php");
45  $gui = new ilRatingCategoryGUI($this->obj_id, $this->export_callback, $this->export_subobj_title);
46  $ilCtrl->forwardCommand($gui);
47  break;
48 
49  default:
50  return $this->$cmd();
51  break;
52  }
53  }
54 
63  function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
64  {
65  global $ilUser;
66 
67  // db-column is defined as not null, el stupido
68  if(!trim($a_sub_obj_type))
69  {
70  $a_sub_obj_type = "-";
71  }
72 
73  $this->obj_id = $a_obj_id;
74  $this->obj_type = $a_obj_type;
75  $this->sub_obj_id = $a_sub_obj_id;
76  $this->sub_obj_type = $a_sub_obj_type;
77  $this->id = "rtg_".$this->obj_id."_".$this->obj_type."_".$this->sub_obj_id."_".
78  $this->sub_obj_type;
79 
80  $this->setUserId($ilUser->getId());
81  }
82 
88  function setUserId($a_userid)
89  {
90  $this->userid = $a_userid;
91  }
92 
98  function getUserId()
99  {
100  return $this->userid;
101  }
102 
108  function setYourRatingText($a_val)
109  {
110  $this->your_rating_text = $a_val;
111  }
112 
118  function getYourRatingText()
119  {
120  return $this->your_rating_text;
121  }
122 
128  function enableCategories($a_value)
129  {
130  $this->enable_categories = (bool)$a_value;
131  }
132 
138  public function setCtrlPath(array $a_value)
139  {
140  $this->ctrl_path = $a_value;
141  }
142 
153  protected function renderDetails($a_js_id, $a_may_rate, array $a_categories = null, $a_onclick = null, $a_average = false)
154  {
155  global $lng, $ilCtrl;
156 
157  $ttpl = new ilTemplate("tpl.rating_details.html", true, true, "Services/Rating");
158 
159  $rate_text = null;
160  if($this->getYourRatingText() != "#")
161  {
162  $rate_text = ($this->getYourRatingText() != "")
163  ? $this->getYourRatingText()
164  : $lng->txt("rating_your_rating");
165  }
166 
167  // no categories: 1 simple rating (link)
168  if(!$a_categories)
169  {
170  if ($a_may_rate)
171  {
172  $rating = ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
173  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(), 0);
174 
175  if((bool)$a_average)
176  {
177  $overall_rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
178  $this->sub_obj_id, $this->sub_obj_type);
179  }
180 
181  // user rating links
182  for($i = 1; $i <= 5; $i++)
183  {
184  if ((bool)$a_average &&
185  $i == $rating)
186  {
187  $ttpl->setCurrentBlock("rating_mark_simple");
188  $ttpl->setVariable("SRC_MARK_SIMPLE",
189  ilUtil::getImagePath("icon_rate_marker.svg"));
190  $ttpl->parseCurrentBlock();
191  }
192 
193  $ttpl->setCurrentBlock("rating_link_simple");
194  if(stristr($a_onclick, "%rating%"))
195  {
196  $url_save = "#";
197  }
198  else
199  {
200  $ilCtrl->setParameter($this, "rating", $i);
201  if(!$this->ctrl_path)
202  {
203  $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
204  }
205  else
206  {
207  $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
208  }
209  }
210  $ttpl->setVariable("HREF_RATING", $url_save);
211 
212  if($a_onclick)
213  {
214  $onclick = str_replace("%rating%", $i, $a_onclick);
215  $ttpl->setVariable("ONCLICK_RATING", ' onclick="'.$onclick.'"');
216  }
217 
218  if((bool)$a_average)
219  {
220  $ref_rating = $overall_rating["avg"];
221  }
222  else
223  {
224  $ref_rating = $rating;
225  }
226 
227  if ($ref_rating >= $i)
228  {
229  $ttpl->setVariable("SRC_ICON",
230  ilUtil::getImagePath("icon_rate_on.svg"));
231  }
232  else
233  {
234  $ttpl->setVariable("SRC_ICON",
235  ilUtil::getImagePath("icon_rate_off.svg"));
236  }
237  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
238  $ttpl->parseCurrentBlock();
239  }
240 
241  // remove
242  if($rating)
243  {
244  $ttpl->setCurrentBlock("rating_simple_del_bl");
245  $ttpl->setVariable("CAPTION_RATING_DEL", $lng->txt("rating_remove"));
246 
247  if(stristr($a_onclick, "%rating%"))
248  {
249  $url_save = "#";
250  }
251  else
252  {
253  $ilCtrl->setParameter($this, "rating", 0);
254  if(!$this->ctrl_path)
255  {
256  $url_save = $ilCtrl->getLinkTarget($this, "saveRating");
257  }
258  else
259  {
260  $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "saveRating");
261  }
262  }
263  $ttpl->setVariable("HREF_RATING_DEL", $url_save);
264 
265  if($a_onclick)
266  {
267  $onclick = str_replace("%rating%", 0, $a_onclick);
268  $ttpl->setVariable("ONCLICK_RATING_DEL", ' onclick="'.$onclick.'"');
269  }
270 
271  $ttpl->parseCurrentBlock();
272  }
273 
274  if($rate_text)
275  {
276  $ttpl->setCurrentBlock("rating_simple_title");
277  $ttpl->setVariable("TXT_RATING_SIMPLE", $rate_text);
278  $ttpl->parseCurrentBlock();
279  }
280 
281  // user rating text
282  $ttpl->setCurrentBlock("user_rating_simple");
283 
284  if((bool)$a_average &&
285  $overall_rating["cnt"])
286  {
287  $ttpl->setVariable("NUMBER_VOTES_SIMPLE", $overall_rating["cnt"]);
288  }
289 
290  $ttpl->parseCurrentBlock();
291  }
292  }
293  // categories: overall & user (form)
294  else
295  {
296  $has_user_rating = false;
297  foreach($a_categories as $category)
298  {
299  $user_rating = round(ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
300  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(), $category["id"]));
301 
302  $overall_rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
303  $this->sub_obj_id, $this->sub_obj_type, $category["id"]);
304 
305  for($i = 1; $i <= 5; $i++)
306  {
307  if ($a_may_rate && $i == $user_rating)
308  {
309  $has_user_rating = true;
310 
311  $ttpl->setCurrentBlock("rating_mark");
312  $ttpl->setVariable("SRC_MARK",
313  ilUtil::getImagePath("icon_rate_marker.svg"));
314  $ttpl->parseCurrentBlock();
315  }
316 
317  $ttpl->setCurrentBlock("user_rating_icon");
318  if ($overall_rating["avg"] >= $i)
319  {
320  $ttpl->setVariable("SRC_ICON",
321  ilUtil::getImagePath("icon_rate_on.svg"));
322  }
323  else if ($overall_rating["avg"] + 1 <= $i)
324  {
325  $ttpl->setVariable("SRC_ICON",
326  ilUtil::getImagePath("icon_rate_off.svg"));
327  }
328  else
329  {
330  $nr = round(($overall_rating["avg"] + 1 - $i) * 10);
331  $ttpl->setVariable("SRC_ICON",
332  ilUtil::getImagePath("icon_rate_$nr.svg"));
333  }
334  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
335 
336  if ($a_may_rate)
337  {
338  $ttpl->setVariable("HREF_RATING", "il.Rating.setValue(".$category["id"].",".$i.", '".$a_js_id."')");
339  $ttpl->setVariable("CATEGORY_ID", $category["id"]);
340  $ttpl->setVariable("ICON_VALUE", $i);
341  $ttpl->setVariable("JS_ID", $a_js_id);
342  $ttpl->setVariable("ICON_MOUSEACTION", " onmouseover=\"il.Rating.toggleIcon(this,".$i.")\"".
343  " onmouseout=\"il.Rating.toggleIcon(this,".$i.",1)\"");
344  }
345 
346  $ttpl->parseCurrentBlock();
347  }
348 
349  if($a_may_rate)
350  {
351  $ttpl->setCurrentBlock("user_rating_category_column");
352  $ttpl->setVariable("JS_ID", $a_js_id);
353  $ttpl->setVariable("CATEGORY_ID", $category["id"]);
354  $ttpl->setVariable("CATEGORY_VALUE", $user_rating);
355  $ttpl->parseCurrentBlock();
356  }
357 
358  // category title
359  $ttpl->setCurrentBlock("user_rating_category");
360  $ttpl->setVariable("TXT_RATING_CATEGORY", $category["title"]);
361  $ttpl->parseCurrentBlock();
362  }
363 
364  if($overall_rating["cnt"])
365  {
366  $ttpl->setCurrentBlock("votes_number_bl");
367  $ttpl->setVariable("NUMBER_VOTES", sprintf($lng->txt("rating_number_votes"), $overall_rating["cnt"]));
368  $ttpl->parseCurrentBlock();
369  }
370 
371  if($a_may_rate)
372  {
373  // remove
374  if($has_user_rating)
375  {
376  $ttpl->setCurrentBlock("user_rating_categories_del_bl");
377  $ttpl->setVariable("CAPTION_RATING_DEL_CAT", $lng->txt("rating_remove"));
378 
379  $ilCtrl->setParameter($this, "rating", 0);
380  if(!$this->ctrl_path)
381  {
382  $url_save = $ilCtrl->getLinkTarget($this, "resetUserRating");
383  }
384  else
385  {
386  $url_save = $ilCtrl->getLinkTargetByClass($this->ctrl_path, "resetUserRating");
387  }
388  $ttpl->setVariable("HREF_RATING_DEL_CAT", $url_save);
389 
390  $ttpl->parseCurrentBlock();
391  }
392 
393  if(!$this->ctrl_path)
394  {
395  $url_form = $ilCtrl->getFormAction($this, "saveRating");
396  }
397  else
398  {
399  $url_form = $ilCtrl->getFormActionByClass($this->ctrl_path, "saveRating");
400  }
401  $ttpl->setVariable("FORM_ACTION", $url_form);
402  $ttpl->setVariable("TXT_SUBMIT", $lng->txt("rating_overlay_submit"));
403  $ttpl->setVariable("CMD_SUBMIT", "saveRating");
404  $ttpl->touchBlock("user_rating_categories_form_out");
405 
406  // overall / user title
407  /*
408  $ttpl->setCurrentBlock("user_rating_categories");
409  $ttpl->setVariable("TXT_RATING_OVERALL", $lng->txt("rating_overlay_title_overall"));
410  $ttpl->parseCurrentBlock();
411  */
412  }
413  }
414 
415  return $ttpl->get();
416  }
417 
427  function getHTML($a_show_overall = true, $a_may_rate = true, $a_onclick = null, $a_additional_id = null)
428  {
429  global $lng;
430 
431  $unique_id = $this->id;
432  if($a_additional_id)
433  {
434  $unique_id .= "_".$a_additional_id;
435  }
436 
437  $categories = array();
438  if($this->enable_categories)
439  {
440  $categories = ilRatingCategory::getAllForObject($this->obj_id);
441  }
442 
443  $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
444  if($may_rate && !$a_may_rate)
445  {
446  $may_rate = false;
447  }
448 
449  $has_overlay = false;
450  if($may_rate || $categories)
451  {
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  {
461  $user_rating = round(ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
462  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId()));
463  }
464 
465  // (1) overall rating
466  if($a_show_overall)
467  {
468  $rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
469  $this->sub_obj_id, $this->sub_obj_type);
470  }
471  else
472  {
473  $rating = array("avg"=>$user_rating);
474  }
475 
476  for($i = 1; $i <= 5; $i++)
477  {
478  if ($a_show_overall &&
479  $i == $user_rating)
480  {
481  $ttpl->setCurrentBlock("rating_mark");
482  $ttpl->setVariable("SRC_MARK",
483  ilUtil::getImagePath("icon_rate_marker.svg"));
484  $ttpl->parseCurrentBlock();
485  }
486 
487  $ttpl->setCurrentBlock("rating_icon");
488  if ($rating["avg"] >= $i)
489  {
490  $ttpl->setVariable("SRC_ICON",
491  ilUtil::getImagePath("icon_rate_on.svg"));
492  }
493  else if ($rating["avg"] + 1 <= $i)
494  {
495  $ttpl->setVariable("SRC_ICON",
496  ilUtil::getImagePath("icon_rate_off.svg"));
497  }
498  else
499  {
500  $nr = round(($rating["avg"] + 1 - $i) * 10);
501  $ttpl->setVariable("SRC_ICON",
502  ilUtil::getImagePath("icon_rate_$nr.svg"));
503  }
504  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
505  $ttpl->parseCurrentBlock();
506  }
507  $ttpl->setCurrentBlock("rating_icon");
508 
509  if($a_show_overall)
510  {
511  if ($rating["cnt"] == 0)
512  {
513  $tt = $lng->txt("rat_not_rated_yet");
514  }
515  else if ($rating["cnt"] == 1)
516  {
517  $tt = $lng->txt("rat_one_rating");
518  }
519  else
520  {
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  {
528  $ttpl->setCurrentBlock("rat_nr");
529  $ttpl->setVariable("RT_NR", $rating["cnt"]);
530  $ttpl->parseCurrentBlock();
531  }
532  }
533 
534  // add overlay (trigger)
535  if ($has_overlay)
536  {
537  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
538  $ov = new ilOverlayGUI($unique_id);
539  $ov->setTrigger("tr_".$unique_id, "click", "tr_".$unique_id);
540  $ov->add();
541 
542  $ttpl->setCurrentBlock("act_rat_start");
543  $ttpl->setVariable("ID", $unique_id);
544  $ttpl->parseCurrentBlock();
545 
546  $ttpl->touchBlock("act_rat_end");
547  }
548 
549  $ttpl->parseCurrentBlock();
550 
551 
552  // (2) user rating
553 
554  if($has_overlay)
555  {
556  $ttpl->setVariable("RATING_DETAILS",
557  $this->renderDetails("rtov_", $may_rate, $categories, $a_onclick));
558 
559  $ttpl->setCurrentBlock("user_rating");
560  $ttpl->setVariable("ID", $unique_id);
561  $ttpl->parseCurrentBlock();
562  }
563 
564  $ttpl->setVariable("TTID", $unique_id);
565 
566  return $ttpl->get();
567  }
568 
569  function getBlockHTML($a_title)
570  {
571  $categories = array();
572  if($this->enable_categories)
573  {
574  $categories = ilRatingCategory::getAllForObject($this->obj_id);
575  }
576 
577  $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
578 
579  $ttpl = new ilTemplate("tpl.rating_block.html", true, true, "Services/Rating");
580 
581  $ttpl->setVariable("TITLE", $a_title);
582 
583  $ttpl->setVariable("RATING_DETAILS",
584  $this->renderDetails("rtsb_", $may_rate, $categories, null, true));
585 
586  return $ttpl->get();
587  }
588 
592  function saveRating()
593  {
594  global $ilCtrl;
595 
596  if(!is_array($_REQUEST["rating"]))
597  {
598  $rating = (int)ilUtil::stripSlashes($_GET["rating"]);
599  if(!$rating)
600  {
601  $this->resetUserRating();
602  }
603  else
604  {
605  ilRating::writeRatingForUserAndObject($this->obj_id, $this->obj_type,
606  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(),
607  $rating);
608  }
609  }
610  else
611  {
612  foreach($_POST["rating"] as $cat_id => $rating)
613  {
614  ilRating::writeRatingForUserAndObject($this->obj_id, $this->obj_type,
615  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(),
616  $rating, $cat_id);
617  }
618  }
619 
620  if($this->update_callback)
621  {
622  call_user_func($this->update_callback, $this->obj_id, $this->obj_type,
623  $this->sub_obj_id, $this->sub_obj_type);
624  }
625 
626  if($ilCtrl->isAsynch())
627  {
628  exit();
629  }
630  }
631 
632  public function setUpdateCallback($a_callback)
633  {
634  $this->update_callback = $a_callback;
635  }
636 
640  function resetUserRating()
641  {
642  ilRating::resetRatingForUserAndObject($this->obj_id, $this->obj_type,
643  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId());
644  }
645 
646  function setExportCallback($a_callback, $a_subobj_title)
647  {
648  $this->export_callback = $a_callback;
649  $this->export_subobj_title = $a_subobj_title;
650  }
651 
661  public function getListGUIProperty($a_ref_id, $a_may_rate, $a_ajax_hash, $_parent_ref_id)
662  {
663  return $this->getHTML(true, $a_may_rate,
664  "il.Object.saveRatingFromListGUI(".$a_ref_id.", '".$a_ajax_hash."', %rating%);",
665  $_parent_ref_id);
666  }
667 }
668 
669 ?>