ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUserCertificateZip.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  private readonly string $typeInFileName;
27  private bool $files_added_to_archive = false;
28 
29  public function __construct(
30  private readonly int $objectId,
31  private readonly string $certificatePath,
32  private readonly string $webDirectory = CLIENT_WEB_DIR,
33  private readonly string $installionId = IL_INST_ID
34  ) {
35  // The mapping to types is made to reflect the old behaviour of
36  // the adapters
37  $iliasType = ilObject::_lookupType($this->objectId);
38 
39  $typeInFileName = 'not_defined';
40  if ('crs' === $iliasType) {
41  $typeInFileName = 'course';
42  } elseif ('tst' === $iliasType) {
43  $typeInFileName = 'test';
44  } elseif ('exc' === $iliasType) {
45  $typeInFileName = 'exc';
46  } elseif ('sahs' === $iliasType) {
47  $typeInFileName = 'scorm';
48  }
49 
50  $this->typeInFileName = $typeInFileName;
51  }
52 
53  public function createArchiveDirectory(): string
54  {
55  $type = ilObject::_lookupType($this->objectId);
56  $certificateId = $this->objectId;
57 
58  $directory = $this->webDirectory . $this->certificatePath . time() . '__' . $this->installionId . '__' . $type . '__' . $certificateId . '__certificate/';
59  ilFileUtils::makeDirParents($directory);
60 
61  return $directory;
62  }
63 
70  public function addPDFtoArchiveDirectory(string $pdfdata, string $dir, string $filename): void
71  {
72  $fh = fopen($dir . $filename, 'wb');
73  fwrite($fh, $pdfdata);
74  fclose($fh);
75  $this->files_added_to_archive = true;
76  }
77 
81  public function zipCertificatesInArchiveDirectory(string $dir, bool $deliver = true): string
82  {
83  if (!$this->files_added_to_archive) {
84  throw new \ILIAS\Filesystem\Exception\IOException('No files added to archive directory');
85  }
86 
87  $zipFile = time() . '__' . $this->installionId . '__' . $this->typeInFileName . '__' . $this->objectId . '__certificates.zip';
88  $zipFilePath = $this->webDirectory . $this->certificatePath . $zipFile;
89 
90  ilFileUtils::zip($dir, $zipFilePath);
91  ilFileUtils::delDir($dir);
92 
93  if ($deliver) {
94  ilFileDelivery::deliverFileLegacy($zipFilePath, $zipFile, 'application/zip', false, true);
95  }
96 
97  return $zipFilePath;
98  }
99 }
const IL_INST_ID
Definition: constants.php:40
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
static zip(string $a_dir, string $a_file, bool $compress_content=false)
__construct(private readonly int $objectId, private readonly string $certificatePath, private readonly string $webDirectory=CLIENT_WEB_DIR, private readonly string $installionId=IL_INST_ID)
static _lookupType(int $id, bool $reference=false)