From 2901e45aaea86dbb70bc48677d9269e5975aab66 Mon Sep 17 00:00:00 2001 From: Jason Den Dulk Date: Mon, 17 Feb 2025 11:26:59 +1100 Subject: [PATCH 1/2] Issue #116: Add pagination to results display page. --- db/install.xml | 1 + db/upgrade.php | 14 ++++++++++++++ edit_form.php | 3 +++ lang/en/report_customsql.php | 7 +++++++ locallib.php | 28 ++++++++++++++++++++++++++++ settings.php | 11 +++++++++++ version.php | 2 +- view.php | 25 ++++++++++++++++++++++++- 8 files changed, 89 insertions(+), 2 deletions(-) diff --git a/db/install.xml b/db/install.xml index de5c328..785d9e3 100644 --- a/db/install.xml +++ b/db/install.xml @@ -13,6 +13,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index db09dd2..e40e475 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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; } diff --git a/edit_form.php b/edit_form.php index a0f9f5c..589c806 100644 --- a/edit_form.php +++ b/edit_form.php @@ -98,6 +98,9 @@ public function definition() { $mform->addRule('querylimit', get_string('requireint', 'report_customsql'), 'numeric', null, 'client'); + $mform->addElement('select', 'perpage', get_string('perpage', 'report_customsql'), report_customsql_items_per_page_options(true)); + $mform->addHelpButton('perpage', 'perpage', 'report_customsql'); + $runat = []; if ($hasparameters) { $runat[] = $mform->createElement('select', 'runable', null, report_customsql_runable_options('manual')); diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 26ad086..9de18d7 100644 --- a/lang/en/report_customsql.php +++ b/lang/en/report_customsql.php @@ -54,6 +54,8 @@ $string['dailyheader'] = 'Daily'; $string['dailyheader_help'] = 'These queries are automatically run every day at the specified time. These links let you view the results that has already been accumulated.'; $string['defaultcategory'] = 'Miscellaneous'; +$string['defaultperpage'] = 'Default # results per page'; +$string['defaultperpage_desc'] = 'Default number od results per page to display in results view.'; $string['delete'] = 'Delete'; $string['deleteareyousure'] = 'Are you sure you want to delete this query?'; $string['deletecategoryareyousure'] = '

Are you sure you want to delete this category?

It cannot contain any queries.

'; @@ -90,6 +92,7 @@ $string['errordeletingreport'] = 'Error deleting a query.'; $string['errorinsertingreport'] = 'Error inserting a query.'; $string['errorupdatingreport'] = 'Error updating a query.'; +$string['itemsperpage'] = '{$a} per page'; $string['invalidreportid'] = 'Invalid query id {$a}.'; $string['lastexecuted'] = 'This query was last run on {$a->lastrun}. It took {$a->lastexecutiontime}s to run.'; $string['messageprovider:notification'] = 'Ad-hoc database query notifications'; @@ -103,6 +106,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 {$a} in the SQL. Instead, put the un-prefixed table name inside {} 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.'; @@ -113,6 +117,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['perpage'] = 'Number of results per page'; +$string['perpage_help'] = 'The maximum number of results to display per page (does not effect downloads)'; $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'; @@ -175,6 +181,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})'; diff --git a/locallib.php b/locallib.php index 7c9a6e6..9a60a8a 100644 --- a/locallib.php +++ b/locallib.php @@ -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; @@ -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('itemsperpage', 'report_customsql', 100); + $options[REPORT_CUSTOMSQL_PER_PAGE_500] = get_string('itemsperpage', 'report_customsql', 500); + $options[REPORT_CUSTOMSQL_PER_PAGE_1000] = get_string('itemsperpage', 'report_customsql', 1000); + return $options; +} + function report_customsql_capability_options() { return [ 'report/customsql:view' => get_string('anyonewhocanveiwthisreport', 'report_customsql'), diff --git a/settings.php b/settings.php index 06e4664..aafe525 100644 --- a/settings.php +++ b/settings.php @@ -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']; @@ -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/defaultperpage', + get_string('defaultperpage', 'report_customsql'), + get_string('defaultperpage_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)); diff --git a/version.php b/version.php index d2a47c9..cc6360b 100644 --- a/version.php +++ b/version.php @@ -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; diff --git a/view.php b/view.php index 2c23af6..c8bc695 100644 --- a/view.php +++ b/view.php @@ -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) { @@ -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', 'defaultperpage'); + } + $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; } } @@ -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', From d5990d509795b0f86b54e8a028e577a81f2624f1 Mon Sep 17 00:00:00 2001 From: Jason Den Dulk Date: Mon, 17 Feb 2025 14:20:59 +1100 Subject: [PATCH 2/2] Fixes for comments --- edit_form.php | 3 +-- lang/en/report_customsql.php | 7 ++----- locallib.php | 6 +++--- settings.php | 6 +++--- view.php | 2 +- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/edit_form.php b/edit_form.php index 589c806..29b1c71 100644 --- a/edit_form.php +++ b/edit_form.php @@ -98,8 +98,7 @@ public function definition() { $mform->addRule('querylimit', get_string('requireint', 'report_customsql'), 'numeric', null, 'client'); - $mform->addElement('select', 'perpage', get_string('perpage', 'report_customsql'), report_customsql_items_per_page_options(true)); - $mform->addHelpButton('perpage', 'perpage', 'report_customsql'); + $mform->addElement('select', 'perpage', get_string('perpage'), report_customsql_items_per_page_options(true)); $runat = []; if ($hasparameters) { diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 9de18d7..6744327 100644 --- a/lang/en/report_customsql.php +++ b/lang/en/report_customsql.php @@ -54,8 +54,6 @@ $string['dailyheader'] = 'Daily'; $string['dailyheader_help'] = 'These queries are automatically run every day at the specified time. These links let you view the results that has already been accumulated.'; $string['defaultcategory'] = 'Miscellaneous'; -$string['defaultperpage'] = 'Default # results per page'; -$string['defaultperpage_desc'] = 'Default number od results per page to display in results view.'; $string['delete'] = 'Delete'; $string['deleteareyousure'] = 'Are you sure you want to delete this query?'; $string['deletecategoryareyousure'] = '

