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_second_categories_co...

82 lines
3.6 KiB

6 years ago
class CourseSecondCategoriesController < ApplicationController
6 years ago
before_action :require_login, :check_auth
6 years ago
before_action :set_category
before_action :teacher_allowed
6 years ago
# 目录重命名
def rename_category
tip_exception("毕设子目录不能重命名") if @category.category_type == "graduation"
tip_exception("名称不能为空") if params[:name].blank?
tip_exception("已存在同名子目录") if @course_module.course_second_categories.exists?(parent_id: @category.parent_id, name: params[:name].strip)
@category.update_attributes!(name: params[:name].strip)
6 years ago
normal_status(0, "更新成功")
end
# 子目录的拖动
def move_category
tip_exception("移动失败") if params[:position].blank?
unless params[:position].to_i == @category.position
if params[:position].to_i < @category.position
@course_module.course_second_categories
.where("parent_id = #{@category.parent_id} and position < #{@category.position} and position >= ?", params[:position])
.update_all("position = position + 1")
6 years ago
else
@course_module.course_second_categories
.where("parent_id = #{@category.parent_id} and position > #{@category.position} and position <= ?", params[:position])
.update_all("position = position - 1")
6 years ago
end
5 years ago
@category.update!(position: params[:position])
6 years ago
normal_status(0, "移动成功")
else
normal_status(-1, "位置没有变化")
end
end
def destroy
tip_exception("毕设子目录不能删除") if @category.category_type == "graduation"
ActiveRecord::Base.transaction do
begin
parent_id = @category.parent_id
@course_module.course_second_categories.where("parent_id = #{parent_id} and position > #{@category.position}")
.update_all("position = position - 1")
6 years ago
# 更新相应对象的子目录id
if @course_module.module_type == "shixun_homework"
@category.homework_commons.update_all(course_second_category_id: parent_id)
@right_url = "/classrooms/#{@course.id}/shixun_homeworks/#{@course_module.id}"
6 years ago
elsif @course_module.module_type == "attachment"
Attachment.where(course_second_category_id: @category.id).update_all(course_second_category_id: parent_id)
if parent_id == 0
@right_url = "/classrooms/#{@course.id}/files/#{@course_module.id}"
else
@right_url = "/classrooms/#{@course.id}/file/#{parent_id}"
end
elsif @course_module.module_type == "video"
@course.course_videos.where(course_second_category_id: @category.id).update_all(course_second_category_id: parent_id)
@right_url = "/classrooms/#{@course.id}/course_videos"
5 years ago
elsif @course_module.module_type == "common_homework"
@category.homework_commons.update_all(course_second_category_id: parent_id)
5 years ago
@right_url = "/classrooms/#{@course.id}/common_homeworks/#{@course_module.id}"
elsif @course_module.module_type == "group_homework"
@category.homework_commons.update_all(course_second_category_id: parent_id)
5 years ago
@right_url = "/classrooms/#{@course.id}/group_homeworks/#{@course_module.id}"
6 years ago
end
@category.destroy
rescue Exception => e
uid_logger_error(e.message)
tip_exception("删除子目录失败")
end
end
end
private
def set_category
@category = CourseSecondCategory.find_by!(id: params[:id])
@course_module = @category.course_module
@course = @course_module.try(:course)
end
end