From 2ef815ff9a07f954835178f5fd051597f55f4c80 Mon Sep 17 00:00:00 2001 From: Jwalit Shah Date: Tue, 30 Nov 2021 10:52:53 +1100 Subject: [PATCH] Issue moodleou#98 - allow report results to be retrieved via WS via /webservice/pluginfile * separate download endpoint no longer needed, refactor to use new code --- download.php | 73 ------------------------------- lib.php | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ view.php | 8 +++- 3 files changed, 127 insertions(+), 74 deletions(-) delete mode 100644 download.php create mode 100644 lib.php diff --git a/download.php b/download.php deleted file mode 100644 index 5e1f622..0000000 --- a/download.php +++ /dev/null @@ -1,73 +0,0 @@ -. - -/** - * Script to download the CSV version of a SQL report. - * - * @package report_customsql - * @copyright 2009 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -require_once(dirname(__FILE__) . '/../../config.php'); -require_once(dirname(__FILE__) . '/locallib.php'); -require_once($CFG->libdir . '/filelib.php'); -require_once($CFG->libdir . '/dataformatlib.php'); - -$id = required_param('id', PARAM_INT); -$csvtimestamp = required_param('timestamp', PARAM_INT); -$dataformat = optional_param('dataformat', '', PARAM_ALPHA); - -$report = $DB->get_record('report_customsql_queries', array('id' => $id)); -if (!$report) { - print_error('invalidreportid', 'report_customsql', report_customsql_url('index.php'), $id); -} - -require_login(); -$context = context_system::instance(); -if (!empty($report->capability)) { - require_capability($report->capability, $context); -} - -list($csvfilename) = report_customsql_csv_filename($report, $csvtimestamp); - -$handle = fopen($csvfilename, 'r'); -if ($handle === false) { - print_error('unknowndownloadfile', 'report_customsql', - report_customsql_url('view.php?id=' . $id)); -} - -$fields = report_customsql_read_csv_row($handle); - -$rows = new ArrayObject([]); -while ($row = report_customsql_read_csv_row($handle)) { - $rows->append($row); -} - -fclose($handle); - -$filename = clean_filename($report->displayname); - -\core\dataformat::download_data($filename, $dataformat, $fields, $rows->getIterator(), function(array $row) use ($dataformat) { - // HTML export content will need escaping. - if (strcasecmp($dataformat, 'html') === 0) { - $row = array_map(function($cell) { - return s($cell); - }, $row); - } - - return $row; -}); diff --git a/lib.php b/lib.php new file mode 100644 index 0000000..7a305ec --- /dev/null +++ b/lib.php @@ -0,0 +1,120 @@ +. + +/** + * Report customsql functions. + * + * @package report_customsql + * @author Jwalit Shah + * @copyright 2021 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * File download support for customsql. + * + * Exits if the required permissions are not satisfied. + * + * @param stdClass $course course object + * @param stdClass $cm + * @param stdClass $context context object + * @param string $filearea file area + * @param array $args extra arguments + * @param bool $forcedownload whether or not force download + * @param array $options additional options affecting the file serving + * @return bool false if file not found, does not return if found - just send the file + */ +function report_customsql_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) { + global $CFG, $DB; + + require_once(dirname(__FILE__) . '/locallib.php'); + + if ($context->contextlevel != CONTEXT_SYSTEM) { + return false; + } + + if ($filearea != 'download') { + return false; + } + + $id = (int)array_shift($args); + $dataformat = required_param('dataformat', PARAM_ALPHA); + + $report = $DB->get_record('report_customsql_queries', array('id' => $id)); + if (!$report) { + throw new \moodle_exception('invalidreportid', 'report_customsql', + report_customsql_url('index.php'), $id); + } + + require_login(); + $context = context_system::instance(); + if (!empty($report->capability)) { + require_capability($report->capability, $context); + } + + $queryparams = report_customsql_get_query_placeholders_and_field_names($report->querysql); + // Get any query param values that are given in the URL. + $paramvalues = []; + foreach ($queryparams as $queryparam => $notused) { + $value = optional_param($queryparam, null, PARAM_RAW); + if ($value !== null && $value !== '') { + $paramvalues[$queryparam] = $value; + } + $report->queryparams = serialize($paramvalues); + } + + // Check timestamp param. + $csvtimestamp = optional_param('timestamp', null, PARAM_INT); + if ($csvtimestamp === null) { + $runtime = time(); + if ($report->runable !== 'manual') { + $runtime = $report->lastrun; + } + $csvtimestamp = \report_customsql_generate_csv($report, $runtime); + } + list($csvfilename) = report_customsql_csv_filename($report, $csvtimestamp); + + $handle = fopen($csvfilename, 'r'); + if ($handle === false) { + throw new \moodle_exception('unknowndownloadfile', 'report_customsql', + report_customsql_url('view.php?id=' . $id)); + } + + $fields = report_customsql_read_csv_row($handle); + + $rows = new ArrayObject([]); + while ($row = report_customsql_read_csv_row($handle)) { + $rows->append($row); + } + + fclose($handle); + + $filename = clean_filename($report->displayname); + + \core\dataformat::download_data($filename, $dataformat, $fields, $rows->getIterator(), function(array $row) use ($dataformat) { + // HTML export content will need escaping. + if (strcasecmp($dataformat, 'html') === 0) { + $row = array_map(function($cell) { + return s($cell); + }, $row); + } + + return $row; + }); + die; +} diff --git a/view.php b/view.php index de615e1..f0ca5fc 100644 --- a/view.php +++ b/view.php @@ -211,8 +211,14 @@ echo report_customsql_time_note($report, 'p'); + $urlparams = []; + if (!empty($paramvalues)) { + $urlparams = $paramvalues; + } + $urlparams['timestamp'] = $csvtimestamp; + $downloadurl = new moodle_url('/pluginfile.php/1/report_customsql/download/' . $id, $urlparams); echo $OUTPUT->download_dataformat_selector(get_string('downloadthisreportas', 'report_customsql'), - report_customsql_url('download.php'), 'dataformat', ['id' => $id, 'timestamp' => $csvtimestamp]); + $downloadurl->out(), 'dataformat', $urlparams); } }