Skip to content
Browse files

change attendance calculation

  • Loading branch information...
1 parent 4515cbc commit 1585968b118d8ceeb4d50eedcfab4e98449e76f0 @felixtsai committed Dec 10, 2012
Showing with 50 additions and 8 deletions.
  1. +19 −0 app/models/attendance.rb
  2. +4 −3 app/models/session.rb
  3. +8 −2 app/models/student_assignment.rb
  4. +17 −1 db/seeds.rb
  5. +2 −2 spec/models/session_spec.rb
View
19 app/models/attendance.rb
@@ -6,6 +6,10 @@ class Attendance < ActiveRecord::Base
delegate :date, to: :session
+ validates_presence_of :session_id, :student_id
+ validate :student_already_in_class_validator, on: :create
+ validate :student_class_validator, on: :create
+
scope :on_time, where("status = ?", 'On time')
scope :late, where("status = ?", 'Late')
scope :by_date, order("created_at desc")
@@ -28,4 +32,19 @@ def self.attendance_count
end
@days.count
end
+
+ def student_already_in_class_validator
+ class_attendance = Session.find(self.session_id).attendances
+ if class_attendance.find_by_student_id(self.student_id)
+ errors[:base] << "Student already checked in for this class."
+ end
+ end
+
+ def student_class_validator
+ student_sessions = Student.find(self.student_id).afterschool_class.sessions
+ unless student_sessions.find_by_id(self.session_id)
+ errors[:base] << "Student is not in this class."
+ end
+ end
+
end
View
7 app/models/session.rb
@@ -12,7 +12,7 @@ class Session < ActiveRecord::Base
scope :by_date, order('date DESC')
# validates_with UniqueClassValidator
- validate :unique_class_validator
+ validate :unique_class_validator, on: :create
def session_student_count
#students in attendance (either on time or late)
@@ -32,16 +32,17 @@ def late_count
end
def overall_attendance_percent
- afterschool_class.students.count != 0 ? "#{((attendances.count.to_f) / (afterschool_class.students.count.to_f)*100).to_i}%" :"N/A"
+ afterschool_class.students.count != 0 ? "#{((attendances.count.to_f) / (afterschool_class.students.count.to_f)*100).to_i}%" : "N/A"
end
def overall_assignment_completion_percentage
- student_assignments.count != 0 ? "#{(student_assignments.completed.count.to_f / student_assignments.count.to_f * 100).to_i}%" : "N/A"
+ student_assignments.count != 0 ? "#{(student_assignments.completed.count.to_f / attendances.count.to_f * 100).to_i}%" : "N/A"
end
def subject_completion_percentage(subject)
total = student_assignments.select{|sa| sa.subject_name == subject}.count.to_f
completed = student_assignments.select{|sa| sa.subject_name == subject && sa.completion_time != nil}.count.to_f
+ completed = student_assignments.joins(:assignment).where(:subject )
total != 0 ? "#{(completed/total * 100).to_i}%" : "N/A"
end
View
10 app/models/student_assignment.rb
@@ -9,7 +9,13 @@ class StudentAssignment < ActiveRecord::Base
scope :completed, where("completion_time IS NOT NULL")
scope :by_current, joins(:assignment).where('assignments.due_date > ?', Time.zone.now.to_date)
- #Write validation so you cannot create two student-assignments for the same assigment for
- # the same student.
+ validate :duplicate_assignment_validator, on: :create
+
+ def duplicate_assignment_validator
+ student_homework = Student.find(self.student_id).student_assignments
+ if student_homework.find_by_assignment_id(self.assignment_id)
+ errors[:base] << "This assignment has already been assigned."
+ end
+ end
end
View
18 db/seeds.rb
@@ -15,4 +15,20 @@
Subject.find_or_create_by_name("Math")
Subject.find_or_create_by_name("Social Studies")
Subject.find_or_create_by_name("Science")
-Subject.find_or_create_by_name("Extra Credit")
+Subject.find_or_create_by_name("Extra Credit")
+
+Admin.create(email: "user@example.com", password: "foobar")
+@class = AfterschoolClass.create(grade_level_id: 1, teachers_attributes: {"0" => {salutation: "Mr.", first_name: "Janky", last_name: "Jank"}})
+@student1 = Student.create(first_name: 'Doctor', last_name: 'Who', afterschool_class_id: @class.id)
+@student2 = Student.create(first_name: 'Rose', last_name: 'Tyler', afterschool_class_id: @class.id)
+@student3 = Student.create(first_name: 'Martha', last_name: 'Jones', afterschool_class_id: @class.id)
+@student4 = Student.create(first_name: 'Donna', last_name: 'Noble', afterschool_class_id: @class.id)
+@student5 = Student.create(first_name: 'Jack', last_name: 'Harkness', afterschool_class_id: @class.id)
+@session = Session.create(date: Time.zone.now.to_date, start_time: Time.zone.now, afterschool_class_id: @class.id)
+@attendance1 = Attendance.create(student_id: @student1.id, session_id: @session.id)
+@attendance2 = Attendance.create(student_id: @student2.id, session_id: @session.id)
+@attendance3 = Attendance.create(student_id: @student3.id, session_id: @session.id)
+@attendance4 = Attendance.create(student_id: @student4.id, session_id: @session.id)
+@attendance1.update_attributes(status: "Late")
+@assignment1 = Assignment.create(description: "add things", due_date: Time.zone.now.to_date + 1, grade_level_id: 1, subject_id: 1)
+@assignment2 = Assignment.create(description: "congress", due_date: Time.zone.now.to_date + 1, grade_level_id: 1, subject_id: 2)
View
4 spec/models/session_spec.rb
@@ -73,12 +73,12 @@
it "should provide overall assignment completion percentage for the class" do
@student1.student_assignments.first.update_attributes(:completion_time => Time.now)
- @session.overall_assignment_completion_percentage.should == "10%"
+ @session.overall_assignment_completion_percentage.should == "12.5%"
end
it "should provide assignment completion percentage by subject" do
@student1.student_assignments.first.update_attributes(:completion_time => Time.now)
- @session.subject_completion_percentage(Subject.find(1).name).should == "20%"
+ @session.subject_completion_percentage(Subject.find(1).name).should == "25%"
end
end

0 comments on commit 1585968

Please sign in to comment.
Something went wrong with that request. Please try again.