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)
tracked
Attribute accessor for tutoring session.
Attribute accessor for tutoring session.
Attribute accessor for tutoring session.
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
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
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
return true if tutoring session is confirmed by tutor
# File app/models/tutoring_session.rb, line 231 def confirmed? self.status == 'c' end
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 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 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
Returns user who created the session.
# File app/models/tutoring_session.rb, line 390 def created_by user end
Checks whether the session is created by student.
# File app/models/tutoring_session.rb, line 385 def created_by_student? created_by == 'Student' end
Returns tutor of tutoring session.
# File app/models/tutoring_session.rb, line 405 def created_for tutor end
Returns the user of tutoring session.
# File app/models/tutoring_session.rb, line 155 def student user end
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
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
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
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
# 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
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
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
Returns last message
# File app/models/tutoring_session.rb, line 322 def last_message messages.last.try(:message) end
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
Checks for ongoing session.
# File app/models/tutoring_session.rb, line 145 def ongoing_session? session_type == 1 and end_time.nil? end
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
Checks whether the session is paid.
# File app/models/tutoring_session.rb, line 277 def paid? invoice and invoice.paid? end
Checks whether the session is passed.
# File app/models/tutoring_session.rb, line 262 def passed? end_time < Time.now end
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
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
Returns true if tutoring session pending
# File app/models/tutoring_session.rb, line 327 def pending? self.status.nil? end
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
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
return true if tutoring session is rejected by tutor
# File app/models/tutoring_session.rb, line 257 def rejected? self.status == 'r' end
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
Returns session duration in hours.
# File app/models/tutoring_session.rb, line 361 def session_length_hours (length / 60.0).round 2 end
Return tutoring session rate.
# File app/models/tutoring_session.rb, line 351 def session_rate (self.tutor.rate_in_cents / 60.0) * length end
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
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
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
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
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
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
Checks whether tutoring session is updated by tutor.
# File app/models/tutoring_session.rb, line 400 def updated_by_tutor? !updated_by_user? end
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
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
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
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
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
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