From e20cb5617c0811bac1331c4a0be498f896bcd4a2 Mon Sep 17 00:00:00 2001 From: Sahithi Ammana <112002925+SahithiAmmana@users.noreply.github.com> Date: Wed, 4 Sep 2024 10:06:20 -0500 Subject: [PATCH] Reimplementing Cake model --- app/helpers/response_helper.rb | 24 ++++++++++++++++++ app/models/cake.rb | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 app/helpers/response_helper.rb create mode 100644 app/models/cake.rb diff --git a/app/helpers/response_helper.rb b/app/helpers/response_helper.rb new file mode 100644 index 000000000..6e290f491 --- /dev/null +++ b/app/helpers/response_helper.rb @@ -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 diff --git a/app/models/cake.rb b/app/models/cake.rb new file mode 100644 index 000000000..6fbc24932 --- /dev/null +++ b/app/models/cake.rb @@ -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