Class: Canvas::Migration::Worker::SenkyoshiWorker

Inherits:
Base
  • Object
show all
Defined in:
lib/canvas/migration/worker/senkyoshi_worker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enqueue(content_migration) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 56

def self.enqueue(content_migration)
  Delayed::Job.enqueue(
    new(content_migration.id),
    priority: Delayed::LOW_PRIORITY,
    max_attempts: 1,
    strand: content_migration.strand,
  )
end

Instance Method Details

#_import_content(migration, settings, attachment, converter) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 181

def _import_content(migration, settings, attachment, converter)
  migration.import_content

  # Now that the import is done, change the attachment back
  # to the blackboard zip for record keeping
  attachment.write_attribute(
    :filename,
    File.basename(settings[:file_path]),
  )
  attachment.save
  migration.update_import_progress(100)
  saved = migration.save
  if converter.respond_to?(:post_process)
    converter.post_process
  end
  saved
end

#_import_scorm(migration, settings) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 154

def _import_scorm(migration, settings)
  plugin = PluginSetting.find_by(name: "senkyoshi_importer")
  if !plugin.disabled
    Senkyoshi.configure do |config|
      config.scorm_url = plugin.settings[:scorm_url]
      config.scorm_launch_url = plugin.settings[:scorm_launch_url]
      config.scorm_shared_id = plugin.settings[:scorm_shared_id]
      config.scorm_shared_auth = plugin.settings[:scorm_shared_auth]
      config.scorm_oauth_consumer_key =
        plugin.settings[:scorm_oauth_consumer_key]
    end

    meta = {
      name: migration.context.name,
    }
    Zip::File.open(settings[:file_path], "rb") do |bb_zip|
      senkyoshi_course = Senkyoshi::CanvasCourse.new(
        meta,
        migration.context,
        bb_zip,
      )
      senkyoshi_course.process_scorm(local: true)
    end
    migration.update_import_progress(90)
  end
end

#_process_converted_data(migration, overview_file_path, export_folder_path) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 134

def _process_converted_data(migration,
                            overview_file_path,
                            export_folder_path)
  if overview_file_path
    file = File.new(overview_file_path)
    Canvas::Migration::Worker::upload_overview_file(file, migration)
    migration.update_conversion_progress(90)
  end

  if export_folder_path
    Canvas::Migration::Worker::upload_exported_data(
      export_folder_path,
      migration,
    )
    Canvas::Migration::Worker::clear_exported_data(
      export_folder_path,
    )
  end
end

#_process_imscc(migration, settings, imscc_path) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 87

def _process_imscc(migration, settings, imscc_path)
  # Get the attachment for the ContentMigration
  # And change its file to the imscc file temporarily
  attachment = migration.attachment
  attachment.write_attribute(:filename, File.basename(imscc_path))
  attachment.save

  converter_class = settings[:converter_class]
  unless converter_class
    if settings[:no_archive_file]
      raise ArgumentError, ARGUMENT_ERROR
    end
    settings[:archive] = Canvas::Migration::Archive.new(settings)
    converter_class = settings[:archive].get_converter
  end

  converter = converter_class.new(settings)
  course = converter.export
  export_folder_path = course[:export_folder_path]
  overview_file_path = course[:overview_file_path]

  _process_converted_data(
    migration,
    overview_file_path,
    export_folder_path,
  )

  migration.migration_settings[:worker_class] = converter_class.name
  migration.migration_settings[:migration_ids_to_import] =
    { copy: { everything: true } }.merge(
      migration.migration_settings[:migration_ids_to_import] || {},
    )
  if path = converter.course[:files_import_root_path]
    migration.migration_settings[:files_import_root_path] = path
  end

  migration.workflow_state = :exported
  saved = migration.save
  migration.update_conversion_progress(100)

  if migration.import_immediately? && !migration.for_course_copy?
    _import_scorm(migration, settings)
    saved = _import_content(migration, settings, attachment, converter)
  end
  saved
end

#_setup_settings(migration) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 65

def _setup_settings(migration)
  settings = migration.migration_settings.clone
  settings[:content_migration_id] = migration_id
  settings[:user_id] = migration.user_id
  settings[:content_migration] = migration

  if migration.attachment
    settings[:attachment_id] = migration.attachment.id
    settings[:file_path] = migration.attachment.full_filename
  elsif settings[:file_url]
    attachment = Canvas::Migration::Worker.
      download_attachment(migration, settings[:file_url])
    settings[:attachment_id] = attachment.id
    settings[:file_path] = attachment.full_filename
  elsif !settings[:no_archive_file]
    no_migration_file = "File required for content migration."
    text = I18n.t(:no_migration_file, no_migration_file)
    raise Canvas::Migration::Error, text
  end
  settings
end

#performObject



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
# File 'lib/canvas/migration/worker/senkyoshi_worker.rb', line 25

def perform
  migration ||= ContentMigration.where(id: migration_id).first
  migration.job_progress.start unless migration.skip_job_progress

  begin
    migration.update_conversion_progress(1)

    settings = _setup_settings(migration)

    # Create the imscc file using Senkyoshi
    imscc_path = settings[:file_path].ext(".imscc")
    Senkyoshi.parse_and_process_single(settings[:file_path], imscc_path)

    migration.update_conversion_progress(50)

    if File.exists?(imscc_path)
      _process_imscc(migration, settings, imscc_path)
    else
      text = I18n.t(:no_imscc_file, "Imscc file creation failed.")
      raise Canvas::Migration::Error, text
    end
  rescue Canvas::Migration::Error => e
    migration.add_error(e.message, exception: e)
    migration.workflow_state = :failed
    migration.job_progress.fail unless migration.skip_job_progress
    migration.save
  rescue => e
    migration.fail_with_error!(e) if migration
  end
end