Class: Course::Duplication::CourseDuplicationService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/course/duplication/course_duplication_service.rb

Overview

Service to provide a full duplication of a Course.

Constant Summary collapse

DEFAULT_COURSE_DUPLICATION_OPTIONS =
{ mode: :course, new_title: 'Duplicated', current_user: User.system }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.duplicate_course(source_course, options = {}, all_objects = [], selected_objects = []) ⇒ Course

Constructor for the course duplication service.

Parameters:

  • source_course (Course)

    The course to duplicate.

  • options (Hash) (defaults to: {})

    The options to be sent to the Duplicator object.

  • all_objects (Array) (defaults to: [])

    All the objects in the course.

  • selected_objects (Array) (defaults to: [])

    The objects to duplicate.

Options Hash (options):

  • :current_user (User) — default: +User.system+

    The user triggering the duplication.

  • :new_title (String) — default: 'Duplicated'

    The title for the duplicated course.

  • :new_start_at (DateTime)

    Start date and time for the duplicated course.

Returns:

  • (Course)

    The duplicated course


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/course/duplication/course_duplication_service.rb', line 16

def duplicate_course(source_course, options = {}, all_objects = [], selected_objects = [])
  excluded_objects = all_objects - selected_objects
  options[:excluded_objects] = excluded_objects
  options[:source_course] = source_course
  options[:time_shift] =
    if options[:new_start_at]
      Time.zone.parse(options[:new_start_at]) - source_course.start_at
    else
      0
    end
  options.reverse_merge!(DEFAULT_COURSE_DUPLICATION_OPTIONS)
  service = new(options)
  service.duplicate_course(source_course)
end

Instance Method Details

#duplicate_course(source_course) ⇒ Course

Duplicate the course with the duplicator. Do not just pass in @selected_objects or object parents could be set incorrectly.

Returns:

  • (Course)

    The duplicated course


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/course/duplication/course_duplication_service.rb', line 39

def duplicate_course(source_course)
  duplicated_course = Course.transaction do
    new_course = duplicator.duplicate(source_course)
    raise ActiveRecord::Rollback unless new_course.save
    duplicator.set_option(:destination_course, new_course)

    source_course.duplication_manifest.each do |item|
      raise ActiveRecord::Rollback unless duplicator.duplicate(item).save
    end
    raise ActiveRecord::Rollback unless update_course_settings(duplicator, new_course, source_course)
    raise ActiveRecord::Rollback unless update_sidebar_settings(duplicator, new_course, source_course)
    new_course
  end
  notify_duplication_complete(duplicated_course) unless duplicated_course.nil?
  duplicated_course
end