From 5b91dabaac2937982ae76d78a6f35eaf250e11c3 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 15 Jul 2022 12:56:20 +0100 Subject: [PATCH] MDL-75235 reportbuilder: external method to set report filter values. --- lib/db/services.php | 6 ++ .../amd/build/local/repository/filters.min.js | 20 ++--- .../amd/build/local/repository/filters.min.js.map | 2 +- reportbuilder/amd/src/local/repository/filters.js | 18 +++++ reportbuilder/classes/external/filters/set.php | 93 ++++++++++++++++++++++ reportbuilder/tests/external/filters/set_test.php | 86 ++++++++++++++++++++ reportbuilder/upgrade.txt | 6 +- version.php | 2 +- 8 files changed, 219 insertions(+), 14 deletions(-) rewrite reportbuilder/amd/build/local/repository/filters.min.js (69%) create mode 100644 reportbuilder/classes/external/filters/set.php create mode 100644 reportbuilder/tests/external/filters/set_test.php diff --git a/lib/db/services.php b/lib/db/services.php index b1a14ef8c0e..5973b9c65ab 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -2781,6 +2781,12 @@ $functions = array( 'type' => 'write', 'ajax' => true, ], + 'core_reportbuilder_set_filters' => [ + 'classname' => 'core_reportbuilder\external\filters\set', + 'description' => 'Set filter values for given report', + 'type' => 'write', + 'ajax' => true, + ], 'core_dynamic_tabs_get_content' => [ 'classname' => 'core\external\dynamic_tabs_get_content', 'description' => 'Returns the content for a dynamic tab', diff --git a/reportbuilder/amd/build/local/repository/filters.min.js b/reportbuilder/amd/build/local/repository/filters.min.js dissimilarity index 69% index ec76f23edb3..e6bc60f4771 100644 --- a/reportbuilder/amd/build/local/repository/filters.min.js +++ b/reportbuilder/amd/build/local/repository/filters.min.js @@ -1,10 +1,10 @@ -define("core_reportbuilder/local/repository/filters",["exports","core/ajax"],(function(_exports,_ajax){var obj; -/** - * Module to handle filter AJAX requests - * - * @module core_reportbuilder/local/repository/filters - * @copyright 2021 Paul Holden - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.resetFilters=_exports.reorderFilter=_exports.deleteFilter=_exports.addFilter=void 0,_ajax=(obj=_ajax)&&obj.__esModule?obj:{default:obj};_exports.resetFilters=reportId=>{const request={methodname:"core_reportbuilder_filters_reset",args:{reportid:reportId}};return _ajax.default.call([request])[0]};_exports.addFilter=(reportId,uniqueIdentifier)=>{const request={methodname:"core_reportbuilder_filters_add",args:{reportid:reportId,uniqueidentifier:uniqueIdentifier}};return _ajax.default.call([request])[0]};_exports.deleteFilter=(reportId,filterId)=>{const request={methodname:"core_reportbuilder_filters_delete",args:{reportid:reportId,filterid:filterId}};return _ajax.default.call([request])[0]};_exports.reorderFilter=(reportId,filterId,position)=>{const request={methodname:"core_reportbuilder_filters_reorder",args:{reportid:reportId,filterid:filterId,position:position}};return _ajax.default.call([request])[0]}})); - -//# sourceMappingURL=filters.min.js.map \ No newline at end of file +define("core_reportbuilder/local/repository/filters",["exports","core/ajax"],(function(_exports,_ajax){var obj; +/** + * Module to handle filter AJAX requests + * + * @module core_reportbuilder/local/repository/filters + * @copyright 2021 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.setFilters=_exports.resetFilters=_exports.reorderFilter=_exports.deleteFilter=_exports.addFilter=void 0,_ajax=(obj=_ajax)&&obj.__esModule?obj:{default:obj};_exports.resetFilters=reportId=>{const request={methodname:"core_reportbuilder_filters_reset",args:{reportid:reportId}};return _ajax.default.call([request])[0]};_exports.setFilters=(reportId,reportParameters,filterValues)=>{const request={methodname:"core_reportbuilder_set_filters",args:{reportid:reportId,parameters:reportParameters,values:filterValues}};return _ajax.default.call([request])[0]};_exports.addFilter=(reportId,uniqueIdentifier)=>{const request={methodname:"core_reportbuilder_filters_add",args:{reportid:reportId,uniqueidentifier:uniqueIdentifier}};return _ajax.default.call([request])[0]};_exports.deleteFilter=(reportId,filterId)=>{const request={methodname:"core_reportbuilder_filters_delete",args:{reportid:reportId,filterid:filterId}};return _ajax.default.call([request])[0]};_exports.reorderFilter=(reportId,filterId,position)=>{const request={methodname:"core_reportbuilder_filters_reorder",args:{reportid:reportId,filterid:filterId,position:position}};return _ajax.default.call([request])[0]}})); + +//# sourceMappingURL=filters.min.js.map \ No newline at end of file diff --git a/reportbuilder/amd/build/local/repository/filters.min.js.map b/reportbuilder/amd/build/local/repository/filters.min.js.map index 2aec8af02b5..e9ee66b1aec 100644 --- a/reportbuilder/amd/build/local/repository/filters.min.js.map +++ b/reportbuilder/amd/build/local/repository/filters.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filters.min.js","sources":["../../../src/local/repository/filters.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Module to handle filter AJAX requests\n *\n * @module core_reportbuilder/local/repository/filters\n * @copyright 2021 Paul Holden \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\n\n/**\n * Reset all filters for given report\n *\n * @method\n * @param {Number} reportId\n * @return {Promise}\n */\nexport const resetFilters = reportId => {\n const request = {\n methodname: 'core_reportbuilder_filters_reset',\n args: {reportid: reportId}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Add a filter to the given report\n *\n * @param {Number} reportId\n * @param {String} uniqueIdentifier\n * @return {Promise}\n */\nexport const addFilter = (reportId, uniqueIdentifier) => {\n const request = {\n methodname: 'core_reportbuilder_filters_add',\n args: {reportid: reportId, uniqueidentifier: uniqueIdentifier}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Remove filter from given report\n *\n * @param {Number} reportId\n * @param {Number} filterId\n * @return {Promise}\n */\nexport const deleteFilter = (reportId, filterId) => {\n const request = {\n methodname: 'core_reportbuilder_filters_delete',\n args: {reportid: reportId, filterid: filterId}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Reorder a filter in a given report\n *\n * @param {Number} reportId\n * @param {Number} filterId\n * @param {Number} position\n * @return {Promise}\n */\nexport const reorderFilter = (reportId, filterId, position) => {\n const request = {\n methodname: 'core_reportbuilder_filters_reorder',\n args: {reportid: reportId, filterid: filterId, position: position}\n };\n\n return Ajax.call([request])[0];\n};\n"],"names":["reportId","request","methodname","args","reportid","Ajax","call","uniqueIdentifier","uniqueidentifier","filterId","filterid","position"],"mappings":";;;;;;;oOAgC4BA,iBAClBC,QAAU,CACZC,WAAY,mCACZC,KAAM,CAACC,SAAUJ,kBAGdK,cAAKC,KAAK,CAACL,UAAU,uBAUP,CAACD,SAAUO,0BAC1BN,QAAU,CACZC,WAAY,iCACZC,KAAM,CAACC,SAAUJ,SAAUQ,iBAAkBD,0BAG1CF,cAAKC,KAAK,CAACL,UAAU,0BAUJ,CAACD,SAAUS,kBAC7BR,QAAU,CACZC,WAAY,oCACZC,KAAM,CAACC,SAAUJ,SAAUU,SAAUD,kBAGlCJ,cAAKC,KAAK,CAACL,UAAU,2BAWH,CAACD,SAAUS,SAAUE,kBACxCV,QAAU,CACZC,WAAY,qCACZC,KAAM,CAACC,SAAUJ,SAAUU,SAAUD,SAAUE,SAAUA,kBAGtDN,cAAKC,KAAK,CAACL,UAAU"} \ No newline at end of file +{"version":3,"file":"filters.min.js","sources":["../../../src/local/repository/filters.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Module to handle filter AJAX requests\n *\n * @module core_reportbuilder/local/repository/filters\n * @copyright 2021 Paul Holden \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\n\n/**\n * Reset all filters for given report\n *\n * @method\n * @param {Number} reportId\n * @return {Promise}\n */\nexport const resetFilters = reportId => {\n const request = {\n methodname: 'core_reportbuilder_filters_reset',\n args: {reportid: reportId}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Set filter values for given report\n *\n * @method\n * @param {Number} reportId\n * @param {String} reportParameters\n * @param {String} filterValues\n * @return {Promise}\n */\nexport const setFilters = (reportId, reportParameters, filterValues) => {\n const request = {\n methodname: 'core_reportbuilder_set_filters',\n args: {reportid: reportId, parameters: reportParameters, values: filterValues}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Add a filter to the given report\n *\n * @param {Number} reportId\n * @param {String} uniqueIdentifier\n * @return {Promise}\n */\nexport const addFilter = (reportId, uniqueIdentifier) => {\n const request = {\n methodname: 'core_reportbuilder_filters_add',\n args: {reportid: reportId, uniqueidentifier: uniqueIdentifier}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Remove filter from given report\n *\n * @param {Number} reportId\n * @param {Number} filterId\n * @return {Promise}\n */\nexport const deleteFilter = (reportId, filterId) => {\n const request = {\n methodname: 'core_reportbuilder_filters_delete',\n args: {reportid: reportId, filterid: filterId}\n };\n\n return Ajax.call([request])[0];\n};\n\n/**\n * Reorder a filter in a given report\n *\n * @param {Number} reportId\n * @param {Number} filterId\n * @param {Number} position\n * @return {Promise}\n */\nexport const reorderFilter = (reportId, filterId, position) => {\n const request = {\n methodname: 'core_reportbuilder_filters_reorder',\n args: {reportid: reportId, filterid: filterId, position: position}\n };\n\n return Ajax.call([request])[0];\n};\n"],"names":["reportId","request","methodname","args","reportid","Ajax","call","reportParameters","filterValues","parameters","values","uniqueIdentifier","uniqueidentifier","filterId","filterid","position"],"mappings":";;;;;;;wPAgC4BA,iBAClBC,QAAU,CACZC,WAAY,mCACZC,KAAM,CAACC,SAAUJ,kBAGdK,cAAKC,KAAK,CAACL,UAAU,wBAYN,CAACD,SAAUO,iBAAkBC,sBAC7CP,QAAU,CACZC,WAAY,iCACZC,KAAM,CAACC,SAAUJ,SAAUS,WAAYF,iBAAkBG,OAAQF,sBAG9DH,cAAKC,KAAK,CAACL,UAAU,uBAUP,CAACD,SAAUW,0BAC1BV,QAAU,CACZC,WAAY,iCACZC,KAAM,CAACC,SAAUJ,SAAUY,iBAAkBD,0BAG1CN,cAAKC,KAAK,CAACL,UAAU,0BAUJ,CAACD,SAAUa,kBAC7BZ,QAAU,CACZC,WAAY,oCACZC,KAAM,CAACC,SAAUJ,SAAUc,SAAUD,kBAGlCR,cAAKC,KAAK,CAACL,UAAU,2BAWH,CAACD,SAAUa,SAAUE,kBACxCd,QAAU,CACZC,WAAY,qCACZC,KAAM,CAACC,SAAUJ,SAAUc,SAAUD,SAAUE,SAAUA,kBAGtDV,cAAKC,KAAK,CAACL,UAAU"} \ No newline at end of file diff --git a/reportbuilder/amd/src/local/repository/filters.js b/reportbuilder/amd/src/local/repository/filters.js index 4af4d12b840..0a4d567bb80 100644 --- a/reportbuilder/amd/src/local/repository/filters.js +++ b/reportbuilder/amd/src/local/repository/filters.js @@ -40,6 +40,24 @@ export const resetFilters = reportId => { }; /** + * Set filter values for given report + * + * @method + * @param {Number} reportId + * @param {String} reportParameters + * @param {String} filterValues + * @return {Promise} + */ +export const setFilters = (reportId, reportParameters, filterValues) => { + const request = { + methodname: 'core_reportbuilder_set_filters', + args: {reportid: reportId, parameters: reportParameters, values: filterValues} + }; + + return Ajax.call([request])[0]; +}; + +/** * Add a filter to the given report * * @param {Number} reportId diff --git a/reportbuilder/classes/external/filters/set.php b/reportbuilder/classes/external/filters/set.php new file mode 100644 index 00000000000..eb8eb33212b --- /dev/null +++ b/reportbuilder/classes/external/filters/set.php @@ -0,0 +1,93 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\external\filters; + +use external_api; +use external_function_parameters; +use external_value; +use core_reportbuilder\manager; +use core_reportbuilder\permission; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once("{$CFG->libdir}/externallib.php"); + +/** + * External method for setting report filter values + * + * @package core_reportbuilder + * @copyright 2022 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class set extends external_api { + + /** + * External method parameters + * + * @return external_function_parameters + */ + public static function execute_parameters(): external_function_parameters { + return new external_function_parameters([ + 'reportid' => new external_value(PARAM_INT, 'Report ID'), + 'parameters' => new external_value(PARAM_RAW, 'JSON encoded report parameters', VALUE_DEFAULT, ''), + 'values' => new external_value(PARAM_RAW, 'JSON encoded filter values'), + ]); + } + + /** + * External method execution + * + * @param int $reportid + * @param string $parameters + * @param string $values + * @return bool + */ + public static function execute(int $reportid, string $parameters, string $values): bool { + [ + 'reportid' => $reportid, + 'parameters' => $parameters, + 'values' => $values, + ] = self::validate_parameters(self::execute_parameters(), [ + 'reportid' => $reportid, + 'parameters' => $parameters, + 'values' => $values, + ]); + + $report = manager::get_report_from_id($reportid, (array) json_decode($parameters)); + self::validate_context($report->get_context()); + + // System report permission is implicitly handled, we need to make sure custom report can be viewed. + $persistent = $report->get_report_persistent(); + if ($persistent->get('type') === $report::TYPE_CUSTOM_REPORT) { + permission::require_can_view_report($persistent); + } + + return $report->set_filter_values((array) json_decode($values)); + } + + /** + * External method return value + * + * @return external_value + */ + public static function execute_returns(): external_value { + return new external_value(PARAM_BOOL, 'Success'); + } +} diff --git a/reportbuilder/tests/external/filters/set_test.php b/reportbuilder/tests/external/filters/set_test.php new file mode 100644 index 00000000000..8e0a1316d49 --- /dev/null +++ b/reportbuilder/tests/external/filters/set_test.php @@ -0,0 +1,86 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\external\filters; + +use core_reportbuilder_generator; +use core_reportbuilder\manager; +use core_reportbuilder\report_access_exception; +use external_api; +use externallib_advanced_testcase; +use core_user\reportbuilder\datasource\users; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once("{$CFG->dirroot}/webservice/tests/helpers.php"); + +/** + * Unit tests external filters set class + * + * @package core_reportbuilder + * @covers \core_reportbuilder\external\filters\set + * @copyright 2022 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class set_test extends externallib_advanced_testcase { + + /** + * Text execute method + */ + public function test_execute(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + /** @var core_reportbuilder_generator $generator */ + $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); + $report = $generator->create_report(['name' => 'My report', 'source' => users::class]); + + $values = [ + 'entity:filter_operator' => 'something', + 'entity:filter_value' => 42, + ]; + + $result = set::execute($report->get('id'), '', json_encode($values)); + $result = external_api::clean_returnvalue(set::execute_returns(), $result); + + $this->assertTrue($result); + + // We should get our original filter values back. + $instance = manager::get_report_from_persistent($report); + $this->assertEquals($values, $instance->get_filter_values()); + } + + /** + * Test execute method for a user without permission to view the report + */ + public function test_execute_access_exception(): void { + $this->resetAfterTest(); + + /** @var core_reportbuilder_generator $generator */ + $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); + $report = $generator->create_report(['name' => 'My report', 'source' => users::class]); + + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + $this->expectException(report_access_exception::class); + $this->expectExceptionMessage('You cannot view this report'); + set::execute($report->get('id'), '', json_encode(['foo' => 1])); + } +} diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index 8f848d4adf9..948d5a80f69 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -3,13 +3,15 @@ Information provided here is intended especially for developers. === 4.1 === +* New external method `core_reportbuilder_set_filters` for setting report filter values (plus `setFilters` AJAX repository + export for calling from Javascript modules) * New method `set_filter_form_default` in base system report class, to override whether the default filters form should be shown for a report * 'set_default_per_page' and 'get_default_per_page' methods have been added to \local\report\base class to manage the default displayed rows per page. * Added two new methods in the datasource class: - - add_all_from_entity() to add all columns/filters/conditions from the given entity to the report at once - - add_all_from_entities() to add all columns/filters/conditions from all the entities added to the report at once + - add_all_from_entity() to add all columns/filters/conditions from the given entity to the report at once + - add_all_from_entities() to add all columns/filters/conditions from all the entities added to the report at once * New database helper methods for generating multiple unique values: `generate_aliases` and `generate_param_names` * The base aggregation `format_value` method has a `$columntype` argument in order to preserve type during aggregation. When defining column callbacks, strict typing will now be preserved in your callback methods when the column is being aggregated diff --git a/version.php b/version.php index 19a8e6dfe31..3db953621b7 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2022072200.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2022072200.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.1dev (Build: 20220722)'; // Human-friendly version name -- 2.11.4.GIT