ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBadgeImage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
23use ilBadge;
31use Exception;
32
34{
35 public const IMAGE_SIZE_XS = 4;
36 public const IMAGE_SIZE_S = 3;
37 public const IMAGE_SIZE_M = 2;
38 public const IMAGE_SIZE_L = 1;
39 public const IMAGE_SIZE_XL = 0;
40 public const IMAGE_URL_COUNT = 5;
41
45
46 public function __construct(
47 Services $resourceStorage,
48 FileUpload $uploadService,
50 ) {
51 $this->resource_storage = $resourceStorage;
52 $this->upload_service = $uploadService;
53 $this->main_template = $main_template;
54 }
55
56 public function getImageFromBadge(ilBadge $badge, int $size = self::IMAGE_SIZE_XS): string
57 {
58 return $this->getImageFromResourceId($badge, $size);
59 }
60
61 public function getImageFromResourceId(
62 ilBadge $badge,
63 int $size = self::IMAGE_SIZE_XS
64 ): string {
65 $image_src = '';
66
67 if ($badge->getImageRid() !== '' && $badge->getImageRid() !== null) {
68 $identification = $this->resource_storage->manage()->find($badge->getImageRid());
69 if ($identification !== null) {
70 $flavour = $this->resource_storage->flavours()->get($identification, new \ilBadgePictureDefinition());
71 $urls = $this->resource_storage->consume()->flavourUrls($flavour)->getURLsAsArray();
72 if (\count($urls) === self::IMAGE_URL_COUNT && isset($urls[$size])) {
73 $image_src = $urls[$size];
74 }
75 }
76 } elseif ($badge->getImage()) {
77 $image_src = ilWACSignedPath::signFile($badge->getImagePath());
78 }
79
80 return $image_src;
81 }
82
83
84 public function processImageUpload(ilBadge $badge): void
85 {
86 try {
87 $array_result = $this->upload_service->getResults();
88 $array_result = array_pop($array_result);
89 $stakeholder = new ilBadgeFileStakeholder();
90 $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
91 $this->resource_storage->flavours()->ensure($identification, new \ilBadgePictureDefinition());
92 $badge->setImageRid($identification->serialize());
93 $badge->update();
94 } catch (IllegalStateException $e) {
95 $this->main_template->setOnScreenMessage('failure', $e->getMessage(), true);
96 }
97 }
98
102 public function cloneBadgeImageByRid(ResourceIdentification $identification): string
103 {
104 return $this->resource_storage->manage()->clone($identification)->serialize();
105 }
106}
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:38
getImagePath(bool $a_full_path=true)
setImageRid(?string $image_rid)
Class ilWACSignedPath.
static signFile(string $path_to_file)