Skip to content

Commit

Permalink
Merge branch 'MDL-75235' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jul 29, 2022
2 parents b47fcbb + 5b91dab commit 0fb9cde
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 14 deletions.
6 changes: 6 additions & 0 deletions lib/db/services.php
Expand Up @@ -2781,6 +2781,12 @@
'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',
Expand Down
2 changes: 1 addition & 1 deletion reportbuilder/amd/build/local/repository/filters.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions reportbuilder/amd/src/local/repository/filters.js
Expand Up @@ -39,6 +39,24 @@ export const resetFilters = reportId => {
return Ajax.call([request])[0];
};

/**
* 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
*
Expand Down
93 changes: 93 additions & 0 deletions reportbuilder/classes/external/filters/set.php
@@ -0,0 +1,93 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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 <paulh@moodle.com>
* @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');
}
}
10 changes: 5 additions & 5 deletions reportbuilder/classes/external/system_report_exporter.php
Expand Up @@ -21,9 +21,9 @@
use core\external\persistent_exporter;
use core_table\local\filter\integer_filter;
use core_table\local\filter\string_filter;
use core_reportbuilder\system_report;
use core_reportbuilder\form\filter;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\report\base;
use core_reportbuilder\table\system_report_table;
use core_reportbuilder\table\system_report_table_filterset;
use renderer_base;
Expand Down Expand Up @@ -53,7 +53,7 @@ protected static function define_class(): string {
*/
protected static function define_related(): array {
return [
'source' => base::class,
'source' => system_report::class,
'parameters' => 'string',
];
}
Expand Down Expand Up @@ -85,7 +85,7 @@ protected static function define_other_properties(): array {
* @return array
*/
protected function get_other_values(renderer_base $output): array {
/** @var base $source */
/** @var system_report $source */
$source = $this->related['source'];

/** @var string $parameters */
Expand All @@ -102,8 +102,8 @@ protected function get_other_values(renderer_base $output): array {
$table = system_report_table::create($reportid, (array) json_decode($parameters, true));
$table->set_filterset($filterset);

// Generate filters form if report contains any filters.
$filterspresent = !empty($source->get_active_filters());
// Generate filters form if report uses the default form, and contains any filters.
$filterspresent = $source->get_filter_form_default() && !empty($source->get_active_filters());
if ($filterspresent) {
$filtersform = new filter(null, null, 'post', '', [], true, [
'reportid' => $reportid,
Expand Down
8 changes: 4 additions & 4 deletions reportbuilder/classes/output/system_report.php
Expand Up @@ -22,9 +22,9 @@
use renderer_base;
use stdClass;
use templatable;
use core_reportbuilder\system_report as system_report_base;
use core_reportbuilder\external\system_report_exporter;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\report\base;

/**
* System report output class
Expand All @@ -38,7 +38,7 @@ class system_report implements renderable, templatable {
/** @var report $report */
protected $report;

/** @var base $source */
/** @var system_report_base $source */
protected $source;

/** @var array $parameters */
Expand All @@ -48,10 +48,10 @@ class system_report implements renderable, templatable {
* Class constructor
*
* @param report $report
* @param base $source
* @param system_report_base $source
* @param array $parameters
*/
public function __construct(report $report, base $source, array $parameters) {
public function __construct(report $report, system_report_base $source, array $parameters) {
$this->report = $report;
$this->source = $source;
$this->parameters = $parameters;
Expand Down
22 changes: 22 additions & 0 deletions reportbuilder/classes/system_report.php
Expand Up @@ -40,6 +40,9 @@ abstract class system_report extends base {
/** @var string[] $basefields List of base fields */
private $basefields = [];

/** @var bool $filterformdefault Whether to use the default filters form */
private $filterformdefault = true;

/** @var action[] $actions */
private $actions = [];

Expand Down Expand Up @@ -119,6 +122,25 @@ final public function get_base_fields(): array {
return $this->basefields;
}

/**
* Override whether to use the default system report filters form, for instance this can be disabled if the UI requires
* it's own custom filter management form for a specific report
*
* @param bool $filterformdefault
*/
final public function set_filter_form_default(bool $filterformdefault = true): void {
$this->filterformdefault = $filterformdefault;
}

/**
* Whether to use the default filters form
*
* @return bool
*/
final public function get_filter_form_default(): bool {
return $this->filterformdefault;
}

/**
* Adds an action to the report
*
Expand Down

0 comments on commit 0fb9cde

Please sign in to comment.