76 lines
2.5 KiB
Ruby
76 lines
2.5 KiB
Ruby
class SubjectsController < ApplicationController
|
|
layout 'base_subject'
|
|
before_filter :require_login
|
|
before_filter :check_authentication
|
|
before_filter :find_subject, :except => [:index, :create_subject, :new_subject, :append_to_stage]
|
|
|
|
include ApplicationHelper
|
|
|
|
def index
|
|
@order = params[:order] || "created_at"
|
|
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`)")
|
|
@subjects = Subject.where(:id => ids[0].allId.nil? ? "-1" : ids[0].allId.split(",")).reorder("#{@order} #{bsort}")
|
|
@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
|
|
end
|
|
|
|
def new_subject
|
|
if Shixun.where(:user_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 = 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
|
|
|
|
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
|