Class: Senkyoshi::CanvasCourse

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, course_resource, blackboard_export) ⇒ CanvasCourse

A new canvas course accepts the metadata for a course and the pandarus course resourse



16
17
18
19
20
# File 'lib/senkyoshi/canvas_course.rb', line 16

def initialize(, course_resource, blackboard_export)
   = 
  @course_resource = course_resource
  @scorm_packages = ScormPackage.get_scorm_packages(blackboard_export)
end

Instance Attribute Details

#scorm_packagesObject (readonly)

Returns the value of attribute scorm_packages.



10
11
12
# File 'lib/senkyoshi/canvas_course.rb', line 10

def scorm_packages
  @scorm_packages
end

Class Method Details

.clientObject

Create a new pandarus instance to communicate with the canvas server



40
41
42
43
44
45
# File 'lib/senkyoshi/canvas_course.rb', line 40

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



50
51
52
53
54
55
56
57
58
59
# File 'lib/senkyoshi/canvas_course.rb', line 50

def self.(, blackboard_export = nil)
  course_name = [:name] || [:title]
  canvas_course = client.create_new_course(
    Senkyoshi.configuration.,
    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



26
27
28
29
30
31
32
33
34
35
# File 'lib/senkyoshi/canvas_course.rb', line 26

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/senkyoshi/canvas_course.rb', line 106

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"],
  }

  CanvasCourse.client.create_assignment(
    course_id,
    scorm_package["title"],
    payload,
  )
end

#_create_scorm_assignment_local(scorm_package) ⇒ Object

Creates a scorm assignment from a Canvas course object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/senkyoshi/canvas_course.rb', line 83

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



76
77
78
# File 'lib/senkyoshi/canvas_course.rb', line 76

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



65
66
67
68
69
70
71
# File 'lib/senkyoshi/canvas_course.rb', line 65

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



154
155
156
157
158
# File 'lib/senkyoshi/canvas_course.rb', line 154

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



173
174
175
176
177
178
179
# File 'lib/senkyoshi/canvas_course.rb', line 173

def process_scorm(local: false)
  create_scorm_assignments(
    upload_scorm_packages(@scorm_packages),
    @course_resource.id,
    local,
  )
end

#upload_content(filename) ⇒ Object

Create a migration for the course and upload the imscc file to be imported into the course



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/senkyoshi/canvas_course.rb', line 185

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/senkyoshi/canvas_course.rb', line 132

def upload_scorm_package(scorm_package, course_id, tmp_name)
  zip = scorm_package.write_zip tmp_name
  File.open(zip, "rb") do |file|
    RestClient.post(
      "#{Senkyoshi.configuration.scorm_url}/api/scorm_courses",
      {
        oauth_consumer_key: "scorm-player",
        lms_course_id: course_id,
        file: file,
      },
      SharedAuthorization: Senkyoshi.configuration.scorm_shared_auth,
    ) do |resp|
      result = JSON.parse(resp.body)["response"]
      result["points_possible"] = scorm_package.points_possible
      result
    end
  end
end

#upload_scorm_packages(scorm_packages) ⇒ Object

Uploads all scorm packages to scorm manager specified in senkyoshi.yml config file



164
165
166
167
168
169
170
171
# File 'lib/senkyoshi/canvas_course.rb', line 164

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/senkyoshi/canvas_course.rb', line 205

def upload_to_s3(migration, filename)
  File.open(filename, "rb") do |file|
    # Attach the file to the S3 auth
    pre_attachment = migration.pre_attachment
    upload_url = pre_attachment["upload_url"]
    upload_params = pre_attachment["upload_params"]
    upload_params[:file] = file

    # Post to S3
    RestClient.post(
      upload_url,
      upload_params,
    ) do |response|
      # Post to Canvas
      RestClient.post(
        response.headers[:location],
        nil,
        Authorization: "Bearer #{Senkyoshi.configuration.canvas_token}",
      )
    end
  end
end