ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBadge.php
Go to the documentation of this file.
1<?php
2
22
24{
25 private ilLogger $log;
26 protected ilDBInterface $db;
27 protected int $id = 0;
28 protected int $parent_id = 0;
29 protected string $type_id = '';
30 protected bool $active = false;
31 protected string $title = '';
32 protected string $desc = '';
33 protected ?string $image_rid = null;
34 protected string $valid = '';
35 protected ?array $config = null;
36 protected string $criteria = '';
38
39 public function __construct(
40 ?int $a_id = null,
41 ?Container $container = null
42 ) {
43 if ($container === null) {
44 global $DIC;
46 }
47
48 $this->db = $container->database();
49 $this->resource_storage = $container->resourceStorage();
50 $this->log = $container->logger()->forComponent('badge');
51 if ($a_id) {
52 $this->read($a_id);
53 }
54 }
55
59 public static function getInstancesByParentId(
60 int $a_parent_id,
61 ?array $a_filter = null
62 ): array {
63 global $DIC;
64
65 $ilDB = $DIC->database();
66
67 $res = [];
68
69 $sql = 'SELECT * FROM badge_badge' .
70 ' WHERE parent_id = ' . $ilDB->quote($a_parent_id);
71
72 if ($a_filter) {
73 if ($a_filter['title'] ?? false) {
74 $sql .= ' AND ' . $ilDB->like('title', 'text', '%' . trim($a_filter['title']) . '%');
75 }
76 if ($a_filter['type'] ?? false) {
77 $sql .= ' AND type_id = ' . $ilDB->quote($a_filter['type'], 'integer');
78 }
79 }
80
81 $set = $ilDB->query($sql .
82 ' ORDER BY title');
83 while ($row = $ilDB->fetchAssoc($set)) {
84 $obj = new self();
85 $obj->importDBRow($row);
86 $res[] = $obj;
87 }
88
89 return $res;
90 }
91
95 public static function getInstancesByType(
96 string $a_type_id
97 ): array {
98 global $DIC;
99
100 $ilDB = $DIC->database();
101
102 $res = [];
103
104 $set = $ilDB->query('SELECT * FROM badge_badge' .
105 ' WHERE type_id = ' . $ilDB->quote($a_type_id, 'text') .
106 ' ORDER BY title');
107 while ($row = $ilDB->fetchAssoc($set)) {
108 $obj = new self();
109 $obj->importDBRow($row);
110 $res[] = $obj;
111 }
112
113 return $res;
114 }
115
116 public function clone(int $target_parent_obj_id): void
117 {
118 $this->setParentId($target_parent_obj_id);
119 $this->setActive(false);
120
121 if (!$this->getId()) {
122 return;
123 }
124
125 $image_rid = $this->getImageRid();
126 $this->setId(0);
127 if ($image_rid !== null) {
128 $cloned_rid = $this->resource_storage->manage()->clone(new ResourceIdentification($image_rid));
129 $this->setImageRid($cloned_rid);
130 }
131 $this->create();
132 }
133
134 public function getTypeInstance(): ?ilBadgeType
135 {
136 if ($this->getTypeId()) {
138
139 return $handler->getTypeInstanceByUniqueId($this->getTypeId());
140 }
141
142 return null;
143 }
144
145 public function copy(
146 int $a_new_parent_id,
147 string $copy_suffix
148 ): void {
149 $this->setTitle($this->getTitle() . ' ' . $copy_suffix);
150 $this->setParentId($a_new_parent_id);
151 $this->setActive(false);
152
153 if ($this->getId()) {
154 $this->setId(0);
155 $old_rid = $this->getImageRid();
156 $this->create();
157 if ($old_rid !== null) {
158 $new_rid = $this->resource_storage->manage()->clone(new ResourceIdentification($old_rid));
159 $this->setImageRid($new_rid);
160 $this->update();
161 }
162 }
163 }
164
183 public static function getObjectInstances(
184 ?array $filter = null
185 ): array {
186 global $DIC;
187
188 $ilDB = $DIC->database();
189
190 $rows = [];
191
192 $where = '';
193
194 if ($filter['type']) {
195 $where .= ' AND bb.type_id = ' . $ilDB->quote($filter['type'], ilDBConstants::T_TEXT);
196 }
197 if ($filter['title']) {
198 $where .= ' AND ' . $ilDB->like('bb.title', ilDBConstants::T_TEXT, '%' . $filter['title'] . '%');
199 }
200 if ($filter['object']) {
201 $where .= ' AND ' . $ilDB->like('od.title', ilDBConstants::T_TEXT, '%' . $filter['object'] . '%');
202 }
203
204 $set = $ilDB->query('SELECT bb.*, od.title parent_title, od.type parent_type' .
205 ' FROM badge_badge bb' .
206 ' INNER JOIN object_data od ON bb.parent_id = od.obj_id' .
207 ' WHERE od.type != ' . $ilDB->quote('bdga', ilDBConstants::T_TEXT) .
208 $where);
209 while ($row = $ilDB->fetchAssoc($set)) {
210 $row['deleted'] = false;
211 $rows[] = $row;
212 }
213
214 $set = $ilDB->query('SELECT bb.*, od.title parent_title, od.type parent_type' .
215 ' FROM badge_badge bb' .
216 ' INNER JOIN object_data_del od ON bb.parent_id = od.obj_id' .
217 ' WHERE od.type != ' . $ilDB->quote('bdga', ilDBConstants::T_TEXT) .
218 $where);
219 while ($row = $ilDB->fetchAssoc($set)) {
220 $row['deleted'] = true;
221 $rows[] = $row;
222 }
223
224 return $rows;
225 }
226
227 //
228 // setter/getter
229 //
230
231 public function setId(int $a_id): void
232 {
233 $this->id = $a_id;
234 }
235
236 public function getId(): int
237 {
238 return $this->id;
239 }
240
241 public function setParentId(int $a_id): void
242 {
243 $this->parent_id = $a_id;
244 }
245
246 public function getParentId(): int
247 {
248 return $this->parent_id;
249 }
250
251 public function setTypeId(string $a_id): void
252 {
253 $this->type_id = trim($a_id);
254 }
255
256 public function getTypeId(): string
257 {
258 return $this->type_id;
259 }
260
261 public function setActive(bool $a_value): void
262 {
263 $this->active = $a_value;
264 }
265
266 public function isActive(): bool
267 {
268 return $this->active;
269 }
270
271 public function setTitle(string $a_value): void
272 {
273 $this->title = trim($a_value);
274 }
275
276 public function getTitle(): string
277 {
278 return $this->title;
279 }
280
281 public function setDescription(string $a_value): void
282 {
283 $this->desc = trim($a_value);
284 }
285
286 public function getDescription(): string
287 {
288 return $this->desc;
289 }
290
291 public function setCriteria(string $a_value): void
292 {
293 $this->criteria = trim($a_value);
294 }
295
296 public function getCriteria(): string
297 {
298 return $this->criteria;
299 }
300
301 public function setValid(string $a_value): void
302 {
303 $this->valid = trim($a_value);
304 }
305
306 public function getValid(): string
307 {
308 return $this->valid;
309 }
310
311 public function setConfiguration(?array $a_value = null): void
312 {
313 if (is_array($a_value) && !count($a_value)) {
314 $a_value = null;
315 }
316 $this->config = $a_value;
317 }
318
319 public function getConfiguration(): ?array
320 {
321 return $this->config;
322 }
323
324 //
325 // crud
326 //
327
328 protected function read(int $a_id): void
329 {
330 $ilDB = $this->db;
331
332 $set = $ilDB->query('SELECT * FROM badge_badge' .
333 ' WHERE id = ' . $ilDB->quote($a_id, 'integer'));
334 if ($ilDB->numRows($set)) {
335 $row = $ilDB->fetchAssoc($set);
336 $this->importDBRow($row);
337 }
338 }
339
340 protected function importDBRow(
341 array $a_row
342 ): void {
343 $this->setId($a_row['id']);
344 $this->setParentId($a_row['parent_id']);
345 $this->setTypeId($a_row['type_id']);
346 $this->setActive($a_row['active']);
347 $this->setTitle($a_row['title']);
348 $this->setDescription($a_row['descr']);
349 $this->setCriteria($a_row['crit']);
350 $this->setImageRid($a_row['image_rid']);
351 $this->setValid($a_row['valid']);
352 $this->setConfiguration($a_row['conf']
353 ? unserialize($a_row['conf'], ['allowed_classes' => false])
354 : null);
355 }
356
357 public function create(): void
358 {
359 $ilDB = $this->db;
360
361 if ($this->getId()) {
362 $this->update();
363
364 return;
365 }
366
367 $id = $ilDB->nextId('badge_badge');
368 $this->setId($id);
369
370 $fields = $this->getPropertiesForStorage();
371
372 $fields['id'] = ['integer', $id];
373 $fields['parent_id'] = ['integer', $this->getParentId()];
374 $fields['type_id'] = ['text', $this->getTypeId()];
375
376 $ilDB->insert('badge_badge', $fields);
377 }
378
379 public function update(): void
380 {
381 $ilDB = $this->db;
382
383 if (!$this->getId()) {
384 $this->create();
385
386 return;
387 }
388
389 $fields = $this->getPropertiesForStorage();
390
391 $ilDB->update(
392 'badge_badge',
393 $fields,
394 ['id' => ['integer', $this->getId()]]
395 );
396 }
397
398 public function delete(): void
399 {
400 $ilDB = $this->db;
401
402 if (!$this->getId()) {
403 return;
404 }
405
406 if ($this->getImageRid()) {
407 try {
408 $this->resource_storage->manage()->remove(new ResourceIdentification($this->getImageRid()), new ilBadgeFileStakeholder());
409 } catch (Exception $e) {
410 $this->log->warning(sprintf('There was an exception, while deleting the badge with id %s. Exception: %s', $this->getId(), $e->getMessage()));
411 }
412 }
413
414 $this->deleteStaticFiles();
415
417
418 $ilDB->manipulate('DELETE FROM badge_badge' .
419 ' WHERE id = ' . $ilDB->quote($this->getId(), 'integer'));
420 }
421
425 protected function getPropertiesForStorage(): array
426 {
427 return [
428 'active' => ['integer', $this->isActive()],
429 'title' => ['text', $this->getTitle()],
430 'descr' => ['text', $this->getDescription()],
431 'crit' => ['text', $this->getCriteria()],
432 'image_rid' => ['text', $this->getImageRid()],
433 'valid' => ['text', $this->getValid()],
434 'conf' => [
435 'text', $this->getConfiguration() ? serialize($this->getConfiguration()) : null
436 ]
437 ];
438 }
439
440 //
441 // helper
442 //
443
447 public function getParentMeta(): array
448 {
449 $parent_type = ilObject::_lookupType($this->getParentId());
450 $parent_title = '';
451 if ($parent_type) {
452 $parent_title = ilObject::_lookupTitle($this->getParentId());
453 $deleted = false;
454 } else {
455 // already deleted?
456 $parent = ilObjectDataDeletionLog::get($this->getParentId());
457 if ($parent['type']) {
458 $parent_type = $parent['type'];
459 $parent_title = $parent['title'];
460 }
461 $deleted = true;
462 }
463
464 return [
465 'id' => $this->getParentId(),
466 'type' => $parent_type,
467 'title' => $parent_title,
468 'deleted' => $deleted
469 ];
470 }
471
472 //
473 // PUBLISHING
474 //
475
476 protected function prepareJson(
477 string $a_base_url,
478 string $a_img_suffix
479 ): stdClass {
480 $json = new stdClass();
481 $json->{'@context'} = 'https://w3id.org/openbadges/v1';
482 $json->type = 'BadgeClass';
483 $json->id = $a_base_url . 'class.json';
484 $json->name = $this->getTitle();
485 $json->description = $this->getDescription();
486 $json->image = $a_base_url . 'image.' . $a_img_suffix;
487 $json->criteria = $a_base_url . 'criteria.txt';
488 $json->issuer = ilBadgeHandler::getInstance()->getIssuerStaticUrl();
489
490 return $json;
491 }
492
493 public function deleteStaticFiles(): void
494 {
495 // remove instance files
496 $path = ilBadgeHandler::getInstance()->getBadgePath($this);
497 if (is_dir($path)) {
498 ilFileUtils::delDir($path);
499 }
500 }
501
502 public static function getExtendedTypeCaption(
503 ilBadgeType $a_type
504 ): string {
505 global $DIC;
506
507 $lng = $DIC->language();
508
509 return $a_type->getCaption() . ' (' .
510 ($a_type instanceof ilBadgeAuto
511 ? $lng->txt('badge_subtype_auto')
512 : $lng->txt('badge_subtype_manual')) . ')';
513 }
514
515 public function getImageRid(): ?string
516 {
517 // '-' was written by the removed filesystem migration for badges without an image.
518 if ($this->image_rid === null || $this->image_rid === '' || $this->image_rid === '-') {
519 return null;
520 }
521
522 return $this->image_rid;
523 }
524
525 public function setImageRid(?string $image_rid): void
526 {
527 $this->image_rid = $image_rid;
528 }
529}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:38
static deleteByBadgeId(int $a_badge_id)
setCriteria(string $a_value)
string $criteria
string $valid
ilLogger $log
setId(int $a_id)
string $image_rid
clone(int $target_parent_obj_id)
deleteStaticFiles()
string $desc
string $type_id
static getInstancesByType(string $a_type_id)
copy(int $a_new_parent_id, string $copy_suffix)
array $config
prepareJson(string $a_base_url, string $a_img_suffix)
setTitle(string $a_value)
setActive(bool $a_value)
static getExtendedTypeCaption(ilBadgeType $a_type)
setValid(string $a_value)
string $title
setParentId(int $a_id)
static getObjectInstances(?array $filter=null)
getConfiguration()
read(int $a_id)
ilDBInterface $db
setConfiguration(?array $a_value=null)
static getInstancesByParentId(int $a_parent_id, ?array $a_filter=null)
setImageRid(?string $image_rid)
int $parent_id
setDescription(string $a_value)
getPropertiesForStorage()
importDBRow(array $a_row)
__construct(?int $a_id=null, ?Container $container=null)
Services $resource_storage
setTypeId(string $a_id)
bool $active
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
Component logger with individual log levels by component id.
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilDBInterface.
$handler
Definition: oai.php:31
global $lng
Definition: privfeed.php:26
foreach($_POST as $key=> $value) $res
global $DIC
Definition: shib_login.php:26
$container
@noRector
Definition: wac.php:39