ILIAS  release_4-3 Revision
 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 
23  function __construct()
24  {
25  global $lng;
26 
27  $lng->loadLanguageModule("rating");
28  }
29 
33  function &executeCommand()
34  {
35  global $ilCtrl;
36 
37  $next_class = $ilCtrl->getNextClass($this);
38  $cmd = $ilCtrl->getCmd();
39 
40  switch($next_class)
41  {
42  case "ilratingcategorygui":
43  include_once("./Services/Rating/classes/class.ilRatingCategoryGUI.php");
44  $gui = new ilRatingCategoryGUI($this->obj_id, $this->export_callback, $this->export_subobj_title);
45  $ilCtrl->forwardCommand($gui);
46  break;
47 
48  default:
49  return $this->$cmd();
50  break;
51  }
52  }
53 
62  function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
63  {
64  global $ilUser;
65 
66  $this->obj_id = $a_obj_id;
67  $this->obj_type = $a_obj_type;
68  $this->sub_obj_id = $a_sub_obj_id;
69  $this->sub_obj_type = $a_sub_obj_type;
70  $this->id = "rtg_".$this->obj_id."_".$this->obj_type."_".$this->sub_obj_id."_".
71  $this->sub_obj_type;
72 
73  $this->setUserId($ilUser->getId());
74  }
75 
81  function setUserId($a_userid)
82  {
83  $this->userid = $a_userid;
84  }
85 
91  function getUserId()
92  {
93  return $this->userid;
94  }
95 
101  function setYourRatingText($a_val)
102  {
103  $this->your_rating_text = $a_val;
104  }
105 
111  function getYourRatingText()
112  {
113  return $this->your_rating_text;
114  }
115 
121  function enableCategories($a_value)
122  {
123  $this->enable_categories = (bool)$a_value;
124  }
125 
134  protected function renderDetails($a_js_id, $a_may_rate, array $a_categories = null)
135  {
136  global $lng, $ilCtrl;
137 
138  $ttpl = new ilTemplate("tpl.rating_details.html", true, true, "Services/Rating");
139 
140  $rate_text = ($this->getYourRatingText() != "")
141  ? $this->getYourRatingText()
142  : $lng->txt("rating_your_rating");
143 
144  // no categories: 1 simple rating (link)
145  if(!$a_categories)
146  {
147  if ($a_may_rate)
148  {
149  $rating = ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
150  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(), 0);
151 
152  // user rating links
153  for($i = 1; $i <= 5; $i++)
154  {
155  $ttpl->setCurrentBlock("rating_link_simple");
156  $ilCtrl->setParameter($this, "rating", $i);
157  $ttpl->setVariable("HREF_RATING", $ilCtrl->getLinkTarget($this, "saveRating"));
158  if ($rating >= $i)
159  {
160  $ttpl->setVariable("SRC_ICON",
161  ilUtil::getImagePath("icon_rate_on.png"));
162  }
163  else
164  {
165  $ttpl->setVariable("SRC_ICON",
166  ilUtil::getImagePath("icon_rate_off.png"));
167  }
168  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
169  $ttpl->parseCurrentBlock();
170  }
171 
172  // user rating text
173  $ttpl->setCurrentBlock("user_rating_simple");
174  $ttpl->setVariable("TXT_RATING_SIMPLE", $rate_text);
175  $ttpl->parseCurrentBlock();
176  }
177  }
178  // categories: overall & user (form)
179  else
180  {
181  foreach($a_categories as $category)
182  {
183  $user_rating = round(ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
184  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(), $category["id"]));
185 
186  $overall_rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
187  $this->sub_obj_id, $this->sub_obj_type, $category["id"]);
188 
189  for($i = 1; $i <= 5; $i++)
190  {
191  if ($a_may_rate && $i == $user_rating)
192  {
193  $ttpl->setCurrentBlock("rating_mark");
194  $ttpl->setVariable("SRC_MARK",
195  ilUtil::getImagePath("icon_rate_marker.png"));
196  $ttpl->parseCurrentBlock();
197  }
198 
199  $ttpl->setCurrentBlock("user_rating_icon");
200  if ($overall_rating["avg"] >= $i)
201  {
202  $ttpl->setVariable("SRC_ICON",
203  ilUtil::getImagePath("icon_rate_on.png"));
204  }
205  else if ($overall_rating["avg"] + 1 <= $i)
206  {
207  $ttpl->setVariable("SRC_ICON",
208  ilUtil::getImagePath("icon_rate_off.png"));
209  }
210  else
211  {
212  $nr = round(($overall_rating["avg"] + 1 - $i) * 10);
213  $ttpl->setVariable("SRC_ICON",
214  ilUtil::getImagePath("icon_rate_$nr.png"));
215  }
216  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
217 
218  if ($a_may_rate)
219  {
220  $ttpl->setVariable("HREF_RATING", "il.Rating.setValue(".$category["id"].",".$i.", '".$a_js_id."')");
221  $ttpl->setVariable("CATEGORY_ID", $category["id"]);
222  $ttpl->setVariable("ICON_VALUE", $i);
223  $ttpl->setVariable("JS_ID", $a_js_id);
224  $ttpl->setVariable("ICON_MOUSEACTION", " onmouseover=\"il.Rating.toggleIcon(this,".$i.")\"".
225  " onmouseout=\"il.Rating.toggleIcon(this,".$i.",1)\"");
226  }
227 
228  $ttpl->parseCurrentBlock();
229  }
230 
231  if($a_may_rate)
232  {
233  $ttpl->setCurrentBlock("user_rating_category_column");
234  $ttpl->setVariable("JS_ID", $a_js_id);
235  $ttpl->setVariable("CATEGORY_ID", $category["id"]);
236  $ttpl->setVariable("CATEGORY_VALUE", $user_rating);
237  $ttpl->parseCurrentBlock();
238  }
239 
240  // category title
241  $ttpl->setCurrentBlock("user_rating_category");
242  $ttpl->setVariable("TXT_RATING_CATEGORY", $category["title"]);
243  $ttpl->parseCurrentBlock();
244  }
245 
246  if($overall_rating["cnt"])
247  {
248  $ttpl->setCurrentBlock("votes_number_bl");
249  $ttpl->setVariable("NUMBER_VOTES", sprintf($lng->txt("rating_number_votes"), $overall_rating["cnt"]));
250  $ttpl->parseCurrentBlock();
251  }
252 
253  if($a_may_rate)
254  {
255  $ttpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this, "saveRating"));
256  $ttpl->setVariable("TXT_SUBMIT", $lng->txt("rating_overlay_submit"));
257  $ttpl->setVariable("CMD_SUBMIT", "saveRating");
258  $ttpl->touchBlock("user_rating_categories_form_out");
259 
260  // overall / user title
261  $ttpl->setCurrentBlock("user_rating_categories");
262  $ttpl->setVariable("TXT_RATING_OVERALL", $lng->txt("rating_overlay_title_overall"));
263  $ttpl->parseCurrentBlock();
264  }
265  }
266 
267  return $ttpl->get();
268  }
269 
273  function getHTML()
274  {
275  global $lng;
276 
277  $categories = array();
278  if($this->enable_categories)
279  {
280  $categories = ilRatingCategory::getAllForObject($this->obj_id);
281  }
282 
283  $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
284 
285  $has_overlay = false;
286  if($may_rate || $categories)
287  {
288  $has_overlay = true;
289  }
290 
291  $ttpl = new ilTemplate("tpl.rating_input.html", true, true, "Services/Rating");
292 
293  // user rating
294  $user_rating = 0;
295  if ($may_rate)
296  {
297  $user_rating = round(ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
298  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId()));
299  }
300 
301  // (1) overall rating
302  $rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
303  $this->sub_obj_id, $this->sub_obj_type);
304 
305  for($i = 1; $i <= 5; $i++)
306  {
307  if ($i == $user_rating)
308  {
309  $ttpl->setCurrentBlock("rating_mark");
310  $ttpl->setVariable("SRC_MARK",
311  ilUtil::getImagePath("icon_rate_marker.png"));
312  $ttpl->parseCurrentBlock();
313  }
314 
315  $ttpl->setCurrentBlock("rating_icon");
316  if ($rating["avg"] >= $i)
317  {
318  $ttpl->setVariable("SRC_ICON",
319  ilUtil::getImagePath("icon_rate_on.png"));
320  }
321  else if ($rating["avg"] + 1 <= $i)
322  {
323  $ttpl->setVariable("SRC_ICON",
324  ilUtil::getImagePath("icon_rate_off.png"));
325  }
326  else
327  {
328  $nr = round(($rating["avg"] + 1 - $i) * 10);
329  $ttpl->setVariable("SRC_ICON",
330  ilUtil::getImagePath("icon_rate_$nr.png"));
331  }
332  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
333  $ttpl->parseCurrentBlock();
334  }
335  $ttpl->setCurrentBlock("rating_icon");
336  if ($rating["cnt"] == 0)
337  {
338  $tt = $lng->txt("rat_not_rated_yet");
339  }
340  else if ($rating["cnt"] == 1)
341  {
342  $tt = $lng->txt("rat_one_rating");
343  }
344  else
345  {
346  $tt = sprintf($lng->txt("rat_nr_ratings"), $rating["cnt"]);
347  }
348  include_once("./Services/UIComponent/Tooltip/classes/class.ilTooltipGUI.php");
349  ilTooltipGUI::addTooltip($this->id."_tt", $tt);
350  if ($rating["cnt"] > 0)
351  {
352  $ttpl->setCurrentBlock("rat_nr");
353  $ttpl->setVariable("RT_NR", $rating["cnt"]);
354  $ttpl->parseCurrentBlock();
355  }
356 
357  // add overlay (trigger)
358  if ($has_overlay)
359  {
360  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
361  $ov = new ilOverlayGUI($this->id);
362  $ov->setTrigger("tr_".$this->id, "click", "tr_".$this->id);
363  $ov->add();
364 
365  $ttpl->setCurrentBlock("act_rat_start");
366  $ttpl->setVariable("ID", $this->id);
367  $ttpl->setVariable("SRC_ARROW", ilUtil::getImagePath("mm_down_arrow_dark.png"));
368  $ttpl->parseCurrentBlock();
369 
370  $ttpl->setCurrentBlock("act_rat_end");
371  $ttpl->setVariable("SRC_ARROW", ilUtil::getImagePath("mm_down_arrow_dark.png"));
372  $ttpl->parseCurrentBlock();
373  }
374 
375  $ttpl->parseCurrentBlock();
376 
377 
378  // (2) user rating
379 
380  if($has_overlay)
381  {
382  $ttpl->setVariable("RATING_DETAILS",
383  $this->renderDetails("rtov_", $may_rate, $categories));
384 
385  $ttpl->setCurrentBlock("user_rating");
386  $ttpl->setVariable("ID", $this->id);
387  $ttpl->parseCurrentBlock();
388  }
389 
390  $ttpl->setVariable("TTID", $this->id);
391 
392  return $ttpl->get();
393  }
394 
395  function getBlockHTML($a_title)
396  {
397  $categories = array();
398  if($this->enable_categories)
399  {
400  $categories = ilRatingCategory::getAllForObject($this->obj_id);
401  }
402 
403  $may_rate = ($this->getUserId() != ANONYMOUS_USER_ID);
404 
405  $ttpl = new ilTemplate("tpl.rating_block.html", true, true, "Services/Rating");
406 
407  $ttpl->setVariable("TITLE", $a_title);
408 
409  $ttpl->setVariable("RATING_DETAILS",
410  $this->renderDetails("rtsb_", $may_rate, $categories));
411 
412  return $ttpl->get();
413  }
414 
418  function saveRating()
419  {
420  if(!is_array($_REQUEST["rating"]))
421  {
422  ilRating::writeRatingForUserAndObject($this->obj_id, $this->obj_type,
423  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(),
424  ilUtil::stripSlashes($_GET["rating"]));
425  }
426  else
427  {
428  foreach($_POST["rating"] as $cat_id => $rating)
429  {
430  ilRating::writeRatingForUserAndObject($this->obj_id, $this->obj_type,
431  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(),
432  $rating, $cat_id);
433  }
434  }
435  }
436 
437  function setExportCallback($a_callback, $a_subobj_title)
438  {
439  $this->export_callback = $a_callback;
440  $this->export_subobj_title = $a_subobj_title;
441  }
442 }
443 
444 ?>