diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index c3bd777ae..f242f3438 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -186,6 +186,7 @@ class HomeworkCommonsController < ApplicationController @work_count = @student_works.size @work_excel = @student_works + @students = @course.students # 分页参数 page = params[:page] || 1 @@ -236,94 +237,51 @@ class HomeworkCommonsController < ApplicationController end def update_score - student_works = @homework.student_works - myshixuns = Myshixun.where(shixun_id: @homework.homework_commons_shixun&.shixun_id, user_id: @course.students.pluck(:user_id)) - myshixuns = Myshixun.where(shixun_id: @homework.homework_commons_shixun&.shixun_id). - joins("join course_members on myshixuns.user_id=course_members.user_id").where(course_members: {course_id: @course.id, role: 4}).includes(:games) - myshixuns.find_each(batch_size: 100) do |myshixun| - work = student_works.select{|work| work.user_id == myshixun.user_id}.first - setting_time = @homework.homework_group_setting work.user_id - - if setting_time.end_time.present? && (setting_time.end_time > Time.now || (@homework.allow_late && @homework.late_time && @homework.late_time > Time.now)) - #logger.info("#############setting_time: #{setting_time.end_time}") - - user_total_score = 0 - pass_consume_time = 0 - final_score = 0 - homework.homework_challenge_settings.each do |setting| - game = myshixun.games.where(:challenge_id => setting.challenge_id, :status => 2).first - unless game.nil? - pass_consume_time += (game.cost_time / 60.0).to_f - user_total_score += game.final_score.to_i < 0 ? 0 : game.challenge.score.to_i - adjust_score = work.challenge_work_scores.where(:challenge_id => setting.challenge_id).last - final_score += adjust_score.present? ? adjust_score.score : (homework.homework_detail_manual.answer_open_evaluation ? setting.score : (game.final_score > 0 ? game.real_score(setting.score) : 0)) - end - end - if work.work_status == 0 - is_complete = myshixun.is_complete? && (myshixun.done_time < setting_time.end_time) - work.work_status = setting_time.end_time > Time.now ? 1 : (is_complete ? 1 : 2) - work.late_penalty = setting_time.end_time > Time.now ? 0 : (is_complete ? 0 : homework.late_penalty) - work.commit_time = myshixun.created_at > setting_time.publish_time ? setting_time.publish_time : myshixun.created_at - work.myshixun_id = myshixun.id - end - - games = myshixun.games.where(:challenge_id => homework.homework_challenge_settings.map(&:challenge_id)) - myshixun_endtime = games.select{|game| game.status == 2}.size == games.size ? games.map(&:end_time).max : nil - if myshixun_endtime.present? - min_efficiency_changed = min_efficiency_changed.present? ? min_efficiency_changed : false - work.compelete_status = 1 - work.cost_time = myshixun_endtime.to_i - setting_time.publish_time.to_i - - efficiency = (pass_consume_time == 0 ? 0 : Math.log((user_total_score / pass_consume_time.to_f) + 1.0)) - work.efficiency = format("%.2f", efficiency) + begin + 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 - # 如果作业的最大效率值有变更则更新所有作品的效率分 - if homework.work_efficiency && homework.max_efficiency < work.efficiency - homework.update_column("max_efficiency", work.efficiency) - 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 - - work.update_time = Time.now - - work.final_score = final_score - score = work.final_score + work.eff_score - work.late_penalty - work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) unless work.ultimate_score - #logger.info("#############work_score: #{score}") - work.save! end - end - @homework.student_works.each do || - + @homework.update_attribute('calculation_time', Time.now) + normal_status("更新成功") + rescue Exception => e + uid_logger(e.message) + tip_exception(e.message) + raise ActiveRecord::Rollback end end def update_student_score - work = @homework.students_works.find_by(user_id: current_user.id) + work = @homework.student_works.find_by(user_id: current_user.id) myshixun = Myshixun.find_by(shixun_id: params[:shixun_id], user_id: current_user.id) - if work && myshixun - # 判断作品是否关联过myshixun - if work.myshixun_id.nil? - work.myshixun_id = myshixun.id - work.update_time = myshixun.updated_at - setting_time = @homework.homework_group_setting myshixun.user_id - games = myshixun.games.where(:challenge_id => @homework.homework_challenge_settings.pluck(:challenge_id)) - myshixun_endtime = games.select{|game| game.status == 2}.size == games.size ? games.map(&:end_time).max : nil - compelete_status = 0 - if myshixun_endtime.present? && myshixun_endtime < setting_time.end_time - if myshixun_endtime < setting_time.publish_time - compelete_status = 2 - else - compelete_status = 1 - end - end - games.each do |game| - unless game.nil? - pass_consume_time += (game.cost_time / 60.0).to_f - user_total_score += game.final_score.to_i < 0 ? 0 : game.challenge.score.to_i - adjust_score = work.challenge_work_scores.where(:challenge_id => setting.challenge_id).last - final_score += adjust_score.present? ? adjust_score.score : (homework.homework_detail_manual.answer_open_evaluation ? setting.score : (game.final_score > 0 ? game.real_score(setting.score) : 0)) - end + ActiveRecord::Base.transaction do + begin + if work && myshixun && (work.update_time.nil? || work.update_time < myshixun.updated_at) + challenge_settings = @homework.homework_challenge_settings + games = myshixun.games.where(challenge_id: challenge_settings.pluck(:challenge_id)) + HomeworksService.new.update_myshixun_work_score work, myshixun, games, @homework, challenge_settings + normal_status("更新成功") + else + normal_status("已是最新成绩") end + rescue Exception => e + uid_logger(e.message) + tip_exception(e.message) + raise ActiveRecord::Rollback end end end @@ -1036,7 +994,7 @@ class HomeworkCommonsController < ApplicationController if homework.course_acts.size == 0 homework.course_acts << CourseActivity.new(user_id: homework.user_id, course_id: homework.course_id) end - + # 发消息 HomeworkCommonPushNotifyJob.perform_later(homework.id, tiding_group_ids) else create_homework_group_settings(homework) @@ -1057,6 +1015,7 @@ class HomeworkCommonsController < ApplicationController homework.save! + # 更新学生状态及成绩 HomeworkPublishUpdateWorkStatusJob.perform_later(tiding_group_ids, homework.id) end normal_status(0, "发布成功") diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 055731168..fd5a8b233 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -335,6 +335,8 @@ module ApplicationHelper end def to_markdown(text) + request_url = request.base_url + return nil if text.blank? options = { :autolink => true, @@ -345,9 +347,11 @@ module ApplicationHelper :superscript => true, :tables => true } - markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,options) - markdown.render(text).html_safe + m_t = markdown.render(text) + ss = m_t&.include?("src=\"") ? m_t&.gsub("src=\"","src=\"#{request_url}") : m_t + Rails.logger.info("############_____________ss________________##################{ss}") + ss end end diff --git a/app/services/homeworks_service.rb b/app/services/homeworks_service.rb index 1e155dde0..48db76295 100644 --- a/app/services/homeworks_service.rb +++ b/app/services/homeworks_service.rb @@ -272,4 +272,67 @@ class HomeworksService end end end + + # 计算实训作品成绩 + def update_myshixun_work_score work, myshixun, games, homework, challenge_settings + user_total_score = 0 + pass_consume_time = 0 + final_score = 0 + setting_time = homework.homework_group_setting myshixun.user_id + homework_end_or_late_time = homework.allow_late ? homework.late_time : setting_time.end_time + games.each do |game| + # 在截止时间前通关的关卡才考虑得分 + if game.status == 2 && game.end_time <= homework_end_or_late_time + challenge_setting = challenge_settings.select{|setting| setting.challenge_id == game.challenge_id}.first + pass_consume_time += (game.cost_time / 60.0).to_f + user_total_score += game.final_score.to_i < 0 ? 0 : game.challenge.score.to_i + adjust_score = work.challenge_work_scores.where(:challenge_id => game.challenge_id).last + final_score += if adjust_score.present? + adjust_score.score + elsif homework.homework_detail_manual.answer_open_evaluation + challenge_setting.score + elsif game.final_score > 0 + game.real_score(challenge_setting.score) + else + 0 + end + end + end + + myshixun_endtime = games.select{|game| game.status == 2}.size == games.size ? games.map(&:end_time).max : nil + if myshixun_endtime.present? + work.cost_time = myshixun_endtime.to_i - setting_time.publish_time.to_i + + efficiency = (pass_consume_time == 0 ? 0 : Math.log((user_total_score / pass_consume_time.to_f) + 1.0)) + work.efficiency = format("%.2f", efficiency) + + if myshixun_endtime <= homework_end_or_late_time + work.compelete_status = myshixun_endtime < setting_time.publish_time ? 2 : 1 + + # 如果作业的最大效率值有变更则更新所有作品的效率分 + homework.update_column("max_efficiency", work.efficiency) if homework.work_efficiency && homework.max_efficiency < work.efficiency + end + end + + if work.work_status == 0 + is_complete = myshixun_endtime && (myshixun_endtime < setting_time.end_time) + if is_complete || (!homework.allow_late && myshixun.created_at < setting_time.end_time) + work.work_status = 1 + elsif homework.allow_late && myshixun.created_at < homework.late_time + work.work_status = 2 + end + work.late_penalty = work.work_status == 2 ? homework.late_penalty : 0 + work.commit_time = myshixun.created_at > setting_time.publish_time ? setting_time.publish_time : myshixun.created_at + work.myshixun_id = myshixun.id + end + + work.update_time = myshixun.updated_at + + work.final_score = final_score + score = work.final_score + work.eff_score - work.late_penalty + work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) unless work.ultimate_score + #logger.info("#############work_score: #{score}") + work.calculation_time = Time.now + work.save! + end end \ No newline at end of file diff --git a/app/templates/exercise_export/blank_exercise.html.erb b/app/templates/exercise_export/blank_exercise.html.erb index bdb86d9d9..9ee1d40f7 100644 --- a/app/templates/exercise_export/blank_exercise.html.erb +++ b/app/templates/exercise_export/blank_exercise.html.erb @@ -68,17 +68,17 @@ (<%= q&.question_score %>分)
- <%= convert_to_char((index+1).to_s)%><%= s.choice_text%> + <%= convert_to_char((index+1).to_s)%> + <%= to_markdown(s.choice_text) %>
<% end %> <% elsif q.question_type == 1 %> <% q.exercise_choices.each_with_index do |s,index| %>- <%= convert_to_char((index+1).to_s)%><%= s.choice_text%> + <%= convert_to_char((index+1).to_s)%> + <%= to_markdown(s.choice_text) %>
<% end %> <% elsif q.question_type == 2 %> @@ -101,7 +103,7 @@ <% q.exercise_choices.each_with_index do |s,index| %> - <%= s.choice_text %> + <%= to_markdown(s.choice_text) %> <% end %> diff --git a/app/views/homework_commons/_homework_public_navigation.json.jbuilder b/app/views/homework_commons/_homework_public_navigation.json.jbuilder index 2c548074c..243c6aecd 100644 --- a/app/views/homework_commons/_homework_public_navigation.json.jbuilder +++ b/app/views/homework_commons/_homework_public_navigation.json.jbuilder @@ -11,4 +11,5 @@ json.homework_id homework.id json.homework_type homework.homework_type if homework.homework_type == "practice" json.shixun_identifier homework.shixuns.take.try(:identifier) + json.shixun_id homework.shixuns.take.try(:id) end diff --git a/app/views/homework_commons/works_list.json.jbuilder b/app/views/homework_commons/works_list.json.jbuilder index 60441909b..a7dc292b8 100644 --- a/app/views/homework_commons/works_list.json.jbuilder +++ b/app/views/homework_commons/works_list.json.jbuilder @@ -16,6 +16,7 @@ json.work_public @homework.work_public if @user_course_identity < Course::STUDENT + json.calculation_time @homework.calculation_time if @homework.homework_type == "practice" # 教师身份的评阅、提交状态、分班过滤 if @homework.homework_type != "practice" json.teacher_comment teacher_comment @homework, @current_user.id @@ -30,7 +31,7 @@ elsif @user_course_identity == Course::STUDENT if @homework.homework_type == "practice" json.(@work, :id, :work_status, :update_time, :ultimate_score) - + json.calculation_time @work.calculation_time json.late_penalty @work.late_penalty if @homework.allow_late json.cost_time @work.myshixun.try(:total_cost_time) json.work_score work_score_format(@work.work_score, true, @score_open) @@ -94,7 +95,7 @@ if @homework.homework_type == "practice" json.user_login work.user.try(:login) json.user_name work.user.try(:real_name) json.student_id work.user.try(:student_id) - json.group_name @course.course_student(work.user_id).try(:course_group_name) + json.group_name @students.select{|student| student.user_id == work.user_id}.first.try(:course_group_name) end elsif @homework.homework_type == "group" || @homework.homework_type == "normal" json.anonymous_comment @homework.anonymous_comment @@ -133,7 +134,7 @@ elsif @homework.homework_type == "group" || @homework.homework_type == "normal" end json.student_id work.user.try(:student_id) - json.group_name @course.course_student(work.user_id).try(:course_group_name) + json.group_name @students.select{|student| student.user_id == work.user_id}.first.try(:course_group_name) if @homework.homework_type == "group" if @homework.homework_detail_group.base_on_project json.project_info project_info work, @current_user, @user_course_identity diff --git a/config/routes.rb b/config/routes.rb index f81d90267..0f47cda76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -338,6 +338,8 @@ Rails.application.routes.draw do get :publish_groups get :end_groups post :alter_name + get :update_score + get :update_student_score end collection do diff --git a/db/migrate/20190701013243_add_calculation_time_to_homework.rb b/db/migrate/20190701013243_add_calculation_time_to_homework.rb new file mode 100644 index 000000000..4a622ee92 --- /dev/null +++ b/db/migrate/20190701013243_add_calculation_time_to_homework.rb @@ -0,0 +1,6 @@ +class AddCalculationTimeToHomework < ActiveRecord::Migration[5.2] + def change + add_column :homework_commons, :calculation_time, :datetime + add_column :student_works, :calculation_time, :datetime + end +end