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.8KB

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