Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplementing Cake model #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions app/helpers/response_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ResponseHelper

# Assigns total contribution for cake question across all reviewers to a hash map
# Key : question_id, Value : total score for cake question
def store_total_cake_score
reviewee = ResponseMap.select(:reviewee_id, :type).where(id: @response.map_id.to_s).first
@total_score = scores_per_question(reviewee.type,
@review_questions,
@participant.id,
@assignment.id,
reviewee.reviewee_id)
end

# Calculates the total score for each of the questions for a given participant and assignment.
# Total scores per question, with question_id as the key and total score as the value.
def scores_per_question(review_type, questions, participant_id, assignment_id, reviewee_id)
questions.each_with_object({}) do |question, scores|
next unless question.is_a?(Cake)

score = question.running_total(review_type, question.id, participant_id, assignment_id, reviewee_id)
scores[question.id] = score || 0
end
end
end
45 changes: 45 additions & 0 deletions app/models/cake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Cake < ScoredQuestion
include ActionView::Helpers
validates :size, presence: true

# Retrieves and calculates the total score for a specific question.
def running_total(review_type, question_id, participant_id, assignment_id, reviewee_id)
team_id = Team.joins(teams_users: :participant)
.where('participants.id = ? AND teams.parent_id = ?', participant_id, assignment_id)
.pluck(:id)
.first
return 0 unless team_id

if review_type == 'TeammateReviewResponseMap'
answers = Participant.joins(user: :teams_users)
.where('teams_users.team_id = ? AND participants.parent_id = ?', team_id, assignment_id)
.pluck(:id)
.flat_map do |team_member_id|
Answer.joins(response: :response_map)
.where("response_maps.reviewee_id = ? AND response_maps.reviewed_object_id = ?
AND response_maps.reviewer_id = ? AND answers.question_id = ?
AND response_maps.reviewee_id != ? AND answers.answer IS NOT NULL",
team_member_id, assignment_id, participant_id, question_id, reviewee_id)
end
answers.compact.sum(&:answer)
else
0
end
end

# The score a user gave to a specific question.
def score_given_by_user(participant_id, question_id, assignment_id, reviewee_id)
Answer.joins(response: :response_map)
.where("response_maps.reviewer_id = ? AND response_maps.reviewee_id = ? AND response_maps.reviewed_object_id = ? AND answers.question_id = ?",
participant_id, reviewee_id, assignment_id, question_id)
.pluck(:answer)
.first || 0
end

# The score a user has left to give for a question.
def score_remaining_for_user(review_type, question_id, participant_id, assignment_id, reviewee_id)
total_score = running_total(review_type, question_id, participant_id, assignment_id, reviewee_id)
remaining_score = 100 - total_score
remaining_score.negative? ? 0 : remaining_score
end
end