2016-07-01 11:11:53 +08:00
# encoding: utf-8
2016-06-14 10:38:11 +08:00
class SyllabusesController < ApplicationController
2016-06-30 10:44:11 +08:00
include ApplicationHelper
helper :attachments
include AttachmentsHelper
include CoursesHelper
2016-06-14 10:38:11 +08:00
2016-07-01 16:25:52 +08:00
before_filter :is_logged , :only = > [ :index , :show , :edit , :new , :update , :destroy , :delete_syllabus ]
2016-07-07 15:04:57 +08:00
before_filter :find_syllabus , :only = > [ :show , :edit , :update , :destroy , :syllabus_courselist , :edit_syllabus_eng_name , :edit_syllabus_title , :update_base_info , :delete_syllabus , :delete_des ]
2016-06-14 10:38:11 +08:00
def index
user = User . current
@syllabuses = user . syllabuses
end
def show
2016-06-28 15:13:04 +08:00
respond_to do | format |
format . js
format . html { render :layout = > 'base_syllabus' }
format . api
end
end
def new
2016-06-29 10:45:56 +08:00
@syllabus = Syllabus . new
render :layout = > 'new_base'
2016-06-28 15:13:04 +08:00
end
2016-06-14 10:38:11 +08:00
2016-06-28 15:13:04 +08:00
def create
if User . current . user_extensions . identity
@syllabus = Syllabus . new
@syllabus . title = params [ :title ]
@syllabus . eng_name = params [ :eng_name ]
@syllabus . user_id = User . current . id
2016-07-01 16:25:52 +08:00
@syllabus . description = Message . where ( " id = 19412 " ) . first . nil? ? '' : Message . where ( " id = 19412 " ) . first . content
2016-06-28 15:13:04 +08:00
if @syllabus && @syllabus . save
respond_to do | format |
flash [ :notice ] = l ( :notice_successful_create )
format . html { redirect_to syllabus_path ( @syllabus ) }
format . api { render :action = > 'show' , :status = > :created , :location = > url_for ( :controller = > 'syllabuses' , :action = > 'show' , :id = > @syllabus . id ) }
end
else
respond_to do | format |
flash [ :notice ] = l ( :notice_create_failed )
format . html { redirect_to new_syllabus_path } #Added by young
format . api { render_validation_errors ( @syllabus ) }
end
end
end
2016-06-14 10:38:11 +08:00
end
2016-06-29 10:45:56 +08:00
def edit
respond_to do | format |
format . html { render :layout = > 'base_syllabus' }
end
end
2016-07-01 16:25:52 +08:00
def delete_syllabus
respond_to do | format |
format . js
end
end
2016-06-29 10:45:56 +08:00
def update
@syllabus . description = params [ :syllabus ] [ :description ]
2016-06-30 10:44:11 +08:00
@syllabus . des_status = 1
2016-06-29 10:45:56 +08:00
@syllabus . save_attachments ( params [ :attachments ] )
if @syllabus . save
2016-06-30 10:44:11 +08:00
if params [ :asset_id ]
ids = params [ :asset_id ] . split ( ',' )
update_kindeditor_assets_owner ids , @syllabus . id , OwnerTypeHelper :: SYLLABUS
end
2016-06-29 10:45:56 +08:00
redirect_to syllabus_path ( @syllabus )
else
redirect_to syllabus_path ( @syllabus )
end
end
2016-06-30 10:44:11 +08:00
#删除课程大纲的描述
2016-07-01 16:25:52 +08:00
def delete_des
if @syllabus
@syllabus . description = Message . where ( " id = 19412 " ) . first . nil? ? '' : Message . where ( " id = 19412 " ) . first . content
@syllabus . des_status = 0
@syllabus . attachments . destroy_all
if @syllabus . save
redirect_to syllabus_path ( @syllabus )
end
end
end
2016-06-30 10:44:11 +08:00
def destroy
2016-07-26 10:38:04 +08:00
if @syllabus && @syllabus . courses . not_deleted . empty?
2016-06-30 10:44:11 +08:00
@syllabus . destroy
2016-07-01 16:25:52 +08:00
redirect_to user_courselist_user_path ( User . current . id )
2016-06-30 10:44:11 +08:00
end
end
#班级列表
def syllabus_courselist
@order , @c_sort , @type = params [ :order ] || 1 , params [ :sort ] || 1 , params [ :type ] || 1
#确定 sort_type
if @order . to_i == @type . to_i
@c_sort = @c_sort . to_i == 1 ? 2 : 1 #1升序 2降序
else
@c_sort = 2
end
sort_name = " updated_on "
sort_type = @c_sort == 1 ? " asc " : " desc "
2016-07-22 10:20:28 +08:00
@courses = @syllabus . courses . where ( " is_delete = ? " , 0 ) . select ( " courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS #{ sort_name } " ) . order ( " #{ sort_name } #{ sort_type } " )
2016-06-30 10:44:11 +08:00
#根据 作业+资源数排序
if @order . to_i == 2
@type = 2
@courses . each do | course |
course [ :infocount ] = ( User . current . admin? || User . current . allowed_to? ( :as_teacher , course ) ) ? ( course . homework_commons . count + visable_attachemnts_incourse ( course ) . count ) : ( course . homework_commons . where ( " publish_time <= ' #{ Date . today } ' " ) . count + visable_attachemnts_incourse ( course ) . count )
if course [ :infocount ] < 0
course [ :infocount ] = 0
end
end
@c_sort == 1 ? ( @courses = @courses . sort { | x , y | x [ :infocount ] < = > y [ :infocount ] } ) : ( @courses = @courses . sort { | x , y | y [ :infocount ] < = > x [ :infocount ] } )
@courses = sortby_time_countcommon_nosticky @courses , sort_name
else
@type = 1
end
#分页
@limit = 10
@is_remote = true
@atta_count = @courses . count
@atta_pages = Paginator . new @atta_count , @limit , params [ 'page' ] || 1
@offset || = @atta_pages . offset
@courses = paginateHelper @courses , @limit
respond_to do | format |
format . js
format . html { render :layout = > 'base_syllabus' }
end
end
2016-07-07 15:04:57 +08:00
#修改课程名称
def edit_syllabus_title
if @syllabus && params [ :title ] != " "
@syllabus . update_column ( " title " , params [ :title ] )
end
respond_to do | format |
format . js
end
end
2016-07-01 11:11:53 +08:00
#修改英文名称
def edit_syllabus_eng_name
if @syllabus
@syllabus . update_column ( " eng_name " , params [ :eng_name ] )
end
respond_to do | format |
format . js
end
end
#编辑属性
def update_base_info
if @syllabus
@syllabus . update_attributes ( :credit = > params [ :credit ] , :hours = > params [ :hours ] , :theory_hours = > params [ :theory_hours ] , :practice_hours = > params [ :practice_hours ] , :applicable_major = > params [ :applicable_major ] , :pre_course = > params [ :pre_course ] )
@syllabus . update_attributes ( :syllabus_type = > params [ :syllabus_type ] )
respond_to do | format |
format . js
end
end
end
2016-06-14 10:38:11 +08:00
private
def find_syllabus
@syllabus = Syllabus . find params [ :id ]
end
def is_logged
redirect_to signin_path unless User . current . logged?
end
end