加入课程、退出课程、编辑课程接口

This commit is contained in:
z9hang 2014-12-12 14:54:01 +08:00
parent 1851181768
commit 6c4d358b7e
2 changed files with 141 additions and 53 deletions

View File

@ -26,36 +26,38 @@ class CoursesController < ApplicationController
def join
if User.current.logged?
course = Course.find_by_id params[:object_id]
if course
if course_endTime_timeout? course
@state = 2
else
if User.current.member_of_course?(course)
@state = 3
else
if params[:course_password] == course.password
members = []
members << Member.new(:role_ids => [10], :user_id => User.current.id)
course.members << members
StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id])
@state = 0
else
@state = 1
end
end
end
else
@state = 4
end
cs = CoursesService.new
@state,course = cs.join_course params
#course = Course.find_by_id params[:object_id]
#if course
# if course_endTime_timeout? course
# @state = 2
# else
# if User.current.member_of_course?(course)
# @state = 3
# else
# if params[:course_password] == course.password
# members = []
# members << Member.new(:role_ids => [10], :user_id => User.current.id)
# course.members << members
# StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id])
# @state = 0
# else
# @state = 1
# end
# end
# end
#else
# @state = 4
#end
else
@state = 5
@state = 5 #未登录
end
respond_to do |format|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => course, :object_id => params[:object_id]} }
end
rescue Exception => e
@state = 4
@state = 4 #已经加入了课程
respond_to do |format|
# format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => nil, :object_id => nil} }
@ -64,15 +66,18 @@ class CoursesController < ApplicationController
def unjoin
if User.current.logged?
@member = Member.where('course_id = ? and user_id = ?', params[:object_id], User.current.id)
@member.first.destroy
joined = StudentsForCourse.where('student_id = ? and course_id = ?', User.current.id, params[:object_id])
joined.each do |join|
join.delete
end
#
# @member = Member.where('course_id = ? and user_id = ?', params[:object_id], User.current.id)
# @member.first.destroy
#
# joined = StudentsForCourse.where('student_id = ? and course_id = ?', User.current.id, params[:object_id])
# joined.each do |join|
# join.delete
# end
cs = CoursesService.new
cs.exit_course params,User.current
end
respond_to do |format|
# format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Course.find(params[:object_id]), :object_id => params[:object_id]} }
@ -87,27 +92,16 @@ class CoursesController < ApplicationController
#更新课程信息
def update
@course.safe_attributes = params[:course]
@course.time = params[:time]
@course.term = params[:term]
@course.class_period = params[:class_period]
if @course.save
if params[:course][:is_public] == '0'
course_status = CourseStatus.find_by_course_id(@course.id)
course_status.destroy if course_status
elsif params[:course][:is_public] == '1'
course_status = CourseStatus.find_by_course_id(@course.id)
course_status.destroy if course_status
course_status = CourseStatus.create(:course_id => @course.id, :grade => 0)
end
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_to settings_course_url(@course)
}
format.api { render_api_ok }
end
cs = CoursesService.new
@course = cs.edit_course params,@course
if @course.errors.full_messages.count <= 0
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_to settings_course_url(@course)
}
format.api { render_api_ok }
end
else
respond_to do |format|
format.html {
@ -117,6 +111,36 @@ class CoursesController < ApplicationController
format.api { render_validation_errors(@course) }
end
end
#@course.safe_attributes = params[:course]
#@course.time = params[:time]
#@course.term = params[:term]
#@course.class_period = params[:class_period]
#if @course.save
# if params[:course][:is_public] == '0'
# course_status = CourseStatus.find_by_course_id(@course.id)
# course_status.destroy if course_status
# elsif params[:course][:is_public] == '1'
# course_status = CourseStatus.find_by_course_id(@course.id)
# course_status.destroy if course_status
# course_status = CourseStatus.create(:course_id => @course.id, :grade => 0)
# end
#
# respond_to do |format|
# format.html {
# flash[:notice] = l(:notice_successful_update)
# redirect_to settings_course_url(@course)
# }
# format.api { render_api_ok }
# end
#else
# respond_to do |format|
# format.html {
# settings
# render :action => 'settings'
# }
# format.api { render_validation_errors(@course) }
# end
#end
end
def new_join

View File

@ -114,4 +114,68 @@ class CoursesService
@course
end
#编辑课程
def edit_course params,course
course.safe_attributes = params[:course]
course.time = params[:time]
course.term = params[:term]
course.class_period = params[:class_period]
if course.save
if params[:course][:is_public] == '0'
course_status = CourseStatus.find_by_course_id(course.id)
course_status.destroy if course_status
elsif params[:course][:is_public] == '1'
course_status = CourseStatus.find_by_course_id(course.id)
course_status.destroy if course_status
course_status = CourseStatus.create(:course_id => course.id, :grade => 0)
end
end
course
end
#退出课程
def exit_course params,user
@member = Member.where('course_id = ? and user_id = ?', params[:object_id], user.id)
@member.first.destroy
joined = StudentsForCourse.where('student_id = ? and course_id = ?', user.id, params[:object_id])
joined.each do |join|
join.delete
end
end
#加入课程
#@state == 0 加入成功
#@state == 1 密码错误
#@state == 2 课程已过期 请联系课程管理员重启课程。(在配置课程处)
#@state == 3 您已经加入了课程
#@state == 4 您加入的课程不存在
#@state == 5 您还未登录
#@state 其他 未知错误,请稍后再试
def join_course params
course = Course.find_by_id params[:object_id]
if course
if course_endTime_timeout? course
@state = 2
else
if User.current.member_of_course?(course)
@state = 3
else
if params[:course_password] == course.password
members = []
members << Member.new(:role_ids => [10], :user_id => User.current.id)
course.members << members
StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id])
@state = 0
else
@state = 1
end
end
end
else
@state = 4
end
[@state,course]
end
end