$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): string { $this->pdf = new PdfDocument; $this->page = 0; $this->footer = self::printable('Lizenziert für '.$licensee.' · Lizenzkopie '.$copyId); $this->newPage(); $this->write(self::printable((string) $guide['title']), 18, true, 8); if ($variantName !== '') { $this->write(self::printable('Variante: '.$variantName), 11, true, 6); } $facts = array_filter([ filled($guide['learning_goal']) ? 'Lernziel: '.$guide['learning_goal'] : null, $guide['duration_minutes'] !== null ? 'Dauer: '.$guide['duration_minutes'].($guide['duration_max'] !== null ? ' bis '.$guide['duration_max'] : '').' Minuten' : null, filled($guide['prerequisites']) ? 'Voraussetzungen: '.$guide['prerequisites'] : null, filled($guide['material_note']) ? 'Ausstattung: '.$guide['material_note'] : null, ]); foreach ($facts as $fact) { $this->write(self::printable($fact), 10.5, false, 4); } $this->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 { $text = strtr($text, ['→' => '->', '←' => '<-', '≥' => '>=', '≤' => '<=', '✓' => '+', '✗' => 'x', '≠' => '!=', '≈' => '~']); $latin = Transliterator::create('Any-Latin; Latin-ASCII'); $out = ''; foreach (mb_str_split($text) as $char) { $encoded = mb_convert_encoding($char, 'Windows-1252', 'UTF-8'); $out .= $encoded === '?' && $char !== '?' ? ($latin?->transliterate($char) ?: '?') : $char; } return $out; } /** * @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->write(self::printable($label), 10, false, 6); return; } $this->write($title, 12.5, true, 5); foreach ($section['blocks'] ?? [] as $block) { match ($block['type']) { 'heading' => $this->write(self::printable($block['text']), 11, true, 4), 'paragraph' => $this->write(self::printable($block['text']), 10.5, false, 5), 'bullets' => $this->items($block['items'], fn (int $n): string => '•'), 'numbered' => $this->items($block['items'], fn (int $n): string => $n.'.'), }; } $this->y -= 4; } /** * @param list $items */ private function items(array $items, callable $marker): void { foreach ($items as $index => $item) { $lines = PdfDocument::wrap(self::printable($item), self::RIGHT - self::LEFT - 18, 10.5); foreach ($lines as $line => $text) { $this->room(14); if ($line === 0) { $this->pdf->text(self::LEFT + 4, $this->y, $marker($index + 1), 10.5); } $this->pdf->text(self::LEFT + 18, $this->y, $text, 10.5); $this->y -= 14; } } $this->y -= 3; } private function write(string $text, float $size, bool $bold, float $after): void { foreach (PdfDocument::wrap($text, self::RIGHT - self::LEFT, $size, $bold) as $line) { $this->room($size + 4); $this->pdf->text(self::LEFT, $this->y, $line, $size, $bold); $this->y -= $size + 4; } $this->y -= $after; } private function room(float $needed): void { if ($this->y - $needed < self::BOTTOM) { $this->closePage(); $this->newPage(); } } private function newPage(): void { $this->pdf->addPage(); $this->page++; $this->y = self::TOP; } 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); } }