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..29b1c71 100644 --- a/edit_form.php +++ b/edit_form.php @@ -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')); diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 26ad086..6744327 100644 --- a/lang/en/report_customsql.php +++ b/lang/en/report_customsql.php @@ -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 {$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 +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'; @@ -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})'; diff --git a/locallib.php b/locallib.php index 7c9a6e6..d050199 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('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'), diff --git a/settings.php b/settings.php index 06e4664..4878aaa 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/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)); 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..90b6da2 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', '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; } } @@ -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',