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

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