trustieplus/app/controllers/subjects_controller.rb

158 lines
5.7 KiB
Ruby

# encoding: utf-8
class SubjectsController < ApplicationController
layout 'base_subject'
before_filter :require_login
before_filter :check_authentication
before_filter :find_subject, :except => [:index, :new, :create, :create_subject, :new_subject, :append_to_stage]
include ApplicationHelper
include SubjectsHelper
def index
@order = params[:order] || "myshixun_count"
bsort = params[:sort] || "desc"
@sort = bsort == "desc" ? "asc" : "desc"
search = params[:search]
#ids = Subject.find_by_sql("select group_concat(id) as allId from subjects where id in (SELECT distinct(subject_id) FROM `stages`)")
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
@subject_created_count = Subject.where(:user_id => User.current.id).count
@subjects_all_count = @subjects.count
#@subjects = Subject.where("name like ?", "%#{search}%").reorder("#{@order} #{@sort}")
@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
def show
@stages = @subject.stages
@is_manager = User.current.manager_of_subject?(@subject)
end
def new
@subject = Subject.new
render :layout => "base_edu"
end
def create
if params[:subject]
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)
end
redirect_to subject_path(subject)
end
def edit
render :layout => "base_edu"
end
def update
if params[:subject]
subject = @subject.update_attributes(:name => params[:subject][:name], :learning_notes => params[:subject][:learning_notes], :description => params[:subject][:description])
end
redirect_to subject_path(@subject)
end
def update_attr
if params[:name]
@subject.update_attributes(:name => params[:name])
elsif params[:description]
@subject.update_attributes(:description => params[:description])
end
end
def new_subject
if Course.where(:tea_id => User.current.id).count > 0
@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
respond_to do |format|
format.js
end
end
def create_subject
if params[:course_list_id]
course = CourseList.find params[:course_list_id]
if course
subject = Subject.create(:name => course.name, :user_id => User.current.id, :visits => 0, :status => 0, :course_list_id => course.id)
end
end
redirect_to subject_path(subject)
end
def choose_subject_shixun
#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))
@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[:choose_ids].split(",")).reorder("id desc")
end
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
#if Shixun.where(:id => @subject.stage_shixuns.map(&:shixun_id), :status => [0, 1]).count > 0
# @status = 2
#else
@subject.update_attributes(:status => 1)
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
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
@subject.update_attributes(:status => 0)
redirect_to subject_path(@subject)
end
def cancel_has_publish
@subject.update_attributes(:status => 0)
redirect_to subject_path(@subject)
end
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
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
end
end