Skip to content

Commit

Permalink
fix(live-feedack-backend) backend modifications
Browse files Browse the repository at this point in the history
change hash for live feedback to now be ordered by weight
also refactor to reduce branch condition size for hound
this comes at the realization that weights are not necessarily the index

change database to use creator (and link to user) instead of linking it to course_user
made respective changes in concerns and controllers and views
  • Loading branch information
syoopie committed Aug 29, 2024
1 parent 81e480c commit c075a67
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 41 deletions.
57 changes: 32 additions & 25 deletions app/controllers/concerns/course/statistics/live_feedback_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,42 @@
module Course::Statistics::LiveFeedbackConcern
private

def find_submitter_course_user(submission)
submission.creator.course_users.find { |u| u.course_id == @assessment.course_id }
end

def find_live_feedback_course_user(live_feedback)
live_feedback.creator.course_users.find { |u| u.course_id == @assessment.course_id }
end

def initialize_feedback_count
Array.new(@question_order_hash.size, 0)
end

def find_user_assessment_live_feedback(submitter_course_user, assessment_live_feedbacks)
assessment_live_feedbacks.select do |live_feedback|
find_live_feedback_course_user(live_feedback) == submitter_course_user
end
end

def update_feedback_count(feedback_count, user_assessment_live_feedback)
user_assessment_live_feedback.each do |feedback|
index = @ordered_questions.index(feedback.question_id)
feedback.code.each do |code|
unless code.comments.empty?
feedback_count[index] += 1
break
end
end
end
end

def initialize_student_hash(students)
students.to_h { |student| [student, nil] }
end

def fetch_hash_for_live_feedback_assessment(submissions, assessment_live_feedbacks, students)
def fetch_hash_for_live_feedback_assessment(submissions, assessment_live_feedbacks)
students = @all_students
student_hash = initialize_student_hash(students)

populate_hash(submissions, student_hash, assessment_live_feedbacks)
Expand All @@ -28,28 +59,4 @@ def populate_hash(submissions, student_hash, assessment_live_feedbacks)
student_hash[submitter_course_user] = [submission, feedback_count]
end
end

def find_submitter_course_user(submission)
submission.creator.course_users.find { |u| u.course_id == @assessment.course_id }
end

def initialize_feedback_count
Array.new(@question_order_hash.size, 0)
end

def find_user_assessment_live_feedback(submitter_course_user, assessment_live_feedbacks)
assessment_live_feedbacks.select { |live_feedback| live_feedback.creator == submitter_course_user }
end

def update_feedback_count(feedback_count, user_assessment_live_feedback)
user_assessment_live_feedback.each do |feedback|
index = @question_order_hash[feedback.question_id]
feedback.code.each do |code|
unless code.comments.empty?
feedback_count[index] += 1
break
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ def generate_live_feedback
response_status, response_body = @answer.generate_live_feedback
response_body['feedbackUrl'] = ENV.fetch('CODAVERI_URL')

course_user = CourseUser.find_by(course_id: @assessment.course_id, user_id: @submission.creator_id)
live_feedback = Course::Assessment::LiveFeedback.create_with_codes(
@submission.assessment_id,
@answer.question_id,
course_user.id,
@submission.creator,
response_body['transactionId'],
@answer.actable.files
)
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/course/statistics/assessments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ def live_feedback_statistics
@course_users_hash = preload_course_users_hash(@assessment.course)

load_course_user_students_info
create_question_order_hash
load_ordered_questions

@student_live_feedback_hash = fetch_hash_for_live_feedback_assessment(submissions,
assessment_live_feedbacks,
@all_students)
assessment_live_feedbacks)
end

def live_feedback_history
Expand All @@ -62,6 +61,10 @@ def live_feedback_history

private

def load_ordered_questions
@ordered_questions = create_question_order_hash.keys.sort_by { |question_id| @question_order_hash[question_id] }
end

def assessment_params
params.permit(:id)
end
Expand Down Expand Up @@ -97,8 +100,9 @@ def create_question_order_hash
end

