ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPortfolioCertificateFileService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
30{
31 private readonly Filesystem $filesystem;
32 private const PERSISTENT_CERTIFICATES_DIRECTORY = 'PersistentCertificates/';
33 private const CERTIFICATE_FILENAME = 'certificate.pdf';
34
35 public function __construct(?Filesystem $filesystem = null)
36 {
37 global $DIC;
38
39 if (null === $filesystem) {
40 $filesystem = $DIC->filesystem()->storage();
41 }
42 $this->filesystem = $filesystem;
43 }
44
50 public function createCertificateFile(int $userId, int $objectId): void
51 {
52 $userCertificateRepository = new ilUserCertificateRepository();
53
54 $userCertificate = $userCertificateRepository->fetchActiveCertificate($userId, $objectId);
55
56 $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
57 if (!$this->filesystem->hasDir($dirPath)) {
58 $this->filesystem->createDir($dirPath);
59 }
60
61 $pdfGenerator = new ilPdfGenerator($userCertificateRepository);
62
63 $pdfScalar = $pdfGenerator->generate($userCertificate->getId());
64
65 $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
66 if ($this->filesystem->has($completePath)) {
67 $this->filesystem->delete($completePath);
68 }
69
70 $this->filesystem->write($completePath, $pdfScalar);
71 }
72
77 public function deliverCertificate(int $userId, int $objectId): void
78 {
79 $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
80 $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
81
82 $completePath = $dirPath . '/' . $fileName;
83 if ($this->filesystem->has($completePath)) {
84 $userCertificateRepository = new ilUserCertificateRepository();
85
86 $userCertificate = $userCertificateRepository->fetchActiveCertificateForPresentation($userId, $objectId);
87
88 $downloadFilePath = CLIENT_DATA_DIR . '/' . $completePath;
90 $downloadFilePath,
91 ilFileUtils::getValidFilename($userCertificate->getObjectTitle() . '.pdf')
92 );
93 }
94 }
95
99 public function deleteUserDirectory(int $userId): void
100 {
101 $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
102
103 if ($this->filesystem->hasDir($dirPath)) {
104 $this->filesystem->deleteDir($dirPath);
105 }
106 }
107
112 public function deleteCertificateFile(int $userId, int $objectId): void
113 {
114 $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
115
116 $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
117
118 if ($this->filesystem->has($completePath)) {
119 $this->filesystem->delete($completePath);
120 }
121 }
122
126 public function createCertificateFilePath(int $userId, int $objectId): string
127 {
128 $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId . '/';
129 $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
130 $completePath = $dirPath . $fileName;
131 if ($this->filesystem->has($completePath)) {
132 return CLIENT_DATA_DIR . '/' . $completePath;
133 }
134
135 throw new ilException(sprintf('Certificate File does not exist in "%s"', $completePath));
136 }
137}
Indicates that a file is missing or not found.
Indicates general problems with the input or output operations.
Definition: IOException.php:28
Base class for ILIAS Exception handling.
static deliverFileAttached(string $path_to_file, ?string $download_file_name=null, ?string $mime_type=null, bool $delete_file=false)
static getValidFilename(string $a_filename)
const CLIENT_DATA_DIR
Definition: constants.php:46
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
global $DIC
Definition: shib_login.php:26