From 424714c500a49898ddc44d6720d30c365a830f05 Mon Sep 17 00:00:00 2001 From: Zachary Doll Date: Mon, 18 Nov 2013 11:10:18 -0600 Subject: [PATCH] Reactions now apply their award value to the parent's row in the score column --- controllers/class.reactcontroller.php | 2 +- models/class.reactionmodel.php | 58 ++++++++++++++++++++++++++- settings/class.hooks.php | 15 +------ 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/controllers/class.reactcontroller.php b/controllers/class.reactcontroller.php index 26ddfbb..ca2259f 100644 --- a/controllers/class.reactcontroller.php +++ b/controllers/class.reactcontroller.php @@ -170,7 +170,7 @@ class ReactController extends Gdn_Controller { } private function _RenderActions($ID, $Type, $Echo = TRUE) { - $Reactions = $this->ReactionModel->GetReactions($ID, $Type); + $Reactions = $this->ReactionModel->GetAllReactions($ID, $Type); $ActionsString = ''; foreach($Reactions as $Action) { $ActionsString .= Anchor( diff --git a/models/class.reactionmodel.php b/models/class.reactionmodel.php index 60e0395..4224bde 100644 --- a/models/class.reactionmodel.php +++ b/models/class.reactionmodel.php @@ -69,7 +69,7 @@ class ReactionModel extends Gdn_Model { * @param int $ID * @param enum $Type is the kind of ID. Valid: comment, discussion, activity */ - public function GetReactions($ID, $Type) { + public function GetAllReactions($ID, $Type) { if(in_array($Type, array('discussion', 'comment', 'activity')) && $ID > 0) { $ReactionSet = array(); if(empty(self::$_Reactions[$Type . $ID])) { @@ -109,6 +109,22 @@ class ReactionModel extends Gdn_Model { return NULL; } } + + /** + * @todo document this + * @param type $ID + * @param type $Type + */ + public function GetReactions($ID, $Type) { + return $this->SQL-> + Select('a.ActionID, a.AwardValue, COUNT(a.ActionID) as Count') + ->From('Action a') + ->Join('Reaction r', 'a.ActionID = r.ActionID', 'left') + ->Where('r.ParentID', $ID) + ->Where('r.ParentType', $Type) + ->GroupBy('a.ActionID') + ->Get(); + } /** * Return a list of reactions a user has received @@ -201,7 +217,47 @@ class ReactionModel extends Gdn_Model { $EventArgs['Exists'] = TRUE; } + $this->CalculateScore($ID, $Type); $this->FireEvent('AfterReactionSave', $EventArgs); return $Reaction; } + + /** + * @todo document + * @param type $ID + * @param type $Type + * @param type $Increment + * @return type + */ + private function CalculateScore($ID, $Type) { + // Activities don't have scores + if($Type == 'activity') { + return; + } + + $Reactions = $this->GetReactions($ID, $Type); + $Score = 0; + foreach($Reactions as $Reaction) { + $Score += $Reaction->AwardValue * $Reaction->Count; + } + switch($Type) { + default: + return; + case 'discussion': + $this->SQL + ->Update('Discussion') + ->Set('Score', $Score) + ->Where('DiscussionID', $ID) + ->Put(); + break; + case 'comment': + $this->SQL + ->Update('Comment') + ->Set('Score', $Score) + ->Where('CommentID', $ID) + ->Put(); + break; + } + return TRUE; + } } \ No newline at end of file diff --git a/settings/class.hooks.php b/settings/class.hooks.php index bf18032..6dd23b8 100755 --- a/settings/class.hooks.php +++ b/settings/class.hooks.php @@ -31,17 +31,6 @@ class YagaHooks implements Gdn_IPlugin { } } - /** - * Display points in the user info list - * @param object $Sender - * @todo Use the unused Points column in the User Table to store the points - */ - public function UserInfoModule_OnBasicInfo_Handler($Sender) { - $Model = new YagaModel(); - $Points = $Model->GetUserPoints($Sender->User->UserID); - echo Wrap(T('Yaga.Points', 'Points'), 'dt') . ' ' . Wrap($Points, 'dd'); - } - /** * Display the reaction counts on the profile page * @param object $Sender @@ -135,7 +124,7 @@ class YagaHooks implements Gdn_IPlugin { $this->_ReactionModel = new ReactionModel(); } - $Reactions = $this->_ReactionModel->GetReactions($ID, $Type); + $Reactions = $this->_ReactionModel->GetAllReactions($ID, $Type); foreach($Reactions as $Reaction) { if($Reaction->UserIDs) { foreach($Reaction->UserIDs as $Index => $UserID) { @@ -226,7 +215,7 @@ class YagaHooks implements Gdn_IPlugin { $this->_ReactionModel = new ReactionModel(); } - $Reactions = $this->_ReactionModel->GetReactions($ID, $Type); + $Reactions = $this->_ReactionModel->GetAllReactions($ID, $Type); $ActionsString = ''; foreach($Reactions as $Action) { if(CheckPermission($Action->Permission)) {