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



32
33
34
35
36
# File 'lib/senkyoshi/canvas_course.rb', line 32

def initialize(, course_resource, blackboard_export)
  @metadata = 
  @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.



26
27
28
# File 'lib/senkyoshi/canvas_course.rb', line 26

def scorm_packages
  @scorm_packages
end

Class Method Details

.clientObject

Create a new pandarus instance to communicate with the canvas server



56
57
58
59
60
61
# File 'lib/senkyoshi/canvas_course.rb', line 56

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/senkyoshi/canvas_course.rb', line 66

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/senkyoshi/canvas_course.rb', line 42

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/senkyoshi/canvas_course.rb', line 122

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/senkyoshi/canvas_course.rb', line 99

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



92
93
94
# File 'lib/senkyoshi/canvas_course.rb', line 92

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



81
82
83
84
85
86
87
# File 'lib/senkyoshi/canvas_course.rb', line 81

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



170
171
172
173
174
# File 'lib/senkyoshi/canvas_course.rb', line 170

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



189
190
191
192
193
194
195
# File 'lib/senkyoshi/canvas_course.rb', line 189

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/senkyoshi/canvas_course.rb', line 201

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/senkyoshi/canvas_course.rb', line 148

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



180
181
182
183
184
185
186
187
# File 'lib/senkyoshi/canvas_course.rb', line 180

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/senkyoshi/canvas_course.rb', line 221

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::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