Class: Poll

Inherits:
ApplicationRecord show all
Defined in:
app/models/poll.rb

Overview

the poll model

Instance Method Summary collapse

Instance Method Details

#consider_all_pending_surveys_sent_onceObject

consider all surveys to have been sent once
Use this method after upgrading old colibri versions (without customer email tracking)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/poll.rb', line 199

def consider_all_pending_surveys_sent_once
  log="going to fix all pending surveys without metadatas so that they can be considered to have been sent once...\n"
  metas={}
  metas["sent"]=1
  surveys=Survey.where(poll_id: self.id)
  surveys.each do |s|
    unless s.metas
      s.metas=ActiveSupport::JSON.encode(metas)
      unless s.save
        log="#{log} -> survey #{s.id} updating metadatas failed\n"
      else
        log="#{log} -> survey #{s.id} metadatas correctly fixed to #{metas} \n"
      end
    else
      log="#{log} -> survey #{s.id} has already some metadatas: #{s.metas} \n"
    end
  end
  log
end

#count_sent_surveys(time_start = nil, time_end = nil, groups = nil) ⇒ Object

Returns, for the active poll, the number of surveys delivered to clients
possibility to define a date range and a text indication (groups) to filter on a group of users time_start and time_end should be in format AAAA-MM-DD 00:00:00



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/poll.rb', line 126

def count_sent_surveys(time_start=nil, time_end=nil, groups=nil)
  puts("BEGIN________________________________count_sent_surveys method poll model")
  # shares to a TEAM email do not count 
  if ENV['TEAM']
    domain=ENV.fetch('TEAM')
  else
    domain="cerema.fr"
  end
  sf_req=[]
  sat_req=[]
  sur_req=[]
  sf_expr=[]
  sat_expr=[]
  sur_expr=[]
  #shared_folders basics
  sf_req[0]=""
  sf_expr.push("folders.poll_id = ?")
  sf_req.push(self.id)
  sf_expr.push("shared_folders.share_email not like ?")
  sf_req.push("%#{domain}%")
  #satisfactions basics
  sat_req[0]=""
  sat_expr.push("satisfactions.folder_id < ?")
  sat_req.push(0)
  #surveys basics
  sur_req[0]=""
  #complements :-)
  if Validations.date_reg_exp.match(time_start) && Validations.date_reg_exp.match(time_end)
    time_start = Validations.date_reg_exp.match(time_start)[0]
    time_end = Validations.date_reg_exp.match(time_end)[0]
    sf_expr.push("shared_folders.created_at BETWEEN ? and ?")
    sf_req.push(time_start)
    sf_req.push(time_end)
    sat_expr.push("satisfactions.created_at BETWEEN ? and ?")
    sat_req.push(time_start)
    sat_req.push(time_end)
    sur_expr.push("surveys.created_at BETWEEN ? and ?")
    sur_req.push(time_start)
    sur_req.push(time_end)
  end
  if groups
    unless groups.include?("!")
      sf_expr.push("users.groups like ?")
      sf_req.push("%#{groups}%")
      sat_expr.push("users.groups like ?")
      sat_req.push("%#{groups}%")
      sur_expr.push("users.groups like ?")
      sur_req.push("%#{groups}%")
    else
      sf_expr.push("(users.groups is null or users.groups not like ?)")
      sf_req.push("%#{groups.gsub("!","")}%")
      sat_expr.push("(users.groups is null or users.groups not like ?)")
      sat_req.push("%#{groups.gsub("!","")}%")
      sur_expr.push("(users.groups is null or users.groups not like ?)")
      sur_req.push("%#{groups.gsub("!","")}%")
    end
  end
  sf_req[0]=sf_expr.join(" and ")
  sat_req[0]=sat_expr.join(" and ")
  sur_req[0]=sur_expr.join(" and ")
  puts("********************************************************")
  nb1=SharedFolder.joins(:folder).joins(:user).where(sf_req).count
  nb2=self.satisfactions.joins(:user).where(sat_req).count
  nb3=self.surveys.joins(:user).where(sur_req).count
  puts("satis launched in the folders/files system:#{nb1} satis collected out of the folders/files system:#{nb2} pending surveys #{nb3}")
  nb=nb1+nb2+nb3
  puts("END__________________________________count_sent_surveys method poll model")
  nb
