Skip to content

Commit

Permalink
fix: exclude anonymous posts in build_course_stats (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayesha-waris authored Jan 8, 2024
1 parent 20cd380 commit 0def92c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ def build_course_stats(course_id)
data = Content.collection.aggregate(
[
# Match all content in the course by the specified author
{ "$match" => { :course_id => course_id, :author_id => self.external_id } },
{ "$match" => { :course_id => course_id,
:author_id => self.external_id, "anonymous_to_peers" => false, "anonymous" => false } },
# Keep a count of flags for each entry
{
"$addFields" => {
Expand Down Expand Up @@ -391,4 +392,4 @@ def update_all_users_in_course(course_id)
user.build_course_stats(course_id)
end
author_usernames
end
end
23 changes: 23 additions & 0 deletions spec/api/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,29 @@ def get_new_stats
expect(new_stats["active_flags"]).to eq @original_stats["active_flags"] - 1
end
end
describe "build_course_stats" do
let(:user) { create_test_user 3}
let(:course_id) { DFLT_COURSE_ID }

context 'when the user has made anonymous posts' do
before do
make_anonymous_to_peers_thread(user, "anon thread 1 by author", DFLT_COURSE_ID, "anon_thread_1")
make_anonymous_thread(user, "anon thread 2 by author", DFLT_COURSE_ID, "anon_thread_2")
end

it 'does not include anonymous posts in the counts after making a non-anonymous post' do

make_thread(user, "thread by new author #{user}", DFLT_COURSE_ID, "new_thread")

get "/api/v1/users/#{course_id}/stats"
expect(last_response.status).to eq(200)
stats = parse(last_response.body)
expect(stats["user_stats"][0]["replies"]).to eq(0)
expect(stats["user_stats"][0]["responses"]).to eq(0)
expect(stats["user_stats"][0]["threads"]).to eq(1)
end
end
end
end

describe "POST /api/v1/users/:course_id/update_stats" do
Expand Down
18 changes: 18 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,24 @@ def make_thread(author, text, course_id, commentable_id, thread_type=:discussion
thread
end

def make_anonymous_to_peers_thread(author, text, course_id, commentable_id, thread_type=:discussion, context=:course)
thread = CommentThread.new(title: text, body: text, course_id: course_id, commentable_id: commentable_id, anonymous_to_peers: true)
thread.thread_type = thread_type
thread.author = author
thread.context = context
thread.save!
thread
end

def make_anonymous_thread(author, text, course_id, commentable_id, thread_type=:discussion, context=:course)
thread = CommentThread.new(title: text, body: text, course_id: course_id, commentable_id: commentable_id, anonymous: true)
thread.thread_type = thread_type
thread.author = author
thread.context = context
thread.save!
thread
end

def make_comment(author, parent, text)
if parent.is_a?(CommentThread)
coll = parent.comments
Expand Down

0 comments on commit 0def92c

Please sign in to comment.