Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/lib/ -> badgeslib.php (source)

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

Contains classes, functions and constants used in badges.

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: 1374 lines (49 kb)
Included or required:0 times
Referenced: 8 times
Includes or requires: 1 file
 badges/lib/bakerlib.php

Defines 43 functions


Functions that are not part of a class:

badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash)   X-Ref
Sends notifications to users about awarded badges.

param: badge $badge Badge that was issued
param: int $userid Recipient ID
param: string $issued Unique hash of an issued badge
param: string $filepathhash File path hash of an issued badge for attachments

badges_calculate_message_schedule($schedule)   X-Ref
Caclulates date for the next message digest to badge creators.

param: in $schedule Type of message schedule BADGE_MESSAGE_DAILY|BADGE_MESSAGE_WEEKLY|BADGE_MESSAGE_MONTHLY.
return: int Timestamp for next cron

badge_message_from_template($message, $params)   X-Ref
Replaces variables in a message template and returns text ready to be emailed to a user.

param: string $message Message body.
return: string Message with replaced values

badges_get_badges($type, $courseid = 0, $sort = '', $dir = '', $page = 0, $perpage = BADGE_PERPAGE, $user = 0)   X-Ref
Get all badges.

param: int Type of badges to return
param: int Course ID for course badges
param: string $sort An SQL field to sort by
param: string $dir The sort direction ASC|DESC
param: int $page The page or records to return
param: int $perpage The number of records to return per page
param: int $user User specific search
return: array $badge Array of records matching criteria

badges_get_user_badges($userid, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false)   X-Ref
Get badges for a specific user.

param: int $userid User ID
param: int $courseid Badges earned by a user in a specific course
param: int $page The page or records to return
param: int $perpage The number of records to return per page
param: string $search A simple string to search for
param: bool $onlypublic Return only public badges
return: array of badges ordered by decreasing date of issue

badges_add_course_navigation(navigation_node $coursenode, stdClass $course)   X-Ref
Extends the course administration navigation with the Badges page

param: navigation_node $coursenode
param: object $course

badges_award_handle_manual_criteria_review(stdClass $data)   X-Ref
Triggered when badge is manually awarded.

param: object      $data
return: boolean

badges_process_badge_image(badge $badge, $iconfile)   X-Ref
Process badge image from form data

param: badge $badge Badge object
param: string $iconfile Original file

print_badge_image(badge $badge, stdClass $context, $size = 'small')   X-Ref
Print badge image.

param: badge $badge Badge object
param: stdClass $context
param: string $size

badges_bake($hash, $badgeid, $userid = 0, $pathhash = false)   X-Ref
Bake issued badge.

param: string $hash Unique hash of an issued badge.
param: int $badgeid ID of the original badge.
param: int $userid ID of badge recipient (optional).
param: boolean $pathhash Return file pathhash instead of image url (optional).
return: string|url Returns either new file path hash or new file URL

get_backpack_settings($userid, $refresh = false)   X-Ref
Returns external backpack settings and badges from this backpack.

This function first checks if badges for the user are cached and
tries to retrieve them from the cache. Otherwise, badges are obtained
through curl request to the backpack.

param: int $userid Backpack user ID.
param: boolean $refresh Refresh badges collection in cache.
return: null|object Returns null is there is no backpack or object with backpack settings.

badges_download($userid)   X-Ref
Download all user badges in zip archive.

param: int $userid ID of badge owner.

badges_check_backpack_accessibility()   X-Ref
Checks if badges can be pushed to external backpack.

return: string Code of backpack accessibility status.

badges_user_has_backpack($userid)   X-Ref
Checks if user has external backpack connected.

param: int $userid ID of a user.
return: bool True|False whether backpack connection exists.

badges_handle_course_deletion($courseid)   X-Ref
Handles what happens to the course badges when a course is deleted.

param: int $courseid course ID.
return: void.

badges_setup_backpack_js()   X-Ref
Loads JS files required for backpack support.

return: void

badges_local_backpack_js($checksite = false)   X-Ref
No js files are required for backpack support.
This only exists to directly support the custom V1 backpack api.

param: boolean $checksite Call check site function.
return: void

badges_create_site_backpack($data)   X-Ref
Create the site backpack with this data.

param: stdClass $data The new backpack data.
return: boolean

badges_update_site_backpack($id, $data)   X-Ref
Update the backpack with this id.

param: integer $id The backpack to edit
param: stdClass $data The new backpack data.
return: boolean

badges_delete_site_backpack($id)   X-Ref
Delete the backpack with this id.

param: integer $id The backpack to delete.
return: boolean

badges_save_external_backpack(stdClass $data)   X-Ref
Perform the actual create/update of external bakpacks. Any checks on the validity of the id will need to be
performed before it reaches this function.

param: stdClass $data The backpack data we are updating/inserting
return: int Returns the id of the new/updated record

badges_save_backpack_credentials(stdClass $data)   X-Ref
Create a backpack with the provided details. Stores the auth details of the backpack

