<%INIT>
if ($session{'CurrentUser'}) {
	my ($sth, $ref, $TicketComments, $CommentsSeen);

	$sth = $RT::Handle->dbh->prepare ('SELECT COUNT(DISTINCT Transactions.id) AS comments FROM Transactions JOIN Attachments ON Transactions.id = Attachments.TransactionId WHERE Transactions.ObjectType="RT::Ticket" AND Transactions.Type IN ("Comment","Correspond","Create") AND Transactions.ObjectId=?');
	$sth->execute ($Ticket->id);
	$ref = $sth->fetchrow_hashref ();
	$sth->finish ();
	$TicketComments = (defined $ref ? $ref->{'comments'} : 0) || 0;

	$sth = $RT::Handle->dbh->prepare ('SELECT Comments AS comments FROM CommentsSeen WHERE UserId=? AND TicketId=?');
	$sth->execute ($session{'CurrentUser'}->id, $Ticket->id);
	$ref = $sth->fetchrow_hashref ();
	$sth->finish ();
	$CommentsSeen = (defined $ref && defined $ref->{'comments'}) ? $ref->{'comments'} : undef;

	if (not defined $CommentsSeen) {
		$RT::Handle->dbh->do ('INSERT INTO CommentsSeen (UserId,TicketId,Comments) VALUES (?,?,?)', undef, $session{'CurrentUser'}->id, $Ticket->id, $TicketComments);
	} else {
		$RT::Handle->dbh->do ('UPDATE CommentsSeen SET Comments=? WHERE UserId=? AND TicketId=?', undef, $TicketComments, $session{'CurrentUser'}->id, $Ticket->id);
	}
}
</%INIT>
<%args>
$Ticket => undef
</%args>
