From 47601e1e4ed43daedcfea923adbc0c2e5d66296c Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Wed, 27 Apr 2011 12:34:01 -0600 Subject: [PATCH] copy/import module links correctly. fixes #4312 Change-Id: I98ecc6bb63252269bf789320a8a5b2de5612efda Reviewed-on: https://gerrit.instructure.com/3308 Tested-by: Hudson Reviewed-by: Brian Palmer --- app/models/assignment.rb | 11 ----------- app/models/attachment.rb | 11 ----------- app/models/calendar_event.rb | 11 ----------- app/models/course.rb | 17 +++++++++++++++-- app/models/quiz.rb | 11 ----------- 5 files changed, 15 insertions(+), 46 deletions(-) diff --git a/app/models/assignment.rb b/app/models/assignment.rb index 0d67849714d..ab581e41a49 100644 --- a/app/models/assignment.rb +++ b/app/models/assignment.rb @@ -1450,17 +1450,6 @@ class Assignment < ActiveRecord::Base item end - def self.find_or_create_for_new_context(new_context, old_context, old_id) - res = new_context.assignments.active.find_by_cloned_item_id(old_context.assignments.find_by_id(old_id).cloned_item_id || 0) rescue nil - res = nil if res && !res.cloned_item_id - if !res - old = old_context.assignments.active.find_by_id(old_id) - res = old.clone_for(new_context) if old - res.save if res - end - res - end - def expects_submission? submission_types && submission_types.strip != "" && submission_types != "none" && submission_types != 'not_graded' && submission_types != "on_paper" end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 045b8ded612..e0bf27ab107 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -609,17 +609,6 @@ class Attachment < ActiveRecord::Base "application/x-shockwave-flash" => "flash" }[content_type] || "file" end - - def self.find_or_create_for_new_context(new_context, old_context, old_id) - res = new_context.attachments.active.find_by_cloned_item_id(old_context.attachments.find_by_id(old_id).cloned_item_id || 0) rescue nil - res = nil if res && !res.cloned_item_id - if !res - old = old_context.attachments.active.find_by_id(old_id) - res = old.clone_for(new_context) if old - res.save if res - end - res - end set_policy do given { |user, session| self.cached_context_grants_right?(user, session, :manage_files) } #admins.include? user } diff --git a/app/models/calendar_event.rb b/app/models/calendar_event.rb index 6ab8fa68fb4..cbe6071d3d8 100644 --- a/app/models/calendar_event.rb +++ b/app/models/calendar_event.rb @@ -276,17 +276,6 @@ class CalendarEvent < ActiveRecord::Base end item end - - def self.find_or_create_for_new_context(new_context, old_context, old_id) - res = new_context.calendar_events.active.find_by_cloned_item_id(old_context.calendar_events.find_by_id(old_id).cloned_item_id || 0) rescue nil - res = nil if res && !res.cloned_item_id - if !res - old = old_context.calendar_events.active.find_by_id(old_id) - res = old.clone_for(new_context) if old - res.save if res - end - res - end def self.max_visible_calendars 10 diff --git a/app/models/course.rb b/app/models/course.rb index 767a8675043..00dd01e6f88 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1034,6 +1034,18 @@ class Course < ActiveRecord::Base def turnitin_enabled? !!self.turnitin_settings end + + def self.find_or_create_for_new_context(obj_class, new_context, old_context, old_id) + association_name = obj_class.table_name + old_item = old_context.send(association_name).find_by_id(old_id) + res = new_context.send(association_name).active.find_by_cloned_item_id(old_item.cloned_item_id) if old_item + if !res + old_item = old_context.send(association_name).active.find_by_id(old_id) + res = old_item.clone_for(new_context) if old_item + res.save if res + end + res + end def self.migrate_content_links(html, from_context, to_context, supported_types=nil, user_to_check_for_permission=nil) return html unless from_context @@ -1058,7 +1070,8 @@ class Course < ActiveRecord::Base 'files' => Attachment, 'conferences' => WebConference, 'quizzes' => Quiz, - 'groups' => Group + 'groups' => Group, + 'modules' => ContextModule }.each do |type, obj_class| sub_regex = Regexp.new("#{type}/(\\d+)[^\\s]*$") is_sub_item ||= sub_spot.match(sub_regex) @@ -1076,7 +1089,7 @@ class Course < ActiveRecord::Base end end if !new_id && allow_migrate_content && to_context != from_context - new_obj = obj_class.find_or_create_for_new_context(to_context, from_context, item[1]) rescue nil + new_obj = self.find_or_create_for_new_context(obj_class, to_context, from_context, item[1]) new_id ||= new_obj.id if new_obj end if !limit_migrations_to_listed_types || new_id diff --git a/app/models/quiz.rb b/app/models/quiz.rb index 06f3571b2dd..de358544ea3 100644 --- a/app/models/quiz.rb +++ b/app/models/quiz.rb @@ -711,17 +711,6 @@ class Quiz < ActiveRecord::Base dup end - def self.find_or_create_for_new_context(new_context, old_context, old_id) - res = new_context.quizzes.active.find_by_cloned_item_id(old_context.quizzes.find_by_id(old_id).cloned_item_id || 0) rescue nil - res = nil if res && !res.cloned_item_id - if !res - old = old_context.quizzes.active.find_by_id(old_id) - res = old.clone_for(new_context) if old - res.save if res - end - res - end - def statistics_csv(options={}) options ||= {} columns = []