Skip to content
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
50 changes: 46 additions & 4 deletions app/controllers/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def create
account_id: course.account_id
)

submit_grade!(status) if status.save
if status.save
submit_grade!(status)
handle_last_attended_date(status)
end
render_status(status)
else
not_acceptable
Expand All @@ -75,9 +78,14 @@ def create
def update
if status = Status.find_by(id: params[:id])
if load_and_authorize_section(status.section_id, status.tool_consumer_instance_guid)
previous_attendance = status.attendance
status.attendance = status_params[:attendance]
status.teacher_id = user_id
submit_grade!(status) if status.save

if status.save
submit_grade!(status)
handle_last_attended_date(status, previous_attendance)
end
render_status(status)
else
not_acceptable
Expand All @@ -90,7 +98,13 @@ def update
def destroy
if status = Status.find_by(id: params[:id])
if load_and_authorize_section(status.section_id, status.tool_consumer_instance_guid)
submit_grade!(status) if status.destroy
previous_attendance = status.attendance

if status.destroy
submit_grade!(status)
handle_last_attended_date(status, previous_attendance, recalculate: true)
end

render_status(status)
else
not_acceptable
Expand Down Expand Up @@ -134,4 +148,32 @@ def status_params
fixed: true
})
end
end

def handle_last_attended_date(status, previous_attendance = nil, recalculate: false)
if recalculate || (previous_attendance && ['present', 'late'].include?(previous_attendance) && !['present', 'late'].include?(status.attendance))
update_last_attended_date(status, recalculate: true)
elsif ['present', 'late'].include?(status.attendance)
update_last_attended_date(status)
end
end

def update_last_attended_date(status, recalculate: false)
last_date = if recalculate
StudentCourseStats.new(
status.student_id,
status.course_id,
status.section_id,
status.tool_consumer_instance_guid
).last_attended_date
else
status.class_date
end

submit_last_attended_date(status.course_id, status.student_id, last_date) if last_date
end

def submit_last_attended_date(course_id, student_id, date)
url = "#{canvas_url}/api/v1/courses/#{course_id}/users/#{student_id}/last_attended?date=#{date}"
canvas.authenticated_put(url, {})
end
end
12 changes: 11 additions & 1 deletion app/models/student_course_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def stats
presences: presences,
tardies: tardies,
absences: absences,
attendance_grade: grade
attendance_grade: grade,
last_attended_date: last_attended_date
}
end

Expand Down Expand Up @@ -71,6 +72,15 @@ def score
end
end

def last_attended_date
@last_attended_date ||= Status
.where(student_id: student_id, section_id: section_ids, course_id: course_id, tool_consumer_instance_guid: tool_consumer_instance_guid, attendance: ['present', 'late'])
.order(class_date: :desc)
.limit(1)
.pluck(:class_date)
.first
end

private

def tardy_weight
Expand Down