CommentModel::setWatch PHP Method

setWatch() public method

Record the user's watch data.
Since: 2.0.0
public setWatch ( object $Discussion, integer $Limit, integer $Offset, integer $TotalComments )
$Discussion object Discussion being watched.
$Limit integer Max number to get.
$Offset integer Number to skip.
$TotalComments integer Total in entire discussion (hard limit).
    public function setWatch($Discussion, $Limit, $Offset, $TotalComments)
    {
        $NewComments = false;
        $Session = Gdn::session();
        if ($Session->UserID > 0) {
            // Max comments we could have seen
            $CountWatch = $Limit + $Offset;
            if ($CountWatch > $TotalComments) {
                $CountWatch = $TotalComments;
            }
            // This dicussion looks familiar...
            if (is_numeric($Discussion->CountCommentWatch)) {
                if ($CountWatch < $Discussion->CountCommentWatch) {
                    $CountWatch = $Discussion->CountCommentWatch;
                }
                if (isset($Discussion->DateLastViewed)) {
                    $NewComments |= Gdn_Format::toTimestamp($Discussion->DateLastComment) > Gdn_Format::toTimestamp($Discussion->DateLastViewed);
                }
                if ($TotalComments > $Discussion->CountCommentWatch) {
                    $NewComments |= true;
                }
                // Update the watch data.
                if ($NewComments) {
                    // Only update the watch if there are new comments.
                    $this->SQL->put('UserDiscussion', array('CountComments' => $CountWatch, 'DateLastViewed' => Gdn_Format::toDateTime()), array('UserID' => $Session->UserID, 'DiscussionID' => $Discussion->DiscussionID));
                }
            } else {
                // Make sure the discussion isn't archived.
                $ArchiveDate = c('Vanilla.Archive.Date', false);
                if (!$ArchiveDate || Gdn_Format::toTimestamp($Discussion->DateLastComment) > Gdn_Format::toTimestamp($ArchiveDate)) {
                    $NewComments = true;
                    // Insert watch data.
                    $this->SQL->Options('Ignore', true);
                    $this->SQL->insert('UserDiscussion', array('UserID' => $Session->UserID, 'DiscussionID' => $Discussion->DiscussionID, 'CountComments' => $CountWatch, 'DateLastViewed' => Gdn_Format::toDateTime()));
                }
            }
            /**
             * Fuzzy way of trying to automatically mark a cateogyr read again
             * if the user reads all the comments on the first few pages.
             */
            // If this discussion is in a category that has been marked read,
            // check if reading this thread causes it to be completely read again
            $CategoryID = val('CategoryID', $Discussion);
            if ($CategoryID) {
                $Category = CategoryModel::categories($CategoryID);
                if ($Category) {
                    $DateMarkedRead = val('DateMarkedRead', $Category);
                    if ($DateMarkedRead) {
                        // Fuzzy way of looking back about 2 pages into the past
                        $LookBackCount = c('Vanilla.Discussions.PerPage', 50) * 2;
                        // Find all discussions with content from after DateMarkedRead
                        $DiscussionModel = new DiscussionModel();
                        $Discussions = $DiscussionModel->get(0, 101, array('CategoryID' => $CategoryID, 'DateLastComment>' => $DateMarkedRead));
                        unset($DiscussionModel);
                        // Abort if we get back as many as we asked for, meaning a
                        // lot has happened.
                        $NumDiscussions = $Discussions->numRows();
                        if ($NumDiscussions <= $LookBackCount) {
                            // Loop over these and see if any are still unread
                            $MarkAsRead = true;
                            while ($Discussion = $Discussions->NextRow(DATASET_TYPE_ARRAY)) {
                                if ($Discussion['Read']) {
                                    continue;
                                }
                                $MarkAsRead = false;
                                break;
                            }
                            // Mark this category read if all the new content is read
                            if ($MarkAsRead) {
                                $CategoryModel = new CategoryModel();
                                $CategoryModel->SaveUserTree($CategoryID, array('DateMarkedRead' => Gdn_Format::toDateTime()));
                                unset($CategoryModel);
                            }
                        }
                    }
                }
            }
        }
    }

Usage Example

コード例 #1
0
 /**
  * Display comments in a discussion since a particular CommentID.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $DiscussionID Unique discussion ID
  * @param int $LastCommentID Only shows comments posted after this one
  */
 public function getNew($DiscussionID, $LastCommentID = 0)
 {
     $this->setData('Discussion', $this->DiscussionModel->getID($DiscussionID), true);
     // Check permissions.
     $this->permission('Vanilla.Discussions.View', true, 'Category', $this->Discussion->PermissionCategoryID);
     $this->setData('CategoryID', $this->CategoryID = $this->Discussion->CategoryID, true);
     // Get the comments.
     $Comments = $this->CommentModel->getNew($DiscussionID, $LastCommentID)->result();
     $this->setData('Comments', $Comments, true);
     // Set the data.
     if (count($Comments) > 0) {
         $LastComment = $Comments[count($Comments) - 1];
         // Mark the comment read.
         $this->setData('Offset', $this->Discussion->CountComments, true);
         $this->CommentModel->setWatch($this->Discussion, $this->Discussion->CountComments, $this->Discussion->CountComments, $this->Discussion->CountComments);
         $LastCommentID = $this->json('LastCommentID');
         if (is_null($LastCommentID) || $LastComment->CommentID > $LastCommentID) {
             $this->json('LastCommentID', $LastComment->CommentID);
         }
     } else {
         $this->setData('Offset', $this->CommentModel->getOffset($LastCommentID), true);
     }
     $this->View = 'comments';
     $this->render();
 }