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.
/lib/ -> badgeslib.php (source)

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 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.

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

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

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

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

return: array $badge Array of records matching criteria
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

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

return: array of badges ordered by decreasing date of issue
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

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.

return: boolean
param: object      $data

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.

return: string|url Returns either new file path hash or new file URL
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).

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.

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

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.

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

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

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

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.

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

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

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

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

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

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

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

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.

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

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

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

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

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

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)

return: array(stdClass)
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

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

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

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.

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

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

return: mixed The id or false if it doesn't exist.
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.

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

return: boolean
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.

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

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

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

return: boolean
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.

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.

return: true if the email was sent successfully, false otherwise.
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

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

return: array
param: boolean $enabled

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

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

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.

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

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.

return: string The public URL to access Badgr objects
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