Skip to content
Snippets Groups Projects
Commit 439c3776 authored by Farbod Zamani's avatar Farbod Zamani Committed by Ron Lucke
Browse files

Resolves "using templates for courseware pdfExport"

parent 1abc89da
No related branches found
No related tags found
No related merge requests found
Showing
with 73 additions and 162 deletions
...@@ -398,16 +398,32 @@ abstract class BlockType ...@@ -398,16 +398,32 @@ abstract class BlockType
return $destinationFolder; 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>'; $template = null;
$html .= '<h6>' . _('Block-Daten') . ': ' . '</h6>'; try {
foreach($this->getPayload() as $key => $value) { $template_name = strtosnakecase((new \ReflectionClass($this))->getShortName());
if ($value !== '') { $template_path = $GLOBALS['template_factory']->get_path() . "courseware/block_types/{$template_name}.php";
$html .= '<h6>' . $key . ' => ' . $value . '</h6>'; 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 $template;
return $html;
} }
} }
...@@ -58,12 +58,4 @@ class Code extends BlockType ...@@ -58,12 +58,4 @@ class Code extends BlockType
{ {
return []; return [];
} }
public function pdfExport()
{
$html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>';
$html .= '<pre>' . htmlspecialchars($this->getPayload()['content']) . '</pre>';
return $html;
}
} }
...@@ -57,12 +57,4 @@ class Confirm extends BlockType ...@@ -57,12 +57,4 @@ class Confirm extends BlockType
{ {
return []; return [];
} }
public function pdfExport()
{
$html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>';
$html .= '<p>' . htmlspecialchars($this->getPayload()['text']) . '</p>';
return $html;
}
} }
...@@ -58,12 +58,4 @@ class Date extends BlockType ...@@ -58,12 +58,4 @@ class Date extends BlockType
{ {
return []; 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;
}
} }
...@@ -121,16 +121,4 @@ class Embed extends BlockType ...@@ -121,16 +121,4 @@ class Embed extends BlockType
{ {
return []; 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;
}
} }
...@@ -106,13 +106,4 @@ class Headline extends BlockType ...@@ -106,13 +106,4 @@ class Headline extends BlockType
{ {
return []; 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;
}
} }
...@@ -59,12 +59,4 @@ class KeyPoint extends BlockType ...@@ -59,12 +59,4 @@ class KeyPoint extends BlockType
{ {
return []; return [];
} }
public function pdfExport()
{
$html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>';
$html .= '<p>' . htmlspecialchars($this->getPayload()['text']) . '</p>';
return $html;
}
} }
...@@ -60,13 +60,4 @@ class Link extends BlockType ...@@ -60,13 +60,4 @@ class Link extends BlockType
{ {
return []; 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;
}
} }
...@@ -165,12 +165,4 @@ class Text extends BlockType ...@@ -165,12 +165,4 @@ class Text extends BlockType
return array(); return array();
}); });
} }
public function pdfExport()
{
$html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>';
$html .= $this->getPayload()['text'];
return $html;
}
} }
...@@ -60,12 +60,4 @@ class Typewriter extends BlockType ...@@ -60,12 +60,4 @@ class Typewriter extends BlockType
{ {
return []; return [];
} }
public function pdfExport()
{
$html = '<h5>' . sprintf(_('Block-Typ: %s'), $this->getTitle()) . '</h5>';
$html .= '<p>' . htmlspecialchars($this->getPayload()['text']) . '</p>';
return $html;
}
} }
...@@ -56,28 +56,4 @@ class AccordionContainer extends ContainerType ...@@ -56,28 +56,4 @@ class AccordionContainer extends ContainerType
return Schema::fromJsonString(file_get_contents($schemaFile)); 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;
}
} }
...@@ -245,14 +245,33 @@ abstract class ContainerType ...@@ -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>'; $template = null;
try {
foreach ($this->container->blocks as $block) { $template_name = strtosnakecase((new \ReflectionClass($this))->getShortName());
$html .= $block->type->PdfExport(); $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 $template;
return $html;
} }
} }
...@@ -56,24 +56,4 @@ class ListContainer extends ContainerType ...@@ -56,24 +56,4 @@ class ListContainer extends ContainerType
return Schema::fromJsonString(file_get_contents($schemaFile)); 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;
}
} }
...@@ -57,28 +57,4 @@ class TabsContainer extends ContainerType ...@@ -57,28 +57,4 @@ class TabsContainer extends ContainerType
return Schema::fromJsonString(file_get_contents($schemaFile)); 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;
}
} }
...@@ -768,8 +768,10 @@ SQL; ...@@ -768,8 +768,10 @@ SQL;
{ {
$containers = \Courseware\Container::findBySQL('structural_element_id = ?', [$this->id]); $containers = \Courseware\Container::findBySQL('structural_element_id = ?', [$this->id]);
$html = '';
foreach ($containers as $container) { foreach ($containers as $container) {
$html .= $container->type->pdfExport(); $container_html_template = $container->type->getPdfHtmlTemplate();
$html .= $container_html_template ? $container_html_template->render() : '';
} }
return $html; return $html;
......
<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5>
<pre><?= htmlReady($payload['content']) ?></pre>
<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5>
<p><?= htmlReady($payload['text']) ?></p>
<h5><?= sprintf(_('Block-Typ: %s'), htmlReady($title)) ?></h5>
<p><?= date('d.m.Y h:i', (int) $payload['timestamp'] / 1000) ?></p>
<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; ?>
<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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment