ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilBadgeImage.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Badge;
22 
23 use ilBadge;
29 use ilWACSignedPath;
31 use 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,
49  ilGlobalTemplateInterface $main_template
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 instanceof ilBadge) {
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 }
cloneBadgeImageByRid(ResourceIdentification $identification)
getImageFromResourceId(ilBadge $badge, int $size=self::IMAGE_SIZE_XS)
processImageUpload(ilBadge $badge)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getImageFromBadge(ilBadge $badge, int $size=self::IMAGE_SIZE_XS)
ilGlobalTemplateInterface $main_template
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getImagePath(bool $a_full_path=true)
static signFile(string $path_to_file)
__construct(Services $resourceStorage, FileUpload $uploadService, ilGlobalTemplateInterface $main_template)
setImageRid(?string $image_rid)