diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cffe2df15..a3a278e0d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base include RenderExpand include RenderHelper include ControllerRescueHandler + include GitHelper + include LoggerHelper protect_from_forgery prepend: true, unless: -> { request.format.json? } @@ -202,55 +204,6 @@ class ApplicationController < ActionController::Base # end end - # 版本库目录空间 - def repo_namespace(user_login, shixun_identifier) - "#{user_login}/#{shixun_identifier}.git" - end - - # 版本库文件内容,带转码 - def git_fle_content(repo_path, path) - begin - content = GitService.file_content(repo_path: repo_path, path: path)["content"] - logger.info("@@@@@@@@@@@@@@@@@@#{content}") - - decode_content = nil - if content.present? - content = Base64.decode64(content) - cd = CharDet.detect(content) - logger.info "encoding: #{cd['encoding']} confidence: #{cd['confidence']}" - - decode_content = - if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8 - content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '}) - else - content.force_encoding('UTF-8') - end - end - - decode_content - rescue Exception => e - uid_logger_error(e.message) - raise Educoder::TipException.new("文档内容获取异常") - end - end - - # 更新文件代码 - # content: 文件内容;message:提交描述 - def update_file_content(content, repo_path, path, mail, username, message) - GitService.update_file(repo_path: repo_path, file_path: path, message: message, - content: content, author_name: username, author_email: mail) - end - - # 版本库Fork功能 - def project_fork(container, original_rep_path, username) - raise Educoder::TipException.new("fork源路径为空,fork失败!") if original_rep_path.blank? - # 将要生成的仓库名字 - new_repo_name = "#{username}/#{container.try(:identifier)}#{ Time.now.strftime("%Y%m%d%H%M%S")}" - uid_logger("start fork container: repo_name is #{new_repo_name}") - GitService.fork_repository(repo_path: original_rep_path, fork_repository_path: (new_repo_name + ".git")) - container.update_attributes!(:repo_name => new_repo_name) - end - def start_user_session(user) session[:user_id] = user.id session[:ctime] = Time.now.utc.to_i @@ -258,6 +211,8 @@ class ApplicationController < ActionController::Base end def user_setup + # reacct静态资源加载不需要走这一步 + return if params[:controller] == "main" # Find the current user User.current = find_current_user uid_logger("user_step: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous")) @@ -330,16 +285,6 @@ class ApplicationController < ActionController::Base end end - # 以用户id开始的日志定义 - def uid_logger(message) - Rails.logger.info("##:#{current_user.try(:id)} --#{message}") - end - - # 以用户id开始的日志定义 - def uid_logger_error(message) - Rails.logger.error("##:#{current_user.try(:id)} --#{message}") - end - ## 输出错误信息 def error_status(message = nil) @status = -1 diff --git a/app/controllers/concerns/git_helper.rb b/app/controllers/concerns/git_helper.rb new file mode 100644 index 000000000..0d8c91a55 --- /dev/null +++ b/app/controllers/concerns/git_helper.rb @@ -0,0 +1,52 @@ +module GitHelper + extend ActiveSupport::Concern + + # 版本库目录空间 + def repo_namespace(user_login, shixun_identifier) + "#{user_login}/#{shixun_identifier}.git" + end + + # 版本库文件内容,带转码 + def git_fle_content(repo_path, path) + begin + content = GitService.file_content(repo_path: repo_path, path: path)["content"] + logger.info("@@@@@@@@@@@@@@@@@@#{content}") + + decode_content = nil + if content.present? + content = Base64.decode64(content) + cd = CharDet.detect(content) + logger.info "encoding: #{cd['encoding']} confidence: #{cd['confidence']}" + + decode_content = + if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8 + content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '}) + else + content.force_encoding('UTF-8') + end + end + + decode_content + rescue Exception => e + uid_logger_error(e.message) + raise Educoder::TipException.new("文档内容获取异常") + end + end + + # 更新文件代码 + # content: 文件内容;message:提交描述 + def update_file_content(content, repo_path, path, mail, username, message) + GitService.update_file(repo_path: repo_path, file_path: path, message: message, + content: content, author_name: username, author_email: mail) + end + + # 版本库Fork功能 + def project_fork(container, original_rep_path, username) + raise Educoder::TipException.new("fork源路径为空,fork失败!") if original_rep_path.blank? + # 将要生成的仓库名字 + new_repo_name = "#{username}/#{container.try(:identifier)}#{ Time.now.strftime("%Y%m%d%H%M%S")}" + uid_logger("start fork container: repo_name is #{new_repo_name}") + GitService.fork_repository(repo_path: original_rep_path, fork_repository_path: (new_repo_name + ".git")) + container.update_attributes!(:repo_name => new_repo_name) + end +end \ No newline at end of file diff --git a/app/controllers/concerns/logger_helper.rb b/app/controllers/concerns/logger_helper.rb new file mode 100644 index 000000000..44d0448ce --- /dev/null +++ b/app/controllers/concerns/logger_helper.rb @@ -0,0 +1,16 @@ +module LoggerHelper + extend ActiveSupport::Concern + + extend LoggerHelper + + + # 以用户id开始的日志定义 + def uid_logger(message) + Rails.logger.info("##:#{current_user.try(:id)} --#{message}") + end + + # 以用户id开始的日志定义 + def uid_logger_error(message) + Rails.logger.error("##:#{current_user.try(:id)} --#{message}") + end +end \ No newline at end of file diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 252664139..cb3b5c929 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -937,7 +937,7 @@ class CoursesController < ApplicationController @page = params[:page] || 1 @page_size = params[:page_size] || 15 - @courses = Course.by_user(current_user).hidden(false).ended(false).deleted(0).by_keywords(params[:search]).distinct + @courses = Course.by_user(current_user).hidden(false).processing.not_deleted.by_keywords(params[:search]).distinct # @total_count = @courses.count # offset = 0 diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 00ae559b1..32a314c54 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -1,5 +1,5 @@ class ShixunsController < ApplicationController - before_action :require_login, except: [:download_file, :index] + before_action :require_login, except: [:download_file, :index, :menus] # before_action :check_auth, except: [:download_file, :index] before_action :find_shixun, except: [:index, :new, :create, :menus, :get_recommend_shixuns, :propaedeutics, diff --git a/app/models/course.rb b/app/models/course.rb index 61e48d350..e4ade6e9f 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -62,7 +62,7 @@ class Course < ApplicationRecord scope :hidden, ->(is_hidden = true) { where(is_hidden: is_hidden) } scope :ended, ->(is_end = true) { where(is_end: is_end) } scope :processing, -> { where(is_end: false) } - scope :not_deleted, -> { where(is_delete: false) } + scope :not_deleted, -> { where(is_delete: 0) } scope :deleted, ->(is_delete = 1) { where(is_delete: is_delete) } scope :by_user, ->(user) { joins(:course_members).where('course_members.user_id = ?', user.id).order(updated_at: :desc) } scope :by_keywords, lambda { |keywords| diff --git a/app/models/user.rb b/app/models/user.rb index 60765b852..ece69dc02 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -388,10 +388,10 @@ class User < ApplicationRecord # 用户是否有权限查看实训 def shixun_permission(shixun) # 性能优化:先处理不需要权限的实训(已发布并且没有单位权限限制的实训) - return true if manager_of_shixun?(shixun) # 实训管理员 return false if shixun.status != 2 || shixun.hidden # 隐藏或者未发布的实训:false return true if shixun.use_scope == 0 # 对所有学校公开 - return true if shixun.use_scope == 1 && shixun.shixun_schools.exists?(school_id: school_id) # 对部分高校公开 + return true if shixun.use_scope == 1 && (manager_of_shixun?(shixun) || shixun.shixun_schools.exists?(school_id: school_id)) # 对部分高校公开 + # return true if manager_of_shixun?(shixun) # 实训管理员 return false end diff --git a/app/services/users/course_service.rb b/app/services/users/course_service.rb index 6271db17b..9eb34917e 100644 --- a/app/services/users/course_service.rb +++ b/app/services/users/course_service.rb @@ -11,7 +11,7 @@ class Users::CourseService end def call - courses = category_scope_courses.deleted(false) + courses = category_scope_courses.not_deleted courses = status_filter(courses) @@ -38,9 +38,9 @@ class Users::CourseService case params[:status] when 'processing' then - relations.ended(false) + relations.processing when 'end' then - relations.ended(true) + relations.ended else relations end diff --git a/app/tasks/exercise_publish_task.rb b/app/tasks/exercise_publish_task.rb new file mode 100644 index 000000000..d0a52646b --- /dev/null +++ b/app/tasks/exercise_publish_task.rb @@ -0,0 +1,111 @@ +class ExercisePublishTask + include ExercisesHelper + include GitHelper + + def publish + Rails.logger.info("log--------------------------------exercise_publish start") + puts "--------------------------------exercise_publish start" + exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now) + exercises.each do |exercise| + exercise.update_column('exercise_status', 2) + course = exercise.course + tid_str = "" + course.teachers.find_each do |member| + tid_str += "," if tid_str != "" + tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + end + if exercise.unified_setting + course.student.find_each do |student| + tid_str += "," if tid_str != "" + tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + end + end + if tid_str != "" + tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str + ActiveRecord::Base.connection.execute tid_sql + end + + if exercise.exercise_users.count == 0 + str = "" + course.students.find_each do |student| + str += "," if str != "" + str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + end + + if str != "" + sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str + ActiveRecord::Base.connection.execute sql + end + end + + if exercise.course_acts.size == 0 + exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id) + end + end + + # 分组设置发布时间的测验 + exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800) + exercise_group_settings.each do |exercise_group| + exercise = exercise_group.exercise + if exercise.present? + course = exercise.course + exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1 + tid_str = "" + members = course.students.where(:course_group_id => exercise_group.course_group_id) + members.find_each do |member| + tid_str += "," if tid_str != "" + tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + end + if tid_str != "" + tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str + ActiveRecord::Base.connection.execute tid_sql + end + end + end + Rails.logger.info("log--------------------------------exercise_publish end") + puts "--------------------------------exercise_publish end" + end + + def end + exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now) + exercises.each do |exercise| + course = exercise.course + exercise.update_column('exercise_status', 3) + + exercise.exercise_users.each do |exercise_user| + if exercise_user.commit_status == 0 && !exercise_user.start_at.nil? + exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true) + + s_score = calculate_student_score(exercise, exercise_user.user)[:total_score] + exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0))) + if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? + exercise_user.update_attributes(:subjective_score => 0) + end + end + end + end + + all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now) + exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")" + ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting| + exercise = exercise_setting.exercise + + users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id) + exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id)) + + exercise_users.each do |exercise_user| + if exercise_user.commit_status == 0 && !exercise_user.start_at.nil? + exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true) + + s_score = calculate_student_score(exercise, exercise_user.user)[:total_score] + exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0))) + if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? + exercise_user.update_attributes(:subjective_score => 0) + end + end + end + end + Rails.logger.info("log--------------------------------exercise_end completed") + end + +end \ No newline at end of file diff --git a/lib/tasks/exercise_publish.rake b/lib/tasks/exercise_publish.rake index bc9330b66..a26de1499 100644 --- a/lib/tasks/exercise_publish.rake +++ b/lib/tasks/exercise_publish.rake @@ -1,250 +1,268 @@ -#coding=utf-8 - -namespace :exercise_publish do - desc "publish exercise and end exercise" - def get_mulscore(question, user) - ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id) - arr = [] - ecs.each do |ec| - arr << ec.exercise_choice.choice_position - end - #arr = arr.sort - str = arr.sort.join("") - return str - end - - def tran_base64_decode64 str - if str.blank? - str - else - s_size = str.size % 4 - if s_size != 0 - str += "=" * (4 - s_size) - end - Base64.decode64(str.tr("-_", "+/")).force_encoding("utf-8") - end - end - - #计算试卷的总分和试卷的答题状态 - # def calculate_student_score(exercise,user) - # score1 = 0.0 #选择题/判断题 - # score2 = 0.0 #填空题 - # score5 = 0.0 #实训题 - # ques_stand = [] #问题是否正确 - # exercise_questions = exercise.exercise_questions - # exercise_questions.each do |q| - # if q.question_type != 5 - # answers_content = q.exercise_answers.search_answer_users("user_id",user.id) #学生的答案 - # else - # answers_content = q.exercise_shixun_answers.search_shixun_answers("user_id",user.id) #学生的答案 - # end - # if q.question_type <= 2 #为选择题或判断题时 - # answer_choice_array = [] - # answers_content.each do |a| - # answer_choice_array.push(a.exercise_choice.choice_position) #学生答案的位置 - # end - # user_answer_content = answer_choice_array.sort - # standard_answer = q.exercise_standard_answers.pluck(:exercise_choice_id).sort #该问题的标准答案,可能有多个 - # if user_answer_content == standard_answer #答案一致,多选或单选才给分,答案不对不给分 - # if standard_answer.count > 0 - # multi_each_score = (q.question_score / standard_answer.count) #当多选答案正确时,每个answer的分数均摊。 - # else - # multi_each_score = 0.0 - # end - # answers_content.update_all(:score => multi_each_score) - # score1 = score1 + q.question_score - # end - # elsif q.question_type == 3 #填空题 - # null_standard_answer = q.exercise_standard_answers - # standard_answer_array = null_standard_answer.select(:exercise_choice_id,:answer_text) - # standard_answer_ids = standard_answer_array.pluck(:exercise_choice_id).reject(&:blank?).uniq #标准答案的exercise_choice_id数组 - # standard_answer_count = standard_answer_ids.count - # if standard_answer_count > 0 #存在标准答案时才有分数 - # each_standard_score = (q.question_score.to_f / standard_answer_count).round(1) #每一空的得分 - # else - # each_standard_score = 0.0 - # end - # if q.is_ordered - # answers_content.each do |u| - # i_standard_answer = standard_answer_array.where(exercise_choice_id:u.exercise_choice_id).pluck(:answer_text).reject(&:blank?).map!(&:downcase) #该选项的全部标准答案 - # if i_standard_answer.include?(u.answer_text.downcase) #该空的标准答案包含用户的答案才有分数 - # u.update_attribute("score",each_standard_score) - # score2 = score2 + each_standard_score - # end - # end - # else - # st_answer_text = standard_answer_array.pluck(:answer_text).reject(&:blank?).map!(&:downcase) - # answers_content.each do |u| - # u_answer_text = u.answer_text.downcase - # if st_answer_text.include?(u_answer_text) #只要标准答案包含用户的答案,就有分数。同时,下一次循环时,就会删除该标准答案。防止用户的相同答案获分 - # u.update_attribute("score",each_standard_score) - # score2 = score2 + each_standard_score - # st_answer_text.delete(u_answer_text) - # end - # end - # end - # elsif q.question_type == 5 #实训题时,主观题这里不评分 - # q.exercise_shixun_challenges.each do |exercise_cha| - # game = Game.user_games(user.id,exercise_cha.challenge_id).first #当前用户的关卡 - # if game.present? - # exercise_cha_score = 0 - # answer_status = 0 - # cha_path = challenge_path exercise_cha.challenge.path - # if game.status == 2 && game.final_score >= 0 - # exercise_cha_score = game.real_score exercise_cha.question_score #每一关卡的得分 - # answer_status = 1 - # end - # if exercise_cha.exercise_shixun_answers.search_shixun_answers("user_id",user.id).blank? #把关卡的答案存入试卷的实训里 - # game_challenge = game.game_codes.search_challenge_path(cha_path).first - # if game_challenge.present? - # game_code = game_challenge - # code = game_code.try(:new_code) - # else - # code = git_fle_content(exercise_cha.shixun.repo_path,cha_path) - # end - # sx_option = { - # :exercise_question_id => q.id, - # :exercise_shixun_challenge_id => exercise_cha.id, - # :user_id => user.id, - # :score => exercise_cha_score, - # :answer_text => code, - # :status => answer_status - # } - # ex_shixun_answer = ExerciseShixunAnswer.new(sx_option) - # ex_shixun_answer.save! - # end - # score5 += exercise_cha_score - # end - # end - # end - # user_scores = answers_content.score_reviewed.pluck(:score).sum - # if user_scores > 0 - # stand_answer = 1 - # else - # stand_answer = 0 - # end - # ques_option = { - # "q_id":q.id, #该问题的id - # "q_type":q.question_type, - # "q_position":q.question_number, #该问题的位置 - # "stand_status":stand_answer, #该问题是否正确,1为正确,0为错误 - # "user_score":user_scores #每个问题的总得分 - # } - # ques_stand.push(ques_option) - # end - # total_score = score1 + score2 + score5 - # { - # "total_score":total_score, - # "stand_status":ques_stand - # } - # end - - task :publish => :environment do - Rails.logger.info("log--------------------------------exercise_publish start") - puts "--------------------------------exercise_publish start" - exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now) - exercises.each do |exercise| - exercise.update_column('exercise_status', 2) - course = exercise.course - tid_str = "" - course.teachers.find_each do |member| - tid_str += "," if tid_str != "" - tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" - end - if exercise.unified_setting - course.student.find_each do |student| - tid_str += "," if tid_str != "" - tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" - end - end - if tid_str != "" - tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str - ActiveRecord::Base.connection.execute tid_sql - end - - if exercise.exercise_users.count == 0 - str = "" - course.student.find_each do |student| - str += "," if str != "" - str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" - end - - if str != "" - sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str - ActiveRecord::Base.connection.execute sql - end - end - - if exercise.course_acts.size == 0 - exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id) - end - end - - # 分组设置发布时间的测验 - exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800) - exercise_group_settings.each do |exercise_group| - exercise = exercise_group.exercise - if exercise.present? - course = exercise.course - exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1 - tid_str = "" - members = course.students.where(:course_group_id => exercise_group.course_group_id) - members.find_each do |member| - tid_str += "," if tid_str != "" - tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" - end - if tid_str != "" - tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str - ActiveRecord::Base.connection.execute tid_sql - end - end - end - Rails.logger.info("log--------------------------------exercise_publish end") - puts "--------------------------------exercise_publish end" - end - - task :end => :environment do - # include ExercisesHelper - # include ApplicationController - - exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now) - exercises.each do |exercise| - course = exercise.course - exercise.update_column('exercise_status', 3) - - exercise.exercise_users.each do |exercise_user| - if exercise_user.commit_status == 0 && !exercise_user.start_at.nil? - exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true) - - s_score = calculate_student_score(exercise, exercise_user.user)[:total_score] - exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0))) - if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? - exercise_user.update_attributes(:subjective_score => 0) - end - end - end - end - - all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now) - exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")" - ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting| - exercise = exercise_setting.exercise - - users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id) - exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id)) - - exercise_users.each do |exercise_user| - if exercise_user.commit_status == 0 && !exercise_user.start_at.nil? - exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true) - - s_score = calculate_student_score(exercise, exercise_user.user)[:total_score] - exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0))) - if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? - exercise_user.update_attributes(:subjective_score => 0) - end - end - end - end - Rails.logger.info("log--------------------------------exercise_end completed") - end -end +# #coding=utf-8 +# +# namespace :exercise_publish do +# desc "publish exercise and end exercise" +# def get_mulscore(question, user) +# ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id) +# arr = [] +# ecs.each do |ec| +# arr << ec.exercise_choice.choice_position +# end +# #arr = arr.sort +# str = arr.sort.join("") +# return str +# end +# +# def tran_base64_decode64 str +# if str.blank? +# str +# else +# s_size = str.size % 4 +# if s_size != 0 +# str += "=" * (4 - s_size) +# end +# Base64.decode64(str.tr("-_", "+/")).force_encoding("utf-8") +# end +# end +# +# #计算试卷的总分和试卷的答题状态 +# def calculate_student_score(exercise,user) +# score1 = 0.0 #选择题/判断题 +# score2 = 0.0 #填空题 +# score5 = 0.0 #实训题 +# ques_stand = [] #问题是否正确 +# exercise_questions = exercise.exercise_questions +# exercise_questions.each do |q| +# if q.question_type != 5 +# answers_content = q.exercise_answers.search_answer_users("user_id",user.id) #学生的答案 +# else +# answers_content = q.exercise_shixun_answers.search_shixun_answers("user_id",user.id) #学生的答案 +# end +# if q.question_type <= 2 #为选择题或判断题时 +# answer_choice_array = [] +# answers_content.each do |a| +# answer_choice_array.push(a.exercise_choice.choice_position) #学生答案的位置 +# end +# user_answer_content = answer_choice_array.sort +# standard_answer = q.exercise_standard_answers.pluck(:exercise_choice_id).sort #该问题的标准答案,可能有多个 +# if user_answer_content == standard_answer #答案一致,多选或单选才给分,答案不对不给分 +# if standard_answer.count > 0 +# multi_each_score = (q.question_score / standard_answer.count) #当多选答案正确时,每个answer的分数均摊。 +# else +# multi_each_score = 0.0 +# end +# answers_content.update_all(:score => multi_each_score) +# score1 = score1 + q.question_score +# end +# elsif q.question_type == 3 #填空题 +# null_standard_answer = q.exercise_standard_answers +# standard_answer_array = null_standard_answer.select(:exercise_choice_id,:answer_text) +# standard_answer_ids = standard_answer_array.pluck(:exercise_choice_id).reject(&:blank?).uniq #标准答案的exercise_choice_id数组 +# standard_answer_count = standard_answer_ids.count +# if standard_answer_count > 0 #存在标准答案时才有分数 +# each_standard_score = (q.question_score.to_f / standard_answer_count).round(1) #每一空的得分 +# else +# each_standard_score = 0.0 +# end +# if q.is_ordered +# answers_content.each do |u| +# i_standard_answer = standard_answer_array.where(exercise_choice_id:u.exercise_choice_id).pluck(:answer_text).reject(&:blank?).map!(&:downcase) #该选项的全部标准答案 +# if i_standard_answer.include?(u.answer_text.downcase) #该空的标准答案包含用户的答案才有分数 +# u.update_attribute("score",each_standard_score) +# score2 = score2 + each_standard_score +# end +# end +# else +# st_answer_text = standard_answer_array.pluck(:answer_text).reject(&:blank?).map!(&:downcase) +# answers_content.each do |u| +# u_answer_text = u.answer_text.downcase +# if st_answer_text.include?(u_answer_text) #只要标准答案包含用户的答案,就有分数。同时,下一次循环时,就会删除该标准答案。防止用户的相同答案获分 +# u.update_attribute("score",each_standard_score) +# score2 = score2 + each_standard_score +# st_answer_text.delete(u_answer_text) +# end +# end +# end +# elsif q.question_type == 5 #实训题时,主观题这里不评分 +# q.exercise_shixun_challenges.each do |exercise_cha| +# game = Game.user_games(user.id,exercise_cha.challenge_id).first #当前用户的关卡 +# if game.present? +# exercise_cha_score = 0 +# answer_status = 0 +# cha_path = exercise_cha.challenge.path.present? ? exercise_cha.challenge.path.split(";").reject(&:blank?) : [] +# if game.status == 2 && game.final_score >= 0 +# exercise_cha_score = game.real_score exercise_cha.question_score #每一关卡的得分 +# answer_status = 1 +# end +# if exercise_cha.exercise_shixun_answers.search_shixun_answers("user_id",user.id).blank? #把关卡的答案存入试卷的实训里 +# game_challenge = game.game_codes.search_challenge_path(cha_path).first +# if game_challenge.present? +# game_code = game_challenge +# code = game_code.try(:new_code) +# else +# begin +# content = GitService.file_content(repo_path: exercise_cha.shixun.repo_path, path: cha_path)["content"] +# decode_content = nil +# if content.present? +# content = Base64.decode64(content) +# cd = CharDet.detect(content) +# decode_content = +# if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8 +# content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '}) +# else +# content.force_encoding('UTF-8') +# end +# end +# decode_content +# rescue Exception => e +# uid_logger_error(e.message) +# raise Educoder::TipException.new("文档内容获取异常") +# end +# code = git_fle_content(exercise_cha.shixun.repo_path,cha_path) +# end +# sx_option = { +# :exercise_question_id => q.id, +# :exercise_shixun_challenge_id => exercise_cha.id, +# :user_id => user.id, +# :score => exercise_cha_score, +# :answer_text => code, +# :status => answer_status +# } +# ex_shixun_answer = ExerciseShixunAnswer.new(sx_option) +# ex_shixun_answer.save! +# end +# score5 += exercise_cha_score +# end +# end +# end +# user_scores = answers_content.score_reviewed.pluck(:score).sum +# if user_scores > 0 +# stand_answer = 1 +# else +# stand_answer = 0 +# end +# ques_option = { +# "q_id":q.id, #该问题的id +# "q_type":q.question_type, +# "q_position":q.question_number, #该问题的位置 +# "stand_status":stand_answer, #该问题是否正确,1为正确,0为错误 +# "user_score":user_scores #每个问题的总得分 +# } +# ques_stand.push(ques_option) +# end +# total_score = score1 + score2 + score5 +# { +# "total_score":total_score, +# "stand_status":ques_stand +# } +# end +# +# task :publish => :environment do +# Rails.logger.info("log--------------------------------exercise_publish start") +# puts "--------------------------------exercise_publish start" +# exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now) +# exercises.each do |exercise| +# exercise.update_column('exercise_status', 2) +# course = exercise.course +# tid_str = "" +# course.teachers.find_each do |member| +# tid_str += "," if tid_str != "" +# tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" +# end +# if exercise.unified_setting +# course.student.find_each do |student| +# tid_str += "," if tid_str != "" +# tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" +# end +# end +# if tid_str != "" +# tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str +# ActiveRecord::Base.connection.execute tid_sql +# end +# +# if exercise.exercise_users.count == 0 +# str = "" +# course.student.find_each do |student| +# str += "," if str != "" +# str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" +# end +# +# if str != "" +# sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str +# ActiveRecord::Base.connection.execute sql +# end +# end +# +# if exercise.course_acts.size == 0 +# exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id) +# end +# end +# +# # 分组设置发布时间的测验 +# exercise_group_settings = ExerciseGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 1800, Time.now - 1800) +# exercise_group_settings.each do |exercise_group| +# exercise = exercise_group.exercise +# if exercise.present? +# course = exercise.course +# exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1 +# tid_str = "" +# members = course.students.where(:course_group_id => exercise_group.course_group_id) +# members.find_each do |member| +# tid_str += "," if tid_str != "" +# tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" +# end +# if tid_str != "" +# tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str +# ActiveRecord::Base.connection.execute tid_sql +# end +# end +# end +# Rails.logger.info("log--------------------------------exercise_publish end") +# puts "--------------------------------exercise_publish end" +# end +# +# task :end => :environment do +# # include ExercisesHelper +# # include ApplicationController +# +# exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now) +# exercises.each do |exercise| +# course = exercise.course +# exercise.update_column('exercise_status', 3) +# +# exercise.exercise_users.each do |exercise_user| +# if exercise_user.commit_status == 0 && !exercise_user.start_at.nil? +# exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true) +# +# s_score = calculate_student_score(exercise, exercise_user.user)[:total_score] +# exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0))) +# if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? +# exercise_user.update_attributes(:subjective_score => 0) +# end +# end +# end +# end +# +# all_exercises = Exercise.where("end_time > ? and exercise_status = 2",Time.now) +# exercise_ids = all_exercises.blank? ? "(-1)" : "(" + all_exercises.map(&:id).join(",") + ")" +# ExerciseGroupSetting.where("end_time <= '#{Time.now}' and exercise_id in #{exercise_ids}").each do |exercise_setting| +# exercise = exercise_setting.exercise +# +# users = exercise.course.students.where(:course_group_id => exercise_setting.course_group_id) +# exercise_users = exercise.exercise_users.where(:user_id => users.map(&:user_id)) +# +# exercise_users.each do |exercise_user| +# if exercise_user.commit_status == 0 && !exercise_user.start_at.nil? +# exercise_user.update_attributes(:commit_status => 1, :end_at => Time.now, :status => true) +# +# s_score = calculate_student_score(exercise, exercise_user.user)[:total_score] +# exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (exercise_user.subjective_score && exercise_user.subjective_score > 0 ? exercise_user.subjective_score : 0))) +# if exercise_user.user.exercise_answers.where(:exercise_question_id => exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? +# exercise_user.update_attributes(:subjective_score => 0) +# end +# end +# end +# end +# Rails.logger.info("log--------------------------------exercise_end completed") +# end +# end