diff --git a/app/controllers/discusses_controller.rb b/app/controllers/discusses_controller.rb index 1d3b524f4..db3047e74 100644 --- a/app/controllers/discusses_controller.rb +++ b/app/controllers/discusses_controller.rb @@ -10,13 +10,13 @@ class DiscussesController < ApplicationController if current_user.admin? @disscuss_count = Discuss.where(:dis_id => @container.id, :dis_type => @container.class.to_s, :root_id => nil).count @discusses = Discuss.limit(LIMIT).where(:dis_id => @container.id, :dis_type => @container.class.to_s, - :root_id => nil).includes(:user, :praise_tread).offset(offset) + :root_id => nil).includes(:user, :praise_treads).offset(offset) else disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id is null and (hidden = :hidden or user_id = :user_id)", {dis_id: @container.id, dis_type: @container.class.to_s, hidden: false, user_id: current_user.id}) @disscuss_count = disscusses.count - @discusses = disscusses.limit(LIMIT).includes(:user, :praise_tread).offset(offset) + @discusses = disscusses.limit(LIMIT).includes(:user, :praise_treads).offset(offset) end @current_user = current_user diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 07113792d..e6f9876bc 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -1300,7 +1300,9 @@ class ExercisesController < ApplicationController if params[:format] == "xlsx" if @user_course_identity > Course::ASSISTANT_PROFESSOR tip_exception(403,"无权限操作") - elsif (@exercise_status == 1) || (@exercise_users_size == 0) || ( @export_ex_users&.exercise_user_committed.size == 0) + elsif @exercise_status == 1 + normal_status(-1,"试卷未发布") + elsif (@exercise_users_size == 0) || ( @export_ex_users&.exercise_user_committed.size == 0) normal_status(-1,"暂无用户提交") else respond_to do |format| diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index ea09b9003..c36be012f 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -40,10 +40,8 @@ class GamesController < ApplicationController next_game = @game.next_of_current_game(@shixun.id, @game.myshixun_id, game_challenge.position) # 关卡点赞数, praise_or_tread = 1则表示赞过 - praise_count = PraiseTread.where(praise_tread_object_id: game_challenge.id, praise_tread_object_type: "Challenge", - praise_or_tread: 1).count - user_praise = PraiseTread.where(praise_tread_object_id: game_challenge.id, praise_tread_object_type: "Challenge", - user_id: current_user.id, praise_or_tread: 1).present? ? true : false + praise_count = game_challenge.praises_count + user_praise = game_challenge.praise_treads.exists?(user_id:current_user.id, praise_or_tread: 1) # 实训的最大评测次数,这个值是为了优化查询,每次只取最新的最新一次评测的结果集 max_query_index = @game.query_index.to_i diff --git a/app/controllers/zips_controller.rb b/app/controllers/zips_controller.rb index 8553cbb44..4089e5231 100644 --- a/app/controllers/zips_controller.rb +++ b/app/controllers/zips_controller.rb @@ -6,12 +6,9 @@ class ZipsController < ApplicationController before_action :require_admin_or_teacher def shixun_report - # student_work_ids = Array.wrap(params[:student_work_ids]) - # service = BatchExportShixunReportService.new(@homework, @student_work_ids) service = BatchExportShixunReportService.new(@homework, @all_student_works) - filename = filename_for_content_disposition(service.filename) send_file service.zip, filename: filename, type: 'application/zip' rescue BatchExportShixunReportService::Error => ex @@ -23,8 +20,8 @@ class ZipsController < ApplicationController exercises = ExportExercisesService.new(@exercise,@ex_users,@request_url) file_name = filename_for_content_disposition(exercises.filename) - send_file exercises.ex_zip, filename: file_name, type: 'application/zip' + send_file exercises.ex_zip, filename: file_name, type: 'application/zip' rescue Exception => e normal_status(-1, e.message) end @@ -44,7 +41,7 @@ class ZipsController < ApplicationController ActiveRecord::Base.transaction do begin @exercise = Exercise.includes(:exercise_users,:exercise_questions).find_by(id:params[:exercise_id]) - @exercise_status = @exercise.get_exercise_status(current_user.id) + @exercise_status = @exercise.present? ? @exercise.get_exercise_status(current_user.id) : 1 group_id = params[:exercise_group_id] if @exercise.blank? normal_status(-1,"试卷不存在") @@ -101,34 +98,40 @@ class ZipsController < ApplicationController def load_homework @homework = HomeworkCommon.find(params[:homework_common_id]) + @homework_status = @homework.present? ? @homework.homework_detail_manual.comment_status : 0 + if @homework.blank? + normal_status(-1,"该作业不存在") + elsif @homework_status == 0 + normal_status(-1,"该作业未发布") + else + @course = @homework.course + ##7。2 -hs新增 + @member = @course.course_member(current_user.id) + + @all_student_works = @homework.teacher_works(@member).where("work_status > 0") + work_status = params[:work_status] + group_id = params[:course_group] + + if work_status.present? + @all_student_works = @all_student_works.where(work_status:work_status) + end + if group_id.present? + group_user_ids = @course.students.where(course_group_id: params[:course_group]).pluck(:user_id) + @all_student_works = @all_student_works.where(user_id: group_user_ids) + end - @course = @homework.course - ##7。2 -hs新增 - @member = @course.course_member(current_user.id) - @all_student_works = @homework.teacher_works(@member).where("work_status > 0") - work_status = params[:work_status] - group_id = params[:course_group] - - if work_status.present? - @all_student_works = @all_student_works.where(work_status:work_status) - end - - if group_id.present? - group_user_ids = @course.students.where(course_group_id: params[:course_group]).pluck(:user_id) - @all_student_works = @all_student_works.where(user_id: group_user_ids) - end - - unless params[:search].blank? - @all_student_works = @all_student_works.joins(user: :user_extension).where("concat(lastname, firstname) like ? + unless params[:search].blank? + @all_student_works = @all_student_works.joins(user: :user_extension).where("concat(lastname, firstname) like ? or student_id like ?", "%#{params[:search]}%", "%#{params[:search]}%") - end + end - student_work_sizes = @all_student_works&.size - if student_work_sizes.blank? || student_work_sizes == 0 - normal_status(-1,"暂无用户提交") - elsif student_work_sizes > 100 - normal_status(-2,"100") + student_work_sizes = @all_student_works&.size + if student_work_sizes.blank? || student_work_sizes == 0 + normal_status(-1,"暂无用户提交") + elsif student_work_sizes > 100 + normal_status(-2,"100") + end end end end diff --git a/app/models/challenge.rb b/app/models/challenge.rb index a4fd71830..7afb919a0 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -10,7 +10,7 @@ class Challenge < ApplicationRecord has_many :games, :dependent => :destroy has_many :challenge_chooses, :dependent => :destroy has_many :homework_challenge_settings, :dependent => :destroy - has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + has_many :praise_treads, as: :praise_tread_object, dependent: :destroy has_one :praise_tread_cache, as: :object, dependent: :destroy has_many :tidings # 参考答案 @@ -20,7 +20,7 @@ class Challenge < ApplicationRecord # acts_as_attachable scope :base_attrs, -> { select([:id, :subject, :position, :shixun_id, :st, :score, :path, :task_pass, :modify_time, - :web_route, :answer, :exec_time]) } + :web_route, :answer, :exec_time, :praises_count]) } scope :choose_type, -> { where(st: 1) } scope :practice_type, -> { where(st: 0) } diff --git a/app/models/discuss.rb b/app/models/discuss.rb index 93f0d62cd..737a89ec1 100644 --- a/app/models/discuss.rb +++ b/app/models/discuss.rb @@ -5,7 +5,7 @@ class Discuss < ApplicationRecord belongs_to :parent, class_name: 'Discuss', foreign_key: :parent_id, optional: true has_many :children, -> { reorder(created_at: :asc) }, class_name: 'Discuss', foreign_key: :parent_id - has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + has_many :praise_treads, as: :praise_tread_object, dependent: :destroy has_many :tidings, as: :container, dependent: :destroy has_one :praise_tread_cache, as: :object, dependent: :destroy diff --git a/app/services/batch_export_shixun_report_service.rb b/app/services/batch_export_shixun_report_service.rb index ebb273a87..2c294395d 100644 --- a/app/services/batch_export_shixun_report_service.rb +++ b/app/services/batch_export_shixun_report_service.rb @@ -3,7 +3,6 @@ class BatchExportShixunReportService MAX_BATCH_LIMIT = 20 - # attr_reader :homework, :student_work_ids attr_reader :homework, :all_student_works @@ -15,7 +14,7 @@ class BatchExportShixunReportService end def filename - @_filename ||= "#{Time.now.strftime('%Y%m%d%H%M%S')}-#{homework.name}.zip" + @_filename ||= "#{homework.name}-#{Time.now.strftime('%Y%m%d%H%M%S')}.zip" end def zip @@ -37,7 +36,7 @@ class BatchExportShixunReportService rescue => ex Rails.logger.error(ex.message) - zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write('文件重复') } + zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") } next end end @@ -45,15 +44,4 @@ class BatchExportShixunReportService end end - # private - # - # def validate! - # if student_work_ids.size.zero? - # raise Error, '请选择学生实训作业' - # end - # - # if student_work_ids.size > MAX_BATCH_LIMIT - # raise Error, '导出实训报告太多,请分批导出' - # end - # end end diff --git a/app/services/shixuns_service.rb b/app/services/shixuns_service.rb index 83d61b352..4efefd4c2 100644 --- a/app/services/shixuns_service.rb +++ b/app/services/shixuns_service.rb @@ -47,7 +47,7 @@ class ShixunsService if current_user.admin? disscuss_count = Discuss.where(dis_id: dis_id, dis_type: dis_type, root_id: nil).count discusses = Discuss.where(dis_id: dis_id, dis_type: dis_type, root_id: nil) - .includes(:user, :praise_tread).limit(LIMIT).offset(offset) + .includes(:user, :praise_treads).limit(LIMIT).offset(offset) else disscusses = Discuss.where( 'dis_id = :dis_id and dis_type = :dis_type and root_id is null and (hidden = :hidden or user_id = :user_id)', @@ -55,7 +55,7 @@ class ShixunsService ) disscuss_count = disscusses.count - discusses = disscusses.includes(:user, :praise_tread).limit(LIMIT).offset(offset) + discusses = disscusses.includes(:user, :praise_treads).limit(LIMIT).offset(offset) end base_data discusses, dis, current_user @@ -81,7 +81,7 @@ class ShixunsService offset = page * LIMIT find_status = true if position end - discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).includes(:user, :praise_tread).offset(offset) + discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).includes(:user, :praise_treads).offset(offset) base_data discusses, dis, current_user Myshixun.find(params[:myshixun_id]).update_attribute(:onclick_time, Time.now) @@ -109,8 +109,8 @@ class ShixunsService if discusses.present? discusses.each do |d| # 总点赞数 - praise_count = d.praise_tread.where(:praise_or_tread => 1).count - user_praise= d.praise_tread.select{|pt| pt.user_id == current_user.id}.length > 0 ? true : false + praise_count = d.praises_count + user_praise= d.praise_treads.select{|pt| pt.user_id == current_user.id}.length > 0 ? true : false manager = current_user.manager_of_shixun?(dis, current_user) game_url = if manager diff --git a/app/views/discusses/_discuss.json.jbuilder b/app/views/discusses/_discuss.json.jbuilder index e7e6e425f..992466559 100644 --- a/app/views/discusses/_discuss.json.jbuilder +++ b/app/views/discusses/_discuss.json.jbuilder @@ -14,8 +14,8 @@ json.game_url discuss.game_url(container, current_user) if discuss.parent_id json.can_delete discuss.can_deleted?(current_user) else - json.praise_count discuss.praise_tread.where(praise_or_tread: 1).count - json.user_praise discuss.praise_tread.select{|pt| pt.user_id == current_user.id}.length > 0 ? true : false + json.praise_count discuss.praises_count + json.user_praise discuss.praise_treads.select{|pt| pt.user_id == current_user.id}.length > 0 end diff --git a/db/migrate/20190705011739_add_praises_count_for_challenges.rb b/db/migrate/20190705011739_add_praises_count_for_challenges.rb new file mode 100644 index 000000000..6ae1d8420 --- /dev/null +++ b/db/migrate/20190705011739_add_praises_count_for_challenges.rb @@ -0,0 +1,5 @@ +class AddPraisesCountForChallenges < ActiveRecord::Migration[5.2] + def change + add_column :challenges, :praises_count, :integer, :default => 0 + end +end diff --git a/db/migrate/20190705013204_modify_praises_count_for_challenges.rb b/db/migrate/20190705013204_modify_praises_count_for_challenges.rb new file mode 100644 index 000000000..59aa6ca53 --- /dev/null +++ b/db/migrate/20190705013204_modify_praises_count_for_challenges.rb @@ -0,0 +1,9 @@ +class ModifyPraisesCountForChallenges < ActiveRecord::Migration[5.2] + def change + challenges = Challenge.where(nil).unscoped + challenges.find_each do |c| + praises_count = c.praise_treads.where(praise_or_tread: 1).count + c.update_column(:praises_count, praises_count) + end + end +end diff --git a/db/migrate/20190705022502_add_praises_count_for_discuess.rb b/db/migrate/20190705022502_add_praises_count_for_discuess.rb new file mode 100644 index 000000000..47fd5ec1a --- /dev/null +++ b/db/migrate/20190705022502_add_praises_count_for_discuess.rb @@ -0,0 +1,5 @@ +class AddPraisesCountForDiscuess < ActiveRecord::Migration[5.2] + def change + add_column :discusses, :praises_count, :integer, :default => 0 + end +end diff --git a/db/migrate/20190705022737_modify_praises_count_for_discuess.rb b/db/migrate/20190705022737_modify_praises_count_for_discuess.rb new file mode 100644 index 000000000..4d5b6ca97 --- /dev/null +++ b/db/migrate/20190705022737_modify_praises_count_for_discuess.rb @@ -0,0 +1,9 @@ +class ModifyPraisesCountForDiscuess < ActiveRecord::Migration[5.2] + def change + discusses = Discuss.includes(:praise_treads).unscoped + discusses.find_each do |d| + praises_count = d.praise_treads.liker.count + d.update_column(:praises_count, praises_count) + end + end +end