From 439c37760f736b7806b801b1e609e83baff0ffa3 Mon Sep 17 00:00:00 2001 From: Farbod Zamani Date: Fri, 25 Feb 2022 11:21:08 +0000 Subject: Resolves "using templates for courseware pdfExport" --- lib/models/Courseware/BlockTypes/BlockType.php | 32 +++++++++++++++------ lib/models/Courseware/BlockTypes/Code.php | 8 ------ lib/models/Courseware/BlockTypes/Confirm.php | 8 ------ lib/models/Courseware/BlockTypes/Date.php | 8 ------ lib/models/Courseware/BlockTypes/Embed.php | 12 -------- lib/models/Courseware/BlockTypes/Headline.php | 9 ------ lib/models/Courseware/BlockTypes/KeyPoint.php | 8 ------ lib/models/Courseware/BlockTypes/Link.php | 9 ------ lib/models/Courseware/BlockTypes/Text.php | 8 ------ lib/models/Courseware/BlockTypes/Typewriter.php | 8 ------ .../ContainerTypes/AccordionContainer.php | 24 ---------------- .../Courseware/ContainerTypes/ContainerType.php | 33 +++++++++++++++++----- .../Courseware/ContainerTypes/ListContainer.php | 20 ------------- .../Courseware/ContainerTypes/TabsContainer.php | 24 ---------------- lib/models/Courseware/StructuralElement.php | 4 ++- templates/courseware/block_types/code.php | 2 ++ templates/courseware/block_types/confirm.php | 2 ++ templates/courseware/block_types/date.php | 2 ++ templates/courseware/block_types/default.php | 8 ++++++ templates/courseware/block_types/embed.php | 6 ++++ templates/courseware/block_types/headline.php | 4 +++ templates/courseware/block_types/key_point.php | 2 ++ templates/courseware/block_types/link.php | 3 ++ templates/courseware/block_types/text.php | 2 ++ templates/courseware/block_types/typewriter.php | 2 ++ .../container_types/accordion_container.php | 15 ++++++++++ templates/courseware/container_types/default.php | 1 + .../courseware/container_types/list_container.php | 12 ++++++++ .../courseware/container_types/tabs_container.php | 15 ++++++++++ 29 files changed, 129 insertions(+), 162 deletions(-) create mode 100755 templates/courseware/block_types/code.php create mode 100755 templates/courseware/block_types/confirm.php create mode 100755 templates/courseware/block_types/date.php create mode 100644 templates/courseware/block_types/default.php create mode 100755 templates/courseware/block_types/embed.php create mode 100755 templates/courseware/block_types/headline.php create mode 100755 templates/courseware/block_types/key_point.php create mode 100755 templates/courseware/block_types/link.php create mode 100755 templates/courseware/block_types/text.php create mode 100755 templates/courseware/block_types/typewriter.php create mode 100644 templates/courseware/container_types/accordion_container.php create mode 100644 templates/courseware/container_types/default.php create mode 100644 templates/courseware/container_types/list_container.php create mode 100644 templates/courseware/container_types/tabs_container.php diff --git a/lib/models/Courseware/BlockTypes/BlockType.php b/lib/models/Courseware/BlockTypes/BlockType.php index 203f645..bee1df7 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '
' . _('Block-Daten') . ': ' . '
'; - foreach($this->getPayload() as $key => $value) { - if ($value !== '') { - $html .= '
' . $key . ' => ' . $value . '
'; + $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 99888d4..2ee6342 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '
' . htmlspecialchars($this->getPayload()['content']) . '
'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Confirm.php b/lib/models/Courseware/BlockTypes/Confirm.php index 92c61d4..7204e6b 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '

' . htmlspecialchars($this->getPayload()['text']) . '

'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Date.php b/lib/models/Courseware/BlockTypes/Date.php index 66075f3..df37590 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '

' . date('d.m.Y h:i', (int) $this->getPayload()['timestamp'] / 1000) . '

