1: <?php if (!defined('APPLICATION')) exit();
2:
3:
4: 5: 6: 7: 8: 9: 10: 11:
12:
13: class BadgeModel extends Gdn_Model {
14:
15: 16: 17: 18:
19: private static $_Badges = NULL;
20:
21: 22: 23:
24: public function __construct() {
25: parent::__construct('Badge');
26: }
27:
28: 29: 30: 31: 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: 47: 48: 49: 50: 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: 64: 65:
66: public function GetCount() {
67: return count($this->Get());
68: }
69:
70: 71: 72: 73: 74: 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: 88: 89: 90: 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: 103: 104: 105: 106: 107:
108: public function Delete($BadgeID) {
109: $Badge = $this->GetByID($BadgeID);
110: if(!empty($Badge)) {
111: try {
112: $this->Database->BeginTransaction();
113:
114: $this->SQL->Delete('Badge', array('BadgeID' => $BadgeID));
115:
116:
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:
126: $this->SQL->Update('User')
127: ->Set('CountBadges', 'CountBadges - 1', FALSE)
128: ->Where('UserID', $UserIDs)
129: ->Put();
130:
131:
132: foreach($UserIDs as $UserID) {
133: UserModel::GivePoints($UserID, -1 * $Badge->AwardValue, 'Badge');
134: }
135:
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: 150: 151: 152: 153: 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: