ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilRatingGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once("./Services/Rating/classes/class.ilRating.php");
25 
35 {
36  function __construct()
37  {
38  global $lng;
39 
40  $lng->loadLanguageModule("rating");
41  }
42 
46  function &executeCommand()
47  {
48  global $ilCtrl;
49 
50  $next_class = $ilCtrl->getNextClass($this);
51  $cmd = $ilCtrl->getCmd();
52 
53  switch($next_class)
54  {
55  default:
56  return $this->$cmd();
57  break;
58  }
59  }
60 
69  function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
70  {
71  global $ilUser;
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 
78  //$this->setSaveCmd("saveTags");
79  $this->setUserId($ilUser->getId());
80  //$this->setInputFieldName("il_tags");
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 
106  function getHTML()
107  {
108  global $lng, $ilCtrl;
109 
110  $ttpl = new ilTemplate("tpl.rating_input.html", true, true, "Services/Rating");
111 
112  // (1) overall rating
113  $rating = ilRating::getOverallRatingForObject($this->obj_id, $this->obj_type,
114  $this->sub_obj_id, $this->sub_obj_type);
115  if ($rating["cnt"] >= 1)
116  {
117  for($i = 1; $i <= 5; $i++)
118  {
119  $ttpl->setCurrentBlock("rating_icon");
120  if ($rating["avg"] >= $i)
121  {
122  $ttpl->setVariable("SRC_ICON",
123  ilUtil::getImagePath("icon_rate_on.gif"));
124  }
125  else if ($rating["avg"] + 1 <= $i)
126  {
127  $ttpl->setVariable("SRC_ICON",
128  ilUtil::getImagePath("icon_rate_off.gif"));
129  }
130  else
131  {
132  $nr = round(($rating["avg"] + 1 - $i) * 10);
133  $ttpl->setVariable("SRC_ICON",
134  ilUtil::getImagePath("icon_rate_$nr.gif"));
135  }
136  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
137  $ttpl->parseCurrentBlock();
138  }
139  $ttpl->setCurrentBlock("rating_icon");
140  $ttpl->setVariable("TXT_OVERALL_RATING", $lng->txt("rating_average_rating"));
141  $ttpl->setVariable("NR_USERS",
142  sprintf($lng->txt("rating_nr_users"), $rating["cnt"]));
143  $ttpl->setVariable("VAL_OVERALL_RATING", "(".round($rating["avg"], 1)."/5)");
144  $ttpl->parseCurrentBlock();
145  }
146 
147  // (2) user rating
148  if ($this->getUserId() != ANONYMOUS_USER_ID)
149  {
150  $rating = ilRating::getRatingForUserAndObject($this->obj_id, $this->obj_type,
151  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId());
152 
153  // user rating links
154  for($i = 1; $i <= 5; $i++)
155  {
156  $ttpl->setCurrentBlock("rating_link");
157  $ilCtrl->setParameter($this, "rating", $i);
158  $ttpl->setVariable("HREF_RATING", $ilCtrl->getLinkTarget($this, "saveRating"));
159  if ($rating >= $i)
160  {
161  $ttpl->setVariable("SRC_ICON",
162  ilUtil::getImagePath("icon_rate_on.gif"));
163  }
164  else
165  {
166  $ttpl->setVariable("SRC_ICON",
167  ilUtil::getImagePath("icon_rate_off.gif"));
168  }
169  $ttpl->setVariable("ALT_ICON", "(".$i."/5)");
170  $ttpl->parseCurrentBlock();
171  }
172  $ttpl->setCurrentBlock("user_rating");
173 
174  // user rating text
175  $ttpl->setVariable("TXT_YOUR_RATING", $lng->txt("rating_your_rating"));
176  if ($rating > 0)
177  {
178  $ttpl->setVariable("VAL_RATING", "(".$rating."/5)");
179  }
180  $ttpl->parseCurrentBlock();
181  }
182 
183  return $ttpl->get();
184  }
185 
189  function saveRating()
190  {
191  ilRating::writeRatingForUserAndObject($this->obj_id, $this->obj_type,
192  $this->sub_obj_id, $this->sub_obj_type, $this->getUserId(),
193  ilUtil::stripSlashes($_GET["rating"]));
194  }
195 }
196 
197 ?>