2013-10-22 09:02:48 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
module FilesHelper
|
2014-12-05 09:54:13 +08:00
|
|
|
include AttachmentsHelper
|
2013-10-22 09:02:48 +08:00
|
|
|
|
|
|
|
def downloadAll containers
|
|
|
|
paths = []
|
|
|
|
files = []
|
|
|
|
tmpfile = "tmp.zip"
|
|
|
|
|
|
|
|
containers.each do |container|
|
|
|
|
next if container.attachments.empty?
|
|
|
|
if container.is_a?(Version);end
|
|
|
|
container.attachments.each do |attachment|
|
|
|
|
paths << attachment.diskfile
|
|
|
|
file = attachment.diskfile
|
|
|
|
# logger.error "[FilesHelper] downloadAll: #{e}"
|
|
|
|
begin
|
|
|
|
File.new(file, "r")
|
|
|
|
rescue Exception => e
|
|
|
|
logger.error e
|
|
|
|
next
|
|
|
|
end
|
|
|
|
files << file
|
|
|
|
# zip.add(file.path.dup.sub(directory, ''), file.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
zipfile_name = "archive.zip"
|
|
|
|
if File.exists? File.open(zipfile_name, "w+")
|
|
|
|
ff = File.open(zipfile_name, "w+")
|
|
|
|
ff.close
|
|
|
|
File.delete ff
|
|
|
|
end
|
|
|
|
Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|
|
|
|
|
files.each do |filename|
|
|
|
|
directory = File.dirname filename
|
|
|
|
# Two arguments:
|
|
|
|
# - The name of the file as it will appear in the archive
|
|
|
|
# - The original file, including the path to find it
|
|
|
|
dir = filename.sub(directory+"/", '')
|
|
|
|
zipfile.add(dir, filename)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
File.new(zipfile_name,'w+')
|
|
|
|
end
|
|
|
|
|
2014-11-21 15:54:29 +08:00
|
|
|
def courses_check_box_tags(name,courses,current_course,attachment)
|
2014-11-20 17:44:30 +08:00
|
|
|
s = ''
|
|
|
|
courses.each do |course|
|
2015-03-12 11:17:36 +08:00
|
|
|
if !course_contains_attachment?(course,attachment) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course)
|
2014-11-26 14:54:31 +08:00
|
|
|
s << "<label>#{ check_box_tag name, course.id, false, :id => nil } #{h course.name}</label> [#{get_course_term course}]<br/>"
|
2014-11-20 17:44:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
s.html_safe
|
|
|
|
end
|
|
|
|
|
2014-12-05 09:54:13 +08:00
|
|
|
#判断用户是否拥有不包含当前资源的课程,需用户在该课程中角色为教师且该课程属于当前学期或下一学期
|
|
|
|
def has_course? user,file
|
2014-11-24 14:39:31 +08:00
|
|
|
result = false
|
|
|
|
user.courses.each do |course|
|
2014-12-05 09:54:13 +08:00
|
|
|
if !course_contains_attachment?(course,file) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course)
|
2014-11-24 14:39:31 +08:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-05-13 16:34:53 +08:00
|
|
|
# 判断指定的资源时候符合类型
|
|
|
|
def isTypeOk(attachment, type, contentType)
|
|
|
|
result = false
|
|
|
|
if type != 0
|
|
|
|
if attachment.attachtype == type
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
if result
|
2014-05-14 17:12:04 +08:00
|
|
|
if contentType != '0' && contentType != attachment.suffix_type
|
2014-05-13 16:34:53 +08:00
|
|
|
result = false
|
2014-05-14 17:12:04 +08:00
|
|
|
end
|
2014-05-13 16:34:53 +08:00
|
|
|
end
|
|
|
|
result
|
2014-05-12 18:46:39 +08:00
|
|
|
end
|
|
|
|
|
2014-11-22 13:45:17 +08:00
|
|
|
def visable_attachemnts attachments
|
|
|
|
result = []
|
|
|
|
attachments.each do |attachment|
|
2014-12-01 15:03:47 +08:00
|
|
|
if attachment.is_public? || (attachment.container_type == "Course" && User.current.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id
|
2014-11-22 13:45:17 +08:00
|
|
|
result << attachment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-11-27 17:17:33 +08:00
|
|
|
def visable_attachemnts_incourse attachments
|
|
|
|
result = []
|
|
|
|
attachments.each do |attachment|
|
|
|
|
if attachment.is_public? || (attachment.author.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id
|
|
|
|
result << attachment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def visable_attachemnts_insite attachments,course
|
|
|
|
result = []
|
|
|
|
attachments.each do |attachment|
|
2014-12-12 18:06:10 +08:00
|
|
|
if attachment.is_public? || (attachment.container_type == "Course" && attachment.container_id == course.id && User.current.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id
|
2014-11-27 17:17:33 +08:00
|
|
|
result << attachment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-12-12 16:53:49 +08:00
|
|
|
def attachment_candown attachment
|
|
|
|
candown = false
|
2014-12-12 16:59:18 +08:00
|
|
|
if attachment.container
|
2014-12-12 16:53:49 +08:00
|
|
|
if attachment.container.class.to_s != "HomeworkAttach" && (attachment.container.has_attribute?(:project) || attachment.container.has_attribute?(:project_id)) && attachment.container.project
|
|
|
|
project = attachment.container.project
|
|
|
|
candown= User.current.member_of?(project) || (project.is_public && attachment.is_public == 1)
|
|
|
|
elsif attachment.container.is_a?(Project)
|
|
|
|
project = attachment.container
|
|
|
|
candown= User.current.member_of?(project) || (project.is_public && attachment.is_public == 1)
|
|
|
|
elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board &&
|
|
|
|
attachment.container.board.project
|
|
|
|
project = attachment.container.board.project
|
|
|
|
candown = User.current.member_of?(project) || (project.is_public && attachment.is_public == 1)
|
|
|
|
elsif (attachment.container.has_attribute?(:course) ||attachment.container.has_attribute?(:course_id) ) && attachment.container.course
|
|
|
|
course = attachment.container.course
|
|
|
|
candown = User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1)
|
|
|
|
elsif attachment.container.is_a?(Course)
|
|
|
|
course = attachment.container
|
|
|
|
candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1)
|
|
|
|
elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board &&
|
|
|
|
attachment.container.board.course
|
|
|
|
course = attachment.container.board.course
|
|
|
|
candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1)
|
|
|
|
elsif attachment.container.class.to_s=="HomeworkAttach" && attachment.container.bid.reward_type == 3
|
|
|
|
candown = true
|
|
|
|
elsif attachment.container_type == "Bid" && attachment.container && attachment.container.courses
|
|
|
|
course = attachment.container.courses.first
|
|
|
|
candown = User.current.member_of_course?(attachment.container.courses.first) || (course.is_public == 1 && attachment.is_public == 1)
|
|
|
|
else
|
|
|
|
candown = (attachment.is_public == 1 || attachment.is_public == true)
|
|
|
|
end
|
2014-12-12 16:59:18 +08:00
|
|
|
end
|
2014-12-12 16:53:49 +08:00
|
|
|
candown
|
|
|
|
end
|
|
|
|
|
2014-11-27 17:17:33 +08:00
|
|
|
|
2014-12-02 15:30:32 +08:00
|
|
|
|
2013-10-22 09:02:48 +08:00
|
|
|
end
|