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