Skip to content

Commit

Permalink
Merge eaad37d into f0103c0
Browse files Browse the repository at this point in the history
  • Loading branch information
samarth-p committed Oct 31, 2023
2 parents f0103c0 + eaad37d commit 3a2f3d4
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 233 deletions.
177 changes: 87 additions & 90 deletions app/helpers/due_date_helper.rb
Original file line number Diff line number Diff line change
@@ -1,98 +1,95 @@
# app/helpers/due_date_helper.rb

module DueDateHelper
module DueDateHelper

def self.deadline_sort(due_dates)
# Override the comparator operator to sort due dates by due_at
due_dates.sort { |m1, m2| m1.due_at.to_i <=> m2.due_at.to_i}
end
def self.deadline_sort(due_dates)
# Override the comparator operator to sort due dates by due_at
due_dates.sort { |m1, m2| m1.due_at.to_i <=> m2.due_at.to_i }
end

def self.default_permission(deadline_type, permission_type)
DeadlineRight::DEFAULT_PERMISSION[deadline_type][permission_type]
def self.default_permission(deadline_type, permission_type)
DeadlineRight::DEFAULT_PERMISSION[deadline_type][permission_type]
end

def self.calculate_assignment_round(assignment_id, response)
return 0 unless ResponseMap.find(response.map_id).type == 'ReviewResponseMap'

due_dates = DueDate.where(parent_id: assignment_id)
sorted_deadlines = deadline_sort(due_dates)
determine_assignment_round(response, sorted_deadlines)
end

def self.determine_assignment_round(response, sorted_due_dates)
round = 1
sorted_due_dates.each do |due_date|
break if response.created_at < due_date.due_at
round += 1 if due_date.deadline_type_id == 2
end
round
end

def self.find_current_due_date(due_dates)
due_dates.find { |due_date| due_date.due_at > Time.now }
end

def self.is_teammate_review_allowed(student)
due_date = find_current_due_date(student.assignment.due_dates)
return true if student.assignment.find_current_stage == 'Finished'
return true if teammate_review_allowed?(due_date)

false
end

def self.teammate_review_allowed?(due_date)
return false unless due_date

[2, 3].include?(due_date.teammate_review_allowed_id)
end

def self.calculate_assignment_round(assignment_id, response)
return 0 unless ResponseMap.find(response.map_id).type == 'ReviewResponseMap'

due_dates = DueDate.where(parent_id: assignment_id)
sorted_deadlines = deadline_sort(due_dates)
determine_assignment_round(response, sorted_deadlines)
end

def self.determine_assignment_round(response, sorted_due_dates)
round = 1
sorted_due_dates.each do |due_date|
break if response.created_at < due_date.due_at
round += 1 if due_date.deadline_type_id == 2
end
round
end


def self.find_current_due_date(due_dates)
due_dates.find { |due_date| due_date.due_at > Time.now }
end

def self.is_teammate_review_allowed(student)
due_date = find_current_due_date(student.assignment.due_dates)
return true if student.assignment.find_current_stage == 'Finished'
return true if teammate_review_allowed?(due_date)

false
end

def self.teammate_review_allowed?(due_date)
return false unless due_date

[2, 3].include?(due_date.teammate_review_allowed_id)
end

def self.copy(old_assignment_id, new_assignment_id)
duedates = DueDate.where(parent_id: old_assignment_id)

ActiveRecord::Base.transaction do
duedates.each do |orig_due_date|
duplicate_due_date(orig_due_date, new_assignment_id)
end
end
end

def self.duplicate_due_date(orig_due_date, new_assignment_id)
new_due_date = orig_due_date.dup
new_due_date.parent_id = new_assignment_id
new_due_date.save
end

def self.get_next_due_date(assignment_id, topic_id = nil)
if Assignment.find(assignment_id).staggered_deadline?
next_due_date = find_next_topic_due_date(assignment_id, topic_id)
else
next_due_date = AssignmentDueDate.find_by(['parent_id = ? && due_at >= ?', assignment_id, Time.zone.now])
end
next_due_date
end

def self.find_next_topic_due_date(assignment_id, topic_id)
next_due_date = TopicDueDate.find_by(['parent_id = ? and due_at >= ?', topic_id, Time.zone.now])
# if certion TopicDueDate is not exist, we should query next corresponding AssignmentDueDate.
# eg. Time.now is 08/28/2016
# One topic uses following deadlines:
# TopicDueDate 08/01/2016
# TopicDueDate 08/02/2016
# TopicDueDate 08/03/2016
# AssignmentDueDate 09/04/2016
# In this case, we cannot find due_at later than Time.now in TopicDueDate.
# So we should find next corresponding AssignmentDueDate, starting with the 4th one, not the 1st one!
if next_due_date.nil?
topic_due_date_size = TopicDueDate.where(parent_id: topic_id).size
following_assignment_due_dates = AssignmentDueDate.where(parent_id: assignment_id)[topic_due_date_size..-1]

if following_assignment_due_dates
next_due_date = following_assignment_due_dates.find { |due_date| due_date.due_at >= Time.zone.now }
end
end

next_due_date
end
def self.copy(old_assignment_id, new_assignment_id)
duedates = DueDate.where(parent_id: old_assignment_id)

ActiveRecord::Base.transaction do
duedates.each do |orig_due_date|
duplicate_due_date(orig_due_date, new_assignment_id)
end
end
end

def self.duplicate_due_date(orig_due_date, new_assignment_id)
new_due_date = orig_due_date.dup
new_due_date.parent_id = new_assignment_id
new_due_date.save
end

def self.get_next_due_date(assignment_id, topic_id = nil)
if Assignment.find(assignment_id).staggered_deadline?
next_due_date = find_next_topic_due_date(assignment_id, topic_id)
else
next_due_date = AssignmentDueDate.find_by(['parent_id = ? && due_at >= ?', assignment_id, Time.zone.now])
end
next_due_date
end

def self.find_next_topic_due_date(assignment_id, topic_id)
next_due_date = TopicDueDate.find_by(['parent_id = ? and due_at >= ?', topic_id, Time.zone.now])
# if certion TopicDueDate is not exist, we should query next corresponding AssignmentDueDate.
# eg. Time.now is 08/28/2016
# One topic uses following deadlines:
# TopicDueDate 08/01/2016
# TopicDueDate 08/02/2016
# TopicDueDate 08/03/2016
# AssignmentDueDate 09/04/2016
# In this case, we cannot find due_at later than Time.now in TopicDueDate.
# So we should find next corresponding AssignmentDueDate, starting with the 4th one, not the 1st one!
if next_due_date.nil?
topic_due_date_size = TopicDueDate.where(parent_id: topic_id).size
following_assignment_due_dates = AssignmentDueDate.where(parent_id: assignment_id)[topic_due_date_size..-1]

if following_assignment_due_dates
next_due_date = following_assignment_due_dates.find { |due_date| due_date.due_at >= Time.zone.now }
end
end
next_due_date
end
end
8 changes: 3 additions & 5 deletions app/models/due_date.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class DueDate < ApplicationRecord
validate :due_at_is_valid_datetime
# has_paper_trail

# Validates if 'due_at' is a valid datetime, and raises an error if not.
def due_at_is_valid_datetime
if due_at.present?
begin
DateTime.parse(due_at.to_s)
rescue ArgumentError, StandardError
errors.add(:due_at, 'must be a valid datetime')
rescue ArgumentError, StandardError => e
errors.add(:due_at, "must be a valid datetime: #{e.message}")
end
end
nil
nil
end

end

0 comments on commit 3a2f3d4

Please sign in to comment.