find-classroom/app/controllers/courses_controller.rb

262 lines
7.7 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
2017-01-07 03:42:33 +08:00
def ne
2016-09-09 23:07:36 +08:00
@course=Course.new
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
2017-01-05 16:46:03 +08:00
def excel
@course= current_user.teaching_courses
end
def excel_in
2017-01-05 20:46:31 +08:00
2017-01-04 13:03:09 +08:00
@course = Course.find_by_id(params[:id])
if @course.update_attributes(course_params)
flash={:info => " 导入成功"}
else
flash={:warning => "导入失败"}
end
redirect_to excel_courses_path, flash: flash
2017-01-05 17:36:49 +08:00
2017-01-05 16:46:03 +08:00
end
2016-09-10 12:17:12 +08:00
#-------------------------for students----------------------
def list
@course=Course.all
2016-11-20 01:39:16 +08:00
@course_open=Course.where("open = ?", true)-current_user.courses
2016-11-16 20:17:26 +08:00
@course_close=@course-@course_open
2017-01-02 22:24:48 +08:00
@course_display=@course
@theparams=params
2016-12-07 23:29:28 +08:00
@credit_isdegree, @credit_nodegree=cal_degree
2016-09-09 23:07:36 +08:00
end
2017-01-04 01:04:31 +08:00
2017-01-04 01:00:21 +08:00
def classtable
@course=current_user.courses
end
2016-09-09 23:07:36 +08:00
def credit#add credit method
@course_isdegree=current_user.courses.where("degree= ?", true)
@course_nodegree=current_user.courses.where("degree= ?", false)
2016-12-07 23:29:28 +08:00
@credit_isdegree, @credit_nodegree=cal_degree
end
2016-12-07 21:59:13 +08:00
def isdegree #add isdegree method
@course=Course.find_by_id(params[:id])
@course.update_attributes(degree: true)
2016-12-07 21:59:13 +08:00
redirect_to courses_path,flash: {:success=> "已经成功将 #{@course.name} 选为学位课"}
end
2016-12-07 21:59:13 +08:00
def nodegree #add nodegree method
@course=Course.find_by_id(params[:id])
@course.update_attributes(degree: false)
2016-12-07 21:59:13 +08:00
redirect_to courses_path,flash: {:success=> "已经成功将 #{@course.name} 选为非学位课"}
end
2017-01-02 22:24:48 +08:00
2017-01-04 01:04:31 +08:00
2016-09-09 23:07:36 +08:00
def select
2016-12-10 16:17:57 +08:00
@allcourse=current_user.courses
2016-09-09 23:07:36 +08:00
@course=Course.find_by_id(params[:id])
2016-12-10 17:11:31 +08:00
2016-12-10 16:17:57 +08:00
@allcourse.each do |k|
if(k.course_week.nil?||@course.course_week.nil?)
next
else
week1 = (@course.course_week[0,1].to_i..@course.course_week[3,@course.course_week.length-1].to_i).to_a
week2 = (k.course_week[0,1].to_i..k.course_week[3,k.course_week.length-1].to_i).to_a
time1 = (@course.course_time[3].to_i..@course.course_time[5,@course.course_time.length-1].to_i).to_a
time2 = (k.course_time[3].to_i..k.course_time[5,k.course_time.length-1].to_i).to_a
weekn1=@course.course_time[2]
weekn2=k.course_time[2]
if (week1 & week2)!=[] && (time1 & time2)!=[] && weekn1==weekn2
flash={:sucess => "选课时间冲突: #{@course.name}"}
redirect_to list_courses_path, flash: flash
return
end
end
end
2016-12-10 17:11:31 +08:00
2016-12-07 22:06:14 +08:00
if !@course.limit_num.nil? && @course.limit_num!=0
2016-12-07 21:06:14 +08:00
if(@course.student_num < @course.limit_num)
current_user.courses<<@course
@course.student_num+=1
@course.update_attributes(:student_num=>@course.student_num)
flash={:success => "成功选择课程: #{@course.name}"}
redirect_to courses_path, flash: flash
else
2016-12-07 22:06:14 +08:00
flash={:danger => "选课人数已满: #{@course.name}"}
2016-12-07 21:06:14 +08:00
@course=Course.all
@course_open=Course.where(:open=>true)
@course_open=@course_open-current_user.courses
redirect_to list_courses_path, flash: flash
end
2016-12-02 02:42:23 +08:00
else
2016-12-10 16:17:57 +08:00
current_user.courses<<@course
2016-12-07 21:06:14 +08:00
@course.student_num+=1
@course.update_attributes(:student_num=>@course.student_num)
flash={:success => "成功选择课程: #{@course.name}"}
redirect_to courses_path, flash: flash
2016-12-10 17:11:31 +08:00
end
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)
2016-12-02 02:42:23 +08:00
@course.student_num-=1
@course.update_attributes(:student_num=>@course.student_num)
2016-09-09 23:07:36 +08:00
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
2017-01-04 13:03:09 +08:00
def excel_out
@course=Course.find_by_id(params[:id])
if @course.update_attributes(course_params)
flash={:info => " 导出成功"}
else
flash={:warning => "导出失败"}
end
redirect_to excel_courses_path, flash: flash
end
2016-09-09 23:07:36 +08:00
2017-01-04 13:03:09 +08:00
def show
@course = Course.find(params[:id])
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?
2016-12-07 23:29:28 +08:00
@credit_isdegree, @credit_nodegree=cal_degree
end
def cal_degree
@credit_isdegree=0
@credit_nodegree=0
current_user.courses.each do |course|
if course.degree
@credit_isdegree=@credit_isdegree + course.credit.sub(/\d+\// , "").to_i
else
@credit_nodegree=@credit_nodegree + course.credit.sub(/\d+\// , "").to_i
end
end
return @credit_isdegree, @credit_nodegree
2016-09-10 12:17:12 +08:00
end
def detail
@course=Course.find_by_id(params[:id])
end
2016-11-16 20:17:26 +08:00
def search
temp="%"+params[:name]+"%"
@course=Course.all
@course_open=Course.where("name like ? AND open =?", temp ,true)
@course_close=Course.where("name like ? AND open =?", temp ,false)
2017-01-02 22:24:48 +08:00
@course_display=Course.where("name like ? ", temp)
2016-11-16 20:17:26 +08:00
2017-01-07 03:42:33 +08:00
if params[:course_time]!=""
@course_open=@course_open.where("course_time =?", params[:course_time])
@course_close=@course_close.where("course_time =?", params[:course_time])
@course_display=@course_display.where("course_time =?", params[:course_time])
2016-11-16 20:17:26 +08:00
end
2017-01-07 03:42:33 +08:00
if params[:class_room]!=""
@course_open=@course_open.where("class_room =?", params[:class_room])
@course_close=@course_close.where("class_room =?", params[:class_room])
@course_display=@course_display.where("class_room =?", params[:class_room])
2016-11-16 20:17:26 +08:00
end
2017-01-02 22:24:48 +08:00
2017-01-07 03:42:33 +08:00
2017-01-02 22:24:48 +08:00
@course_display=@course_display
2016-11-16 20:17:26 +08:00
@course_open=@course_open-current_user.courses
@course_close=@course_close-current_user.courses
2017-01-02 22:24:48 +08:00
@theparams=params
2016-11-16 20:17:26 +08:00
render 'list'
end
def refresh_search
@course=Course.all
2016-11-20 01:39:16 +08:00
@course_open=Course.where("open = ?", true)-current_user.courses
@course_close=@course-@course_open
2017-01-02 22:24:48 +08:00
@course_display=@course
2016-11-20 01:39:16 +08:00
@theparams=params
render 'list'
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
2017-01-05 17:44:04 +08:00
params.require(:course).permit(:course_code, :excel, :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
end