ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
21 
22  const PERSISTENT_CERTIFICATES_DIRECTORY = 'PersistentCertificates/';
23 
24  const CERTIFICATE_FILENAME = 'certificate.pdf';
25 
30  public function __construct(Filesystem $filesystem = null, ilLogger $logger = null)
31  {
32  global $DIC;
33 
34  if (null === $filesystem) {
35  $filesystem = $DIC->filesystem()->storage();
36  }
37  $this->filesystem = $filesystem;
38 
39  if (null === $logger) {
40  $logger = $DIC->logger()->root();
41  }
42  $this->logger = $logger;
43  }
44 
52  public function createCertificateFile(int $userId, int $objectId)
53  {
54  $userCertificateRepository = new ilUserCertificateRepository();
55 
56  $userCertificate = $userCertificateRepository->fetchActiveCertificate($userId, $objectId);
57 
58  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
59  if (false === $this->filesystem->hasDir($dirPath)) {
60  $this->filesystem->createDir($dirPath);
61  }
62 
63  $pdfGenerator = new ilPdfGenerator($userCertificateRepository, $this->logger);
64 
65  $pdfScalar = $pdfGenerator->generate($userCertificate->getId());
66 
67  $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
68  if ($this->filesystem->has($completePath)) {
69  $this->filesystem->delete($completePath);
70  }
71 
72  $this->filesystem->write($completePath, $pdfScalar);
73  }
74 
81  public function deliverCertificate(int $userId, int $objectId)
82  {
83  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId;
84  $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
85 
86  $completePath = $dirPath . '/' . $fileName;
87  if ($this->filesystem->has($completePath)) {
88  $userCertificateRepository = new ilUserCertificateRepository();
89 
90  $userCertificate = $userCertificateRepository->fetchActiveCertificateForPresentation($userId, $objectId);
91 
92  $downloadFilePath = CLIENT_DATA_DIR . '/' . $completePath;
93  $delivery = new \ilFileDelivery($downloadFilePath);
94  $delivery->setMimeType(\ilMimeTypeUtil::APPLICATION__PDF);
95  $delivery->setConvertFileNameToAsci(true);
96  $delivery->setDownloadFileName(\ilFileUtils::getValidFilename($userCertificate->getObjectTitle() . '.pdf'));
97 
98  $delivery->deliver();
99  }
100  }
101 
106  public function deleteUserDirectory(int $userId)
107  {
108  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
109 
110  if (true === $this->filesystem->hasDir($dirPath)) {
111  $this->filesystem->deleteDir($dirPath);
112  }
113  }
114 
121  public function deleteCertificateFile(int $userId, int $objectId)
122  {
123  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId;
124 
125  $completePath = $dirPath . '/' . $objectId . '_' . self::CERTIFICATE_FILENAME;
126 
127  if ($this->filesystem->has($completePath)) {
128  $this->filesystem->delete($completePath);
129  }
130  }
131 
132 
139  public function createCertificateFilePath(int $userId, int $objectId)
140  {
141  $dirPath = self::PERSISTENT_CERTIFICATES_DIRECTORY . $userId . '/' . $objectId . '/';
142  $fileName = $objectId . '_' . self::CERTIFICATE_FILENAME;
143  $completePath = $dirPath . $fileName;
144  if ($this->filesystem->has($completePath)) {
145  return CLIENT_DATA_DIR . '/' . $completePath;
146  }
147 
148  throw new ilException(sprintf('Certificate File does not exist in "%s"', $completePath));
149  }
150 }
Class ilPdfGeneratorConstantsTest.
const CLIENT_DATA_DIR
Definition: constants.php:44
global $DIC
Definition: goto.php:24
__construct(Filesystem $filesystem=null, ilLogger $logger=null)
Component logger with individual log levels by component id.
Class FlySystemFileAccessTest disabled disabled disabled.
static getValidFilename($a_filename)
Get valid filename.