General developer forum

Website with badges (programação)

 
Picture of Ricardo Souza
Website with badges (programação)
 

I would like a page with the badges obtained by the User.

Nothing else. No menu, text, download button.

What code do I need to put on my NEW page for this result?

It can not be mybadges page. I want something clean with just the user badges.

 
Average of ratings: -
Picture of Sara Arjona Téllez
Re: Website with badges (programação)
Core developersMoodle HQPlugin developersTesters

Hi Ricardo!

You should create a page, similar to the badges/mybadges.php, but replacing the $USER->id parameter in the function badges_get_user_badges to the user identifier you want to see. When you create this page, make sure only users with the right capabilities are able to access them to avoid all the users, even guests, access to this information).

If you would like this feature will be integrated sometime in Moodle core, you can add a comment to some related issue, such as https://tracker.moodle.org/browse/MDL-51878, to explain what you want, or even create a new one if you know there are more people interested in. 

Best wishes!

 
Average of ratings: -
Picture of Ricardo Souza
Re: Website with badges (programação)
 

Thank you !

But I'm still having trouble.

Is there a line of code or command that only displays the badges of the user?

 
Average of ratings: -
Picture of Sara Arjona Téllez
Re: Website with badges (programação)
Core developersMoodle HQPlugin developersTesters

Hi Ricardo!

There is a function in lib/badges.lib to get all the badges for an user:

/**
 * Get badges for a specific user.
 *
 * @param int $userid User ID
 * @param int $courseid Badges earned by a user in a specific course
 * @param int $page The page or records to return
 * @param int $perpage The number of records to return per page
 * @param string $search A simple string to search for
 * @param bool $onlypublic Return only public badges
 * @return array of badges ordered by decreasing date of issue
 */
function badges_get_user_badges($userid, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) {

There is a specific forum for technical questions related to development, so I suggest you to move this conversation there: "General developer forum" (https://moodle.org/mod/forum/view.php?id=55).

Kind regards.

 
Average of ratings: -
Picture of Ricardo Souza
Re: Website with badges (programação)
 

Thank you for choosing to help. I already created a topic in the programming forum.

 
Average of ratings: -
Mary Cooch
Re: Website with badges (programação)
Documentation writersMoodle Course Creator Certificate holdersMoodle HQParticularly helpful MoodlersTestersTranslators

Hello. Just to let  you know I have moved this thread to the General developer forum ( and deleted your other one which simply linked here.) Continue the conversation smile

 
Average of ratings: Useful (1)
Picture of Darko Miletić
Re: Website with badges (programação)
Core developersParticularly helpful Moodlers

You have a fully working example of Badges API in badges block.

https://github.com/moodle/moodle/blob/master/blocks/badges/block_badges.php


But here is the simple page printing badges for current user:

require_once('/path/to/config.php');
require_once($CFG->libdir.'/badgeslib.php');

require_login();

$PAGE->set_context(context_system::instance());
$PAGE->set_url('/badgetest.php');
$PAGE->set_heading("Badges");
$PAGE->set_title("Badges");

echo $OUTPUT->header();

if ($badges = badges_get_user_badges($USER->id)) {
    $output = $PAGE->get_renderer('core', 'badges');
    echo $output->print_badges_list($badges, $USER->id, true);
} else {
    echo $OUTPUT->notification("No Badges", 'info');
}

echo $OUTPUT->footer();



 
Average of ratings: Useful (1)
Picture of Ricardo Souza
Re: Website with badges (programação)
 

Thank you again! You've helped me a lot in the past few weeks. Hugs.

 
Average of ratings: -