Skip to content

Commit

Permalink
MDL-81065 core_filters: New WS core_filters_get_all_states
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Mar 7, 2024
1 parent b2fa19f commit f693e84
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
108 changes: 108 additions & 0 deletions filter/classes/external/get_all_states.php
@@ -0,0 +1,108 @@
<?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/>.

namespace core_filters\external;

use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_multiple_structure;
use core_external\external_value;
use core_external\external_warnings;
use context;

/**
* External function for getting all filter states.
*
* @package core_filters
* @copyright 2024 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.4
*/
class get_all_states extends external_api {

/**
* Webservice parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([]);
}


/**
* Main method of the external function.
*
* @return array containing all filter states.
*/
public static function execute(): array {
global $CFG;
require_once($CFG->libdir . '/filterlib.php');

$system = \context_system::instance();
external_api::validate_context($system);

$filterstates = $warnings = [];
$states = filter_get_all_states();

foreach ($states as $state) {
$context = context::instance_by_id($state->contextid);
$classname = \core\context_helper::parse_external_level($context->contextlevel);

$filterstates[] = [
'contextlevel' => $classname::get_short_name(),
'instanceid' => $context->instanceid,
'contextid' => $state->contextid,
'filter' => $state->filter,
'state' => $state->active,
'sortorder' => $state->sortorder,
];
}

return [
'filters' => $filterstates,
'warnings' => $warnings,
];
}

/**
* Webservice returns.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure(
[
'filters' => new external_multiple_structure(
new external_single_structure(
[
'contextlevel' => new external_value(PARAM_ALPHA, 'The context level where the filters are:
(coursecat, course, module).'),
'instanceid' => new external_value(PARAM_INT, 'The instance id of item associated with the context.'),
'contextid' => new external_value(PARAM_INT, 'The context id.'),
'filter' => new external_value(PARAM_PLUGIN, 'Filter plugin name.'),
'state' => new external_value(PARAM_INT, 'Filter state: 1 for on, -1 for off, -9999 if disabled.'),
'sortorder' => new external_value(PARAM_INT, 'Execution order.'),
]
),
'All filters states'
),
'warnings' => new external_warnings(),
]
);
}
}
53 changes: 53 additions & 0 deletions filter/tests/external/external_test.php
Expand Up @@ -202,4 +202,57 @@ public function test_get_available_in_context_module() {
$this->assertEquals('context', $result['warnings'][0]['item']);
$this->assertEquals($forum->cmid, $result['warnings'][0]['itemid']);
}

/**
* Test get_all_states
* @covers \core_filters\external\get_all_states::execute
*/
public function test_get_all_states() {
$this->resetAfterTest(true);
$this->setAdminUser();

// Get all filters and disable them all globally except for the first.
$allfilters = filter_get_all_installed();
reset($allfilters);
$firstfilter = key($allfilters);
foreach ($allfilters as $filter => $filterdata) {
if ($filter == $firstfilter) {
filter_set_global_state($filter, TEXTFILTER_ON);
continue;
}
filter_set_global_state($filter, TEXTFILTER_DISABLED);
}

// Set some filters at particular levels.
$course = self::getDataGenerator()->create_course();
filter_set_local_state($firstfilter, \context_course::instance($course->id)->id, TEXTFILTER_ON);
$forum = self::getDataGenerator()->create_module('forum', (object) ['course' => $course->id]);
filter_set_local_state($firstfilter, \context_module::instance($forum->cmid)->id, TEXTFILTER_OFF);

$result = get_all_states::execute();
$result = external_api::clean_returnvalue(get_all_states::execute_returns(), $result);

$totalcount = count($allfilters) + 2; // All filters plus two local states.
$this->assertCount($totalcount, $result['filters']);

$customfound = 0;
foreach ($result['filters'] as $filter) {
if ($filter['contextlevel'] == 'course' && $filter['instanceid'] == $course->id) {
$this->assertEquals($firstfilter, $filter['filter']);
$this->assertEquals(TEXTFILTER_ON, $filter['state']);
$customfound++;
} else if ($filter['contextlevel'] == 'module' && $filter['instanceid'] == $forum->cmid) {
$this->assertEquals($firstfilter, $filter['filter']);
$this->assertEquals(TEXTFILTER_OFF, $filter['state']);
$customfound++;
} else if ($filter['filter'] == $firstfilter) {
$this->assertEquals($firstfilter, $filter['filter']);
$this->assertEquals(TEXTFILTER_ON, $filter['state']);
$this->assertEquals(1, $filter['sortorder']);
} else {
$this->assertEquals(TEXTFILTER_DISABLED, $filter['state']);
}
}
$this->assertEquals(2, $customfound); // Both custom states found.
}
}
7 changes: 7 additions & 0 deletions lib/db/services.php
Expand Up @@ -2787,6 +2787,13 @@
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_filters_get_all_states' => [
'classname' => 'core_filters\external\get_all_states',
'description' => 'Retrieve all the filters and their states (including overridden ones in any context).',
'type' => 'read',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],

'core_customfield_delete_field' => array(
'classname' => 'core_customfield_external',
'methodname' => 'delete_field',
Expand Down
10 changes: 10 additions & 0 deletions lib/filterlib.php
Expand Up @@ -1316,6 +1316,16 @@ function filter_get_global_states() {
return $DB->get_records('filter_active', array('contextid' => $context->id), 'sortorder', 'filter,active,sortorder');
}

/**
* Retrieve all the filters and their states (including overridden ones in any context).
*
* @return array filters objects containing filter name, context, active state and sort order.
*/
function filter_get_all_states(): array {
global $DB;
return $DB->get_records('filter_active');
}

/**
* Delete all the data in the database relating to a filter, prior to deleting it.
*
Expand Down

0 comments on commit f693e84

Please sign in to comment.