Displaying custom profile fields in a block

Displaying custom profile fields in a block

by Antony Coombs -
Number of replies: 5

Hello all - I'm very new to working with blocks, but have got to grips with the basics of building them. I'm not quite so up-to-speed on how I can call up user information, etc. to display in a block.

What I'm looking to do is display the custom profile fields for the current user, as a kind of quick reference for them (i.e. without them having to visit their profile page)

Any pointers or suggestions would be really welcome

Cheers -- Tony

Average of ratings: -
In reply to Antony Coombs

Re: Displaying custom profile fields in a block

by Antony Coombs -

Ok - doing a Lewis Carroll White Queen and screaming before my finger was pricked - working on it a bit more, I found I can just call the global $USER and then access $USER->custom_field_name

sorry to bother you all

cheers -- Tony

In reply to Antony Coombs

Re: Displaying custom profile fields in a block

by Antony Coombs -

I had a message asking how I did this, so I thought it would be better to put it here so that anyone can see (and correct, if what I've said is either inaccurate or bad practice!)

I'm assuming anyone reading this is ok with the basics of building a block (as shown at http://docs.moodle.org/en/Development:Blocks )

So... in the block content section of your block's php file, you need some code along the lines of the following:

function get_content() {

global $USER; // you need this line to be able to use the global array "$USER" within this function

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

$this->content = new stdClass;

$this->content->text = 'Favourite colour: '.$USER->favcolour; // I haven't worked out how to get the field description dynamically displayed, so I've just hard coded the text to explain the field (i.e. "Favourite colour: "). The "favcolour" bit of "$USER->favcolour" is the unique name you gave your custom profile field when you set it up in the Users > Profile fields section of the site administration. This bit of code simply asks for the "favcolour" entry in the global $USER array. As it stand here, this block only displays a single line of text, so in practice I'm sure you would want to combine it with other stuff.

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

return $this->content;
}

In reply to Antony Coombs

Re: Displaying custom profile fields in a block

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Have a look at the profile_display_fields() function in user/profile/lib.php -- which demonstrates how to retrieve the custom profile fields/descriptions dynamically cool
In reply to Antony Coombs

Re: Displaying custom profile fields in a block

by Stephen Barker -

This was exactly what I was looking to do in order to show a pupil's self-set target grade on the front page when they log on to remind them (Subjects that use books have stickers on the front - I wanted something a bit more hi-tech!)

The code I've put in my block is shown below:

<?php //$Id: block_target_level.php,v 1.0 2009/03/20 15:42:44 defacer Exp $

class block_target_level extends block_base {

function init() {

$this->title = 'My Target';

$this->version = 2009032000;

}

function get_content() {

global $USER;

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

return $this->content;

}

$this->content = new stdClass;

$this->content->text = 'Target level:<br>'.$USER->NCLevel;

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

return $this->content;

}

}

?>

The user field that I have created is called 'NCLevel' and I can see this when I look at a profile.  However, it won't show up in the block on the front page.  If I change the code to something like '$USER->description' it quite happily shows the description field.

Any ideas?!  Many thanks in advance!

Steve

In reply to Stephen Barker

Re: Displaying custom profile fields in a block

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Steve - Try including and making use of the profile_load_data function in /user/profile/lib.php. I suspect you could simply add the follow lines before your call and it may work:

require_once($CFG->dirroot.'/user/profile/lib.php);
profile_load_data($USER);

If that does not work for you I would say check out the other function in /user/profile/lib.php and see what is available. I suspect something will work for you. Let me know if you need further help. Peace - Anthony