Are you sure you want to delete this category?

It cannot contain any queries.

'; @@ -92,7 +90,6 @@ $string['errordeletingreport'] = 'Error deleting a query.'; $string['errorinsertingreport'] = 'Error inserting a query.'; $string['errorupdatingreport'] = 'Error updating a query.'; -$string['itemsperpage'] = '{$a} per page'; $string['invalidreportid'] = 'Invalid query id {$a}.'; $string['lastexecuted'] = 'This query was last run on {$a->lastrun}. It took {$a->lastexecutiontime}s to run.'; $string['messageprovider:notification'] = 'Ad-hoc database query notifications'; @@ -117,8 +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['perpage'] = 'Number of results per page'; -$string['perpage_help'] = 'The maximum number of results to display per page (does not effect downloads)'; +$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'; diff --git a/locallib.php b/locallib.php index 9a60a8a..d050199 100644 --- a/locallib.php +++ b/locallib.php @@ -322,9 +322,9 @@ function report_customsql_items_per_page_options(bool $adddefault = false): arra $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('itemsperpage', 'report_customsql', 100); - $options[REPORT_CUSTOMSQL_PER_PAGE_500] = get_string('itemsperpage', 'report_customsql', 500); - $options[REPORT_CUSTOMSQL_PER_PAGE_1000] = get_string('itemsperpage', 'report_customsql', 1000); + $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; } diff --git a/settings.php b/settings.php index aafe525..4878aaa 100644 --- a/settings.php +++ b/settings.php @@ -43,9 +43,9 @@ // Default items per page. $settings->add(new admin_setting_configselect( - 'report_customsql/defaultperpage', - get_string('defaultperpage', 'report_customsql'), - get_string('defaultperpage_desc', 'report_customsql'), + '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() )); diff --git a/view.php b/view.php index c8bc695..90b6da2 100644 --- a/view.php +++ b/view.php @@ -190,7 +190,7 @@ // Set up pagination. $perpage = (int) $report->perpage; if ($perpage === REPORT_CUSTOMSQL_PER_PAGE_DEFAULT) { - $perpage = (int) get_config('report_customsql', 'defaultperpage'); + $perpage = (int) get_config('report_customsql', 'perpagedefault'); } $nopagination = $perpage === REPORT_CUSTOMSQL_PER_PAGE_NONE; $start = $currentpage * $perpage;