ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
102  public static function getInstancesByUserId(
103  int $a_user_id
104  ): array {
105  global $DIC;
106 
107  $ilDB = $DIC->database();
108 
109  $res = array();
110 
111  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
112  " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
113  " ORDER BY pos");
114  while ($row = $ilDB->fetchAssoc($set)) {
115  $obj = new self();
116  $obj->importDBRow($row);
117  $res[] = $obj;
118  }
119 
120  return $res;
121  }
122 
127  public static function getInstancesByBadgeId(
128  int $a_badge_id
129  ): array {
130  global $DIC;
131 
132  $ilDB = $DIC->database();
133 
134  $res = array();
135 
136  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
137  " WHERE badge_id = " . $ilDB->quote($a_badge_id, "integer"));
138  while ($row = $ilDB->fetchAssoc($set)) {
139  $obj = new self();
140  $obj->importDBRow($row);
141  $res[] = $obj;
142  }
143 
144  return $res;
145  }
146 
151  public static function getInstancesByParentId(
152  int $a_parent_obj_id
153  ): array {
154  global $DIC;
155 
156  $ilDB = $DIC->database();
157 
158  $res = array();
159 
160  $badge_ids = array();
161  foreach (ilBadge::getInstancesByParentId($a_parent_obj_id) as $badge) {
162  $badge_ids[] = $badge->getId();
163  }
164  if (count($badge_ids)) {
165  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
166  " WHERE " . $ilDB->in("badge_id", $badge_ids, "", "integer"));
167  while ($row = $ilDB->fetchAssoc($set)) {
168  $obj = new self();
169  $obj->importDBRow($row);
170  $res[] = $obj;
171  }
172  }
173 
174  return $res;
175  }
176 
181  public static function getAssignedUsers(
182  int $a_badge_id
183  ): array {
184  $res = [];
185 
186  foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
187  $res[] = $ass->getUserId();
188  }
189 
190  return $res;
191  }
192 
193  public static function exists(
194  int $a_badge_id,
195  int $a_user_id
196  ): bool {
197  $obj = new self($a_badge_id, $a_user_id);
198  return $obj->stored;
199  }
200 
201 
202  //
203  // setter/getter
204  //
205 
206  protected function setBadgeId(int $a_value): void
207  {
208  $this->badge_id = $a_value;
209  }
210 
211  public function getBadgeId(): int
212  {
213  return $this->badge_id;
214  }
215 
216  protected function setUserId(int $a_value): void
217  {
218  $this->user_id = $a_value;
219  }
220 
221  public function getUserId(): int
222  {
223  return $this->user_id;
224  }
225 
226  protected function setTimestamp(int $a_value): void
227  {
228  $this->tstamp = $a_value;
229  }
230 
231  public function getTimestamp(): int
232  {
233  return $this->tstamp;
234  }
235 
236  public function setAwardedBy(int $a_id): void
237  {
238  $this->awarded_by = $a_id;
239  }
240 
241  public function getAwardedBy(): int
242  {
243  return $this->awarded_by;
244  }
245 
246  public function setPosition(?int $a_value): void
247  {
248  $this->pos = $a_value;
249  }
250 
251  public function getPosition(): ?int
252  {
253  return $this->pos;
254  }
255 
256 
257  //
258  // crud
259  //
260 
261  protected function importDBRow(array $a_row): void
262  {
263  $this->stored = true;
264  $this->setBadgeId((int) $a_row["badge_id"]);
265  $this->setUserId((int) $a_row["user_id"]);
266  $this->setTimestamp((int) $a_row["tstamp"]);
267  $this->setAwardedBy((int) $a_row["awarded_by"]);
268  $this->setPosition($a_row["pos"]);
269  }
270 
271  protected function read(
272  int $a_badge_id,
273  int $a_user_id
274  ): void {
275  $ilDB = $this->db;
276 
277  $set = $ilDB->query("SELECT * FROM badge_user_badge" .
278  " WHERE badge_id = " . $ilDB->quote($a_badge_id, "integer") .
279  " AND user_id = " . $ilDB->quote($a_user_id, "integer"));
280  $row = $ilDB->fetchAssoc($set);
281  if ($row && $row["user_id"]) {
282  $this->importDBRow($row);
283  }
284  }
285 
289  protected function getPropertiesForStorage(): array
290  {
291  return [
292  "tstamp" => ["integer", $this->stored ? $this->getTimestamp() : time()],
293  "awarded_by" => ["integer", $this->getAwardedBy()],
294  "pos" => ["integer", $this->getPosition()]
295  ];
296  }
297 
298  public function store(): void
299  {
300  $ilDB = $this->db;
301 
302  if (!$this->getBadgeId() ||
303  !$this->getUserId()) {
304  return;
305  }
306 
307  $keys = array(
308  "badge_id" => array("integer", $this->getBadgeId()),
309  "user_id" => array("integer", $this->getUserId())
310  );
311  $fields = $this->getPropertiesForStorage();
312 
313  if (!$this->stored) {
314  $ilDB->insert("badge_user_badge", $fields + $keys);
315  } else {
316  $ilDB->update("badge_user_badge", $fields, $keys);
317  }
318  }
319 
320  public function delete(): void
321  {
322  $ilDB = $this->db;
323 
324  if (!$this->getBadgeId() ||
325  !$this->getUserId()) {
326  return;
327  }
328 
329  $this->deleteStaticFiles();
330 
331  $ilDB->manipulate("DELETE FROM badge_user_badge" .
332  " WHERE badge_id = " . $ilDB->quote($this->getBadgeId(), "integer") .
333  " AND user_id = " . $ilDB->quote($this->getUserId(), "integer"));
334  }
335 
336  public static function deleteByUserId(int $a_user_id): void
337  {
338  foreach (self::getInstancesByUserId($a_user_id) as $ass) {
339  $ass->delete();
340  }
341  }
342 
343  public static function deleteByBadgeId(int $a_badge_id): void
344  {
345  foreach (self::getInstancesByBadgeId($a_badge_id) as $ass) {
346  $ass->delete();
347  }
348  }
349 
350  public static function deleteByParentId(int $a_parent_obj_id): void
351  {
352  foreach (self::getInstancesByParentId($a_parent_obj_id) as $ass) {
353  $ass->delete();
354  }
355  }
356 
357  public static function updatePositions(
358  int $a_user_id,
359  array $a_positions
360  ): void {
361  $existing = array();
362  foreach (self::getInstancesByUserId($a_user_id) as $ass) {
363  $badge = new ilBadge($ass->getBadgeId());
364  $existing[$badge->getId()] = array($badge->getTitle(), $ass);
365  }
366 
367  $new_pos = 0;
368  foreach ($a_positions as $title) {
369  foreach ($existing as $id => $item) {
370  if ($title == $item[0]) {
371  $item[1]->setPosition(++$new_pos);
372  $item[1]->store();
373  unset($existing[$id]);
374  }
375  }
376  }
377  }
378 
385  public static function getBadgesForUser(
386  int $a_user_id,
387  int $a_ts_from,
388  int $a_ts_to
389  ): array {
390  global $DIC;
391 
392  $db = $DIC->database();
393 
394  $set = $db->queryF(
395  "SELECT bdg.parent_id, ub.tstamp, bdg.title FROM badge_user_badge ub JOIN badge_badge bdg" .
396  " ON (ub.badge_id = bdg.id) " .
397  " WHERE ub.user_id = %s AND ub.tstamp >= %s AND ub.tstamp <= %s",
398  array("integer","integer","integer"),
399  array($a_user_id, $a_ts_from, $a_ts_to)
400  );
401  $res = [];
402  while ($rec = $db->fetchAssoc($set)) {
403  $res[] = $rec;
404  }
405  return $res;
406  }
407 
408  public function deleteStaticFiles(): void
409  {
410  // remove instance files
411  $path = ilBadgeHandler::getInstance()->getInstancePath($this);
412  $path = str_replace(".json", ".*", $path);
413  array_map("unlink", glob($path));
414  }
415 
416  public static function clearBadgeCache(
417  int $a_user_id
418  ): void {
419  foreach (self::getInstancesByUserId($a_user_id) as $ass) {
420  $ass->deleteStaticFiles();
421  }
422  }
423 }
$res
Definition: ltiservices.php:69
read(int $a_badge_id, int $a_user_id)
static getNewCounter(int $a_user_id)
fetchAssoc(ilDBStatement $statement)
static getInstancesByUserId(int $a_user_id)
static deleteByParentId(int $a_parent_obj_id)
static getAssignedUsers(int $a_badge_id)
$path
Definition: ltiservices.php:32
global $DIC
Definition: feed.php:28
__construct(int $a_badge_id=null, int $a_user_id=null)
static updatePositions(int $a_user_id, array $a_positions)
$keys
Definition: metadata.php:204
static getInstancesByBadgeId(int $a_badge_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstancesByParentId(int $a_parent_id, array $a_filter=null)
static getInstancesByParentId(int $a_parent_obj_id)
queryF(string $query, array $types, array $values)
static exists(int $a_badge_id, int $a_user_id)
static deleteByUserId(int $a_user_id)
static clearBadgeCache(int $a_user_id)
static deleteByBadgeId(int $a_badge_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static getBadgesForUser(int $a_user_id, int $a_ts_from, int $a_ts_to)
static getLatestTimestamp(int $a_user_id)