ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilRating.php
Go to the documentation of this file.
1<?php
2
25{
26 protected static array $list_data = [];
27
39 public static function writeRatingForUserAndObject(
40 int $a_obj_id,
41 string $a_obj_type,
42 ?int $a_sub_obj_id,
43 ?string $a_sub_obj_type,
44 int $a_user_id,
45 int $a_rating,
46 int $a_category_id = 0
47 ): void {
48 global $DIC;
49
50 $ilDB = $DIC->database();
51
52 if ($a_rating < 0) {
53 $a_rating = 0;
54 }
55
56 if ($a_rating > 5) {
57 $a_rating = 5;
58 }
59
60 if ($a_user_id == ANONYMOUS_USER_ID) {
61 return;
62 }
63
64 if ($a_category_id) {
65 $ilDB->manipulate("DELETE FROM il_rating WHERE " .
66 "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
67 "obj_id = " . $ilDB->quote($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(0, "integer"));
72 }
73
74 $ilDB->manipulate("DELETE FROM il_rating WHERE " .
75 "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
76 "obj_id = " . $ilDB->quote($a_obj_id, "integer") . " AND " .
77 "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
78 "sub_obj_id = " . $ilDB->quote((int) $a_sub_obj_id, "integer") . " AND " .
79 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true) . " AND " .
80 "category_id = " . $ilDB->quote($a_category_id, "integer"));
81
82 if ($a_rating) {
83 $ilDB->manipulate("INSERT INTO il_rating (user_id, obj_id, obj_type," .
84 "sub_obj_id, sub_obj_type, category_id, rating, tstamp) VALUES (" .
85 $ilDB->quote($a_user_id, "integer") . "," .
86 $ilDB->quote($a_obj_id, "integer") . "," .
87 $ilDB->quote($a_obj_type, "text") . "," .
88 $ilDB->quote((int) $a_sub_obj_id, "integer") . "," .
89 $ilDB->quote($a_sub_obj_type, "text") . "," .
90 $ilDB->quote($a_category_id, "integer") . "," .
91 $ilDB->quote($a_rating, "integer") . "," .
92 $ilDB->quote(time(), "integer") . ")");
93 }
94 }
95
105 public static function resetRatingForUserAndObject(
106 int $a_obj_id,
107 string $a_obj_type,
108 int $a_sub_obj_id,
109 string $a_sub_obj_type,
110 int $a_user_id
111 ): void {
112 global $DIC;
113
114 $ilDB = $DIC->database();
115
116 $ilDB->manipulate("DELETE FROM il_rating WHERE " .
117 "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
118 "obj_id = " . $ilDB->quote($a_obj_id, "integer") . " AND " .
119 "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
120 "sub_obj_id = " . $ilDB->quote($a_sub_obj_id, "integer") . " AND " .
121 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true));
122 }
123
124
135 public static function getRatingForUserAndObject(
136 int $a_obj_id,
137 string $a_obj_type,
138 int $a_sub_obj_id,
139 string $a_sub_obj_type,
140 int $a_user_id,
141 ?int $a_category_id = null
142 ): float {
143 global $DIC;
144
145 $ilDB = $DIC->database();
146
147 if (isset(self::$list_data["user"][$a_obj_type . "/" . $a_obj_id])) {
148 return self::$list_data["user"][$a_obj_type . "/" . $a_obj_id] ?? 0;
149 }
150
151 $q = "SELECT AVG(rating) av FROM il_rating WHERE " .
152 "user_id = " . $ilDB->quote($a_user_id, "integer") . " AND " .
153 "obj_id = " . $ilDB->quote($a_obj_id, "integer") . " AND " .
154 "obj_type = " . $ilDB->quote($a_obj_type, "text") . " AND " .
155 "sub_obj_id = " . $ilDB->quote($a_sub_obj_id, "integer") . " AND " .
156 $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true);
157 if ($a_category_id !== null) {
158 $q .= " AND category_id = " . $ilDB->quote($a_category_id, "integer");
159 }
160 $set = $ilDB->query($q);
161 $rec = $ilDB->fetchAssoc($set);
162 return (float) $rec["av"];
163 }
164
166 int $a_obj_id,
167 int $sub_obj_id,
168 string $sub_obj_type,
169 array $all_category_ids
170 ): int {
171 global $DIC;
172
173 $ilDB = $DIC->database();
174 if (empty($all_category_ids)) {
175 return 0;
176 }
177 $category_count = count($all_category_ids);
178 $category_ids_csv = implode(",", array_map("intval", $all_category_ids));
179
180 $query = "
181 SELECT COUNT(*) cnt FROM (
182 SELECT user_id
183 FROM il_rating
184 WHERE obj_id = " . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER) . "
185 AND obj_type = " . $ilDB->quote("wiki", ilDBConstants::T_TEXT) . "
186 AND sub_obj_id = " . $ilDB->quote($sub_obj_id, ilDBConstants::T_INTEGER) . "
187 AND sub_obj_type = " . $ilDB->quote($sub_obj_type, ilDBConstants::T_TEXT) . "
188 AND category_id IN (" . $category_ids_csv . ")
189 GROUP BY user_id
190 HAVING count(DISTINCT category_id) = " . $ilDB->quote($category_count, "integer") . "
191 ) full_users
192 ";
193
194 $set = $ilDB->query($query);
195 $rec = $ilDB->fetchAssoc($set);
196 return (int) ($rec["cnt"] ?? 0);
197 }
198
208 public static function getOverallRatingForObject(
209 int $a_obj_id,
210 string $a_obj_type,
211 ?int $a_sub_obj_id = null,
212 ?string $a_sub_obj_type = null,
213 ?int $a_category_id = null
214 ): array {
215 global $DIC;
216
217 $ilDB = $DIC->database();
218
219 if (isset(self::$list_data["all"][$a_obj_type . "/" . $a_obj_id])) {
220 return self::$list_data["all"][$a_obj_type . "/" . $a_obj_id];
221 }
222
223 $q = "SELECT AVG(rating) av FROM il_rating" .
224 " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer") .
225 " AND obj_type = " . $ilDB->quote($a_obj_type, "text");
226 if ($a_sub_obj_id) {
227 $q .= " AND sub_obj_id = " . $ilDB->quote($a_sub_obj_id, "integer") .
228 " AND " . $ilDB->equals("sub_obj_type", $a_sub_obj_type, "text", true);
229 } else {
230 $q .= " AND sub_obj_type = " . $ilDB->quote("-", "text"); // #13913
231 }
232
233 if ($a_category_id !== null) {
234 $q .= " AND category_id = " . $ilDB->quote($a_category_id, "integer");
235 }
236 $q .= " GROUP BY user_id";
237 $set = $ilDB->query($q);
238 $avg = $cnt = 0;
239 while ($rec = $ilDB->fetchAssoc($set)) {
240 $cnt++;
241 $avg += $rec["av"];
242 }
243 if ($cnt > 0) {
244 $avg = $avg / $cnt;
245 } else {
246 $avg = 0;
247 }
248 return array("cnt" => $cnt, "avg" => $avg);
249 }
250
259 public static function getExportData(
260 int $a_obj_id,
261 string $a_obj_type,
262 ?array $a_category_ids = null
263 ): array {
264 global $DIC;
265
266 $ilDB = $DIC->database();
267
268 $res = array();
269 $q = "SELECT sub_obj_id, sub_obj_type, rating, category_id, user_id, tstamp " .
270 "FROM il_rating WHERE " .
271 "obj_id = " . $ilDB->quote($a_obj_id, "integer") . " AND " .
272 "obj_type = " . $ilDB->quote($a_obj_type, "text") .
273 " ORDER BY tstamp";
274 if ($a_category_ids) {
275 $q .= " AND " . $ilDB->in("category_id", $a_category_ids, "", "integer");
276 }
277 $set = $ilDB->query($q);
278 while ($row = $ilDB->fetchAssoc($set)) {
279 $res[] = $row;
280 }
281 return $res;
282 }
283
289 public static function preloadListGUIData(
290 array $a_obj_ids
291 ): void {
292 global $DIC;
293
294 $ilDB = $DIC->database();
295 $ilUser = $DIC->user();
296
297 $tmp = $res = $res_user = array();
298
299 // collapse by categories
300 $q = "SELECT obj_id, obj_type, user_id, AVG(rating) av" .
301 " FROM il_rating" .
302 " WHERE " . $ilDB->in("obj_id", $a_obj_ids, "", "integer") .
303 " AND sub_obj_id = " . $ilDB->quote(0, "integer") .
304 " GROUP BY obj_id, obj_type, user_id";
305 $set = $ilDB->query($q);
306 while ($rec = $ilDB->fetchAssoc($set)) {
307 $tmp[$rec["obj_type"] . "/" . $rec["obj_id"]][$rec["user_id"]] = (float) $rec["av"];
308 if ($rec["user_id"] == $ilUser->getId()) {
309 // add final average to user result (no sub-objects)
310 $res_user[$rec["obj_type"] . "/" . $rec["obj_id"]] = (float) $rec["av"];
311 }
312 }
313
314 // average for main objects without sub-objects
315 foreach ($tmp as $obj_id => $votes) {
316 $res[$obj_id] = array("avg" => array_sum($votes) / sizeof($votes),
317 "cnt" => sizeof($votes));
318 }
319
320 // file/wiki/lm rating toggles
321
322 $set = $ilDB->query("SELECT file_id, rating" .
323 " FROM file_data" .
324 " WHERE " . $ilDB->in("file_id", $a_obj_ids, "", 'integer'));
325 while ($row = $ilDB->fetchAssoc($set)) {
326 $id = "file/" . $row["file_id"];
327 if ($row["rating"] && !isset($res[$id])) {
328 $res[$id] = array("avg" => 0, "cnt" => 0);
329 } elseif (!$row["rating"] && isset($res[$id])) {
330 unset($res[$id]);
331 }
332 }
333
334 $set = $ilDB->query("SELECT id, rating_overall" .
335 " FROM il_wiki_data" .
336 " WHERE " . $ilDB->in("id", $a_obj_ids, "", 'integer'));
337 while ($row = $ilDB->fetchAssoc($set)) {
338 $id = "wiki/" . $row["id"];
339 if ($row["rating_overall"] && !isset($res[$id])) {
340 $res[$id] = array("avg" => 0, "cnt" => 0);
341 } elseif (!$row["rating_overall"] && isset($res[$id])) {
342 unset($res[$id]);
343 }
344 }
345
346 $set = $ilDB->query("SELECT id, rating" .
347 " FROM content_object" .
348 " WHERE " . $ilDB->in("id", $a_obj_ids, "", 'integer'));
349 while ($row = $ilDB->fetchAssoc($set)) {
350 $id = "lm/" . $row["id"];
351 if ($row["rating"] && !isset($res[$id])) {
352 $res[$id] = array("avg" => 0, "cnt" => 0);
353 } elseif (!$row["rating"] && isset($res[$id])) {
354 unset($res[$id]);
355 }
356 }
357
358 self::$list_data = array("all" => $res, "user" => $res_user);
359 }
360
361 public static function hasRatingInListGUI(
362 int $a_obj_id,
363 string $a_obj_type
364 ): bool {
365 return isset(self::$list_data["all"][$a_obj_type . "/" . $a_obj_id]);
366 }
367}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getNumberOfFinishedCategoryRatingsForWikis(int $a_obj_id, int $sub_obj_id, string $sub_obj_type, array $all_category_ids)
static preloadListGUIData(array $a_obj_ids)
Preload rating data for list guis.
static resetRatingForUserAndObject(int $a_obj_id, string $a_obj_type, int $a_sub_obj_id, string $a_sub_obj_type, int $a_user_id)
Reset rating for a user and an object.
static getRatingForUserAndObject(int $a_obj_id, string $a_obj_type, int $a_sub_obj_id, string $a_sub_obj_type, int $a_user_id, ?int $a_category_id=null)
Get rating for a user and an object.
static hasRatingInListGUI(int $a_obj_id, string $a_obj_type)
static array $list_data
static writeRatingForUserAndObject(int $a_obj_id, string $a_obj_type, ?int $a_sub_obj_id, ?string $a_sub_obj_type, int $a_user_id, int $a_rating, int $a_category_id=0)
Write rating for a user and an object.
static getOverallRatingForObject(int $a_obj_id, string $a_obj_type, ?int $a_sub_obj_id=null, ?string $a_sub_obj_type=null, ?int $a_category_id=null)
Get overall rating for an object.
static getExportData(int $a_obj_id, string $a_obj_type, ?array $a_category_ids=null)
Get export data.
const ANONYMOUS_USER_ID
Definition: constants.php:27
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'vtt', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach($_POST as $key=> $value) $res
global $DIC
Definition: shib_login.php:26