Example #1
0
 /**
  * A convenience function that allows adding to the activity table with a single line.
  */
 function AddActivity($ActivityUserID, $ActivityType, $Story = '', $RegardingUserID = '', $Route = '', $SendEmail = '')
 {
     $ActivityModel = new Gdn_ActivityModel();
     return $ActivityModel->Add($ActivityUserID, $ActivityType, $Story, $RegardingUserID, '', $Route, $SendEmail);
 }
Example #2
0
 /**
  * A convenience function that allows adding to the activity table with a single line.
  */
 function AddActivity($ActivityUserID, $ActivityType, $Story = '', $RegardingUserID = '', $Route = '')
 {
     $ActivityModel = new Gdn_ActivityModel();
     $ActivityModel->Add($ActivityUserID, $ActivityType, $Story, $RegardingUserID, '', $Route);
 }
 public function Save($FormPostValues)
 {
     $Session = Gdn::Session();
     // Define the primary key in this model's table.
     $this->DefineSchema();
     // Add & apply any extra validation rules:
     $this->Validation->ApplyRule('Body', 'Required');
     $this->AddInsertFields($FormPostValues);
     // Validate the form posted values
     $MessageID = FALSE;
     if ($this->Validate($FormPostValues)) {
         $Fields = $this->Validation->SchemaValidationFields();
         // All fields on the form that relate to the schema
         $MessageID = $this->SQL->Insert($this->Name, $Fields);
         $ConversationID = ArrayValue('ConversationID', $Fields, 0);
         // Update the conversation's DateUpdated field
         $this->SQL->Update('Conversation')->Set('DateUpdated', Format::ToDateTime())->Set('UpdateUserID', $Session->UserID)->Where('ConversationID', $ConversationID)->Put();
         // NOTE: INCREMENTING COUNTS INSTEAD OF GETTING ACTUAL COUNTS COULD
         // BECOME A PROBLEM. WATCH FOR IT.
         // Update the message counts for all users in the conversation
         $this->SQL->Update('UserConversation')->Set('CountMessages', 'CountMessages + 1', FALSE)->Where('ConversationID', $ConversationID)->Put();
         $this->SQL->Update('UserConversation')->Set('CountNewMessages', 'CountNewMessages + 1', FALSE)->Where('ConversationID', $ConversationID)->Where('UserID <>', $Session->UserID)->Put();
         // Update the userconversation records to reflect the most recently
         // added message for all users other than the one that added the
         // message (otherwise they would see their name/message on the
         // conversation list instead of the person they are conversing with).
         $this->SQL->Update('UserConversation')->Set('LastMessageID', $MessageID)->Where('ConversationID', $ConversationID)->Where('UserID <>', $Session->UserID)->Put();
         // Update the CountUnreadConversations count on each user related to the discussion.
         // And notify the users of the new message
         $UnreadData = $this->SQL->Select('c.UserID')->Select('c2.CountNewMessages', 'count', 'CountUnreadConversations')->From('UserConversation c')->Join('UserConversation c2', 'c.UserID = c2.UserID')->Where('c2.CountNewMessages >', 0)->Where('c.ConversationID', $ConversationID)->Where('c.UserID <>', $Session->UserID)->GroupBy('c.UserID')->Get();
         $ActivityModel = new Gdn_ActivityModel();
         foreach ($UnreadData->Result() as $User) {
             // Update the CountUnreadConversations count on each user related to the discussion.
             $this->SQL->Update('User')->Set('CountUnreadConversations', $User->CountUnreadConversations)->Where('UserID', $User->UserID)->Put();
             // And notify the users of the new message
             $ActivityID = $ActivityModel->Add($Session->UserID, 'ConversationMessage', '', $User->UserID, '', '/messages/' . $ConversationID . '#' . $MessageID, FALSE);
             $Story = ArrayValue('Body', $Fields, '');
             $ActivityModel->SendNotification($ActivityID, $Story);
         }
     }
     return $MessageID;
 }
