Class: CourseEnrollment

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable, Models::LiquidMethods
Defined in:
app/models/course_enrollment.rb

Overview

== Schema Information

Table name: course_enrollments
Database name: primary

id :bigint not null, primary key
completion_date :datetime
enrollment_date :datetime
passed_email_reminder_sent :boolean default(FALSE)
passed_email_reminder_sent_at :datetime
passed_email_sent :boolean default(FALSE)
passed_email_sent_at :datetime
reminder_1 :datetime
reminder_2 :datetime
reminder_3 :datetime
reminder_4 :datetime
state :string not null
created_at :datetime not null
updated_at :datetime not null
course_id :integer not null
party_id :integer not null

Indexes

index_course_enrollments_on_course_id (course_id)

Foreign Keys

fk_rails_... (course_id => courses.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Has one collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.activeActiveRecord::Relation<CourseEnrollment>

A relation of CourseEnrollments that are active. Active Record Scope

Returns:

See Also:



44
# File 'app/models/course_enrollment.rb', line 44

scope :active, -> { where(state: 'active') }

.for_courseActiveRecord::Relation<CourseEnrollment>

A relation of CourseEnrollments that are for course. Active Record Scope

Returns:

See Also:



45
# File 'app/models/course_enrollment.rb', line 45

scope :for_course, -> (c){ where(course_id: c.id) }

.ready_to_certificateActiveRecord::Relation<CourseEnrollment>

A relation of CourseEnrollments that are ready to certificate. Active Record Scope

Returns:

See Also:



46
# File 'app/models/course_enrollment.rb', line 46

scope :ready_to_certificate, -> { left_outer_joins(:certification).where(state: :completed) }

Instance Method Details



120
121
122
# File 'app/models/course_enrollment.rb', line 120

def certificate_form_link
  url = "#{WEB_URL}/my_account/course_enrollments/#{self.id}/certifications/new"
end

#certificate_not_requested?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/course_enrollment.rb', line 124

def certificate_not_requested?
  return true unless certification.present?
end

#certificationCertification



40
# File 'app/models/course_enrollment.rb', line 40

has_one :certification

#completed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/course_enrollment.rb', line 77

def completed?
  state == 'completed'
end

#content_completed?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/course_enrollment.rb', line 90

def content_completed?
  progress == 100
end

#courseCourse

Returns:

See Also:



35
# File 'app/models/course_enrollment.rb', line 35

belongs_to :course, optional: true

#course_categoriesActiveRecord::Relation<CourseCategory>

Returns:

See Also:



38
# File 'app/models/course_enrollment.rb', line 38

has_many :course_categories, dependent: :delete_all

#course_examsActiveRecord::Relation<CourseExam>

Returns:

See Also:



37
# File 'app/models/course_enrollment.rb', line 37

has_many :course_exams, dependent: :delete_all

#course_titleObject



98
99
100
# File 'app/models/course_enrollment.rb', line 98

def course_title
  course.name
end

#customer_topicsActiveRecord::Relation<CustomerTopic>

Returns:

See Also:



39
# File 'app/models/course_enrollment.rb', line 39

has_many :customer_topics

#notify_failed_examObject



136
137
138
# File 'app/models/course_enrollment.rb', line 136

def notify_failed_exam
  InternalMailer.course_enrollment_exam(self, false).deliver_later
end

#notify_new_enrollmentObject



128
129
130
# File 'app/models/course_enrollment.rb', line 128

def notify_new_enrollment
  InternalMailer.new_course_enrollment(self).deliver_later
end

#notify_successful_examObject



132
133
134
# File 'app/models/course_enrollment.rb', line 132

def notify_successful_exam
  InternalMailer.course_enrollment_exam(self, true).deliver_later
end

#partyParty

Returns:

See Also:



36
# File 'app/models/course_enrollment.rb', line 36

belongs_to :party, optional: true, inverse_of: :course_enrollments

#passed?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/course_enrollment.rb', line 94

def passed?
  self.state == 'completed' && self.course_exams.where(state: 'passed').present?
end

#percentage_progressObject



73
74
75
# File 'app/models/course_enrollment.rb', line 73

def percentage_progress
  "#{progress}%" if customer_topics.present?
end

#progressObject



69
70
71
# File 'app/models/course_enrollment.rb', line 69

def progress
  (customer_topics.completed.size * 100) / customer_topics.size if customer_topics.present?
end

#send_new_certificate_reminder_emailObject



111
112
113
114
115
116
117
118
# File 'app/models/course_enrollment.rb', line 111

def send_new_certificate_reminder_email
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'NEW_CERT_REMINDER').create
  self.update(passed_email_reminder_sent: true, passed_email_reminder_sent_at: Time.current)
end

#send_passed_emailObject



102
103
104
105
106
107
108
109
# File 'app/models/course_enrollment.rb', line 102

def send_passed_email
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'COURSE_EXAM_PASSED').create
  self.update(passed_email_sent: true, passed_email_sent_at: Time.current)
end

#send_progress_reminder(days) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/course_enrollment.rb', line 140

def send_progress_reminder(days)
  customer = self.party
  com = CommunicationBuilder.new(resource: self,
                                  recipient_party: customer,
                                  use_best_email: true,
                                  template_system_code: 'CERT_PROGRESS').create
  case days
  when 30
    self.update_columns(reminder_1: Time.current, reminder_2: nil, reminder_3: nil, reminder_4: nil)
  when 60
    self.update_columns(reminder_2: Time.current, reminder_3: nil, reminder_4: nil)
  when 90
    self.update_columns(reminder_3: Time.current, reminder_4: nil)
  when 120
    self.update_columns(reminder_4: Time.current)
  end
end

#statusObject



81
82
83
84
85
86
87
88
# File 'app/models/course_enrollment.rb', line 81

def status
  if completed?
    "Completed"
  else
    "Ongoing - #{percentage_progress}"
  end
  #There could also be state "renewed" or "expired"
end