public function UpdateCommentCount($Discussion) {
      // Get the discussion.
      if (is_numeric($Discussion))
         $Discussion = $this->SQL->GetWhere('Discussion', array('DiscussionID' => $Discussion))->FirstRow(DATASET_TYPE_ARRAY);
      $DiscussionID = $Discussion['DiscussionID'];

      $this->FireEvent('BeforeUpdateCommentCountQuery');
      
      $Data = $this->SQL
         ->Select('c.CommentID', 'min', 'FirstCommentID')
         ->Select('c.CommentID', 'max', 'LastCommentID')
         ->Select('c.DateInserted', 'max', 'DateLastComment')
         ->Select('c.CommentID', 'count', 'CountComments')
         ->From('Comment c')
         ->Where('c.DiscussionID', $DiscussionID)
         ->Get()->FirstRow(DATASET_TYPE_ARRAY);

      $this->EventArguments['Discussion'] =& $Discussion;
      $this->EventArguments['Counts'] =& $Data;
      $this->FireEvent('BeforeUpdateCommentCount');
      
      if ($Discussion) {
         if ($Data) {
            $this->SQL->Update('Discussion');
            if (!$Discussion['Sink'] && $Data['DateLastComment'])
               $this->SQL->Set('DateLastComment', $Data['DateLastComment']);
            elseif (!$Data['DateLastComment'])
               $this->SQL->Set('DateLastComment', $Discussion['DateInserted']);

            $this->SQL
               ->Set('FirstCommentID', $Data['FirstCommentID'])
               ->Set('LastCommentID', $Data['LastCommentID'])
               ->Set('CountComments', $Data['CountComments'])
               ->Where('DiscussionID', $DiscussionID)
               ->Put();

            // Update the last comment's user ID.
            $this->SQL
               ->Update('Discussion d')
               ->Update('Comment c')
               ->Set('d.LastCommentUserID', 'c.InsertUserID', FALSE)
               ->Where('d.DiscussionID', $DiscussionID)
               ->Where('c.CommentID', 'd.LastCommentID', FALSE, FALSE)
               ->Put();
         } else {
            // Update the discussion with null counts.
            $this->SQL
               ->Update('Discussion')
               ->Set('CountComments', 0)
               ->Set('FirstCommentID', NULL)
               ->Set('LastCommentID', NULL)
               ->Set('DateLastComment', 'DateInserted', FALSE, FALSE)
               ->Set('LastCommentUserID', NULL)
               ->Where('DiscussionID', $DiscussionID);
         }
      }
   }