diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rb index 051c71c..048129f 100644 --- a/app/controllers/statuses_controller.rb +++ b/app/controllers/statuses_controller.rb @@ -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 @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/app/models/student_course_stats.rb b/app/models/student_course_stats.rb index 2b8029b..59d2d17 100644 --- a/app/models/student_course_stats.rb +++ b/app/models/student_course_stats.rb @@ -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 @@ -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