2013-09-12 17:19:51 +08:00
|
|
|
class CoursesController < ApplicationController
|
2014-03-17 22:43:38 +08:00
|
|
|
include CoursesHelper
|
2013-09-12 10:41:15 +08:00
|
|
|
|
|
|
|
before_filter :require_login, :only => [:join, :unjoin]
|
2014-03-17 22:43:38 +08:00
|
|
|
before_filter :allow_join, :only => [:join]
|
2013-09-12 10:41:15 +08:00
|
|
|
|
|
|
|
def join
|
|
|
|
if User.current.logged?
|
|
|
|
course = Project.find(params[:object_id])
|
2014-03-17 22:43:38 +08:00
|
|
|
if ( !course_endTime_timeout?(course) )
|
|
|
|
course_prefs = Course.find_by_extra(course.identifier)
|
|
|
|
if params[:course_password] == course_prefs.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
|
2013-09-12 10:41:15 +08:00
|
|
|
else
|
2014-03-17 22:43:38 +08:00
|
|
|
@state = 2
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
# format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
2013-11-05 20:46:38 +08:00
|
|
|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} }
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unjoin
|
|
|
|
if User.current.logged?
|
|
|
|
|
|
|
|
@member = Member.where('project_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
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
# format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
2013-11-05 20:46:38 +08:00
|
|
|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} }
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|
|
|
|
end
|
2014-03-17 22:43:38 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def allow_join
|
|
|
|
course_endTime_timeout? Project.find(params[:object_id])
|
|
|
|
end
|
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|