Class: Senkyoshi::CanvasCourse
- Inherits:
-
Object
- Object
- Senkyoshi::CanvasCourse
- Defined in:
- lib/senkyoshi/canvas_course.rb
Overview
This class represents a canvas course for which we are uploading data to
Instance Attribute Summary collapse
-
#scorm_packages ⇒ Object
readonly
Returns the value of attribute scorm_packages.
Class Method Summary collapse
-
.client ⇒ Object
Create a new pandarus instance to communicate with the canvas server.
-
.from_metadata(metadata, blackboard_export = nil) ⇒ Object
Find or Create a new CanvasCourse instance from the given metadata.
-
.metadata_from_file(filename) ⇒ Object
Given a filename to a zip file, extract the necessary metadata for the course.
Instance Method Summary collapse
-
#_create_scorm_assignment_external(scorm_package, course_id) ⇒ Object
Creates a scorm assignment using the Canvas api.
-
#_create_scorm_assignment_local(scorm_package) ⇒ Object
Creates a scorm assignment from a Canvas course object.
-
#_scorm_launch_url(package_id) ⇒ Object
Assembles the launch url with the course_id.
-
#create_scorm_assignment(scorm_package, course_id, local) ⇒ Object
Creates a canvas assignment from a scorm package that has already been uploaded to a scorm manager.
-
#create_scorm_assignments(scorm_packages, course_id, local) ⇒ Object
Creates assignments from all previously uploaded scorm packages.
-
#initialize(metadata, course_resource, blackboard_export) ⇒ CanvasCourse
constructor
A new canvas course accepts the metadata for a course and the pandarus course resourse.
- #process_scorm(local: false) ⇒ Object
-
#update_scorm_package(package_id, lms_assignment_id, points_possible) ⇒ Object
Updates scorm manager with lms data for scorm course.
-
#upload_content(filename) ⇒ Object
Create a migration for the course and upload the imscc file to be imported into the course.
-
#upload_scorm_package(scorm_package, course_id, tmp_name) ⇒ Object
Uploads a scorm package to scorm manager specified in senkyoshi.yml config file.
-
#upload_scorm_packages(scorm_packages) ⇒ Object
Uploads all scorm packages to scorm manager specified in senkyoshi.yml config file.
- #upload_to_s3(migration, filename) ⇒ Object
Constructor Details
#initialize(metadata, course_resource, blackboard_export) ⇒ CanvasCourse
A new canvas course accepts the metadata for a course and the pandarus course resourse
33 34 35 36 37 |
# File 'lib/senkyoshi/canvas_course.rb', line 33 def initialize(, course_resource, blackboard_export) @metadata = @course_resource = course_resource @scorm_packages = ScormPackage.get_scorm_packages(blackboard_export) end |
Instance Attribute Details
#scorm_packages ⇒ Object (readonly)
Returns the value of attribute scorm_packages.
27 28 29 |
# File 'lib/senkyoshi/canvas_course.rb', line 27 def scorm_packages @scorm_packages end |
Class Method Details
.client ⇒ Object
Create a new pandarus instance to communicate with the canvas server
57 58 59 60 61 62 |
# File 'lib/senkyoshi/canvas_course.rb', line 57 def self.client @client ||= Pandarus::Client.new( prefix: Senkyoshi.configuration.canvas_url, token: Senkyoshi.configuration.canvas_token, ) end |
.from_metadata(metadata, blackboard_export = nil) ⇒ Object
Find or Create a new CanvasCourse instance from the given metadata
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/senkyoshi/canvas_course.rb', line 67 def self.(, blackboard_export = nil) course_name = [:name] || [:title] canvas_course = client.create_new_course( Senkyoshi.configuration.account_id, course: { name: course_name, }, ) CanvasCourse.new(, canvas_course, blackboard_export) end |
.metadata_from_file(filename) ⇒ Object
Given a filename to a zip file, extract the necessary metadata for the course
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/senkyoshi/canvas_course.rb', line 43 def self.(filename) Zip::File.open(filename) do |file| settings = "course_settings/course_settings.xml" config = file.find_entry(settings).get_input_stream.read doc = Nokogiri::XML(config) { name: doc.at("title").text, } end end |
Instance Method Details
#_create_scorm_assignment_external(scorm_package, course_id) ⇒ Object
Creates a scorm assignment using the Canvas api
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/senkyoshi/canvas_course.rb', line 123 def _create_scorm_assignment_external(scorm_package, course_id) url = _scorm_launch_url(scorm_package["package_id"]) payload = { assignment__submission_types__: ["external_tool"], assignment__integration_id__: scorm_package["package_id"], assignment__integration_data__: { provider: "atomic-scorm", }, assignment__external_tool_tag_attributes__: { url: url, }, assignment__points_possible__: scorm_package["points_possible"], } lms_assignment = CanvasCourse.client.create_assignment( course_id, scorm_package["title"], payload, ) lms_assignment_id = lms_assignment["id"] points_possible = lms_assignment["points_possible"] update_scorm_package( scorm_package["package_id"], lms_assignment_id, points_possible, ) end |
#_create_scorm_assignment_local(scorm_package) ⇒ Object
Creates a scorm assignment from a Canvas course object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/senkyoshi/canvas_course.rb', line 100 def _create_scorm_assignment_local(scorm_package) url = _scorm_launch_url(scorm_package["package_id"]) payload = { title: scorm_package["title"], submission_types: "external_tool", integration_id: scorm_package["package_id"], integration_data: { provider: "atomic-scorm", }, external_tool_tag_attributes: { url: url, }, points_possible: scorm_package["points_possible"], } # @course_resource in this case is a Canvas course object @course_resource.assignments.create(payload) end |
#_scorm_launch_url(package_id) ⇒ Object
Assembles the launch url with the course_id
93 94 95 |
# File 'lib/senkyoshi/canvas_course.rb', line 93 def _scorm_launch_url(package_id) "#{Senkyoshi.configuration.scorm_launch_url}?course_id=#{package_id}" end |
#create_scorm_assignment(scorm_package, course_id, local) ⇒ Object
Creates a canvas assignment from a scorm package that has already been uploaded to a scorm manager
82 83 84 85 86 87 88 |
# File 'lib/senkyoshi/canvas_course.rb', line 82 def create_scorm_assignment(scorm_package, course_id, local) if local _create_scorm_assignment_local(scorm_package) else _create_scorm_assignment_external(scorm_package, course_id) end end |
#create_scorm_assignments(scorm_packages, course_id, local) ⇒ Object
Creates assignments from all previously uploaded scorm packages
204 205 206 207 208 |
# File 'lib/senkyoshi/canvas_course.rb', line 204 def create_scorm_assignments(scorm_packages, course_id, local) scorm_packages.each do |pack| create_scorm_assignment(pack, course_id, local) end end |
#process_scorm(local: false) ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/senkyoshi/canvas_course.rb', line 223 def process_scorm(local: false) create_scorm_assignments( upload_scorm_packages(@scorm_packages), @course_resource.id, local, ) end |
#update_scorm_package(package_id, lms_assignment_id, points_possible) ⇒ Object
Updates scorm manager with lms data for scorm course
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/senkyoshi/canvas_course.rb', line 156 def update_scorm_package(package_id, lms_assignment_id, points_possible) config = Senkyoshi.configuration url = "#{config.scorm_url}/api/scorm_courses/#{package_id}" RestClient.put( url, { oauth_consumer_key: config.scorm_oauth_consumer_key, shared_auth: true, scorm_course: { lms_assignment_id: lms_assignment_id, points_possible: points_possible, }, }, Authorization: "Bearer #{AuthToken.issue_token}", ) end |
#upload_content(filename) ⇒ Object
Create a migration for the course and upload the imscc file to be imported into the course
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/senkyoshi/canvas_course.rb', line 235 def upload_content(filename) client = CanvasCourse.client name = File.basename(filename) # Create a migration for the course and get S3 upload authorization migration = client. create_content_migration_courses( @course_resource.id, :canvas_cartridge_importer, pre_attachment: { name: name }, ) puts "Uploading: #{name}" upload_to_s3(migration, filename) puts "Done uploading: #{name}" puts "Creating Scorm: #{name}" process_scorm puts "Done creating scorm: #{name}" end |
#upload_scorm_package(scorm_package, course_id, tmp_name) ⇒ Object
Uploads a scorm package to scorm manager specified in senkyoshi.yml config file
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/senkyoshi/canvas_course.rb', line 177 def upload_scorm_package(scorm_package, course_id, tmp_name) zip = scorm_package.write_zip tmp_name config = Senkyoshi.configuration url = "#{config.scorm_url}/api/scorm_courses" File.open(zip, "rb") do |file| RestClient.post( url, { oauth_consumer_key: config.scorm_oauth_consumer_key, lms_course_id: course_id, file: file, shared_auth: true, }, Authorization: "Bearer #{AuthToken.issue_token}", ) do |resp| result = JSON.parse(resp.body) response = result["response"] response["points_possible"] = scorm_package.points_possible response["package_id"] = result["package_id"] response end end end |
#upload_scorm_packages(scorm_packages) ⇒ Object
Uploads all scorm packages to scorm manager specified in senkyoshi.yml config file
214 215 216 217 218 219 220 221 |
# File 'lib/senkyoshi/canvas_course.rb', line 214 def upload_scorm_packages(scorm_packages) package_index = 0 scorm_packages.map do |pack| package_index += 1 tmp_name = "#{@metadata[:name]}_#{package_index}.zip" upload_scorm_package(pack, @course_resource.id, tmp_name) end end |
#upload_to_s3(migration, filename) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/senkyoshi/canvas_course.rb', line 255 def upload_to_s3(migration, filename) File.open(filename, "rb") do |file| # Attach the file to the S3 auth = migration. upload_url = ["upload_url"] upload_params = ["upload_params"] upload_params[:file] = file # Post to S3 RestClient::Request.execute( method: :post, url: upload_url, payload: upload_params, timeout: Senkyoshi.configuration.request_timeout, ) do |response| # Post to Canvas RestClient.post( response.headers[:location], nil, Authorization: "Bearer #{Senkyoshi.configuration.canvas_token}", ) end end end |