1: <?php if (!defined('APPLICATION')) exit();
2:
3:
4: 5: 6: 7: 8: 9: 10: 11:
12: class BadgeAwardModel extends Gdn_Model {
13:
14: 15: 16:
17: public function __construct() {
18: parent::__construct('BadgeAward');
19: }
20:
21: 22: 23: 24: 25: 26:
27: public function GetCount($BadgeID = FALSE) {
28: if($BadgeID) {
29: $Wheres = array('BadgeID' => $BadgeID);
30: }
31: else {
32: $Wheres = array();
33: }
34: return $this->SQL->GetCount('BadgeAward', $Wheres);
35: }
36:
37: 38: 39: 40: 41: 42: 43:
44: public function GetRecent($BadgeID, $Limit = 15) {
45: return $this->SQL
46: ->Select('ba.UserID, ba.DateInserted, u.Name, u.Photo, u.Gender, u.Email')
47: ->From('BadgeAward ba')
48: ->Join('User u', 'ba.UserID = u.UserID')
49: ->Where('BadgeID', $BadgeID)
50: ->OrderBy('DateInserted', 'Desc')
51: ->Limit($Limit)
52: ->Get()
53: ->Result();
54: }
55:
56: 57: 58: 59: 60: 61: 62: 63:
64: public function Award($BadgeID, $UserID, $InsertUserID = NULL, $Reason = '') {
65: $Badge = Yaga::BadgeModel()->GetByID($BadgeID);
66: if(!empty($Badge)) {
67: if(!$this->Exists($UserID, $BadgeID)) {
68: $this->SQL->Insert('BadgeAward', array(
69: 'BadgeID' => $BadgeID,
70: 'UserID' => $UserID,
71: 'InsertUserID' => $InsertUserID,
72: 'Reason' => $Reason,
73: 'DateInserted' => date(DATE_ISO8601)
74: ));
75:
76:
77: UserModel::GivePoints($UserID, $Badge->AwardValue, 'Badge');
78:
79:
80: $this->SQL->Update('User')
81: ->Set('CountBadges', 'CountBadges + 1', FALSE)
82: ->Where('UserID', $UserID)
83: ->Put();
84:
85: if(is_null($InsertUserID)) {
86: $InsertUserID = Gdn::Session()->UserID;
87: }
88:
89:
90: $ActivityModel = new ActivityModel();
91:
92: $Activity = array(
93: 'ActivityType' => 'BadgeAward',
94: 'ActivityUserID' => $UserID,
95: 'RegardingUserID' => $InsertUserID,
96: 'Photo' => $Badge->Photo,
97: 'RecordType' => 'Badge',
98: 'RecordID' => $BadgeID,
99: 'Route' => '/badges/detail/' . $Badge->BadgeID . '/' . Gdn_Format::Url($Badge->Name),
100: 'HeadlineFormat' => T('Yaga.HeadlineFormat.BadgeEarned'),
101: 'Data' => array(
102: 'Name' => $Badge->Name
103: ),
104: 'Story' => $Badge->Description
105: );
106:
107:
108: $ActivityModel->Queue($Activity, FALSE);
109:
110: $Activity['NotifyUserID'] = $UserID;
111: $ActivityModel->Queue($Activity, 'BadgeAward', array('Force' => TRUE));
112:
113:
114: $ActivityModel->SaveQueue();
115:
116: $this->EventArguments['UserID'] = $UserID;
117: $this->FireEvent('AfterBadgeAward');
118: }
119: }
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: public function Exists($UserID, $BadgeID) {
131: return $this->SQL
132: ->Select()
133: ->From('BadgeAward')
134: ->Where('BadgeID', $BadgeID)
135: ->Where('UserID', $UserID)
136: ->Get()
137: ->FirstRow();
138: }
139:
140: 141: 142: 143: 144: 145: 146:
147: public function GetByUser($UserID, $DataType = DATASET_TYPE_ARRAY) {
148: return $this->SQL
149: ->Select()
150: ->From('Badge b')
151: ->Join('BadgeAward ba', 'ba.BadgeID = b.BadgeID', 'left')
152: ->Where('ba.UserID', $UserID)
153: ->Get()
154: ->Result($DataType);
155: }
156:
157: 158: 159: 160: 161: 162:
163: public function GetUnobtained($UserID) {
164: $Px = $this->Database->DatabasePrefix;
165: $Sql = 'select b.BadgeID, b.Enabled, b.RuleClass, b.RuleCriteria, '
166: . 'ba.UserID '
167: . "from {$Px}Badge as b "
168: . "left join {$Px}BadgeAward as ba ON b.BadgeID = ba.BadgeID and ba.UserID = :UserID ";
169:
170: return $this->Database->Query($Sql, array(':UserID' => $UserID))->Result();
171: }
172:
173: 174: 175: 176: 177: 178: 179: 180:
181: public function Counts($Column, $UserID = NULL) {
182: if($UserID) {
183: $Where = array('UserID' => $UserID);
184: }
185: else {
186: $Where = NULL;
187: }
188:
189: $Result = array('Complete' => TRUE);
190: switch($Column) {
191: case 'CountBadges':
192: Gdn::Database()->Query(DBAModel::GetCountSQL('count', 'User', 'BadgeAward', 'CountBadges', 'BadgeAwardID', 'UserID', 'UserID', $Where));
193: break;
194: default:
195: throw new Gdn_UserException("Unknown column $Column");
196: }
197: return $Result;
198: }
199:
200: }
201: