ILIAS  release_8 Revision v8.24
class.ilBadge.php
Go to the documentation of this file.
1<?php
2
20
25{
26 protected ilLanguage $lng;
27 protected ilDBInterface $db;
28
29 protected int $id = 0;
30 protected int $parent_id = 0;
31 protected string $type_id = "";
32 protected bool $active = false;
33 protected string $title = "";
34 protected string $desc = "";
35 protected string $image = "";
36 protected string $valid = "";
37 protected ?array $config = null;
38 protected string $criteria = "";
39
40 public function __construct(
41 int $a_id = null
42 ) {
43 global $DIC;
44
45 $this->lng = $DIC->language();
46 $this->db = $DIC->database();
47 if ($a_id) {
48 $this->read($a_id);
49 }
50 }
51
57 public static function getInstancesByParentId(
58 int $a_parent_id,
59 array $a_filter = null
60 ): array {
61 global $DIC;
62
63 $ilDB = $DIC->database();
64
65 $res = [];
66
67 $sql = "SELECT * FROM badge_badge" .
68 " WHERE parent_id = " . $ilDB->quote($a_parent_id);
69
70 if ($a_filter) {
71 if ($a_filter["title"] ?? false) {
72 $sql .= " AND " . $ilDB->like("title", "text", "%" . trim($a_filter["title"]) . "%");
73 }
74 if ($a_filter["type"] ?? false) {
75 $sql .= " AND type_id = " . $ilDB->quote($a_filter["type"], "integer");
76 }
77 }
78
79 $set = $ilDB->query($sql .
80 " ORDER BY title");
81 while ($row = $ilDB->fetchAssoc($set)) {
82 $obj = new self();
83 $obj->importDBRow($row);
84 $res[] = $obj;
85 }
86
87 return $res;
88 }
89
94 public static function getInstancesByType(
95 string $a_type_id
96 ): array {
97 global $DIC;
98
99 $ilDB = $DIC->database();
100
101 $res = [];
102
103 $set = $ilDB->query("SELECT * FROM badge_badge" .
104 " WHERE type_id = " . $ilDB->quote($a_type_id, "text") .
105 " ORDER BY title");
106 while ($row = $ilDB->fetchAssoc($set)) {
107 $obj = new self();
108 $obj->importDBRow($row);
109 $res[] = $obj;
110 }
111
112 return $res;
113 }
114
115 public function clone(int $target_parent_obj_id): void
116 {
117 $this->setParentId($target_parent_obj_id);
118 $this->setActive(false);
119
120 if ($this->getId()) {
121 $img = $this->getImagePath();
122
123 $this->setId(0);
124 $this->create();
125
126 if ($img) {
127 // see uploadImage()
128 copy($img, $this->getImagePath());
129 }
130 }
131 }
132
133 public function getTypeInstance(): ?ilBadgeType
134 {
135 if ($this->getTypeId()) {
136 $handler = ilBadgeHandler::getInstance();
137 return $handler->getTypeInstanceByUniqueId($this->getTypeId());
138 }
139 return null;
140 }
141
142 public function copy(
143 int $a_new_parent_id
144 ): void {
145 $lng = $this->lng;
146
147 $this->setTitle($this->getTitle() . " " . $lng->txt("copy_of_suffix"));
148 $this->setParentId($a_new_parent_id);
149 $this->setActive(false);
150
151 if ($this->getId()) {
152 $img = $this->getImagePath();
153
154 $this->setId(0);
155 $this->create();
156
157 if ($img) {
158 // see uploadImage()
159 copy($img, $this->getImagePath());
160 }
161 }
162 }
163
168 public static function getObjectInstances(
169 array $a_filter = null
170 ): array {
171 global $DIC;
172
173 $ilDB = $DIC->database();
174
175 $res = $raw = [];
176
177 $where = "";
178
179 if ($a_filter["type"]) {
180 $where .= " AND bb.type_id = " . $ilDB->quote($a_filter["type"], "text");
181 }
182 if ($a_filter["title"]) {
183 $where .= " AND " . $ilDB->like("bb.title", "text", "%" . $a_filter["title"] . "%");
184 }
185 if ($a_filter["object"]) {
186 $where .= " AND " . $ilDB->like("od.title", "text", "%" . $a_filter["object"] . "%");
187 }
188
189 $set = $ilDB->query("SELECT bb.*, od.title parent_title, od.type parent_type" .
190 " FROM badge_badge bb" .
191 " JOIN object_data od ON (bb.parent_id = od.obj_id)" .
192 " WHERE od.type <> " . $ilDB->quote("bdga", "text") .
193 $where);
194 while ($row = $ilDB->fetchAssoc($set)) {
195 $raw[] = $row;
196 }
197
198 $set = $ilDB->query("SELECT bb.*, od.title parent_title, od.type parent_type" .
199 " FROM badge_badge bb" .
200 " JOIN object_data_del od ON (bb.parent_id = od.obj_id)" .
201 " WHERE od.type <> " . $ilDB->quote("bdga", "text") .
202 $where);
203 while ($row = $ilDB->fetchAssoc($set)) {
204 $row["deleted"] = true;
205 $raw[] = $row;
206 }
207
208 foreach ($raw as $row) {
209 $res[] = $row;
210 }
211
212 return $res;
213 }
214
215
216 //
217 // setter/getter
218 //
219
220 protected function setId(int $a_id): void
221 {
222 $this->id = $a_id;
223 }
224
225 public function getId(): int
226 {
227 return $this->id;
228 }
229
230 public function setParentId(int $a_id): void
231 {
232 $this->parent_id = $a_id;
233 }
234
235 public function getParentId(): int
236 {
237 return $this->parent_id;
238 }
239
240 public function setTypeId(string $a_id): void
241 {
242 $this->type_id = trim($a_id);
243 }
244
245 public function getTypeId(): string
246 {
247 return $this->type_id;
248 }
249
250 public function setActive(bool $a_value): void
251 {
252 $this->active = $a_value;
253 }
254
255 public function isActive(): bool
256 {
257 return $this->active;
258 }
259
260 public function setTitle(string $a_value): void
261 {
262 $this->title = trim($a_value);
263 }
264
265 public function getTitle(): string
266 {
267 return $this->title;
268 }
269
270 public function setDescription(string $a_value): void
271 {
272 $this->desc = trim($a_value);
273 }
274
275 public function getDescription(): string
276 {
277 return $this->desc;
278 }
279
280 public function setCriteria(string $a_value): void
281 {
282 $this->criteria = trim($a_value);
283 }
284
285 public function getCriteria(): string
286 {
287 return $this->criteria;
288 }
289
290 public function setValid(string $a_value): void
291 {
292 $this->valid = trim($a_value);
293 }
294
295 public function getValid(): string
296 {
297 return $this->valid;
298 }
299
300 public function setConfiguration(array $a_value = null): void
301 {
302 if (is_array($a_value) && !count($a_value)) {
303 $a_value = null;
304 }
305 $this->config = $a_value;
306 }
307
308 public function getConfiguration(): ?array
309 {
310 return $this->config;
311 }
312
313 protected function setImage(string $a_value): void
314 {
315 $this->image = trim($a_value);
316 }
317
318 public function getImage(): string
319 {
320 return $this->image;
321 }
322
326 public function uploadImage(
327 array $a_upload_meta
328 ): void {
329 if ($this->getId() &&
330 $a_upload_meta["tmp_name"]) {
331 $this->setImage($a_upload_meta["name"]);
332 $path = $this->getImagePath();
333
334 try {
335 if (ilFileUtils::moveUploadedFile($a_upload_meta['tmp_name'], $this->getImagePath(false), $path)) {
336 $this->update();
337 }
338 } catch (ilException $e) {
339 throw BadgeException::moveUploadedBadgeImageFailed($this, $e);
340 }
341
342 }
343 }
344
348 public function importImage(
349 string $a_name,
350 string $a_file
351 ): void {
352 if (file_exists($a_file)) {
353 $this->setImage($a_name);
354 copy($a_file, $this->getImagePath()); // #18280
355
356 $this->update();
357 } else {
358 throw BadgeException::uploadedBadgeImageFileNotFound($this);
359 }
360 }
361
362 public function getImagePath(
363 bool $a_full_path = true
364 ): string {
365 if ($this->getId()) {
366 $exp = explode(".", $this->getImage());
367 $suffix = strtolower(array_pop($exp));
368 if ($a_full_path) {
369 return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
370 }
371
372 return "img" . $this->getId() . "." . $suffix;
373 }
374 return "";
375 }
376
377 protected function getFilePath(
378 int $a_id,
379 string $a_subdir = null
380 ): string {
381 $storage = new ilFSStorageBadge($a_id);
382 $storage->create();
383
384 $path = $storage->getAbsolutePath() . "/";
385
386 if ($a_subdir) {
387 $path .= $a_subdir . "/";
388
389 if (!is_dir($path)) {
390 mkdir($path);
391 }
392 }
393
394 return $path;
395 }
396
397
398 //
399 // crud
400 //
401
402 protected function read(int $a_id): void
403 {
404 $ilDB = $this->db;
405
406 $set = $ilDB->query("SELECT * FROM badge_badge" .
407 " WHERE id = " . $ilDB->quote($a_id, "integer"));
408 if ($ilDB->numRows($set)) {
409 $row = $ilDB->fetchAssoc($set);
410 $this->importDBRow($row);
411 }
412 }
413
414 protected function importDBRow(
415 array $a_row
416 ): void {
417 $this->setId($a_row["id"]);
418 $this->setParentId($a_row["parent_id"]);
419 $this->setTypeId($a_row["type_id"]);
420 $this->setActive($a_row["active"]);
421 $this->setTitle($a_row["title"]);
422 $this->setDescription($a_row["descr"]);
423 $this->setCriteria($a_row["crit"]);
424 $this->setImage($a_row["image"]);
425 $this->setValid($a_row["valid"]);
426 $this->setConfiguration($a_row["conf"]
427 ? unserialize($a_row["conf"], ["allowed_classes" => false])
428 : null);
429 }
430
431 public function create(): void
432 {
433 $ilDB = $this->db;
434
435 if ($this->getId()) {
436 $this->update();
437 return;
438 }
439
440 $id = $ilDB->nextId("badge_badge");
441 $this->setId($id);
442
443 $fields = $this->getPropertiesForStorage();
444
445 $fields["id"] = ["integer", $id];
446 $fields["parent_id"] = ["integer", $this->getParentId()];
447 $fields["type_id"] = ["text", $this->getTypeId()];
448
449 $ilDB->insert("badge_badge", $fields);
450 }
451
452 public function update(): void
453 {
454 $ilDB = $this->db;
455
456 if (!$this->getId()) {
457 $this->create();
458 return;
459 }
460
461 $fields = $this->getPropertiesForStorage();
462
463 $ilDB->update(
464 "badge_badge",
465 $fields,
466 ["id" => ["integer", $this->getId()]]
467 );
468 }
469
470 public function delete(): void
471 {
472 $ilDB = $this->db;
473
474 if (!$this->getId()) {
475 return;
476 }
477
478 if (file_exists($this->getImagePath())) {
479 unlink($this->getImagePath());
480 }
481
482 $this->deleteStaticFiles();
483
485
486 $ilDB->manipulate("DELETE FROM badge_badge" .
487 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
488 }
489
493 protected function getPropertiesForStorage(): array
494 {
495 return [
496 "active" => ["integer", $this->isActive()],
497 "title" => ["text", $this->getTitle()],
498 "descr" => ["text", $this->getDescription()],
499 "crit" => ["text", $this->getCriteria()],
500 "image" => ["text", $this->getImage()],
501 "valid" => ["text", $this->getValid()],
502 "conf" => [
503 "text", $this->getConfiguration() ? serialize($this->getConfiguration()) : null
504 ]
505 ];
506 }
507
508
509 //
510 // helper
511 //
512
516 public function getParentMeta(): array
517 {
518 $parent_type = ilObject::_lookupType($this->getParentId());
519 $parent_title = "";
520 if ($parent_type) {
521 $parent_title = ilObject::_lookupTitle($this->getParentId());
522 $deleted = false;
523 } else {
524 // already deleted?
525 $parent = ilObjectDataDeletionLog::get($this->getParentId());
526 if ($parent["type"]) {
527 $parent_type = $parent["type"];
528 $parent_title = $parent["title"];
529 }
530 $deleted = true;
531 }
532
533 return [
534 "id" => $this->getParentId(),
535 "type" => $parent_type,
536 "title" => $parent_title,
537 "deleted" => $deleted
538 ];
539 }
540
541
542 //
543 // PUBLISHING
544 //
545
546 protected function prepareJson(
547 string $a_base_url,
548 string $a_img_suffix
549 ): stdClass {
550 $json = new stdClass();
551 $json->{"@context"} = "https://w3id.org/openbadges/v1";
552 $json->type = "BadgeClass";
553 $json->id = $a_base_url . "class.json";
554 $json->name = $this->getTitle();
555 $json->description = $this->getDescription();
556 $json->image = $a_base_url . "image." . $a_img_suffix;
557 $json->criteria = $a_base_url . "criteria.txt";
558 $json->issuer = ilBadgeHandler::getInstance()->getIssuerStaticUrl();
559
560 return $json;
561 }
562
563
564 public function deleteStaticFiles(): void
565 {
566 // remove instance files
567 $path = ilBadgeHandler::getInstance()->getBadgePath($this);
568 if (is_dir($path)) {
570 }
571 }
572
573 public static function getExtendedTypeCaption(
574 ilBadgeType $a_type
575 ): string {
576 global $DIC;
577
578 $lng = $DIC->language();
579
580 return $a_type->getCaption() . " (" .
581 ($a_type instanceof ilBadgeAuto
582 ? $lng->txt("badge_subtype_auto")
583 : $lng->txt("badge_subtype_manual")) . ")";
584 }
585}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static deleteByBadgeId(int $a_badge_id)
setCriteria(string $a_value)
string $criteria
string $valid
setId(int $a_id)
string $image
setConfiguration(array $a_value=null)
clone(int $target_parent_obj_id)
deleteStaticFiles()
ilLanguage $lng
uploadImage(array $a_upload_meta)
string $desc
string $type_id
__construct(int $a_id=null)
static getInstancesByType(string $a_type_id)
getImagePath(bool $a_full_path=true)
static getInstancesByParentId(int $a_parent_id, array $a_filter=null)
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)
setImage(string $a_value)
string $title
setParentId(int $a_id)
getConfiguration()
copy(int $a_new_parent_id)
read(int $a_id)
ilDBInterface $db
getFilePath(int $a_id, string $a_subdir=null)
int $parent_id
setDescription(string $a_value)
getPropertiesForStorage()
importDBRow(array $a_row)
static getObjectInstances(array $a_filter=null)
setTypeId(string $a_id)
bool $active
importImage(string $a_name, string $a_file)
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...
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
language handling
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
$valid
global $DIC
Definition: feed.php:28
$img
Definition: imgupload.php:83
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.
$path
Definition: ltiservices.php:32
$res
Definition: ltiservices.php:69
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
$lng