ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilRating.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
15 class ilRating
16 {
17  protected static $list_data; // [array]
18 
30  public static function writeRatingForUserAndObject(
31  $a_obj_id,
32  $a_obj_type,
33  $a_sub_obj_id,
34  $a_sub_obj_type,
35  $a_user_id,
36  $a_rating,
37  $a_category_id = 0
38  ) {
39  global $DIC;
40 
41  $ilDB = $DIC->database();
42 
43  if ($a_rating < 0) {
44  $a_rating = 0;
45  }
46 
47  if ($a_rating > 5) {
48  $a_rating = 5;
49  }
50 
51  if ($a_user_id == ANONYMOUS_USER_ID) {
52  return;
53  }
54 
55  if ($a_category_id) {
56  $ilDB->manipulate("DELETE FROM il_rating WHERE " .
57  "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
58  "obj_id = " . $ilDB->quote((int) $a_obj_id, "integer") . " AND " .
59  "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
60  "sub_obj_id = " . $ilDB->quote((int) $a_sub_obj_id, "integer") . " AND " .
61  $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true) . " AND " .
62  "category_id = " . $ilDB->quote(0, "integer"));
63  }
64 
65  $ilDB->manipulate("DELETE FROM il_rating WHERE " .
66  "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
67  "obj_id = " . $ilDB->quote((int) $a_obj_id, "integer") . " AND " .
68  "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
69  "sub_obj_id = " . $ilDB->quote((int) $a_sub_obj_id, "integer") . " AND " .
70  $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true) . " AND " .
71  "category_id = " . $ilDB->quote((int) $a_category_id, "integer"));
72 
73  if ((int) $a_rating) {
74  $ilDB->manipulate("INSERT INTO il_rating (user_id, obj_id, obj_type," .
75  "sub_obj_id, sub_obj_type, category_id, rating, tstamp) VALUES (" .
76  $ilDB->quote($a_user_id, "integer") . "," .
77  $ilDB->quote((int) $a_obj_id, "integer") . "," .
78  $ilDB->quote($a_obj_type, "text") . "," .
79  $ilDB->quote((int) $a_sub_obj_id, "integer") . "," .
80  $ilDB->quote($a_sub_obj_type, "text") . "," .
81  $ilDB->quote($a_category_id, "integer") . "," .
82  $ilDB->quote((int) $a_rating, "integer") . "," .
83  $ilDB->quote(time(), "integer") . ")");
84  }
85  }
86 
96  public static function resetRatingForUserAndObject(
97  $a_obj_id,
98  $a_obj_type,
99  $a_sub_obj_id,
100  $a_sub_obj_type,
101  $a_user_id
102  ) {
103  global $DIC;
104 
105  $ilDB = $DIC->database();
106 
107  $ilDB->manipulate("DELETE FROM il_rating WHERE " .
108  "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
109  "obj_id = " . $ilDB->quote((int) $a_obj_id, "integer") . " AND " .
110  "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
111  "sub_obj_id = " . $ilDB->quote((int) $a_sub_obj_id, "integer") . " AND " .
112  $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true));
113  }
114 
115 
126  public static function getRatingForUserAndObject(
127  $a_obj_id,
128  $a_obj_type,
129  $a_sub_obj_id,
130  $a_sub_obj_type,
131  $a_user_id,
132  $a_category_id = null
133  ) {
134  global $DIC;
135 
136  $ilDB = $DIC->database();
137 
138  if (is_array(self::$list_data)) {
139  return self::$list_data["user"][$a_obj_type . "/" . $a_obj_id];
140  }
141 
142  $q = "SELECT AVG(rating) av FROM il_rating WHERE " .
143  "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
144  "obj_id = " . $ilDB->quote((int) $a_obj_id, "integer") . " AND " .
145  "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
146  "sub_obj_id = " . $ilDB->quote((int) $a_sub_obj_id, "integer") . " AND " .
147  $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true);
148  if ($a_category_id !== null) {
149  $q .= " AND category_id = " . $ilDB->quote((int) $a_category_id, "integer");
150  }
151  $set = $ilDB->query($q);
152  $rec = $ilDB->fetchAssoc($set);
153  return $rec["av"];
154  }
155 
165  public static function getOverallRatingForObject($a_obj_id, $a_obj_type, $a_sub_obj_id = null, $a_sub_obj_type = null, $a_category_id = null)
166  {
167  global $DIC;
168 
169  $ilDB = $DIC->database();
170 
171  if (is_array(self::$list_data)) {
172  return self::$list_data["all"][$a_obj_type . "/" . $a_obj_id];
173  }
174 
175  $q = "SELECT AVG(rating) av FROM il_rating" .
176  " WHERE obj_id = " . $ilDB->quote((int) $a_obj_id, "integer") .
177  " AND obj_type = " . $ilDB->quote($a_obj_type, "text");
178  if ($a_sub_obj_id) {
179  $q .= " AND sub_obj_id = " . $ilDB->quote((int) $a_sub_obj_id, "integer") .
180  " AND " . $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true);
181  } else {
182  $q .= " AND sub_obj_type = " . $ilDB->quote("-", "text"); // #13913
183  }
184 
185  if ($a_category_id !== null) {
186  $q .= " AND category_id = " . $ilDB->quote((int) $a_category_id, "integer");
187  }
188  $q .= " GROUP BY user_id";
189  $set = $ilDB->query($q);
190  $avg = $cnt = 0;
191  while ($rec = $ilDB->fetchAssoc($set)) {
192  $cnt++;
193  $avg += $rec["av"];
194  }
195  if ($cnt > 0) {
196  $avg = $avg/$cnt;
197  } else {
198  $avg = 0;
199  }
200  return array("cnt" => $cnt, "avg" => $avg);
201  }
202 
211  public static function getExportData($a_obj_id, $a_obj_type, array $a_category_ids = null)
212  {
213  global $DIC;
214 
215  $ilDB = $DIC->database();
216 
217  $res = array();
218  $q = "SELECT sub_obj_id, sub_obj_type, rating, category_id, user_id, tstamp " .
219  "FROM il_rating WHERE " .
220  "obj_id = " . $ilDB->quote((int) $a_obj_id, "integer") . " AND " .
221  "obj_type = " . $ilDB->quote($a_obj_type, "text") .
222  " ORDER BY tstamp";
223  if ($a_category_ids) {
224  $q .= " AND " . $ilDB->in("category_id", $a_category_ids, "", "integer");
225  }
226  $set = $ilDB->query($q);
227  while ($row = $ilDB->fetchAssoc($set)) {
228  $res[] = $row;
229  }
230  return $res;
231  }
232 
238  public static function preloadListGUIData(array $a_obj_ids)
239  {
240  global $DIC;
241 
242  $ilDB = $DIC->database();
243  $ilUser = $DIC->user();
244 
245  $tmp = $res = $tmp_user = $res_user = array();
246 
247  // collapse by categories
248  $q = "SELECT obj_id, obj_type, user_id, AVG(rating) av" .
249  " FROM il_rating" .
250  " WHERE " . $ilDB->in("obj_id", $a_obj_ids, "", "integer") .
251  " AND sub_obj_id = " . $ilDB->quote(0, "integer") .
252  " GROUP BY obj_id, obj_type, user_id";
253  $set = $ilDB->query($q);
254  while ($rec = $ilDB->fetchAssoc($set)) {
255  $tmp[$rec["obj_type"] . "/" . $rec["obj_id"]][$rec["user_id"]] = (float) $rec["av"];
256  if ($rec["user_id"] == $ilUser->getId()) {
257  // add final average to user result (no sub-objects)
258  $res_user[$rec["obj_type"] . "/" . $rec["obj_id"]] = (float) $rec["av"];
259  }
260  }
261 
262  // average for main objects without sub-objects
263  foreach ($tmp as $obj_id => $votes) {
264  $res[$obj_id] = array("avg"=>array_sum($votes)/sizeof($votes),
265  "cnt"=>sizeof($votes));
266  }
267 
268  // file/wiki/lm rating toggles
269 
270  $set = $ilDB->query("SELECT file_id, rating" .
271  " FROM file_data" .
272  " WHERE " . $ilDB->in("file_id", $a_obj_ids, "", 'integer'));
273  while ($row = $ilDB->fetchAssoc($set)) {
274  $id = "file/" . $row["file_id"];
275  if ($row["rating"] && !isset($res[$id])) {
276  $res[$id] = array("avg"=>0, "cnt"=>0);
277  } elseif (!$row["rating"] && isset($res[$id])) {
278  unset($res[$id]);
279  }
280  }
281 
282  $set = $ilDB->query("SELECT id, rating_overall" .
283  " FROM il_wiki_data" .
284  " WHERE " . $ilDB->in("id", $a_obj_ids, "", 'integer'));
285  while ($row = $ilDB->fetchAssoc($set)) {
286  $id = "wiki/" . $row["id"];
287  if ($row["rating_overall"] && !isset($res[$id])) {
288  $res[$id] = array("avg"=>0, "cnt"=>0);
289  } elseif (!$row["rating_overall"] && isset($res[$id])) {
290  unset($res[$id]);
291  }
292  }
293 
294  $set = $ilDB->query("SELECT id, rating" .
295  " FROM content_object" .
296  " WHERE " . $ilDB->in("id", $a_obj_ids, "", 'integer'));
297  while ($row = $ilDB->fetchAssoc($set)) {
298  $id = "lm/" . $row["id"];
299  if ($row["rating"] && !isset($res[$id])) {
300  $res[$id] = array("avg"=>0, "cnt"=>0);
301  } elseif (!$row["rating"] && isset($res[$id])) {
302  unset($res[$id]);
303  }
304  }
305 
306  self::$list_data = array("all"=>$res, "user"=>$res_user);
307  }
308 
309  public static function hasRatingInListGUI($a_obj_id, $a_obj_type)
310  {
311  return isset(self::$list_data["all"][$a_obj_type . "/" . $a_obj_id]);
312  }
313 }
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.
global $DIC
Definition: saml.php:7
if(!array_key_exists('StateId', $_REQUEST)) $id
static hasRatingInListGUI($a_obj_id, $a_obj_type)
static getExportData($a_obj_id, $a_obj_type, array $a_category_ids=null)
Get export data.
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.
foreach($_POST as $key=> $value) $res
static $list_data
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.
$ilUser
Definition: imgupload.php:18
Class ilRating.
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.
Create styles array
The data for the language used.
static preloadListGUIData(array $a_obj_ids)
Preload rating data for list guis.
global $ilDB
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.