Commit ee9dd158 authored by Robert Lyon's avatar Robert Lyon

Bug 1826284: Pdf block renders export different for pdf

When the exporttype is pdf we render the block differently

behatnotneeded

Change-Id: I7b444d9e81434f237be003c92e5230c17e421c7b
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 63ccc41f
......@@ -33,6 +33,38 @@ class PluginBlocktypePdf extends MaharaCoreBlocktype {
return array('fileimagevideo' => 8000);
}
public static function render_instance_export(BlockInstance $instance, $editing=false, $versioning=false, $exporting=null) {
if ($exporting != 'pdf') {
return self::render_instance($instance, $editing, $versioning);
}
// The exporting for PDF
require_once(get_config('docroot') . 'lib/view.php');
$configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
$configdata['viewid'] = $instance->get('view');
$view = new View($configdata['viewid']);
$artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
$html = '';
if ($artefactid) {
$artefact = $instance->get_artefact_instance($configdata['artefactid']);
if (!file_exists($artefact->get_path())) {
return '';
}
$urlbase = get_config('wwwroot');
$url = $urlbase . 'artefact/file/download.php?file=' . $artefactid . '&view=' . $view->get('id');
$description = $artefact->get('description');
if ($description) {
$html .= '<div class="card-body">' . $description . '</div>';
}
$html .= '<div class="text-midtone">' . get_string('notrendertopdf', 'artefact.file');
$html .= '<br>' . get_string('notrendertopdffiles', 'artefact.file', 1);
// We need to add an <a> link so that the HTML export() sub-task makes a copy of the artefct for the export 'files/' directory
// We then override the link in the PDF pdf_view_export_data() function.
$html .= '<a href="' . $url . '">' . $artefact->get('title') . '</a>';
$html .= '</div>';
}
return $html;
}
public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
global $USER;
require_once(get_config('docroot') . 'lib/view.php');
......
......@@ -385,3 +385,8 @@ $string['viruszipfile'] = 'Clam AV has found a file that is infected with a viru
$string['filetypenotallowed'] = 'Files with extension "%s" are not allowed.';
$string['filetypenotmatchingmimetype'] = 'File extension does not match file mimetype "%s".';
$string['validfiletypes'] = 'You can upload the following file types:';
$string['notrendertopdf'] = 'This block can not be rendered normally in the PDF export.';
$string['notrendertopdffiles'] = array(
'The file has been added to: ',
'The files have been added to: ',
);
......@@ -159,6 +159,27 @@ class PluginExportPdf extends PluginExportHtml {
$directory = $this->exportdir . '/' . $this->rootdir . '/views/' . parent::text_to_filename($view->get('title'));
}
$filename = $directory . "/index.html";
// Adjust the relative links to files to be textual to mention where the file lives within the zip file
// Because we can't make relative links in a pdf export
$filedata = file_get_contents($filename);
if ($view->get('newlayout')) {
if (preg_match('/var blocks = (\[.*?\]);/', $filedata, $matches)) {
$content = json_decode($matches[1]);
foreach ($content as $c) {
$c->content = preg_replace('/\<a href=\"\.\/(.*?)\".*?\>(.*?)\<\/a\>/s', "$1", $c->content); // $1 = url, $2 = name
// Strip other links out
$c->content = preg_replace('/\<a.*? href=.*?\>(.*?)\<\\/a\>/s', "$1", $c->content); // $1 = name
}
$content = json_encode($content);
$filedata = preg_replace('/var blocks = \[.*?\];/', 'var blocks = ' . $content, $filedata);
}
}
else {
$filedata = preg_replace('/\<a href=\"\.\/(.*?)\".*?\>(.*?)\<\/a\>/s', "$1", $filedata); // $1 = url, $2 = name
// Strip other links out
$filedata = preg_replace('/\<a.*? href=.*?\>(.*?)\<\/a\>/s', "$1", $filedata); // $1 = name
}
file_put_contents($filename, $filedata, LOCK_EX);
// Navigate to the needed page
$page->navigate('file://' . $filename)->waitForNavigation();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment