# 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 = StudentsForCourse.where(:course_id => course_ids.map(&:id)).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.where(school_id: @school.id, is_delete: 0) @obj_count = courses.count courses = courses.joins(shixun_homework_commons: :student_works) .joins('join games on games.myshixun_id = student_works.myshixun_id') .select('courses.id, courses.name, courses.is_end, sum(games.evaluate_count) evaluating_count') .group('courses.id').order('is_end asc, evaluating_count desc') @obj_pages = Paginator.new @obj_count, 8, params['page'] @courses = courses.limit(@obj_pages.per_page).offset(@obj_pages.offset) course_ids = @courses.map(&:id) @student_count = StudentsForCourse.where(course_id: course_ids).group(:course_id).count @shixun_work_count = HomeworkCommon.where(homework_type: 4, course_id: course_ids).group(:course_id).count @attachment_count = Attachment.where(container_id: course_ids, container_type: 'Course').group(:container_id).count @message_count = Message.joins(:board).where(boards: { parent_id: 0, course_id: course_ids }).group('boards.course_id').count @active_time = CourseActivity.where(course_id: course_ids).group(:course_id).maximum(:created_at) @exercise_count = Exercise.where(course_id: course_ids).group(:course_id).count @poll_count = Poll.where(course_id: course_ids).group(:course_id).count @other_work_count = HomeworkCommon.where(homework_type: [1,3], course_id: course_ids).group(:course_id).count 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