Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions app/model/repository/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ public function persistThread(CommentThread $thread) {
}

/**
* Get the number of visible comments for given user
* Get the number of visible comments for given user.
* The method requires the user entity, since it does not use ACL mechanism and compares
* the authorship of comments directly as a performance optimization (we need only the count, not all entities).
* @param $threadId Id of the comments thread.
* @param $userId Id of the viewer or null (if nobody is logged in).
* @param bool $allVisible True = all coments visible for the user, false = comments made by the user.
*/
public function getThreadCommentsCount(CommentThread $thread, ?User $user, bool $allVisible = true)
private function getCommentsCount(CommentThread $thread, User $user, bool $allVisible)
{
if (!$user) {
return 0; // anonymous users cannot see any comments
}

$qb = $this->comments->createQueryBuilder('tc')
->select('COUNT(tc.id)')
->where('tc.commentThread = :id');
Expand All @@ -64,16 +62,34 @@ public function getThreadCommentsCount(CommentThread $thread, ?User $user, bool
}

/**
* Get the number of visible comments for given user
* Get the number of all visible comments for given user.
* @param $threadId Id of the comments thread.
* @param $userId Id of the viewer or null (if nobody is logged in).
*/
public function getThreadLastComment(CommentThread $thread, ?User $user)
public function getThreadCommentsCount(CommentThread $thread, User $user)
{
if (!$user) {
return null; // anonymous users cannot see any comments
}
return $this->getCommentsCount($thread, $user, true);
}

/**
* Get the number of comments made directly by given user.
* @param $threadId Id of the comments thread.
* @param $userId Id of the viewer or null (if nobody is logged in).
*/
public function getAuthoredCommentsCount(CommentThread $thread, User $user)
{
return $this->getCommentsCount($thread, $user, true);
}

/**
* Get the number of visible comments for given user
* The method requires the user entity, since it does not use ACL mechanism and compares
* the authorship of comments directly as a performance optimization (we need only the last entity, not all entities).
* @param $threadId Id of the comments thread.
* @param $userId Id of the viewer or null (if nobody is logged in).
*/
public function getThreadLastComment(CommentThread $thread, User $user)
{
return $this->comments->matching(Criteria::create()
->where(Criteria::expr()->andX(
Criteria::expr()->eq('commentThread', $thread),
Expand Down
2 changes: 1 addition & 1 deletion app/model/view/AssignmentSolutionViewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getSolutionData(AssignmentSolution $solution) {
"submissions" => $submissions,
"commentsStats" => $thread && $user ? [
"count" => $this->comments->getThreadCommentsCount($thread, $user),
"mineCount" => $this->comments->getThreadCommentsCount($thread, $user, false),
"authoredCount" => $this->comments->getAuthoredCommentsCount($thread, $user),
"last" => $this->comments->getThreadLastComment($thread, $user),
] : null,
];
Expand Down