public static function get_dashboard_blocks($userid = 0, $returncontents = false) {
        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 = $params['userid'];
        if (empty($userid)) {
            $userid = $USER->id;
        }

        if ($USER->id != $userid) {
            // We must check if the current user can view other users dashboard.
            require_capability('moodle/site:config', context_system::instance());
            $user = core_user::get_user($userid, '*', MUST_EXIST);
            core_user::require_active_user($user);
        }

        $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)) {
            throw new moodle_exception('mymoodlesetup');
        }

        $PAGE->set_context($context);
        $PAGE->set_pagelayout('mydashboard');
        $PAGE->set_pagetype('my-index');
        $PAGE->blocks->add_region('content');   // Need to add this special regition to retrieve the central blocks.
        $PAGE->set_subpage($currentpage->id);

        // Load the block instances in the current $PAGE for all the regions.
        $returninvisible = has_capability('moodle/my:manageblocks', $context) ? true : false;
        $allblocks = self::get_all_current_page_blocks($returninvisible, $params['returncontents']);

        return array(
            'blocks' => $allblocks,
            'warnings' => $warnings
        );
    }