From 6ee45ac4aa65b80c399111ce61a16b4044a627a6 Mon Sep 17 00:00:00 2001 From: Kevin Pham Date: Mon, 17 May 2021 13:48:33 +1000 Subject: [PATCH] Add embed options - Add `embed` param to the viewreport link to skip unwanted sections by applying the page layout of 'embedded' - Add configuration to hide the print button from the report - Add configuration to hide the total records from the `template` report - Add embed link in configs --- db/install.xml | 2 ++ db/upgrade.php | 13 ++++++++++ editreport.php | 8 ++++++ editreport_form.php | 31 ++++++++++++++++++++++- lang/en/block_configurable_reports.php | 7 ++++++ report.class.php | 34 ++++++++++++++++---------- version.php | 2 +- viewreport.php | 7 +++++- 8 files changed, 88 insertions(+), 16 deletions(-) diff --git a/db/install.xml b/db/install.xml index 5f4454aa..74276c4d 100644 --- a/db/install.xml +++ b/db/install.xml @@ -22,6 +22,8 @@ + + diff --git a/db/upgrade.php b/db/upgrade.php index a51b5be6..f9b89752 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -144,5 +144,18 @@ function xmldb_block_configurable_reports_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2019062001, 'block', 'configurable_reports'); } + if ($oldversion < 2020110301) { + $table = new xmldb_table('block_configurable_reports'); + $field = new xmldb_field('displaytotalrecords', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1', null); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + $field = new xmldb_field('displayprintbutton', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1', null); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + upgrade_plugin_savepoint(true, 2020110301, 'block', 'configurable_reports'); + } + return true; } \ No newline at end of file diff --git a/editreport.php b/editreport.php index 750243a5..859922ad 100755 --- a/editreport.php +++ b/editreport.php @@ -218,6 +218,14 @@ $data->cron = 0; } + if (!isset($data->displaytotalrecords)) { + $data->displaytotalrecords = 0; + } + + if (!isset($data->displayprintbutton)) { + $data->displayprintbutton = 0; + } + if (empty($report)) { $data->ownerid = $USER->id; $data->courseid = $courseid; diff --git a/editreport_form.php b/editreport_form.php index a74c2324..efdc91b9 100644 --- a/editreport_form.php +++ b/editreport_form.php @@ -74,6 +74,10 @@ public function definition() { $mform->addHelpButton('jsordering', 'jsordering', 'block_configurable_reports'); $mform->setDefault('jsordering', 1); + $mform->addElement('checkbox', 'displaytotalrecords', get_string('displaytotalrecords', 'block_configurable_reports'), get_string('displaytotalrecordsdescription', 'block_configurable_reports')); + $mform->addElement('checkbox', 'displayprintbutton', get_string('displayprintbutton', 'block_configurable_reports'), get_string('displayprintbuttondescription', 'block_configurable_reports')); + + $mform->addElement('checkbox', 'cron', get_string('cron', 'block_configurable_reports'), get_string('crondescription', 'block_configurable_reports')); $mform->addHelpButton('cron', 'cron', 'block_configurable_reports'); $mform->setDefault('cron', 0); @@ -83,6 +87,25 @@ public function definition() { $mform->addHelpButton('remote', 'remote', 'block_configurable_reports'); $mform->setDefault('remote', 0); + // Adds an embed link for easy copy/paste once the report is saved. + if (isset($this->_customdata['report']->id) && $this->_customdata['report']->id) { + + $params = [ + 'id' => $this->_customdata['report']->id, + 'courseid' => $this->_customdata['courseid'], + 'embed' => true + ]; + $url = new \moodle_url('/blocks/configurable_reports/viewreport.php', $params); + + $mform->addElement('static', 'embedlink', + get_string('embedlink', 'block_configurable_reports'), + html_writer::tag('pre', $url, ['class' => 'mb-0']). + get_string('embedlinkdescription', 'block_configurable_reports') + ); + + } + + $mform->addElement('header', 'exportoptions', get_string('exportoptions', 'block_configurable_reports')); $options = cr_get_export_plugins(); @@ -102,8 +125,14 @@ public function definition() { $mform->setType('courseid', PARAM_INT); } + // Submit button string. + $submitstring = get_string('add'); + if (!empty($this->_customdata['report']->id)) { + $submitstring = get_string('update'); + } + // Buttons. - $this->add_action_buttons(true, get_string('add')); + $this->add_action_buttons(true, $submitstring); } public function validation($data, $files) { diff --git a/lang/en/block_configurable_reports.php b/lang/en/block_configurable_reports.php index 6ea212a4..0dd9cbc8 100644 --- a/lang/en/block_configurable_reports.php +++ b/lang/en/block_configurable_reports.php @@ -51,6 +51,7 @@ $string['configurable_reports:viewreports'] = "View reports"; $string['exportoptions'] = "Export options"; +$string['embedoptions'] = "Embed options"; $string['field'] = "Field"; // Report form @@ -66,6 +67,12 @@ $string['jsordering'] = 'JavaScript Ordering'; $string['cron'] = 'Auto run daily'; $string['crondescription'] = 'Schedule this query to run each day (At night)'; +$string['displaytotalrecords'] = 'Total Records'; +$string['displaytotalrecordsdescription'] = 'Displays the total number of results in the report'; +$string['displayprintbutton'] = 'Print Button'; +$string['displayprintbuttondescription'] = 'Displays the print button at the bottom of the report'; +$string['embedlink'] = 'Embed Link'; +$string['embedlinkdescription'] = 'You can copy this link to embed the report in an HTML block'; $string['cron_help'] = 'Schedule this query to run each day (At night)'; $string['remote'] = 'Run on remote db'; $string['remotedescription'] = 'Do you want to run this query on the remote db'; diff --git a/report.class.php b/report.class.php index 5b0323f7..c0c90e50 100755 --- a/report.class.php +++ b/report.class.php @@ -655,9 +655,11 @@ public function print_template($config, \moodle_page $moodle_page) { echo format_text($pagecontents['header'], FORMAT_HTML); } - $a = new \stdClass(); - $a->totalrecords = $this->totalrecords; - echo \html_writer::tag('div', get_string('totalrecords', 'block_configurable_reports', $a), array('id' => 'totalrecords')); + if ($this->config->displaytotalrecords) { + $a = new \stdClass(); + $a->totalrecords = $this->totalrecords; + echo \html_writer::tag('div', get_string('totalrecords', 'block_configurable_reports', $a), array('id' => 'totalrecords')); + } if ($recordtpl) { if ($this->config->pagination) { @@ -672,7 +674,7 @@ public function print_template($config, \moodle_page $moodle_page) { } else { $recordtext = $recordtpl; } - + foreach ($this->finalreport->table->head as $key => $c) { $recordtext = str_ireplace("[[$c]]", $r[$key], $recordtext); } @@ -688,16 +690,20 @@ public function print_template($config, \moodle_page $moodle_page) { } echo "\n"; - echo '

'; - echo $OUTPUT->pix_icon('print', get_string('printreport', 'block_configurable_reports'), 'block_configurable_reports'); - echo " ".get_string('printreport', 'block_configurable_reports').""; - echo "
\n"; + if ($this->config->displayprintbutton) { + echo '

'; + echo $OUTPUT->pix_icon('print', get_string('printreport', 'block_configurable_reports'), 'block_configurable_reports'); + echo " ".get_string('printreport', 'block_configurable_reports').""; + echo "
\n"; + } } public function print_report_page(\moodle_page $moodlepage) { global $DB, $CFG, $OUTPUT, $USER; - cr_print_js_function(); + if ($this->config->displayprintbutton) { + cr_print_js_function(); + } $components = cr_unserialize($this->config->components); $template = (isset($components['template']['config']) && $components['template']['config']->enabled && $components['template']['config']->record) ? $components['template']['config'] : false; @@ -782,10 +788,12 @@ public function print_report_page(\moodle_page $moodlepage) { echo '
'.get_string('norecordsfound', 'block_configurable_reports').'
'; } - echo '

'; - echo $OUTPUT->pix_icon('print', get_string('printreport', 'block_configurable_reports'), 'block_configurable_reports'); - echo " ".get_string('printreport', 'block_configurable_reports').""; - echo "
\n"; + if ($this->config->displayprintbutton) { + echo '

'; + echo $OUTPUT->pix_icon('print', get_string('printreport', 'block_configurable_reports'), 'block_configurable_reports'); + echo " ".get_string('printreport', 'block_configurable_reports').""; + echo "
\n"; + } } public function utf8_strrev($str) { diff --git a/version.php b/version.php index 7918476c..7843f440 100755 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020110300; // Plugin version. +$plugin->version = 2020110301; // Plugin version. $plugin->requires = 2017111300; // require Moodle version (3.4). $plugin->maturity = MATURITY_STABLE; $plugin->release = '3.9.0'; diff --git a/viewreport.php b/viewreport.php index e4f1a665..45b4aa17 100755 --- a/viewreport.php +++ b/viewreport.php @@ -29,6 +29,7 @@ $download = optional_param('download', false, PARAM_BOOL); $format = optional_param('format', '', PARAM_ALPHA); $courseid = optional_param('courseid', null, PARAM_INT); +$embed = optional_param('embed', false, PARAM_BOOL); if (!$report = $DB->get_record('block_configurable_reports', ['id' => $id])) { print_error('reportdoesnotexists', 'block_configurable_reports'); @@ -98,9 +99,13 @@ $PAGE->set_title($reportname); $PAGE->set_heading( $reportname); $PAGE->set_cacheable( true); + if ($embed) { + $PAGE->set_pagelayout('embedded'); + } echo $OUTPUT->header(); - if ($hasmanageallcap || ($hasmanageowncap && $report->ownerid == $USER->id)) { + $canmanage = ($hasmanageallcap || ($hasmanageowncap && $report->ownerid == $USER->id)); + if (!$embed && $canmanage) { $currenttab = 'viewreport'; include('tabs.php'); }