# encoding: utf-8 class CollegesController < ApplicationController before_filter :find_department, :only => [:statistics, :course_statistics, :student_shixun, :engineering_capability, :student_eval, :shixun_time, :shixun_report_count, :teachers, :shixun_chart_data, :student_hot_evaluations] before_filter :manager_auth, :except => [:home, :get_home_data] include ApplicationHelper # GET /colleges/id/home def home render file: 'public/react/build/index.html', :layout => false end # home action的数据 # GET /colleges/id/get_home_data def get_home_data banners = [] school = School.where(:identifier => params[:id]).first school.school_images.where(:status => 1).reorder("position asc").each do |image| banners << {link: image.link, image_path: url_to_avatar(image)} end attach = school.attachments.first render :json => {logo_link: url_to_avatar(school), video: "/attachments/download/" + "#{attach.id}" + "/" + "#{attach.filename}", video_name: school.video_name, video_desc: school.video_desc, course_link: school.course_link, course_name: school.course_name, banners: banners} end def verification_school_identifier schools = School.where(identifier: params[:identifier]) schools = schools.where('id != ?', params[:school_id]) if params[:school_id].present? render :json => { status: schools.exists? ? 1 : 0 } end def online_school @school = School.find params[:id] @school.update_attribute(:is_online, params[:status].to_i) end def statistics # 教师、学生总数 count_statistic = UserExtensions.where(school_id: @school.id).select('SUM(IF(identity=0, 1, 0)) AS teachers_count, SUM(IF(identity=1, 1, 0)) AS students_count').first @teachers_count = count_statistic['teachers_count'] @students_count = count_statistic['students_count'] # 课堂总数 @courses_count = Course.where(school_id: @school.id, is_delete: 0).where('id != 1309').count # 实训总数 @shixuns_count = Shixun.visible.joins('left join user_extensions on user_extensions.user_id = shixuns.user_id').where(user_extensions: { school_id: @school.id }).count respond_to do |format| format.html {render :layout => "base_edu"} end end def shixun_time time_sum = Game.joins('left join user_extensions on user_extensions.user_id = games.user_id').where(user_extensions: { school_id: @school.id }).sum(:cost_time) shixun_time_sum = (time_sum / (24 * 60 * 60.0)).ceil render json: { shixun_time: shixun_time_sum } end def shixun_report_count shixun_report_count = StudentWork.where(work_status: [1, 2]).where('myshixun_id != 0') .joins('left join user_extensions on user_extensions.user_id = student_works.user_id') .where(user_extensions: { school_id: @school.id }).count render json: { shixun_report_count: shixun_report_count } end def teachers @teachers = User.find_by_sql("SELECT users.id, users.login, users.lastname, users.firstname, users.nickname, IFNULL((SELECT count(shixuns.id) FROM shixuns where shixuns.user_id =users.id group by shixuns.user_id), 0) AS publish_shixun_count, (SELECT count(c.id) FROM courses c, course_members m WHERE c.id != 1309 and m.course_id = c.id AND m.role in (1,2,3) and c.school_id = #{@school.id} AND m.user_id=users.id AND c.is_delete = 0) as course_count FROM `users`, user_extensions ue where users.id=ue.user_id and ue.identity=0 and ue.school_id=#{@school.id} ORDER BY publish_shixun_count desc, course_count desc, id desc LIMIT 10") # ).order("publish_shixun_count desc, experience desc").limit(10) @teachers = @teachers.map do |teacher| course_ids = Course.find_by_sql("SELECT c.id FROM courses c, course_members m WHERE c.id != 1309 and m.course_id = c.id AND m.role in (1,2,3) AND m.user_id=#{teacher.id} AND c.is_delete = 0 and c.school_id = #{@school.id}") course_count = course_ids.size homeworks = HomeworkCommon.where(:homework_type => 4, :course_id => course_ids.map(&:id)) un_shixun_work_count = homeworks.where("publish_time > '#{Time.now}' or publish_time is null").count shixun_work_count = homeworks.size - un_shixun_work_count student_count = CourseMember.where(:course_id => course_ids.map(&:id), :role => 4).count myshixun_ids = StudentWork.select("myshixun_id").where("homework_common_id in (#{homeworks.map(&:id).join(',').strip == "" ? -1 : homeworks.map(&:id).join(',')}) and myshixun_id is not null") complete_myshixun = Myshixun.select("id").where(:status => 1, :id => myshixun_ids.map(&:myshixun_id)).size all_myshixun = Myshixun.select("id").where(:id => myshixun_ids.map(&:myshixun_id)).size complete_rate = all_myshixun == 0 ? 0 : ((complete_myshixun * 100) / all_myshixun).try(:round, 2).to_f real_name = teacher.show_real_name teacher = teacher.attributes.dup.merge({ real_name: real_name, course_count: course_count, shixun_work_count: shixun_work_count, un_shixun_work_count: un_shixun_work_count, student_count: student_count, complete_rate: complete_rate }).to_json JSON.parse(teacher) end end def shixun_chart_data shixun_ids = HomeworkCommonsShixuns.joins(homework_common: :course).where(courses: {school_id: @school.id, is_delete: 0}).where('courses.id != 1309').pluck('distinct shixun_id') shixun_count_map = ShixunTagRepertoire.joins(:tag_repertoire).where(shixun_id: shixun_ids).group('tag_repertoires.name').order('count_shixun_id desc').count(:shixun_id) names = [] data = [] shixun_count_map.each do |name, count| break if names.size == 9 names << name data << { value: count, name: name } end if shixun_count_map.keys.size > 9 other_count = shixun_count_map.values[9..-1].reduce(:+) names << 'Others' data << { name: 'Others', value: other_count } end render json: { names: names, data: data } end # 在线课堂 def course_statistics @courses = Course.find_by_sql("SELECT c.id, (select concat(lastname,firstname) from users u where u.id=c.tea_id) as username, (select count(cm.id) from course_members cm where c.id=cm.course_id and cm.role=4 group by c.id) as student_count, (select count(hc.id) from homework_commons hc where c.id=hc.course_id and hc.homework_type=4 group by c.id) as hcm_count, (select count(hc.id) from homework_commons hc where c.id=hc.course_id and hc.homework_type in (1,3) group by c.id) as hcm_nonshixun_count, (select count(e.id) from exercises e where c.id=e.course_id group by c.id) as exercises_count, (select count(p.id) from polls p where c.id=p.course_id group by c.id) as polls_count, (select count(a.id) from attachments a where c.id=a.container_id and a.container_type='Course' group by c.id) as attachments_count, (select count(m.id) from messages m inner join boards b on b.id=m.board_id and b.parent_id=0 where b.course_id=c.id group by c.id) as messages_count, c.tea_id, c.name, c.is_end, (SELECT MAX(created_at) FROM `course_activities` ca WHERE ca.course_id = c.id) AS update_time FROM `courses` c WHERE c.school_id = #{@school.id} and c.is_delete = 0") @courses.each do |course| course[:evaluating_count] = Output.find_by_sql("select sum(g.evaluate_count) as evaluating_count from games g inner join (select myshixun_id from student_works sw inner join homework_commons hc on sw.homework_common_id=hc.id and sw.myshixun_id !=0 and hc.course_id=#{course.id} and homework_type=4) aa on g.myshixun_id=aa.myshixun_id").first.try(:evaluating_count).to_i course[:task_count] = course.hcm_count.to_i + course.attachments_count.to_i + course.messages_count.to_i + course.hcm_nonshixun_count.to_i + course.exercises_count.to_i + course.polls_count.to_i end @courses = @courses.sort{|x,y| [y[:evaluating_count], y[:task_count]] <=> [x[:evaluating_count], x[:task_count]] } @courses = @courses.sort_by { |course| course.is_end ? 1 : 0 } # SELECT c.id, (select concat(firstname,lastname) from users u where u.id=c.tea_id) as username, # (select count(sfc.id) from students_for_courses sfc where c.id=sfc.course_id group by c.id) as student_count, # (select count(hc.id) from homework_commons hc where c.id=hc.course_id and hc.homework_type=4 group by c.id) as hcm_count, # c.tea_id, c.name, c.is_end, # (SELECT MAX(created_at) FROM `course_activities` ca WHERE ca.course_id = c.id) AS update_time # FROM `courses` c WHERE (c.school_id = 117 and c.is_delete = 0) ORDER BY update_time desc LIMIT 8 OFFSET 0 # @courses = Course.where("courses.school_id = #{@department.school_id} and courses.is_delete = 0").select("courses.id, courses.tea_id, courses.name, courses.is_end, # (SELECT MAX(created_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS update_time").order("update_time desc") @courses = paginateHelper @courses, 8 end # 学生实训 def student_shixun @students = User.joins(:user_extensions).where(user_extensions: { school_id: @school.id, identity: 1 }).includes(:user_extensions).order('experience desc').limit(10) student_ids = @students.map(&:id) @shixun_count = Myshixun.where(user_id: student_ids).group(:user_id).count @study_shixun_count = Myshixun.where(user_id: student_ids, status: 0).group(:user_id).count end def student_hot_evaluations games = Game.joins(:myshixun).joins('join shixun_tag_repertoires str on str.shixun_id = myshixuns.shixun_id') games = games.joins('join tag_repertoires tr on tr.id = str.tag_repertoire_id') games = games.joins("join user_extensions ue on ue.user_id = myshixuns.user_id and ue.school_id = #{@school.id}") evaluate_count_map = games.group('tr.name').reorder('sum_games_evaluate_count desc').limit(10).sum('games.evaluate_count') render json: { names: evaluate_count_map.keys, values: evaluate_count_map.values } end # 工程能力 def engineering_capability end # 学生测评 # day:日 week:周 mon:月 year:年 def student_eval # game_ids = Game.find_by_sql("SELECT games.id FROM games, users, user_extensions ue WHERE games.`user_id` = users.`id` AND users.id = ue.`user_id` AND ue.`school_id` = #{@department.school_id}").map(&:id) # game_ids = game_ids.join(',').strip == "" ? -1 : game_ids.join(',') @index = params[:index] || "day" end_day = Time.now - 1.day @time_data = [] @eval_data = [] case @index when "day" same_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(end_day - 365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day - 365.days).end_of_day}'").first.try(:count).to_i last_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(end_day - 1.day).beginning_of_day}' AND outputs.created_at < '#{(end_day - 1.day).end_of_day}'").first.try(:count).to_i @time_range = "#{end_day.strftime("%Y-%m-%d")}~#{end_day.strftime("%Y-%m-%d")}" @time_data = ["2:00", "4:00", "6:00", "8:00", "10:00", "12:00", "14:00", "16:00", "18:00", "20:00", "22:00", "24:00"] for i in 0..11 @eval_data << Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{end_day.beginning_of_day + (i * 2).hour}' AND outputs.created_at < '#{end_day.beginning_of_day + ((i + 1) * 2).hour}'").first.try(:count).to_i end when "week" start_day = end_day - 6.days same_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day - 365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day - 365.days).end_of_day}'").first.try(:count).to_i last_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day - 7.days).beginning_of_day}' AND outputs.created_at < '#{(end_day - 7.days).end_of_day}'").first.try(:count).to_i @time_range = "#{start_day.strftime("%Y-%m-%d")}~#{end_day.strftime("%Y-%m-%d")}" for i in 0..6 @time_data << (start_day + i.days).strftime("%m.%d") @eval_data << Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day + i.days).beginning_of_day}' AND outputs.created_at < '#{(start_day + (i + 1).days).end_of_day}'").first.try(:count).to_i end when "mon" start_day = end_day - 30.days same_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day - 365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day - 365.days).end_of_day}'").first.try(:count).to_i last_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day - 30.days).beginning_of_day}' AND outputs.created_at < '#{(end_day - 30.days).end_of_day}'").first.try(:count).to_i @time_range = "#{start_day.strftime("%Y-%m-%d")}~#{end_day.strftime("%Y-%m-%d")}" for i in 0..9 @time_data << (start_day + (3 * i).days).strftime("%m.%d") @eval_data << Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day + (3 * i).days).beginning_of_day}' AND outputs.created_at < '#{(start_day + (3 * (i + 1)).days).end_of_day}'").first.try(:count).to_i end when "year" start_day = end_day - 365.days same_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day - 365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day - 365.days).end_of_day}'").first.try(:count).to_i last_count = same_count @time_range = "#{start_day.strftime("%Y-%m-%d")}~#{end_day.strftime("%Y-%m-%d")}" for i in 0..11 @time_data << (start_day + i.month).strftime("%m.%d") @eval_data << Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day + i.days).beginning_of_day}' AND outputs.created_at < '#{(start_day + (i + 1).days).end_of_day}'").first.try(:count).to_i end end @new_count = @eval_data.sum @same_rate = (@new_count == 0 || same_count == 0) ? 0 : (@new_count * 1.0 / same_count - 1).to_f @last_rate = (@new_count == 0 || last_count == 0) ? 0 : (@new_count * 1.0 / last_count - 1).to_f @total_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 ").first.try(:count).to_i @trend_count = Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b ON outputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{Time.now - 7.days}'").first.try(:count).to_i end private def manager_auth # unless (User.current.admin? || DepartmentMember.where(:user_id => User.current.id, :department_id => @department.id).first.present?) unless (User.current.admin? || User.current.business? || (@department.present? && DepartmentMember.where(:user_id => User.current.id, :department_id => @department.id).first.present?) || (User.current.user_extensions.try(:school_id) == @school.id && User.current.user_extensions.try(:identity) == 0) || (@school.try(:customer_id) && User.current.try(:partner).try(:customer_ids) && User.current.try(:partner).try(:customer_ids).include?(@school.try(:customer_id)))) render_403 end end def find_department @department = Department.find_by_identifier(params[:id]) if @department.present? @school = @department.school else @school = School.find_by_id(params[:id]) end rescue ActiveRecord::RecordNotFound render_404 end end