@ -1,9 +1,10 @@
class Weapps :: AttendancesController < ApplicationController
before_action :require_login
before_action :find_course , only : [ :create , :index , :student_attendances ]
before_action :find_attendance , except : [ :create , :index , :student_attendances ]
before_action :find_course , only : [ :create , :index , :student_attendances , :history_attendances ]
before_action :find_attendance , except : [ :create , :index , :student_attendances , :history_attendances ]
before_action :user_course_identity
before_action :teacher_allowed , only : [ :create ]
before_action :edit_auth , only : [ :update , :destroy , :end ]
def create
ActiveRecord :: Base . transaction do
@ -13,19 +14,34 @@ class Weapps::AttendancesController < ApplicationController
group_ids . each do | group_id |
@course . course_attendance_groups . create! ( course_group_id : group_id , course_attendance : attendance )
end
CreateStudentAttendanceRecordJob . perform_now ( attendance . id , group_ids )
else
@course . course_attendance_groups . create! ( course_group_id : 0 , course_attendance : attendance )
CreateStudentAttendanceRecordJob . perform_later ( attendance . id , [ 0 ] )
end
render_ok ( { attendance_id : attendance . id } )
end
end
def index
tip_exception ( 403 ) if @user_course_identity > = Course :: STUDENT
current_date = Date . current
current_end_time = Time . current . strftime ( " %H:%M:%S " )
@current_attendance = @course . course_attendances . where ( " attendance_date = ' #{ current_date } ' and end_time > ' #{ current_end_time } ' " ) . take
all_attendances = @course . course_attendances . where ( " attendance_date < ' #{ current_date } ' or (attendance_date = ' #{ current_date } ' and end_time < ' #{ current_end_time } ') " )
@all_member_attendances = CourseMemberAttendance . where ( course_attendance_id : all_attendances )
if params [ :group_id ] . present?
all_attendances = all_attendances . joins ( :course_attendance_groups ) . where ( course_attendance_groups : { course_group_id : [ params [ :group_id ] , 0 ] } )
@all_member_attendances = @all_member_attendances . joins ( :course_member ) . where ( course_members : { course_group_id : params [ :group_id ] } )
end
@history_attendances = all_attendances . order ( " id asc " )
@all_history_count = @history_attendances . size
end
def student_attendances
tip_exception ( 403 , " " ) if @user_course_identity != Course :: STUDENT
tip_exception ( " 学生身份的签到列表 " ) if @user_course_identity != Course :: STUDENT
member = @course . students . find_by ( user_id : current_user . id )
current_date = Date . current
current_end_time = Time . current . strftime ( " %H:%M:%S " )
@ -49,8 +65,8 @@ class Weapps::AttendancesController < ApplicationController
@leave_count = 0
@absence_count = 0
else
@normal_count = @course . course_member_attendances . where ( course_attendance_id : student_attendance_ids , attendance_status : 1 ) . size
@leave_count = @course . course_member_attendances . where ( course_attendance_id : student_attendance_ids , attendance_status : 2 ) . size
@normal_count = @course . course_member_attendances . where ( course_attendance_id : student_attendance_ids , attendance_status : " NORMAL " ) . size
@leave_count = @course . course_member_attendances . where ( course_attendance_id : student_attendance_ids , attendance_status : " LEAVE " ) . size
@absence_count = student_attendance_ids . uniq . size - @normal_count - @leave_count
end
@ -59,17 +75,49 @@ class Weapps::AttendancesController < ApplicationController
end
def show
@normal_count = @attendance . normal_count
@leave_count = @attendance . leave_count
@absence_count = @attendance . absence_count
@all_count = @attendance . course_member_attendances . size
@_is_current_attendance = @attendance . current_attendance?
if @attendance . course_attendance_groups . first & . course_group_id . to_i == 0
@groups = @course . course_groups
else
@groups = @course . course_groups . where ( id : @attendance . course_attendance_groups . pluck ( :course_group_id ) )
end
@groups = @groups . includes ( :course_members ) if @_is_current_attendance
@all_attendances = @attendance . course_member_attendances
end
def update
tip_exception ( 403 , " " ) unless @user_course_identity < Course :: PROFESSOR || @attendance . user_id == current_user . id
@attendance . update! ( name : params [ :name ] )
render_ok
end
def destroy
@attendance . destroy!
render_ok
end
def history_attendances
current_date = Date . current
current_end_time = Time . current . strftime ( " %H:%M:%S " )
@history_attendances = @course . course_attendances . where ( " attendance_date < ' #{ current_date } ' or
( attendance_date = '#{current_date}' and end_time < '#{current_end_time}' ) " ).order( " id desc " )
@all_history_count = @history_attendances . size
@history_attendances = paginate @history_attendances . includes ( :course_member_attendances )
end
def end
a_end_time = " #{ @attendance . attendance_date } #{ @attendance . end_time } " . to_time
tip_exception ( " 该签到已截止 " ) unless @attendance . current_attendance?
@attendance . update! ( end_time : Time . current )
render_ok
end
private
@ -81,4 +129,8 @@ class Weapps::AttendancesController < ApplicationController
@attendance = CourseAttendance . find params [ :id ]
@course = @attendance . course
end
def edit_auth
tip_exception ( 403 , " " ) unless @user_course_identity < Course :: PROFESSOR || @attendance . user_id == current_user . id
end
end