Class: Course::LessonPlan::Item

Inherits:
ApplicationRecord show all
Includes:
ItemTodoConcern
Defined in:
app/models/course/lesson_plan/item.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ItemTodoConcern

#can_user_start?, #create_todos, #has_todo?

Class Method Details

.opening_within_next_dayObject

Scopes the lesson plan items to those which are opening in the next 24 hours.


47
48
49
# File 'app/models/course/lesson_plan/item.rb', line 47

scope :opening_within_next_day, (lambda do
  where(start_at: (Time.zone.now)..(1.day.from_now))
end)

.ordered_by_dateObject

Orders the lesson plan items by the starting date.


16
17
18
# File 'app/models/course/lesson_plan/item.rb', line 16

scope :ordered_by_date, (lambda do
  order(:start_at)
end)

.publishedObject

Returns only the lesson plan items that are published.


26
27
28
# File 'app/models/course/lesson_plan/item.rb', line 26

scope :published, (lambda do
  where(published: true)
end)

.upcoming_items_from_course_by_type(course) ⇒ Hash

Finds the lesson plan items which are starting within the next day for a given course. Rearrange the items into a hash keyed by the actable type as a string. For example: { ActableType_1_as_String => [ActableItems...], ActableType_2_as_String => [ActableItems...] }

Parameters:

  • course (Course)

    The course to check for published items starting within the next day.

Returns:

  • (Hash)

64
65
66
67
68
69
70
71
72
73
# File 'app/models/course/lesson_plan/item.rb', line 64

def self.upcoming_items_from_course_by_type(course)
  opening_items = course.lesson_plan_items.published.opening_within_next_day.includes(:actable)
  opening_items_hash = Hash.new { |hash, actable_type| hash[actable_type] = [] }
  opening_items.select { |item| item.actable.include_in_consolidated_email?(:opening) }.
    each do |item|
      opening_items_hash[item.actable_type].push(item.actable)
    end
  # Sort the items for each actable type by start_at time, followed by title.
  opening_items_hash.each_value { |items| items.sort_by! { |item| [item.start_at, item.title] } }
end

.with_actable_typesObject

Scopes the lesson plan items to those which belong to the given actable_types. Each actable type is further scoped to return the IDs of items for display. actable_data is provided to help the actable types figure out what should be displayed.


36
37
38
39
40
41
42
43
# File 'app/models/course/lesson_plan/item.rb', line 36

scope :with_actable_types, lambda { |actable_hash|
  where(
    actable_hash.map do |actable_type, actable_data|
      "course_lesson_plan_items.id IN (#{actable_type.constantize.
      ids_showable_in_lesson_plan(actable_data).to_sql})"
    end.join(' OR ')
  )
}

Instance Method Details

#copy_attributes(other, duplicator) ⇒ Object

Copy attributes for lesson plan item from the object being duplicated. Shift the time related fields.

Parameters:

  • other (Object)

    The source object to copy attributes from.

  • duplicator (Duplicator)

    The Duplicator object


80
81
82
83
84
85
86
87
88
89
# File 'app/models/course/lesson_plan/item.rb', line 80

def copy_attributes(other, duplicator)
  self.title = other.title
  self.description = other.description
  self.published = duplicator.options[:unpublish_all] ? false : other.published
  self.base_exp = other.base_exp
  self.time_bonus_exp = other.time_bonus_exp
  self.start_at = duplicator.time_shift(other.start_at)
  self.bonus_end_at = duplicator.time_shift(other.bonus_end_at) if other.bonus_end_at
  self.end_at = duplicator.time_shift(other.end_at) if other.end_at
end

#self_directed_started?Boolean

Test if the lesson plan item has started for self directed learning.

Returns:

  • (Boolean)

94
95
96
97
98
99
100
# File 'app/models/course/lesson_plan/item.rb', line 94

def self_directed_started?
  if course&.advance_start_at_duration
    start_at.blank? || start_at - course.advance_start_at_duration < Time.zone.now
  else
    started?
  end
end