/
konbata.rb
116 lines (98 loc) · 3.26 KB
/
konbata.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright (C) 2017 Atomic Jolt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require "canvas_cc"
require "zip"
require "konbata/configuration"
require "konbata/error_logger"
require "konbata/models/scorm_course"
require "konbata/models/interactive_scorm_course"
require "konbata/models/non_interactive_scorm_course"
require "konbata/models/pdf_course"
require "konbata/models/upload_course"
module Konbata
INPUT_DIR = "sources".freeze
OUTPUT_DIR = "canvas".freeze
FILE_BASE = "$IMS_CC_FILEBASE$".freeze
def self.configuration
@configuration ||= Konbata::Configuration.new
end
##
# Create a random hex prepended with aj_
# This is because the instructure qti migration tool requires
# the first character to be a letter.
##
def self.create_random_hex
"aj_" + SecureRandom.hex(32)
end
##
# Iterates through every zip file in the sources directory and converts
# them to a Canvas .imscc file.
##
def self.convert_zips(type)
ErrorLogger.setup
FileUtils.mkdir_p(OUTPUT_DIR)
zip_paths = Dir.glob("#{INPUT_DIR}/*.zip")
zip_paths.each do |zip_path|
puts "Converting #{File.basename(zip_path)}"
convert_zip(zip_path, type)
end
ErrorLogger.notify_or_remove
end
##
# Converts the given zip file to a Canvas .imscc file.
##
def self.convert_zip(zip_path, type)
# Formats path to not have any spaces as the SCORM upload can't handle it.
formatted_path = zip_path.gsub(/\s/, "_")
if formatted_path != zip_path
File.rename(zip_path, formatted_path)
zip_path = formatted_path
end
if type == :pdfs
course = Konbata::PDFCourse.new(zip_path)
else
klass = "Konbata::#{type.to_s.camelize}ScormCourse".constantize
course = klass.new(zip_path)
end
create_imscc(course)
course.cleanup
end
##
# Creates an .imscc file for the given course object.
##
def self.create_imscc(course)
imscc = CanvasCc::CanvasCC::CartridgeCreator.
new(course.canvas_course).
create(Dir.mktmpdir)
FileUtils.cp(imscc, OUTPUT_DIR)
FileUtils.remove_entry_secure(imscc)
end
##
# Uploads all .imscc files in the canvas directory to Canvas.
##
def self.upload_courses(type)
imscc_paths = Dir.glob("#{OUTPUT_DIR}/*.imscc")
imscc_paths.each { |imscc| Konbata.upload_course(imscc, type) }
end
##
# Uploads a course to Canvas.
##
def self.upload_course(imscc_file_path, type)
metadata = Konbata::UploadCourse.metadata_from_file(imscc_file_path)
course = Konbata::UploadCourse.from_metadata(metadata, type)
source_for_imscc = "#{INPUT_DIR}/#{metadata[:title]}.zip"
course.upload_content(imscc_file_path, source_for_imscc)
end
end