ILIAS  Release_4_2_x_branch Revision 61807
 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 
15 {
16  protected $id = "rtg_";
17 
18  function __construct()
19  {
20  global $lng;
21 
22  $lng->loadLanguageModule("rating");
23  }
24 
28  function &executeCommand()
29  {
30  global $ilCtrl;
31 
32  $next_class = $ilCtrl->getNextClass($this);
33  $cmd = $ilCtrl->getCmd();
34 
35  switch($next_class)
36  {
37  default:
38  return $this->$cmd();
39  break;
40  }
41  }
42 
51  function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
52  {
53  global $ilUser;
54 
55  $this->obj_id = $a_obj_id;
56  $this->obj_type = $a_obj_type;
57  $this->sub_obj_id = $a_sub_obj_id;
58  $this->sub_obj_type = $a_sub_obj_type;
59  $this->id = "rtg_".$this->obj_id."_".$this->obj_type."_".$this->sub_obj_id."_".
60  $this->sub_obj_type;
61 
62  //$this->setSaveCmd("saveTags");
63  $this->setUserId($ilUser->getId());
64  //$this->setInputFieldName("il_tags");
65  }
66 
72  function setUserId($a_userid)
73  {
74  $this->userid = $a_userid;
75  }
76 
82  function getUserId()
83  {
84  return $this->userid;
85  }
86 
92  function setYourRatingText($a_val)
93  {
94  $this->your_rating_text = $a_val;
95  }
96 
102  function getYourRatingText()
103  {
104  return $this->your_rating_text;
105  }
106 
110  function getHTML()
111  {
112  global $lng, $ilCtrl;
113 
114  $ttpl = new ilTemplate("tpl.rating_input.html", true, true, "Services/Rating");
115 
116  // user rating
117  $user_rating = 0;
118  if ($this->getUserId() != ANONYMOUS_USER_ID)
119  {
120  $user_rating = ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
121  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId());
122  }
123 
124 
125  // (1) overall rating
126  $rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
127  $this->sub_obj_id, $this->sub_obj_type);
128 
129  for($i = 1; $i <= 5; $i++)
130  {
131  if ($i == $user_rating)
132  {
133  $ttpl->setCurrentBlock("rating_mark");
134  $ttpl->setVariable("SRC_MARK",
135  ilUtil::getImagePath("icon_rate_marker.png"));
136  $ttpl->parseCurrentBlock();
137  }
138 
139  $ttpl->setCurrentBlock("rating_icon");
140  if ($rating["avg"] >= $i)
141  {
142  $ttpl->setVariable("SRC_ICON",
143  ilUtil::getImagePath("icon_rate_on.png"));
144  }
145  else if ($rating["avg"] + 1 <= $i)
146  {
147  $ttpl->setVariable("SRC_ICON",
148  ilUtil::getImagePath("icon_rate_off.png"));
149  }
150  else
151  {
152  $nr = round(($rating["avg"] + 1 - $i) * 10);
153  $ttpl->setVariable("SRC_ICON",
154  ilUtil::getImagePath("icon_rate_$nr.png"));
155  }
156  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
157  $ttpl->parseCurrentBlock();
158  }
159  $ttpl->setCurrentBlock("rating_icon");
160  if ($rating["cnt"] == 0)
161  {
162  $tt = $lng->txt("rat_not_rated_yet");
163  }
164  else if ($rating["cnt"] == 1)
165  {
166  $tt = $lng->txt("rat_one_rating");
167  }
168  else
169  {
170  $tt =
171  sprintf($lng->txt("rat_nr_ratings"), $rating["cnt"]);
172  }
173  include_once("./Services/UIComponent/Tooltip/classes/class.ilTooltipGUI.php");
174  ilTooltipGUI::addTooltip($this->id."_tt", $tt);
175  if ($rating["cnt"] > 0)
176  {
177  $ttpl->setCurrentBlock("rat_nr");
178  $ttpl->setVariable("RT_NR", $rating["cnt"]);
179  $ttpl->parseCurrentBlock();
180  }
181 
182  if ($this->getUserId() != ANONYMOUS_USER_ID)
183  {
184  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
185  $ov = new ilOverlayGUI($this->id);
186  $ov->setTrigger("tr_".$this->id, "click", "tr_".$this->id);
187  $ov->add();
188 
189  $ttpl->setCurrentBlock("act_rat_start");
190  $ttpl->setVariable("ID", $this->id);
191  $ttpl->setVariable("SRC_ARROW", ilUtil::getImagePath("mm_down_arrow_dark.gif"));
192  $ttpl->parseCurrentBlock();
193 
194  //$ttpl->touchBlock("act_rat_end");
195  $ttpl->setCurrentBlock("act_rat_end");
196  $ttpl->setVariable("SRC_ARROW", ilUtil::getImagePath("mm_down_arrow_dark.gif"));
197  $ttpl->parseCurrentBlock();
198  }
199 
200  $ttpl->parseCurrentBlock();
201 
202  // (2) user rating
203  if ($this->getUserId() != ANONYMOUS_USER_ID)
204  {
205  $rating = $user_rating;
206 
207  // user rating links
208  for($i = 1; $i <= 5; $i++)
209  {
210  $ttpl->setCurrentBlock("rating_link");
211  $ilCtrl->setParameter($this, "rating", $i);
212  $ttpl->setVariable("HREF_RATING", $ilCtrl->getLinkTarget($this, "saveRating"));
213  if ($rating >= $i)
214  {
215  $ttpl->setVariable("SRC_ICON",
216  ilUtil::getImagePath("icon_rate_on.png"));
217  }
218  else
219  {
220  $ttpl->setVariable("SRC_ICON",
221  ilUtil::getImagePath("icon_rate_off.png"));
222  }
223  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
224  $ttpl->parseCurrentBlock();
225  }
226  $ttpl->setCurrentBlock("user_rating");
227  $ttpl->setVariable("ID", $this->id);
228 
229  // user rating text
230  $rate_text = ($this->getYourRatingText() != "")
231  ? $this->getYourRatingText()
232  : $lng->txt("rating_your_rating");
233  $ttpl->setVariable("TXT_YOUR_RATING", $rate_text);
234  $ttpl->parseCurrentBlock();
235  }
236 
237  $ttpl->setVariable("TTID", $this->id);
238 
239  return $ttpl->get();
240  }
241 
245  function saveRating()
246  {
247  ilRating::writeRatingForUserAndObject($this->obj_id, $this->obj_type,
248  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(),
249  ilUtil::stripSlashes($_GET["rating"]));
250  }
251 }
252 
253 ?>