'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Embed.php b/lib/models/Courseware/BlockTypes/Embed.php index 5d067ec..a6496c6 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '
' . _('Block-Daten') . ': ' . '
'; - $html .= '
' . _('Titel') . ' => ' . $payload['title'] . '
'; - $html .= '
' . _('Quelle') . ' => ' . $payload['source'] . '
'; - $html .= '
' . _('URL') . ' => ' . $payload['url'] . '
'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Headline.php b/lib/models/Courseware/BlockTypes/Headline.php index a3add74..855e2a9 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '
' . htmlspecialchars($this->getPayload()['title']) . '
'; - $html .= '
' . htmlspecialchars($this->getPayload()['subtitle']) . '
'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/KeyPoint.php b/lib/models/Courseware/BlockTypes/KeyPoint.php index 90f4852..fae16d3 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '

' . htmlspecialchars($this->getPayload()['text']) . '

'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Link.php b/lib/models/Courseware/BlockTypes/Link.php index 1e804b7..7b93aeb 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '

' . htmlspecialchars($this->getPayload()['title']) . '

'; - $html .= '

' . htmlspecialchars($this->getPayload()['url']) . '

'; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Text.php b/lib/models/Courseware/BlockTypes/Text.php index 3857c02..c4fe67c 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= $this->getPayload()['text']; - - return $html; - } } diff --git a/lib/models/Courseware/BlockTypes/Typewriter.php b/lib/models/Courseware/BlockTypes/Typewriter.php index 064065b..583e64e 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 = '
' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '
'; - $html .= '

' . htmlspecialchars($this->getPayload()['text']) . '

'; - - return $html; - } } diff --git a/lib/models/Courseware/ContainerTypes/AccordionContainer.php b/lib/models/Courseware/ContainerTypes/AccordionContainer.php index 510a388..8325204 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 = '

' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '

'; - - $payload = $this->getPayload(); - - $sections = $payload['sections']; - foreach ($sections as $section) { - $block_ids = $section['blocks']; - $html .= '

' . $section['name'] . '

'; - foreach ($block_ids as $block_id) { - $block = $this->container->blocks->find($block_id); - if ($block) { - $html .= $block->type->PdfExport(); - } - else { - $html .= '

' . _('Block konnte nicht gefunden werden') . '

'; - } - } - } - - return $html; - } } diff --git a/lib/models/Courseware/ContainerTypes/ContainerType.php b/lib/models/Courseware/ContainerTypes/ContainerType.php index a2552d4..4b28b34 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 = '

' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '

'; - - 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 d8e283c..4918271 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 = '

' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '

'; - - $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 .= '

' . _('Block konnte nicht gefunden werden') . '

'; - } - } - - return $html; - } } diff --git a/lib/models/Courseware/ContainerTypes/TabsContainer.php b/lib/models/Courseware/ContainerTypes/TabsContainer.php index 72a15f4..b884bbb 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 = '

' . sprintf(_('Container-Typ: %s'), $this->getTitle()) . '

'; - - $payload = $this->getPayload(); - - $sections = $payload['sections']; - foreach ($sections as $section) { - $block_ids = $section['blocks']; - $html .= '

' . $section['name'] . '

'; - foreach ($block_ids as $block_id) { - $block = $this->container->blocks->find($block_id); - if ($block) { - $html .= $block->type->PdfExport(); - } - else { - $html .= '

' . _('Block konnte nicht gefunden werden') . '

'; - } - } - } - - return $html; - } } diff --git a/lib/models/Courseware/StructuralElement.php b/lib/models/Courseware/StructuralElement.php index 54e2880..ba1563e 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 0000000..0c369e2 --- /dev/null +++ b/templates/courseware/block_types/code.php @@ -0,0 +1,2 @@ +
+
diff --git a/templates/courseware/block_types/confirm.php b/templates/courseware/block_types/confirm.php new file mode 100755 index 0000000..714e8e8 --- /dev/null +++ b/templates/courseware/block_types/confirm.php @@ -0,0 +1,2 @@ +
+

