ILIAS  release_8 Revision v8.24
class.ilECSCmsData.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
24{
25 public const MAPPING_UNMAPPED = 1;
28 public const MAPPING_MAPPED = 4;
29 public const MAPPING_DELETED = 5;
30
32
33 private int $obj_id;
34 private int $server_id;
35 private int $mid;
36 private int $tree_id;
37 private string $cms_id;
38 private string $title;
39 private string $term;
41 private bool $deleted = false;
42
43 public function __construct(int $a_obj_id = 0)
44 {
45 global $DIC;
46 $this->db = $DIC->database();
47
48 $this->obj_id = $a_obj_id;
49 $this->read();
50 }
51
52 public static function treeExists(int $a_server_id, int $a_mid, int $a_tree_id): bool
53 {
54 global $DIC;
55
56 $ilDB = $DIC['ilDB'];
57
58 $query = 'SELECT COUNT(*) num FROM ecs_cms_data ' .
59 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
60 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
61 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer');
62
63 $res = $ilDB->query($query);
64 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
65 return $row->num > 0;
66 }
67 return false;
68 }
69
75 public static function findDeletedNodes(int $a_server_id, int $a_mid, int $a_tree_id): array
76 {
77 global $DIC;
78
79 $ilDB = $DIC['ilDB'];
80
81 $query = 'SELECT ed.obj_id obj_id FROM ecs_cms_data ed ' .
82 'LEFT JOIN ecs_cms_tree et ON ed.obj_id = et.child ' .
83 'WHERE et.child IS NULL ' .
84 'AND server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
85 'AND mid = ' . $ilDB->quote($a_mid) . ' ' .
86 'AND tree_id = ' . $ilDB->quote($a_tree_id);
87 $res = $ilDB->query($query);
88
89 $deleted = array();
90 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
91 $deleted[] = $row->obj_id;
92 }
93 return $deleted;
94 }
95
96 public static function lookupObjId(int $a_server_id, int $a_mid, int $a_tree_id, string $cms_id): int
97 {
98 global $DIC;
99
100 $ilDB = $DIC['ilDB'];
101 $logger = $DIC->logger()->wsrv();
102
103 $query = 'SELECT obj_id FROM ecs_cms_data ' .
104 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
105 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
106 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ' .
107 'AND cms_id = ' . $ilDB->quote($cms_id, 'text');
108 $res = $ilDB->query($query);
109
110 $logger->debug($query);
111
112 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
113 return $row->obj_id;
114 }
115 return 0;
116 }
117
121 public static function lookupFirstTreeOfNode($a_server_id, $a_mid, $cms_id): int
122 {
123 global $DIC;
124
125 $ilDB = $DIC->database();
126 $logger = $DIC->logger()->wsrv();
127
128 $logger->debug($a_server_id . ' ' . $a_mid . ' ' . $cms_id);
129
130 $query = 'SELECT tree_id FROM ecs_cms_data ' .
131 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
132 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
133 'AND cms_id = ' . $ilDB->quote($cms_id, 'text') . ' ' .
134 'ORDER BY tree_id ';
135 $res = $ilDB->query($query);
136
137 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
138 return $row->tree_id;
139 }
140 return 0;
141 }
142
146 public static function lookupTitle(int $a_server_id, int $a_mid, int $a_tree_id): string
147 {
148 global $DIC;
149
150 $ilDB = $DIC['ilDB'];
151
152 $query = 'SELECT * FROM ecs_cms_data ' .
153 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
154 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
155 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer');
156 $res = $ilDB->query($query);
157 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
158 return $row->title;
159 }
160 return '';
161 }
162
166 public static function lookupTopTerm(int $a_server_id, int $a_mid, int $a_tree_id): string
167 {
168 global $DIC;
169
170 $ilDB = $DIC['ilDB'];
171
172 $query = 'SELECT term FROM ecs_cms_data ' .
173 'JOIN ecs_cms_tree ON obj_id = child ' .
174 'WHERE tree = ' . $ilDB->quote($a_tree_id, 'integer') . ' ' .
175 'AND server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
176 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
177 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ' .
178 'ORDER BY depth';
179 $res = $ilDB->query($query);
180 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
181 return $row->term;
182 }
183 return '';
184 }
185
189 public static function lookupStatusByObjId(int $a_server_id, int $a_mid, int $a_tree_id, int $obj_id): int
190 {
191 global $DIC;
192
193 $ilDB = $DIC['ilDB'];
194
195 $query = 'SELECT status,deleted FROM ecs_cms_data ' .
196 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
197 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
198 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ' .
199 'AND obj_id = ' . $ilDB->quote($obj_id, 'integer');
200 $res = $ilDB->query($query);
201 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
202 if ($row->deleted) {
204 }
205 return (int) $row->status;
206 }
208 }
209
213 public static function lookupStatusByCmsId(int $a_server_id, int $a_mid, int $a_tree_id, string $cms_id): int
214 {
215 global $DIC;
216
217 $ilDB = $DIC['ilDB'];
218
219 $query = 'SELECT status FROM ecs_cms_data ' .
220 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
221 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
222 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ' .
223 'AND cms_id = ' . $ilDB->quote($cms_id, 'text');
224 $res = $ilDB->query($query);
225 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
226 return (int) $row->status;
227 }
229 }
230
231 public static function updateStatus(int $a_server_id, int $a_mid, int $a_tree_id): void
232 {
233 // Set all status to pending unmapped
234 self::writeAllStatus($a_server_id, $a_mid, $a_tree_id, self::MAPPING_UNMAPPED);
235
236 // Set mapped for mapped and their descendent
237 foreach (ilECSNodeMappingAssignments::lookupAssignmentIds($a_server_id, $a_mid, $a_tree_id) as $assignment) {
238 $cmsTree = new ilECSCmsTree($a_tree_id);
239 $subIds = self::lookupCmsIds(array_merge($cmsTree->getSubTreeIds($assignment), array($assignment)));
240
242 $a_server_id,
243 $a_mid,
244 $a_tree_id,
245 $subIds,
246 self::MAPPING_MAPPED
247 );
248 }
249 }
250
254 public static function lookupCmsId($a_obj_id): string
255 {
256 $cms_ids = self::lookupCmsIds(array($a_obj_id));
257 return $cms_ids[0];
258 }
259
264 public static function lookupCmsIds(array $a_obj_ids): array
265 {
266 global $DIC;
267
268 $ilDB = $DIC['ilDB'];
269
270 $query = 'SELECT cms_id FROM ecs_cms_data ' .
271 'WHERE ' . $ilDB->in('obj_id', $a_obj_ids, false, 'integer');
272 $res = $ilDB->query($query);
273
274 $cms_ids = [];
275 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
276 $cms_ids[] = $row->cms_id;
277 }
278 return $cms_ids;
279 }
280
281 public static function lookupCmsIdsOfTree($a_server_id, $a_mid, $a_tree_id): array
282 {
283 global $DIC;
284
285 $ilDB = $DIC['ilDB'];
286
287 $query = 'SELECT cms_id FROM ecs_cms_data ' .
288 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
289 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
290 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer');
291 $res = $ilDB->query($query);
292 $cms_ids = array();
293 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
294 $cms_ids[] = $row->cms_id;
295 }
296 return $cms_ids;
297 }
298
302 public static function writeStatus($a_server_id, $a_mid, $a_tree_id, $cms_ids, $status): bool
303 {
304 global $DIC;
305
306 $ilDB = $DIC['ilDB'];
307
308 $query = 'UPDATE ecs_cms_data ' .
309 'SET status = ' . $ilDB->quote($status, 'integer') . ' ' .
310 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
311 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
312 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ' .
313 'AND ' . $ilDB->in('cms_id', $cms_ids, false, 'text');
314 $ilDB->manipulate($query);
315 return true;
316 }
317
321 public static function writeAllStatus($a_server_id, $a_mid, $a_tree_id, $status): bool
322 {
323 global $DIC;
324
325 $ilDB = $DIC['ilDB'];
326
327 $query = 'UPDATE ecs_cms_data ' .
328 'SET status = ' . $ilDB->quote($status, 'integer') . ' ' .
329 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
330 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
331 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ';
332 $ilDB->manipulate($query);
333 return true;
334 }
335
339 public static function writeAllDeleted($a_server_id, $a_mid, $a_tree_id, $a_deleted_flag): bool
340 {
341 global $DIC;
342
343 $ilDB = $DIC['ilDB'];
344
345 $query = 'UPDATE ecs_cms_data ' .
346 'SET deleted = ' . $ilDB->quote($a_deleted_flag, 'integer') . ' ' .
347 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
348 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
349 'AND tree_id = ' . $ilDB->quote($a_tree_id, 'integer') . ' ';
350 $ilDB->manipulate($query);
351 return true;
352 }
353
359 public static function lookupTreeIds(int $a_server_id, int $a_mid): array
360 {
361 global $DIC;
362
363 $ilDB = $DIC['ilDB'];
364
365 $query = 'SELECT DISTINCT(tree_id) tid FROM ecs_cms_data ' .
366 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
367 'AND mid = ' . $ilDB->quote($a_mid, 'integer');
368 $res = $ilDB->query($query);
369
370 $tree_ids = [];
371 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
372 $tree_ids[] = (int) $row->tid;
373 }
374 return $tree_ids;
375 }
376
377
378 public function setTitle($a_title): void
379 {
380 $this->title = $a_title;
381 }
382
383 public function getTitle(): string
384 {
385 return $this->title;
386 }
387
388 public function setTerm($a_term): void
389 {
390 $this->term = $a_term;
391 }
392
393 public function getTerm(): string
394 {
395 return $this->term;
396 }
397
398 public function setObjId($a_id): void
399 {
400 $this->obj_id = $a_id;
401 }
402
403 public function getObjId(): int
404 {
405 return $this->obj_id;
406 }
407
408 public function setCmsId($a_id): void
409 {
410 $this->cms_id = $a_id;
411 }
412
413 public function getCmsId(): string
414 {
415 return $this->cms_id;
416 }
417
418 public function setServerId($a_id): void
419 {
420 $this->server_id = $a_id;
421 }
422
423 public function getServerId(): int
424 {
425 return $this->server_id;
426 }
427
428 public function setTreeId($a_id): void
429 {
430 $this->tree_id = $a_id;
431 }
432
433 public function getTreeId(): int
434 {
435 return $this->tree_id;
436 }
437
438 public function setMid($a_id): void
439 {
440 $this->mid = $a_id;
441 }
442
443 public function getMid(): int
444 {
445 return $this->mid;
446 }
447
448 public function setStatus($a_status): void
449 {
450 $this->status = $a_status;
451 }
452
453 public function getStatus(): int
454 {
455 return $this->status;
456 }
457
458 public function setDeleted($a_is_deleted): void
459 {
460 $this->deleted = $a_is_deleted;
461 }
462
463 public function isDeleted(): bool
464 {
465 return $this->deleted;
466 }
467
468
469 public function save(): bool
470 {
471 $this->obj_id = $this->db->nextId('ecs_cms_data');
472
473 $query = 'INSERT INTO ecs_cms_data (obj_id,server_id,mid,tree_id,cms_id,title,term,status,deleted) ' .
474 'VALUES ( ' .
475 $this->db->quote($this->obj_id, 'integer') . ', ' .
476 $this->db->quote($this->server_id, 'integer') . ', ' .
477 $this->db->quote($this->mid, 'integer') . ', ' .
478 $this->db->quote($this->tree_id, 'integer') . ', ' .
479 $this->db->quote($this->cms_id, 'text') . ', ' .
480 $this->db->quote($this->title, 'text') . ', ' .
481 $this->db->quote($this->term, 'text') . ', ' .
482 $this->db->quote($this->status, 'integer') . ', ' .
483 $this->db->quote($this->deleted, 'integer') . ' ' .
484 ')';
485 $this->db->manipulate($query);
486 return true;
487 }
488
489 public function update(): void
490 {
491 $query = "UPDATE ecs_cms_data SET " .
492 'title = ' . $this->db->quote($this->title, 'text') . ', ' .
493 'term = ' . $this->db->quote($this->term, 'text') . ', ' .
494 'status = ' . $this->db->quote($this->status, 'text') . ', ' .
495 'deleted = ' . $this->db->quote($this->isDeleted(), 'integer') . ' ' .
496 'WHERE obj_id = ' . $this->db->quote($this->obj_id, 'integer');
497 $this->db->manipulate($query);
498 }
499
500 public function delete(): void
501 {
502 $query = "DELETE FROM ecs_cms_data " .
503 'WHERE obj_id = ' . $this->db->quote($this->obj_id, 'integer');
504 $this->db->manipulate($query);
505 }
506
507 public function deleteTree(): void
508 {
509 $query = "DELETE FROM ecs_cms_data " .
510 'WHERE server_id = ' . $this->db->quote($this->server_id, 'integer') . ' ' .
511 'AND mid = ' . $this->db->quote($this->mid, 'integer') . ' ' .
512 'AND tree_id = ' . $this->db->quote($this->tree_id, 'integer') . ' ';
513 $this->db->manipulate($query);
514 }
515
516 public static function deleteByServerId($a_server_id): void
517 {
518 global $DIC;
519
520 $ilDB = $DIC['ilDB'];
521
522 $query = "DELETE FROM ecs_cms_data " .
523 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer');
524 $ilDB->manipulate($query);
525 }
526
527 protected function read(): void
528 {
529 $query = 'SELECT * FROM ecs_cms_data ' .
530 'WHERE obj_id = ' . $this->db->quote($this->obj_id, 'integer');
531 $res = $this->db->query($query);
532 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
533 $this->title = $row->title;
534 $this->term = $row->term;
535 $this->server_id = $row->server_id;
536 $this->mid = $row->mid;
537 $this->tree_id = $row->tree_id;
538 $this->cms_id = $row->cms_id;
539 $this->status = $row->status;
540 $this->deleted = $row->deleted;
541 }
542 }
543}
static lookupObjId(int $a_server_id, int $a_mid, int $a_tree_id, string $cms_id)
const MAPPING_PENDING_DISCONNECTABLE
ilDBInterface $db
static findDeletedNodes(int $a_server_id, int $a_mid, int $a_tree_id)
Find deleted nodes Uses a left join since this is more robust.
static writeAllStatus($a_server_id, $a_mid, $a_tree_id, $status)
Update status.
__construct(int $a_obj_id=0)
const MAPPING_PENDING_NOT_DISCONNECTABLE
static lookupTreeIds(int $a_server_id, int $a_mid)
static lookupCmsIds(array $a_obj_ids)
static lookupStatusByObjId(int $a_server_id, int $a_mid, int $a_tree_id, int $obj_id)
Lookup status.
static writeAllDeleted($a_server_id, $a_mid, $a_tree_id, $a_deleted_flag)
Write deleted status.
static lookupTitle(int $a_server_id, int $a_mid, int $a_tree_id)
Lookup title by obj id.
static writeStatus($a_server_id, $a_mid, $a_tree_id, $cms_ids, $status)
Update status.
setDeleted($a_is_deleted)
static lookupCmsId($a_obj_id)
Lookup cms id.
static lookupFirstTreeOfNode($a_server_id, $a_mid, $cms_id)
Lookup first obj_id of cms node.
static lookupStatusByCmsId(int $a_server_id, int $a_mid, int $a_tree_id, string $cms_id)
Lookup status.
static lookupTopTerm(int $a_server_id, int $a_mid, int $a_tree_id)
Lookup term (highest term in cms tree)
static lookupCmsIdsOfTree($a_server_id, $a_mid, $a_tree_id)
static updateStatus(int $a_server_id, int $a_mid, int $a_tree_id)
static deleteByServerId($a_server_id)
static treeExists(int $a_server_id, int $a_mid, int $a_tree_id)
setStatus($a_status)
static lookupAssignmentIds(int $a_server_id, int $a_mid, int $a_tree_id)
Lookup assignments.
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query