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