You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/export_exercises_service.rb

36 lines
1.0 KiB

6 years ago
class ExportExercisesService
include ExercisesHelper
include StudentWorksHelper
attr_reader :exercise, :ex_users
5 years ago
def initialize(exercise, ex_users,request_url)
6 years ago
@exercise = exercise
@ex_users = ex_users
5 years ago
@request_url = request_url
6 years ago
end
def filename
exercise_export_name = exercise.user.real_name + "_" + exercise.exercise_name + "_" + Time.now.strftime('%Y%m%d_%H%M%S')
"#{exercise_export_name.strip}.zip"
end
def ex_zip
zip_file = Tempfile.new(filename)
pdfs = []
Zip::File.open(zip_file.path, Zip::File::CREATE) do |zip|
ex_users.each do |ex_user|
5 years ago
export = ExerciseUserPdfService.new(exercise, ex_user,request_url)
6 years ago
pdf = export.ex_pdf
pdfs << pdf
begin
zip.add(export.filename, pdf.path)
rescue => ex
Rails.logger.error(ex.message)
zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") }
end
end
zip_file
end
end
end