Commit c9acdfb6 authored by Juan Leyva's avatar Juan Leyva
Browse files

MDL-65400 block: Support getting block settings via WS

parent aaff6692
......@@ -67,6 +67,16 @@ class core_block_external extends external_api {
),
'Block contents (if required).', VALUE_OPTIONAL
),
'configs' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_RAW, 'Name.'),
'value' => new external_value(PARAM_RAW, 'Value.'),
'type' => new external_value(PARAM_ALPHA, 'Type (instance or plugin).'),
)
),
'Block instance and plugin configuration settings.', VALUE_OPTIONAL
),
), 'Block information.'
);
}
......@@ -110,6 +120,17 @@ class core_block_external extends external_api {
if ($returncontents) {
$block['contents'] = (array) $blockinstances[$bc->blockinstanceid]->get_content_for_external($OUTPUT);
}
$configs = (array) $blockinstances[$bc->blockinstanceid]->get_config_for_external();
foreach ($configs as $type => $data) {
foreach ((array) $data as $name => $value) {
$block['configs'][] = [
'name' => $name,
'value' => $value,
'type' => $type,
];
}
}
$allblocks[] = $block;
}
}
......
......@@ -305,6 +305,21 @@ class block_base {
return $bc;
}
/**
* Return the plugin config settings for external functions.
*
* In some cases the configs will need formatting or be returned only if the current user has some capabilities enabled.
*
* @return stdClass the configs for both the block instance and plugin (as object with name -> value)
* @since Moodle 3.8
*/
public function get_config_for_external() {
return (object) [
'instance' => new stdClass(),
'plugin' => new stdClass(),
];
}
/**
* Convert the contents of the block to HTML.
*
......
......@@ -213,6 +213,19 @@ class core_block_externallib_testcase extends externallib_advanced_testcase {
$this->assertEquals('', $result['blocks'][0]['contents']['footer']);
$this->assertCount(1, $result['blocks'][0]['contents']['files']);
$this->assertEquals($newblock, $result['blocks'][0]['name']);
$configcounts = 0;
foreach ($result['blocks'][0]['configs'] as $config) {
if ($config['type'] = 'plugin' && $config['name'] == 'allowcssclasses' && $config['value'] == 0) {
$configcounts++;
} else if ($config['type'] = 'instance' && $config['name'] == 'text' && $config['value'] == $body) {
$configcounts++;
} else if ($config['type'] = 'instance' && $config['name'] == 'title' && $config['value'] == $title) {
$configcounts++;
} else if ($config['type'] = 'instance' && $config['name'] == 'format' && $config['value'] == 0) {
$configcounts++;
}
}
$this->assertEquals(4, $configcounts);
}
/**
......@@ -225,6 +238,9 @@ class core_block_externallib_testcase extends externallib_advanced_testcase {
$user = $this->getDataGenerator()->create_user();
$PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
// Force a setting change to check the returned blocks settings.
set_config('displaycategories', 0, 'block_recentlyaccessedcourses');
// Get the expected default blocks.
$alldefaultblocksordered = $DB->get_records_menu('block_instances',
array('pagetypepattern' => 'my-index'), 'defaultregion, defaultweight ASC', 'id, blockname');
......@@ -242,6 +258,12 @@ class core_block_externallib_testcase extends externallib_advanced_testcase {
// Check all the returned blocks are in the expected blocks array.
$this->assertContains($block['name'], $alldefaultblocksordered);
$returnedblocks[] = $block['name'];
// Check the configuration returned for this default block.
if ($block['name'] == 'recentlyaccessedcourses') {
$this->assertEquals('displaycategories', $block['configs'][0]['name']);
$this->assertEquals(0, $block['configs'][0]['value']);
$this->assertEquals('plugin', $block['configs'][0]['type']);
}
}
// Remove lp block.
array_shift($alldefaultblocksordered);
......
......@@ -4,6 +4,9 @@ information provided here is intended especially for developers.
=== 3.8 ===
* Block block_community is no longer a part of core.
* Block block_participants is no longer a part of core.
* Block plugins should overwrite get_config_for_external function to return the blocks settings viewable by the current user.
If the block plugin does not have any setting that could be considerated private (like a private/access key/token),
is ok to return all the settings via the get_config_for_external function.
=== 3.7 ===
* The block:addinstance capability is no longer required if the block can only be added to a dashboard.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment