ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilBadge.php
Go to the documentation of this file.
1 <?php
2 
23 
24 class ilBadge
25 {
26  private ilLogger $log;
27  protected ilDBInterface $db;
28  protected int $id = 0;
29  protected int $parent_id = 0;
30  protected string $type_id = "";
31  protected bool $active = false;
32  protected string $title = "";
33  protected string $desc = "";
34  protected string $image = "";
35  protected ?string $image_rid = null;
36  protected string $valid = "";
37  protected ?array $config = null;
38  protected string $criteria = "";
39 
41 
42  public function __construct(
43  ?int $a_id = null,
45  ) {
46 
47  if ($container === null) {
48  global $DIC;
49  $container = $DIC;
50  }
51 
52  $this->db = $container->database();
53  $this->resource_storage = $container->resourceStorage();
54  $this->log = $container->logger()->root();
55  if ($a_id) {
56  $this->read($a_id);
57  }
58  }
59 
64  public static function getInstancesByParentId(
65  int $a_parent_id,
66  ?array $a_filter = null
67  ): array {
68  global $DIC;
69 
70  $ilDB = $DIC->database();
71 
72  $res = [];
73 
74  $sql = "SELECT * FROM badge_badge" .
75  " WHERE parent_id = " . $ilDB->quote($a_parent_id);
76 
77  if ($a_filter) {
78  if ($a_filter["title"] ?? false) {
79  $sql .= " AND " . $ilDB->like("title", "text", "%" . trim($a_filter["title"]) . "%");
80  }
81  if ($a_filter["type"] ?? false) {
82  $sql .= " AND type_id = " . $ilDB->quote($a_filter["type"], "integer");
83  }
84  }
85 
86  $set = $ilDB->query($sql .
87  " ORDER BY title");
88  while ($row = $ilDB->fetchAssoc($set)) {
89  $obj = new self();
90  $obj->importDBRow($row);
91  $res[] = $obj;
92  }
93 
94  return $res;
95  }
96 
100  public static function getInstancesByType(
101  string $a_type_id
102  ): array {
103  global $DIC;
104 
105  $ilDB = $DIC->database();
106 
107  $res = [];
108 
109  $set = $ilDB->query("SELECT * FROM badge_badge" .
110  " WHERE type_id = " . $ilDB->quote($a_type_id, "text") .
111  " ORDER BY title");
112  while ($row = $ilDB->fetchAssoc($set)) {
113  $obj = new self();
114  $obj->importDBRow($row);
115  $res[] = $obj;
116  }
117 
118  return $res;
119  }
120 
121  public function clone(int $target_parent_obj_id): void
122  {
123  $this->setParentId($target_parent_obj_id);
124  $this->setActive(false);
125 
126  if ($this->getId()) {
127  if ($this->getImageRid()) {
128  $current_collection_id = new ResourceIdentification($this->getImageRid());
129  $new_collection_id = $this->resource_storage->manage()->clone($current_collection_id);
130  $this->setId(0);
131  $this->setImageRid($new_collection_id);
132  $this->create();
133  } else {
134  $this->log->warning('Please run the "Migration of files of badges to the resource storage service" job, before working with badges.');
135  }
136  }
137  }
138 
139  public function getTypeInstance(): ?ilBadgeType
140  {
141  if ($this->getTypeId()) {
143  return $handler->getTypeInstanceByUniqueId($this->getTypeId());
144  }
145  return null;
146  }
147 
148  public function copy(
149  int $a_new_parent_id,
150  string $copy_suffix
151  ): void {
152  $this->setTitle($this->getTitle() . " " . $copy_suffix);
153  $this->setParentId($a_new_parent_id);
154  $this->setActive(false);
155 
156  if ($this->getId()) {
157  $this->setId(0);
158  $old_rid = $this->getImageRid();
159  $this->create();
160  if ($old_rid !== null) {
161  $new_rid = $this->resource_storage->manage()->clone(new ResourceIdentification($old_rid));
162  $this->setImageRid($new_rid);
163  $this->update();
164  }
165  }
166  }
167 
187  public static function getObjectInstances(
188  ?array $filter = null
189  ): array {
190  global $DIC;
191 
192  $ilDB = $DIC->database();
193 
194  $rows = [];
195 
196  $where = '';
197 
198  if ($filter['type']) {
199  $where .= ' AND bb.type_id = ' . $ilDB->quote($filter['type'], ilDBConstants::T_TEXT);
200  }
201  if ($filter['title']) {
202  $where .= ' AND ' . $ilDB->like('bb.title', ilDBConstants::T_TEXT, '%' . $filter['title'] . '%');
203  }
204  if ($filter['object']) {
205  $where .= ' AND ' . $ilDB->like('od.title', ilDBConstants::T_TEXT, '%' . $filter['object'] . '%');
206  }
207 
208  $set = $ilDB->query('SELECT bb.*, od.title parent_title, od.type parent_type' .
209  ' FROM badge_badge bb' .
210  ' INNER JOIN object_data od ON bb.parent_id = od.obj_id' .
211  ' WHERE od.type != ' . $ilDB->quote('bdga', ilDBConstants::T_TEXT) .
212  $where);
213  while ($row = $ilDB->fetchAssoc($set)) {
214  $row['deleted'] = false;
215  $rows[] = $row;
216  }
217 
218  $set = $ilDB->query('SELECT bb.*, od.title parent_title, od.type parent_type' .
219  ' FROM badge_badge bb' .
220  ' INNER JOIN object_data_del od ON bb.parent_id = od.obj_id' .
221  ' WHERE od.type != ' . $ilDB->quote('bdga', ilDBConstants::T_TEXT) .
222  $where);
223  while ($row = $ilDB->fetchAssoc($set)) {
224  $row['deleted'] = true;
225  $rows[] = $row;
226  }
227 
228  return $rows;
229  }
230 
231  //
232  // setter/getter
233  //
234 
235  public function setId(int $a_id): void
236  {
237  $this->id = $a_id;
238  }
239 
240  public function getId(): int
241  {
242  return $this->id;
243  }
244 
245  public function setParentId(int $a_id): void
246  {
247  $this->parent_id = $a_id;
248  }
249 
250  public function getParentId(): int
251  {
252  return $this->parent_id;
253  }
254 
255  public function setTypeId(string $a_id): void
256  {
257  $this->type_id = trim($a_id);
258  }
259 
260  public function getTypeId(): string
261  {
262  return $this->type_id;
263  }
264 
265  public function setActive(bool $a_value): void
266  {
267  $this->active = $a_value;
268  }
269 
270  public function isActive(): bool
271  {
272  return $this->active;
273  }
274 
275  public function setTitle(string $a_value): void
276  {
277  $this->title = trim($a_value);
278  }
279 
280  public function getTitle(): string
281  {
282  return $this->title;
283  }
284 
285  public function setDescription(string $a_value): void
286  {
287  $this->desc = trim($a_value);
288  }
289 
290  public function getDescription(): string
291  {
292  return $this->desc;
293  }
294 
295  public function setCriteria(string $a_value): void
296  {
297  $this->criteria = trim($a_value);
298  }
299 
300  public function getCriteria(): string
301  {
302  return $this->criteria;
303  }
304 
305  public function setValid(string $a_value): void
306  {
307  $this->valid = trim($a_value);
308  }
309 
310  public function getValid(): string
311  {
312  return $this->valid;
313  }
314 
315  public function setConfiguration(?array $a_value = null): void
316  {
317  if (is_array($a_value) && !count($a_value)) {
318  $a_value = null;
319  }
320  $this->config = $a_value;
321  }
322 
323  public function getConfiguration(): ?array
324  {
325  return $this->config;
326  }
327 
328  public function setImage(?string $a_value): void
329  {
330  if ($a_value !== null) {
331  $this->image = trim($a_value);
332  }
333  }
334 
335  public function getImage(): string
336  {
337  return $this->image;
338  }
339 
343  public function uploadImage(
344  array $a_upload_meta
345  ): void {
346  if ($this->getId() &&
347  $a_upload_meta["tmp_name"]) {
348  $this->setImage($a_upload_meta["name"]);
349  $path = $this->getImagePath();
350 
351  try {
352  if (ilFileUtils::moveUploadedFile($a_upload_meta['tmp_name'], $this->getImagePath(false), $path)) {
353  $this->update();
354  }
355  } catch (ilException $e) {
356  throw BadgeException::moveUploadedBadgeImageFailed($this, $e);
357  }
358 
359  }
360  }
361 
365  public function importImage(
366  string $a_name,
367  string $a_file
368  ): void {
369  if (file_exists($a_file)) {
370  $this->setImage($a_name);
371  copy($a_file, $this->getImagePath()); // #18280
372 
373  $this->update();
374  } else {
375  throw BadgeException::uploadedBadgeImageFileNotFound($this);
376  }
377  }
378 
379  public function getImagePath(
380  bool $a_full_path = true
381  ): string {
382  if ($this->getId()) {
383  $exp = explode(".", $this->getImage());
384  $suffix = strtolower(array_pop($exp));
385  if ($a_full_path) {
386  return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
387  }
388 
389  return "img" . $this->getId() . "." . $suffix;
390  }
391 
392  return "";
393  }
394 
395  protected function getFilePath(
396  int $a_id,
397  ?string $a_subdir = null
398  ): string {
399  $storage = new ilFSStorageBadge($a_id);
400  $storage->create();
401 
402  $path = $storage->getAbsolutePath() . "/";
403 
404  if ($a_subdir) {
405  $path .= $a_subdir . "/";
406 
407  if (!is_dir($path)) {
408  mkdir($path);
409  }
410  }
411 
412  return $path;
413  }
414 
415 
416  //
417  // crud
418  //
419 
420  protected function read(int $a_id): void
421  {
422  $ilDB = $this->db;
423 
424  $set = $ilDB->query("SELECT * FROM badge_badge" .
425  " WHERE id = " . $ilDB->quote($a_id, "integer"));
426  if ($ilDB->numRows($set)) {
427  $row = $ilDB->fetchAssoc($set);
428  $this->importDBRow($row);
429  }
430  }
431 
432  protected function importDBRow(
433  array $a_row
434  ): void {
435  $this->setId($a_row["id"]);
436  $this->setParentId($a_row["parent_id"]);
437  $this->setTypeId($a_row["type_id"]);
438  $this->setActive($a_row["active"]);
439  $this->setTitle($a_row["title"]);
440  $this->setDescription($a_row["descr"]);
441  $this->setCriteria($a_row["crit"]);
442  $this->setImage($a_row["image"]);
443  $this->setImageRid($a_row["image_rid"]);
444  $this->setValid($a_row["valid"]);
445  $this->setConfiguration($a_row["conf"]
446  ? unserialize($a_row["conf"], ["allowed_classes" => false])
447  : null);
448  }
449 
450  public function create(): void
451  {
452  $ilDB = $this->db;
453 
454  if ($this->getId()) {
455  $this->update();
456  return;
457  }
458 
459  $id = $ilDB->nextId("badge_badge");
460  $this->setId($id);
461 
462  $fields = $this->getPropertiesForStorage();
463 
464  $fields["id"] = ["integer", $id];
465  $fields["parent_id"] = ["integer", $this->getParentId()];
466  $fields["type_id"] = ["text", $this->getTypeId()];
467 
468  $ilDB->insert("badge_badge", $fields);
469  }
470 
471  public function update(): void
472  {
473  $ilDB = $this->db;
474 
475  if (!$this->getId()) {
476  $this->create();
477  return;
478  }
479 
480  $fields = $this->getPropertiesForStorage();
481 
482  $ilDB->update(
483  "badge_badge",
484  $fields,
485  ["id" => ["integer", $this->getId()]]
486  );
487  }
488 
489  public function delete(): void
490  {
491  $ilDB = $this->db;
492 
493  if (!$this->getId()) {
494  return;
495  }
496 
497  if ($this->getImageRid()) {
498  try {
499  $this->resource_storage->manage()->remove(new ResourceIdentification($this->getImageRid()), new ilBadgeFileStakeholder());
500  } catch (Exception $e) {
501  $this->log->warning(sprintf('There was an exception, while deleting the badge with id %s. Exception: %s', $this->getId(), $e->getMessage()));
502  }
503  }
504 
505  $this->deleteStaticFiles();
506 
508 
509  $ilDB->manipulate("DELETE FROM badge_badge" .
510  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
511  }
512 
516  protected function getPropertiesForStorage(): array
517  {
518  return [
519  "active" => ["integer", $this->isActive()],
520  "title" => ["text", $this->getTitle()],
521  "descr" => ["text", $this->getDescription()],
522  "crit" => ["text", $this->getCriteria()],
523  "image" => ["text", $this->getImage()],
524  "image_rid" => ["text", $this->getImageRid()],
525  "valid" => ["text", $this->getValid()],
526  "conf" => [
527  "text", $this->getConfiguration() ? serialize($this->getConfiguration()) : null
528  ]
529  ];
530  }
531 
532 
533  //
534  // helper
535  //
536 
540  public function getParentMeta(): array
541  {
542  $parent_type = ilObject::_lookupType($this->getParentId());
543  $parent_title = "";
544  if ($parent_type) {
545  $parent_title = ilObject::_lookupTitle($this->getParentId());
546  $deleted = false;
547  } else {
548  // already deleted?
549  $parent = ilObjectDataDeletionLog::get($this->getParentId());
550  if ($parent["type"]) {
551  $parent_type = $parent["type"];
552  $parent_title = $parent["title"];
553  }
554  $deleted = true;
555  }
556 
557  return [
558  "id" => $this->getParentId(),
559  "type" => $parent_type,
560  "title" => $parent_title,
561  "deleted" => $deleted
562  ];
563  }
564 
565 
566  //
567  // PUBLISHING
568  //
569 
570  protected function prepareJson(
571  string $a_base_url,
572  string $a_img_suffix
573  ): stdClass {
574  $json = new stdClass();
575  $json->{"@context"} = "https://w3id.org/openbadges/v1";
576  $json->type = "BadgeClass";
577  $json->id = $a_base_url . "class.json";
578  $json->name = $this->getTitle();
579  $json->description = $this->getDescription();
580  $json->image = $a_base_url . "image." . $a_img_suffix;
581  $json->criteria = $a_base_url . "criteria.txt";
582  $json->issuer = ilBadgeHandler::getInstance()->getIssuerStaticUrl();
583 
584  return $json;
585  }
586 
587 
588  public function deleteStaticFiles(): void
589  {
590  // remove instance files
591  $path = ilBadgeHandler::getInstance()->getBadgePath($this);
592  if (is_dir($path)) {
594  }
595  }
596 
597  public static function getExtendedTypeCaption(
598  ilBadgeType $a_type
599  ): string {
600  global $DIC;
601 
602  $lng = $DIC->language();
603 
604  return $a_type->getCaption() . " (" .
605  ($a_type instanceof ilBadgeAuto
606  ? $lng->txt("badge_subtype_auto")
607  : $lng->txt("badge_subtype_manual")) . ")";
608  }
609 
610  public function getImageRid(): ?string
611  {
612  return $this->image_rid;
613  }
614 
615  public function setImageRid(?string $image_rid): void
616  {
617  $this->image_rid = $image_rid;
618  }
619 }
getPropertiesForStorage()
uploadImage(array $a_upload_meta)
$res
Definition: ltiservices.php:66
static getInstancesByType(string $a_type_id)
getConfiguration()
getFilePath(int $a_id, ?string $a_subdir=null)
static getInstancesByParentId(int $a_parent_id, ?array $a_filter=null)
__construct(?int $a_id=null, ?Container $container=null)
prepareJson(string $a_base_url, string $a_img_suffix)
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...
setValid(string $a_value)
ilLogger $log
string $type_id
setId(int $a_id)
string $valid
deleteStaticFiles()
string $desc
int $parent_id
setTitle(string $a_value)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
clone(int $target_parent_obj_id)
string $criteria
$path
Definition: ltiservices.php:29
$container
Definition: wac.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setImage(?string $a_value)
setDescription(string $a_value)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupTitle(int $obj_id)
static getExtendedTypeCaption(ilBadgeType $a_type)
string $image
setConfiguration(?array $a_value=null)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
global $DIC
Definition: shib_login.php:22
bool $active
getImagePath(bool $a_full_path=true)
array $config
copy(int $a_new_parent_id, string $copy_suffix)
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)
setParentId(int $a_id)
$handler
Definition: oai.php:30
static deleteByBadgeId(int $a_badge_id)
global $lng
Definition: privfeed.php:31
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)
static getObjectInstances(?array $filter=null)
string $image_rid
setActive(bool $a_value)
static _lookupType(int $id, bool $reference=false)
importDBRow(array $a_row)
string $title
ilDBInterface $db
setImageRid(?string $image_rid)
Services $resource_storage