Class: Effective::LearndashEnrollment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/learndash_enrollment.rb

Constant Summary collapse

PROGRESS_STATUSES =
['not-started', 'in-progress', 'completed']

Instance Method Summary collapse

Instance Method Details

#assign_api_attributes(data = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/effective/learndash_enrollment.rb', line 70

def assign_api_attributes(data = nil)
  data ||= EffectiveLearndash.api.find_enrollment(self) || EffectiveLearndash.api.create_enrollment(self)

  assign_attributes(
    last_synced_at: Time.zone.now,
    progress_status: data[:progress_status],
    last_step: data[:last_step],
    steps_completed: data[:steps_completed],
    steps_total: data[:steps_total],
    date_started: Time.use_zone('UTC') { Time.zone.parse(data[:date_started]) },
    date_completed: (Time.use_zone('UTC') { Time.zone.parse(data[:date_completed]) } if data[:date_completed].present?)
  )
end

#completed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/effective/learndash_enrollment.rb', line 57

def completed?
  progress_status == 'completed'
end

#in_progress?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/effective/learndash_enrollment.rb', line 53

def in_progress?
  progress_status == 'in-progress'
end

#not_started?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/effective/learndash_enrollment.rb', line 49

def not_started?
  progress_status == 'not-started'
end

#refresh!(force: false) ⇒ Object



61
62
63
64
65
66
67
68
# File 'app/models/effective/learndash_enrollment.rb', line 61

def refresh!(force: false)
  unless force
    return if last_synced_at.present? && (Time.zone.now - last_synced_at) < 5
  end

  assign_api_attributes
  save!
end

#to_sObject



45
46
47
# File 'app/models/effective/learndash_enrollment.rb', line 45

def to_s
  persisted? ? "#{learndash_user} #{learndash_course}" : 'learndash enrollment'
end