Skip to content

Instantly share code, notes, and snippets.

@FMCorz

FMCorz/functions.php

Last active Mar 26, 2020
Embed
What would you like to do?
Useful functions for Level up! integrations
<?php
/**
* Manually award points.
*
* @param int $userid The ID of the target user.
* @param int $points The numbers of points to award.
* @param int $courseid The course ID to use, this has no effect in 'For the whole site' mode.
*/
function block_xp_award_points($userid, $points, $courseid = 0) {
if (!class_exists('block_xp\di')) {
return;
}
$world = \block_xp\di::get('course_world_factory')->get_world($courseid);
$world->get_store()->increase($userid, $points);
}
/**
* Display a user's level badge.
*
* @param int $userid The ID of the user to display the badge of.
* @param int $courseid The course ID to use, this has no effect in 'For the whole site' mode.
* @return string
*/
function block_xp_render_user_level_badge($userid, $courseid = null) {
global $COURSE;
if (!class_exists('block_xp\di')) {
return '';
}
if (empty($courseid)) {
$courseid = $COURSE->id;
}
$world = \block_xp\di::get('course_world_factory')->get_world($courseid);
$state = $world->get_store()->get_state($userid);
return \block_xp\di::get('renderer')->level_badge($state->get_level());
}
/**
* Display a user's experience points.
*
* @param int $userid The ID of the user to display the xp of.
* @param int $courseid The course ID to use, this has no effect in 'For the whole site' mode.
* @return string
*/
function block_xp_render_user_xp($userid, $courseid = null) {
global $COURSE;
if (!class_exists('block_xp\di')) {
return '';
}
if (empty($courseid)) {
$courseid = $COURSE->id;
}
$world = \block_xp\di::get('course_world_factory')->get_world($courseid);
$state = $world->get_store()->get_state($userid);
return \block_xp\di::get('renderer')->xp($state->get_xp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.