You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/course_groups_controller.rb

65 lines
2.2 KiB

class CourseGroupsController < ApplicationController
before_action :require_login, :check_auth
before_action :set_group, except: [:create]
before_action :find_course, only: [:create]
before_action :teacher_or_admin_allowed
def create
tip_exception("分班名称不能为空") if params[:name].blank?
if @course.course_groups.where(name: params[:name]).count > 0
normal_status(-1, "已存在同名分班")
else
@course.course_groups.create!(name: params[:name], position: @course.course_groups.count + 1)
normal_status(0, "创建成功")
end
end
def destroy
ActiveRecord::Base.transaction do
begin
@course.course_groups.where("position > #{@group.position}").update_all("position = position - 1")
# 将该分班的学生转到未分班
@group.course_members.update_all(course_group_id: 0)
@group.destroy
rescue Exception => e
uid_logger_error(e.message)
tip_exception("删除分班失败")
end
end
end
# 分班重命名
def rename_group
tip_exception("名称不能为空") if params[:name].blank?
if @course.course_groups.where(name: params[:name]).count > 0
normal_status(-1, "已存在同名分班")
else
@group.update_attributes(name: params[:name].strip)
normal_status(0, "更新成功")
end
end
# 分班的拖动
def move_category
tip_exception("移动失败") if params[:position].blank?
unless params[:position].to_i == @group.position
if params[:position].to_i < @group.position
@course.course_groups.where("position < #{@group.position} and position >= ?", params[:position]).update_all("position = position + 1")
else
@course.course_groups.where("position > #{@group.position} and position <= ?", params[:position]).update_all("position = position - 1")
end
@group.update_attributes(position: params[:position])
normal_status(0, "移动成功")
else
normal_status(-1, "位置没有变化")
end
end
private
def set_group
@group = CourseGroup.find_by!(id: params[:id])
@course = @group.course
end
end