ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCmiXapiDelModel.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 {
30  public const DB_TABLE_NAME = 'cmix_settings';
31  public const DB_USERS_TABLE_NAME = 'cmix_users';
32 
33  public const DB_DEL_OBJ = 'cmix_del_object';
34  public const DB_DEL_USERS = 'cmix_del_user';
35 
36  private \ILIAS\DI\Container $dic;
37 
38  private ilDBInterface $db;
39 
40  private static ?ilCmiXapiDelModel $instance = null;
41 
42  protected ilLogger $log;
43 
44  public function __construct()
45  {
46  global $DIC;
47  $this->dic = $DIC;
48  $this->db = $this->dic->database();
49  $this->log = ilLoggerFactory::getLogger('cmix');
50  //
51  }
52 
53  public static function init(): ilCmiXapiDelModel
54  {
55  return self::$instance ?? self::$instance = new self();
56  }
57 
58  public function getXapiObjIdForUser(int $userId): ?array
59  {
60  $data = null;
61  $where = $this->db->quote($userId, 'integer');
62  $result = $this->db->query("SELECT obj_id FROM " . self::DB_USERS_TABLE_NAME . " WHERE usr_id = " . $where);
63  while($row = $this->db->fetchAssoc($result)) {
64  if(is_null($data)) {
65  $data = [];
66  }
67  $data[] = $row['obj_id'];
68  }
69  return $data;
70  }
71 
72  public function setXapiUserAsDeleted(int $userId): bool
73  {
74  $values = [
75  'usr_id' => ['integer', $userId],
76  'added' => ['timestamp', date('Y-m-d H:i:s')]
77  ];
78  $this->db->insert(self::DB_DEL_USERS, $values);
79  return true;
80  }
81 
82  public function setUserAsUpdated(int $usrId)
83  {
84  $this->db->update(self::DB_DEL_USERS, [
85  'updated' => ['timestamp', date('Y-m-d H:i:s')]
86  ], [
87  'usr_id' => ['integer', $usrId]
88  ]);
89  }
90 
91  public function resetUpdatedXapiUser(int $usrId, int $objId)
92  {
93  $this->db->update(self::DB_DEL_USERS, [
94  'updated' => ['timestamp', null]
95  ], [
96  'usr_id' => ['integer', $usrId],
97  'obj_id' => ['integer', $objId]
98  ]);
99  }
100 
101 
102 
103  public function getXapiObjectsByDeletedUsers(): array
104  {
105  $data = [];
106  $result = $this->db->query("SELECT obj.obj_id, obj.lrs_type_id, obj.activity_id, usr.usr_id, usr.usr_ident, del.added FROM " .
107  self::DB_TABLE_NAME . " obj, " .
108  self::DB_USERS_TABLE_NAME . " usr, " .
109  self::DB_DEL_USERS . " del " .
110  #" INNER JOIN " . self::DB_DEL_USERS . " del ON usr.usr_id = xdel.usr_id" .
111  " WHERE usr.usr_id = del.usr_id AND obj.obj_id = usr.obj_id AND del.updated IS NULL");
112  while($row = $this->db->fetchAssoc($result)) {
113  if(is_null($data)) {
114  $data = [];
115  }
116  $data[] = $row;
117  }
118  return $data;
119  }
120 
121  public function getXapiObjectsByUser(int $userId): array
122  {
123  $data = [];
124  $result = $this->db->query("SELECT obj.obj_id, obj.lrs_type_id, obj.activity_id FROM " .
125  self::DB_TABLE_NAME . " obj, " .
126  self::DB_USERS_TABLE_NAME . " usr" .
127  #" INNER JOIN " . self::DB_DEL_USERS . " del ON usr.usr_id = xdel.usr_id" .
128  " WHERE usr.usr_id = " . $this->db->quote($userId, 'integer') . " AND obj.obj_id = usr.obj_id");
129  while($row = $this->db->fetchAssoc($result)) {
130  if(is_null($data)) {
131  $data = [];
132  }
133  $data[] = $row;
134  }
135  return $data;
136  }
137 
138  public function getNewDeletedUsers()
139  {
140  $data = array();
141 
142  $result = $this->db->query("SELECT * FROM " . self::DB_DEL_USERS . " WHERE updated IS NULL");
143  while($row = $this->db->fetchAssoc($result)) {
144  $data[] = $row;
145  }
146  return $data;
147  }
148 
149  public function deleteUserEntry($usrId, $objId)
150  {
151  $this->db->manipulateF(
152  'DELETE FROM ' . self::DB_DEL_USERS . ' WHERE usr_id = %s AND obj_id = %s',
153  ['integer', 'integer'],
154  [$usrId, $objId]
155  );
156  }
157 
158  // XXCF OBJECTS
159 
160  public function getXapiObjectData(int $objId)
161  {
162  $data = null;
163  $where = $this->db->quote($objId, 'integer');
164  $result = $this->db->query("SELECT lrs_type_id, activity_id, delete_data FROM " . self::DB_TABLE_NAME . " WHERE obj_id = " . $where);
165  while($row = $this->db->fetchAssoc($result)) {
166  $data = $row;
167  }
168  return $data;
169  }
170 
171  public function getAllXapiDelObjectData(): array
172  {
173  $data = array();
174 
175  $result = $this->db->query("SELECT * FROM " . self::DB_DEL_OBJ . " WHERE 1");
176  while($row = $this->db->fetchAssoc($result)) {
177  $data[] = $row;
178  }
179  return $data;
180  }
181 
182  public function getNewDeletedXapiObjects()
183  {
184  $data = array();
185 
186  $result = $this->db->query("SELECT * FROM " . self::DB_DEL_OBJ . " WHERE updated IS NULL");
187  while($row = $this->db->fetchAssoc($result)) {
188  $data[] = $row;
189  }
190  return $data;
191  }
192 
193  public function deleteXapiObjectEntry($objId)
194  {
195  $this->db->manipulateF(
196  'DELETE FROM ' . self::DB_DEL_OBJ . ' WHERE obj_id = %s',
197  ['integer'],
198  [$objId]
199  );
200  }
201 
202  public function setXapiObjAsDeleted(int $objId, int $typeId, string $actId): void
203  {
204  if(!$this->dic->cron()->manager()->isJobActive('xapi_deletion_cron')) {
205  $xapiDelete = new ilCmiXapiStatementsDeleteRequest($objId, $typeId, $actId, null, ilCmiXapiStatementsDeleteRequest::DELETE_SCOPE_ALL);
206  $xapiDelete->delete();
207  } else {
208  $values = [
209  'obj_id' => ['integer', $objId],
210  'type_id' => ['integer', $typeId],
211  'activity_id' => ['string', $actId],
212  'added' => ['timestamp', date('Y-m-d H:i:s')]
213  ];
214  $this->db->insert(self::DB_DEL_OBJ, $values);
215  }
216  }
217 
218  public function setXapiObjAsDeletedForUser(int $objId, int $typeId, string $actId, int $usrId): void
219  {
220  if(!$this->dic->cron()->manager()->isJobActive('xapi_deletion_cron')) {
221  $xapiDelete = new ilCmiXapiStatementsDeleteRequest($objId, $typeId, $actId, $usrId, ilCmiXapiStatementsDeleteRequest::DELETE_SCOPE_ALL);
222  $xapiDelete->delete();
223  } else {
224  $counter = 0;
225  $result = $this->db->queryF(
226  'SELECT count(*) as counter FROM ' . self::DB_DEL_USERS . ' WHERE usr_id = %s AND obj_id = %s',
227  ['integer', 'integer'],
228  [$usrId, $objId]
229  );
230  while($row = $this->db->fetchAssoc($result)) {
231  $counter = $row['counter'];
232  }
233 
234  if ($counter == 0) {
235  $values = [
236  'usr_id' => ['integer', $usrId],
237  'obj_id' => ['integer', $objId],
238  'added' => ['timestamp', date('Y-m-d H:i:s')]
239  ];
240  $this->db->insert(self::DB_DEL_USERS, $values);
241  }
242  }
243  }
244 
245 
246  public function setXapiObjAsUpdated(int $objId)
247  {
248 
249  $this->db->update(self::DB_DEL_OBJ, [
250  'updated' => ['timestamp', date('Y-m-d H:i:s')]
251  ], [
252  'obj_id' => ['integer', $objId]
253  ]);
254  }
255 
256  public function resetUpdatedXapiObj(int $objId)
257  {
258 
259  $this->db->update(self::DB_DEL_OBJ, [
260  'updated' => ['timestamp', null]
261  ], [
262  'obj_id' => ['integer', $objId]
263  ]);
264  }
265 
266  public function removeCmixUsersForObject(int $objId): void
267  {
268  $this->db->manipulateF(
269  'DELETE FROM cmix_users WHERE obj_id = %s',
270  ['integer'],
271  [$objId]
272  );
273  $this->log->debug('cmix_users deleted for objId=' . (string) $objId);
274  }
275 
276  public function removeCmixUsersForObjectAndUser(int $objId, int $usrId): void
277  {
278  $this->db->manipulateF(
279  'DELETE FROM cmix_users WHERE obj_id = %s AND usr_id = %s',
280  ['integer','integer'],
281  [$objId,$usrId]
282  );
283  $this->log->debug('cmix_user with usrId ' . (string) $usrId . ' deleted for objId=' . (string) $objId);
284  }
285 
286 }
setXapiObjAsDeleted(int $objId, int $typeId, string $actId)
static getLogger(string $a_component_id)
Get component logger.
deleteUserEntry($usrId, $objId)
ILIAS DI Container $dic
removeCmixUsersForObjectAndUser(int $objId, int $usrId)
$objId
Definition: xapitoken.php:57
Class ilCmiXapiStatementsDeleteRequest.
global $DIC
Definition: feed.php:28
resetUpdatedXapiUser(int $usrId, int $objId)
Class ilCmiXapiDelModel.
setXapiObjAsDeletedForUser(int $objId, int $typeId, string $actId, int $usrId)
static ilCmiXapiDelModel $instance
$typeId
Definition: ltiregstart.php:36