diff --git a/app/controllers/student_works_controller.rb b/app/controllers/student_works_controller.rb index 526535157..9e37571b1 100644 --- a/app/controllers/student_works_controller.rb +++ b/app/controllers/student_works_controller.rb @@ -458,7 +458,7 @@ class StudentWorksController < ApplicationController @shixun = @homework.shixuns.take # 提示: 这里如果includes outputs表的话: sum(:evaluate_count)会出现错误 @games = @work.myshixun.games.joins(:challenge).reorder("challenges.position asc") if @work.myshixun - @comment = @work.student_works_scores.shixun_comment.first + @comment = @work.shixun_work_comments.find_by(challenge_id: 0) # 用户最大评测次数 if @games @@ -474,19 +474,29 @@ class StudentWorksController < ApplicationController # 实训作品的评阅 def shixun_work_comment - tip_exception("评阅不能为空") if params[:comment].blank? - tip_exception("缺少is_hidden参数") if params[:is_hidden].blank? || ![1, 0].include?(params[:is_hidden]) - comment = @work.student_works_scores.shixun_comment.first || StudentWorksScore.new(student_work_id: @work.id, user_id: current_user.id) - comment.comment = params[:comment] - comment.is_hidden = params[:is_hidden] - comment.save! - normal_status("评阅成功") + tip_exception("请至少输入一个评阅") if params[:comment].blank? && params[:hidden_comment].blank? + ActiveRecord::Base.transaction do + challenge = @homework.shixuns.first&.challenges.find_by(id: params[:challenge_id]) unless params[:challenge_id].blank? + if challenge.present? + comment = @work.shixun_work_comments.find_by(challenge_id: challenge.id) || + ShixunWorkComment.new(student_work_id: @work.id, user_id: current_user.id, challenge_id: challenge.id) + else + comment = @work.shixun_work_comments.find_by(challenge_id: 0) || + ShixunWorkComment.new(student_work_id: @work.id, user_id: current_user.id, challenge_id: 0) + end + comment.comment = params[:comment] + comment.hidden_comment = params[:hidden_comment] + comment.save! + normal_status("评阅成功") + end end # 删除实训作品评阅 def destroy_work_comment - @work.student_works_scores.shixun_comment.first.destroy! if @work.student_works_scores.shixun_comment.first.present? - normal_status("删除成功") + ActiveRecord::Base.transaction do + @work.shixun_work_comments.find_by!(id: params[:comment_id]).destroy_all + normal_status("删除成功") + end end def export_shixun_work_report diff --git a/app/models/shixun_work_comment.rb b/app/models/shixun_work_comment.rb new file mode 100644 index 000000000..7a67238ab --- /dev/null +++ b/app/models/shixun_work_comment.rb @@ -0,0 +1,5 @@ +class ShixunWorkComment < ApplicationRecord + belongs_to :student_work + belongs_to :user + belongs_to :challenge, optional: true +end diff --git a/app/models/student_work.rb b/app/models/student_work.rb index 168cfeb68..9c9efa17c 100644 --- a/app/models/student_work.rb +++ b/app/models/student_work.rb @@ -7,6 +7,7 @@ class StudentWork < ApplicationRecord belongs_to :myshixun, optional: true has_many :student_works_evaluation_distributions, dependent: :destroy has_many :student_works_scores, dependent: :destroy + has_many :shixun_work_comments, dependent: :destroy belongs_to :project, optional: true # attachtype: 1(正常提交的附件), 7(补交的附件) diff --git a/app/views/student_works/shixun_work_report.json.jbuilder b/app/views/student_works/shixun_work_report.json.jbuilder index 29a5c1874..e8e3af057 100644 --- a/app/views/student_works/shixun_work_report.json.jbuilder +++ b/app/views/student_works/shixun_work_report.json.jbuilder @@ -36,6 +36,9 @@ if @shixun challenge_score = @homework.challenge_score game.challenge_id json.game_score_full challenge_score json.game_score @work.work_challenge_score game, challenge_score + challenge_comment = @work.shixun_work_comments.find_by(challenge_id: game.challenge_id) + json.challenge_comment challenge_comment&.comment + json.challenge_comment_hidden challenge_comment&.hidden_comment end end @@ -54,8 +57,8 @@ if @shixun json.passed_time @work.myshixun&.passed_time # 评阅信息 - json.work_comment @user_course_identity < Course::STUDENT || !@comment&.is_hidden ? @comment&.comment : nil - json.work_comment_hidden @comment&.is_hidden + json.work_comment @comment&.comment + json.work_comment_hidden @user_course_identity < Course::STUDENT ? @comment&.hidden_comment : nil # 图形统计 # 1: 效率 diff --git a/db/migrate/20190909080508_create_shixun_work_comments.rb b/db/migrate/20190909080508_create_shixun_work_comments.rb new file mode 100644 index 000000000..eb21f0d04 --- /dev/null +++ b/db/migrate/20190909080508_create_shixun_work_comments.rb @@ -0,0 +1,13 @@ +class CreateShixunWorkComments < ActiveRecord::Migration[5.2] + def change + create_table :shixun_work_comments do |t| + t.references :student_work, index: true, type: :integer + t.references :challenge, index: true, type: :integer, default: 0 + t.references :user, index: true, type: :integer + t.text :comment + t.text :hidden_comment + + t.timestamps + end + end +end diff --git a/db/migrate/20190909082555_migrate_shixun_work_comment.rb b/db/migrate/20190909082555_migrate_shixun_work_comment.rb new file mode 100644 index 000000000..f58531b80 --- /dev/null +++ b/db/migrate/20190909082555_migrate_shixun_work_comment.rb @@ -0,0 +1,13 @@ +class MigrateShixunWorkComment < ActiveRecord::Migration[5.2] + def change + StudentWorksScore.where(is_ultimate: 0, score: nil).where("created_at > '2019-09-04 00:00:00'").each do |work_score| + if work_score.student_work && work_score.student_work.homework_common&.shixuns + if work_score.is_hidden + ShixunWorkComment.create!(student_work_id: work_score.student_work_id, user_id: work_score.user_id, hidden_comment: work_score.comment) + else + ShixunWorkComment.create!(student_work_id: work_score.student_work_id, user_id: work_score.user_id, comment: work_score.comment) + end + end + end + end +end diff --git a/spec/models/shixun_work_comment_spec.rb b/spec/models/shixun_work_comment_spec.rb new file mode 100644 index 000000000..0c40ede68 --- /dev/null +++ b/spec/models/shixun_work_comment_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ShixunWorkComment, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end