phpmoodlemoodle-api

Moodle: Error In fetching capabilities of logged-in User


class block_add extends block_base {

function init() {
    $this->title = get_string('pluginname', 'block_add');

}

function applicable_formats() {
    return array('all' => true, 'tag' => false);
}

function specialization() {
    $this->title = isset($this->config->title) ? $this->config->title : get_string('newblock', 'block_add');
}

function instance_allow_multiple() {
    return true;
}

function get_content()
{

    global $USER, $DB;

    if ($this->content !== NULL) {
        return $this->content;
    }

    $this->content = new stdClass();
    $roles = $DB->get_records('role_capabilities',['userid'=>$USER->id]);->(// in this lines i am getting error "error readig from database")
    var_dump($roles);
    foreach ($roles as $role) {
        $this->content->footer .= "Role Id  = ".$role->contextid."</br>"."Context Id  = ".$role->contextid."</br>"."Permission  = ".$role->capability."</br>" ;
    }

}




function has_config() {return true;}

}

i am getting "error reading from database" idk why, i am trying to print all assigned capabilities of logged-in user (for example: Admin),i am newbie in php as well on Moodle


Solution

  • The role_capabilities table is for role capabilities not user capabilities, so there isn't a userid column

    Capabilities (permissions) in Moodle is quite complex. There are capabilities at various levels (context) - site, category, course, module/activity, block, user. The capabilities are assigned to a role and a user is assigned to one or more roles. The capabilities can also be overridden. So it's not straightforward.

    I'd suggest looking through /lib/accesslib.php at the various get functions to see if there an existing function that does what you want.

    The list of capabilities will also be very long, so probably not suitable for a block.

    You can see a list of capabilities for a user at

    Site administration > Users > Permissions > Check system permissions

    and a list of capabilities for a role at

    Site administration > Users > Permissions > Capability overview