ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
16{
17 protected static $list_data; // [array]
18
30 public static function writeRatingForUserAndObject($a_obj_id, $a_obj_type, $a_sub_obj_id, $a_sub_obj_type,
31 $a_user_id, $a_rating, $a_category_id = 0)
32 {
33 global $ilDB;
34
35 if ($a_user_id == ANONYMOUS_USER_ID)
36 {
37 return;
38 }
39
40 if($a_category_id)
41 {
42 $ilDB->manipulate("DELETE FROM il_rating WHERE ".
43 "user_id = ".$ilDB->quote($a_user_id, "integer")." AND ".
44 "obj_id = ".$ilDB->quote((int) $a_obj_id, "integer")." AND ".
45 "obj_type = ".$ilDB->quote($a_obj_type, "text")." AND ".
46 "sub_obj_id = ".$ilDB->quote((int) $a_sub_obj_id, "integer")." AND ".
47 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true)." AND ".
48 "category_id = ".$ilDB->quote(0, "integer"));
49 }
50
51 $ilDB->manipulate("DELETE FROM il_rating WHERE ".
52 "user_id = ".$ilDB->quote($a_user_id, "integer")." AND ".
53 "obj_id = ".$ilDB->quote((int) $a_obj_id, "integer")." AND ".
54 "obj_type = ".$ilDB->quote($a_obj_type, "text")." AND ".
55 "sub_obj_id = ".$ilDB->quote((int) $a_sub_obj_id, "integer")." AND ".
56 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true)." AND ".
57 "category_id = ".$ilDB->quote((int) $a_category_id, "integer"));
58
59 if((int)$a_rating)
60 {
61 $ilDB->manipulate("INSERT INTO il_rating (user_id, obj_id, obj_type,".
62 "sub_obj_id, sub_obj_type, category_id, rating, tstamp) VALUES (".
63 $ilDB->quote($a_user_id, "integer").",".
64 $ilDB->quote((int) $a_obj_id, "integer").",".
65 $ilDB->quote($a_obj_type, "text").",".
66 $ilDB->quote((int) $a_sub_obj_id, "integer").",".
67 $ilDB->quote($a_sub_obj_type, "text").",".
68 $ilDB->quote($a_category_id, "integer").",".
69 $ilDB->quote((int) $a_rating, "integer").",".
70 $ilDB->quote(time(), "integer").")");
71 }
72 }
73
83 public static function resetRatingForUserAndObject($a_obj_id, $a_obj_type, $a_sub_obj_id, $a_sub_obj_type,
84 $a_user_id)
85 {
86 global $ilDB;
87
88 $ilDB->manipulate("DELETE FROM il_rating WHERE ".
89 "user_id = ".$ilDB->quote($a_user_id, "integer")." AND ".
90 "obj_id = ".$ilDB->quote((int) $a_obj_id, "integer")." AND ".
91 "obj_type = ".$ilDB->quote($a_obj_type, "text")." AND ".
92 "sub_obj_id = ".$ilDB->quote((int) $a_sub_obj_id, "integer")." AND ".
93 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true));
94 }
95
96
107 public static function getRatingForUserAndObject($a_obj_id, $a_obj_type, $a_sub_obj_id, $a_sub_obj_type,
108 $a_user_id, $a_category_id = null)
109 {
110 global $ilDB;
111
112 if(is_array(self::$list_data))
113 {
114 return self::$list_data["user"][$a_obj_type."/".$a_obj_id];
115 }
116
117 $q = "SELECT AVG(rating) av FROM il_rating WHERE ".
118 "user_id = ".$ilDB->quote($a_user_id, "integer")." AND ".
119 "obj_id = ".$ilDB->quote((int) $a_obj_id, "integer")." AND ".
120 "obj_type = ".$ilDB->quote($a_obj_type, "text")." AND ".
121 "sub_obj_id = ".$ilDB->quote((int) $a_sub_obj_id, "integer")." AND ".
122 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true);
123 if ($a_category_id !== null)
124 {
125 $q .= " AND category_id = ".$ilDB->quote((int) $a_category_id, "integer");
126 }
127 $set = $ilDB->query($q);
128 $rec = $ilDB->fetchAssoc($set);
129 return $rec["av"];
130 }
131
141 public static function getOverallRatingForObject($a_obj_id, $a_obj_type, $a_sub_obj_id = null, $a_sub_obj_type = null, $a_category_id = null)
142 {
143 global $ilDB;
144
145 if(is_array(self::$list_data))
146 {
147 return self::$list_data["all"][$a_obj_type."/".$a_obj_id];
148 }
149
150 $q = "SELECT AVG(rating) av FROM il_rating".
151 " WHERE obj_id = ".$ilDB->quote((int) $a_obj_id, "integer").
152 " AND obj_type = ".$ilDB->quote($a_obj_type, "text");
153 if($a_sub_obj_id)
154 {
155 $q .= " AND sub_obj_id = ".$ilDB->quote((int) $a_sub_obj_id, "integer").
156 " AND ".$ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true);
157 }
158 else
159 {
160 $q .= " AND sub_obj_type = ".$ilDB->quote("-", "text"); // #13913
161 }
162
163 if ($a_category_id !== null)
164 {
165 $q .= " AND category_id = ".$ilDB->quote((int) $a_category_id, "integer");
166 }
167 $q .= " GROUP BY user_id";
168 $set = $ilDB->query($q);
169 $avg = $cnt = 0;
170 while($rec = $ilDB->fetchAssoc($set))
171 {
172 $cnt++;
173 $avg += $rec["av"];
174 }
175 if ($cnt > 0)
176 {
177 $avg = $avg/$cnt;
178 }
179 else
180 {
181 $avg = 0;
182 }
183 return array("cnt" => $cnt, "avg" => $avg);
184 }
185
194 public static function getExportData($a_obj_id, $a_obj_type, array $a_category_ids = null)
195 {
196 global $ilDB;
197
198 $res = array();
199 $q = "SELECT sub_obj_id, sub_obj_type, rating, category_id, user_id, tstamp ".
200 "FROM il_rating WHERE ".
201 "obj_id = ".$ilDB->quote((int) $a_obj_id, "integer")." AND ".
202 "obj_type = ".$ilDB->quote($a_obj_type, "text").
203 " ORDER BY tstamp";
204 if($a_category_ids)
205 {
206 $q .= " AND ".$ilDB->in("category_id", $a_category_ids, "", "integer");
207 }
208 $set = $ilDB->query($q);
209 while($row = $ilDB->fetchAssoc($set))
210 {
211 $res[] = $row;
212 }
213 return $res;
214 }
215
221 public static function preloadListGUIData(array $a_obj_ids)
222 {
223 global $ilDB, $ilUser;
224
225 $tmp = $res = $tmp_user = $res_user = array();
226
227 // collapse by categories
228 $q = "SELECT obj_id, obj_type, user_id, AVG(rating) av".
229 " FROM il_rating".
230 " WHERE ".$ilDB->in("obj_id", $a_obj_ids, "", "integer").
231 " AND sub_obj_id = ".$ilDB->quote(0, "integer").
232 " GROUP BY obj_id, obj_type, user_id";
233 $set = $ilDB->query($q);
234 while($rec = $ilDB->fetchAssoc($set))
235 {
236 $tmp[$rec["obj_type"]."/".$rec["obj_id"]][$rec["user_id"]] = (float)$rec["av"];
237 if($rec["user_id"] == $ilUser->getId())
238 {
239 // add final average to user result (no sub-objects)
240 $res_user[$rec["obj_type"]."/".$rec["obj_id"]] = (float)$rec["av"];
241 }
242 }
243
244 // average for main objects without sub-objects
245 foreach($tmp as $obj_id => $votes)
246 {
247 $res[$obj_id] = array("avg"=>array_sum($votes)/sizeof($votes),
248 "cnt"=>sizeof($votes));
249 }
250
251 // file/wiki/lm rating toggles
252
253 $set = $ilDB->query("SELECT file_id, rating".
254 " FROM file_data".
255 " WHERE ".$ilDB->in("file_id", $a_obj_ids, "", integer));
256 while($row = $ilDB->fetchAssoc($set))
257 {
258 $id = "file/".$row["file_id"];
259 if($row["rating"] && !isset($res[$id]))
260 {
261 $res[$id] = array("avg"=>0, "cnt"=>0);
262 }
263 else if(!$row["rating"] && isset($res[$id]))
264 {
265 unset($res[$id]);
266 }
267 }
268
269 $set = $ilDB->query("SELECT id, rating_overall".
270 " FROM il_wiki_data".
271 " WHERE ".$ilDB->in("id", $a_obj_ids, "", integer));
272 while($row = $ilDB->fetchAssoc($set))
273 {
274 $id = "wiki/".$row["id"];
275 if($row["rating_overall"] && !isset($res[$id]))
276 {
277 $res[$id] = array("avg"=>0, "cnt"=>0);
278 }
279 else if(!$row["rating_overall"] && isset($res[$id]))
280 {
281 unset($res[$id]);
282 }
283 }
284
285 $set = $ilDB->query("SELECT id, rating".
286 " FROM content_object".
287 " WHERE ".$ilDB->in("id", $a_obj_ids, "", integer));
288 while($row = $ilDB->fetchAssoc($set))
289 {
290 $id = "lm/".$row["id"];
291 if($row["rating"] && !isset($res[$id]))
292 {
293 $res[$id] = array("avg"=>0, "cnt"=>0);
294 }
295 else if(!$row["rating"] && isset($res[$id]))
296 {
297 unset($res[$id]);
298 }
299 }
300
301 self::$list_data = array("all"=>$res, "user"=>$res_user);
302 }
303
304 public static function hasRatingInListGUI($a_obj_id, $a_obj_type)
305 {
306 return isset(self::$list_data["all"][$a_obj_type."/".$a_obj_id]);
307 }
308}
309
310?>
Class ilRating.
static hasRatingInListGUI($a_obj_id, $a_obj_type)
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.
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.
static preloadListGUIData(array $a_obj_ids)
Preload rating data for list guis.
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.
static $list_data
static getExportData($a_obj_id, $a_obj_type, array $a_category_ids=null)
Get export 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.
global $ilDB
global $ilUser
Definition: imgupload.php:15