From ff4dae56d1dbc30304697136157b4e88ed937cbc Mon Sep 17 00:00:00 2001 From: sw <939547590@qq.com> Date: Thu, 28 May 2015 10:30:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E6=89=80=E6=9C=89=E4=BD=9C?= =?UTF-8?q?=E5=93=81=E9=99=84=E4=BB=B6=E6=89=93=E5=8C=85=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/zipdown_controller.rb | 82 +++++++++++++++++---------- app/views/student_work/index.html.erb | 8 ++- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index 06d69f72e..9b0b97ff2 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -29,26 +29,25 @@ class ZipdownController < ApplicationController render file: 'public/no_file_found.html' return end + elsif params[:obj_class] == "HomeworkCommon" + homework = HomeworkCommon.find params[:obj_id] + render_403 if User.current.allowed_to?(:as_teacher,homework.course) + file_count = 0 + homework.student_works.map { |work| file_count += work.attachments.count} + if file_count > 0 + zipfile = zip_homework_common homework + else + render file: 'public/no_file_found.html' + return + end else logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!" end - - # if zipfile - # if zipfile.length > 1 - # @mut_down_files = zipfile #zipfile.each{|x| File.basename(x)} - # else - # send_file zipfile.first[:real_file], :filename => bid.name + ".zip", :type => detect_content_type(zipfile.first[:real_file]) - # return - # end - # end - respond_to do |format| format.json { render json: zipfile.to_json } end - #rescue Exception => e - # render file: 'public/no_file_found.html' end #下载某一学生的作业的所有文件 @@ -98,29 +97,34 @@ class ZipdownController < ApplicationController digests << out_file.file_digest end end - homework_id = bid.id user_id = bid.author_id - - out_file = find_or_pack(homework_id, user_id, digests.sort){ zipping("#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER) } + [{files:[out_file.file_path], count: 1, index: 1, + real_file: out_file.file_path, file: File.basename(out_file.file_path), + size:(out_file.pack_size / 1024.0 / 1024.0).round(2) + }] + end - - # zips = split_pack_files(bid_homework_path, Setting.pack_attachment_max_size.to_i*1024) - # x = 0 - # - # - # zips.each { |o| - # x += 1 - # file = zipping "#{Time.now.to_i}_#{bid.name}_#{x}.zip", o[:files], OUTPUT_FOLDER - # o[:real_file] = file - # o[:file] = File.basename(file) - # o[:size] = (File.size(file) / 1024.0 / 1024.0).round(2) - # } - + def zip_homework_common homework_common + bid_homework_path = [] + digests = [] + homework_common.student_works.each do |work| + unless work.attachments.empty? + out_file = zip_student_work_by_user(work) + bid_homework_path << out_file.file_path + digests << out_file.file_digest + end + end + homework_id = homework_common.id + user_id = homework_common.user_id + out_file = find_or_pack(homework_id, user_id, digests.sort){ + zipping("#{Time.now.to_i}_#{homework_common.name}.zip", + bid_homework_path, OUTPUT_FOLDER) + } [{files:[out_file.file_path], count: 1, index: 1, real_file: out_file.file_path, file: File.basename(out_file.file_path), size:(out_file.pack_size / 1024.0 / 1024.0).round(2) @@ -131,8 +135,6 @@ class ZipdownController < ApplicationController homeworks_attach_path = [] not_exist_file = [] # 需要将所有homework.attachments遍历加入zip - - digests = [] homework_attach.attachments.each do |attach| if File.exist?(attach.diskfile) @@ -143,12 +145,30 @@ class ZipdownController < ApplicationController digests << 'not_exist_file' end end - out_file = find_or_pack(homework_attach.bid_id, homework_attach.user_id, digests.sort){ zipping("#{homework_attach.user.lastname}#{homework_attach.user.firstname}_#{((homework_attach.user.user_extensions.nil? || homework_attach.user.user_extensions.student_id.nil?) ? "" : homework_attach.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) } + end + def zip_student_work_by_user work + homeworks_attach_path = [] + not_exist_file = [] + # 需要将所有homework.attachments遍历加入zip + digests = [] + work.attachments.each do |attach| + if File.exist?(attach.diskfile) + homeworks_attach_path << attach.diskfile + digests << attach.digest + else + not_exist_file << attach.filename + digests << 'not_exist_file' + end + end + out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ + zipping("#{work.user.lastname}#{work.user.firstname}_#{((work.user.user_extensions.nil? || work.user.user_extensions.student_id.nil?) ? "" : work.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", + homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) + } end diff --git a/app/views/student_work/index.html.erb b/app/views/student_work/index.html.erb index e7c6e77e7..0309e2d72 100644 --- a/app/views/student_work/index.html.erb +++ b/app/views/student_work/index.html.erb @@ -38,7 +38,13 @@
- 附件 + <% if @homework.student_works.empty?%> + <%= link_to "附件", "javascript:void(0)", class: "down_btn fr", :onclick => "alert('没有学生提交作业,无法下载附件')" %> + <% else%> + <%= link_to "附件", zipdown_assort_path(obj_class: @homework.class, obj_id: @homework, format: :json), + remote: true, class: "down_btn fr" %> + <% end%> + 列表 导出全部: