实训作业的排序调整

dev_hs
cxt 5 years ago
parent b039fd5839
commit bdd02b3dbd

@ -41,7 +41,6 @@ class CoursesController < ApplicationController
before_action :find_board, only: :board_list before_action :find_board, only: :board_list
before_action :validate_page_size, only: :mine before_action :validate_page_size, only: :mine
before_action :course_tasks, only: [:tasks_list, :update_task_position] before_action :course_tasks, only: [:tasks_list, :update_task_position]
before_action :find_container, only: [:update_task_position]
if RUBY_PLATFORM =~ /linux/ if RUBY_PLATFORM =~ /linux/
require 'simple_xlsx_reader' require 'simple_xlsx_reader'
@ -1150,17 +1149,20 @@ class CoursesController < ApplicationController
end end
def update_task_position def update_task_position
tip_exception("缺少position参数") if params[:position].blank? Rails.logger.info("#######task_ids_length#{params[:task_ids].uniq.length}#########task_count:#{@tasks.count}")
unless params[:position].to_i == @task.position tip_exception("task_ids参数有误") if params[:task_ids].blank? || params[:task_ids].uniq.length != @tasks.count
if params[:position].to_i < @task.position ActiveRecord::Base.transaction do
@tasks.where("position < #{@task.position} and position >= ?", params[:position]).update_all("position = position + 1") begin
else @tasks.each do |task|
@tasks.where("position > #{@task.position} and position <= ?", params[:position]).update_all("position = position - 1") position = params[:task_ids].reverse.index(task.id).to_i + 1
task.update_attributes!(position: position)
end
normal_status(0, "移动成功")
rescue Exception => e
uid_logger(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end end
@task.update_attributes(position: params[:position])
normal_status(0, "移动成功")
else
normal_status(-1, "位置没有变化")
end end
end end
@ -1247,24 +1249,24 @@ class CoursesController < ApplicationController
end end
end end
def find_container # def find_container
case params[:container_type] # case params[:container_type]
when 'shixun_homework', 'common_homework', 'group_homework' # when 'shixun_homework', 'common_homework', 'group_homework'
@task = HomeworkCommon.find_by(id: params[:container_id]) # @task = HomeworkCommon.find_by(id: params[:container_id])
when 'exercise' # when 'exercise'
@task = Exercise.find_by(id: params[:container_id]) # @task = Exercise.find_by(id: params[:container_id])
when 'poll' # when 'poll'
@task = Poll.find_by(id: params[:container_id]) # @task = Poll.find_by(id: params[:container_id])
when 'graduation_topic' # when 'graduation_topic'
@task = GraduationTopic.find_by(id: params[:container_id]) # @task = GraduationTopic.find_by(id: params[:container_id])
when 'graduation_task' # when 'graduation_task'
@task = GraduationTask.find_by(id: params[:container_id]) # @task = GraduationTask.find_by(id: params[:container_id])
when 'attachment' # when 'attachment'
@task = Attachment.find_by(id: params[:container_id]) # @task = Attachment.find_by(id: params[:container_id])
else # else
tip_exception("container_type参数有误") # tip_exception("container_type参数有误")
end # end
end # end
def student_act_score group_id, search def student_act_score group_id, search
sql_select = %Q{SELECT cm.*,( sql_select = %Q{SELECT cm.*,(

@ -9,6 +9,7 @@ class HomeworksService
homework_detail_manual = HomeworkDetailManual.new homework_detail_manual = HomeworkDetailManual.new
homework.homework_detail_manual = homework_detail_manual homework.homework_detail_manual = homework_detail_manual
homework.position = course.practice_homeworks.first&.position.to_i + 1
if homework.save! if homework.save!
homework_detail_manual.save! if homework_detail_manual homework_detail_manual.save! if homework_detail_manual

@ -1,13 +1,5 @@
class MigrateCourseTaskPosition < ActiveRecord::Migration[5.2] class MigrateCourseTaskPosition < ActiveRecord::Migration[5.2]
def change def change
add_column :homework_commons, :position, :integer, :default => 0 add_column :homework_commons, :position, :integer, :default => 0
Course.find_each do |course|
puts course.id
course.practice_homeworks.order("IF(ISNULL(homework_commons.publish_time),0,1), homework_commons.publish_time DESC,
homework_commons.created_at DESC").reverse.each_with_index do |homework, index|
homework.update_columns(position: index + 1)
end
end
end end
end end

@ -0,0 +1,11 @@
class MigrateCourseShixunHomeworkPosition < ActiveRecord::Migration[5.2]
def change
Course.find_each do |course|
puts course.id
course.practice_homeworks.order("IF(ISNULL(homework_commons.publish_time),0,1), homework_commons.publish_time DESC,
homework_commons.created_at DESC").reverse.each_with_index do |homework, index|
homework.update_columns(position: index + 1)
end
end
end
end
Loading…
Cancel
Save