ILIAS  release_8 Revision v8.23
class.ilECSUserConsents.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
25 {
29  private static array $instances = [];
30 
31  private int $usr_id;
35  private array $consents = [];
36 
37  protected ilDBInterface $db;
38 
39  protected function __construct(int $a_usr_id)
40  {
41  global $DIC;
42 
43  $this->db = $DIC->database();
44  $this->usr_id = $a_usr_id;
45  if ($a_usr_id > 0) {
46  $this->read();
47  }
48  }
49 
50  public static function getInstanceByUserId(int $a_usr_id): self
51  {
52  if (!isset(self::$instances[$a_usr_id])) {
53  self::$instances[$a_usr_id] = new self($a_usr_id);
54  }
55  return self::$instances[$a_usr_id];
56  }
57 
58  public function getUserId(): int
59  {
60  return $this->usr_id;
61  }
62 
63  public function hasConsented(int $server_id, int $a_mid): bool
64  {
65  return array_key_exists("{$server_id}:{$a_mid}", $this->consents);
66  }
67 
68  public function delete(): void
69  {
70  foreach ($this->consents as $mid => $consent) {
71  $consent->delete();
72  }
73  $this->consents = [];
74  }
75 
76  public function add(int $server_id, int $a_mid): void
77  {
78  if (!$this->hasConsented($server_id, $a_mid)) {
79  $consent = new ilECSUserConsent($this->getUserId(), $server_id, $a_mid);
80  $consent->save();
81  $this->consents["{$server_id}:{$a_mid}"] = $consent;
82  }
83  }
84 
85  protected function read(): void
86  {
87  $query = 'SELECT * FROM ecs_user_consent ' .
88  'WHERE usr_id = ' . $this->db->quote(
89  $this->getUserId(),
91  );
92  $res = $this->db->query($query);
93  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
94  $this->consents["{$row->server_id}:{$row->mid}"] = new ilECSUserConsent(
95  (int) $row->usr_id,
96  (int) $row->server_id,
97  (int) $row->mid
98  );
99  }
100  }
101 }
$res
Definition: ltiservices.php:69
hasConsented(int $server_id, int $a_mid)
Class ilECSUserConsents.
global $DIC
Definition: feed.php:28
static getInstanceByUserId(int $a_usr_id)
$query
Class ilECSUserConsent.
add(int $server_id, int $a_mid)