Commit 15b41641 authored by Mathew May's avatar Mathew May
Browse files

MDL-73087 blocks: Add a way to fetch my courses via WS

parent d5698ac6
...@@ -221,6 +221,7 @@ class core_block_external extends external_api { ...@@ -221,6 +221,7 @@ class core_block_external extends external_api {
array( array(
'userid' => new external_value(PARAM_INT, 'User id (optional), default is current user.', VALUE_DEFAULT, 0), 'userid' => new external_value(PARAM_INT, 'User id (optional), default is current user.', VALUE_DEFAULT, 0),
'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false), 'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
'mypage' => new external_value(PARAM_TEXT, 'What my page to return blocks of', VALUE_DEFAULT, MY_PAGE_DEFAULT),
) )
); );
} }
...@@ -228,20 +229,21 @@ class core_block_external extends external_api { ...@@ -228,20 +229,21 @@ class core_block_external extends external_api {
/** /**
* Returns blocks information for the given user dashboard. * Returns blocks information for the given user dashboard.
* *
* @param int $userid The user id to retrive the blocks from, optional, default is to current user. * @param int $userid The user id to retrieve the blocks from, optional, default is to current user.
* @param bool $returncontents Whether to return the block contents * @param bool $returncontents Whether to return the block contents
* @param string $mypage The page to get blocks of within my
* @return array Blocks list and possible warnings * @return array Blocks list and possible warnings
* @throws moodle_exception * @throws moodle_exception
* @since Moodle 3.6 * @since Moodle 3.6
*/ */
public static function get_dashboard_blocks($userid = 0, $returncontents = false) { public static function get_dashboard_blocks($userid = 0, $returncontents = false, $mypage = MY_PAGE_DEFAULT) {
global $CFG, $USER, $PAGE; global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/my/lib.php'); require_once($CFG->dirroot . '/my/lib.php');
$warnings = array(); $warnings = array();
$params = self::validate_parameters(self::get_dashboard_blocks_parameters(), $params = self::validate_parameters(self::get_dashboard_blocks_parameters(),
['userid' => $userid, 'returncontents' => $returncontents]); ['userid' => $userid, 'returncontents' => $returncontents, 'mypage' => $mypage]);
$userid = $params['userid']; $userid = $params['userid'];
if (empty($userid)) { if (empty($userid)) {
...@@ -258,8 +260,14 @@ class core_block_external extends external_api { ...@@ -258,8 +260,14 @@ class core_block_external extends external_api {
$context = context_user::instance($userid);; $context = context_user::instance($userid);;
self::validate_context($context); self::validate_context($context);
// Get the My Moodle page info. Should always return something unless the database is broken. $currentpage = null;
if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) { if ($params['mypage'] === MY_PAGE_DEFAULT) {
$currentpage = my_get_page($userid);
} else if ($params['mypage'] === MY_PAGE_COURSES) {
$currentpage = my_get_page($userid, MY_PAGE_PUBLIC, MY_PAGE_COURSES);
}
if (!$currentpage) {
throw new moodle_exception('mymoodlesetup'); throw new moodle_exception('mymoodlesetup');
} }
......
...@@ -476,4 +476,71 @@ class core_block_externallib_testcase extends externallib_advanced_testcase { ...@@ -476,4 +476,71 @@ class core_block_externallib_testcase extends externallib_advanced_testcase {
$this->expectException('moodle_exception'); $this->expectException('moodle_exception');
core_block_external::get_dashboard_blocks($user2->id); core_block_external::get_dashboard_blocks($user2->id);
} }
/**
* Test user get default dashboard blocks for my courses page.
*/
public function test_get_dashboard_blocks_my_courses() {
global $PAGE, $DB;
$this->resetAfterTest(true);
$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_myoverview');
$systempage = $DB->get_record('my_pages', ['userid' => null, 'name' => MY_PAGE_COURSES, 'private' => false]);
// Get the expected default blocks.
$alldefaultblocksordered = $DB->get_records_menu(
'block_instances',
['pagetypepattern' => 'my-index', 'subpagepattern' => $systempage->id],
'defaultregion, defaultweight ASC',
'id, blockname'
);
$this->setUser($user);
// Check for the default blocks.
$result = core_block_external::get_dashboard_blocks($user->id, false, MY_PAGE_COURSES);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result);
// Expect all default blocks defined in blocks_add_default_system_blocks().
$this->assertCount(count($alldefaultblocksordered), $result['blocks']);
$returnedblocks = [];
foreach ($result['blocks'] as $block) {
// 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'] == 'myoverview') {
// Convert config to associative array to avoid DB sorting randomness.
$config = array_column($block['configs'], null, 'name');
$this->assertArrayHasKey('displaycategories', $config);
$this->assertEquals(json_encode('0'), $config['displaycategories']['value']);
$this->assertEquals('plugin', $config['displaycategories']['type']);
}
}
// Check that we received the blocks in the expected order.
$this->assertEquals(array_values($alldefaultblocksordered), $returnedblocks);
}
/**
* Test user passing the wrong page type and getting an exception.
*/
public function test_get_dashboard_blocks_incorrect_page() {
global $PAGE;
$this->resetAfterTest(true);
$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.
$this->setUser($user);
$this->expectException('moodle_exception');
// Check for the default blocks with a fake page, no need to assign as it'll throw.
core_block_external::get_dashboard_blocks($user->id, false, 'fakepage');
}
} }
...@@ -5,6 +5,7 @@ information provided here is intended especially for developers. ...@@ -5,6 +5,7 @@ information provided here is intended especially for developers.
* Block block_quiz_results has been completely removed from core. * Block block_quiz_results has been completely removed from core.
The Quiz results block is hidden by default since Moodle 2.9. It is recommended to use the Activity results block instead, which works with any type of activity (not just quizzes). The Quiz results block is hidden by default since Moodle 2.9. It is recommended to use the Activity results block instead, which works with any type of activity (not just quizzes).
* External function core_block::get_dashboard_blocks has a new parameter to indicate if you want to receive the block on the my/courses page.
=== 3.8 === === 3.8 ===
* Block block_community is no longer a part of core. * Block block_community is no longer a part of core.
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$version = 2021120100.00; // YYYYMMDD = weekly release date of this DEV branch. $version = 2021120100.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches. // RR = release increments - 00 in DEV branches.
// .XX = incremental changes. // .XX = incremental changes.
$release = '4.0dev+ (Build: 20211201)'; // Human-friendly version name $release = '4.0dev+ (Build: 20211201)'; // Human-friendly version name
......
Supports Markdown
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