end

#csv(satisfactions = nil) ⇒ Object

generate csv file for a list of feedbacks please note the satisfactions active records must be done with a jointure on the user table to get the email of the user owner



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/poll.rb', line 64

def csv(satisfactions=nil)
  unless satisfactions
    satisfactions = self.satisfactions.joins(:user).select("satisfactions.*,users.email as email")
  end
  headers = ['id',I18n.t('sb.project'),I18n.t('sb.client'),I18n.t('sb.project_manager'),I18n.t('sb.collected_by'),I18n.t('sb.date'),I18n.t('sb.description')]+self.get_names
  csv = CSV.generate(headers: true, :col_sep => ';') do |c|
    c << headers
    satisfactions.each do |a|
      # ****************************************************************************
      # regular expression check !!!!!
      casenum = Validations.project_id_reg_exp.match(a.case_number)
      client = Validations.extract_client(a.case_number)
      w = Validations.extract_project_manager(a.case_number)
      # ****************************************************************************
      project_description = Validations.extract_project_description(a.case_number)
      closed=[]
      for i in (1..self.closed_names_number.to_i)
        closed << a["closed#{i}"]
      end
      open=[]
      for i in (1..self.open_names_number.to_i)
        open << a["open#{i}"]
      end
      c << [a.id,casenum,client,w]+[a.email,a.created_at,project_description]+closed+open             
    end
  end
  csv  
end

#get_closed_namesObject

Return a table with all closed questions used in views/satisfactions/_form.html.erb



21
22
23
# File 'app/models/poll.rb', line 21

def get_closed_names
  self.closed_names.split(';')
end

#get_namesObject

Return a table with all closed and open questions used by the create method of the poll controller



35
36
37
# File 'app/models/poll.rb', line 35

def get_names
  get_closed_names + get_open_names
end

#get_open_namesObject

Return a table with all open questions used in views/satisfactions/_form.html.erb



28
29
30
# File 'app/models/poll.rb', line 28

def get_open_names
  self.open_names.split(';')
end

#hash_closedObject

Return all closed questions in a hash



41
42
43
44
45
46
47
48
# File 'app/models/poll.rb', line 41

def hash_closed
  table=self.closed_names.split(';')
  hash={}
  table.each_with_index do |t,i|
    hash["closed#{i+1}"]=t.strip
  end
  hash
end

#hash_openObject

Return all open questions in a hash



52
53
54
55
56
57
58
59
# File 'app/models/poll.rb', line 52

def hash_open
  table=self.open_names.split(';')
  hash={}
  table.each_with_index do |t,i|
    hash["open#{i+1}"]=t.strip
  end
  hash
end

#stats(satisfactions = nil) ⇒ Object

Calculates stats on a list of feedbacks



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/poll.rb', line 95

def stats(satisfactions=nil)
  tab = Array.new(self.closed_names_number){Array.new(5,0)}
  unless satisfactions
    satisfactions=self.satisfactions
  end
  satisfactions.each do |s|
    for i in 1..self.closed_names_number
      if value = s["closed#{i}"]
        tab[i-1][value] += 1
      else
        tab[i-1][0] += 1
      end
    end
  end
  result={}
  closed=self.hash_closed
  puts("stats method poll model working on the following list of closed questions : #{closed}")
  for i in 1..self.closed_names_number
    result[closed["closed#{i}"]]={}
    for y in 0..4
      level=I18n.t("sb.satisfaction_level_#{y}")
      result[closed["closed#{i}"]][level] = ( tab[i-1][y].to_f / satisfactions.length * 100 ).round(2)
    end
  end
  result
end