ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilPortfolioCertificateFileService.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 
10 {
14  private $filesystem;
15 
19  private $logger;
20 
25  const PERSISTENT_CERTIFICATES_DIRECTORY = 'PersistentCertificates/';
26 
27  const CERTIFICATE_FILENAME = 'certificate.pdf';
28 
29  public function __construct(Filesystem $filesystem = null, Logger $logger = null)
30  {
31  global $DIC;
32 
33  if (null === $filesystem) {
34  $filesystem = $DIC->filesystem()->storage();
35  }
36  $this->filesystem = $filesystem;
37 
38  if (null === $logger) {
39  $logger = $DIC->logger()->root();
40  }
41  $this->logger = $logger;
42  }
43 
51  public function createCertificateFile(int $userId, int $objectId)
52  {
53  $userCertificateRepository = new ilUserCertificateRepository();
54 
55  $userCertificate = $userCertificateRepository->fetchActiveCertificate($userId, $objectId);
56 
57  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
58  if (false === $this->filesystem->hasDir($dirPath)) {
59  $this->filesystem->createDir($dirPath);
60  }
61 
62  $pdfGenerator = new ilPdfGenerator($userCertificateRepository, $this->logger);
63 
64  $pdfScalar = $pdfGenerator->generate($userCertificate->getId());
65 
66  $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
67  if ($this->filesystem->has($completePath)) {
68  $this->filesystem->delete($completePath);
69  }
70 
71  $this->filesystem->write($completePath, $pdfScalar);
72  }
73 
80  public function deliverCertificate(int $userId, int $objectId)
81  {
82  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
83  $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
84 
85  $completePath = $dirPath . '/' . $fileName;
86  if ($this->filesystem->has($completePath)) {
87  $userCertificateRepository = new ilUserCertificateRepository();
88 
89  $userCertificate = $userCertificateRepository->fetchActiveCertificateForPresentation($userId, $objectId);
90 
91  $downloadFilePath = CLIENT_DATA_DIR . '/' . $completePath;
92  $delivery = new \ilFileDelivery($downloadFilePath);
93  $delivery->setMimeType(\ilMimeTypeUtil::APPLICATION__PDF);
94  $delivery->setConvertFileNameToAsci(true);
95  $delivery->setDownloadFileName(\ilFileUtils::getValidFilename($userCertificate->getObjectTitle() . '.pdf'));
96 
97  $delivery->deliver();
98  }
99  }
100 
105  public function deleteUserDirectory(int $userId)
106  {
107  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
108 
109  if (true === $this->filesystem->hasDir($dirPath)) {
110  $this->filesystem->deleteDir($dirPath);
111  }
112  }
113 
120  public function deleteCertificateFile(int $userId, int $objectId)
121  {
122  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
123 
124  $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
125 
126  if ($this->filesystem->has($completePath)) {
127  $this->filesystem->delete($completePath);
128  }
129  }
130 
131 
138  public function createCertificateFilePath(int $userId, int $objectId)
139  {
140  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId . '/';
141  $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
142  $completePath = $dirPath . $fileName;
143  if ($this->filesystem->has($completePath)) {
144  return CLIENT_DATA_DIR . '/' . $completePath;
145  }
146 
147  throw new ilException(sprintf('Certificate File does not exist in "%"', $completePath));
148  }
149 }
Class ilPdfGeneratorConstantsTest.
global $DIC
Definition: saml.php:7
__construct(Filesystem $filesystem=null, Logger $logger=null)
Class FlySystemFileAccessTest.
static getValidFilename($a_filename)
Get valid filename.