aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware
diff options
context:
space:
mode:
authorFarbod Zamani <zamani@elan-ev.de>2022-02-25 11:21:08 +0000
committerRon Lucke <lucke@elan-ev.de>2022-02-25 11:21:08 +0000
commit439c37760f736b7806b801b1e609e83baff0ffa3 (patch)
treef11913f4b39201cae389a408818921ffcf6a06d7 /lib/models/Courseware
parent1abc89da19452000a39201f86ee2acf94f3c9ef3 (diff)
Resolves "using templates for courseware pdfExport"
Diffstat (limited to 'lib/models/Courseware')
-rwxr-xr-xlib/models/Courseware/BlockTypes/BlockType.php32
-rwxr-xr-xlib/models/Courseware/BlockTypes/Code.php8
-rwxr-xr-xlib/models/Courseware/BlockTypes/Confirm.php8
-rwxr-xr-xlib/models/Courseware/BlockTypes/Date.php8
-rwxr-xr-xlib/models/Courseware/BlockTypes/Embed.php12
-rwxr-xr-xlib/models/Courseware/BlockTypes/Headline.php9
-rwxr-xr-xlib/models/Courseware/BlockTypes/KeyPoint.php8
-rwxr-xr-xlib/models/Courseware/BlockTypes/Link.php9
-rwxr-xr-xlib/models/Courseware/BlockTypes/Text.php8
-rwxr-xr-xlib/models/Courseware/BlockTypes/Typewriter.php8
-rwxr-xr-xlib/models/Courseware/ContainerTypes/AccordionContainer.php24
-rwxr-xr-xlib/models/Courseware/ContainerTypes/ContainerType.php33
-rwxr-xr-xlib/models/Courseware/ContainerTypes/ListContainer.php20
-rwxr-xr-xlib/models/Courseware/ContainerTypes/TabsContainer.php24
-rwxr-xr-xlib/models/Courseware/StructuralElement.php4
15 files changed, 53 insertions, 162 deletions
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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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 = '<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 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;