Skip to content

Commit

Permalink
Merge branch 'MDL-67547' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 26, 2020
2 parents 1a3a864 + f8f5a2f commit 7d02452
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 18 deletions.
11 changes: 11 additions & 0 deletions dataformat/html/classes/writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,24 @@ public function start_sheet($columns) {
echo \html_writer::end_tag('tr');
}

/**
* Method to define whether the dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return true;
}

/**
* Write a single record
*
* @param array $record
* @param int $rownum
*/
public function write_record($record, $rownum) {
$record = $this->format_record($record);

echo \html_writer::start_tag('tr');
foreach ($record as $cell) {
echo \html_writer::tag('td', $cell);
Expand Down
2 changes: 1 addition & 1 deletion dataformat/json/classes/writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function write_record($record, $rownum) {
echo ",";
}

echo json_encode($record, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($this->format_record($record), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

$this->sheetdatadded = true;
}
Expand Down
50 changes: 43 additions & 7 deletions dataformat/pdf/classes/writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,52 @@ public function start_sheet($columns) {
$this->print_heading();
}

/**
* Method to define whether the dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return true;
}

/**
* When exporting images, we need to return their Base64 encoded content. Otherwise TCPDF will create a HTTP
* request for them, which will lead to the login page (i.e. not the image it expects) and throw an exception
*
* Note: ideally we would copy the file to a temp location and return it's path, but a bug in TCPDF currently
* prevents that
*
* @param \stored_file $file
* @return string|null
*/
protected function export_html_image_source(\stored_file $file): ?string {
// Set upper dimensions for embedded images.
$resizedimage = $file->resize_image(400, 300);

return '@' . base64_encode($resizedimage);
}

/**
* Write a single record
*
* @param array $record
* @param int $rownum
*/
public function write_record($record, $rownum) {
$rowheight = 0;

// If $record is an object convert it to an array.
if (is_object($record)) {
$record = (array)$record;
}

$record = $this->format_record($record);
foreach ($record as $cell) {
$rowheight = max($rowheight, $this->pdf->getStringHeight($this->colwidth, $cell, false, true, '', 1));
// We need to calculate the row height (accounting for any content). Unfortunately TCPDF doesn't provide an easy
// method to do that, so we create a second PDF inside a transaction, add cell content and use the largest cell by
// height. Solution similar to that at https://stackoverflow.com/a/1943096.
$pdf2 = clone $this->pdf;
$pdf2->startTransaction();
$pdf2->AddPage('L');
$pdf2->writeHTMLCell($this->colwidth, 0, '', '', $cell, 1, 1, false, true, 'L');
$rowheight = max($rowheight, $pdf2->getY() - $pdf2->getMargins()['top']);
$pdf2->rollbackTransaction();
}

$margins = $this->pdf->getMargins();
Expand All @@ -123,7 +159,7 @@ public function write_record($record, $rownum) {
// Determine whether we're at the last element of the record.
$nextposition = ($lastkey === $key) ? 1 : 0;
// Write the element.
$this->pdf->Multicell($this->colwidth, $rowheight, $cell, 1, 'L', false, $nextposition);
$this->pdf->writeHTMLCell($this->colwidth, $rowheight, '', '', $cell, 1, $nextposition, false, true, 'L');
}
}

Expand Down
74 changes: 74 additions & 0 deletions dataformat/pdf/tests/writer_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tests for the dataformat_pdf writer
*
* @package dataformat_pdf
* @copyright 2020 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace dataformat_pdf;

use core\dataformat;
use context_system;
use html_writer;
use moodle_url;

/**
* Writer tests
*
* @package dataformat_pdf
* @copyright 2020 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class writer_testcase extends \advanced_testcase {

/**
* Test writing data whose content contains an image with pluginfile.php source
*/
public function test_write_data_with_pluginfile_image(): void {
global $CFG;

$this->resetAfterTest(true);

$imagefixture = "{$CFG->dirroot}/lib/filestorage/tests/fixtures/testimage.jpg";
$image = get_file_storage()->create_file_from_pathname([
'contextid' => context_system::instance()->id,
'component' => 'dataformat_pdf',
'filearea' => 'test',
'itemid' => 0,
'filepath' => '/',
'filename' => basename($imagefixture),

], $imagefixture);

$imageurl = moodle_url::make_pluginfile_url($image->get_contextid(), $image->get_component(), $image->get_filearea(),
$image->get_itemid(), $image->get_filepath(), $image->get_filename());

// Insert out test image into the data so it is exported.
$columns = ['animal', 'image'];
$row = ['cat', html_writer::img($imageurl->out(), 'My image')];

// Export to file. Assert that the exported file exists.
$exportfile = dataformat::write_data('My export', 'pdf', $columns, [$row]);
$this->assertFileExists($exportfile);

// The exported file should be a reasonable size (~275kb).
$this->assertGreaterThan(270000, filesize($exportfile));
}
}
7 changes: 7 additions & 0 deletions dataformat/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ information provided here is intended especially for developers.
* Calls to the following dataformat plugin methods have been removed:
- write_header()
- write_footer()
* The following methods have been added to the base class to allow instances to define support for exporting
HTML content, with additional support for defining how images should be embedded:
- supports_html()
- export_html_image_source()
* Dataformat writers should also call the following method to ensure data is properly formatted before being
written, which takes into account prior methods defining support for HTML:
- format_record()

=== 3.4 ===
* In order to allow multiple sheets in an exported file the functions write_header() and write_footer() have
Expand Down
76 changes: 76 additions & 0 deletions lib/classes/dataformat/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,82 @@ public function start_sheet($columns) {
// Override me if needed.
}

/**
* Method to define whether the dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return false;
}

/**
* Apply formatting to the cells of a given record
*
* @param array|\stdClass $record
* @return array
*/
protected function format_record($record): array {
$record = (array)$record;

// If the dataformat supports export of HTML, we need to allow them to manage embedded images.
if ($this->supports_html()) {
$record = array_map([$this, 'replace_pluginfile_images'], $record);
}

return $record;
}

/**
* Given a stored_file, return a suitable source attribute for an img element in the export (or null to use the original)
*
* @param \stored_file $file
* @return string|null
*/
protected function export_html_image_source(\stored_file $file): ?string {
return null;
}

/**
* We need to locate all img tags within a given cell that match pluginfile URL's. Partly so the exported file will show
* the image without requiring the user is logged in; and also to prevent some of the dataformats requesting the file
* themselves, which is likely to fail due to them not having an active session
*
* @param string|null $content
* @return string
*/
protected function replace_pluginfile_images(?string $content): string {
$content = (string)$content;

// Examine content to see if it contains any HTML image tags.
return preg_replace_callback('/(?<pre><img[^>]+src=")(?<source>[^"]*)(?<post>".*>)/i', function(array $matches) {
$source = $matches['source'];

// Now check if the image source looks like a pluginfile URL.
if (preg_match('/pluginfile.php\/(?<context>\d+)\/(?<component>[^\/]+)\/(?<filearea>[^\/]+)\/(?:(?<itemid>\d+)\/)?' .
'(?<path>.*)/u', $source, $args)) {

$context = $args['context'];
$component = clean_param($args['component'], PARAM_COMPONENT);
$filearea = clean_param($args['filearea'], PARAM_AREA);
$itemid = $args['itemid'] ?: 0;
$path = clean_param(urldecode($args['path']), PARAM_PATH);

// Try and get the matching file from storage, allow the dataformat to define the replacement source.
$fullpath = "/{$context}/{$component}/{$filearea}/{$itemid}/{$path}";
if ($file = get_file_storage()->get_file_by_hash(sha1($fullpath))) {
$exportsource = $this->export_html_image_source($file);

if ($exportsource) {
$source = $exportsource;
}
}
}

return $matches['pre'] . $source . $matches['post'];
}, $content);
}

/**
* Write a single record
*
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/dataformat/spout_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public function start_sheet($columns) {
/**
* Write a single record
*
* @param object $record
* @param array $record
* @param int $rownum
*/
public function write_record($record, $rownum) {
$row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$record);
$row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray($this->format_record($record));
$this->writer->addRow($row);
}

Expand Down
11 changes: 10 additions & 1 deletion lib/tablelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
*/
class table_dataformat_export_format extends table_default_export_format_parent {

/** @var $dataformat */
/** @var \core\dataformat\base $dataformat */
protected $dataformat;

/** @var $rownum */
Expand Down Expand Up @@ -2138,6 +2138,15 @@ public function __construct(&$table, $dataformat) {
\core\session\manager::write_close();
}

/**
* Whether the current dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return $this->dataformat->supports_html();
}

/**
* Start document
*
Expand Down
14 changes: 7 additions & 7 deletions mod/forum/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow', 'deleted', 'privatereplyto',
'privatereplytofullname', 'wordcount', 'charcount'];

$canviewfullname = has_capability('moodle/site:viewfullnames', $forum->get_context());
$canviewfullname = has_capability('moodle/site:viewfullnames', $context);

$datamapper = $legacydatamapperfactory->get_post_data_mapper();
$exportdata = new ArrayObject($datamapper->to_legacy_objects($posts));
Expand All @@ -132,7 +132,7 @@
$dataformat,
$fields,
$iterator,
function($exportdata) use ($fields, $striphtml, $humandates, $canviewfullname) {
function($exportdata) use ($fields, $striphtml, $humandates, $canviewfullname, $context) {
$data = new stdClass();

foreach ($fields as $field) {
Expand All @@ -149,18 +149,18 @@ function($exportdata) use ($fields, $striphtml, $humandates, $canviewfullname) {
$data->privatereplytofullname = fullname($user, $canviewfullname);
}

if ($field == 'message') {
$data->message = file_rewrite_pluginfile_urls($data->message, 'pluginfile.php', $context->id, 'mod_forum',
'post', $data->id);
}

// Convert any boolean fields to their integer equivalent for output.
if (is_bool($data->$field)) {
$data->$field = (int) $data->$field;
}
}

if ($striphtml) {
// The following call to html_to_text uses the option that strips out
// all URLs, but format_text complains if it finds @@PLUGINFILE@@ tokens.
// So, we need to replace @@PLUGINFILE@@ with a real URL, but it doesn't
// matter what. We use http://example.com/.
$data->message = str_replace('@@PLUGINFILE@@/', 'http://example.com/', $data->message);
$data->message = html_to_text(format_text($data->message, $data->messageformat), 0, false);
$data->messageformat = FORMAT_PLAIN;
}
Expand Down

0 comments on commit 7d02452

Please sign in to comment.