123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- declare(strict_types = 1);
-
- class Badges
- {
- /**
- * Given a UserID, returns that user's badges
- *
- * @param int $UserID
- * @return array of BadgeIDs
- */
- public static function get_badges($UserID)
- {
- return Users::user_info($UserID)['Badges'];
- }
-
-
- /**
- * Awards UserID the given BadgeID
- *
- * @param int $UserID
- * @param int $BadgeID
- * @return bool success?
- */
- public static function award_badge($UserID, $BadgeID)
- {
- if (self::has_badge($UserID, $BadgeID)) {
- return false;
- } else {
- $QueryID = G::$DB->get_query_id();
- G::$DB->query("
- INSERT INTO `users_badges`(`UserID`, `BadgeID`)
- VALUES($UserID, $BadgeID)
- ");
-
- G::$DB->set_query_id($QueryID);
- G::$Cache->delete_value("user_info_$UserID");
- return true;
- }
- }
-
-
- /**
- * Given a UserID, return that user's displayed badges
- *
- * @param int $UserID
- * @return array of BadgeIDs
- */
- public static function get_displayed_badges($UserID)
- {
- $Result = [];
- $Badges = self::get_badges($UserID);
-
- foreach ($Badges as $Badge => $Displayed) {
- if ($Displayed) {
- $Result[] = $Badge;
- }
- }
- return $Result;
- }
-
-
- /**
- * Returns true if the given user owns the given badge
- *
- * @param int $UserID
- * @param int $BadgeID
- * @return bool
- */
- public static function has_badge($UserID, $BadgeID)
- {
- $Badges = self::get_badges($UserID);
- return (array_key_exists($BadgeID, $Badges)) ?: false;
- }
-
-
- /**
- * Creates HTML for displaying a badge.
- *
- * @param int $BadgeID
- * @param bool $Tooltip Should HTML contain a tooltip?
- * @return string HTML
- */
- public static function display_badge($BadgeID, $Tooltip = false)
- {
- $html = '';
- if (($Badges = G::$Cache->get_value('badges')) && array_key_exists($BadgeID, $Badges)) {
- extract($Badges[$BadgeID]);
- } else {
- self::update_badge_cache();
- if (($Badges = G::$Cache->get_value('badges')) && array_key_exists($BadgeID, $Badges)) {
- extract($Badges[$BadgeID]);
- } else {
- global $Debug;
- $Debug->analysis("Invalid BadgeID $BadgeID requested.");
- }
- }
-
- if ($Tooltip) {
- $html .= "<a class='badge_icon'><img class='badge tooltip' alt='$Name' title='$Name: $Description' src='$Icon' /></a>";
- } else {
- $html .= "<a class='badge_icon'><img class='badge' alt='$Name' title='$Name' src='$Icon' /></a>";
- }
-
- return $html;
- }
-
-
- /**
- * display_badges()
- */
- public static function display_badges($BadgeIDs, $Tooltip = false)
- {
- $html = '';
- foreach ($BadgeIDs as $BadgeID) {
- $html .= self::display_badge($BadgeID, $Tooltip);
- }
- return $html;
- }
-
-
- /**
- * update_badge_cache()
- */
- private static function update_badge_cache()
- {
- $QueryID = G::$DB->get_query_id();
-
- G::$DB->query("
- SELECT
- `ID`,
- `Icon`,
- `Name`,
- `Description`
- FROM
- `badges`
- ");
-
- $badges = [];
- if (G::$DB->has_results()) {
- while (list($id, $icon, $name, $description) = G::$DB->next_record()) {
- $badges[$id] = array('Icon' => $icon, 'Name' => $name, 'Description' => $description);
- }
- G::$Cache->cache_value('badges', $badges);
- }
-
- G::$DB->set_query_id($QueryID);
- }
-
-
- /**
- * get_all_badges()
- */
- public static function get_all_badges()
- {
- if (($Badges = G::$Cache->get_value('badges'))) {
- return $Badges;
- } else {
- self::update_badge_cache();
- return G::$Cache->get_value('badges');
- }
- }
- }
|