param: stdClass $data Backpack specific data.
return: int The id of the external backpack that the credentials correspond to

badges_open_badges_backpack_api(?int $backpackid = null)   X-Ref
Is any backpack enabled that supports open badges V1?

param: int|null $backpackid Check the version of the given id OR if null the sitewide backpack
return: boolean

badges_get_site_backpack($id, int $userid = 0)   X-Ref
Get a site backpacks by id for a particular user or site (if userid is 0)

param: int $id The backpack id.
param: int $userid The owner of the backpack, 0 if it's a sitewide backpack else a user's site backpack
return: array(stdClass)

badges_get_user_backpack(?int $userid = 0)   X-Ref
Get the user backpack for the currently logged in user OR the provided user

param: int|null $userid The user whose backpack you're requesting for. If null, get the logged in user's backpack
return: mixed The user's backpack or none.

badges_get_site_primary_backpack()   X-Ref
Get the primary backpack for the site

return: array(stdClass)

badges_get_site_backpacks()   X-Ref
List the backpacks at site level.

return: array(stdClass)

badges_change_sortorder_backpacks(int $backpackid, int $direction)   X-Ref
Moves the backpack in the list one position up or down.

param: int $backpackid The backpack identifier to be moved.
param: int $direction The direction (BACKPACK_MOVE_UP/BACKPACK_MOVE_DOWN) where to move the backpack.

badges_get_badge_api_versions()   X-Ref
List the supported badges api versions.

return: array(version)

badges_get_default_issuer()   X-Ref
Get the default issuer for a badge from this site.

return: array

badges_disconnect_user_backpack($userid)   X-Ref
Disconnect from the user backpack by deleting the user preferences.

param: integer $userid The user to diconnect.
return: boolean

badges_external_get_mapping($sitebackpackid, $type, $internalid, $param = 'externalid')   X-Ref
Used to remember which objects we connected with a backpack before.

param: integer $sitebackpackid The site backpack to connect to.
param: string $type The type of this remote object.
param: string $internalid The id for this object on the Moodle site.
param: string $param The param we need to return. Defaults to the externalid.
return: mixed The id or false if it doesn't exist.

badges_external_create_mapping($sitebackpackid, $type, $internalid, $externalid)   X-Ref
Save the info about which objects we connected with a backpack before.

param: integer $sitebackpackid The site backpack to connect to.
param: string $type The type of this remote object.
param: string $internalid The id for this object on the Moodle site.
param: string $externalid The id of this object on the remote site.
return: boolean

badges_external_delete_mappings($sitebackpackid)   X-Ref
Delete all external mapping information for a backpack.

param: integer $sitebackpackid The site backpack to connect to.
return: boolean

badges_external_delete_mapping($sitebackpackid, $type, $internalid)   X-Ref
Delete a specific external mapping information for a backpack.

param: integer $sitebackpackid The site backpack to connect to.
param: string $type The type of this remote object.
param: string $internalid The id for this object on the Moodle site.
return: boolean

badges_send_verification_email($email, $backpackid, $backpackpassword)   X-Ref
Create and send a verification email to the email address supplied.

Since we're not sending this email to a user, email_to_user can't be used
but this function borrows largely the code from that process.

param: string $email the email address to send the verification email to.
param: int $backpackid the id of the backpack to connect to
param: string $backpackpassword the user entered password to connect to this backpack
return: true if the email was sent successfully, false otherwise.

badges_list_criteria($enabled = true)   X-Ref
Return all the enabled criteria types for this site.

param: boolean $enabled
return: array

badge_award_criteria_competency_has_records_for_competencies($competencyids)   X-Ref
Check if any badge has records for competencies.

param: array $competencyids Array of competencies ids.
return: boolean Return true if competencies were found in any badge.

badge_assemble_notification(stdClass $badge)   X-Ref
Creates single message for all notification and sends it out

param: object $badge A badge which is notified about.

badges_verify_site_backpack()   X-Ref
Attempt to authenticate with the site backpack credentials and return an error
if the authentication fails. If external backpacks are not enabled, this will
not perform any test.

return: string

badges_verify_backpack(int $backpackid)   X-Ref
Attempt to authenticate with a backpack credentials and return an error
if the authentication fails.
If external backpacks are not enabled or the backpack version is different
from OBv2, this will not perform any test.

param: int $backpackid Backpack identifier to verify.
return: string The result of the verification process.

badges_get_oauth2_service_options()   X-Ref
Get OAuth2 services for the external backpack.

return: array

badges_generate_badgr_open_url($backpack, $type, $externalid)   X-Ref
Generate a public badgr URL that conforms to OBv2. This is done because badgr responses do not currently conform to
the spec.

WARNING: This is an extremely hacky way of implementing this and should be removed once the standards are conformed to.

param: stdClass $backpack The Badgr backpack we are pushing to
param: string $type The type of object we are dealing with either Issuer, Assertion OR Badge.
param: string $externalid The externalid as provided by the backpack
return: string The public URL to access Badgr objects