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