Understanding how Achievements are Earned

///Understanding how Achievements are Earned
Understanding how Achievements are Earned 2019-03-30T16:24:44-04:00

How exactly does the earning process in BadgeOS work? Well, its a somewhat elaborate process, but it’s very linear and logical. The very first thing that happens is that something triggers badgeos_maybe_award_achievement_to_user(). This could be a hook we’re listening for in badgeos_get_activity_triggers() – a filterable function – which we respond to using badgeos_trigger_event().

However we get there, badgeos_maybe_award_achievement_to_user() will then check if the user has access to the achievement using badgeos_user_has_access_to_achievement() – a filterable function – and, if they do, we run badgeos_check_achievement_completion_for_user() – another filterable function. If we both have access and have completed the achievement, we run badgeos_award_achievement_to_user() which then fires a couple of different do_action() calls, like badgeos_award_achievement. One of the core calls hooked to badgeos_award_achievement is badgeos_maybe_award_additional_achievements_to_user() which looks for other related things and starts the process over again with badgeos_maybe_awarad_achievement_to_user() for each related thing.

Functions:

badgeos_get_activity_triggers()

Retrieve the registered activity triggers (hooks).

badgeos_trigger_event()

Fires any time a registered activity trigger (hook) is called, runs maybe_award_achievement_to_user().

badgeos_maybe_award_achievement_to_user( $achievement_id = 0, $user_id = 0 )

Check if a given user has access to, and has completed, a given achievement, and awards it if so.

badgeos_user_has_access_to_achievement( $achievement_id = 0, $user_id = 0)

Checks if the given user can access the given achievement. This can be extended/overridden via filters.

badgeos_check_achievement_completion_for_user( $achievement_id = 0, $user_id = 0 )

Checks if the given user has completed the given achievement. This can be extended/overridden via filters.

badgeos_award_achievement_to_user( $achievement_id = 0, $user_id = 0 )

Awards the given achievement to the given user.

badgeos_maybe_award_additional_achievements_to_user( $achievement_id = 0, $user_id = 0 )

Checks if the awarded achievement is connected to any other achievements, then attempts to award each via badgeos_maybe_award_achievement_to_user.

Filters:

badgeos_activity_triggers

user_has_access_to_achievement

user_deserves_achievement

Actions:

badgeos_award_achievement

badgeos_revoke_achievement

badgeos_unlock_{$achievement_type}

badgeos_unlock_all_{$achievement_type}