UserModel::givePoints PHP Method

givePoints() public static method

Add points to a user's total.
Since: 2.1.0
public static givePoints ( integer $UserID, integer $Points, string $Source = 'Other', integer | false $Timestamp = false )
$UserID integer
$Points integer
$Source string
$Timestamp integer | false
    public static function givePoints($UserID, $Points, $Source = 'Other', $Timestamp = false)
    {
        if (!$Timestamp === false) {
            $Timestamp = time();
        }
        if (is_array($Source)) {
            $CategoryID = val('CategoryID', $Source, 0);
            $Source = $Source[0];
        } else {
            $CategoryID = 0;
        }
        if ($CategoryID > 0) {
            $CategoryIDs = [$CategoryID, 0];
        } else {
            $CategoryIDs = [$CategoryID];
        }
        foreach ($CategoryIDs as $ID) {
            // Increment source points for the user.
            self::givePointsInternal($UserID, $Points, 'a', $Source, $ID);
            // Increment total points for the user.
            self::givePointsInternal($UserID, $Points, 'w', 'Total', $ID, $Timestamp);
            self::givePointsInternal($UserID, $Points, 'm', 'Total', $ID, $Timestamp);
            self::givePointsInternal($UserID, $Points, 'a', 'Total', $ID, $Timestamp);
            // Increment global daily points.
            self::givePointsInternal(0, $Points, 'd', 'Total', $ID, $Timestamp);
        }
        // Grab the user's total points.
        $totalPoints = Gdn::sql()->getWhere('UserPoints', ['UserID' => $UserID, 'SlotType' => 'a', 'Source' => 'Total', 'CategoryID' => 0])->value('Points');
        Gdn::userModel()->setField($UserID, 'Points', $totalPoints);
        // Fire a give points event.
        Gdn::userModel()->EventArguments['UserID'] = $UserID;
        Gdn::userModel()->EventArguments['CategoryID'] = $CategoryID;
        Gdn::userModel()->EventArguments['TotalPoints'] = $totalPoints;
        Gdn::userModel()->EventArguments['GivenPoints'] = $Points;
        Gdn::userModel()->EventArguments['Source'] = $Source;
        Gdn::userModel()->EventArguments['Timestamp'] = $Timestamp;
        Gdn::userModel()->EventArguments['Points'] = $totalPoints;
        // Deprecated in favor of TotalPoints
        Gdn::userModel()->fireEvent('GivePoints');
    }

Usage Example

Example #1
0
 /**
  * Give point(s) to the current user if the right conditions are met.
  *
  * @param CommentModel $sender Sending controller instance.
  * @param array $args Event arguments.
  */
 public function commentModel_afterSaveComment_handler($sender, $args)
 {
     if (!c('QnA.Points.Enabled', false) || !$args['Insert']) {
         return;
     }
     $discussionModel = new DiscussionModel();
     $discussion = $discussionModel->getID($args['CommentData']['DiscussionID'], DATASET_TYPE_ARRAY);
     $isCommentAnAnswer = $discussion['Type'] === 'Question';
     $isQuestionResolved = $discussion['QnA'] === 'Accepted';
     $isCurrentUserOriginalPoster = $discussion['InsertUserID'] == GDN::session()->UserID;
     if (!$isCommentAnAnswer || $isQuestionResolved || $isCurrentUserOriginalPoster) {
         return;
     }
     $userAnswersToQuestion = $sender->getWhere(array('DiscussionID' => $args['CommentData']['DiscussionID'], 'InsertUserId' => GDN::session()->UserID));
     // Award point(s) only for the first answer to the question
     if ($userAnswersToQuestion->count() > 1) {
         return;
     }
     UserModel::givePoints(GDN::session()->UserID, c('QnA.Points.Answer', 1), 'QnA');
 }
UserModel