diff --git a/templates/courseware/block_types/date.php b/templates/courseware/block_types/date.php new file mode 100755 index 0000000..2f74804 --- /dev/null +++ b/templates/courseware/block_types/date.php @@ -0,0 +1,2 @@ +
+

diff --git a/templates/courseware/block_types/default.php b/templates/courseware/block_types/default.php new file mode 100644 index 0000000..6e09444 --- /dev/null +++ b/templates/courseware/block_types/default.php @@ -0,0 +1,8 @@ +
+
:
+ $value): ?> + + +
' . $value) ?>
+ + diff --git a/templates/courseware/block_types/embed.php b/templates/courseware/block_types/embed.php new file mode 100755 index 0000000..f22f133 --- /dev/null +++ b/templates/courseware/block_types/embed.php @@ -0,0 +1,6 @@ +
+
+
' . $payload['title']) ?>
+
' . $payload['source']) ?>
+
' . $payload['url']) ?>
+ diff --git a/templates/courseware/block_types/headline.php b/templates/courseware/block_types/headline.php new file mode 100755 index 0000000..fc57326 --- /dev/null +++ b/templates/courseware/block_types/headline.php @@ -0,0 +1,4 @@ +
+
+
+ diff --git a/templates/courseware/block_types/key_point.php b/templates/courseware/block_types/key_point.php new file mode 100755 index 0000000..714e8e8 --- /dev/null +++ b/templates/courseware/block_types/key_point.php @@ -0,0 +1,2 @@ +
+

diff --git a/templates/courseware/block_types/link.php b/templates/courseware/block_types/link.php new file mode 100755 index 0000000..c1da764 --- /dev/null +++ b/templates/courseware/block_types/link.php @@ -0,0 +1,3 @@ +
+

+

diff --git a/templates/courseware/block_types/text.php b/templates/courseware/block_types/text.php new file mode 100755 index 0000000..f92fe07 --- /dev/null +++ b/templates/courseware/block_types/text.php @@ -0,0 +1,2 @@ +
+ diff --git a/templates/courseware/block_types/typewriter.php b/templates/courseware/block_types/typewriter.php new file mode 100755 index 0000000..714e8e8 --- /dev/null +++ b/templates/courseware/block_types/typewriter.php @@ -0,0 +1,2 @@ +
+

diff --git a/templates/courseware/container_types/accordion_container.php b/templates/courseware/container_types/accordion_container.php new file mode 100644 index 0000000..afc25e0 --- /dev/null +++ b/templates/courseware/container_types/accordion_container.php @@ -0,0 +1,15 @@ +

+ +

+ + blocks->find($block_id); ?> + + type->getPdfHtmlTemplate(); ?> + + render(); ?> + + +

+ + + diff --git a/templates/courseware/container_types/default.php b/templates/courseware/container_types/default.php new file mode 100644 index 0000000..fc4fb0a --- /dev/null +++ b/templates/courseware/container_types/default.php @@ -0,0 +1 @@ +

diff --git a/templates/courseware/container_types/list_container.php b/templates/courseware/container_types/list_container.php new file mode 100644 index 0000000..1dc4376 --- /dev/null +++ b/templates/courseware/container_types/list_container.php @@ -0,0 +1,12 @@ +

+ + blocks->find($block_id); ?> + + type->getPdfHtmlTemplate(); ?> + + render(); ?> + + +

+ + diff --git a/templates/courseware/container_types/tabs_container.php b/templates/courseware/container_types/tabs_container.php new file mode 100644 index 0000000..afc25e0 --- /dev/null +++ b/templates/courseware/container_types/tabs_container.php @@ -0,0 +1,15 @@ +

+ +

+ + blocks->find($block_id); ?> + + type->getPdfHtmlTemplate(); ?> + + render(); ?> + + +

+ + + -- cgit v1.0