find-classroom/app/controllers/courses_controller.rb

128 lines
3.2 KiB
Ruby
Raw Normal View History

2016-09-09 23:07:36 +08:00
class CoursesController < ApplicationController
2016-09-10 12:17:12 +08:00
before_action :student_logged_in, only: [:select, :quit, :list]
2016-11-13 19:12:16 +08:00
before_action :teacher_logged_in, only: [:new, :create, :edit, :destroy, :update, :open, :close]
before_action :logged_in, only: [:index]
2016-09-10 12:17:12 +08:00
#-------------------------for teachers----------------------
2016-09-09 23:07:36 +08:00
def new
@course=Course.new
end
def create
@course = Course.new(course_params)
if @course.save
current_user.teaching_courses<<@course
redirect_to courses_path, flash: {success: "新课程申请成功"}
else
flash[:warning] = "信息填写有误,请重试"
render 'new'
end
end
def edit
@course=Course.find_by_id(params[:id])
end
def update
@course = Course.find_by_id(params[:id])
if @course.update_attributes(course_params)
flash={:info => "更新成功"}
else
flash={:warning => "更新失败"}
end
2016-09-10 12:17:12 +08:00
redirect_to courses_path, flash: flash
end
def destroy
@course=Course.find_by_id(params[:id])
current_user.teaching_courses.delete(@course)
@course.destroy
flash={:success => "成功删除课程: #{@course.name}"}
redirect_to courses_path, flash: flash
end
2016-11-13 19:12:16 +08:00
def open
@course=Course.find_by_id(params[:id])
@course.update_attributes(open:true)
redirect_to courses_path, flash: {:success => "已经成功开启该课程:#{ @course.name}"}
end
def close
@course=Course.find_by_id(params[:id])
@course.update_attributes(open:false)
redirect_to courses_path, flash: {:success => "已经成功关闭该课程:#{ @course.name}"}
end
2016-09-10 12:17:12 +08:00
#-------------------------for students----------------------
def list
@course=Course.all
2016-11-13 19:12:16 +08:00
@course_open=Course.where("open = ?", true)
@course_open=@course_open-current_user.courses
2016-09-09 23:07:36 +08:00
end
def select
@course=Course.find_by_id(params[:id])
current_user.courses<<@course
flash={:success => "成功选择课程: #{@course.name}"}
2016-09-10 12:17:12 +08:00
redirect_to courses_path, flash: flash
2016-09-09 23:07:36 +08:00
end
def quit
2016-09-23 14:57:25 +08:00
@course=Course.find_by_id(params[:id])
2016-09-09 23:07:36 +08:00
current_user.courses.delete(@course)
flash={:success => "成功退选课程: #{@course.name}"}
2016-09-10 12:17:12 +08:00
redirect_to courses_path, flash: flash
2016-09-09 23:07:36 +08:00
end
2016-09-10 12:17:12 +08:00
#-------------------------for both teachers and students----------------------
2016-09-09 23:07:36 +08:00
2016-09-10 12:17:12 +08:00
def index
@course=current_user.teaching_courses if teacher_logged_in?
@course=current_user.courses if student_logged_in?
end
def detail
@course=Course.find_by_id(params[:id])
end
2016-09-10 12:17:12 +08:00
private
# Confirms a student logged-in user.
def student_logged_in
unless student_logged_in?
2016-09-09 23:07:36 +08:00
redirect_to root_url, flash: {danger: '请登陆'}
end
end
2016-09-10 12:17:12 +08:00
# Confirms a teacher logged-in user.
2016-09-09 23:07:36 +08:00
def teacher_logged_in
unless teacher_logged_in?
redirect_to root_url, flash: {danger: '请登陆'}
end
end
2016-09-10 12:17:12 +08:00
# Confirms a logged-in user.
def logged_in
unless logged_in?
redirect_to root_url, flash: {danger: '请登陆'}
end
end
2016-09-09 23:07:36 +08:00
def course_params
params.require(:course).permit(:course_code, :avatar, :name, :course_type, :teaching_type, :exam_type,
:credit, :limit_num, :class_room, :course_time, :course_week, :course_introduction)
2016-09-09 23:07:36 +08:00
end
2016-09-09 23:07:36 +08:00
end