Skip to content

Commit

Permalink
Merge pull request #203 from mvaraujo/slk-export
Browse files Browse the repository at this point in the history
Provide support for SYLK export #200
  • Loading branch information
jleyva authored Oct 3, 2024
2 parents 6be60b5 + 1d42109 commit 4fa58a5
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
5 changes: 3 additions & 2 deletions export/json/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
*/
function export_report($report) {
$table = $report->table;
$report_name = format_string($report->name) ?? 'report';
$filename = $report_name.'_'.(time()).'.json';

$reportname = format_string($report->name) ?? 'report';
$filename = $reportname . '_' . (time()) . '.json';
$json = [];
$headers = $table->head;
foreach ($table->data as $data) {
Expand Down
105 changes: 105 additions & 0 deletions export/slk/export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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/>.

/**
* Configurable Reports
* A Moodle block for creating customizable reports
* @package blocks
* @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
* @date: 2009
*/

function export_report($report)
{
$filename = $report->name ?? 'report';
$table = $report->table;

set_header($filename);

echo "ID;P\n";

if (!empty($table->head)) {
$head = [];

foreach ($table->head as $title) {
$head[] = $title;
}

dump_slk_row($head);
}

foreach ($table->data as $row) {
dump_slk_row($row);
}

echo "E\n";

exit;
}

function set_header($filename)
{
global $CFG;

require_once("$CFG->libdir/moodlelib.php");

$gmdate = gmdate("Ymd_Hi");
$filename = clean_filename("$filename-$gmdate.slk");

if (strpos($CFG->wwwroot, 'https://') === 0) { // 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: application/download; charset=iso-8859-1");
header("Content-Disposition: attachment; filename=\"$filename\"");
}

$row = 1;

function dump_slk_row($data)
{
// Refer to https://en.wikipedia.org/wiki/Symbolic_Link_(SYLK)

global $row;
$col = 1;

foreach ($data as $datum) {
$datum =
htmlspecialchars_decode(
strip_tags(
nl2br(
str_replace('"', "'", $datum)
)
)
);

if (preg_match('!!u', $datum)) {
// https://stackoverflow.com/questions/4407854/how-do-i-detect-if-have-to-apply-utf-8-decode-or-encode-on-a-string

$datum = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $datum);
}

echo "C;Y$row;X$col;K\"$datum\"\n";

++$col;
}

++$row;
}
Binary file added export/slk/pix.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lang/en/block_configurable_reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
$string['enablejspagination'] = "Enable JavaScript Pagination";
$string['export_csv'] = "Export in CSV format";
$string['export_ods'] = "Export in ODS format";
$string['export_slk'] = "Export in SYLK format";
$string['export_xls'] = "Export in XLS format";
$string['export_json'] = "Export in JSON format";
$string['viewreport'] = "View report";
Expand Down
1 change: 1 addition & 0 deletions report.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ public function create_report(): bool {
if (!$this->finalreport) {
$this->finalreport = new stdClass;
}
$this->finalreport->name = $this->config->name;
$this->finalreport->table = $table;
$this->finalreport->calcs = $calcs;

Expand Down
1 change: 1 addition & 0 deletions reports/sql/report.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function create_report(): bool {
if (!$this->finalreport) {
$this->finalreport = new stdClass;
}
$this->finalreport->name = $this->config->name;
$this->finalreport->table = $table;
$this->finalreport->calcs = $calcs;

Expand Down

0 comments on commit 4fa58a5

Please sign in to comment.