Example #4
0
 public function Save($FormPostValues)
 {
     $Session = Gdn::Session();
     // Define the primary key in this model's table.
     $this->DefineSchema();
     // Add & apply any extra validation rules:
     $this->Validation->ApplyRule('Body', 'Required');
     $MaxCommentLength = Gdn::Config('Vanilla.Comment.MaxLength');
     if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
         $this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
         $this->Validation->ApplyRule('Body', 'Length');
     }
     $CommentID = ArrayValue('CommentID', $FormPostValues);
     $CommentID = is_numeric($CommentID) && $CommentID > 0 ? $CommentID : FALSE;
     $Insert = $CommentID === FALSE;
     if ($Insert) {
         $this->AddInsertFields($FormPostValues);
     } else {
         $this->AddUpdateFields($FormPostValues);
     }
     // Validate the form posted values
     if ($this->Validate($FormPostValues, $Insert)) {
         // If the post is new and it validates, check for spam
         if (!$Insert || !$this->CheckForSpam('Comment')) {
             $Fields = $this->Validation->SchemaValidationFields();
             $Fields = RemoveKeyFromArray($Fields, $this->PrimaryKey);
             $DiscussionModel = new Gdn_DiscussionModel();
             $DiscussionID = ArrayValue('DiscussionID', $Fields);
             $Discussion = $DiscussionModel->GetID($DiscussionID);
             $DiscussionAuthorMentioned = FALSE;
             if ($Insert === FALSE) {
                 $this->SQL->Put($this->Name, $Fields, array('CommentID' => $CommentID));
             } else {
                 // Make sure that the comments get formatted in the method defined by Garden
                 $Fields['Format'] = Gdn::Config('Garden.InputFormatter', '');
                 $CommentID = $this->SQL->Insert($this->Name, $Fields);
                 $this->EventArguments['CommentID'] = $CommentID;
                 // IsNewDiscussion is passed when the first comment for new discussions are created.
                 $this->EventArguments['IsNewDiscussion'] = ArrayValue('IsNewDiscussion', $FormPostValues);
                 $this->FireEvent('AfterSaveComment');
                 // Notify any users who were mentioned in the comment
                 $Usernames = GetMentions($Fields['Body']);
                 $UserModel = Gdn::UserModel();
                 $DiscussionName = '';
                 foreach ($Usernames as $Username) {
                     $User = $UserModel->GetWhere(array('Name' => $Username))->FirstRow();
                     if ($User && $User->UserID != $Session->UserID) {
                         if ($User->UserID == $Discussion->InsertUserID) {
                             $DiscussionAuthorMentioned = TRUE;
                         }
                         $ActivityModel = new Gdn_ActivityModel();
                         $ActivityID = $ActivityModel->Add($Session->UserID, 'CommentMention', Anchor(Format::Text($Discussion->Name), 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID), $User->UserID, '', 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID, FALSE);
                         $Story = ArrayValue('Body', $Fields, '');
                         $ActivityModel->SendNotification($ActivityID, $Story);
                     }
                 }
             }
             // Record user-comment activity
             if ($Insert === TRUE && $Discussion !== FALSE && $DiscussionAuthorMentioned === FALSE) {
                 $this->RecordActivity($Discussion, $Session->UserID, $CommentID);
             }
             // Only record activity if inserting a comment, not on edit.
             $this->UpdateCommentCount($DiscussionID);
             // Update the discussion author's CountUnreadDiscussions (ie.
             // the number of discussions created by the user that s/he has
             // unread messages in) if this comment was not added by the
             // discussion author.
             $Data = $this->SQL->Select('d.InsertUserID')->Select('d.DiscussionID', 'count', 'CountDiscussions')->From('Discussion d')->Join('Comment c', 'd.DiscussionID = c.DiscussionID')->Join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = d.InsertUserID')->Where('w.CountComments >', 0)->Where('c.InsertUserID', $Session->UserID)->Where('c.InsertUserID <>', 'd.InsertUserID', TRUE, FALSE)->GroupBy('d.InsertUserID')->Get();
             if ($Data->NumRows() > 0) {
                 $UserData = $Data->FirstRow();
                 $this->SQL->Update('User')->Set('CountUnreadDiscussions', $UserData->CountDiscussions)->Where('UserID', $UserData->InsertUserID)->Put();
             }
             $this->UpdateUser($Session->UserID);
         }
     }
     return $CommentID;
 }
Example #5
0
 public function Notifications()
 {
     $this->Permission('Garden.SignIn.Allow');
     $Session = Gdn::Session();
     // Drop notification count back to zero.
     $SQL = Gdn::SQL();
     $SQL->Update('User')->Set('CountNotifications', '0')->Where('UserID', $Session->UserID)->Put();
     $ActivityModel = new Gdn_ActivityModel();
     $this->NotificationData = $ActivityModel->GetNotifications($Session->UserID);
     $this->Render();
 }
 public function GetData($Limit = 5, $DiscussionID = '')
 {
     $ActivityModel = new Gdn_ActivityModel();
     $this->_ActivityData = $ActivityModel->Get('', 0, $Limit);
 }