mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-73087 blocks: Add a way to fetch my courses via WS
This commit is contained in:
parent
d5698ac689
commit
15b416416c
@ -221,6 +221,7 @@ class core_block_external extends external_api {
|
||||
array(
|
||||
'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),
|
||||
'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 {
|
||||
/**
|
||||
* 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 string $mypage The page to get blocks of within my
|
||||
* @return array Blocks list and possible warnings
|
||||
* @throws moodle_exception
|
||||
* @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;
|
||||
|
||||
require_once($CFG->dirroot . '/my/lib.php');
|
||||
|
||||
$warnings = array();
|
||||
$params = self::validate_parameters(self::get_dashboard_blocks_parameters(),
|
||||
['userid' => $userid, 'returncontents' => $returncontents]);
|
||||
['userid' => $userid, 'returncontents' => $returncontents, 'mypage' => $mypage]);
|
||||
|
||||
$userid = $params['userid'];
|
||||
if (empty($userid)) {
|
||||
@ -258,8 +260,14 @@ class core_block_external extends external_api {
|
||||
$context = context_user::instance($userid);;
|
||||
self::validate_context($context);
|
||||
|
||||
// Get the My Moodle page info. Should always return something unless the database is broken.
|
||||
if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
|
||||
$currentpage = null;
|
||||
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');
|
||||
}
|
||||
|
||||
|
@ -476,4 +476,71 @@ class core_block_externallib_testcase extends externallib_advanced_testcase {
|
||||
$this->expectException('moodle_exception');
|
||||
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.
|
||||
|
||||
* 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).
|
||||
* 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 ===
|
||||
* Block block_community is no longer a part of core.
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
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.
|
||||
// .XX = incremental changes.
|
||||
$release = '4.0dev+ (Build: 20211201)'; // Human-friendly version name
|
||||
|
Loading…
x
Reference in New Issue
Block a user