Skip to content

Commit 5c8ea0c

Browse files
committed
fix quiz re-importing with unique assignment index
test plan: * create a course with a published quiz * export the course to .imscc file * reimport the file back into the course * should not give an import error * copy the course * import the file into the new course * should not get an import error * create a new course * import the file into the course * copy in from the original course * should not get an import error fixes #CNVS-7249 Change-Id: Ieb5d5cf9de0cb8a1465f2f7e704c5db0cef7098e Reviewed-on: https://gerrit.instructure.com/23010 QA-Review: August Thornton <august@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Jeremy Stanley <jeremy@instructure.com> Product-Review: Bracken Mosbacker <bracken@instructure.com>
1 parent 3dec746 commit 5c8ea0c

5 files changed

Lines changed: 54 additions & 11 deletions

File tree

app/models/assignment.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ def self.process_migration(data, migration)
15201520
end
15211521

15221522

1523-
def self.import_from_migration(hash, context, item=nil)
1523+
def self.import_from_migration(hash, context, item=nil, quiz=nil)
15241524
hash = hash.with_indifferent_access
15251525
return nil if hash[:migration_id] && hash[:assignments_to_import] && !hash[:assignments_to_import][hash[:migration_id]]
15261526
item ||= find_by_context_type_and_context_id_and_id(context.class.to_s, context.id, hash[:id])
@@ -1609,14 +1609,18 @@ def self.import_from_migration(hash, context, item=nil)
16091609
gs = context.grading_standards.find_by_migration_id(hash[:grading_standard_migration_id])
16101610
item.grading_standard = gs if gs
16111611
end
1612-
if hash[:quiz_migration_id]
1613-
if quiz = context.quizzes.find_by_migration_id(hash[:quiz_migration_id])
1614-
# the quiz is published because it has an assignment
1615-
quiz.assignment = item
1616-
quiz.generate_quiz_data
1617-
quiz.published_at = Time.now
1618-
quiz.workflow_state = 'available'
1619-
quiz.save
1612+
if quiz
1613+
item.quiz = quiz
1614+
elsif hash[:quiz_migration_id]
1615+
if q = context.quizzes.find_by_migration_id(hash[:quiz_migration_id])
1616+
if !item.quiz || item.quiz.id == q.id
1617+
# the quiz is published because it has an assignment
1618+
q.assignment = item
1619+
q.generate_quiz_data
1620+
q.published_at = Time.now
1621+
q.workflow_state = 'available'
1622+
q.save
1623+
end
16201624
end
16211625
item.submission_types = 'online_quiz'
16221626
item.saved_by = :quiz

app/models/quiz.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ def self.import_from_migration(hash, context, question_data, item=nil, allow_upd
10671067
item.reload # reload to catch question additions
10681068

10691069
if hash[:assignment] && hash[:available]
1070-
item.assignment = Assignment.import_from_migration(hash[:assignment], context, item.assignment)
1070+
hash[:assignment][:migration_id] += "_#{item.id}" if hash[:assignment][:migration_id]
1071+
item.assignment = Assignment.import_from_migration(hash[:assignment], context, item.assignment, item)
10711072
elsif !item.assignment && grading = hash[:grading]
10721073
# The actual assignment will be created when the quiz is published
10731074
item.quiz_type = 'assignment'

lib/canvas/migration/migrator_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ def prepend_id_to_questions(questions, prepend_value=nil)
194194
def prepend_id_to_assessments(assessments, prepend_value=nil)
195195
assessments.each do |a|
196196
a[:migration_id] = prepend_id(a[:migration_id], prepend_value)
197+
if h = a[:assignment]
198+
h[:migration_id] = prepend_id(h[:migration_id], prepend_value)
199+
end
200+
197201
a[:questions].each do |q|
198202
if q[:question_type] == "question_reference"
199203
q[:migration_id] = prepend_id(q[:migration_id], prepend_value)

spec/lib/cc/importer/canvas_cartridge_converter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def import_learning_outcomes
920920

921921
Quiz.import_from_migration(quiz_hash, @copy_to, {})
922922
q = @copy_to.quizzes.find_by_migration_id("ie3d8f8adfad423eb225229c539cdc450")
923-
a = @copy_to.assignments.find_by_migration_id("i0c012cbae54b972138520466e557f5e4")
923+
a = q.assignment
924924
a.assignment_group.id.should == ag.id
925925
q.assignment_group_id.should == ag.id
926926
end

spec/models/content_migration_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,40 @@ def create_outcome(context, group=nil)
767767
@copy_to.quizzes.find_by_migration_id(mig_id(@quiz)).should_not be_nil
768768
end
769769

770+
it "should create a new assignment if copying a new quiz (even if the assignment migration_id matches)" do
771+
pending unless Qti.qti_enabled?
772+
quiz = @copy_from.quizzes.create!(:title => "new quiz")
773+
quiz2 = @copy_to.quizzes.create!(:title => "already existing quiz")
774+
775+
[quiz, quiz2].each do |q|
776+
q.did_edit
777+
q.offer!
778+
end
779+
780+
a = quiz2.assignment
781+
a.migration_id = mig_id(quiz.assignment)
782+
a.save!
783+
784+
run_course_copy
785+
786+
@copy_to.quizzes.map(&:title).sort.should == ["already existing quiz", "new quiz"]
787+
@copy_to.assignments.map(&:title).sort.should == ["already existing quiz", "new quiz"]
788+
789+
# Re-copying should find and update the old "new" quiz and assignment
790+
@cm = ContentMigration.new(:context => @copy_to, :user => @user, :source_course => @copy_from, :copy_options => {:everything => "1"})
791+
@cm.user = @user
792+
@cm.migration_settings[:import_immediately] = true
793+
@cm.save!
794+
795+
quiz.title = "mwhaha changed"
796+
quiz.save!
797+
798+
run_course_copy
799+
800+
@copy_to.quizzes.map(&:title).sort.should == ["already existing quiz", "mwhaha changed"]
801+
@copy_to.assignments.map(&:title).sort.should == ["already existing quiz", "mwhaha changed"]
802+
end
803+
770804
it "should have correct question count on copied surveys and practive quizzes" do
771805
pending unless Qti.qti_enabled?
772806
sp = @copy_from.quizzes.create!(:title => "survey pub", :quiz_type => "survey")

0 commit comments

Comments
 (0)