ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCertificateVerificationFileService.php
Go to the documentation of this file.
1<?php
2
23{
28
29 public function __construct(
34 ) {
35 $this->language = $language;
36 $this->database = $database;
37 $this->logger = $logger;
38 $this->classMap = $classMap;
39 }
40
44 public function createFile(ilUserCertificatePresentation $userCertificatePresentation): ?ilCertificateVerificationObject
45 {
46 $userCertificate = $userCertificatePresentation->getUserCertificate();
47 $objectType = $userCertificate->getObjType();
48
49 $this->language->loadLanguageModule('cert');
50
51 $verificationObjectType = $this->classMap->getVerificationTypeByType($objectType);
52
53 $verificationObject = new ilCertificateVerificationObject($verificationObjectType);
54 $verificationObject->setTitle($userCertificatePresentation->getObjectTitle());
55 $verificationObject->setDescription($userCertificatePresentation->getObjectDescription());
56
57 $objectId = $userCertificate->getObjId();
58 $userId = $userCertificate->getUserId();
59
60 $issueDate = new ilDate($userCertificate->getAcquiredTimestamp(), IL_CAL_UNIX);
61
62 $verificationObject->setProperty('issued_on', $issueDate);
63
64 $ilUserCertificateRepository = new ilUserCertificateRepository($this->database, $this->logger);
65 $pdfGenerator = new ilPdfGenerator($ilUserCertificateRepository);
66
67 $pdfAction = new ilCertificatePdfAction(
68 $pdfGenerator,
70 $this->language->txt('error_creating_certificate_pdf')
71 );
72
73 $certificateScalar = $pdfAction->createPDF($userId, $objectId);
74
75 if ($certificateScalar) {
76 // we need the object id for storing the certificate file
77 $verificationObject->create();
78
79 $path = $this->initStorage($verificationObject->getId(), 'certificate');
80
81 $fileName = $objectType . '_' . $objectId . '_' . $userId . '.pdf';
82
83 if (file_put_contents($path . $fileName, $certificateScalar)) {
84 $verificationObject->setProperty('file', $fileName);
85 $verificationObject->update();
86
87 return $verificationObject;
88 }
89
90 $this->logger->info('File could not be created');
91 $verificationObject->delete();
92 }
93 return null;
94 }
95
96 public function initStorage(int $objectId, string $subDirectory = ''): string
97 {
98 $storage = new ilVerificationStorageFile($objectId);
99 $storage->create();
100
101 $path = $storage->getAbsolutePath() . "/";
102
103 if ($subDirectory !== '') {
104 $path .= $subDirectory . "/";
105
106 if (!is_dir($path)) {
107 mkdir($path);
108 }
109 }
110
111 return $path;
112 }
113}
const IL_CAL_UNIX
Just a wrapper class to create Unit Test for other classes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createFile(ilUserCertificatePresentation $userCertificatePresentation)
__construct(ilLanguage $language, ilDBInterface $database, ilLogger $logger, ilCertificateVerificationClassMap $classMap)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class for single dates.
language handling
Component logger with individual log levels by component id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilDBInterface.
$path
Definition: ltiservices.php:30