def fetch_live_feedbacks
user = current_course.course_users.find_by(id: params[:course_user_id]).user
Course::Assessment::LiveFeedback.where(assessment_id: assessment_params[:id],
creator_course_id: params[:course_user_id],
creator: user,
question_id: params[:question_id]).
order(created_at: :asc).includes(:code, code: :comments)
end
Expand Down
9 changes: 6 additions & 3 deletions app/models/course/assessment/live_feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ class Course::Assessment::LiveFeedback < ApplicationRecord
belongs_to :assessment, class_name: 'Course::Assessment', foreign_key: 'assessment_id', inverse_of: :live_feedbacks
belongs_to :question, class_name: 'Course::Assessment::Question', foreign_key: 'question_id',
inverse_of: :live_feedbacks
belongs_to :creator, class_name: 'CourseUser', foreign_key: 'creator_course_id', inverse_of: :live_feedbacks
has_many :code, class_name: 'Course::Assessment::LiveFeedbackCode', foreign_key: 'feedback_id',
inverse_of: :feedback, dependent: :destroy

def self.create_with_codes(assessment_id, question_id, course_user_id, feedback_id, files)
validates :assessment, presence: true
validates :question, presence: true
validates :creator, presence: true

def self.create_with_codes(assessment_id, question_id, user, feedback_id, files)
live_feedback = new(
assessment_id: assessment_id,
question_id: question_id,
creator_course_id: course_user_id,
creator: user,
feedback_id: feedback_id
)

Expand Down
2 changes: 0 additions & 2 deletions app/models/course_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class CourseUser < ApplicationRecord
has_many :groups, through: :group_users, class_name: 'Course::Group', source: :group
has_many :personal_times, class_name: 'Course::PersonalTime', inverse_of: :course_user, dependent: :destroy
belongs_to :reference_timeline, class_name: 'Course::ReferenceTimeline', inverse_of: :course_users, optional: true
has_many :live_feedbacks, class_name: 'Course::Assessment::LiveFeedback', foreign_key: 'creator_course_id',
inverse_of: :creator, dependent: :destroy

validate :validate_reference_timeline_belongs_to_course

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ json.array! @student_live_feedback_hash.each do |course_user, (submission, live_
end

json.liveFeedbackCount live_feedback_count
json.questionIds(@question_order_hash.keys.sort_by { |key| @question_order_hash[key] })
json.questionIds @ordered_questions
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :course_assessment_live_feedbacks do |t|
t.references :assessment, null: false, index: true, foreign_key: { to_table: :course_assessments }
t.references :question, null: false, index: true, foreign_key: { to_table: :course_assessment_questions }
t.references :creator_course, null: false, index: true, foreign_key: { to_table: :course_users }
t.references :creator, null: false, index: true, foreign_key: { to_table: :users }
t.datetime :created_at, null: false
t.string :feedback_id
end
Expand Down
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@
create_table "course_assessment_live_feedbacks", force: :cascade do |t|
t.bigint "assessment_id", null: false
t.bigint "question_id", null: false
t.bigint "creator_course_id", null: false
t.bigint "creator_id", null: false
t.datetime "created_at", null: false
t.string "feedback_id"
t.index ["assessment_id"], name: "index_course_assessment_live_feedbacks_on_assessment_id"
t.index ["creator_course_id"], name: "index_course_assessment_live_feedbacks_on_creator_course_id"
t.index ["creator_id"], name: "index_course_assessment_live_feedbacks_on_creator_id"
t.index ["question_id"], name: "index_course_assessment_live_feedbacks_on_question_id"
end

Expand Down Expand Up @@ -1509,7 +1509,7 @@
add_foreign_key "course_assessment_live_feedback_comments", "course_assessment_live_feedback_code", column: "code_id"
add_foreign_key "course_assessment_live_feedbacks", "course_assessment_questions", column: "question_id"
add_foreign_key "course_assessment_live_feedbacks", "course_assessments", column: "assessment_id"
add_foreign_key "course_assessment_live_feedbacks", "course_users", column: "creator_course_id"
add_foreign_key "course_assessment_live_feedbacks", "users", column: "creator_id"
add_foreign_key "course_assessment_question_bundle_assignments", "course_assessment_question_bundles", column: "bundle_id"
add_foreign_key "course_assessment_question_bundle_assignments", "course_assessment_submissions", column: "submission_id"
add_foreign_key "course_assessment_question_bundle_assignments", "course_assessments", column: "assessment_id"
Expand Down

0 comments on commit c075a67

Please sign in to comment.