Skip to content

Commit

Permalink
Ad-hoc DB report: Add unit tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalits authored and timhunt committed Feb 18, 2022
1 parent 0b1c953 commit ee3e49d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
19 changes: 19 additions & 0 deletions internaldoc/functionality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ When the table is displayed, any value that looks like a URL is displayed as a l

If a column has a name ending with the four characters 'date', and contains an
integer value, then that is assumed to be a unix time-stamp, and formatted nicely.


Download report via Webservice
------------------------------

Any report that is added, can be downloaded via the standard Moodle webservice.

Example webservice url will be as follows:
http://<domainname>/webservice/pluginfile.php/1/report_customsql/download/<reportid>?token=<wstoken>&dataformat=csv

Required url parameters:
token - the webservice token
dataformat - the dataformat the report will be downloaded in

Optional url parameters:
timestamp - the timestamp of a previously run scheduled report

any arbitrary number of custom parameter=value pair as defined in the report. E.g.
http://<domainname>/webservice/pluginfile.php/1/report_customsql/download/<reportid>?token=<wstoken>&dataformat=csv&username=admin&foo=bar
8 changes: 7 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
defined('MOODLE_INTERNAL') || die();

/**
* File download support for customsql.
* Called by pluginfile, to download user generated reports via selected dataformat.
* Generated reports can also be downloaded via webservice/pluginfile.
*
* Example url for download:
* /pluginfile/<contextid>/report_customsql/download/<reportid>/?dataformat=csv&parameter1=value1&parameter2=value2
* Example url for download via WS:
* /webservice/pluginfile/<contextid>/report_customsql/download/<reportid>/?token=<wstoken>&dataformat=csv&parameter1=value1&parameter2=value2
*
* Exits if the required permissions are not satisfied.
*
Expand Down
24 changes: 24 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ function report_customsql_url($relativeurl, $params = []) {
return new moodle_url('/report/customsql/' . $relativeurl, $params);
}

/**
* Create the download url for the report.
*
* @param int $reportid The reportid.
* @param array $params Parameters for the url.
*
* @return moodle_url The download url.
*/
function report_customsql_downloadurl($reportid, $params = []) {
$downloadurl = moodle_url::make_pluginfile_url(
context_system::instance()->id,
'report_customsql',
'download',
$reportid,
null,
null
);
// Add the params to the url.
// Used to pass values for the arbitrary number of params in the sql report.
$downloadurl->params($params);

return $downloadurl;
}

function report_customsql_capability_options() {
return array(
'report/customsql:view' => get_string('anyonewhocanveiwthisreport', 'report_customsql'),
Expand Down
40 changes: 40 additions & 0 deletions tests/report_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,46 @@ public function test_report_customsql_email_report() {
$sink->close();
}

/**
* Test plugin downloading of reports.
*
* @return void
*/
public function test_report_custom_sql_download_report_url() {
global $CFG, $DB;

$this->resetAfterTest(true);

$user = $this->getDataGenerator()->create_user();

$id = $this->create_a_database_row('daily', 2, 1, $user->id);
$report = $DB->get_record('report_customsql_queries', ['id' => $id]);

// Give our test user the capability to view the report.
$userrole = $DB->get_record('role', ['shortname' => 'user']);
role_change_permission($userrole->id, context_system::instance(), $report->capability, CAP_ALLOW);

// Test download url with the required dataformat param.
$urlparams = [
'dataformat' => 'csv'
];
$baseurl = "https://www.example.com/moodle/pluginfile.php";
$path = "/1/report_customsql/download/";

$url = report_customsql_downloadurl($report->id, $urlparams);
$expected = "{$baseurl}{$path}{$report->id}?dataformat=csv";
$this->assertEquals($expected, $url->out(false));

// Add some custom parameters to the params.
$timenow = time();
$urlparams['timestamp'] = $timenow;
$urlparams['foo'] = 'bar';

$url = report_customsql_downloadurl($report->id, $urlparams);
$expected = "{$baseurl}{$path}{$report->id}?dataformat=csv&timestamp={$timenow}&foo=bar";
$this->assertEquals($expected, $url->out(false));
}

public function test_report_customsql_write_csv_row() {
global $CFG;
$this->resetAfterTest();
Expand Down
4 changes: 2 additions & 2 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@
$urlparams = $paramvalues;
}
$urlparams['timestamp'] = $csvtimestamp;
$downloadurl = new moodle_url('/pluginfile.php/1/report_customsql/download/' . $id, $urlparams);
$downloadurl = report_customsql_downloadurl($id, $urlparams);
echo $OUTPUT->download_dataformat_selector(get_string('downloadthisreportas', 'report_customsql'),
$downloadurl->out(), 'dataformat', $urlparams);
$downloadurl, 'dataformat', $urlparams);
}
}

Expand Down

0 comments on commit ee3e49d

Please sign in to comment.