@ -9,19 +9,20 @@ class FilesController < ApplicationController
before_action :set_pagination , only : % i [ index public_with_course_and_project mine_with_course_and_project ]
before_action :validate_upload_params , only : % i [ upload import ]
before_action :find_file , only : % i [ show setting update ]
before_action :publish_params , only : % i [ upload import update ]
SORT_TYPE = %w[ created_on downloads quotes ]
def index
sort = params [ :sort ] || 0 # 0: 降序; 1: 升序
sort_type = params [ :sort_type ] || 'created_on' # created_on: 时间排序, downloads: 下载次数排序; quotes: 引用次数排序
course_second_category_id = params [ :course_second_category_id ] || 0 # 0: 为主目录, 其他为次目录id
@ course_second_category_id = params [ :course_second_category_id ] || 0 # 0: 为主目录, 其他为次目录id
@user = current_user
@attachments = course_second_category_id. to_i == 0 ? @course . attachments : @course . attachments . by_course_second_category_id ( course_second_category_id)
@attachments = @attachments . includes ( a ttachment_group_settings: :course_group , a uthor: [ :user_extension , :course_members ] )
@attachments = @ course_second_category_id. to_i == 0 ? @course . attachments . includes ( :course_second_category ) : @course . attachments . by_course_second_category_id ( @ course_second_category_id)
@attachments = @attachments . includes ( a uthor: [ :user_extension , :course_members ] )
. ordered ( sort : sort . to_i , sort_type : sort_type . strip )
get_category ( @course , course_second_category_id)
get_category ( @course , @ course_second_category_id)
@total_count = @attachments . size
@publish_count = @attachments . published . size
@unpublish_count = @total_count - @publish_count
@ -137,9 +138,9 @@ class FilesController < ApplicationController
def upload
attachment_ids = params [ :attachment_ids ]
course_second_category_id = params [ :course_second_category_id ] || 0 # 0: 为主目录, 其他为次目录id
is_unified_setting = params . has_key? ( :is_unified_setting ) ? params [ :is_unified_setting ] : true
publish_time = params [ :publish_time ]
course_group_publish_times = params [ :course_group_publish_times ] || [ ]
# is_unified_setting = params.has_key?(:is_unified_setting) ? params[:is_unified_setting] : true
# publish_time = params[:publish_time ]
# course_group_publish_times = params[:course_group_publish_times] || [ ]
begin
attachment_ids . each do | attchment_id |
@ -148,9 +149,12 @@ class FilesController < ApplicationController
attachment . container = @course
attachment . course_second_category_id = course_second_category_id
attachment . description = params [ :description ]
attachment . is_public = params [ :is_public ] ? 1 : 0
attachment . set_publish_time ( publish_time ) if is_unified_setting
attachment . set_course_group_publish_time ( @course , course_group_publish_times ) if @course . course_groups . size > 0 && ! is_unified_setting && publish_time . blank?
attachment . is_public = params [ :is_public ] && @course . is_public == 1 ? 1 : 0
attachment . is_publish = @atta_is_publish
attachment . delay_publish = @atta_delay_publish
attachment . publish_time = @atta_publish_time
# attachment.set_publish_time(publish_time) if is_unified_setting
# attachment.set_course_group_publish_time(@course, course_group_publish_times) if @course.course_groups.size > 0 && !is_unified_setting && publish_time.blank?
attachment . save!
end
end
@ -188,8 +192,9 @@ class FilesController < ApplicationController
attach_copied_obj . created_on = Time . now
attach_copied_obj . author = current_user
attach_copied_obj . is_public = 0
attach_copied_obj . is_publish = 1
attach_copied_obj . publish_time = Time . now
attach_copied_obj . is_publish = @atta_is_publish
attach_copied_obj . delay_publish = @atta_delay_publish
attach_copied_obj . publish_time = @atta_publish_time
attach_copied_obj . course_second_category_id = course_second_category_id
attach_copied_obj . copy_from = ori . copy_from . nil? ? ori . id : ori . copy_from
if attach_copied_obj . attachtype == nil
@ -209,11 +214,7 @@ class FilesController < ApplicationController
def update
return normal_status ( 403 , " 您没有权限进行该操作 " ) if current_user . course_identity ( @course ) > = 5 && @file . author != current_user
is_unified_setting = params [ :is_unified_setting ]
publish_time = params [ :publish_time ]
publish_time = format_time ( Time . parse ( publish_time ) ) unless publish_time . blank?
is_public = params [ :is_public ]
course_group_publish_times = params [ :course_group_publish_times ] || [ ]
@old_attachment = @file
@new_attachment = Attachment . find_by_id params [ :new_attachment_id ]
@ -225,25 +226,29 @@ class FilesController < ApplicationController
old_course_second_category_id = @old_attachment . course_second_category_id
@old_attachment . copy_attributes_from_new_attachment ( @new_attachment )
@old_attachment . is_public = is_public == true ? 1 : 0 if is_public
@old_attachment . course_second_category_id = old_course_second_category_id
@old_attachment . save!
@new_attachment . delete
end
@old_attachment . is_public = is_public == true && @course . is_public == 1 ? 1 : 0
@old_attachment . is_publish = @atta_is_publish
@old_attachment . delay_publish = @atta_delay_publish
@old_attachment . publish_time = @atta_publish_time
if params [ :description ] && ! params [ :description ] . strip . blank? && params [ :description ] != @old_attachment . description
@old_attachment . description = params [ :description ]
end
@old_attachment . set_public ( is_public )
# @old_attachment.set_public(is_public )
if is_unified_setting
@old_attachment . set_publish_time ( publish_time )
@old_attachment . attachment_group_settings . destroy_all
end
# if is_unified_setting
# @old_attachment.set_publish_time(publish_time )
# @old_attachment.attachment_group_settings. destroy_all
# end
if publish_time . blank? && @course . course_groups . size > 0 && ! is_unified_setting
@old_attachment . set_course_group_publish_time ( @course , course_group_publish_times )
end
# if publish_time.blank? && @course.course_groups.size > 0 && ! is_unified_setting
# @old_attachment.set_course_group_publish_time(@course, course_group_publish_times )
# end
@old_attachment . save!
rescue Exception = > e
@ -304,11 +309,19 @@ class FilesController < ApplicationController
end
def file_validate_sort_type
normal_status ( - 2 , " 参数sort_ty o pe暂时只支持 'created_on', 'quotes', 'downloads'" ) if params . has_key? ( :sort_type ) && ! SORT_TYPE . include? ( params [ :sort_type ] . strip )
normal_status ( - 2 , " 参数sort_ty pe暂时只支持 'created_on', 'quotes', 'downloads'" ) if params . has_key? ( :sort_type ) && ! SORT_TYPE . include? ( params [ :sort_type ] . strip )
end
def validate_upload_params
find_attachment_ids
find_course_second_category_id
end
def publish_params
tip_exception ( " 缺少发布参数 " ) if params [ :delay_publish ] . blank?
tip_exception ( " 缺少延期发布的时间参数 " ) if params [ :delay_publish ] . to_i == 1 && params [ :publish_time ] . blank?
@atta_is_publish = params [ :delay_publish ] . to_i == 1 && params [ :publish_time ] . to_time > Time . now ? 0 : 1
@atta_delay_publish = params [ :delay_publish ] . to_i
@atta_publish_time = params [ :delay_publish ] . to_i == 1 && params [ :publish_time ] ? params [ :publish_time ] : Time . now
end
end