My profile API

Jump to: navigation, search


The My Profile API is a set of methods that allows user related information to be displayed on the my profile page. The API is similar to the navigation API as the page is made up of a tree of nodes.

The following picture shows the different elements of the my profile page.



The category class requires the following properties be filled out when creating a new object:

string The name of category. Used as a key for child nodes to attach to, or for other categories to use in $after.
string The text being displayed. This needs to be a localised string (get_string());
string The name for the category that this category comes after. Optional
string HTML class attributes for this category. Classes should be separated by a space, e.g. 'class1 class2'. Optional

Adding a category

$category = new core_user\output\myprofile\category('badges', get_string('badges', 'badges'), null);


Nodes contain most of the information for the user profile page. The node class requires the following properties be filled out when creating a new object:

string The parent category. Use the name of the category that this node belongs to.
string The name of this node. Used as a key for other nodes.
string A localised string for the node. Please use get_string().
string By using the name of another node you can position this after that one. Optional
string|moodle_url A url that this node links to. It is recommended that you use a moodle_url. Optional
string Additional content to display under this node. Optional
string|pix_icon An icon to be displayed next to the node. May be a pix_icon. Optional
string HTML class attributes for this node. Classes should be separated by a space, e.g. 'class1 class2'. Optional

Adding a node

The following is an example of how to create a node.

$url = new moodle_url('/mod/forum/user.php', array('id' => $user->id, 'mode' => 'discussions'));
$string = get_string('myprofileowndis', 'mod_forum');
$node = new core_user\output\myprofile\node('miscellaneous', 'forumdiscussions', $string, null, $url);

Nodes can be added to categories in two different ways. This first way uses $parentcat to link the node.


If you are creating a category in the same area you can link it the following way where $category has been created further up the page.



By default the nodes with content will be displayed before nodes without content. This can be overridden by specifying the node you want to follow in the $after variable. Please, when you are creating your nodes, group similar nodes together. This will ensure that some sort of uniformity is maintained on the page.

Adding information to the User's profile page

To add the category and node information into the my profile page, you need to create a function in the lib.php file. The function needs to start with the component name followed by '_myprofile_navigation()'. You will need to add the following parameters to your function:

core_user\output\myprofile\tree The myprofile tree to add categories and nodes to.
stdClass The user object that the profile page belongs to.
boolean If the $user object is the current user.
stdClass The course to determine if we are in a course context or system context.
public function mod_forum_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {

Reports and breadcrumbs

When adding a report to the report category of the my profile page. You should also look to use add_report_nodes() in the report page, to create the appropriate breadcrumb in different contexts. This function is part of the moodle_page class and should be accessed through the global $PAGE.

The function add_report_nodes() takes the following parameters:

integer The user ID.
array Takes two values - Name, and the URL of the final node that we are creating.

Example of using the function In a report page.

$navigationinfo = array(
        'name' => get_string('reportname', 'plugin_component'),
        'url' => new moodle_url('/location/to/page.php')
$PAGE->add_report_nodes($user->id, $navigationinfo);
// Now set the heading.
$PAGE->set_heading(get_string('awesomeheading', 'plugin_component');