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 :user_course_identity before_action :teacher_allowed, only: [:create] def create ActiveRecord::Base.transaction do attendance = @course.course_attendances.create!(create_params.merge(user_id: current_user.id)) unless params[:group_ids].blank? group_ids = @course.charge_group_ids(current_user) & params[:group_ids] group_ids.each do |group_id| @course.course_attendance_groups.create!(course_group_id: group_id, course_attendance: attendance) end else @course.course_attendance_groups.create!(course_group_id: 0, course_attendance: attendance) end render_ok({attendance_id: attendance.id}) end end def index end def student_attendances tip_exception(403, "") 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") # 先算出该学生所在分班的签到id # 分班id为0 表示签到不限制分班 group_ids = [member&.course_group_id.to_i, 0] all_attendance_ids = @course.course_attendance_groups.where(course_group_id: group_ids).pluck(:course_attendance_id) @history_attendances = @course.course_attendances.where(id: all_attendance_ids.uniq). where("attendance_date < '#{current_date}' or (attendance_date = '#{current_date}' and end_time < '#{current_end_time}')").order("id desc") @current_attendance = @course.course_attendances.where(id: all_attendance_ids.uniq). where("attendance_date = '#{current_date}' and start_time <= '#{current_end_time}' and end_time > '#{current_end_time}'").take @history_count = @history_attendances.size student_attendance_ids = @history_attendances.pluck(:id) student_attendance_ids += @current_attendance.present? ? [@current_attendance.id] : [] if student_attendance_ids.uniq.blank? @normal_count = 0 @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 @absence_count = student_attendance_ids.uniq.size - @normal_count - @leave_count end @all_history_count = @history_attendances.size @history_attendances = paginate @history_attendances.includes(:course_member_attendances) end def show 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 end private def create_params params.permit(:name, :mode, :attendance_date, :start_time, :end_time) end def find_attendance @attendance = CourseAttendance.find params[:id] @course = @attendance.course end end