Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

badges.class.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?
  2. class Badges {
  3. /**
  4. * Given a UserID, returns that user's badges
  5. *
  6. * @param int $UserID
  7. * @return array of BadgeIDs
  8. */
  9. public static function get_badges($UserID) {
  10. return Users::user_info($UserID)['Badges'];
  11. }
  12. /**
  13. * Awards UserID the given BadgeID
  14. *
  15. * @param int $UserID
  16. * @param int $BadgeID
  17. * @return bool success?
  18. */
  19. public static function award_badge($UserID, $BadgeID) {
  20. if (self::has_badge($UserID, $BadgeID)) {
  21. return false;
  22. } else {
  23. $QueryID = G::$DB->get_query_id();
  24. G::$DB->query("
  25. INSERT INTO users_badges
  26. (UserID, BadgeID)
  27. VALUES
  28. ($UserID, $BadgeID)");
  29. G::$DB->set_query_id($QueryID);
  30. G::$Cache->delete_value('user_info_'.$UserID);
  31. return true;
  32. }
  33. }
  34. /**
  35. * Given a UserID, return that user's displayed badges
  36. *
  37. * @param int $UserID
  38. * @return array of BadgeIDs
  39. */
  40. public static function get_displayed_badges($UserID) {
  41. $Result = [];
  42. $Badges = self::get_badges($UserID);
  43. foreach ($Badges as $Badge => $Displayed) {
  44. if ($Displayed)
  45. $Result[] = $Badge;
  46. }
  47. return $Result;
  48. }
  49. /**
  50. * Returns true if the given user owns the given badge
  51. *
  52. * @param int $UserID
  53. * @param int $BadgeID
  54. * @return bool
  55. */
  56. public static function has_badge($UserID, $BadgeID) {
  57. $Badges = self::get_badges($UserID);
  58. return array_key_exists($BadgeID, $Badges);
  59. }
  60. /**
  61. * Creates HTML for displaying a badge.
  62. *
  63. * @param int $BadgeID
  64. * @param bool $Tooltip Should HTML contain a tooltip?
  65. * @return string HTML
  66. */
  67. public static function display_badge($BadgeID, $Tooltip = false) {
  68. $html = "";
  69. if (($Badges = G::$Cache->get_value('badges')) && array_key_exists($BadgeID, $Badges)) {
  70. extract($Badges[$BadgeID]);
  71. } else {
  72. self::update_badge_cache();
  73. if (($Badges = G::$Cache->get_value('badges')) && array_key_exists($BadgeID, $Badges)) {
  74. extract($Badges[$BadgeID]);
  75. } else {
  76. global $Debug;
  77. $Debug->analysis('Invalid BadgeID ' . $BadgeID . ' requested.');
  78. }
  79. }
  80. if ($Tooltip) {
  81. $html .= '<a class="badge_icon"><img class="tooltip" alt="'.$Name.'" title="'.$Name.'</br>'.$Description.'" src="'.$Icon.'" /></a>';
  82. } else {
  83. $html .= '<a class="badge_icon"><img alt="'.$Name.'" title="'.$Name.'" src="'.$Icon.'" /></a>';
  84. }
  85. return $html;
  86. }
  87. public static function display_badges($BadgeIDs, $Tooltip = false) {
  88. $html = "";
  89. foreach ($BadgeIDs as $BadgeID) {
  90. $html .= self::display_badge($BadgeID, $Tooltip);
  91. }
  92. return $html;
  93. }
  94. private static function update_badge_cache() {
  95. $QueryID = G::$DB->get_query_id();
  96. G::$DB->query("
  97. SELECT
  98. ID, Icon, Name, Description
  99. FROM badges");
  100. $badges = [];
  101. if (G::$DB->has_results()) {
  102. while(list($id, $icon, $name, $description) = G::$DB->next_record()) {
  103. $badges[$id] = array('Icon' => $icon, 'Name' => $name, 'Description' => $Description);
  104. }
  105. G::$Cache->cache_value('badges', $badges);
  106. }
  107. G::$DB->set_query_id($QueryID);
  108. }
  109. public static function get_all_badges() {
  110. if (($Badges = G::$Cache->get_value('badges'))) {
  111. return $Badges;
  112. } else {
  113. self::update_badge_cache();
  114. return G::$Cache->get_value('badges');
  115. }
  116. }
  117. }
  118. ?>