summaryrefslogtreecommitdiff
path: root/app/models/conference.rb
blob: 880b958eedeccdd6872ed4cc1a6285af9382656f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Conference < ActiveRecord::Base
  attr_accessible :name, :start, :end, :description, :pin, 
                  :open_for_anybody, :max_members, :announce_new_member_by_name,
                  :announce_left_member_by_name
  
  belongs_to :conferenceable, :polymorphic => true, :touch => true
  has_many :conference_invitees, :dependent => :destroy
  has_many :phone_numbers, :as => :phone_numberable, :dependent => :destroy

  before_validation {
    if !self.pin.blank?
      self.pin = self.pin.to_s.gsub(/[^0-9]/, '')
    end
  }

  validates_presence_of  :conferenceable_type, :conferenceable_id
  validates_presence_of  :conferenceable
  validates_presence_of  :name
  validates_presence_of  :start, :if => Proc.new { |conference| !conference.end.blank? }
  validates_presence_of  :end, :if => Proc.new { |conference| !conference.start.blank? }
  validates_presence_of  :max_members
  validates_numericality_of :max_members, :only_integer => true, 
                                          :greater_than => 0,
                                          :less_than => ((GsParameter.get('MAXIMUM_NUMBER_OF_PEOPLE_IN_A_CONFERENCE').nil? ? 10 : GsParameter.get('MAXIMUM_NUMBER_OF_PEOPLE_IN_A_CONFERENCE')) + 1),
                                          :allow_nil => false,
                                          :allow_blank => false

  validates_inclusion_of :open_for_anybody, :in => [true, false]
  
  validates_length_of       :pin, :minimum => (GsParameter.get('MINIMUM_PIN_LENGTH').nil? ? 4 : GsParameter.get('MINIMUM_PIN_LENGTH')),
                                  :allow_nil => true,
                                  :allow_blank => true
  
  validate :start_and_end_dates_must_make_sense, :if => Proc.new { |conference| !conference.start.blank? && !conference.end.blank? }

  
  before_save :send_pin_email_when_pin_has_changed

  default_scope where(:state => 'active').order(:start)
  
  # State Machine stuff
  state_machine :initial => :active do
  end
  
  def sip_domain
    self.conferenceable.try(:sip_domain)
  end
  
  def to_s
    name
  end

  def list_conference
    require 'freeswitch_event'
    result = FreeswitchAPI.api_result(FreeswitchAPI.api('conference', "conference#{self.id}", 'xml_list'))
    if result =~ /^\<\?xml/
      return Hash.from_xml(result)
    end
    return nil
  end

  def list_members
    data = self.list_conference
    if data.blank?
      return {}
    end

    return data.fetch('conferences',{}).fetch('conference',{}).fetch('members',{}).fetch('member',{})
  end
  

  private
  def start_and_end_dates_must_make_sense
    errors.add(:end, 'must be later than the start') if self.end < self.start
  end

  def send_pin_email_when_pin_has_changed
    if !self.new_record? && self.conferenceable.class == User && self.pin_changed?
      Notifications.new_pin(self).deliver
    end
  end

end