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

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

View File

@ -114,4 +114,68 @@ class CoursesService
@course @course
end 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 end