ILIAS  release_8 Revision v8.23
class.ilPortfolioCertificateFileService.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
30 {
32  private ilLogger $logger;
33  private const PERSISTENT_CERTIFICATES_DIRECTORY = 'PersistentCertificates/';
34  private const CERTIFICATE_FILENAME = 'certificate.pdf';
35 
36  public function __construct(?Filesystem $filesystem = null, ?ilLogger $logger = null)
37  {
38  global $DIC;
39 
40  if (null === $filesystem) {
41  $filesystem = $DIC->filesystem()->storage();
42  }
43  $this->filesystem = $filesystem;
44 
45  if (null === $logger) {
46  $logger = $DIC->logger()->root();
47  }
48  $this->logger = $logger;
49  }
50 
58  public function createCertificateFile(int $userId, int $objectId): void
59  {
60  $userCertificateRepository = new ilUserCertificateRepository();
61 
62  $userCertificate = $userCertificateRepository->fetchActiveCertificate($userId, $objectId);
63 
64  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
65  if (false === $this->filesystem->hasDir($dirPath)) {
66  $this->filesystem->createDir($dirPath);
67  }
68 
69  $pdfGenerator = new ilPdfGenerator($userCertificateRepository, $this->logger);
70 
71  $pdfScalar = $pdfGenerator->generate($userCertificate->getId());
72 
73  $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
74  if ($this->filesystem->has($completePath)) {
75  $this->filesystem->delete($completePath);
76  }
77 
78  $this->filesystem->write($completePath, $pdfScalar);
79  }
80 
87  public function deliverCertificate(int $userId, int $objectId): void
88  {
89  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
90  $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
91 
92  $completePath = $dirPath . '/' . $fileName;
93  if ($this->filesystem->has($completePath)) {
94  $userCertificateRepository = new ilUserCertificateRepository();
95 
96  $userCertificate = $userCertificateRepository->fetchActiveCertificateForPresentation($userId, $objectId);
97 
98  $downloadFilePath = CLIENT_DATA_DIR . '/' . $completePath;
100  $downloadFilePath,
101  ilFileUtils::getValidFilename($userCertificate->getObjectTitle() . '.pdf')
102  );
103  }
104  }
105 
110  public function deleteUserDirectory(int $userId): void
111  {
112  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
113 
114  if (true === $this->filesystem->hasDir($dirPath)) {
115  $this->filesystem->deleteDir($dirPath);
116  }
117  }
118 
125  public function deleteCertificateFile(int $userId, int $objectId): void
126  {
127  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
128 
129  $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
130 
131  if ($this->filesystem->has($completePath)) {
132  $this->filesystem->delete($completePath);
133  }
134  }
135 
142  public function createCertificateFilePath(int $userId, int $objectId): string
143  {
144  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId . '/';
145  $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
146  $completePath = $dirPath . $fileName;
147  if ($this->filesystem->has($completePath)) {
148  return CLIENT_DATA_DIR . '/' . $completePath;
149  }
150 
151  throw new ilException(sprintf('Certificate File does not exist in "%s"', $completePath));
152  }
153 }
__construct(?Filesystem $filesystem=null, ?ilLogger $logger=null)
Class ilPdfGeneratorConstantsTest.
static getValidFilename(string $a_filename)
global $DIC
Definition: feed.php:28
const CLIENT_DATA_DIR
Definition: constants.php:46
static deliverFileAttached(string $path_to_file, ?string $download_file_name=null, ?string $mime_type=null, bool $delete_file=false)
Class FlySystemFileAccessTest disabled disabled disabled.