BioTorrents.de’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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types = 1);
  3. class Badges
  4. {
  5. /**
  6. * Given a UserID, returns that user's badges
  7. *
  8. * @param int $UserID
  9. * @return array of BadgeIDs
  10. */
  11. public static function get_badges($UserID)
  12. {
  13. return Users::user_info($UserID)['Badges'];
  14. }
  15. /**
  16. * Awards UserID the given BadgeID
  17. *
  18. * @param int $UserID
  19. * @param int $BadgeID
  20. * @return bool success?
  21. */
  22. public static function award_badge($UserID, $BadgeID)
  23. {
  24. if (self::has_badge($UserID, $BadgeID)) {
  25. return false;
  26. } else {
  27. $QueryID = G::$DB->get_query_id();
  28. G::$DB->query("
  29. INSERT INTO `users_badges`(`UserID`, `BadgeID`)
  30. VALUES($UserID, $BadgeID)
  31. ");
  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)) ?: false;
  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='badge tooltip' alt='$Name' title='$Name: $Description' src='$Icon' /></a>";
  89. } else {
  90. $html .= "<a class='badge_icon'><img class='badge' alt='$Name' title='$Name' src='$Icon' /></a>";
  91. }
  92. return $html;
  93. }
  94. /**
  95. * display_badges()
  96. */
  97. public static function display_badges($BadgeIDs, $Tooltip = false)
  98. {
  99. $html = '';
  100. foreach ($BadgeIDs as $BadgeID) {
  101. $html .= self::display_badge($BadgeID, $Tooltip);
  102. }
  103. return $html;
  104. }
  105. /**
  106. * update_badge_cache()
  107. */
  108. private static function update_badge_cache()
  109. {
  110. $QueryID = G::$DB->get_query_id();
  111. G::$DB->query("
  112. SELECT
  113. `ID`,
  114. `Icon`,
  115. `Name`,
  116. `Description`
  117. FROM
  118. `badges`
  119. ");
  120. $badges = [];
  121. if (G::$DB->has_results()) {
  122. while (list($id, $icon, $name, $description) = G::$DB->next_record()) {
  123. $badges[$id] = array('Icon' => $icon, 'Name' => $name, 'Description' => $description);
  124. }
  125. G::$Cache->cache_value('badges', $badges);
  126. }
  127. G::$DB->set_query_id($QueryID);
  128. }
  129. /**
  130. * get_all_badges()
  131. */
  132. public static function get_all_badges()
  133. {
  134. if (($Badges = G::$Cache->get_value('badges'))) {
  135. return $Badges;
  136. } else {
  137. self::update_badge_cache();
  138. return G::$Cache->get_value('badges');
  139. }
  140. }
  141. }