$guide Ergebnis von {@see ExerciseGuide::build} * @return list>|null */ public function sections(array $guide): ?array { $included = []; foreach ($guide['entries'] as $entry) { if ($entry['class'] === 'text') { if ($entry['may_download'] && $entry['allowed']) { $included[] = $entry; } elseif ($entry['requirement'] === CompositionMember::REQUIRED) { return null; } continue; } if ($entry['allowed']) { $included[] = $entry; } } return array_filter($included, fn (array $entry): bool => $entry['class'] === 'text') === [] ? null : $included; } /** * @param array $guide * @param list> $sections */ public function render(array $guide, array $sections, string $licensee, string $copyId, string $variantName, string $guideUrl, ?PdfImages $images = null): string { $this->pdf = new PdfDocument; $this->page = 0; $this->footer = self::printable('Lizenziert für '.$licensee.' · Lizenzkopie '.$copyId); $this->flow = new PdfFlow($this->pdf, self::LEFT, self::RIGHT, self::TOP, self::BOTTOM, function (): void { $this->closePage(); $this->newPage(); }, $images === null ? null : Closure::fromCallable($images)); $this->newPage(); $this->flow->write(self::printable((string) $guide['title']), 18, true, 8); if ($variantName !== '') { $this->flow->write(self::printable('Variante: '.$variantName), 11, true, 6); } $this->fact('Lernziel', $guide['learning_goal']); if ($guide['duration_minutes'] !== null) { $this->flow->write(self::printable('Dauer: '.$guide['duration_minutes'].($guide['duration_max'] !== null ? ' bis '.$guide['duration_max'] : '').' Minuten'), 10.5, false, 4); } if (filled($guide['prerequisites'])) { $this->flow->write(self::printable('Voraussetzungen: '.$guide['prerequisites']), 10.5, false, 4); } $this->fact('Ausstattung', $guide['material_note']); $this->flow->y -= 6; foreach ($sections as $section) { $this->section($section, $guideUrl); } $this->closePage(); $ascii = Transliterator::create('Any-Latin; Latin-ASCII'); return $this->pdf->output($ascii?->transliterate((string) $guide['title']) ?: 'Uebung', 'Lizenzkopie '.$copyId); } /** * Zeichen außerhalb von Windows-1252 werden, wo möglich, lesbar ersetzt (Pfeil, Ungleichheitszeichen, andere * lateinische Buchstaben); sonst bleibt „?“. */ public static function printable(string $text): string { return PdfFlow::printable($text); } /** * @param array $section */ private function section(array $section, string $guideUrl): void { $title = self::printable((string) $section['title']); if ($section['class'] !== 'text') { $label = in_array($section['class'], ['film', 'audio'], true) ? 'Film: '.$title.' - online abrufbar im Mitgliederbereich (geschützte Wiedergabe): '.$guideUrl : 'Material: '.$title.' - im Mitgliederbereich abrufbar: '.$guideUrl; $this->flow->write(self::printable($label), 10, false, 6); return; } $this->flow->write($title, 12.5, true, 5, keep: 40); $this->flow->formatted($section['blocks'] ?? []); $this->flow->y -= 4; } /** Angabe mit Bezeichnung; formatierte Angaben stehen unter der Bezeichnung, einfache dahinter. */ private function fact(string $label, ?string $value): void { $blocks = FormattedText::blocks($value); if ($blocks === []) { return; } if (count($blocks) === 1 && $blocks[0]['type'] === 'paragraph') { $this->flow->runs([['text' => $label.': ', 'bold' => true, 'italic' => false, 'href' => null], ...$blocks[0]['runs']], self::LEFT, 10.5, after: 4); return; } $this->flow->write(self::printable($label.':'), 10.5, true, 2); $this->flow->formatted($blocks); } private function newPage(): void { $this->pdf->addPage(); $this->page++; $this->flow->restart(); } private function closePage(): void { $this->pdf->line(self::LEFT, 54, self::RIGHT, 54, 0.4); $this->pdf->text(self::LEFT, 40, $this->footer, 8); $this->pdf->textRight(self::RIGHT, 40, 'Seite '.$this->page, 8); } }