From 88f2308562e650d9fdb4a76a0fd183cc2041e4af Mon Sep 17 00:00:00 2001 From: Zachary Doll Date: Wed, 13 Nov 2013 10:41:39 -0600 Subject: [PATCH] Activity reactions fixing --- controllers/class.badgescontroller.php | 5 +++++ controllers/class.reactcontroller.php | 7 ++++--- models/class.badgemodel.php | 19 +++++++++++++++++++ models/class.reactionmodel.php | 8 ++++++-- rules/class.reactioncount.php | 22 ++++++++++------------ settings/class.hooks.php | 2 +- 6 files changed, 45 insertions(+), 18 deletions(-) diff --git a/controllers/class.badgescontroller.php b/controllers/class.badgescontroller.php index d3309e1..9389272 100644 --- a/controllers/class.badgescontroller.php +++ b/controllers/class.badgescontroller.php @@ -58,6 +58,11 @@ class BadgesController extends Gdn_Controller { * @param string $Slug */ public function Detail($BadgeID, $Slug = NULL) { + $UserID = Gdn::Session()->UserID; + $Badges = $this->BadgeModel->GetBadgesToCheckForUser($UserID); + decho($Badges); + + $this->Render('Blank', 'Utility', 'Dashboard'); } } diff --git a/controllers/class.reactcontroller.php b/controllers/class.reactcontroller.php index 658b53e..873ca34 100644 --- a/controllers/class.reactcontroller.php +++ b/controllers/class.reactcontroller.php @@ -135,7 +135,8 @@ class ReactController extends Gdn_Controller { $ActivityModel = new ActivityModel(); $Activity = $ActivityModel->GetID($ActivityID); - +// decho($Activity); +// die(); if($Activity) { $Anchor = '#Activity_' . $ActivityID . ' .ReactMenu'; } @@ -145,12 +146,12 @@ class ReactController extends Gdn_Controller { $UserID = Gdn::Session()->UserID; - if($Activity->InsertUserID == $UserID) { + if($Activity['ActivityUserID'] == $UserID) { throw new Gdn_UserException('You cannot react to your own content.'); } // It has passed through the gauntlet - $this->ReactionModel->SetReaction($ActivityID, 'activity', $Activity->RegardingUserID, $UserID, $ActionID); + $this->ReactionModel->SetReaction($ActivityID, 'activity', $Activity['ActivityUserID'], $UserID, $ActionID); $this->JsonTarget($Anchor, $this->_RenderActions($ActivityID, 'activity', FALSE), 'ReplaceWith'); diff --git a/models/class.badgemodel.php b/models/class.badgemodel.php index 2ab0653..8a378a2 100644 --- a/models/class.badgemodel.php +++ b/models/class.badgemodel.php @@ -235,4 +235,23 @@ class BadgeModel extends Gdn_Model { ->Get() ->Result(); } + + /** + * Returns the list of unobtained but enabled badges for a specific user + * + * @param int $UserID + * @param bool $Enabled Description + * @return DataSet + */ + public function GetBadgesToCheckForUser($UserID) { + return $this->SQL + ->Select() + ->From('Badge b') + ->Join('BadgeAward ba', 'b.BadgeID = ba.BadgeID', 'left') + ->Where('ba.UserID', $UserID) + ->Where('b.Enabled', 1) + //->OrWhere('b.BadgeID is not null') // needed to get the full set of badges + ->Get() + ->Result(); + } } \ No newline at end of file diff --git a/models/class.reactionmodel.php b/models/class.reactionmodel.php index 0163a85..ad02249 100644 --- a/models/class.reactionmodel.php +++ b/models/class.reactionmodel.php @@ -162,6 +162,8 @@ class ReactionModel extends Gdn_Model { // clear the cache unset(self::$_Reactions[$Type . $ID]); + $EventArgs = array('ID' => $ID, 'Type' => $Type, 'UserID' => $AuthorID, 'InsertUserID' => $UserID, 'Action' => $ActionID); + $CurrentReaction = $this->GetUserReaction($ID, $Type, $UserID); if($CurrentReaction) { if($ActionID == $CurrentReaction->ActionID) { @@ -170,6 +172,7 @@ class ReactionModel extends Gdn_Model { 'ParentType' => $Type, 'InsertUserID' => $UserID, 'ActionID' => $ActionID)); + $EventArgs['Exists'] = FALSE; } else { // update the record @@ -181,6 +184,7 @@ class ReactionModel extends Gdn_Model { ->Where('ParentType', $Type) ->Where('InsertUserID', $UserID) ->Put(); + $EventArgs['Exists'] = TRUE; } } else { @@ -193,10 +197,10 @@ class ReactionModel extends Gdn_Model { 'ParentAuthorID' => $AuthorID, 'InsertUserID' => $UserID, 'DateInserted' => date(DATE_ISO8601))); + $EventArgs['Exists'] = TRUE; } - $EventArgs['Reaction'] = $Reaction->Result(); - $this->FireEvent('AfterReactionSave', $EventArgs); + $this->FireEvent('AfterReactionSave', $EventArgs); return $Reaction; } } \ No newline at end of file diff --git a/rules/class.reactioncount.php b/rules/class.reactioncount.php index 5fa6880..1bf0b35 100644 --- a/rules/class.reactioncount.php +++ b/rules/class.reactioncount.php @@ -10,21 +10,19 @@ include_once 'interface.yagarule.php'; class ReactionCount implements YagaRule{ public function Award($Sender, $User, $Criteria) { - $Reaction = $Sender->EventArguments['Reaction'][0]; - decho($Criteria->ActionID); - decho($Reaction); - die(); - $ReactionModel = new ReactionModel(); - $Count = $ReactionModel->GetUserReactionCount($Reaction->ParentAuthorID, $Criteria->ActionID); + $ActionID = $Sender->EventArguments['ActionID']; - decho($Count); - if($Count >= $Criteria->Target) { - decho($Reaction); - - return $Reaction->InsertUserID; + if($Criteria->ActionID != $ActionID) { + return FALSE; + } + + $ReactionModel = new ReactionModel(); + $Count = $ReactionModel->GetUserReactionCount($Sender->EventArguments['UserID'], $Criteria->ActionID); + + if($Count >= $Criteria->Target) { + return $Sender->EventArguments['InsertUserID']; } else { - die(); return FALSE; } } diff --git a/settings/class.hooks.php b/settings/class.hooks.php index 16755f5..035ba4e 100755 --- a/settings/class.hooks.php +++ b/settings/class.hooks.php @@ -280,7 +280,7 @@ class YagaHooks implements Gdn_IPlugin { * This is the dispatcher to check badge awards * @todo Optimize this by caching the rules... or something * - * @param string $Hook What rules will be checked this pass around + * @param string $Hook The rule hooks to check */ private function _AwardBadges($Sender, $Hook) { $Session = Gdn::Session();