Last active
September 12, 2024 03:21
-
-
Save FMCorz/2e6d29c8dda5ec9d6d6c349d6b2e8528 to your computer and use it in GitHub Desktop.
Useful functions for Level Up XP integrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 sitewide 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 sitewide 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 sitewide 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()); | |
} | |
/** | |
* Delete a user. | |
* | |
* @param int $userid The ID of the user. | |
* @param int $courseid The course ID to use, this has no effect in sitewide mode. | |
* @return string | |
*/ | |
function block_xp_delete_user($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); | |
$world->get_store()->delete($userid); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment