diff --git a/lib/models/Courseware/BlockTypes/BlockType.php b/lib/models/Courseware/BlockTypes/BlockType.php index 203f6450be78329603cdbef8a3dfad9bfc62739d..bee1df7d450bc8a81f6c8a4f5ccbd2df9d1b6c3d 100755 --- a/lib/models/Courseware/BlockTypes/BlockType.php +++ b/lib/models/Courseware/BlockTypes/BlockType.php @@ -398,16 +398,32 @@ abstract class BlockType return $destinationFolder; } - public function pdfExport() + /** + * Gets the related block's html template if exists otherwise a default one, to be exported as pdf if exists. + * + * It turns the classname into snakecase in order to find the + * template file in templates/courseware/block_types. + * + * @return mixed the \Flexi_Template instance if exists, otherwise null. + */ + public function getPdfHtmlTemplate(): ?\Flexi_Template { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<h6>' . _('Block-Daten') . ': ' . '</h6>'; - foreach($this->getPayload() as $key => $value) { - if ($value !== '') { - $html .= '<h6>' . $key . ' => ' . $value . '</h6>'; + $template = null; + try { + $template_name = strtosnakecase((new \ReflectionClass($this))->getShortName()); + $template_path = $GLOBALS['template_factory']->get_path() . "courseware/block_types/{$template_name}.php"; + if (file_exists($template_path)) { + $template = $GLOBALS['template_factory']->open("courseware/block_types/{$template_name}"); + } else { + $template = $GLOBALS['template_factory']->open("courseware/block_types/default"); } + $template->set_attributes([ + 'title' => $this->getTitle(), + 'payload' => $this->getPayload() + ]); + } catch (\Exception $e) { + // it catches the exception mostly because the template file could not be found. } - - return $html; + return $template; } } diff --git a/lib/models/Courseware/BlockTypes/Code.php b/lib/models/Courseware/BlockTypes/Code.php index 99888d4f380c9c4d65312ae8257c1f512f313e09..2ee634255a3b536560f9fe209fc9f704d7df2197 100755 --- a/lib/models/Courseware/BlockTypes/Code.php +++ b/lib/models/Courseware/BlockTypes/Code.php @@ -58,12 +58,4 @@ class Code extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<pre>' . htmlspecialchars($this->getPayload()['content']) . '</pre>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Confirm.php b/lib/models/Courseware/BlockTypes/Confirm.php index 92c61d4d467c4f2e8a645ff7a503ded94ab06a7d..7204e6bf8c816d211bc8a9227560a8bcdee38572 100755 --- a/lib/models/Courseware/BlockTypes/Confirm.php +++ b/lib/models/Courseware/BlockTypes/Confirm.php @@ -57,12 +57,4 @@ class Confirm extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<p>' . htmlspecialchars($this->getPayload()['text']) . '</p>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Date.php b/lib/models/Courseware/BlockTypes/Date.php index 66075f32fcdbf7b00eaa445cf95d9e23c4e74089..df37590b3154433e74f809f25637748ab5da1e2a 100755 --- a/lib/models/Courseware/BlockTypes/Date.php +++ b/lib/models/Courseware/BlockTypes/Date.php @@ -58,12 +58,4 @@ class Date extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<p>' . date('d.m.Y h:i', (int) $this->getPayload()['timestamp'] / 1000) . '</p>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Embed.php b/lib/models/Courseware/BlockTypes/Embed.php index 5d067ec8732f5d2619903722e291d01a77088102..a6496c665439fe57cf33323170c5b4143787aaaa 100755 --- a/lib/models/Courseware/BlockTypes/Embed.php +++ b/lib/models/Courseware/BlockTypes/Embed.php @@ -121,16 +121,4 @@ class Embed extends BlockType { return []; } - - public function pdfExport() - { - $payload = $this->getPayload(); - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<h6>' . _('Block-Daten') . ': ' . '</h6>'; - $html .= '<h6>' . _('Titel') . ' => ' . $payload['title'] . '</h6>'; - $html .= '<h6>' . _('Quelle') . ' => ' . $payload['source'] . '</h6>'; - $html .= '<h6>' . _('URL') . ' => ' . $payload['url'] . '</h6>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Headline.php b/lib/models/Courseware/BlockTypes/Headline.php index a3add744f578ec05d47a9672a21b053be996bac5..855e2a94a54fe6ddb84e1cdb522bdcee822b4a96 100755 --- a/lib/models/Courseware/BlockTypes/Headline.php +++ b/lib/models/Courseware/BlockTypes/Headline.php @@ -106,13 +106,4 @@ class Headline extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<h5>' . htmlspecialchars($this->getPayload()['title']) . '</h5>'; - $html .= '<h6>' . htmlspecialchars($this->getPayload()['subtitle']) . '</h6>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/KeyPoint.php b/lib/models/Courseware/BlockTypes/KeyPoint.php index 90f4852770a6cbba15cf43339a41621ca0b77e7e..fae16d3b9a6565de6c5df6b8efa88b18ac47cd5f 100755 --- a/lib/models/Courseware/BlockTypes/KeyPoint.php +++ b/lib/models/Courseware/BlockTypes/KeyPoint.php @@ -59,12 +59,4 @@ class KeyPoint extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<p>' . htmlspecialchars($this->getPayload()['text']) . '</p>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Link.php b/lib/models/Courseware/BlockTypes/Link.php index 1e804b745800caf146a25dc9303d15baef8e489b..7b93aeb7166cd9139873408c896f92e89630aeba 100755 --- a/lib/models/Courseware/BlockTypes/Link.php +++ b/lib/models/Courseware/BlockTypes/Link.php @@ -60,13 +60,4 @@ class Link extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<p>' . htmlspecialchars($this->getPayload()['title']) . '</p>'; - $html .= '<p>' . htmlspecialchars($this->getPayload()['url']) . '</p>'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Text.php b/lib/models/Courseware/BlockTypes/Text.php index 3857c02911e0268128f6500b407368e651cb4090..c4fe67c68db239f9a3c8ce1d82265201686aaf53 100755 --- a/lib/models/Courseware/BlockTypes/Text.php +++ b/lib/models/Courseware/BlockTypes/Text.php @@ -165,12 +165,4 @@ class Text extends BlockType return array(); }); } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= $this->getPayload()['text']; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Typewriter.php b/lib/models/Courseware/BlockTypes/Typewriter.php index 064065bd6139426a897b4ce37881ce0939ec69ea..583e64e7a1e6a06be57b1b27fbbed5a47e8322b4 100755 --- a/lib/models/Courseware/BlockTypes/Typewriter.php +++ b/lib/models/Courseware/BlockTypes/Typewriter.php @@ -60,12 +60,4 @@ class Typewriter extends BlockType { return []; } - - public function pdfExport() - { - $html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>'; - $html .= '<p>' . htmlspecialchars($this->getPayload()['text']) . '</p>'; - - return $html; - } } diff --git a/lib/models/Courseware/ContainerTypes/AccordionContainer.php b/lib/models/Courseware/ContainerTypes/AccordionContainer.php index 510a388cdbbc9e6ce48a16a68e2ae591dcffb9e5..832520401aa807626f1db1e5df5ed5b09cf90f31 100755 --- a/lib/models/Courseware/ContainerTypes/AccordionContainer.php +++ b/lib/models/Courseware/ContainerTypes/AccordionContainer.php @@ -56,28 +56,4 @@ class AccordionContainer extends ContainerType return Schema::fromJsonString(file_get_contents($schemaFile)); } - - public function pdfExport() - { - $html = '<h3>' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '</h3>'; - - $payload = $this->getPayload(); - - $sections = $payload['sections']; - foreach ($sections as $section) { - $block_ids = $section['blocks']; - $html .= '<h4>' . $section['name'] . '</h4>'; - foreach ($block_ids as $block_id) { - $block = $this->container->blocks->find($block_id); - if ($block) { - $html .= $block->type->PdfExport(); - } - else { - $html .= '<p>' . _('Block konnte nicht gefunden werden') . '</p>'; - } - } - } - - return $html; - } } diff --git a/lib/models/Courseware/ContainerTypes/ContainerType.php b/lib/models/Courseware/ContainerTypes/ContainerType.php index a2552d415af96ac27268e07a24aea33397d150ed..4b28b345875bacfaa8c6f1e2a45f98a4fdc470e3 100755 --- a/lib/models/Courseware/ContainerTypes/ContainerType.php +++ b/lib/models/Courseware/ContainerTypes/ContainerType.php @@ -245,14 +245,33 @@ abstract class ContainerType } } - public function pdfExport() + /** + * Gets the related container's html template if exists otherwise a default one, to be exported as pdf if exists. + * + * It turns the classname into snakecase in order to find the + * template file in templates/courseware/container_types. + * + * @return mixed the \Flexi_Template instance if exists, otherwise null. + */ + public function getPdfHtmlTemplate(): ?\Flexi_Template { - $html = '<h3>' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '</h3>'; - - foreach ($this->container->blocks as $block) { - $html .= $block->type->PdfExport(); + $template = null; + try { + $template_name = strtosnakecase((new \ReflectionClass($this))->getShortName()); + $template_path = $GLOBALS['template_factory']->get_path() . "courseware/container_types/{$template_name}.php"; + if (file_exists($template_path)) { + $template = $GLOBALS['template_factory']->open("courseware/container_types/{$template_name}"); + } else { + $template = $GLOBALS['template_factory']->open("courseware/container_types/default"); + } + $template->set_attributes([ + 'title' => $this->getTitle(), + 'payload' => $this->getPayload(), + 'container' => $this->container + ]); + } catch (\Exception $e) { + // it catches the exception mostly because the template file could not be found. } - - return $html; + return $template; } } diff --git a/lib/models/Courseware/ContainerTypes/ListContainer.php b/lib/models/Courseware/ContainerTypes/ListContainer.php index d8e283c24f59bda4ac142c3f7af2db5cb1e10f8c..4918271a40e322e5aafb03748adc276159d49570 100755 --- a/lib/models/Courseware/ContainerTypes/ListContainer.php +++ b/lib/models/Courseware/ContainerTypes/ListContainer.php @@ -56,24 +56,4 @@ class ListContainer extends ContainerType return Schema::fromJsonString(file_get_contents($schemaFile)); } - - public function pdfExport() - { - $html = '<h3>' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '</h3>'; - - $payload = $this->getPayload(); - $block_ids = $payload['sections'][0]['blocks']; - - foreach ($block_ids as $block_id) { - $block = $this->container->blocks->find($block_id); - if ($block) { - $html .= $block->type->PdfExport(); - } - else { - $html .= '<p>' . _('Block konnte nicht gefunden werden') . '</p>'; - } - } - - return $html; - } } diff --git a/lib/models/Courseware/ContainerTypes/TabsContainer.php b/lib/models/Courseware/ContainerTypes/TabsContainer.php index 72a15f464a8c928cecb2b774775353b1e0653947..b884bbb67ee0a745137d01d80cbf3495b641721c 100755 --- a/lib/models/Courseware/ContainerTypes/TabsContainer.php +++ b/lib/models/Courseware/ContainerTypes/TabsContainer.php @@ -57,28 +57,4 @@ class TabsContainer extends ContainerType return Schema::fromJsonString(file_get_contents($schemaFile)); } - - public function pdfExport() - { - $html = '<h3>' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '</h3>'; - - $payload = $this->getPayload(); - - $sections = $payload['sections']; - foreach ($sections as $section) { - $block_ids = $section['blocks']; - $html .= '<h4>' . $section['name'] . '</h4>'; - foreach ($block_ids as $block_id) { - $block = $this->container->blocks->find($block_id); - if ($block) { - $html .= $block->type->PdfExport(); - } - else { - $html .= '<p>' . _('Block konnte nicht gefunden werden') . '</p>'; - } - } - } - - return $html; - } } diff --git a/lib/models/Courseware/StructuralElement.php b/lib/models/Courseware/StructuralElement.php index 54e2880541447d1654314d468716a857674ff288..ba1563e143424ab075847d3793c8821ca0c959db 100755 --- a/lib/models/Courseware/StructuralElement.php +++ b/lib/models/Courseware/StructuralElement.php @@ -768,8 +768,10 @@ SQL; { $containers = \Courseware\Container::findBySQL('structural_element_id = ?', [$this->id]); + $html = ''; foreach ($containers as $container) { - $html .= $container->type->pdfExport(); + $container_html_template = $container->type->getPdfHtmlTemplate(); + $html .= $container_html_template ? $container_html_template->render() : ''; } return $html; diff --git a/templates/courseware/block_types/code.php b/templates/courseware/block_types/code.php new file mode 100755 index 0000000000000000000000000000000000000000..0c369e2f31878a137d5a06c9f3e0a9e1dbefd989 --- /dev/null +++ b/templates/courseware/block_types/code.php @@ -0,0 +1,2 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<pre><?= htmlReady($payload['content']) ?></pre> diff --git a/templates/courseware/block_types/confirm.php b/templates/courseware/block_types/confirm.php new file mode 100755 index 0000000000000000000000000000000000000000..714e8e8e6f2c072075626efad1ffa99817635436 --- /dev/null +++ b/templates/courseware/block_types/confirm.php @@ -0,0 +1,2 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<p><?= htmlReady($payload['text']) ?></p> diff --git a/templates/courseware/block_types/date.php b/templates/courseware/block_types/date.php new file mode 100755 index 0000000000000000000000000000000000000000..2f748049af5cdb12b82ef3a74680b19a008a0b74 --- /dev/null +++ b/templates/courseware/block_types/date.php @@ -0,0 +1,2 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<p><?= date('d.m.Y h:i', (int) $payload['timestamp'] / 1000) ?></p> diff --git a/templates/courseware/block_types/default.php b/templates/courseware/block_types/default.php new file mode 100644 index 0000000000000000000000000000000000000000..6e09444e9984cb1bf877d0f559eb652c310a314f --- /dev/null +++ b/templates/courseware/block_types/default.php @@ -0,0 +1,8 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<h6><?= _('Block-Daten') ?>:</h6> +<? foreach ($payload as $key => $value): ?> + <? $value = is_bool($value) ? ($value ? 'true' : 'false') : $value; ?> + <? if (!empty($value)): ?> + <h6><?= htmlReady(str_replace('_', ' ', strtocamelcase($key, true)) . ' => ' . $value) ?></h6> + <? endif; ?> +<? endforeach; ?> diff --git a/templates/courseware/block_types/embed.php b/templates/courseware/block_types/embed.php new file mode 100755 index 0000000000000000000000000000000000000000..f22f133614ef061a1e8aa64dc8d00b2c515d6968 --- /dev/null +++ b/templates/courseware/block_types/embed.php @@ -0,0 +1,6 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<h6><?= _('Block-Daten') . ': ' ?></h6> +<h6><?= htmlReady(_('Titel') . ' => ' . $payload['title']) ?></h6> +<h6><?= htmlReady(_('Quelle') . ' => ' . $payload['source']) ?></h6> +<h6><?= htmlReady(_('URL') . ' => ' . $payload['url']) ?></h6> + diff --git a/templates/courseware/block_types/headline.php b/templates/courseware/block_types/headline.php new file mode 100755 index 0000000000000000000000000000000000000000..fc573268d41a5de31a796cdfe93428aab3b44bfc --- /dev/null +++ b/templates/courseware/block_types/headline.php @@ -0,0 +1,4 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<h5><?= htmlReady($payload['title']) ?></h5> +<h6><?= htmlReady($payload['subtitle']) ?></h6> + diff --git a/templates/courseware/block_types/key_point.php b/templates/courseware/block_types/key_point.php new file mode 100755 index 0000000000000000000000000000000000000000..714e8e8e6f2c072075626efad1ffa99817635436 --- /dev/null +++ b/templates/courseware/block_types/key_point.php @@ -0,0 +1,2 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<p><?= htmlReady($payload['text']) ?></p> diff --git a/templates/courseware/block_types/link.php b/templates/courseware/block_types/link.php new file mode 100755 index 0000000000000000000000000000000000000000..c1da7643d143a6e2a7634521a2a2270fb108fbc4 --- /dev/null +++ b/templates/courseware/block_types/link.php @@ -0,0 +1,3 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<p><?= htmlReady($payload['title']) ?></p> +<p><?= htmlReady($payload['url']) ?></p> diff --git a/templates/courseware/block_types/text.php b/templates/courseware/block_types/text.php new file mode 100755 index 0000000000000000000000000000000000000000..f92fe0748f8f724badda280c9e8bfb0632bee657 --- /dev/null +++ b/templates/courseware/block_types/text.php @@ -0,0 +1,2 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<?= formatReady($payload['text']) ?> diff --git a/templates/courseware/block_types/typewriter.php b/templates/courseware/block_types/typewriter.php new file mode 100755 index 0000000000000000000000000000000000000000..714e8e8e6f2c072075626efad1ffa99817635436 --- /dev/null +++ b/templates/courseware/block_types/typewriter.php @@ -0,0 +1,2 @@ +<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5> +<p><?= htmlReady($payload['text']) ?></p> diff --git a/templates/courseware/container_types/accordion_container.php b/templates/courseware/container_types/accordion_container.php new file mode 100644 index 0000000000000000000000000000000000000000..afc25e04bd7ec04587173d6031cbabd6cc0b67ad --- /dev/null +++ b/templates/courseware/container_types/accordion_container.php @@ -0,0 +1,15 @@ +<h3><?= sprintf(_('Container-Typ: %s'), htmlReady($title)) ?></h3> +<? foreach ($payload['sections'] as $section): ?> + <h4><?= htmlReady($section['name']) ?></h4> + <? foreach ($section['blocks'] as $block_id): ?> + <? $block = $container->blocks->find($block_id); ?> + <? if ($block): ?> + <? $block_html_template = $block->type->getPdfHtmlTemplate(); ?> + <? if ($block_html_template): ?> + <?= $block_html_template->render(); ?> + <? endif; ?> + <? else: ?> + <p><?= _('Block konnte nicht gefunden werden') ?></p> + <? endif; ?> + <? endforeach ?> +<? endforeach ?> diff --git a/templates/courseware/container_types/default.php b/templates/courseware/container_types/default.php new file mode 100644 index 0000000000000000000000000000000000000000..fc4fb0a693bfc7c7c5b3a8c83608fe0990cc2bb9 --- /dev/null +++ b/templates/courseware/container_types/default.php @@ -0,0 +1 @@ +<h3><?= sprintf(_('Container-Typ: %s'), htmlReady($title)) ?></h3> diff --git a/templates/courseware/container_types/list_container.php b/templates/courseware/container_types/list_container.php new file mode 100644 index 0000000000000000000000000000000000000000..1dc4376864de1acef82b69ba0c7e6a6f4b6500d9 --- /dev/null +++ b/templates/courseware/container_types/list_container.php @@ -0,0 +1,12 @@ +<h3><?= sprintf(_('Container-Typ: %s'), htmlReady($title)) ?></h3> +<? foreach ($payload['sections'][0]['blocks'] as $block_id): ?> + <? $block = $container->blocks->find($block_id); ?> + <? if ($block): ?> + <? $block_html_template = $block->type->getPdfHtmlTemplate(); ?> + <? if ($block_html_template): ?> + <?= $block_html_template->render(); ?> + <? endif; ?> + <? else: ?> + <p><?= _('Block konnte nicht gefunden werden') ?></p> + <? endif; ?> +<? endforeach ?> diff --git a/templates/courseware/container_types/tabs_container.php b/templates/courseware/container_types/tabs_container.php new file mode 100644 index 0000000000000000000000000000000000000000..afc25e04bd7ec04587173d6031cbabd6cc0b67ad --- /dev/null +++ b/templates/courseware/container_types/tabs_container.php @@ -0,0 +1,15 @@ +<h3><?= sprintf(_('Container-Typ: %s'), htmlReady($title)) ?></h3> +<? foreach ($payload['sections'] as $section): ?> + <h4><?= htmlReady($section['name']) ?></h4> + <? foreach ($section['blocks'] as $block_id): ?> + <? $block = $container->blocks->find($block_id); ?> + <? if ($block): ?> + <? $block_html_template = $block->type->getPdfHtmlTemplate(); ?> + <? if ($block_html_template): ?> + <?= $block_html_template->render(); ?> + <? endif; ?> + <? else: ?> + <p><?= _('Block konnte nicht gefunden werden') ?></p> + <? endif; ?> + <? endforeach ?> +<? endforeach ?>