Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<FIELD NAME="querysql" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The SQL to run to generate this report."/>
<FIELD NAME="queryparams" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The SQL parameters to generate this report."/>
<FIELD NAME="querylimit" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="5000" SEQUENCE="false" COMMENT="Limit the number of results returned."/>
<FIELD NAME="perpage" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="-1" SEQUENCE="false" COMMENT="Number of results displayed per page (0 = no pagination)."/>
<FIELD NAME="capability" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="The capability that a user needs to have to run this report."/>
<FIELD NAME="lastrun" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Timestamp of when this report was last run."/>
<FIELD NAME="lastexecutiontime" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Time this report took to run last time it was executed, in milliseconds."/>
Expand Down
14 changes: 14 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,19 @@ function xmldb_report_customsql_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2021111600, 'report', 'customsql');
}

if ($oldversion < 2025021400) {
// Define field perpage to be added to report_customsql_queries.
$table = new xmldb_table('report_customsql_queries');
$field = new xmldb_field('perpage', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '-1', 'querylimit');

// Conditionally launch add field perpage.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Customsql savepoint reached.
upgrade_plugin_savepoint(true, 2025021400, 'report', 'customsql');
}

return true;
}
2 changes: 2 additions & 0 deletions edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function definition() {
$mform->addRule('querylimit', get_string('requireint', 'report_customsql'),
'numeric', null, 'client');

$mform->addElement('select', 'perpage', get_string('perpage'), report_customsql_items_per_page_options(true));

$runat = [];
if ($hasparameters) {
$runat[] = $mform->createElement('select', 'runable', null, report_customsql_runable_options('manual'));
Expand Down
4 changes: 4 additions & 0 deletions lang/en/report_customsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
$string['morethanonerowreturned'] = 'More than one row was returned. This query should return one row.';
$string['nodatareturned'] = 'This query did not return any data.';
$string['noexplicitprefix'] = 'Please do to include the table name prefix <code>{$a}</code> in the SQL. Instead, put the un-prefixed table name inside <code>{}</code> characters.';
$string['nopagination'] = 'No pagination';
$string['noreportsavailable'] = 'No queries available';
$string['norowsreturned'] = 'No rows were returned. This query should return one row.';
$string['noscheduleifplaceholders'] = 'Queries containing placeholders can only be run on-demand.';
Expand All @@ -113,6 +114,8 @@
$string['notrunyet'] = 'This query has not yet been run.';
$string['onerow'] = 'The query returns one row, accumulate the results one row at a time';
$string['parametervalue'] = '{$a->name}: {$a->value}';
$string['perpagedefault'] = 'Default # results per page';
$string['perpagedefault_desc'] = 'Default number of results per page to display in results view.';
$string['pluginname'] = 'Ad-hoc database queries';
$string['privacy:metadata:reportcustomsqlqueries'] = 'Ad-hoc database queries';
$string['privacy:metadata:reportcustomsqlqueries:displayname'] = 'The name of the report as displayed in the UI';
Expand Down Expand Up @@ -175,6 +178,7 @@
$string['runquery'] = 'Run query';
$string['schedulednote'] = 'These queries are automatically run on the first day of each week or month, to report on the previous week or month. These links let you view the results that has already been accumulated.';
$string['scheduledqueries'] = 'Scheduled queries';
$string['settingsdefault'] = 'Settings default';
$string['showonlythiscategory'] = 'Show only {$a}';
$string['startofweek'] = 'Day to run weekly reports';
$string['startofweek_default'] = 'Use site calendar start of week ({$a})';
Expand Down
28 changes: 28 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
require_once($CFG->libdir . '/validateurlsyntax.php');

define('REPORT_CUSTOMSQL_LIMIT_EXCEEDED_MARKER', '-- ROW LIMIT EXCEEDED --');
/** Use settings default */
const REPORT_CUSTOMSQL_PER_PAGE_DEFAULT = -1;
/** No pagination */
const REPORT_CUSTOMSQL_PER_PAGE_NONE = 0;
/** 100 items per page */
const REPORT_CUSTOMSQL_PER_PAGE_100 = 100;
/** 500 items per page */
const REPORT_CUSTOMSQL_PER_PAGE_500 = 500;
/** 1000 items per page */
const REPORT_CUSTOMSQL_PER_PAGE_1000 = 1000;

function report_customsql_execute_query($sql, $params = null, $limitnum = null) {
global $CFG, $DB;
Expand Down Expand Up @@ -300,6 +310,24 @@ function report_customsql_downloadurl($reportid, $params = []) {
return $downloadurl;
}

/**
* Return the options for selecting the umber of rows to display per page.
*
* @param bool $adddefault Add an option to select the default.
* @return array
*/
function report_customsql_items_per_page_options(bool $adddefault = false): array {
$options = [];
if ($adddefault) {
$options[REPORT_CUSTOMSQL_PER_PAGE_DEFAULT] = get_string('settingsdefault', 'report_customsql');
}
$options[REPORT_CUSTOMSQL_PER_PAGE_NONE] = get_string('nopagination', 'report_customsql');
$options[REPORT_CUSTOMSQL_PER_PAGE_100] = get_string('showperpage', '', 100);
$options[REPORT_CUSTOMSQL_PER_PAGE_500] = get_string('showperpage', '', 500);
$options[REPORT_CUSTOMSQL_PER_PAGE_1000] = get_string('showperpage', '', 1000);
return $options;
}

function report_customsql_capability_options() {
return [
'report/customsql:view' => get_string('anyonewhocanveiwthisreport', 'report_customsql'),
Expand Down
11 changes: 11 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

defined('MOODLE_INTERNAL') || die();

require_once (__DIR__ . '/locallib.php');

if ($ADMIN->fulltree) {
// Start of week, used for the day to run weekly reports.
$days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
Expand All @@ -39,6 +41,15 @@
get_string('startofweek', 'report_customsql'),
get_string('startofweek_desc', 'report_customsql'), -1, $options));

// Default items per page.
$settings->add(new admin_setting_configselect(
'report_customsql/perpagedefault',
get_string('perpagedefault', 'report_customsql'),
get_string('perpagedefault_desc', 'report_customsql'),
REPORT_CUSTOMSQL_PER_PAGE_NONE,
report_customsql_items_per_page_options()
));

$settings->add(new admin_setting_configtext_with_maxlength('report_customsql/querylimitdefault',
get_string('querylimitdefault', 'report_customsql'),
get_string('querylimitdefault_desc', 'report_customsql'), 5000, PARAM_INT, null, 10));
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023121300;
$plugin->version = 2025021400;
$plugin->requires = 2022112800;
$plugin->component = 'report_customsql';
$plugin->maturity = MATURITY_STABLE;
Expand Down
25 changes: 24 additions & 1 deletion view.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
require_once($CFG->libdir . '/adminlib.php');

$id = required_param('id', PARAM_INT);
$currentpage = optional_param('page', 0, PARAM_INT);

$urlparams = ['id' => $id];
$report = $DB->get_record('report_customsql_queries', ['id' => $id]);
if (!$report) {
Expand Down Expand Up @@ -185,13 +187,24 @@
list($table->head, $linkcolumns) = report_customsql_get_table_headers(
report_customsql_read_csv_row($handle));

// Set up pagination.
$perpage = (int) $report->perpage;
if ($perpage === REPORT_CUSTOMSQL_PER_PAGE_DEFAULT) {
$perpage = (int) get_config('report_customsql', 'perpagedefault');
}
$nopagination = $perpage === REPORT_CUSTOMSQL_PER_PAGE_NONE;
$start = $currentpage * $perpage;
$stop = $start + $perpage;

$rowlimitexceeded = false;
while ($row = report_customsql_read_csv_row($handle)) {
$data = report_customsql_display_row($row, $linkcolumns);
if (isset($data[0]) && $data[0] === REPORT_CUSTOMSQL_LIMIT_EXCEEDED_MARKER) {
$rowlimitexceeded = true;
} else {
$table->data[] = $data;
if ($nopagination || ($count >= $start && $count < $stop)) {
$table->data[] = $data;
}
$count += 1;
}
}
Expand All @@ -203,7 +216,17 @@
}

fclose($handle);
$baseurl = report_customsql_url('view.php', $urlparams);

$pagebar = new paging_bar($count, $currentpage, $perpage, $baseurl);

if (!$nopagination) {
echo $OUTPUT->render($pagebar);
}
echo html_writer::table($table);
if (!$nopagination) {
echo $OUTPUT->render($pagebar);
}

if ($rowlimitexceeded) {
echo html_writer::tag('p', get_string('recordlimitreached', 'report_customsql',
Expand Down
Loading