ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLikeData.php
Go to the documentation of this file.
1<?php
2
31{
32 public const TYPE_LIKE = 0;
33 public const TYPE_DISLIKE = 1;
34 public const TYPE_LOVE = 2;
35 public const TYPE_LAUGH = 3;
36 public const TYPE_ASTOUNDED = 4;
37 public const TYPE_SAD = 5;
38 public const TYPE_ANGRY = 6;
39
40 protected array $data = array();
41 protected ilDBInterface $db;
42 protected ilLanguage $lng;
43
44 public function __construct(
45 array $a_obj_ids = array(),
46 ?ilDBInterface $db = null,
47 ?ilLanguage $lng = null
48 ) {
49 global $DIC;
50
51 $this->db = ($db == null)
52 ? $DIC->database()
53 : $db;
54
55 $this->lng = ($lng == null)
56 ? $DIC->language()
57 : $lng;
58 $this->loadDataForObjects($a_obj_ids);
59 $this->lng->loadLanguageModule("like");
60 }
61
65 public function getExpressionTypes(): array
66 {
67 return array(
68 self::TYPE_LIKE => $this->lng->txt("like_like"),
69 self::TYPE_DISLIKE => $this->lng->txt("like_dislike"),
70 self::TYPE_LOVE => $this->lng->txt("like_love"),
71 self::TYPE_LAUGH => $this->lng->txt("like_laugh"),
72 self::TYPE_ASTOUNDED => $this->lng->txt("like_astounded"),
73 self::TYPE_SAD => $this->lng->txt("like_sad"),
74 self::TYPE_ANGRY => $this->lng->txt("like_angry")
75 );
76 }
77
78
90 public function addExpression(
91 int $a_user_id,
92 int $a_like_type,
93 int $a_obj_id,
94 string $a_obj_type,
95 int $a_sub_obj_id = 0,
96 string $a_sub_obj_type = "",
97 int $a_news_id = 0
98 ): void {
99 $ilDB = $this->db;
100
101 if ($a_user_id == ANONYMOUS_USER_ID) {
102 return;
103 }
104
105 $this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id] = 1;
106
107 if ($a_sub_obj_type == "") {
108 $a_sub_obj_type = "-";
109 }
110
111 $ilDB->replace(
112 "like_data",
113 array(
114 "user_id" => array("integer", $a_user_id),
115 "obj_id" => array("integer", $a_obj_id),
116 "obj_type" => array("text", $a_obj_type),
117 "sub_obj_id" => array("integer", $a_sub_obj_id),
118 "sub_obj_type" => array("text", $a_sub_obj_type),
119 "news_id" => array("integer", $a_news_id),
120 "like_type" => array("integer", $a_like_type)
121 ),
122 array(
123 "exp_ts" => array("timestamp", ilUtil::now())
124 )
125 );
126 }
127
139 public function removeExpression(
140 int $a_user_id,
141 int $a_like_type,
142 int $a_obj_id,
143 string $a_obj_type,
144 int $a_sub_obj_id = 0,
145 string $a_sub_obj_type = "",
146 int $a_news_id = 0
147 ): void {
148 $ilDB = $this->db;
149
150 if ($a_user_id == ANONYMOUS_USER_ID) {
151 return;
152 }
153
154 if (isset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id])) {
155 unset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id]);
156 }
157
158 if ($a_sub_obj_type == "") {
159 $a_sub_obj_type = "-";
160 }
161
162 $ilDB->manipulate(
163 "DELETE FROM like_data WHERE " .
164 " user_id = " . $ilDB->quote($a_user_id, "integer") .
165 " AND obj_id = " . $ilDB->quote($a_obj_id, "integer") .
166 " AND obj_type = " . $ilDB->quote($a_obj_type, "text") .
167 " AND sub_obj_id = " . $ilDB->quote($a_sub_obj_id, "integer") .
168 " AND sub_obj_type = " . $ilDB->quote($a_sub_obj_type, "text") .
169 " AND news_id = " . $ilDB->quote($a_news_id, "integer") .
170 " AND like_type = " . $ilDB->quote($a_like_type, "integer")
171 );
172 }
173
178 protected function loadDataForObjects(
179 array $a_obj_ids = array()
180 ): void {
181 $ilDB = $this->db;
182
183 foreach ($a_obj_ids as $id) {
184 $this->data[$id] = array();
185 }
186
187 $set = $ilDB->query("SELECT * FROM like_data " .
188 " WHERE " . $ilDB->in("obj_id", $a_obj_ids, false, "integer") .
189 " ORDER by exp_ts DESC");
190 while ($rec = $ilDB->fetchAssoc($set)) {
191 $subtype = $rec["sub_obj_type"] == "-"
192 ? ""
193 : $rec["sub_obj_type"];
194 $this->data[$rec["obj_id"]][$rec["sub_obj_id"]][$subtype][$rec["news_id"]][$rec["like_type"]][$rec["user_id"]] =
195 $rec["exp_ts"];
196 }
197 }
198
210 public function getExpressionCounts(
211 int $obj_id,
212 string $obj_type,
213 int $sub_obj_id,
214 string $sub_obj_type,
215 int $news_id
216 ): array {
217 if (!is_array($this->data[$obj_id])) {
218 throw new ilLikeDataException("No data loaded for object $obj_id.");
219 }
220
221 if ($sub_obj_type == "-") {
222 $sub_obj_type = "";
223 }
224
225 $cnt = array();
226 foreach ($this->getExpressionTypes() as $k => $txt) {
227 $cnt[$k] = 0;
228 if (isset($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k])) {
229 $cnt[$k] = count($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k]);
230 }
231 }
232 return $cnt;
233 }
234
247 public function isExpressionSet(
248 int $a_user_id,
249 int $a_like_type,
250 int $a_obj_id,
251 string $a_obj_type,
252 int $a_sub_obj_id = 0,
253 string $a_sub_obj_type = "",
254 int $a_news_id = 0
255 ): bool {
256 if (isset($this->data[$a_obj_id][$a_sub_obj_id][$a_sub_obj_type][$a_news_id][$a_like_type][$a_user_id])) {
257 return true;
258 }
259 return false;
260 }
261
266 public function getExpressionEntries(
267 int $obj_id,
268 string $obj_type,
269 int $sub_obj_id,
270 string $sub_obj_type,
271 int $news_id
272 ): array {
273 if (!is_array($this->data[$obj_id])) {
274 throw new ilLikeDataException("No data loaded for object $obj_id.");
275 }
276
277 if ($sub_obj_type == "-") {
278 $sub_obj_type = "";
279 }
280
281 $exp = array();
282 foreach ($this->getExpressionTypes() as $k => $txt) {
283 if (isset($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k])) {
284 foreach ($this->data[$obj_id][$sub_obj_id][$sub_obj_type][$news_id][$k] as $user => $ts) {
285 $exp[] = array(
286 "expression" => $k,
287 "user_id" => $user,
288 "timestamp" => $ts
289 );
290 }
291 }
292 }
293
294 $exp = ilArrayUtil::sortArray($exp, "timestamp", "desc");
295 return $exp;
296 }
297
307 int $obj_id,
308 ?string $since_ts = null
309 ): array {
310 if (!is_array($this->data[$obj_id])) {
311 throw new ilLikeDataException("No data loaded for object $obj_id.");
312 }
313 $exp = array();
314 foreach ($this->data[$obj_id] as $sub_obj_id => $si) {
315 foreach ($si as $sub_obj_type => $so) {
316 foreach ($so as $news_id => $ni) {
317 foreach ($ni as $exp_type => $entry) {
318 foreach ($entry as $user => $ts) {
319 if ($since_ts == null || $ts > $since_ts) {
320 $exp[] = array(
321 "sub_obj_id" => $sub_obj_id,
322 "sub_obj_type" => $sub_obj_type,
323 "news_id" => $news_id,
324 "expression" => $exp_type,
325 "user_id" => $user,
326 "timestamp" => $ts
327 );
328 }
329 }
330 }
331 }
332 }
333 }
334
335 $exp = ilArrayUtil::sortArray($exp, "timestamp", "desc");
336 return $exp;
337 }
338}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilDBInterface $db
getExpressionCounts(int $obj_id, string $obj_type, int $sub_obj_id, string $sub_obj_type, int $news_id)
Get expression counts for obj/subobj/news.
const TYPE_ASTOUNDED
__construct(array $a_obj_ids=array(), ?ilDBInterface $db=null, ?ilLanguage $lng=null)
getExpressionTypes()
Get types.
addExpression(int $a_user_id, int $a_like_type, int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
Add expression for a user and object.
removeExpression(int $a_user_id, int $a_like_type, int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
Remove expression for a user and object.
getExpressionEntriesForObject(int $obj_id, ?string $since_ts=null)
Get expression entries for obj/subobj/news.
isExpressionSet(int $a_user_id, int $a_like_type, int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
Is expression set for a user and object?
ilLanguage $lng
getExpressionEntries(int $obj_id, string $obj_type, int $sub_obj_id, string $sub_obj_type, int $news_id)
Get expression entries for obj/subobj/news.
loadDataForObjects(array $a_obj_ids=array())
Load data (for objects)
static now()
Return current timestamp in Y-m-d H:i:s format.
const ANONYMOUS_USER_ID
Definition: constants.php:27
$txt
Definition: error.php:31
Interface ilDBInterface.
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26