Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]

Badge assertion library.

Author: Yuliya Bozhko <yuliya.bozhko@totaralms.com>
Copyright: 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 952 lines (33 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: badge  - X-Ref

Class that represents badge.

__construct($badgeid)   X-Ref
Constructs with badge details.

param: int $badgeid badge ID.

get_context()   X-Ref
Use to get context instance of a badge.

return: context instance.

get_aggregation_methods()   X-Ref
Return array of aggregation methods

return: array

get_accepted_criteria()   X-Ref
Return array of accepted criteria types for this badge

return: array

save()   X-Ref
Save/update badge information in 'badge' table only.
Cannot be used for updating awards and criteria settings.

return: boolean Returns true on success.

make_clone()   X-Ref
Creates and saves a clone of badge with all its properties.
Clone is not active by default and has 'Copy of' attached to its name.

return: int ID of new badge.

is_active()   X-Ref
Checks if badges is active.
Used in badge award.

return: boolean A status indicating badge is active

get_status_name()   X-Ref
Use to get the name of badge status.

return: string

set_status($status = 0)   X-Ref
Use to set badge status.
Only active badges can be earned/awarded/issued.

param: int $status Status from BADGE_STATUS constants

is_locked()   X-Ref
Checks if badges is locked.
Used in badge award and editing.

return: boolean A status indicating badge is locked

has_awards()   X-Ref
Checks if badge has been awarded to users.
Used in badge editing.

return: boolean A status indicating badge has been awarded at least once

get_awards()   X-Ref
Gets list of users who have earned an instance of this badge.

return: array An array of objects with information about badge awards.

is_issued($userid)   X-Ref
Indicates whether badge has already been issued to a user.

return: boolean
param: int $userid User to check

issue($userid, $nobake = false)   X-Ref
Issue a badge to user.

param: int $userid User who earned the badge
param: boolean $nobake Not baking actual badges (for testing purposes)

review_all_criteria()   X-Ref
Reviews all badge criteria and checks if badge can be instantly awarded.

return: int Number of awards

get_criteria_completions($userid)   X-Ref
Gets an array of completed criteria from 'badge_criteria_met' table.

return: array Records of criteria completions
param: int $userid Completions for a user

has_criteria()   X-Ref
Checks if badges has award criteria set up.

return: boolean A status indicating badge has at least one criterion

get_criteria()   X-Ref
Returns badge award criteria

return: array An array of badge criteria

get_aggregation_method($criteriatype = 0)   X-Ref
Get aggregation method for badge criteria

return: int One of BADGE_CRITERIA_AGGREGATION_ALL or BADGE_CRITERIA_AGGREGATION_ANY
param: int $criteriatype If none supplied, get overall aggregation method (optional)

can_expire()   X-Ref
Checks if badge has expiry period or date set up.

return: boolean A status indicating badge can expire

calculate_expiry($timestamp)   X-Ref
Calculates badge expiry date based on either expirydate or expiryperiod.

return: int A timestamp
param: int $timestamp Time of badge issue

has_manual_award_criteria()   X-Ref
Checks if badge has manual award criteria set.

return: boolean A status indicating badge can be awarded manually

delete($archive = true)   X-Ref
Fully deletes the badge or marks it as archived.

param: boolean $archive Achive a badge without actual deleting of any data.

add_related_badges($relatedids)   X-Ref
Add multiple related badges.

param: array $relatedids Id of badges.

delete_related_badge($relatedid)   X-Ref
Delete an related badge.

return: boolean A status for delete an related badge.
param: int $relatedid Id related badge.

has_related()   X-Ref
Checks if badge has related badges.

return: boolean A status related badge.

get_related_badges($activeonly = false)   X-Ref
Get related badges of badge.

return: array Related badges information.
param: boolean $activeonly Do not get the inactive badges when is true.

save_alignment($alignment, $alignmentid = 0)   X-Ref
Insert/update alignment information of badge.

return: bool|int A status/ID when insert or update data.
param: stdClass $alignment Data of a alignment.
param: int $alignmentid ID alignment.

delete_alignment($alignmentid)   X-Ref
Delete a alignment of badge.

return: boolean A status for delete a alignment.
param: int $alignmentid ID alignment.

get_alignments()   X-Ref
Get alignments of badge.

return: array List content alignments.

save_endorsement($endorsement)   X-Ref
Insert/update Endorsement information of badge.

return: bool|int A status/ID when insert or update data.
param: stdClass $endorsement Data of an endorsement.

get_endorsement()   X-Ref
Get endorsement of badge.

return: array|stdClass Endorsement information.

markdown_badge_criteria()   X-Ref
Markdown language support for criteria.

return: string $output Markdown content to output.

get_badge_issuer(?int $obversion = null)   X-Ref
Define issuer information by format Open Badges specification version 2.

return: array Issuer informations of the badge.
param: int $obversion OB version to use.