diff --git a/Gemfile b/Gemfile index b8b174a6f..0473e221e 100644 --- a/Gemfile +++ b/Gemfile @@ -84,6 +84,7 @@ gem 'rails-i18n', '~> 5.1' # job gem 'sidekiq' +gem 'sinatra' # batch insert gem 'bulk_insert' diff --git a/Gemfile.lock b/Gemfile.lock index 86f524bc2..728bbc672 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -142,6 +142,7 @@ GEM multi_json (1.13.1) multi_xml (0.6.0) multipart-post (2.0.0) + mustermann (1.0.3) mysql2 (0.5.2) nio4r (2.3.1) nokogiri (1.8.4) @@ -265,6 +266,11 @@ GEM simple_xlsx_reader (1.0.4) nokogiri rubyzip + sinatra (2.0.5) + mustermann (~> 1.0) + rack (~> 2.0) + rack-protection (= 2.0.5) + tilt (~> 2.0) spreadsheet (1.2.3) ruby-ole (>= 1.0) spring (2.0.2) @@ -341,6 +347,7 @@ DEPENDENCIES selenium-webdriver sidekiq simple_xlsx_reader + sinatra spreadsheet spring spring-watcher-listen (~> 2.0.0) diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index c8fd042ea..2af178e47 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -200,7 +200,7 @@ class FilesController < ApplicationController # 资源设置 def update - return normal_status(403, "您没有权限进行该操作") unless current_user.teacher_or_admin?(@course) || @file.author == current_user + return normal_status(403, "您没有权限进行该操作") if current_user.course_identity(@course) >= 5 && @file.author != current_user is_unified_setting = params[:is_unified_setting] publish_time = params[:publish_time] diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index e595c703e..7e0cc9076 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -1117,7 +1117,7 @@ class HomeworkCommonsController < ApplicationController =end # 更新所有学生的效率分(重新取homework确保是更新后的) - HomeworksService.new.update_student_eff_score HomeworkCommon.find_by(id: homework.id) if !homework.allow_late && homework.end_time <= time + HomeworkEndUpdateScoreJob.perform_later(homework.id) if !homework.allow_late && homework.end_time <= time end end homework.save! diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index 9c3847b3c..e558e657c 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -18,7 +18,7 @@ class MyshixunsController < ApplicationController # REDO等删除是否可以做成异步 # 前段需要按照操作过程提示 def reset_my_game - unless (current_user.admin? || current_user.id == @myshixun.user_id) + unless current_user.admin? tip_exception("403", "") end diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index c73fec500..bb7059af6 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -81,6 +81,9 @@ class SubjectsController < ApplicationController @tags = ChallengeTag.where(challenge_id: challenge_ids).pluck(:name).uniq # 用户获取的实训标签 @user_tags = @subject.shixuns.map(&:user_tags_name).flatten.uniq + + # 访问数变更 + @subject.increment!(:visits) end def create diff --git a/app/jobs/homework_end_update_score_job.rb b/app/jobs/homework_end_update_score_job.rb new file mode 100644 index 000000000..ea6d3e5e3 --- /dev/null +++ b/app/jobs/homework_end_update_score_job.rb @@ -0,0 +1,32 @@ +class HomeworkEndUpdateScoreJob < ApplicationJob + # 不允许补交的作业截止后,或者补交截止后需要重新计算一次作业成绩 + queue_as :score + + def perform(*args) + homework = HomeworkCommon.find_by(id: homework_id) + return if homework.blank? + course = homework.course + return if course.blank? + + if homework.unified_setting + student_works = homework.student_works + user_ids = course.students.pluck(:user_id) + else + user_ids = course.students.where(course_group_id: homework.published_settings.pluck(:course_group_id)).pluck(:user_id) + student_works = homework.student_works.where(user_id: user_ids) + end + + myshixuns = Myshixun.where(shixun_id: params[:shixun_id], user_id: user_ids). + includes(:games).where(games: {challenge_id: homework.homework_challenge_settings.pluck(:challenge_id)}) + challenge_settings = homework.homework_challenge_settings + myshixuns.find_each(batch_size: 100) do |myshixun| + work = student_works.select{|work| work.user_id == myshixun.user_id}.first + if work && myshixun && (work.update_time.nil? || work.update_time < myshixun.updated_at) + games = myshixun.games.where(challenge_id: challenge_settings.pluck(:challenge_id)) + HomeworksService.new.update_myshixun_work_score work, myshixun, games, homework, challenge_settings + end + end + HomeworksService.new.update_student_eff_score homework + homework.update_attribute('calculation_time', Time.now) + end +end diff --git a/config/routes.rb b/config/routes.rb index a27dde1f3..01e301c3c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ Rails.application.routes.draw do + require 'sidekiq/web' + mount Sidekiq::Web => '/sidekiq' + resources :edu_settings scope '/api' do get 'home/index' diff --git a/lib/tasks/homework_publishtime.rake b/lib/tasks/homework_publishtime.rake index 4127a9339..918e70a70 100644 --- a/lib/tasks/homework_publishtime.rake +++ b/lib/tasks/homework_publishtime.rake @@ -84,7 +84,8 @@ namespace :homework_publishtime do student_works.joins(:myshixun).where("myshixuns.status != 1").update_all(late_penalty: homework.late_penalty) if student_works.present? else - HomeworksService.new.update_student_eff_score homework + HomeworkEndUpdateScoreJob.perform_later(homework.id) + # HomeworksService.new.update_student_eff_score homework end =begin @@ -206,7 +207,8 @@ namespace :homework_publishtime do homework_commons = HomeworkCommon.joins(:homework_detail_manual).where("homework_type = 4 and allow_late = 1 and late_time <= ? and late_time > ? and homework_detail_manuals.comment_status != 6", Time.now, Time.now - 900) homework_commons.each do |homework| - HomeworksService.new.update_student_eff_score homework + # HomeworksService.new.update_student_eff_score homework + HomeworkEndUpdateScoreJob.perform_later(homework.id) =begin homework_detail_manual = homework.homework_detail_manual diff --git a/spec/jobs/homework_end_update_score_job_spec.rb b/spec/jobs/homework_end_update_score_job_spec.rb new file mode 100644 index 000000000..492b35d43 --- /dev/null +++ b/spec/jobs/homework_end_update_score_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe HomeworkEndUpdateScoreJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end