ÿØÿà JFIF    ÿÛ „  ( %!1"%)+...383-7(-.+  -%%--------+------------/----------------------------ÿÀ  ¶" ÿÄ     ÿÄ @    !1AQaq‘¡"±ÁÑð2’BRráS‚ñ3b¢CT²Ò #cÿÄ    ÿÄ '   1!AQ2a"‘±#ÿÚ   ? ò(/p‘—–¢·,M|¯G»R£üešT. ôŸEoCFñ‡ 4ÔkåóxR|µÁ¨ÓDà!Ç´µ˜‡p ÍQ‹§!þž^õWsŒy  åÁ§$á«%ºq˜™JyyË0y«^Ϗ -3ÃÏËÈ£Æ\P’ä„s&•‹Üs üô'àWt¹pñCp$bÚF`·7.^)¾!)ÒÙ˜–ÅÏš^h¾ø³·ìe±ŒaeÖ—Í,e=B}Jéߦ$$½—šÙÜCC¯¶è8β–Åkèwx LÉngT±æ¹£Ócœ¿Í/©Éÿ ‰ÚCÒ­mq;Tt¶–}0"zÖPcd1š­¤tˆ„ÐÓâqú[™öUCŠ2޿د­É(¸ßúçšmW,Ò ºf‹Ån™ÄLêÁ8üÁkÁ€L¥ÍuJ1œôrÂSŒÓöt!7EUíïb)AËi¬â-ÙHk8òÁiXYwÅ)O^"ÇØriÛô{»wRUKÙÏÚlÓ¬€‚’שT ] ªb—ÊîKC^×M“xùäñz¬zJ× {4[¹M'IÆeH1LCWÕ]œûº WDæà°OÅ‚¹c²ÞuA–rL`HšpCez‡V’— Ž‚Ë„–ñ­'µR0ÕÖ=À:±G»C\nÆÉ5*¢ †3šhQ4ýÒi$Õ1"Ü]¢p-q3zcQ$ªö¨¥æf›‘»´ÝÚHá„e²JÇ–iÊ:·à¬Øc5Åk»Laª*wi [0Ówk ¦a¦0Õ²ÅÅ‚T¸’³q:Æ<¹ú Ò˜Æ}2VE•Ѐp¡Ô3Úº …h³^LJºçì¥Ánó|˜ÖˆÎˆ+« ¦dø‘Mº–d\ªDðÛ„Ì×LÝ——Ð9Ç|•FèçVm™¨€™3õS–9Äå¢<̉Ìm¢±`·º˜&Z²ž Ë®D«¶»6™‰©­HÈ Àá«h­i³04æ’hxœi3ï¨(ÓL·†‹¶ËpŒ ƒÜÒ? :õdsÀ –3æÓ·Üjâ¬9—]8n¼&]I F£¼…ì–—Œ0v°LǐÅfÛ2TÖ’aµ˜N@ÖBSÏ^à´4-±°b×nÈÈœÆD=KdŒø…ÄÈK,›ÖR§fjü$^ÔÎ!„6^ s:Üun\ôkCÞò÷<—Lúl Í›G¹ÀPá¼Nc¡šÒ…ÙWͳœ±vÊáЄ×9‹P‰cEØ‘™›µŸP<×g1 º©Ø¬wiÁ^l5× I/';Ë+´Z‡ÓÄFÕp[Èð’ HÄ ó 9¬Dl4“é±Ï”V=^Hð;žLädÉa‚°Ë,#/Ž~²BÔÛ QãñQtIeórVB,ÏÂf‹4#Μc‹É3ZIÖ Œ¢¥·üxa‡Ãçè„jŠ[5 Ä!U?,3žÎ×…ðdÓ÷háŠA‰ìÄ4÷ÃÜZÃE{‰®+W\ZÍE[‰»µk»MÝ­`¢©†˜ÃV‹-f¢¡†¢a«eŠ%«Y¨©q%jâd,Ôq`) ¤¦…šˆ ¦E>0Ùb²t–Ÿ  ÷jsςҒKÈcø(é›;k(’#ôŒQ ™C> õ³M9óºÆµ¤çâ;uÀ)5>RÔ¸fÓ~Ȧ—’-$fe«"FâUˆÊwµPÏu w+"åÉó¨6s™Û9(À¨ù©hE°½†n`m~“@©›p!Vkhµ Á1šbEqqÊ@ºèl›2 –Yã%‘‚hÛ{a¶‡ÅÞM²¬çL34®Çl]®‹ŽØŒm'RÜe35æöˆ­Á­ cP9šeÁköo´L³Ì=p8]”‡B¶,šº|ɏdzXˆØk*ÇÚ{#ñ‰pã(€¶‡9ý'šÜ³½¯kƒ†Â’ëSOƒ•Å®H6+a¢µˆ‹Y¨b˜b+XˆÖ!°u!¢"µˆˆn2ˆ Å0Ä`Å Ô®c(ÓÜGº•Ä»Œ âWî%q.áÐÄ÷î%umÍ£+ÜQ,Vn¦-GphU¸¢X­&,Gt ·K¢ÅÅ·¥RÄÊÉbKn N­R2g$VµÕó Qr#–ÓZUÏð0ÎtÙÀœz ëöo“Îk¯n„T°"SÞ+³/…€½"ižãO%Í%&Δâ‘ËØ´IˆÓ°Ë‰ÞM3.™yçÁzit›`4C`k¢¼á!álÄï sÂ[×!¤l¾æo"ƒIÔ'@ªu)3›ô®“@Z à¾] üœ'‡4¸PÞkÁ-qÄH‰ÓÍBd´ÿ —¹°ÄA&ÊUC„öz‚‚RæŠtf¬>˜ÑidšEçÑL÷áý¡V±h§­c¿¥¨ƒ@g¼…Ýö^ÞËc.ž„æ“I[ ª+·zÐÒÚ,½Ð»¶€C›7jk$๖€Ž¾ÅÙpp¶ÍþéΤ ½ØÎŒq ñpæžÅ¥#Øâ49ÃÂK^MYV“,¶Éz_òæ:…;Õ«¦I3âJ£¤û5$7Èxün\â]Êe¯(Ÿ&Ž‹¶2<6Ćf× æ¢ƱqWı[(ÙÂŽç‚T7¨@ÈÉÍß‚ïÙ"&3TYlGށ†)†#5ŠA©^T2ÆÁ†)©†©†¤yGXÁ†©"†)©¼ÅV WS†#©]R–r‹q5ÕbâWþBh¯u+ªÅÄ×ü„nÑ^êkªÁj‰jeœWˆÕÕ`µDµ2Ì+Ä µDµµ5Ô{¤Þ0RF’H÷AÛ<ÊÁ§ ƈæ0Ñ£ê4 öÚµ[¬/1²€â<-¬šö²r×0o×w ´xÐÒË„ g01¬„åEM蛇Ã\6µE›‘fÙO k÷CºnÓ1`iîÒ² áÂ!ÑLÄÿ ³¬\õ*:M=Ç»c‹2[‰ØsÕLò\ŒK L^™üÄN\¶#» Ç\……ßïIœÉ$¸ÎñÍ®ÔHÇ Ûí/´>Li”„†g^$ 7NÍÑÑ2M2ªNg=k½Ð}Ÿl6‡ —JWˆ”ÞéT·+µ¦üÂFè¢Vs6ýÆ4»2+2IÝ9ô9®ÇGÙÇ1+Ä8;*Õ§ž½D*îÑWÚçá3Ma¤·ˆ¥t}š²Ü…Q+Æüµ8HíS”üxGéÁˆ¦Ël„éÈ 5 µÄƒ6äÜ(kIš¯S†fÖ&¸®×v|Äyˆ*禌Éù@7-®Ä[]ÎÖ¿ê`º…=:…žJVnݳvðÖKE¼‚ »mà ÷f®:Ë ™–4rð¡4` ܹçž~‹C=œ…¾Ïj|VDtK.ƒ"9 Ï ­–éXá³6WÒïIL-Ї Ç excel/lang/en/dataformat_excel.php000064400000002072152266350720013213 0ustar00. /** * Excel dataformat lang strings. * * @package dataformat_excel * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['dataformat'] = 'Microsoft Excel (.xlsx)'; $string['privacy:metadata'] = 'The Excel data format plugin does not store any personal data.'; $string['shortname'] = 'Excel'; excel/version.php000064400000002104152266350720010047 0ustar00. /** * Data activity filter version information * * @package dataformat_excel * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2025041400; $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'dataformat_excel'; excel/classes/privacy/provider.php000064400000002770152266350720013337 0ustar00. /** * Privacy provider implementation for dataformat_excel. * * @package dataformat_excel * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_excel\privacy; defined('MOODLE_INTERNAL') || die(); /** * Privacy provider implementation for dataformat_excel. * * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements \core_privacy\local\metadata\null_provider { /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ public static function get_reason(): string { return 'privacy:metadata'; } } excel/classes/writer.php000064400000004014152266350720011335 0ustar00. /** * Excel data format writer * * @package dataformat_excel * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_excel; defined('MOODLE_INTERNAL') || die(); /** * Excel data format writer * * @package dataformat_excel * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer extends \core\dataformat\spout_base { /** @var $mimetype */ protected $mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; /** @var $extension */ protected $extension = ".xlsx"; /** * Set the title of the worksheet inside a spreadsheet * * For some formats this will be ignored. * * @param string $title */ public function set_sheettitle($title) { if (!$title) { return; } // Replace any characters in the name that Excel cannot cope with. $title = strtr(trim($title, "'"), '[]*/\?:', ' '); // Shorten the title if necessary. $title = \core_text::substr($title, 0, 31); // After the substr, we might now have a single quote on the end. $title = trim($title, "'"); $this->sheettitle = $title; } } excel/tests/writer_test.php000064400000005301152266350720012101 0ustar00. namespace dataformat_excel; use core\dataformat; /** * Tests for the dataformat_excel writer * * @package dataformat_excel * @copyright 2022 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class writer_test extends \advanced_testcase { /** * Test writing data whose content contains an image with pluginfile.php source */ public function test_write_data(): void { $columns = ['fruit', 'colour', 'animal']; $rows = [ ['banana', 'yellow', 'monkey'], ['apple', 'red', 'wolf'], ['melon', 'green', 'aardvark'], ]; // Export to file. $exportfile = dataformat::write_data('My export', 'excel', $columns, $rows); // Read the file. $excelcells = $this->get_excel(file_get_contents($exportfile)); $this->assertEquals(array_merge([$columns], $rows), $excelcells); } /** * Get an Excel object to check the content * * @param string $content * @return array two-dimensional array with cell values */ private function get_excel(string $content) { $file = tempnam(sys_get_temp_dir(), 'excel_'); $handle = fopen($file, "w"); fwrite($handle, $content); /** @var \OpenSpout\Reader\XLSX\Reader $reader */ $reader = \OpenSpout\Reader\Common\Creator\ReaderFactory::createFromFileByMimeType($file); $reader->open($file); /** @var \OpenSpout\Reader\XLSX\Sheet[] $sheets */ $sheets = $reader->getSheetIterator(); $rowscellsvalues = []; foreach ($sheets as $sheet) { /** @var \OpenSpout\Common\Entity\Row[] $rows */ $rows = $sheet->getRowIterator(); foreach ($rows as $row) { $thisvalues = []; foreach ($row->getCells() as $cell) { $thisvalues[] = $cell->getValue(); } $rowscellsvalues[] = $thisvalues; } } return $rowscellsvalues; } } pdf/lang/en/dataformat_pdf.php000064400000002062152266350720012334 0ustar00. /** * pdf dataformat lang strings. * * @package dataformat_pdf * @copyright 2019 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['dataformat'] = 'Portable Document Format (.pdf)'; $string['privacy:metadata'] = 'The PDF data format plugin does not store any personal data.'; $string['shortname'] = 'PDF'; pdf/version.php000064400000002067152266350720007530 0ustar00. /** * Data activity filter version information * * @package dataformat_pdf * @copyright 2019 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2025041400; $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'dataformat_pdf'; pdf/classes/privacy/provider.php000064400000002754152266350720013012 0ustar00. /** * Privacy provider implementation for dataformat_pdf. * * @package dataformat_pdf * @copyright 2019 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_pdf\privacy; defined('MOODLE_INTERNAL') || die(); /** * Privacy provider implementation for dataformat_pdf. * * @copyright 2019 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements \core_privacy\local\metadata\null_provider { /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ public static function get_reason(): string { return 'privacy:metadata'; } } pdf/classes/writer.php000064400000015570152266350720011017 0ustar00. /** * pdf data format writer * * @package dataformat_pdf * @copyright 2019 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_pdf; defined('MOODLE_INTERNAL') || die(); /** * pdf data format writer * * @package dataformat_pdf * @copyright 2019 Shamim Rezaie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer extends \core\dataformat\base { public $mimetype = "application/pdf"; public $extension = ".pdf"; /** * @var \pdf The pdf object that is used to generate the pdf file. */ protected $pdf; /** * @var float Each column's width in the current sheet. */ protected $colwidth; /** * @var string[] Title of columns in the current sheet. */ protected $columns; /** * writer constructor. */ public function __construct() { global $CFG; require_once($CFG->libdir . '/pdflib.php'); $this->pdf = new \pdf(); $this->pdf->setPrintHeader(false); $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // Set background color for headings. $this->pdf->SetFillColor(238, 238, 238); } public function send_http_headers() { } /** * Start output to file, note that the actual writing of the file is done in {@see close_output_to_file()} */ public function start_output_to_file(): void { $this->start_output(); } public function start_output() { $this->pdf->AddPage('L'); } public function start_sheet($columns) { $margins = $this->pdf->getMargins(); $pagewidth = $this->pdf->getPageWidth() - $margins['left'] - $margins['right']; $this->colwidth = $pagewidth / count($columns); $this->columns = $columns; $this->print_heading($this->pdf); } /** * 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; $record = $this->format_record($record); foreach ($record as $cell) { // 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(); $numpages = $pdf2->getNumPages(); $pdf2->AddPage('L'); $this->print_heading($pdf2); $pdf2->writeHTMLCell($this->colwidth, 0, '', '', $cell, 1, 1, false, true, 'L'); $pagesadded = $pdf2->getNumPages() - $numpages; $margins = $pdf2->getMargins(); $pageheight = $pdf2->getPageHeight() - $margins['top'] - $margins['bottom']; $cellheight = ($pagesadded - 1) * $pageheight + $pdf2->getY() - $margins['top'] - $this->get_heading_height(); $rowheight = max($rowheight, $cellheight); $pdf2->rollbackTransaction(); } $margins = $this->pdf->getMargins(); if ($this->pdf->getNumPages() > 1 && ($this->pdf->GetY() + $rowheight + $margins['bottom'] > $this->pdf->getPageHeight())) { $this->pdf->AddPage('L'); $this->print_heading($this->pdf); } // Get the last key for this record. end($record); $lastkey = key($record); // Reset the record pointer. reset($record); // Loop through each element. foreach ($record as $key => $cell) { // Determine whether we're at the last element of the record. $nextposition = ($lastkey === $key) ? 1 : 0; // Write the element. $this->pdf->writeHTMLCell($this->colwidth, $rowheight, '', '', $cell, 1, $nextposition, false, true, 'L'); } } public function close_output() { $filename = $this->filename . $this->get_extension(); $this->pdf->Output($filename, 'D'); } /** * Write data to disk * * @return bool */ public function close_output_to_file(): bool { $this->pdf->Output($this->filepath, 'F'); return true; } /** * Prints the heading row for a given PDF. * * @param \pdf $pdf A pdf to print headings in */ private function print_heading(\pdf $pdf) { $fontfamily = $pdf->getFontFamily(); $fontstyle = $pdf->getFontStyle(); $pdf->SetFont($fontfamily, 'B'); $total = count($this->columns); $counter = 1; foreach ($this->columns as $columns) { $nextposition = ($counter == $total) ? 1 : 0; $pdf->Multicell($this->colwidth, $this->get_heading_height(), $columns, 1, 'C', true, $nextposition); $counter++; } $pdf->SetFont($fontfamily, $fontstyle); } /** * Returns the heading height. * * @return int */ private function get_heading_height() { $height = 0; foreach ($this->columns as $columns) { $height = max($height, $this->pdf->getStringHeight($this->colwidth, $columns, false, true, '', 1)); } return $height; } } pdf/tests/writer_test.php000064400000005072152266350720011557 0ustar00. /** * Tests for the dataformat_pdf writer * * @package dataformat_pdf * @copyright 2020 Paul Holden * @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 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class writer_test 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)); } } upgrade.txt000064400000003566152266350720006756 0ustar00=== 4.5 Onwards === This file has been replaced by UPGRADING.md. See MDL-81125 for further information. === This files describes API changes in /dataformat/ download system, information provided here is intended especially for developers. === 3.11.1 === * Optional $callback for the following \core\dataformat methods now receive a second argument to define whether the current format writer supports HTML: - download_data() - write_data() - write_data_to_filearea() === 3.9 === * The following methods have been added to the base dataformat class to allow instances to export to a local file. They can be overridden in extending classes to define how files should be created: - start_output_to_file() - close_output_to_file() * 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 been removed from core dataformat plugins and have been replaced. - write_header() has been replaced with the two functions start_output() and start_sheet(). - write_footer() has been replaced with the two functions close_output() and close_sheet(). For backwards compatibility write_header() and write_footer() will continue to work but if used will trigger the function error_log(). === 3.1 === * Added new plugin system with low memory support for csv, ods, xls and json ods/lang/en/dataformat_ods.php000064400000002100152266350720012355 0ustar00. /** * ODS dataformat lang strings. * * @package dataformat_ods * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['dataformat'] = 'OpenDocument (.ods)'; $string['privacy:metadata'] = 'The OpenDocument data format plugin does not store any personal data.'; $string['shortname'] = 'OpenDocument'; ods/version.php000064400000002100152266350720007530 0ustar00. /** * Data activity filter version information * * @package dataformat_ods * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2025041400; $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'dataformat_ods'; ods/classes/privacy/provider.php000064400000002760152266350720013023 0ustar00. /** * Privacy provider implementation for dataformat_ods. * * @package dataformat_ods * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_ods\privacy; defined('MOODLE_INTERNAL') || die(); /** * Privacy provider implementation for dataformat_ods. * * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements \core_privacy\local\metadata\null_provider { /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ public static function get_reason(): string { return 'privacy:metadata'; } } ods/classes/writer.php000064400000003753152266350720011033 0ustar00. /** * ODS data format writer * * @package dataformat_ods * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_ods; defined('MOODLE_INTERNAL') || die(); /** * ODS data format writer * * @package dataformat_ods * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer extends \core\dataformat\spout_base { /** @var $mimetype */ protected $mimetype = "application/vnd.oasis.opendocument.spreadsheet"; /** @var $extension */ protected $extension = ".ods"; /** * Set the title of the worksheet inside a spreadsheet * * For some formats this will be ignored. * * @param string $title */ public function set_sheettitle($title) { if (!$title) { return; } // Replace any characters in the name that ODS cannot cope with. $title = strtr(trim($title, "'"), '[]*/\?:', ' '); // Shorten the title if necessary. $title = \core_text::substr($title, 0, 31); // After the substr, we might now have a single quote on the end. $title = trim($title, "'"); $this->sheettitle = $title; } } ods/tests/writer_test.php000064400000005404152266350720011572 0ustar00. namespace dataformat_ods; use core\dataformat; /** * Tests for the dataformat_ods writer * * @package dataformat_ods * @copyright 2022 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class writer_test extends \advanced_testcase { /** * Test writing data whose content contains an image with pluginfile.php source */ public function test_write_data(): void { $columns = ['fruit', 'colour', 'animal']; $rows = [ ['banana', 'yellow', 'monkey'], ['apple', 'red', 'wolf'], ['melon', 'green', 'aardvark'], ]; // Export to file. $exportfile = dataformat::write_data('My export', 'ods', $columns, $rows); // Read the file. $odscells = $this->get_ods_rows_content(file_get_contents($exportfile)); $this->assertEquals(array_merge([$columns], $rows), $odscells); } /** * Get ods rows from binary content * @param string $content * @return array * @throws \OpenSpout\Common\Exception\IOException * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException */ private function get_ods_rows_content($content) { $file = tempnam(sys_get_temp_dir(), 'ods_'); $handle = fopen($file, "w"); fwrite($handle, $content); /** @var \OpenSpout\Reader\ODS\Reader $reader */ $reader = \OpenSpout\Reader\Common\Creator\ReaderFactory::createFromFileByMimeType($file); $reader->open($file); /** @var \OpenSpout\Reader\ODS\Sheet[] $sheets */ $sheets = $reader->getSheetIterator(); $rowscellsvalues = []; foreach ($sheets as $sheet) { /** @var \OpenSpout\Common\Entity\Row[] $rows */ $rows = $sheet->getRowIterator(); foreach ($rows as $row) { $thisvalues = []; foreach ($row->getCells() as $cell) { $thisvalues[] = $cell->getValue(); } $rowscellsvalues[] = $thisvalues; } } return $rowscellsvalues; } } html/lang/en/dataformat_html.php000064400000002057152266350720012726 0ustar00. /** * html dataformat lang strings. * * @package dataformat_html * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['dataformat'] = 'HTML table'; $string['privacy:metadata'] = 'The HTML table data format plugin does not store any personal data.'; $string['shortname'] = 'HTML'; html/version.php000064400000002101152266350720007710 0ustar00. /** * Data activity filter version information * * @package dataformat_html * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2025041400; $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'dataformat_html'; html/classes/privacy/provider.php000064400000002764152266350720013206 0ustar00. /** * Privacy provider implementation for dataformat_html. * * @package dataformat_html * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_html\privacy; defined('MOODLE_INTERNAL') || die(); /** * Privacy provider implementation for dataformat_html. * * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements \core_privacy\local\metadata\null_provider { /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ public static function get_reason(): string { return 'privacy:metadata'; } } html/classes/writer.php000064400000006420152266350720011204 0ustar00. /** * html data format writer * * @package dataformat_html * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_html; defined('MOODLE_INTERNAL') || die(); /** * html data format writer * * @package dataformat_html * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer extends \core\dataformat\base { /** @var $mimetype */ public $mimetype = "text/html"; /** @var $extension */ public $extension = ".html"; /** * Write the start of the output */ public function start_output() { echo ""; echo \html_writer::empty_tag('meta', ['charset' => 'UTF-8']); echo \html_writer::tag('title', $this->filename); echo " "; } /** * Write the start of the sheet we will be adding data to. * * @param array $columns */ public function start_sheet($columns) { echo ""; echo \html_writer::start_tag('tr'); foreach ($columns as $k => $v) { echo \html_writer::tag('th', $v); } 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); } echo \html_writer::end_tag('tr'); } /** * Write the end of the sheet containing the data. * * @param array $columns */ public function close_sheet($columns) { echo "
"; } /** * Write the end of the sheet containing the data. */ public function close_output() { echo ""; } } json/lang/en/dataformat_json.php000064400000002173152266350720012737 0ustar00. /** * JSON dataformat lang strings. * * @package dataformat_json * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['dataformat'] = 'Javascript Object Notation (.json)'; $string['privacy:metadata'] = 'The JavaScript Object Notation (JSON) data format plugin does not store any personal data.'; $string['shortname'] = 'JavaScript Object Notation (JSON)'; json/version.php000064400000002101152266350720007715 0ustar00. /** * Data activity filter version information * * @package dataformat_json * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2025041400; $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'dataformat_json'; json/classes/privacy/provider.php000064400000002764152266350720013213 0ustar00. /** * Privacy provider implementation for dataformat_json. * * @package dataformat_json * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_json\privacy; defined('MOODLE_INTERNAL') || die(); /** * Privacy provider implementation for dataformat_json. * * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements \core_privacy\local\metadata\null_provider { /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ public static function get_reason(): string { return 'privacy:metadata'; } } json/classes/writer.php000064400000005600152266350720011210 0ustar00. /** * JSON data format writer * * @package dataformat_json * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_json; use core_text; /** * JSON data format writer * * @package dataformat_json * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer extends \core\dataformat\base { /** @var $mimetype */ public $mimetype = "application/json"; /** @var $extension */ public $extension = ".json"; /** @var $sheetstarted */ public $sheetstarted = false; /** @var $sheetdatadded */ public $sheetdatadded = false; /** @var string[] $columns */ protected $columns = []; /** * Write the start of the file. */ public function start_output() { echo "["; } /** * Write the start of the sheet we will be adding data to. * * @param array $columns */ public function start_sheet($columns) { $this->columns = array_map(function($column) { return core_text::strtolower(clean_param($column, PARAM_ALPHANUMEXT)); }, $columns); if ($this->sheetstarted) { echo ","; } else { $this->sheetstarted = true; } $this->sheetdatadded = false; echo "["; } /** * Write a single record * * @param array $record * @param int $rownum */ public function write_record($record, $rownum) { if ($this->sheetdatadded) { echo ","; } // Ensure our record is keyed by column names, rather than numerically. $record = array_combine($this->columns, (array) $record); echo json_encode($this->format_record($record), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); $this->sheetdatadded = true; } /** * Write the end of the sheet containing the data. * * @param array $columns */ public function close_sheet($columns) { echo "]"; } /** * Write the end of the file. */ public function close_output() { echo "]"; } } csv/lang/en/dataformat_csv.php000064400000002070152266350720012377 0ustar00. /** * CSV dataformat lang strings. * * @package dataformat_csv * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['dataformat'] = 'Comma separated values (.csv)'; $string['privacy:metadata'] = 'The CSV data format plugin does not store any personal data.'; $string['shortname'] = 'CSV'; csv/version.php000064400000002100152266350720007536 0ustar00. /** * Data activity filter version information * * @package dataformat_csv * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); $plugin->version = 2025041400; $plugin->requires = 2025040800; // Requires this Moodle version. $plugin->component = 'dataformat_csv'; csv/classes/privacy/provider.php000064400000002760152266350720013031 0ustar00. /** * Privacy provider implementation for dataformat_csv. * * @package dataformat_csv * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_csv\privacy; defined('MOODLE_INTERNAL') || die(); /** * Privacy provider implementation for dataformat_csv. * * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class provider implements \core_privacy\local\metadata\null_provider { /** * Get the language string identifier with the component's language * file to explain why this plugin stores no data. * * @return string */ public static function get_reason(): string { return 'privacy:metadata'; } } csv/classes/writer.php000064400000002476152266350720011042 0ustar00. /** * CSV data format writer * * @package dataformat_csv * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace dataformat_csv; defined('MOODLE_INTERNAL') || die(); /** * CSV data format writer * * @package dataformat_csv * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class writer extends \core\dataformat\spout_base { /** @var $mimetype */ protected $mimetype = "text/csv"; /** @var $extension */ protected $extension = ".csv"; } spout_base.php000064400000011014152267214040007421 0ustar00. /** * Common Spout class for dataformat. * * @package core * @subpackage dataformat * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace core\dataformat; use OpenSpout\Common\Entity\Row; use OpenSpout\Writer\Common\Creator\WriterFactory; /** * Common Spout class for dataformat. * * @package core * @subpackage dataformat * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ abstract class spout_base extends \core\dataformat\base { /** @var $writer */ protected $writer; /** @var $sheettitle */ protected $sheettitle; /** @var $renamecurrentsheet */ protected $renamecurrentsheet = false; /** * Output file headers to initialise the download of the file. */ public function send_http_headers() { $filename = $this->filename . $this->get_extension(); $this->writer = WriterFactory::createFromFile($filename); if (method_exists($this->writer->getOptions(), 'setTempFolder')) { $this->writer->getOptions()->setTempFolder(make_request_directory()); } if (PHPUNIT_TEST) { $this->writer->openToFile('php://output'); } else { $this->writer->openToBrowser($filename); } // By default one sheet is always created, but we want to rename it when we call start_sheet(). $this->renamecurrentsheet = true; } /** * Set the dataformat to be output to current file */ public function start_output_to_file(): void { $this->writer = WriterFactory::createFromFile($this->filepath); if (method_exists($this->writer->getOptions(), 'setTempFolder')) { $this->writer->getOptions()->setTempFolder(make_request_directory()); } $this->writer->openToFile($this->filepath); // By default one sheet is always created, but we want to rename it when we call start_sheet(). $this->renamecurrentsheet = true; $this->start_output(); } /** * Set the title of the worksheet inside a spreadsheet * * For some formats this will be ignored. * * @param string $title */ public function set_sheettitle($title) { $this->sheettitle = $title; } /** * Write the start of the sheet we will be adding data to. * * @param array $columns */ public function start_sheet($columns) { if ($this->sheettitle && $this->writer instanceof \OpenSpout\Writer\AbstractWriterMultiSheets) { if ($this->renamecurrentsheet) { $sheet = $this->writer->getCurrentSheet(); $this->renamecurrentsheet = false; } else { $sheet = $this->writer->addNewSheetAndMakeItCurrent(); } $sheet->setName($this->sheettitle); } // Create a row with cells and apply the style to all cells. $row = Row::fromValues((array)$columns); $this->writer->addRow($row); } /** * Write a single record * * @param array $record * @param int $rownum */ public function write_record($record, $rownum) { $rowvalues = $this->format_record($record); foreach ($rowvalues as $key => $value) { $rowvalues[$key] = \core\dataformat::escape_spreadsheet_formula($value); } $row = Row::fromValues($rowvalues); $this->writer->addRow($row); } /** * Write the end of the file. */ public function close_output() { $this->writer->close(); $this->writer = null; } /** * Write data to disk * * @return bool */ public function close_output_to_file(): bool { $this->close_output(); return true; } } base.php000064400000017711152267214040006201 0ustar00. /** * Base class for dataformat. * * @package core * @subpackage dataformat * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace core\dataformat; use coding_exception; /** * Base class for dataformat. * * @package core * @subpackage dataformat * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ abstract class base { /** @var $mimetype */ protected $mimetype = "text/plain"; /** @var $extension */ protected $extension = ".txt"; /** @var $filename */ protected $filename = ''; /** @var string The location to store the output content */ protected $filepath = ''; /** * Get the file extension * * @return string file extension */ public function get_extension() { return $this->extension; } /** * Set download filename base * * @param string $filename */ public function set_filename($filename) { $this->filename = $filename; } /** * Set file path when writing to file * * @param string $filepath * @throws coding_exception */ public function set_filepath(string $filepath): void { $filedir = dirname($filepath); if (!is_writable($filedir)) { throw new coding_exception('File path is not writable'); } $this->filepath = $filepath; // Some dataformat writers may expect filename to be set too. $this->set_filename(pathinfo($this->filepath, PATHINFO_FILENAME)); } /** * Set the title of the worksheet inside a spreadsheet * * For some formats this will be ignored. * * @param string $title */ public function set_sheettitle($title) { } /** * Output file headers to initialise the download of the file. */ public function send_http_headers() { if (defined('BEHAT_SITE_RUNNING') || PHPUNIT_TEST) { // For text based formats - we cannot test the output with behat if we force a file download. return; } if (is_https()) { // HTTPS sites - watch out for IE! KB812935 and KB316431. header('Cache-Control: max-age=10'); header('Pragma: '); } else { // Normal http - prevent caching at all cost. header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0'); header('Pragma: no-cache'); } header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); header("Content-Type: $this->mimetype\n"); $filename = $this->filename . $this->get_extension(); header("Content-Disposition: attachment; filename=\"$filename\""); } /** * Set the dataformat to be output to current file. Calling code must call {@see base::close_output_to_file()} when finished */ public function start_output_to_file(): void { // Raise memory limit to ensure we can store the entire content. Start collecting output. raise_memory_limit(MEMORY_EXTRA); ob_start(); $this->start_output(); } /** * Write the start of the file. */ public function start_output() { // Override me if needed. } /** * Write the start of the sheet we will be adding data to. * * @param array $columns */ 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('/(?
]+src=")(?[^"]*)(?".*>)/i', function(array $matches) {
            $source = $matches['source'];

            // Now check if the image source looks like a pluginfile URL.
            if (preg_match('/pluginfile.php\/(?\d+)\/(?[^\/]+)\/(?[^\/]+)\/(?:(?\d+)\/)?' .
                    '(?.*)/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
     *
     * @param array $record
     * @param int $rownum
     */
    abstract public function write_record($record, $rownum);

    /**
     * Write the end of the sheet containing the data.
     *
     * @param array $columns
     */
    public function close_sheet($columns) {
        // Override me if needed.
    }

    /**
     * Write the end of the file.
     */
    public function close_output() {
        // Override me if needed.
    }

    /**
     * Write the data to disk. Calling code should have previously called {@see base::start_output_to_file()}
     *
     * @return bool Whether the write succeeded
     */
    public function close_output_to_file(): bool {
        $this->close_output();

        $filecontent = ob_get_contents();
        ob_end_clean();

        return file_put_contents($this->filepath, $filecontent) !== false;
    }
}