Merge branch 'dev_aliyun' into dev_chen

PCqiandao
cxt 5 years ago
commit be46f75aca

@ -1,7 +1,7 @@
class AttendancesController < ApplicationController class AttendancesController < ApplicationController
before_action :require_login before_action :require_login
before_action :find_course, only: [:index, :student_attendances, :history_attendances] before_action :find_course, only: [:index, :statistics]
before_action :find_attendance, except: [:index, :student_attendances, :history_attendances]
before_action :user_course_identity before_action :user_course_identity
def index def index
@ -32,17 +32,23 @@ class AttendancesController < ApplicationController
end end
def statistics def statistics
end
def history_attendances
current_date = Date.current current_date = Date.current
current_end_time = Time.current.strftime("%H:%M:%S") 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}')")
all_member_attendances = CourseMemberAttendance.where(course_attendance_id: history_attendances)
if params[:group_id].present?
history_attendances = history_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 = @course.course_attendances.where("attendance_date < '#{current_date}' or history_attendances = history_attendances.order("attendance_date desc, start_time desc")
(attendance_date = '#{current_date}' and end_time < '#{current_end_time}')").order("id desc") data = AttendanceStatisticsService.call history_attendances, all_member_attendances
@all_history_count = @history_attendances.size @all_history_count = data[:all_history_count]
@history_attendances = paginate @history_attendances.includes(:course_member_attendances) @history_attendances = data[:history_attendances]
@avg_normal_rate = data[:avg_normal_rate]
@avg_absence_rate = data[:avg_absence_rate]
@avg_leave_rate = data[:avg_leave_rate]
end end
private private

@ -37,8 +37,14 @@ class Weapps::AttendancesController < ApplicationController
@all_member_attendances = @all_member_attendances.joins(:course_member).where(course_members: {course_group_id: params[:group_id]}) @all_member_attendances = @all_member_attendances.joins(:course_member).where(course_members: {course_group_id: params[:group_id]})
end end
@history_attendances = all_attendances.order("id asc") all_attendances = all_attendances.order("attendance_date desc, start_time desc")
@all_history_count = @history_attendances.size
data = AttendanceStatisticsService.call all_attendances, @all_member_attendances
@all_history_count = data[:all_history_count]
@history_attendances = data[:history_attendances]
@avg_normal_rate = data[:avg_normal_rate]
@avg_absence_rate = data[:avg_absence_rate]
@avg_leave_rate = data[:avg_leave_rate]
end end
def student_attendances def student_attendances

@ -0,0 +1,54 @@
class AttendanceStatisticsService < ApplicationService
attr_reader :attendances, :member_attendances
def initialize(attendances, member_attendances)
@attendances = attendances
@member_attendances = member_attendances
end
def call
all_normal_rate = []
all_absence_rate = []
all_leave_rate = []
history_attendances = []
attendances.each do |attendance|
normal_count = history_member_count(member_attendances, "NORMAL", attendance.id)
absence_count = history_member_count(member_attendances, "ABSENCE", attendance.id)
leave_count = history_member_count(member_attendances, "LEAVE", attendance.id)
all_count = member_attendances.select{|member_attendance| member_attendance.course_attendance_id == attendance.id}.size
normal_rate = cal_rate(normal_count, all_count)
all_normal_rate << normal_rate
absence_rate = cal_rate(absence_count, all_count)
all_absence_rate << absence_rate
leave_rate = cal_rate(leave_count, all_count)
all_leave_rate << leave_rate
history_attendances << {name: attendance.name, attendance_date: attendance.attendance_date.strftime("%Y-%m-%d"),
start_time: attendance.start_time.strftime("%H:%M"), end_time: attendance.end_time.strftime("%H:%M"),
normal_rate: normal_rate, absence_rate: absence_rate, leave_rate: leave_rate}
end
all_history_count = history_attendances.size
history_attendances = history_attendances[0..9].reverse
avg_normal_rate = cal_rate(all_normal_rate.sum, all_history_count)
avg_absence_rate = cal_rate(all_absence_rate.sum, all_history_count)
avg_leave_rate = cal_rate(all_leave_rate.sum, all_history_count)
{all_history_count: all_history_count, history_attendances: history_attendances, avg_normal_rate: avg_normal_rate,
avg_absence_rate: avg_absence_rate, avg_leave_rate: avg_leave_rate}
end
private
def history_member_count member_attendances, status, attendance_id
member_attendances.select{|member_attendance| member_attendance.attendance_status == status && member_attendance.course_attendance_id == attendance_id}.size
end
def cal_rate base, sum
sum == 0 ? 0 : (base.to_f / sum)
end
end

@ -0,0 +1,10 @@
json.history_attendances @history_attendances.each_with_index.to_a do |attendance, index|
json.(attendance, :name, :attendance_date, :start_time, :end_time, :normal_rate, :absence_rate, :leave_rate)
json.index "签到#{index + 1}"
end
json.all_history_count @all_history_count
json.avg_normal_rate @avg_normal_rate
json.avg_absence_rate @avg_absence_rate
json.avg_leave_rate @avg_leave_rate

@ -5,25 +5,12 @@ json.current_attendance @current_attendance do |attendance|
json.end_time attendance.end_time.strftime("%H:%M") json.end_time attendance.end_time.strftime("%H:%M")
end end
all_normal_rate = []
all_absence_rate = []
all_leave_rate = []
json.history_attendances @history_attendances.each_with_index.to_a do |attendance, index| json.history_attendances @history_attendances.each_with_index.to_a do |attendance, index|
normal_count = history_member_count(@all_member_attendances, "NORMAL", attendance.id) json.(attendance, :name, :attendance_date, :start_time, :end_time, :normal_rate, :absence_rate, :leave_rate)
absence_count = history_member_count(@all_member_attendances, "ABSENCE", attendance.id) json.index "签到#{index + 1}"
leave_count = history_member_count(@all_member_attendances, "LEAVE", attendance.id)
all_count = @all_member_attendances.select{|member_attendance| member_attendance.course_attendance_id == attendance.id}.size
json.index index + 1
json.normal_rate cal_rate(normal_count, all_count)
all_normal_rate << cal_rate(normal_count, all_count)
json.absence_rate cal_rate(absence_count, all_count)
all_absence_rate << cal_rate(absence_count, all_count)
json.leave_rate cal_rate(leave_count, all_count)
all_leave_rate << cal_rate(leave_count, all_count)
end end
json.all_history_count @all_history_count json.all_history_count @all_history_count
json.avg_normal_rate @all_history_count == 0 ? 0 : all_normal_rate.sum / @all_history_count json.avg_normal_rate @avg_normal_rate
json.avg_absence_rate @all_history_count == 0 ? 0 : all_absence_rate.sum / @all_history_count json.avg_absence_rate @avg_absence_rate
json.avg_leave_rate @all_history_count == 0 ? 0 : all_leave_rate.sum / @all_history_count json.avg_leave_rate @avg_leave_rate

@ -551,7 +551,7 @@ Rails.application.routes.draw do
end end
resources :attendances, shallow: true do resources :attendances, shallow: true do
get :statistics, on: :collection
end end
resources :polls, only:[:index,:new,:create] do resources :polls, only:[:index,:new,:create] do

@ -116,9 +116,9 @@ class DetailTop extends Component{
this.setState({ this.setState({
loadtype:true, loadtype:true,
Modalstype: true, Modalstype: true,
Modalstopval:` 课程需经过平台审核方可公开使用,公开的课程将对平台所`, Modalstopval:`课程发布后即可发送课堂使用`,
modalsMidval:"有人公开可见,若仅本人教学使用则无需申请公开,直接发", // modalsMidval:"有人公开可见,若仅本人教学使用则无需申请公开,直接发",
Modalsbottomval:"送到课堂即可。", // Modalsbottomval:"送到课堂即可。",
applyissuePaths:true applyissuePaths:true
}) })

Loading…
Cancel
Save