hps_learning/app/controllers/admin/hps_learnings_controller.rb

136 lines
3.8 KiB
Ruby

require 'zip/zip'
class Admin::HpsLearningsController < OrbitAdminController
def index
@table_fields = ["hps_learning.course_pic", "hps_learning.title", :category, "hps_learning.runtime"]
@classes = HpsClass.all.desc(:created_at).page(params[:page]).per(10)
end
def new
@hpsclass = HpsClass.new
end
def edit
@hpsclass = HpsClass.find(params[:id])
end
def show
@table_fields = ["hps_learning.lecture_pic", "hps_learning.subject", "hps_learning.presenter" , "hps_learning.runtime", "hps_learning.link_to_lecture"]
@hpsclass = HpsClass.find(params[:id])
end
def create
hpsclass = HpsClass.new(hps_class_params)
hpsclass.save
redirect_to admin_hps_learnings_path
end
def update
hpsclass = HpsClass.find(params[:id])
hpsclass.update_attributes(hps_class_params)
redirect_to admin_hps_learnings_path
end
def destroy
hpsclass = HpsClass.find(params[:id])
hpsclass.destroy
redirect_to admin_hps_learnings_path
end
def add_lecture
@hpslecture = HpsLecture.new
end
def edit_lecture
@hpslecture = HpsLecture.find(params[:id])
end
def delete_lecture
hpslecture = HpsLecture.find(params[:id])
clas = hpslecture.hps_class_id
hpslecture.destroy
redirect_to admin_hps_learning_path(clas)
end
def create_lecture
hpsclass = HpsClass.find(params[:id])
hpslecture = HpsLecture.new(hps_lecture_params)
hpslecture.hps_class = hpsclass
hpsfile = HpsLectureFile.find(params[:presentation_temp_id]) rescue nil
hpslecture.hps_lecture_file = hpsfile if !hpsfile.nil?
hpslecture.save
redirect_to admin_hps_learning_path(hpsclass.id)
end
def update_lecture
hpslecture = HpsLecture.find(params[:id])
hpslecture.update_attributes(hps_lecture_params)
hpsfile = HpsLectureFile.find(params[:presentation_temp_id]) rescue nil
hpslecture.hps_lecture_file = hpsfile if !hpsfile.nil?
hpslecture.save
redirect_to admin_hps_learning_path(hpslecture.hps_class_id)
end
def upload_temp_file
filepath = params[:path]
file = params[:presentation_file]
File.open(filepath,"ab"){ |f| f.write(file.read) }
render :json => {"success" => true}.to_json
end
def get_temp_dir_name
dirname = Digest::MD5.hexdigest(Time.now.to_s)
directory = "public/hps_temp_files/#{dirname}"
FileUtils.mkdir_p(directory) unless File.exists?(directory)
path_to_file = "#{directory}/#{params[:filename]}"
File.open(path_to_file,"w")
hpsfile = HpsLectureFile.new
hpsfile.presentation = Rails.root.join(path_to_file).open
hpsfile.save
hpsfile.index_path = hpsfile.presentation.url.sub(".zip", "/index.html")
hpsfile.save
FileUtils.remove_dir(directory)
render :json => {"presentation_id" => hpsfile.id.to_s, "path" => hpsfile.presentation.path}.to_json
end
def clear_temp_dir
presentation = HpsLectureFile.find(params[:presentation_id]) rescue nil
if !presentation.nil?
presentation.remove_presentation
presentation.destroy
end
render :json => {"success" => true}.to_json
end
def extract_zip
path = params[:path]
destination = path.sub("/" + File.basename(path), "")
files = []
Zip::ZipFile.open(path) { |zip_file|
zip_file.each { |f|
f_path=File.join(destination, f.name)
files << f.name
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
foldername = File.basename(path).split(".")
foldername.pop
foldername = foldername.join(".")
if files.include?(foldername + "/index.html")
render :json => {"success" => true}.to_json
else
render :json => {"success" => false, "msg" => "index.html file not present."}.to_json
end
end
private
def hps_class_params
params.require(:hps_class).permit!
end
def hps_lecture_params
params.require(:hps_lecture).permit!
end
end