class TutoringSession

Schema Information

Table name: tutoring_sessions

id                  :integer          not null, primary key
tutor_id         :integer
user_id             :integer
date                :date
created_at          :datetime
updated_at          :datetime
status              :string(1)
length_out_schedule :integer
student_accepted   :boolean          default(FALSE)
occured             :boolean
end_time            :datetime
topic               :string(255)
location            :string(255)
resource_id         :integer
old_session         :boolean          default(FALSE)
category_id         :integer
rate_in_cents       :integer          not null
on_site             :boolean          default(FALSE)
start_time          :datetime
university_id          :integer
session_type        :integer          default(0)
resource_type       :string(255)

Constants

TYPES

tracked

Attributes

end[RW]

Attribute accessor for tutoring session.

message[RW]

Attribute accessor for tutoring session.

start[RW]

Attribute accessor for tutoring session.

Public Instance Methods

audit_update() click to toggle source

used to logs all changes in model.

# File app/models/tutoring_session.rb, line 126
def audit_update
  unless (changes = audited_changes).empty? && audit_comment.blank?
    action = if changes['status']
      if [nil, 'c'] == changes['status']
        'confirmed'
      elsif [nil, 'r'] == changes['status'] or ['c', 'r'] == changes['status']
        'rejected'
      else
        'updated'
      end
    else
      'updated'
    end
    write_audit(:action => action, :audited_changes => changes,
      :comment => audit_comment)
  end
end
can_be_confirmed_by?(user) click to toggle source

Checks whether the session is confirmed by specified user or not.

# File app/models/tutoring_session.rb, line 410
def can_be_confirmed_by?(user)
  self.tutor_id == user.id and status.nil?
end
confirm!(msg = nil) click to toggle source

To confirm the tutoring session and charged the user for requesting the session.

# File app/models/tutoring_session.rb, line 236
def confirm!(msg = nil)
  TutoringSession.transaction do
    self.lock!
    return true if self.reload.confirmed?
    if video_session? or Payment.populate_credits self
      self.status = 'c'
      self.message = msg if msg.present?
      (self.invoice || find_or_create_invoice!).update_attributes({charged: true})
      self.save(:validate => false)
      self.create_activity key: 'tutoring_session.confirm', owner: tutor, recipient: user, type: 'Notification'
      unless video_session?
        TutoringSessionsMailer.delay(queue: :mailer).confirmed_email_tutor(self)
      end
      return true
    else
      return false
    end
  end
end
confirmed?() click to toggle source

return true if tutoring session is confirmed by tutor

# File app/models/tutoring_session.rb, line 231
def confirmed?
  self.status == 'c'
end
cost_in_cents() click to toggle source

Returns cost of session in cents

# File app/models/tutoring_session.rb, line 366
def cost_in_cents
  (length / 60.0) * rate_in_cents
end
create_session_message() click to toggle source

Create message based on status of the tutoring session

# File app/models/tutoring_session.rb, line 165
def create_session_message
  unless self.message.blank?
    if self.new_record?
      session_message = self.build_create_message
    else
      if self.changes.has_key?(:status) and (self.status == 'c' or self.status == 'r')
        if self.status == 'c'
          session_message = self.build_confirmed_message
        elsif self.status == 'r'
          session_message = self.build_rejected_message
        end
      else
        session_message = self.update_messages.build
      end
    end
    session_message.message = self.message
    session_message.created_by_id = User.current.id
    session_message.created_by_type = User.current.type
  end
  return true
end
create_session_notification() click to toggle source

Create notification if tutoring session is not a video session

# File app/models/tutoring_session.rb, line 212
def create_session_notification
  unless video_session?
    unless invoice and invoice.manually_billed?
      self.create_activity key: 'tutoring_session.create', owner: student, recipient: tutor, type: 'Notification'
    end
  end
end
created_by() click to toggle source

Returns user who created the session.

# File app/models/tutoring_session.rb, line 390
def created_by
  user
end
created_by_student?() click to toggle source

Checks whether the session is created by student.

# File app/models/tutoring_session.rb, line 385
def created_by_student?
  created_by == 'Student'
end
created_for() click to toggle source

Returns tutor of tutoring session.

# File app/models/tutoring_session.rb, line 405
def created_for
  tutor
end
student() click to toggle source

Returns the user of tutoring session.

# File app/models/tutoring_session.rb, line 155
def student
  user
end
editable_by?(user) click to toggle source

Checks whether the session is editable by specified user or not.

