2013-09-12 17:19:51 +08:00
|
|
|
class CoursesController < ApplicationController
|
2013-09-13 10:11:36 +08:00
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
|
|
|
|
before_filter :require_login, :only => [:join, :unjoin]
|
|
|
|
|
|
|
|
def join
|
|
|
|
if User.current.logged?
|
|
|
|
course = Project.find(params[:object_id])
|
2013-09-13 21:52:24 +08:00
|
|
|
if params[:course_password].to_i == Course.find_by_extra(course.identifier).state
|
2013-09-12 10:41:15 +08:00
|
|
|
members = []
|
|
|
|
members << Member.new(:role_ids => [5], :user_id => User.current.id)
|
|
|
|
course.members << members
|
|
|
|
|
|
|
|
StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id])
|
|
|
|
else
|
|
|
|
end
|
|
|
|
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 => Project.find(params[:object_id])} }
|
|
|
|
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}}
|
|
|
|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id])} }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|