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/weapps/courses_controller.rb

208 lines
9.1 KiB

5 years ago
class Weapps::CoursesController < Weapps::BaseController
before_action :require_login
5 years ago
before_action :set_course, :user_course_identity, except: [:create]
before_action :check_account, only: [:create]
5 years ago
before_action :teacher_allowed, only: [:edit, :update]
before_action :teacher_or_admin_allowed, only: [:change_member_roles, :delete_course_teachers]
5 years ago
def create
# return render_error("只有老师身份才能创建课堂") unless current_user.is_teacher?
course = Course.new(tea_id: current_user.id)
Weapps::CreateCourseService.call(course, course_params)
render_ok
5 years ago
rescue ApplicationService::Error => ex
render_error(ex.message)
end
def edit
@course = current_course
end
def update
5 years ago
Weapps::UpdateCourseService.call(current_course, update_course_params)
render_ok
end
5 years ago
def show
@course = current_course
@current_user = current_user
end
5 years ago
def shixun_homework_category
@categories = current_course.shixun_course_modules.first&.course_second_categories
end
5 years ago
# 教师列表
def teachers
@course = current_course
@page = (params[:page] || 1).to_i
@limit = (params[:limit] || 20).to_i
search = params[:search].present? ? params[:search].strip : ""
5 years ago
if @course.try(:id) != 1309 || current_user.admin? || current_user.try(:id) == 15582
@teacher_list = @course.course_members.joins(:user).where("course_members.role in (1, 2, 3)")
else
@teacher_list = @course.course_members.joins(:user).where("(course_members.role in (1, 3) or (course_members.user_id = #{current_user.id}
and course_members.role = 2))")
end
if search.present?
@teacher_list = @teacher_list.joins(:user).where("LOWER(CONCAT(users.lastname, users.firstname)) like ?", "%#{search}%")
end
5 years ago
@teacher_list_size = @teacher_list.size
@applications_size = CourseMessage.unhandled_join_course_requests_by_course(@course).size
@teacher_list = @teacher_list.includes(user: [user_extension: :school])
# 中英文混合排序(忽略大小写)
@teacher_list = @teacher_list.sort {|x, y| Pinyin.t(x.user&.real_name, splitter: '').upcase <=> Pinyin.t(y.user&.real_name, splitter: '').upcase}
@teacher_list = @teacher_list[(@page-1)*@limit ... @page*@limit]
5 years ago
end
5 years ago
# 批量删除教师或助教
def delete_course_teachers
begin
@course = current_course
@page = (params[:page] || 1).to_i
@limit = (params[:limit] || 20).to_i
5 years ago
course_members = @course.course_members.where(id: params[:course_member_ids], role: %i[PROFESSOR ASSISTANT_PROFESSOR])
user_ids = course_members.pluck(:user_id)
course_members.destroy_all
CourseDeleteStudentNotifyJob.perform_later(@course.id, user_ids, current_user.id)
@course.students.where(user_id: user_ids).update_all(is_active: 1)
normal_status(0, "删除成功")
rescue => e
uid_logger_error(e.message)
tip_exception("删除失败")
end
end
def students
5 years ago
@course = current_course
@page = (params[:page] || 1).to_i
@limit = (params[:limit] || 20).to_i
search = params[:search].present? ? params[:search].strip : nil
5 years ago
course_group_id = params[:course_group_id].present? ? params[:course_group_id].to_i : nil
@students = CourseMember.students(@course)
if search.present?
@students = @students.joins(user: :user_extension).where("LOWER(CONCAT(users.lastname, users.firstname)) like ? or
user_extensions.student_id like ?", "%#{search}%", "%#{search}%")
end
5 years ago
if course_group_id.present?
@course_group = CourseGroup.find(course_group_id) if course_group_id != 0
@students = @students.where(course_group_id: @course_group&.id.to_i)
5 years ago
end
5 years ago
5 years ago
@students_count = @students.size
@students = @students.includes(user: :user_extension)
# 中英文混合排序(忽略大小写)
@students = @students.sort {|x, y| Pinyin.t(x.user&.real_name, splitter: '').upcase <=> Pinyin.t(y.user&.real_name, splitter: '').upcase}
@students = @students[(@page-1)*@limit ... @page*@limit]
5 years ago
end
5 years ago
5 years ago
# 批量修改角色
def change_member_roles
5 years ago
@course = current_course
tip_exception("请至少选择一个角色") if params[:roles].reject(&:blank?).blank?
5 years ago
tip_exception("不能具有老师、助教两种角色") if params[:roles].include?("PROFESSOR") && params[:roles].include?("ASSISTANT_PROFESSOR")
params[:user_ids].each do |user_id|
course_members = @course.course_members.where(user_id: user_id)
tip_exception("非课堂成员不能修改角色") if course_members.blank?
ActiveRecord::Base.transaction do
# 第一次修改为教师或助教身份时直接创建数据
if params[:roles].include?("CREATOR")
teacher_member = course_members.where(role: %i[CREATOR]).take
elsif (params[:roles].include?("PROFESSOR") || params[:roles].include?("ASSISTANT_PROFESSOR")) && !course_members.exists?(role: %i[CREATOR PROFESSOR ASSISTANT_PROFESSOR])
teacher_member = CourseMember.create!(course_id: @course.id, user_id: user_id, role: params[:roles].include?("PROFESSOR") ? 2 : 3)
# 如果有未审批的申请教师/助教的记录,则修改状态为已审批
apply_teacher = CourseMessage.where(course_id: @course.id, course_message_id: user_id, status: 0).last
5 years ago
apply_teacher.update!(status: 1, apply_user_id: current_user.id) if apply_teacher
5 years ago
elsif course_members.exists?(role: %i[PROFESSOR ASSISTANT_PROFESSOR])
teacher_member = course_members.where(role: %i[PROFESSOR ASSISTANT_PROFESSOR]).take
if params[:roles].include?("PROFESSOR") || params[:roles].include?("ASSISTANT_PROFESSOR")
# 如果之前有老师身份且老师身份要调整时只需要修改role字段
if !params[:roles].include?(teacher_member.role) && params[:roles].include?("PROFESSOR")
teacher_member.PROFESSOR!
elsif !params[:roles].include?(teacher_member.role) && params[:roles].include?("ASSISTANT_PROFESSOR")
teacher_member.ASSISTANT_PROFESSOR!
end
teacher_member.save!
else
# 不含教师的参数时删除记录
teacher_member.destroy!
# CourseDeleteStudentNotifyJob.perform_later(@course.id, [teacher_member.user_id], current_user.id)
end
end
# 学生身份的处理
student_member = course_members.where(role: %i[STUDENT]).take
# 不存在则创建学生身份
if params[:roles].include?("STUDENT") && student_member.blank?
correspond_teacher_exist = CourseMember.exists?(user_id: user_id, is_active: 1, course_id: @course.id, role: %i[CREATOR PROFESSOR ASSISTANT_PROFESSOR])
new_student = CourseMember.new(user_id: user_id, course_id: @course.id, role: 4)
new_student.is_active = 0 if correspond_teacher_exist
new_student.save!
CourseAddStudentCreateWorksJob.perform_later(@course.id, [user_id])
5 years ago
# StudentJoinCourseNotifyJob.perform_later(current_user.id, course.id)
elsif !params[:roles].include?("STUDENT") && student_member.present?
# 删除学生身份时激活老师身份
teacher_member.update_attributes!(is_active: 1) if student_member.is_active && teacher_member.present?
student_member.destroy!
CourseDeleteStudentDeleteWorksJob.perform_later(@course.id, [user_id])
5 years ago
# CourseDeleteStudentNotifyJob.perform_later(@course.id, [params[:user_id]], current_user.id)
elsif params[:roles].include?("STUDENT") && student_member.present? && !params[:roles].include?("PROFESSOR") && !params[:roles].include?("ASSISTANT_PROFESSOR")
# 学生身份存在且学生没有教师身份时更新is_active
student_member.update_attributes!(is_active: 1)
end
end
end
normal_status(0, "修改成功")
end
5 years ago
# 分班列表
def course_groups
@course_groups = @course.course_groups
@course_groups = @course_groups.where("name like ?", "%#{params[:search]}%") unless params[:search].blank?
@all_group_count = @course_groups.size
@teachers = @course.teachers.includes(:user, :teacher_course_groups) if @user_course_identity < Course::NORMAL
@current_group_id = @course.students.where(user_id: current_user.id).take&.course_group_id if @user_course_identity == Course::STUDENT
end
private
def course_params
params.permit(:name, :course_list_name, :credit, course_module_types: [])
5 years ago
end
5 years ago
def update_course_params
params.permit(:name, :course_list_name, :credit)
end
def current_course
@_current_course = Course.find params[:id]
end
def teacher_allowed
5 years ago
return render_forbidden unless @user_course_identity < Course::STUDENT
end
5 years ago
# 课堂教师,课堂管理员以及超级管理员的权限判断
def teacher_or_admin_allowed
unless @user_course_identity < Course::ASSISTANT_PROFESSOR
tip_exception(403, "..")
end
end
5 years ago
def set_course
@course = Course.find_by!(id: params[:id])
tip_exception(404, "") if @course.is_delete == 1 && !current_user.admin?
end
5 years ago
end