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-25 15:41:36 +08:00
before_filter :find_subject , :except = > [ :index , :new , :create , :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
2017-09-22 10:19:59 +08:00
@order = params [ :order ] || " myshixun_count "
bsort = params [ :sort ] || " desc "
2017-07-13 09:20:25 +08:00
@sort = bsort == " desc " ? " asc " : " desc "
search = params [ :search ]
2017-07-24 14:58:27 +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-09-22 10:19:59 +08:00
if @order == " myshixun_count "
#@shixuns = @shixuns.select("shixuns.*, (select count(myshixuns.id) from myshixuns where myshixuns.shixun_id = shixuns.id group by shixuns.id) AS myshixunCount").reorder("status = 2 desc, myshixunCount desc")
@subjects = Subject . where ( " hidden = 0 " ) . select ( " subjects.*, (select count(myshixuns.id) from myshixuns, stage_shixuns where myshixuns.shixun_id = stage_shixuns.shixun_id and stage_shixuns.subject_id = subjects.id group by subjects.id) AS myshixunCount " ) . reorder ( " status = 2 desc, myshixunCount #{ bsort } " )
else
@subjects = Subject . where ( " hidden = 0 " ) . reorder ( " #{ @order } #{ bsort } " )
end
2017-07-24 17:19:25 +08:00
@subject_created_count = Subject . where ( :user_id = > User . current . id ) . 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
2017-07-25 15:41:36 +08:00
def new
@subject = Subject . new
render :layout = > " base_edu "
end
def create
if params [ :subject ]
2017-08-25 16:11:21 +08:00
subject = Subject . create ( :name = > params [ :subject ] [ :name ] , :learning_notes = > params [ :subject ] [ :learning_notes ] , :description = > params [ :subject ] [ :description ] , :user_id = > User . current . id , :visits = > 0 , :status = > 0 )
2017-07-25 15:41:36 +08:00
end
redirect_to subject_path ( subject )
end
2017-07-26 18:57:08 +08:00
def edit
render :layout = > " base_edu "
end
def update
if params [ :subject ]
2017-08-25 16:11:21 +08:00
subject = @subject . update_attributes ( :name = > params [ :subject ] [ :name ] , :learning_notes = > params [ :subject ] [ :learning_notes ] , :description = > params [ :subject ] [ :description ] )
2017-07-26 18:57:08 +08:00
end
2017-07-26 19:05:46 +08:00
redirect_to subject_path ( @subject )
2017-07-26 18:57:08 +08:00
end
2017-07-26 14:42:20 +08:00
def update_attr
if params [ :name ]
@subject . update_attributes ( :name = > params [ :name ] )
elsif params [ :description ]
@subject . update_attributes ( :description = > params [ :description ] )
end
end
2017-07-13 10:16:44 +08:00
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
2017-07-26 14:42:20 +08:00
#course_list = @subject.course_list
@shixuns = User . current . shixuns . where ( :status = > [ 0 , 1 , 2 ] ) . reorder ( " created_at desc " )
#@shixuns = Shixun.where(:user_id => User.current.id, :status => [0, 1, 2], :id => course_list.shixun_major_courses.map(&:shixun_id))
2017-07-14 09:58:20 +08:00
@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
2017-08-16 17:34:25 +08:00
@shixuns = Shixun . where ( :id = > params [ :choose_ids ] . split ( " , " ) ) . reorder ( " id desc " )
2017-07-14 09:58:20 +08:00
end
2017-07-20 15:51:55 +08:00
def publish
apply = ApplyAction . where ( :container_type = > " ApplySubject " , :container_id = > @subject . id ) . order ( " created_at desc " ) . first
if apply && apply . status == 0
@status = 0
else
2017-08-24 20:01:34 +08:00
#if Shixun.where(:id => @subject.stage_shixuns.map(&:shixun_id), :status => [0, 1]).count > 0
# @status = 2
#else
2017-07-26 15:15:47 +08:00
@subject . update_attributes ( :status = > 1 )
2017-07-26 14:42:20 +08:00
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
2017-08-24 20:01:34 +08:00
# end
2017-07-20 15:51:55 +08:00
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 )
end
2017-07-26 14:42:20 +08:00
@subject . update_attributes ( :status = > 0 )
2017-07-20 15:51:55 +08:00
redirect_to subject_path ( @subject )
end
2017-09-30 16:30:49 +08:00
def cancel_has_publish
@subject . update_attributes ( :status = > 0 )
redirect_to subject_path ( @subject )
end
2017-07-27 10:32:55 +08:00
def destroy
if @subject
ApplyAction . where ( :container_type = > " ApplySubject " , :container_id = > @subject . id ) . destroy_all
@subject . destroy
respond_to do | format |
format . js
end
end
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