1: <?php if (!defined('APPLICATION')) exit();
2:
3:
4: 5: 6: 7: 8: 9: 10: 11:
12:
13: class ReactionModel extends Gdn_Model {
14:
15: 16: 17: 18:
19: private static $_Reactions = array();
20:
21: 22: 23:
24: public function __construct() {
25: parent::__construct('Reaction');
26: }
27:
28: 29: 30: 31: 32: 33: 34: 35:
36: public function GetList($ID, $Type) {
37: $Px = $this->Database->DatabasePrefix;
38: $Sql = "select a.*, "
39: . "(select count(r.ReactionID) "
40: . "from {$Px}Reaction as r "
41: . "where r.ParentID = :ParentID and r.ParentType = :ParentType "
42: . "and r.ActionID = a.ActionID) as Count "
43: . "from {$Px}Action AS a "
44: . "order by a.Sort";
45:
46: return $this->Database->Query($Sql, array(':ParentID' => $ID, ':ParentType' => $Type))->Result();
47: }
48: 49: 50: 51: 52: 53: 54:
55: public function GetRecord($ID, $Type) {
56: if(in_array($Type, array('discussion', 'comment', 'activity')) && $ID > 0) {
57: return $this->SQL
58: ->Select('a.*, r.InsertUserID as UserID, r.DateInserted')
59: ->From('Action a')
60: ->Join('Reaction r', 'a.ActionID = r.ActionID')
61: ->Where('r.ParentID', $ID)
62: ->Where('r.ParentType', $Type)
63: ->OrderBy('r.DateInserted')
64: ->Get()
65: ->Result();
66: }
67: else {
68: return NULL;
69: }
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79:
80: public function GetByUser($ID, $Type, $UserID) {
81: return $this->SQL
82: ->Select()
83: ->From('Reaction')
84: ->Where('ParentID', $ID)
85: ->Where('ParentType', $Type)
86: ->Where('InsertUserID', $UserID)
87: ->Get()
88: ->FirstRow();
89: }
90:
91: 92: 93: 94: 95: 96: 97:
98: public function GetUserCount($UserID, $ActionID) {
99: return $this->SQL
100: ->Select()
101: ->From('Reaction')
102: ->Where('ActionID', $ActionID)
103: ->Where('ParentAuthorID', $UserID)
104: ->GetCount();
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public function GetUserTakenCount($UserID, $ActionID) {
115: return $this->SQL
116: ->Select()
117: ->From('Reaction')
118: ->Where('ActionID', $ActionID)
119: ->Where('InsertUserID', $UserID)
120: ->GetCount();
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136:
137: public function Set($ID, $Type, $AuthorID, $UserID, $ActionID) {
138:
139: unset(self::$_Reactions[$Type . $ID]);
140:
141: $EventArgs = array('ParentID' => $ID, 'ParentType' => $Type, 'ParentUserID' => $AuthorID, 'InsertUserID' => $UserID, 'ActionID' => $ActionID);
142: $ActionModel = Yaga::ActionModel();
143: $NewAction = $ActionModel->GetByID($ActionID);
144: $Points = $Score = $NewAction->AwardValue;
145: $CurrentReaction = $this->GetByUser($ID, $Type, $UserID);
146: if($CurrentReaction) {
147: $OldAction = $ActionModel->GetByID($CurrentReaction->ActionID);
148:
149: if($ActionID == $CurrentReaction->ActionID) {
150:
151: $Reaction = $this->SQL->Delete('Reaction', array('ParentID' => $ID,
152: 'ParentType' => $Type,
153: 'InsertUserID' => $UserID,
154: 'ActionID' => $ActionID));
155: $EventArgs['Exists'] = FALSE;
156: $Score = 0;
157: $Points = -1 * $OldAction->AwardValue;
158: }
159: else {
160:
161: $Reaction = $this->SQL
162: ->Update('Reaction')
163: ->Set('ActionID', $ActionID)
164: ->Set('DateInserted', date(DATE_ISO8601))
165: ->Where('ParentID', $ID)
166: ->Where('ParentType', $Type)
167: ->Where('InsertUserID', $UserID)
168: ->Put();
169: $EventArgs['Exists'] = TRUE;
170: $Points = -1 * ($OldAction->AwardValue - $Points);
171: }
172: }
173: else {
174:
175: $Reaction = $this->SQL
176: ->Insert('Reaction',
177: array('ActionID' => $ActionID,
178: 'ParentID' => $ID,
179: 'ParentType' => $Type,
180: 'ParentAuthorID' => $AuthorID,
181: 'InsertUserID' => $UserID,
182: 'DateInserted' => date(DATE_ISO8601)));
183: $EventArgs['Exists'] = TRUE;
184: }
185:
186:
187: $this->SetUserScore($ID, $Type, $UserID, $Score);
188:
189: UserModel::GivePoints($AuthorID, $Points, 'Reaction');
190: $this->FireEvent('AfterReactionSave', $EventArgs);
191: return $Reaction;
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201: 202:
203: private function SetUserScore($ID, $Type, $UserID, $Score) {
204: $Model = FALSE;
205: switch($Type) {
206: default:
207: return FALSE;
208: case 'discussion':
209: $Model = new DiscussionModel();
210: break;
211: case 'comment':
212: $Model = new CommentModel();
213: break;
214: }
215:
216: if($Model) {
217: $Model->SetUserScore($ID, $UserID, $Score);
218: return TRUE;
219: }
220: else {
221: return FALSE;
222: }
223: }
224: }
225: