ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilUserCertificateZip.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 {
28  private readonly string $typeInFileName;
29  private bool $files_added_to_archive = false;
30 
31  public function __construct(
32  private readonly int $objectId,
33  private readonly string $certificatePath,
34  private readonly string $webDirectory = CLIENT_WEB_DIR,
35  private readonly string $installationId = IL_INST_ID
36  ) {
37  // The mapping to types is made to reflect the old behaviour of
38  // the adapters
39  $iliasType = ilObject::_lookupType($this->objectId);
40 
41  $typeInFileName = 'not_defined';
42  if ('crs' === $iliasType) {
43  $typeInFileName = 'course';
44  } elseif ('tst' === $iliasType) {
45  $typeInFileName = 'test';
46  } elseif ('exc' === $iliasType) {
47  $typeInFileName = 'exc';
48  } elseif ('sahs' === $iliasType) {
49  $typeInFileName = 'scorm';
50  }
51 
52  $this->typeInFileName = $typeInFileName;
53  }
54 
55  public function createArchiveDirectory(): string
56  {
57  $type = ilObject::_lookupType($this->objectId);
58  $certificateId = $this->objectId;
59 
60  $directory = $this->webDirectory . $this->certificatePath . time() .
61  '__' . $this->installationId . '__' . $type . '__' . $certificateId . '__certificate/';
62  ilFileUtils::makeDirParents($directory);
63 
64  return $directory;
65  }
66 
73  public function addPDFtoArchiveDirectory(string $pdfdata, string $dir, string $filename): void
74  {
75  $fh = fopen($dir . $filename, 'wb');
76  fwrite($fh, $pdfdata);
77  fclose($fh);
78  $this->files_added_to_archive = true;
79  }
80 
84  public function zipCertificatesInArchiveDirectory(string $dir, bool $deliver = true): string
85  {
86  if (!$this->files_added_to_archive) {
87  throw new IOException('No files added to archive directory');
88  }
89 
90  $zipFile = time(
91  ) . '__' . $this->installationId . '__' . $this->typeInFileName . '__' . $this->objectId . '__certificates.zip';
92  $zipFilePath = $this->webDirectory . $this->certificatePath . $zipFile;
93 
94  ilFileUtils::zip($dir, $zipFilePath);
95  ilFileUtils::delDir($dir);
96 
97  if ($deliver) {
98  ilFileDelivery::deliverFileLegacy($zipFilePath, $zipFile, 'application/zip', false, true);
99  }
100 
101  return $zipFilePath;
102  }
103 }
const IL_INST_ID
Definition: constants.php:40
Indicates general problems with the input or output operations.
Definition: IOException.php:27
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
addPDFtoArchiveDirectory(string $pdfdata, string $dir, string $filename)
Adds PDF data as a file to a given directory.
zipCertificatesInArchiveDirectory(string $dir, bool $deliver=true)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
const CLIENT_WEB_DIR
Definition: constants.php:47
$filename
Definition: buildRTE.php:78
__construct(private readonly int $objectId, private readonly string $certificatePath, private readonly string $webDirectory=CLIENT_WEB_DIR, private readonly string $installationId=IL_INST_ID)
static zip(string $a_dir, string $a_file, bool $compress_content=false)
static _lookupType(int $id, bool $reference=false)