diff --git a/Gemfile b/Gemfile index b9e1b4e2..9ff7d11f 100644 --- a/Gemfile +++ b/Gemfile @@ -62,6 +62,9 @@ gem 'elasticsearch-rails' gem 'oauth2' +# cronjob +gem 'whenever', require: false + #Ruby 2.2+ has removed test/unit from the core library. if RUBY_VERSION>='2.2' gem 'test-unit', '~> 3.0' diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4bdcff56..e684a7c9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -362,6 +362,13 @@ class ApplicationController < ActionController::Base true end + # 运营人员 + def require_business + unless (User.current.business? || User.current.admin?) + render_403 + end + end + def deny_access User.current.logged? ? render_403 : require_login end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index b65f2219..421e5da3 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1635,7 +1635,7 @@ class CoursesController < ApplicationController @course_modules = @course.course_modules.where(:hidden => 0) course_module_type = @course_modules.map(&:module_type) - @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? + @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? || User.current.business? if User.current.member_of_course?(@course) && !@is_teacher member = @course.members.where(:user_id => User.current.id).first if member.try(:course_group_id).to_i == 0 diff --git a/app/controllers/managements/schools_controller.rb b/app/controllers/managements/schools_controller.rb index 6684db66..6dfe84fb 100644 --- a/app/controllers/managements/schools_controller.rb +++ b/app/controllers/managements/schools_controller.rb @@ -1,20 +1,44 @@ class Managements::SchoolsController < Managements::BaseController before_filter :set_navigation_bar - before_filter :set_default_sort_params, only: :statistics + before_filter :contrast_column_select_options, only: [:data_contrast] def statistics @sub_type = 1 + params[:sort_by] ||= :teacher_count + params[:sort_direction] ||= :desc + schools = Management::SchoolReportService.new(params).call @schools = paginateHelper schools end + def yesterday_data + @sub_type = 2 + params[:sort_by] ||= :teacher_increase_count + params[:sort_direction] ||= :desc + + reports = Management::SchoolYesterdayDataService.new(params).call + @reports = paginateHelper reports + end + + def data_contrast + params[:contrast_column] = :teacher_increase_count if params[:contrast_column].blank? + params[:sort_direction] ||= :desc + params[:sort_by] ||= :percentage + + @obj_count, @reports = Management::SchoolDataContrastService.new(params).call + @obj_pages = Paginator.new(@obj_count, Management::SchoolDataContrastService::PAGE_SIZE, params[:page]) + rescue Management::SchoolDataContrastService::ParameterError + raise '参数错误' + end + private def set_navigation_bar @menu_type = 1 end - def set_default_sort_params - params[:sort_by] ||= :teacher_count - params[:sort_direction] ||= :desc + def contrast_column_select_options + @select_options = Management::SchoolDataContrastService::CONTRAST_COLUMN_LIST.map do |column| + [I18n.t("school_daily_report.#{column}"), column] + end end -end \ No newline at end of file +end diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index 1c454424..74560941 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -1,6 +1,6 @@ # encoding: utf-8 class ManagementsController < ApplicationController - before_filter :require_admin + before_filter :require_business layout 'base_management' include ManagementsHelper include SortHelper @@ -63,7 +63,6 @@ class ManagementsController < ApplicationController # 工程认证视频导入模板 def ec_template @template = EcTemplate.where(nil) - end def add_template @@ -197,6 +196,7 @@ class ManagementsController < ApplicationController if request.post? @user.nickname = params[:nickname] @user.lastname = params[:lastname] + @user.business = params[:business].to_i @user.firstname = "" @user.mail = params[:mail].strip == "" ? nil : params[:mail] @user.phone = params[:phone].strip == "" ? nil : params[:phone] @@ -3350,7 +3350,7 @@ end end @users = User.where(:id => user_id).where("#{sql}").includes(:apply_actions, user_extensions: [:department, :school]).order("last_login_on desc") - @xls_users = @users.reorder("created_on desc").limit(1000) #导出excel用户 + @xls_users = @users.reorder("created_on desc").limit(3000) #导出excel用户 @page = (params['page'] || 1).to_i @users_count = @users.count @limit = 20 @@ -4181,7 +4181,7 @@ end sheet1 = book.create_worksheet :name => "course" blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 sheet1.row(0).default_format = blue - sheet1.row(0).concat(["ID","课堂名称","成员","资源","普通作业"," 实训作业","试卷","私有","状态","创建者单位","创建者","动态时间"]) + sheet1.row(0).concat(["ID","课堂名称","成员","资源","普通作业"," 实训作业","试卷","私有","状态","创建者单位","创建者","动态时间","创建时间"]) count_row = 1 courses.each do |course| school = course.teacher.try(:user_extensions).try(:school).try(:name).blank? ? "--" : course.teacher.school_name @@ -4198,6 +4198,7 @@ end sheet1[count_row,9] = school sheet1[count_row,10] = teacher_name sheet1[count_row,11] = format_time(course.updatetime) + sheet1[count_row,12] = format_time(course.created_at) count_row += 1 end book.write xls_report diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 5cadc9a4..7ac4d849 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -12,6 +12,7 @@ class ShixunsController < ApplicationController before_filter :view_allow, :only => [:collaborators, :propaedeutics, :shixun_discuss, :ranking_list] before_filter :require_manager, :only => [ :settings, :add_script, :publish, :collaborators_delete, :shixun_members_added, :add_collaborators, :update, :destroy] before_filter :validation_email, :only => [:new] + before_filter :require_admin, :only => [:destroy] # 移动云ToC模式权限控制 # before_filter :ecloud_auth, :except => [:show, :index] diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d13db006..df2ce0cd 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -274,7 +274,7 @@ class UsersController < ApplicationController # 私信 def private_messages - if User.current == @user || User.current.admin? + if User.current == @user || User.current.admin? || User.current.business? @onclick_time = User.current.onclick_time.onclick_time User.current.onclick_time.update_attribute(:onclick_time, Time.now) @messages = PrivateMessage.find_by_sql("SELECT ui.* FROM (SELECT * FROM private_messages WHERE STATUS != 2 AND user_id = #{@user.id} ORDER BY id DESC) ui GROUP BY ui.target_id ORDER BY ui.send_time DESC") diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index c6fa78d9..729a9564 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -30,6 +30,17 @@ class WelcomeController < ApplicationController require 'simple_xlsx_reader' DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def local_init + LocalShixun.delete_all + LocalMirrorRepository.delete_all + LocalShixunTagRepertoire.delete_all + LocalChallenge.delete_all + LocalTestSet.delete_all + LocalChallengeTag.delete_all + render :json => {status: 0, message: "success"} + end + + def shixun_to_local identifiers = params[:identifiers].split(",") shixuns = Shixun.where(identifier: identifiers) @@ -62,7 +73,7 @@ class WelcomeController < ApplicationController if shixun.challenges.present? shixun.challenges.each do |challenge| new_challenge = LocalChallenge.new - new_challenge.attributes = challenge.attributes.dup.except("id","shixun_id","user_id") + new_challenge.attributes = challenge.attributes.dup.except("id","shixun_id","user_id", "test_set_score") new_challenge.local_shixun_id = local_shixun.id new_challenge.save! # 评测题,选择题暂时不考虑 @@ -98,6 +109,7 @@ class WelcomeController < ApplicationController def local_to_shixun ActiveRecord::Base.transaction do + shixun_list = [] LocalShixun.find_each do |local_shixun| identifier = generate_identifier shixun = Shixun.create!(name: local_shixun.name, description: local_shixun.description, user_id: User.current.id, @@ -141,7 +153,7 @@ class WelcomeController < ApplicationController if local_challenges.present? local_challenges.each do |local_challenge| new_challenge = Challenge.new - new_challenge.attributes = local_challenge.attributes.dup.except("id","local_shixun_id","user_id") + new_challenge.attributes = local_challenge.attributes.dup.except("id","local_shixun_id","user_id", "test_set_score") new_challenge.user_id = User.current.id new_challenge.shixun_id = shixun.id new_challenge.save! @@ -166,8 +178,9 @@ class WelcomeController < ApplicationController end end end - render :json => {status: 0, message: "success", identifier: shixun.identifier} + shixun_list << shixun.identifier end + render :json => {status: 0, message: "success", identifier: shixun_list} end end @@ -205,7 +218,7 @@ class WelcomeController < ApplicationController @tea_users = User.where(homepage_teacher: 1).includes(:user_extensions).limit(10).order("experience desc") @stu_users = User.includes(:user_extensions).where(user_extensions: {identity: 1}).limit(10).order("experience desc") - render :layout => 'educoder' + render :layout => 'base_local' end # 自动导入用户 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c02f7e5a..a2e5948a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -671,7 +671,7 @@ module ApplicationHelper when 1 case sub_type when 1 then "统计总表" - when 2 then "变化报表" + when 2 then "数据变化报表" end when 2 sub_type == 1 ? "课程列表" : (sub_type == 2? "课堂列表" : (sub_type == 3? "实训作业" : "项目列表")) @@ -3995,7 +3995,7 @@ module ApplicationHelper # Returns the javascript tags that are included in the html layout head def javascript_heads - tags = javascript_include_tag('jquery-1.8.3-ui-1.9.2-ujs-2.0.3', 'application', 'jquery.colorbox-min', 'baiduTemplate') + tags = javascript_include_tag('jquery-1.8.3-ui-1.9.2-ujs-2.0.3', 'jquery.colorbox-min', 'baiduTemplate') unless User.current.pref.warn_on_leaving_unsaved == '0' tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });") end @@ -4009,6 +4009,15 @@ module ApplicationHelper tags end + # 临时本地版 + def javascript_heads_local + tags = javascript_include_tag('jquery-1.8.3-ui-1.9.2-ujs-2.0.3', 'jquery.colorbox-min') + unless User.current.pref.warn_on_leaving_unsaved == '0' + tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });") + end + tags + end + def hubspot_head tags = javascript_include_tag('hubspot/messenger.min', 'hubspot/messenger-theme-future') tags << stylesheet_link_tag('hubspot/messenger', 'hubspot/messenger-theme-future', 'hubspot/messenger-theme-flat') diff --git a/app/libs/custom_sortable.rb b/app/libs/custom_sortable.rb index 721cbd1e..a8549452 100644 --- a/app/libs/custom_sortable.rb +++ b/app/libs/custom_sortable.rb @@ -11,7 +11,7 @@ module CustomSortable sort_by ||= self.class.sort_options[:default_by] sort_direction ||= self.class.sort_options[:default_direction] - return relations unless self.class.check_sort_parameter_validate(sort_by, sort_direction) + return relations unless self.class.check_sort_parameter_validate(sort_by.to_s, sort_direction.to_s) order_method = self.class.sort_options[:reorder] ? :reorder : :order relations.send(order_method, "#{sort_by} #{sort_direction}") diff --git a/app/models/open_source_project.rb b/app/models/open_source_project.rb index 0adc0957..93b91fbb 100644 --- a/app/models/open_source_project.rb +++ b/app/models/open_source_project.rb @@ -121,13 +121,6 @@ class OpenSourceProject < ActiveRecord::Base ApplyProjectMaster.delete_all "apply_type = '#{self.class}' AND apply_id = #{self.id} AND user_id = #{user.id}" end - def admin?(user) - if user.admin? or ApplyProjectMaster.find(:all, :conditions => ["user_id = ? and apply_type = 'OpenSourceProject' and apply_id = ? and status = ?", user.id, self.id, 2]).present? - return true - else - return false - end - end def reset_counters! self.class.reset_counters!(id) diff --git a/app/models/school_daily_report.rb b/app/models/school_daily_report.rb new file mode 100644 index 00000000..494eacfd --- /dev/null +++ b/app/models/school_daily_report.rb @@ -0,0 +1,3 @@ +class SchoolDailyReport < ActiveRecord::Base + belongs_to :school +end \ No newline at end of file diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 6ab98172..54882be1 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -28,6 +28,7 @@ class Shixun < ActiveRecord::Base :propaedeutics, :trainee, :major_id, :homepage_show, :webssh, :hidden, :fork_from, :can_copy, :modify_time, :reset_time, :git_url, :use_scope, :vnc, :evaluate_script, :image_text, :exec_time, :test_set_permission, :hide_code, :excute_time, :forbid_copy + belongs_to :creator, foreign_key: :user_id, class_name: 'User' has_many :users, :through => :shixun_members has_many :shixun_members, :dependent => :destroy has_one :repository, :dependent => :destroy diff --git a/app/services/games_service.rb b/app/services/games_service.rb index 8573c683..bddb03b7 100644 --- a/app/services/games_service.rb +++ b/app/services/games_service.rb @@ -17,7 +17,7 @@ class GamesService myshixun = Myshixun.min.find(game.myshixun_id) shixun = Shixun.min.find(myshixun.shixun_id) - unless (myshixun.user_id == current_user.id || current_user.admin? || current_user.id == shixun.try(:user_id) || current_user.is_certification_teacher) + unless (myshixun.user_id == current_user.id || current_user.admin? || current_user.business? || current_user.id == shixun.try(:user_id) || current_user.is_certification_teacher) return{:status => 403} end game_challenge = Challenge.min.find(game.challenge_id) diff --git a/app/services/management/school_data_contrast_service.rb b/app/services/management/school_data_contrast_service.rb new file mode 100644 index 00000000..a1ad4c24 --- /dev/null +++ b/app/services/management/school_data_contrast_service.rb @@ -0,0 +1,66 @@ +class Management::SchoolDataContrastService + ParameterError = Class.new(StandardError) + + PAGE_SIZE = 20 + CONTRAST_COLUMN_LIST = %w( + teacher_increase_count student_increase_count course_increase_count + shixun_increase_count active_user_count + ).freeze + + attr_reader :params, :sort_direction, :contrast_column + + def initialize(params) + @params = params + @sort_direction = params[:sort_direction].to_s + @contrast_column = params[:contrast_column].to_s + end + + def call + validate_parameter! + reports = SchoolDailyReport.select(select_columns) + + keyword = params[:keyword].try(:to_s).try(:strip) + if keyword.present? + reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%") + end + + reports = reports.group(:school_id) + count = reports.count.count + + [count, SchoolDailyReport.find_by_sql(query_report_sql(reports.to_sql))] + end + + private + def validate_parameter! + if %i[begin_date end_date other_begin_date other_end_date].any? { |key| params[key].blank? } + raise ParameterError + end + + unless %w(desc asc).include?(sort_direction) + raise ParameterError + end + + unless CONTRAST_COLUMN_LIST.include?(contrast_column) + raise ParameterError + end + end + + def format_date(date) + Time.zone.parse(date).strftime("%Y-%m-%d") + end + + def offset + (params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * PAGE_SIZE + end + + def select_columns + "school_id, school_name,"\ + "SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0)) total,"\ + "SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0)) other_total" + end + + def query_report_sql(from_sql) + "SELECT reports.*, (other_total - total) increase, (IF(other_total - total = 0, 0.0, round((other_total - total) / IF(total = 0, 1, total), 5))) percentage "\ + "FROM (#{from_sql}) reports ORDER BY percentage #{sort_direction} LIMIT #{PAGE_SIZE} OFFSET #{offset}" + end +end diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 9b794b7b..5a962bd5 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -29,16 +29,12 @@ class Management::SchoolReportService <<-SQL schools.id, schools.name, ( - SELECT COUNT(*) FROM users u - LEFT JOIN user_extensions ue ON ue.user_id = u.id - LEFT JOIN ec_school_users esu ON esu.user_id = u.id - WHERE esu.school_id = schools.id AND ue.identity = #{User::STUDENT} + SELECT COUNT(*) FROM user_extensions ue + WHERE ue.school_id = schools.id AND ue.identity = #{User::STUDENT} ) student_count, ( - SELECT COUNT(*) FROM users u - LEFT JOIN user_extensions ue ON ue.user_id = u.id - LEFT JOIN ec_school_users esu ON esu.user_id = u.id - WHERE esu.school_id = schools.id AND ue.identity = #{User::TEACHER} + SELECT COUNT(*) FROM user_extensions ue + WHERE ue.school_id = schools.id AND ue.identity = #{User::TEACHER} ) teacher_count, ( SELECT COUNT(*) FROM homework_commons hc diff --git a/app/services/management/school_yesterday_data_service.rb b/app/services/management/school_yesterday_data_service.rb new file mode 100644 index 00000000..238a7297 --- /dev/null +++ b/app/services/management/school_yesterday_data_service.rb @@ -0,0 +1,32 @@ +class Management::SchoolYesterdayDataService + include CustomSortable + + attr_reader :params + + sort_columns :student_increase_count, :teacher_increase_count, + :course_increase_count, :shixun_increase_count, :active_user_count, + default_by: :teacher_increase_count, default_direction: :desc + + def initialize(params) + @params = params + end + + def call + reports = SchoolDailyReport.where(date: yesterday) + + keyword = params[:keyword].try(:to_s).try(:strip) + if keyword.present? + reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%") + end + + reports = custom_sort(reports, params[:sort_by], params[:sort_direction]) + + reports + end + + private + def yesterday + # 每日凌晨5点为节点, 25日凌晨4点、3点、2点等等,未到更新数据时间点,看到的数据是:23日-24日的统计数据 + (Time.zone.now - 5.hours).beginning_of_day - 1.days + end +end diff --git a/app/tasks/statistic_school_daily_report_task.rb b/app/tasks/statistic_school_daily_report_task.rb new file mode 100644 index 00000000..3569351f --- /dev/null +++ b/app/tasks/statistic_school_daily_report_task.rb @@ -0,0 +1,44 @@ +class StatisticSchoolDailyReportTask + def call + School.find_each do |school| + # 新增教师和学生 + users = User.joins(:user_extensions) + .where(user_extensions: { school_id: school.id }) + + teacher_count = users.where(created_on: yesterday, user_extensions: { identity: User::TEACHER }).count + student_count = users.where(created_on: yesterday, user_extensions: { identity: User::STUDENT }).count + + # 活跃用户 + active_user_count = users.where(last_login_on: yesterday).count + + # 新增课堂 + course_count = school.courses.where(created_at: yesterday).count + + # 新增实训 + shixun_count = Shixun.joins(creator: :user_extensions) + .where('user_extensions.school_id = ?', school.id) + .where(created_at: yesterday).count + + create_params = { + school_id: school.id, school_name: school.name, teacher_increase_count: teacher_count, + student_increase_count: student_count, course_increase_count: course_count, + shixun_increase_count: shixun_count, active_user_count: active_user_count, date: current_date + } + SchoolDailyReport.create!(create_params) + end + end + + private + def current_date + @_current_date ||= Time.zone.now.beginning_of_day - 1.day + end + + def yesterday + @_yesterday ||= begin + # 每日凌晨5点为节点 + end_time = Time.zone.now.beginning_of_day + 5.hour + begin_time = end_time - 1.day + begin_time..end_time + end + end +end diff --git a/app/views/account/login.html.erb b/app/views/account/login.html.erb index a4af9782..83b6b152 100644 --- a/app/views/account/login.html.erb +++ b/app/views/account/login.html.erb @@ -73,15 +73,6 @@ <% end %> - - <%= render :partial => "account/copyright_info" %> diff --git a/app/views/layouts/_logined_header.html.erb b/app/views/layouts/_logined_header.html.erb index 6b282631..55cd14c9 100644 --- a/app/views/layouts/_logined_header.html.erb +++ b/app/views/layouts/_logined_header.html.erb @@ -6,25 +6,8 @@
职业路径
- -职业路径
- -