CI Feature / test (push) Successful in 3m32s
Neuer kind 'exercise', Varianten je Fassung, Verwendung (erforderlich/ergänzend) und Variantenauswahl am einbindenden Mitglied; der Stand schreibt nur gewählte Varianten fest. Änderungsnachweis über structure_hash. Formatierte Anleitung im Mitgliederbereich und PDF (Fußzeile, Metadaten, Ausgabenachweis, keine gespeicherten Kopien); Sperre trifft nur das Medium. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
208 lines
6.9 KiB
PHP
208 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Domain\Commerce\Receipt\PdfDocument;
|
|
use Transliterator;
|
|
|
|
/**
|
|
* Druckbare Ausgabe einer Übung (DIN A4, mehrere Seiten) aus demselben Ergebnis wie die Ansicht ({@see ExerciseGuide}).
|
|
* Kennzeichnung: Fußzeile mit Lizenznehmer und Lizenzkopie auf jeder Seite (K2) und die Kennung in den Metadaten (K1).
|
|
* Das Dokument wird bei jedem Abruf neu erzeugt und nie abgelegt, damit eine spätere Sperre keine ältere Kopie
|
|
* ausliefern kann.
|
|
*/
|
|
class ExercisePdf
|
|
{
|
|
private const LEFT = 56.0;
|
|
|
|
private const RIGHT = 539.0;
|
|
|
|
private const TOP = 790.0;
|
|
|
|
private const BOTTOM = 72.0;
|
|
|
|
private PdfDocument $pdf;
|
|
|
|
private float $y = self::TOP;
|
|
|
|
private int $page = 0;
|
|
|
|
private string $footer = '';
|
|
|
|
/**
|
|
* Welche Abschnitte gehören ins PDF? Erforderliche Textabschnitte der Auswahl müssen für `laden` zulässig sein, sonst
|
|
* ist das PDF nicht erlaubt (null). Ergänzende ohne Berechtigung entfallen. Videos und Dokumente erscheinen nur als
|
|
* Hinweis.
|
|
*
|
|
* @param array<string, mixed> $guide Ergebnis von {@see ExerciseGuide::build}
|
|
* @return list<array<string, mixed>>|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<string, mixed> $guide
|
|
* @param list<array<string, mixed>> $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<string, mixed> $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<string> $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);
|
|
}
|
|
}
|