Skip to content

Instantly share code, notes, and snippets.

@FMCorz
Last active June 11, 2022 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FMCorz/2e6d29c8dda5ec9d6d6c349d6b2e8528 to your computer and use it in GitHub Desktop.
Save FMCorz/2e6d29c8dda5ec9d6d6c349d6b2e8528 to your computer and use it in GitHub Desktop.
Useful functions for Level Up XP 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