# File app/models/tutoring_session.rb, line 415
def editable_by?(user)
  self.user.id == user.id and status.nil?
end
end_time() click to toggle source

Returns end time.

# File app/models/tutoring_session.rb, line 428
def end_time
  unless self[:end_time]
    initialize_start_time_and_end_time if self.end
  end
  self[:end_time]
end
find_or_build_invoice() click to toggle source

To find/build the invoice.

# File app/models/tutoring_session.rb, line 448
def find_or_build_invoice
  invoice || build_invoice(user_id: user_id, tutor_id: tutor_id)
end
find_or_create_invoice!() click to toggle source

To find and create the invoice.

# File app/models/tutoring_session.rb, line 453
def find_or_create_invoice!
  new_invoice = invoice || build_invoice(user_id: user_id, tutor_id: tutor_id)
  new_invoice.assign_payable_amount
  new_invoice.save!
  new_invoice
end
find_or_create_video_session_room() click to toggle source
# File app/models/tutoring_session.rb, line 296
def find_or_create_video_session_room
  video_session_room || build_video_session_room.save!
  video_session_room
end
tutor_rate() click to toggle source

Returns tutor rate for tutoring session.

# File app/models/tutoring_session.rb, line 356
def tutor_rate
  self.tutor.rate_in_cents_with_charge
end
for_appointment_sidebar(current_user = User.current, recipient = User.current) click to toggle source

Returns json object containing information for appointment sidebar for session.

# File app/models/tutoring_session.rb, line 466
def for_appointment_sidebar(current_user = User.current, recipient = User.current)
  {
    id: id,
    start_time: start_time,
    end_time: end_time,
    other_user_id: other_user(recipient).id,
    other_user_name: other_user(recipient).name,
    status: status_label,
    confirmable: can_be_confirmed_by?(recipient),
    editable: editable_by?(recipient)
  }
end
initialize_start_time_and_end_time() click to toggle source

To initialize start time and end time.

# File app/models/tutoring_session.rb, line 436
def initialize_start_time_and_end_time
  [:start, :end].each do |type|
    self.send("#{type}_time=".to_sym, Time.zone.parse("#{date.to_s(:db)} #{self.send(type)}")) if self.send(type)
  end
end
last_message() click to toggle source

Returns last message

# File app/models/tutoring_session.rb, line 322
def last_message
  messages.last.try(:message)
end
length() click to toggle source

length of session in minutes

# File app/models/tutoring_session.rb, line 160
def length
  ((end_time - start_time)/60.0).ceil rescue 0
end
ongoing_session?() click to toggle source

Checks for ongoing session.

# File app/models/tutoring_session.rb, line 145
def ongoing_session?
  session_type == 1 and end_time.nil?
end
other_user(current_user = User.current) click to toggle source

Returns other user of session except current user.

# File app/models/tutoring_session.rb, line 461
def other_user(current_user = User.current)
  ([tutor, user] - [current_user]).first
end
paid?() click to toggle source

Checks whether the session is paid.

passed?() click to toggle source

Checks whether the session is passed.

# File app/models/tutoring_session.rb, line 262
def passed?
  end_time < Time.now
end
passed_and_paid?() click to toggle source

Checks whether the session is passed(i.e. completed) and paid.

# File app/models/tutoring_session.rb, line 272
def passed_and_paid?
  confirmed? and passed? and paid?
end
passed_but_not_paid?() click to toggle source

Checks whether the session is passed(i.e. completed) but not paid.

# File app/models/tutoring_session.rb, line 267
def passed_but_not_paid?
  confirmed? and passed? and !paid?
end
pending?() click to toggle source

Returns true if tutoring session pending

# File app/models/tutoring_session.rb, line 327
def pending?
  self.status.nil?
end
rate_in_cents() click to toggle source

To find rate of tutor in cents

# File app/models/tutoring_session.rb, line 443
def rate_in_cents
  self[:rate_in_cents] || tutor.rate_in_cents_with_charge
end
reject!(message = nil , current_user) click to toggle source

To reject the tutoring session and refund amount to the user who sent request for session.

# File app/models/tutoring_session.rb, line 302
def reject!(message = nil , current_user)
  return false if self.rejected?
  self.status = 'r'
  self.message = message
  if self.invoice.present? and !self.invoice.paid?
    @refundable_amount = self.invoice.total_amount.cents
    self.user.account_balance.amount_cents += @refundable_amount
    self.user.account_balance.save
    self.invoice.update_tutor_billed_length
  end
  if self.save(validate: false)
    TutoringSessionsMailer.delay(queue: :mailer).rejected_email(self, @refundable_amount,current_user)
    self.create_activity key: 'tutoring_session.reject', owner: tutor, recipient: user, type: 'Notification'
    return true
  else
    false
  end
