2017-07-20 15:51:55 +08:00
# encoding: utf-8
2017-07-13 09:20:25 +08:00
class SubjectsController < ApplicationController
2017-07-13 10:16:44 +08:00
layout 'base_subject'
before_filter :require_login
before_filter :check_authentication
2017-07-14 09:58:20 +08:00
before_filter :find_subject , :except = > [ :index , :create_subject , :new_subject , :append_to_stage ]
2017-07-13 10:16:44 +08:00
include ApplicationHelper
2017-07-13 09:20:25 +08:00
def index
@order = params [ :order ] || " created_at "
bsort = params [ :sort ] || " desc "
@sort = bsort == " desc " ? " asc " : " desc "
search = params [ :search ]
2017-07-14 12:37:20 +08:00
ids = Subject . find_by_sql ( " select group_concat(id) as allId from subjects where id in (SELECT distinct(subject_id) FROM `stages`) " )
2017-07-14 14:03:14 +08:00
@subjects = Subject . where ( :id = > ids [ 0 ] . allId . nil? ? " -1 " : ids [ 0 ] . allId . split ( " , " ) ) . reorder ( " #{ @order } #{ bsort } " )
2017-07-23 19:08:10 +08:00
@subject_created_count = Subject . find_by_sql ( " select count(*) as count from subjects where id in (SELECT distinct(subject_id) FROM `stages`) and user_id= #{ User . current . id } ; " ) . first . try ( :count )
2017-07-14 12:37:20 +08:00
@subjects_all_count = @subjects . count
#@subjects = Subject.where("name like ?", "%#{search}%").reorder("#{@order} #{@sort}")
2017-07-13 09:20:25 +08:00
@subjects_count = @subjects . count
@limit = 16
@is_remote = true
@subject_pages = Paginator . new @subjects_count , @limit , params [ 'page' ] || 1
@offset || = @subject_pages . offset
@subjects = paginateHelper @subjects , @limit
render :layout = > " base_edu "
end
2017-07-13 10:16:44 +08:00
def show
2017-07-14 09:58:20 +08:00
@stages = @subject . stages
2017-07-13 10:16:44 +08:00
end
def new_subject
2017-07-23 19:33:36 +08:00
if Course . where ( :tea_id = > User . current . id ) . count > 0
2017-07-13 09:20:25 +08:00
@status = 1
@course_lists = CourseList . where ( :id = > ShixunMajorCourse . where ( :shixun_id = > Shixun . where ( :user_id = > User . current . id ) . map ( & :id ) ) . map ( & :course_list_id ) )
else
@status = 0
end
2017-07-13 10:16:44 +08:00
respond_to do | format |
format . js
end
2017-07-13 09:20:25 +08:00
end
2017-07-13 10:16:44 +08:00
def create_subject
2017-07-13 09:20:25 +08:00
if params [ :course_list_id ]
course = CourseList . find params [ :course_list_id ]
2017-07-13 10:16:44 +08:00
if course
subject = Subject . create ( :name = > course . name , :user_id = > User . current . id , :visits = > 0 , :status = > 0 , :course_list_id = > course . id )
end
2017-07-13 09:20:25 +08:00
end
2017-07-13 10:16:44 +08:00
redirect_to subject_path ( subject )
end
2017-07-14 09:58:20 +08:00
def choose_subject_shixun
course_list = @subject . course_list
@shixuns = Shixun . where ( :user_id = > User . current . id , :status = > [ 0 , 1 , 2 ] , :id = > course_list . shixun_major_courses . map ( & :shixun_id ) )
@shixuns_count = @shixuns . count
@limit = 8
@shixuns_pages = Paginator . new @shixuns_count , @limit , params [ 'page' ] || 1
@offset || = @shixuns_pages . offset
@shixuns = paginateHelper @shixuns , @limit
end
def append_to_stage
@shixuns = Shixun . where ( :id = > params [ :shixun_id ] )
end
2017-07-20 15:51:55 +08:00
def publish
@subject . update_attributes ( :status = > 1 )
apply = ApplyAction . where ( :container_type = > " ApplySubject " , :container_id = > @subject . id ) . order ( " created_at desc " ) . first
if apply && apply . status == 0
@status = 0
else
ApplyAction . create ( :container_type = > " ApplySubject " , :container_id = > @subject . id , :user_id = > User . current . id , :status = > 0 )
notes = User . current . show_name . to_s + " 申请发布课程实训:<a href=' #{ subject_path ( @subject ) } '> #{ @subject . name } </a> "
JournalsForMessage . create ( :jour_id = > 1 , :jour_type = > 'Principal' , :user_id = > User . current . id , :notes = > notes , :private = > 1 , :reply_id = > 0 )
@status = 1
end
end
def cancel_publish
apply = ApplyAction . where ( :container_type = > " ApplySubject " , :container_id = > @subject . id ) . order ( " created_at desc " ) . first
if apply && apply . status == 0
apply . update_attributes ( :status = > 3 )
@subject . update_column ( 'status' , 0 )
end
redirect_to subject_path ( @subject )
end
2017-07-13 10:16:44 +08:00
private
# Find subject of id params[:id]
def find_subject
@subject = Subject . find_by_id ( params [ :id ] )
render_404 if @subject . nil?
rescue ActiveRecord :: RecordNotFound
render_404
2017-07-13 09:20:25 +08:00
end
end