ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBadgeAssignment.php
Go to the documentation of this file.
1<?php
2
25{
26 protected ilDBInterface $db;
27 protected int $badge_id = 0;
28 protected int $user_id = 0;
29 protected int $tstamp = 0; // unix timestamp
30 protected int $awarded_by = 0;
31 protected ?int $pos = null;
32 protected bool $stored = false;
33
34 public function __construct(
35 ?int $a_badge_id = null,
36 ?int $a_user_id = null
37 ) {
38 global $DIC;
39
40 $this->db = $DIC->database();
41 if ($a_badge_id &&
42 $a_user_id) {
43 $this->setBadgeId($a_badge_id);
44 $this->setUserId($a_user_id);
45
46 $this->read($a_badge_id, $a_user_id);
47 }
48 }
49
50 public static function getNewCounter(
51 int $a_user_id
52 ): int {
53 global $DIC;
54
55 $db = $DIC->database();
56
57 $user = new ilObjUser($a_user_id);
58 $noti_repo = new \ILIAS\Badge\Notification\BadgeNotificationPrefRepository($user);
59
60 $last = $noti_repo->getLastCheckedTimestamp();
61
62
63 // if no last check exists, we use last 24 hours
64 if ($last === 0) {
65 $last = time() - (24 * 60 * 60);
66 }
67
68 if ($last > 0) {
69 $set = $db->queryF(
70 "SELECT count(*) cnt FROM badge_user_badge " .
71 " WHERE user_id = %s AND tstamp >= %s",
72 ["integer", "integer"],
73 [$a_user_id, $last]
74 );
75 $rec = $db->fetchAssoc($set);
76 return (int) $rec["cnt"];
77 }
78 return 0;
79 }
80
81 public static function getLatestTimestamp(
82 int $a_user_id
83 ): int {
84 global $DIC;
85
86 $db = $DIC->database();
87
88 $set = $db->queryF(
89 "SELECT max(tstamp) maxts FROM badge_user_badge " .
90 " WHERE user_id = %s",
91 ["integer"],
92 [$a_user_id]
93 );
94 $rec = $db->fetchAssoc($set);
95 return (int) $rec["maxts"];
96 }
97
101 public static function getInstancesByUserId(
102 int $a_user_id
103 ): array {
104 global $DIC;
105
106 $ilDB = $DIC->database();
107
108 $res = array();
109
110 $set = $ilDB->query("SELECT * FROM badge_user_badge" .
111 " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
112 " ORDER BY pos");
113 while ($row = $ilDB->fetchAssoc($set)) {
114 $obj = new self();
115 $obj->importDBRow($row);
116 $res[] = $obj;
117 }
118
119 return $res;
120 }
121
125 public static function getInstancesByBadgeId(
126 int $a_badge_id
127 ): array {
128 global $DIC;
129
130 $ilDB = $DIC->database();
131
132 $res = array();
133
134 $set = $ilDB->query("SELECT * FROM badge_user_badge" .
135 " WHERE badge_id = " . $ilDB->quote($a_badge_id, "integer"));
136 while ($row = $ilDB->fetchAssoc($set)) {
137 $obj = new self();
138 $obj->importDBRow($row);
139 $res[] = $obj;
140 }
141
142 return $res;
143 }
144
148 public static function getInstancesByParentId(
149 int $a_parent_obj_id
150 ): array {
151 global $DIC;
152
153 $ilDB = $DIC->database();
154
155 $res = array();
156
157 $badge_ids = array();
158 foreach (ilBadge::getInstancesByParentId($a_parent_obj_id) as $badge) {
159 $badge_ids[] = $badge->getId();
160 }
161 if (count($badge_ids)) {
162 $set = $ilDB->query("SELECT * FROM badge_user_badge" .
163 " WHERE " . $ilDB->in("badge_id", $badge_ids, "", "integer"));
164 while ($row = $ilDB->fetchAssoc($set)) {
165 $obj = new self();
166 $obj->importDBRow($row);
167 $res[] = $obj;
168 }
169 }
170
171 return $res;
172 }
173
177 public static function getAssignedUsers(
178 int $a_badge_id
179 ): array {
180 $res = [];
181
182 foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
183 $res[] = $ass->getUserId();
184 }
185
186 return $res;
187 }
188
189 public static function exists(
190 int $a_badge_id,
191 int $a_user_id
192 ): bool {
193 $obj = new self($a_badge_id, $a_user_id);
194 return $obj->stored;
195 }
196
197
198 //
199 // setter/getter
200 //
201
202 protected function setBadgeId(int $a_value): void
203 {
204 $this->badge_id = $a_value;
205 }
206
207 public function getBadgeId(): int
208 {
209 return $this->badge_id;
210 }
211
212 protected function setUserId(int $a_value): void
213 {
214 $this->user_id = $a_value;
215 }
216
217 public function getUserId(): int
218 {
219 return $this->user_id;
220 }
221
222 protected function setTimestamp(int $a_value): void
223 {
224 $this->tstamp = $a_value;
225 }
226
227 public function getTimestamp(): int
228 {
229 return $this->tstamp;
230 }
231
232 public function setAwardedBy(int $a_id): void
233 {
234 $this->awarded_by = $a_id;
235 }
236
237 public function getAwardedBy(): int
238 {
239 return $this->awarded_by;
240 }
241
242 public function setPosition(?int $a_value): void
243 {
244 $this->pos = $a_value;
245 }
246
247 public function getPosition(): ?int
248 {
249 return $this->pos;
250 }
251
252
253 //
254 // crud
255 //
256
257 protected function importDBRow(array $a_row): void
258 {
259 $this->stored = true;
260 $this->setBadgeId((int) $a_row["badge_id"]);
261 $this->setUserId((int) $a_row["user_id"]);
262 $this->setTimestamp((int) $a_row["tstamp"]);
263 $this->setAwardedBy((int) $a_row["awarded_by"]);
264 $this->setPosition($a_row["pos"]);
265 }
266
267 protected function read(
268 int $a_badge_id,
269 int $a_user_id
270 ): void {
271 $ilDB = $this->db;
272
273 $set = $ilDB->query("SELECT * FROM badge_user_badge" .
274 " WHERE badge_id = " . $ilDB->quote($a_badge_id, "integer") .
275 " AND user_id = " . $ilDB->quote($a_user_id, "integer"));
276 $row = $ilDB->fetchAssoc($set);
277 if ($row && $row["user_id"]) {
278 $this->importDBRow($row);
279 }
280 }
281
285 protected function getPropertiesForStorage(): array
286 {
287 return [
288 "tstamp" => ["integer", $this->stored ? $this->getTimestamp() : time()],
289 "awarded_by" => ["integer", $this->getAwardedBy()],
290 "pos" => ["integer", $this->getPosition()]
291 ];
292 }
293
294 public function store(): void
295 {
296 $ilDB = $this->db;
297
298 if (!$this->getBadgeId() ||
299 !$this->getUserId()) {
300 return;
301 }
302
303 $keys = array(
304 "badge_id" => array("integer", $this->getBadgeId()),
305 "user_id" => array("integer", $this->getUserId())
306 );
307 $fields = $this->getPropertiesForStorage();
308
309 if (!$this->stored) {
310 $ilDB->insert("badge_user_badge", $fields + $keys);
311 } else {
312 $ilDB->update("badge_user_badge", $fields, $keys);
313 }
314 }
315
316 public function delete(): void
317 {
318 $ilDB = $this->db;
319
320 if (!$this->getBadgeId() ||
321 !$this->getUserId()) {
322 return;
323 }
324
325 $this->deleteStaticFiles();
326
327 $ilDB->manipulate("DELETE FROM badge_user_badge" .
328 " WHERE badge_id = " . $ilDB->quote($this->getBadgeId(), "integer") .
329 " AND user_id = " . $ilDB->quote($this->getUserId(), "integer"));
330 }
331
332 public static function deleteByUserId(int $a_user_id): void
333 {
334 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
335 $ass->delete();
336 }
337 }
338
339 public static function deleteByBadgeId(int $a_badge_id): void
340 {
341 foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
342 $ass->delete();
343 }
344 }
345
346 public static function deleteByParentId(int $a_parent_obj_id): void
347 {
348 foreach (self::getInstancesByParentId($a_parent_obj_id) as $ass) {
349 $ass->delete();
350 }
351 }
352
353 public static function updatePositions(
354 int $a_user_id,
355 array $a_positions
356 ): void {
357 $existing = array();
358 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
359 $badge = new ilBadge($ass->getBadgeId());
360 $existing[$badge->getId()] = array($badge->getTitle(), $ass);
361 }
362
363 $new_pos = 0;
364 foreach ($a_positions as $title) {
365 foreach ($existing as $id => $item) {
366 if ($title == $item[0]) {
367 $item[1]->setPosition(++$new_pos);
368 $item[1]->store();
369 unset($existing[$id]);
370 }
371 }
372 }
373 }
374
378 public static function getBadgesForUser(
379 int $a_user_id,
380 int $a_ts_from,
381 int $a_ts_to
382 ): array {
383 global $DIC;
384
385 $db = $DIC->database();
386
387 $set = $db->queryF(
388 "SELECT bdg.parent_id, ub.tstamp, bdg.title FROM badge_user_badge ub JOIN badge_badge bdg" .
389 " ON (ub.badge_id = bdg.id) " .
390 " WHERE ub.user_id = %s AND ub.tstamp >= %s AND ub.tstamp <= %s",
391 array("integer","integer","integer"),
392 array($a_user_id, $a_ts_from, $a_ts_to)
393 );
394 $res = [];
395 while ($rec = $db->fetchAssoc($set)) {
396 $res[] = $rec;
397 }
398 return $res;
399 }
400
401 public function deleteStaticFiles(): void
402 {
403 // remove instance files
404 $path = ilBadgeHandler::getInstance()->getInstancePath($this);
405 $path = str_replace(".json", ".*", $path);
406 array_map("unlink", glob($path));
407 }
408
409 public static function clearBadgeCache(
410 int $a_user_id
411 ): void {
412 foreach (self::getInstancesByUserId($a_user_id) as $ass) {
413 $ass->deleteStaticFiles();
414 }
415 }
416}
$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 getNewCounter(int $a_user_id)
static clearBadgeCache(int $a_user_id)
static deleteByUserId(int $a_user_id)
static deleteByBadgeId(int $a_badge_id)
static getInstancesByBadgeId(int $a_badge_id)
static getAssignedUsers(int $a_badge_id)
read(int $a_badge_id, int $a_user_id)
static updatePositions(int $a_user_id, array $a_positions)
static getInstancesByUserId(int $a_user_id)
static getInstancesByParentId(int $a_parent_obj_id)
static exists(int $a_badge_id, int $a_user_id)
__construct(?int $a_badge_id=null, ?int $a_user_id=null)
static deleteByParentId(int $a_parent_obj_id)
static getLatestTimestamp(int $a_user_id)
static getBadgesForUser(int $a_user_id, int $a_ts_from, int $a_ts_to)
static getInstancesByParentId(int $a_parent_id, ?array $a_filter=null)
User class.
Interface ilDBInterface.
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
$path
Definition: ltiservices.php:30
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26