diff --git a/app/controllers/admin/olympiamanagements_controller.rb b/app/controllers/admin/olympiamanagements_controller.rb index 7b327d7..2fa39dc 100644 --- a/app/controllers/admin/olympiamanagements_controller.rb +++ b/app/controllers/admin/olympiamanagements_controller.rb @@ -3,7 +3,9 @@ class Admin::OlympiamanagementsController < OrbitAdminController require 'rubyXL' require 'fileutils' require "axlsx" + before_action :create_first_fields helper Admin::OlympiamanagementsHelper + load File.expand_path(__dir__)+'/zipgenerator.rb' def index end def add_school_data @@ -223,6 +225,12 @@ class Admin::OlympiamanagementsController < OrbitAdminController tmp_filename_data = File.read(dir_path +@filename) send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) end + def sign_up_student_data_list + page_num = params[:page] || 1 + @field_infos = StudentDataField.first.student_data_fields + @sign_up_setting_id = SignUpSetting.last.id + @student_fields = OlympiaStudentDataField.where(:olympia_school_data_fields_id=>params[:olympia_school_id],:sign_up_setting_id=>@sign_up_setting_id).asc(:id).page(page_num).per(10) + end def export_sign_up_student_data # @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) # dir_path = 'tmp/olypiamanagement/' @@ -238,10 +246,47 @@ class Admin::OlympiamanagementsController < OrbitAdminController # send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename) end def download_scan_file_of_certificate + @sign_up_setting_id = SignUpSetting.last.id + @student_data_list = OlympiaStudentDataField.where(:sign_up_setting_id=>@sign_up_setting_id) + @student_data_image_path_arr = @student_data_list.flat_map{|student_data| student_data.olympia_student_images}.map{|student_image| student_image.student_file.file.file} + zip_path = "tmp/student_images/#{@sign_up_setting_id}/" + tmp_filename = '' + FileUtils.rm_r(zip_path, :force => true) if Dir.exist?(zip_path) + FileUtils.mkdir_p zip_path #create dir for storing tmp_file + @student_data_image_path_arr.each do |image_path| + @pathname = Pathname.new(image_path) + @temp_name = @pathname.basename + Dir.chdir(@pathname.dirname.to_s) do + @index = 1 + @original_filename =@pathname.basename.to_s + @new_pathname = Pathname.new("#{Rails.root}/#{zip_path}#{@original_filename}") + while @new_pathname.exist? do + @new_pathname = Pathname.new(@new_pathname.to_s.insert(@new_pathname.to_s.rindex('.'),"-#{@index}")) + @index += 1 + end + #a[0...b].concat('-1').concat(a[b..-1]) + FileUtils.cp(@original_filename,@new_pathname) + end + end + @filename = Pathname.new(zip_path).parent.to_s+"/#{Time.now.strftime('%Y_%m_%d')}_student_images.zip" + zip_file= ZipFileGenerator.new(zip_path,@filename) + begin + zip_file.write + rescue + File.delete(@filename) + zip_file.write + end + tmp_filename_data = File.read(@filename) + send_data(tmp_filename_data, type: 'application/zip', disposition: 'attachment', filename: Pathname.new(@filename).basename.to_s) end def import_editing_number_of_school_class @olympia_school_data_field = OlympiaSchoolDataFields.new end + def create_first_fields + StudentDataField.create if StudentDataField.all.length == 0 + SchoolDataFields.create if StudentDataField.all.length == 0 + SignUpSetting.create if SignUpSetting.all.length == 0 + end def download_import_file @OlympiaSchoolDataFields = OlympiaSchoolDataFields.all.asc(:id) dir_path = 'tmp/olypiamanagement/' diff --git a/app/controllers/admin/zipgenerator.rb b/app/controllers/admin/zipgenerator.rb new file mode 100644 index 0000000..6d7d6cf --- /dev/null +++ b/app/controllers/admin/zipgenerator.rb @@ -0,0 +1,61 @@ +require 'zip' + +# This is a simple example which uses rubyzip to +# recursively generate a zip file from the contents of +# a specified directory. The directory itself is not +# included in the archive, rather just its contents. +# +# Usage: +# directory_to_zip = "/tmp/input" +# output_file = "/tmp/out.zip" +# zf = ZipFileGenerator.new(directory_to_zip, output_file) +# zf.write() +class ZipFileGenerator + # Initialize with the directory to zip and the location of the output archive. + def initialize(input_dir, output_file) + @input_dir = input_dir + @output_file = output_file + end + + # Zip the input directory. + def write + entries = Dir.entries(@input_dir) - %w[. ..] + + ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile| + write_entries entries, '', zipfile + end + end + def extract_zip + FileUtils.mkdir_p(@output_file) + Zip::File.open(@input_dir) do |zip_file| + zip_file.each do |f| + fpath = File.join(@output_file, f.name) + zip_file.extract(f, fpath) unless File.exist?(fpath) + end + end + end + private + + # A helper method to make the recursion work. + def write_entries(entries, path, zipfile) + entries.each do |e| + zipfile_path = path == '' ? e : File.join(path, e) + disk_file_path = File.join(@input_dir, zipfile_path) + + if File.directory? disk_file_path + recursively_deflate_directory(disk_file_path, zipfile, zipfile_path) + else + put_into_archive(disk_file_path, zipfile, zipfile_path) + end + end + end + def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path) + zipfile.mkdir zipfile_path + subdir = Dir.entries(disk_file_path) - %w[. ..] + write_entries subdir, zipfile_path, zipfile + end + + def put_into_archive(disk_file_path, zipfile, zipfile_path) + zipfile.add(zipfile_path, disk_file_path) + end +end diff --git a/app/models/olympia_images_uploader.rb b/app/models/olympia_images_uploader.rb index dc581ac..f9e141d 100644 --- a/app/models/olympia_images_uploader.rb +++ b/app/models/olympia_images_uploader.rb @@ -19,7 +19,7 @@ class OlympiaImagesUploader < CarrierWave::Uploader::Base # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir - return "uploads/#{model.class.to_s.underscore}/olympia_students" + return "uploads/#{model.class.to_s.underscore}/olympia_students/#{model.id}/" end # override diff --git a/app/views/admin/olympiamanagements/school_data_list.html.erb b/app/views/admin/olympiamanagements/school_data_list.html.erb index 3ce8df2..a5f8058 100644 --- a/app/views/admin/olympiamanagements/school_data_list.html.erb +++ b/app/views/admin/olympiamanagements/school_data_list.html.erb @@ -33,7 +33,7 @@ <% else %>
| <%= t('olympiamanagement.'+th_name) %> | + <% end %> +|
|---|---|
| <%= @value %> | + <% else %> +
+ "> |
+ <% end %>
+ <% end %>
+