end
rejected?() click to toggle source

return true if tutoring session is rejected by tutor

# File app/models/tutoring_session.rb, line 257
def rejected?
  self.status == 'r'
end
send_tutoring_session_update_mail() click to toggle source

Send email if user updates the tutoring session

# File app/models/tutoring_session.rb, line 221
def send_tutoring_session_update_mail
  to, from = updated_by_user? ? [tutor, user] : [user, tutor]
  TutoringSessionsMailer.delay(queue: :mailer).updated_email(self, to, from)
  self.create_activity key: 'tutoring_session.update', owner: user, recipient: tutor, type: 'Notification'
end
session_length_hours() click to toggle source

Returns session duration in hours.

# File app/models/tutoring_session.rb, line 361
def session_length_hours
  (length / 60.0).round 2
end
session_rate() click to toggle source

Return tutoring session rate.

# File app/models/tutoring_session.rb, line 351
def session_rate
  (self.tutor.rate_in_cents / 60.0) * length
end
set_rate_in_cents() click to toggle source

Sets tutoring session's rate according to tutor's rate.

# File app/models/tutoring_session.rb, line 150
def set_rate_in_cents
  self.rate_in_cents = self.tutor.rate_in_cents_with_charge
end
start_time() click to toggle source

Returns start time.

# File app/models/tutoring_session.rb, line 420
def start_time
  unless self[:start_time]
    initialize_start_time_and_end_time if self.start
  end
  self[:start_time]
end
status_label() click to toggle source

Returns status of tutoring sessions

# File app/models/tutoring_session.rb, line 337
def status_label
  return "pending" if self.pending?
  return "rejected" if self.rejected?
  if confirmed? and passed?
    return 'passed'
  else
    return 'confirmed'
  end
end
total_amount() click to toggle source

Returns money object for total amount with default currency.

# File app/models/tutoring_session.rb, line 371
def total_amount
  Money.new(cost_in_cents, Money.default_currency)
end
total_payable_amount() click to toggle source

Returns total payable amount to tutor for the tutoring session

# File app/models/tutoring_session.rb, line 376
def total_payable_amount
  if cost_in_cents > self.user.total_balance.cents
    ((cost_in_cents * 1.029) + (30)).round(0)
  else
    cost_in_cents
  end
end
upcoming?() click to toggle source

Returns true if tutoring session is an upcoming session.

# File app/models/tutoring_session.rb, line 332
def upcoming?
  end_time and end_time > Time.now
end
updated_by_tutor?() click to toggle source

Checks whether tutoring session is updated by tutor.

# File app/models/tutoring_session.rb, line 400
def updated_by_tutor?
  !updated_by_user?
end
updated_by_user?() click to toggle source

Checks whether tutoring session is updated by user.

# File app/models/tutoring_session.rb, line 395
def updated_by_user?
  User.current and User.current == user
end
validate_end_time() click to toggle source

Ensure length must be greater than start time when try to save a tutoring session

# File app/models/tutoring_session.rb, line 200
def validate_end_time
  unless video_session?
    unless invoice and invoice.manually_billed?
      if self.end_time <= self.start_time
        self.errors.add :end, 'must be greater than Start Time'
        return false
      end
    end
  end
end
validate_start_time() click to toggle source

Ensure length in the future when try to save a tutoring_session

# File app/models/tutoring_session.rb, line 188
def validate_start_time
  unless video_session?
    unless invoice and invoice.manually_billed?
      if self.start_time.nil? || self.start_time < Time.now
        self.errors.add :start, 'must be in the future'
        return false
      end
    end
  end
end
video_session?() click to toggle source

returns true if tutoring session is a video session.

# File app/models/tutoring_session.rb, line 282
def video_session?
  session_type == TYPES.assoc('Video').last
end
video_session_available?() click to toggle source

Checks if video session is available.

# File app/models/tutoring_session.rb, line 292
def video_session_available?
  video_session? and !video_session_closed?
end
video_session_closed?() click to toggle source

Checks if session is closed.

# File app/models/tutoring_session.rb, line 287
def video_session_closed?
  video_session? and video_session_room and video_session_room.closed?
end