From c167510cee341f148d9117f2cfd118a84c8717ea Mon Sep 17 00:00:00 2001 From: Marcus Vinicius Pompeu Date: Sun, 9 Jan 2022 06:11:21 -0300 Subject: [PATCH 1/2] Exports could refer report name instead of 'report' #201 --- export/csv/export.php | 2 +- export/json/export.php | 3 ++- export/ods/export.php | 3 ++- export/xls/export.php | 3 ++- report.class.php | 1 + reports/sql/report.class.php | 1 + 6 files changed, 9 insertions(+), 4 deletions(-) diff --git a/export/csv/export.php b/export/csv/export.php index eacb87d5..66d134a9 100755 --- a/export/csv/export.php +++ b/export/csv/export.php @@ -28,7 +28,7 @@ function export_report($report) { $table = $report->table; $matrix = array(); - $filename = 'report'; + $filename = $report->name ?? 'report'; if (!empty($table->head)) { $countcols = count($table->head); diff --git a/export/json/export.php b/export/json/export.php index fcd0e944..7a343725 100644 --- a/export/json/export.php +++ b/export/json/export.php @@ -24,7 +24,8 @@ */ function export_report($report){ $table = $report->table; - $filename = 'report_' . (time()) . '.json'; + $report_name = $report->name ?? 'report'; + $filename = $report_name.'_'.(time()).'.json'; $json = []; $headers = $table->head; foreach ($table->data as $data) { diff --git a/export/ods/export.php b/export/ods/export.php index 9886cf58..5f672d94 100755 --- a/export/ods/export.php +++ b/export/ods/export.php @@ -28,7 +28,8 @@ function export_report($report) { $table = $report->table; $matrix = array(); - $filename = 'report_'.(time()).'.ods'; + $report_name = $report->name ?? 'report'; + $filename = $report_name.'_'.(time()).'.ods'; if (!empty($table->head)) { $countcols = count($table->head); diff --git a/export/xls/export.php b/export/xls/export.php index f362d8b3..6b64c0ab 100755 --- a/export/xls/export.php +++ b/export/xls/export.php @@ -28,7 +28,8 @@ function export_report($report) { $table = $report->table; $matrix = array(); - $filename = 'report_'.(time()).'.xls'; + $report_name = $report->name ?? 'report'; + $filename = $report_name.'_'.(time()).'.xlsx'; if (!empty($table->head)) { $countcols = count($table->head); diff --git a/report.class.php b/report.class.php index 5b0323f7..2a6cbe8a 100755 --- a/report.class.php +++ b/report.class.php @@ -542,6 +542,7 @@ public function create_report() { if (!$this->finalreport) { $this->finalreport = new \stdClass; } + $this->finalreport->name = $this->config->name; $this->finalreport->table = $table; $this->finalreport->calcs = $calcs; diff --git a/reports/sql/report.class.php b/reports/sql/report.class.php index 8de6ea1e..5b0803ad 100644 --- a/reports/sql/report.class.php +++ b/reports/sql/report.class.php @@ -168,6 +168,7 @@ public function create_report() { if (!$this->finalreport) { $this->finalreport = new \stdClass; } + $this->finalreport->name = $this->config->name; $this->finalreport->table = $table; $this->finalreport->calcs = $calcs; From a38716439ee86f6217117e141ad79f06e034555a Mon Sep 17 00:00:00 2001 From: Marcus Vinicius Pompeu Date: Sun, 9 Jan 2022 06:04:24 -0300 Subject: [PATCH 2/2] Provide support for SYLK export #200 --- export/slk/export.php | 105 +++++++++++++++++++++++++ export/slk/pix.gif | Bin 0 -> 118 bytes lang/en/block_configurable_reports.php | 1 + 3 files changed, 106 insertions(+) create mode 100755 export/slk/export.php create mode 100644 export/slk/pix.gif diff --git a/export/slk/export.php b/export/slk/export.php new file mode 100755 index 00000000..a652f0fc --- /dev/null +++ b/export/slk/export.php @@ -0,0 +1,105 @@ +. + +/** + * Configurable Reports + * A Moodle block for creating customizable reports + * @package blocks + * @author: Juan leyva + * @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; +} diff --git a/export/slk/pix.gif b/export/slk/pix.gif new file mode 100644 index 0000000000000000000000000000000000000000..8588fe54299922b6cb93292750355bf2e5e39fb9 GIT binary patch literal 118 zcmZ?wbhEHbBb)A&CaDE{a6a}5c0b_{Se(lcOYWME)W{K>+~z`)F) z17tIRv@kH4+-X>OOo3lMN!^WoR_p3_(k+iX+5!|hGFNR=vzn)4b^Js0Y5th$`yT!% RXgIWR`3y^qX`M_A)&Q#tDIEX+ literal 0 HcmV?d00001 diff --git a/lang/en/block_configurable_reports.php b/lang/en/block_configurable_reports.php index 6ea212a4..54723569 100644 --- a/lang/en/block_configurable_reports.php +++ b/lang/en/block_configurable_reports.php @@ -59,6 +59,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";