Home > Enterprise >  I want to print all user role by id into my footer
I want to print all user role by id into my footer

Time:09-13

I want user roles in any part of my block not necessary in footer, kindly help me as I am newbie in php and moodle too, here below is the code.

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();
        $this->content->footer= $DB->get_records('role_assignments', ['userid' => $user->id]); 
        return $this->content;
    }
    
    function has_config() {return true;}
}

CodePudding user response:

I'm not sure what you are trying to do?

Do you want to list the user roles in the footer in a block?

This function returns an array of record objects

$DB->get_records('role_assignments', ['userid' => $user->id]); 

The footer property expects a string or html

$this->content->footer

So you will need to format the data first eg:

$roles = $DB->get_records('role_assignments', ['userid' => $user->id]); 
$this->content->footer = '';

foreach ($roles as $role) {
    $this->content->footer .= $role->roleid . ' ';
}

Having said that, the role_assignments table only has the role ids not the names. You could use an existing function instead.

$roles = get_user_roles(\context_system::instance());

$this->content->footer = '';

foreach ($roles as $role) {
    $this->content->footer .= $role->name . ' ';
}
  • Related