ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBadgeImage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
23use ilBadge;
30
32{
33 public const IMAGE_SIZE_XS = 4;
34 public const IMAGE_SIZE_S = 3;
35 public const IMAGE_SIZE_M = 2;
36 public const IMAGE_SIZE_L = 1;
37 public const IMAGE_SIZE_XL = 0;
38 public const IMAGE_URL_COUNT = 5;
42
43 public function __construct(
44 Services $resourceStorage,
45 FileUpload $uploadService,
47 ) {
48 $this->resource_storage = $resourceStorage;
49 $this->upload_service = $uploadService;
50 $this->main_template = $main_template;
51 }
52
53 public function getImageFromBadge(ilBadge $badge, int $size = self::IMAGE_SIZE_XS): string
54 {
55 return $this->getImageFromResourceId($badge, $size);
56 }
57
58 public function getImageFromResourceId(
59 ilBadge $badge,
60 int $size = self::IMAGE_SIZE_XS
61 ): string {
62 $image_rid = $badge->getImageRid();
63 if ($image_rid === null) {
64 return '';
65 }
66
67 $identification = $this->resource_storage->manage()->find($image_rid);
68 if ($identification === null) {
69 return '';
70 }
71
72 return $this->resource_storage->consume()->src($identification)->getSrc();
73 }
74
75 public function processImageUpload(ilBadge $badge): void
76 {
77 try {
78 $array_result = $this->upload_service->getResults();
79 $array_result = array_pop($array_result);
80 $stakeholder = new ilBadgeFileStakeholder();
81 $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
82 $badge->setImageRid($identification->serialize());
83 $badge->update();
84 } catch (IllegalStateException $e) {
85 $this->main_template->setOnScreenMessage('failure', $e->getMessage(), true);
86 }
87 }
88
92 public function cloneBadgeImageByRid(ResourceIdentification $identification): string
93 {
94 return $this->resource_storage->manage()->clone($identification)->serialize();
95 }
96}
ilGlobalTemplateInterface $main_template
getImageFromResourceId(ilBadge $badge, int $size=self::IMAGE_SIZE_XS)
cloneBadgeImageByRid(ResourceIdentification $identification)
processImageUpload(ilBadge $badge)
__construct(Services $resourceStorage, FileUpload $uploadService, ilGlobalTemplateInterface $main_template)
getImageFromBadge(ilBadge $badge, int $size=self::IMAGE_SIZE_XS)
Class Services.
Definition: Services.php:43
setImageRid(?string $image_rid)