Overview

Packages

  • None
  • Yaga

Classes

  • ActedModel
  • ActionController
  • ActionModel
  • AwardCombo
  • BadgeAwardModel
  • BadgeController
  • BadgeModel
  • BadgesController
  • BadgesModule
  • BestController
  • BestFilterModule
  • CakeDayPost
  • CommentCount
  • CommentMarathon
  • DiscussionBodyLength
  • DiscussionCategory
  • DiscussionCount
  • DiscussionPageCount
  • HasMentioned
  • HolidayVisit
  • LeaderBoardModule
  • LengthOfService
  • ManualAward
  • NecroPost
  • NewbieComment
  • PhotoExists
  • PostCount
  • PostReactions
  • QnAAnserCount
  • RankController
  • RankModel
  • ReactController
  • ReactionCount
  • ReactionModel
  • ReflexComment
  • RulesController
  • SocialConnection
  • Yaga
  • YagaController
  • YagaHooks

Interfaces

  • YagaRule
  • Overview
  • Package
  • Class
  • Tree
  • Todo
  • Download
  1: <?php if (!defined('APPLICATION')) exit();
  2: /* Copyright 2013 Zachary Doll */
  3: 
  4: /**
  5:  * Describes badges and the associated rule criteria
  6:  *
  7:  * Events:
  8:  *
  9:  * @package Yaga
 10:  * @since 1.0
 11:  */
 12: 
 13: class BadgeModel extends Gdn_Model {
 14: 
 15:   /**
 16:    * Used as a cache
 17:    * @var DataSet
 18:    */
 19:   private static $_Badges = NULL;
 20: 
 21:   /**
 22:    * Defines the related database table name.
 23:    */
 24:   public function __construct() {
 25:     parent::__construct('Badge');
 26:   }
 27: 
 28:   /**
 29:    * Returns a list of all badges
 30:    *
 31:    * @return DataSet
 32:    */
 33:   public function Get() {
 34:     if(empty(self::$_Badges)) {
 35:       self::$_Badges = $this->SQL
 36:               ->Select()
 37:               ->From('Badge')
 38:               ->OrderBy('BadgeID')
 39:               ->Get()
 40:               ->Result();
 41:     }
 42:     return self::$_Badges;
 43:   }
 44: 
 45:   /**
 46:    * Gets the badge list with an optional limit and offset
 47:    * 
 48:    * @param int $Limit
 49:    * @param int $Offset
 50:    * @return DataSet
 51:    */
 52:   public function GetLimit($Limit = FALSE, $Offset = FALSE) {
 53:       return $this->SQL
 54:               ->Select()
 55:               ->From('Badge')
 56:               ->OrderBy('BadgeID')
 57:               ->Limit($Limit, $Offset)
 58:               ->Get()
 59:               ->Result();
 60:   }
 61: 
 62:   /**
 63:    * Total number of badges in the system
 64:    * @return int
 65:    */
 66:   public function GetCount() {
 67:     return count($this->Get());
 68:   }
 69: 
 70:   /**
 71:    * Returns data for a specific badge
 72:    *
 73:    * @param int $BadgeID
 74:    * @return DataSet
 75:    */
 76:   public function GetByID($BadgeID) {
 77:     $Badge = $this->SQL
 78:                     ->Select()
 79:                     ->From('Badge')
 80:                     ->Where('BadgeID', $BadgeID)
 81:                     ->Get()
 82:                     ->FirstRow();
 83:     return $Badge;
 84:   }
 85: 
 86:   /**
 87:    * Enable or disable a badge
 88:    *
 89:    * @param int $BadgeID
 90:    * @param bool $Enable
 91:    */
 92:   public function Enable($BadgeID, $Enable) {
 93:     $Enable = (!$Enable) ? FALSE : TRUE;
 94:     $this->SQL
 95:             ->Update('Badge')
 96:             ->Set('Enabled', $Enable)
 97:             ->Where('BadgeID', $BadgeID)
 98:             ->Put();
 99:   }
100: 
101:   /**
102:    * Remove a badge and associated awards
103:    *
104:    * @param int $BadgeID
105:    * @throws Exception
106:    * @return boolean
107:    */
108:   public function Delete($BadgeID) {
109:     $Badge = $this->GetByID($BadgeID);
110:     if(!empty($Badge)) {
111:       try {
112:         $this->Database->BeginTransaction();
113:         // Delete the badge
114:         $this->SQL->Delete('Badge', array('BadgeID' => $BadgeID));
115: 
116:         // Find the affected users
117:         $UserIDSet = $this->SQL->Select('UserID')
118:                 ->From('BadgeAward')
119:                 ->Where('BadgeID', $BadgeID)
120:                 ->Get()
121:                 ->Result();
122: 
123:         $UserIDs = ConsolidateArrayValuesByKey($UserIDSet, 'UserID');
124: 
125:         // Decrement their badge count
126:         $this->SQL->Update('User')
127:                 ->Set('CountBadges', 'CountBadges - 1', FALSE)
128:                 ->Where('UserID', $UserIDs)
129:                 ->Put();
130: 
131:         // Remove their points
132:         foreach($UserIDs as $UserID) {
133:           UserModel::GivePoints($UserID, -1 * $Badge->AwardValue, 'Badge');
134:         }
135:         // Remove the award rows
136:         $this->SQL->Delete('BadgeAward', array('BadgeID' => $BadgeID));
137: 
138:         $this->Database->CommitTransaction();
139:       } catch(Exception $Ex) {
140:         $this->Database->RollbackTransaction();
141:         throw $Ex;
142:       }
143:       return TRUE;
144:     }
145:     return FALSE;
146:   }
147: 
148:   /**
149:    * Get the full list of badges joined with the award data for a specific user
150:    * This shouldn't really be here, but I can't think of a good place to put it
151:    *
152:    * @param int $UserID
153:    * @return DataSet
154:    */
155:   public function GetWithEarned($UserID) {
156:     $Px = $this->Database->DatabasePrefix;
157:     $Sql = 'select b.BadgeID, b.Name, b.Description, b.Photo, b.AwardValue, '
158:             . 'ba.UserID, ba.InsertUserID, ba.Reason, ba.DateInserted, '
159:             . 'ui.Name AS InsertUserName '
160:             . "from {$Px}Badge as b "
161:             . "left join {$Px}BadgeAward as ba ON b.BadgeID = ba.BadgeID and ba.UserID = :UserID "
162:             . "left join {$Px}User as ui on ba.InsertUserID = ui.UserID";
163: 
164:     return $this->Database->Query($Sql, array(':UserID' => $UserID))->Result();
165:   }
166: 
167: }
168: 
Yaga API documentation generated by ApiGen 2.8.0