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:  * Reactions are the actions a user takes against another user's content
  6:  *
  7:  * Events: AfterReactionSave
  8:  *
  9:  * @package Yaga
 10:  * @since 1.0
 11:  */
 12: 
 13: class ReactionModel extends Gdn_Model {
 14: 
 15:   /**
 16:    * Used to cache the reactions
 17:    * @var array
 18:    */
 19:   private static $_Reactions = array();
 20: 
 21:   /**
 22:    * Defines the related database table name.
 23:    */
 24:   public function __construct() {
 25:     parent::__construct('Reaction');
 26:   }
 27: 
 28:   /**
 29:    * Returns all available actions along with the current count specified by
 30:    * the $ID and $Type of content.
 31:    * 
 32:    * @param int $ID
 33:    * @param string $Type
 34:    * @return DataSet
 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:    * Returns the reaction records associated with the specified user content.
 50:    *
 51:    * @param int $ID
 52:    * @param string $Type is the kind of ID. Valid: comment, discussion, activity
 53:    * @return mixed DataSet if it exists, NULL otherwise
 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:    * Return a list of reactions a user has received
 74:    *
 75:    * @param int $ID
 76:    * @param string $Type activity, comment, discussion
 77:    * @param int $UserID
 78:    * @return DataSet
 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:    * Return the count of reactions received by a user
 93:    *
 94:    * @param int $UserID
 95:    * @param int $ActionID
 96:    * @return DataSet
 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:    * Return the count of actions taken by a user
109:    *
110:    * @param int $UserID
111:    * @param int $ActionID
112:    * @return DataSet
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:    * Sets a users reaction against another user's content. A user can only react
125:    * in one way to each unique piece of content. This function makes sure to
126:    * enforce this rule
127:    *
128:    * Events: AfterReactionSave
129:    *
130:    * @param int $ID
131:    * @param string $Type activity, comment, discussion
132:    * @param int $AuthorID
133:    * @param int $UserID
134:    * @param int $ActionID
135:    * @return DataSet
136:    */
137:   public function Set($ID, $Type, $AuthorID, $UserID, $ActionID) {
138:     // clear the cache
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:         // remove the record
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:         // update the record
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:       // insert a record
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:     // Update the parent item score
187:     $this->SetUserScore($ID, $Type, $UserID, $Score);
188:     // Give the user points commesurate with reaction activity
189:     UserModel::GivePoints($AuthorID, $Points, 'Reaction');
190:     $this->FireEvent('AfterReactionSave', $EventArgs);
191:     return $Reaction;
192:   }
193: 
194:   /**
195:    * This updates the items score for future use in ranking and a best of controller
196:    *
197:    * @param int $ID The items ID
198:    * @param string $Type The type of the item (only supports 'discussion' and 'comment'
199:    * @param int $UserID The user that is scoring the item
200:    * @param int $Score What they give it
201:    * @return boolean Whether or not the the request was successful
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: 
Yaga API documentation generated by ApiGen 2.8.0