0

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;}
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    **I want to** is not a question. It just informs us that you want us to do the heavy lifting for you. Where are you stuck? What have you researched? What have you tried? To be clear, we'll help you at stackoverflow but we're not a free do-my-thinking service See [how to ask](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Sep 12 '22 at 16:35
  • Start by reading the PHP Manual and defining the class methods properly – RiggsFolly Sep 12 '22 at 16:36
  • Actually I Am Stuck Brother, Anyways Thanks For Your Advice, Appreciated. – Mohammad-Ahmad Sep 12 '22 at 16:46

1 Answers1

0

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 . ' ';
}
Russell England
  • 9,436
  • 1
  • 27
  • 41
  • Thanks Man, Works Like A Charm, Can You Explain what this (.=) means, if easy. – Mohammad-Ahmad Sep 13 '22 at 09:04
  • No probs :) Yeah `.=` is a string operator to concatenate 2 strings - the equivalent of `$footer = $footer . $role->name` - https://www.php.net/manual/en/language.operators.string.php – Russell England Sep 13 '22 at 13:08