Guest User

Untitled

a guest
Jan 27th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.13 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2.  
  3. hobo_user_model # Don't put anything above this
  4. has_paper_trail :ignore => [:key_timestamp]
  5.  
  6. fields do
  7. name :string, :required, :unique
  8. nickname :string, :unique
  9. pass_expires :date
  10. role enum_string(:international, :sales, :resourcing, :playmaker, :commercial)
  11. email_address :email_address, :login => true
  12. administrator :boolean, :default => false
  13. timestamps
  14.  
  15. end
  16.  
  17. has_many :calls
  18. has_many :companies
  19. has_many :projects
  20. has_many :leads
  21. has_many :signoffs
  22. has_many :todos
  23. belongs_to :access_role
  24.  
  25. set_default_order "name ASC"
  26.  
  27. named_scope :administrators, :conditions => { :administrator => true }
  28. named_scope :decider, :conditions => { :role => "playmaker" }
  29. named_scope :resourcer, :conditions => { :role => "resourcing" }
  30. named_scope :selling, :conditions => "role = 'sales' or role = 'playmaker'"
  31. named_scope :show_calls, :from => "(select *, (select count(*) from calls where user_id=users.id and created_at >= '" + (Date.commercial(Date.today.year, Date.today.cweek, 1) - 11 * 7).strftime("%Y-%m-%d") + "' and result != 'nocall') as total_calls from users) users", :conditions => "total_calls > 0"
  32. named_scope :show_leads, :from => "(select *, (select count(*) from leads where user_id=users.id and created_at >= '" + (Date.commercial(Date.today.year, Date.today.cweek, 1) - 11 * 7).strftime("%Y-%m-%d") + "') as total_leads from users) users", :conditions => "total_leads > 0"
  33. named_scope :show_projects, :from => "(select *, (select count(*) from projects where user_id=users.id and created_at >= '" + (Date.commercial(Date.today.year, Date.today.cweek, 1) - 11 * 7).strftime("%Y-%m-%d") + "') as total_projects from users) users", :conditions => "total_projects > 0"
  34.  
  35. def before_save()
  36. if changing_password? and password == password_confirmation and current_password != password
  37. reg = /^(?=.*\d)(?=.*([a-z]))(?=.*([A-Z]))([\x20-\x7E]){8,40}$/
  38.  
  39. # puts password, password.length ,reg.match(password), "koniec"
  40. if reg.match(password)
  41. reincarnate_pass
  42. else
  43. errors.add(:password, Hobo::Translations.ht("hobo.messages.validate_password", :default => "must be at least 8 characters long and must consist of digits, lowercase and uppercase letters."))
  44. false
  45. end
  46. end
  47. if current_password == password && !current_password.nil?
  48. errors.add(:password, Hobo::Translations.ht("hobo.messages.validate_password", :default => "Your new password must differ from old one"))
  49. false
  50. end
  51. end
  52.  
  53. def after_create
  54. self.update_attribute(:state, "active")
  55. end
  56.  
  57. def reincarnate_pass
  58. acting_user.pass_expires = Date::today >> 3
  59. acting_user.save
  60. end
  61.  
  62. def self.week_number(week)
  63. return (Date.today - 7 * (week - 1)).cweek
  64. end
  65.  
  66. def get_week_number(week)
  67. return User.week_number(week)
  68. end
  69.  
  70. def self.get_start_week(week)
  71. start = (Date.commercial(Date.today.year, Date.today.cweek, 1) - (week - 1) * 7).strftime("%Y-%m-%d")
  72. return start
  73. end
  74.  
  75. def self.get_end_week(week)
  76. return User.get_start_week(week - 1)
  77. end
  78.  
  79. def self.get_count(user, start, finish, table)
  80. user_command = user == nil ? '' : "user_id=" + user.to_s + " and "
  81. finish_command = finish == nil ? '' : " and created_at < '" + finish + "'"
  82. no_call_command = table == 'calls' ? " and result != 'nocall'" : ""
  83. return connection.select_value("select count(*) from " + table + " where " + user_command + "created_at >= '" + start + "'" + finish_command + no_call_command)
  84. end
  85.  
  86. def total_calls
  87. return User.get_count(read_attribute('id'), User.get_start_week(12), nil, 'calls')
  88. end
  89.  
  90. def total_leads
  91. return User.get_count(read_attribute('id'), User.get_start_week(12), nil, 'leads')
  92. end
  93.  
  94. def total_projects
  95. return User.get_count(read_attribute('id'), User.get_start_week(12), nil, 'projects')
  96. end
  97.  
  98. def calls_week(week)
  99. return User.get_count(read_attribute('id'), User.get_start_week(week), User.get_end_week(week), 'calls')
  100. end
  101.  
  102. def leads_week(week)
  103. return User.get_count(read_attribute('id'), User.get_start_week(week), User.get_end_week(week), 'leads')
  104. end
  105.  
  106. def projs_week(week)
  107. return User.get_count(read_attribute('id'), User.get_start_week(week), User.get_end_week(week), 'projects')
  108. end
  109.  
  110. def all_calls_week(week)
  111. if(week == 0) #total
  112. return User.get_count(nil, User.get_start_week(12), nil, 'calls')
  113. end
  114. return User.get_count(nil, User.get_start_week(week), User.get_end_week(week), 'calls')
  115. end
  116.  
  117. def all_leads_week(week)
  118. if(week == 0) #total
  119. return User.get_count(nil, User.get_start_week(12), nil, 'leads')
  120. end
  121. return User.get_count(nil, User.get_start_week(week), User.get_end_week(week), 'leads')
  122. end
  123.  
  124. def all_projs_week(week)
  125. if(week == 0) #total
  126. return User.get_count(nil, User.get_start_week(12), nil, 'projects')
  127. end
  128. return User.get_count(nil, User.get_start_week(week), User.get_end_week(week), 'projects')
  129. end
  130.  
  131. def method_missing(method, *args, &block)
  132. str = method.to_s
  133. if(str.length < 11)
  134. return super
  135. end
  136. if(str[0,10] == 'calls_week')
  137. week = str[10, str.length - 10].to_i
  138. return super if (week == nil)
  139. return calls_week(week)
  140. end
  141. if(str[0,10] == 'leads_week')
  142. week = str[10, str.length - 10].to_i
  143. return super if (week == nil)
  144. return leads_week(week)
  145. end
  146. if(str[0,10] == 'projs_week')
  147. week = str[10, str.length - 10].to_i
  148. return super if (week == nil)
  149. return projs_week(week)
  150. end
  151.  
  152. end
  153.  
  154. def self.generate_projects_graph
  155. return User.generate_graph('projects')
  156. end
  157.  
  158. def self.generate_leads_graph
  159. return User.generate_graph('leads')
  160. end
  161.  
  162. def self.generate_calls_graph
  163. return User.generate_graph('calls')
  164. end
  165.  
  166. def self.generate_graph(table)
  167. no_call_command = table == 'calls' ? " and result != 'nocall'" : ""
  168. ids = connection.select_values("select id from (select *, (select count(*) from " + table + " where user_id = users.id and created_at >= '" + User.get_start_week(12) + "'" + no_call_command + ") as total from users order by name) users where total > 0")
  169. title = Title.new(table.capitalize)
  170.  
  171. chart = OpenFlashChart.new
  172. bar_stack = BarStack.new
  173.  
  174. max = 1
  175. r = 3
  176. g = 1
  177. b = 2
  178. colors = []
  179. 0.upto(ids.length - 1) {
  180. rs = r.to_s(16)
  181. gs = g.to_s(16)
  182. bs = b.to_s(16)
  183. rs = '0' + rs if (rs.length == 1)
  184. gs = '0' + gs if (gs.length == 1)
  185. bs = '0' + bs if (bs.length == 1)
  186. colors << rs + gs + bs
  187. r = (r + 93) & 0xFF
  188. g = (g + 111) & 0xFF
  189. b = (b + 49) & 0xFF
  190. }
  191.  
  192. 12.downto(1) { |k|
  193. data = []
  194. bar_total = 0
  195. 0.upto(ids.length - 1){ |i|
  196. v = User.get_count(ids[i], User.get_start_week(k), User.get_end_week(k), table).to_i
  197. if v > 0
  198. data << BarStackValue.new(v, colors[i])
  199. bar_total += v
  200. end
  201. }
  202. if bar_total > max
  203. max = bar_total
  204. end
  205. bar_stack.append_stack(Array.new(data))
  206. }
  207.  
  208. y = YAxis.new
  209. y.set_range(0, max + 1, 5)
  210.  
  211. x = XAxis.new
  212. weeks = []
  213. 12.downto(1){ |k|
  214. weeks << 'w' + User.week_number(k).to_s
  215. }
  216. x.set_labels(weeks)
  217.  
  218. x_legend = XLegend.new("week")
  219. x_legend.set_style('{font-size: 20px; color: #000000}')
  220.  
  221. y_legend = YLegend.new(table.to_s + " count")
  222. y_legend.set_style('{font-size: 20px; color: #000000}')
  223.  
  224. tooltip = Tooltip.new;
  225. tooltip.set_hover();
  226. bar_stack.set_tooltip( '#x_label# [#val#]<br>Total [#total#]' );
  227.  
  228. chart.add_element(bar_stack)
  229. chart.set_title(title)
  230. chart.set_x_legend(x_legend)
  231. chart.set_y_legend(y_legend)
  232. chart.y_axis = y
  233. chart.set_x_axis(x)
  234. chart.set_tooltip( tooltip );
  235.  
  236. 0.upto(ids.length - 1) { |i|
  237. line = Line.new
  238. line.set_key(connection.select_values("select name from users where id = " + ids[i].to_s), 15)
  239. line.colour = colors[i]
  240. chart.add_element(line)
  241. }
  242.  
  243. return chart
  244. end
  245.  
  246.  
  247. # --- Signup lifecycle --- #
  248.  
  249. lifecycle do
  250.  
  251. state :inactive, :active
  252.  
  253.  
  254. transition :request_password_reset, { :active => :active }, :new_key => true do
  255. UserMailer.deliver_forgot_password(self, lifecycle.key)
  256. end
  257.  
  258. transition :reset_password, { :active => :active }, :available_to => :key_holder,
  259. :params => [ :password, :password_confirmation ] do
  260. #after_save :reincarnate_pass
  261. #puts "barszcz z grzybami"
  262. end
  263.  
  264. transition :deactivate_user, { :active => :inactive }, :available_to => :all
  265.  
  266. transition :deactivate_user, { :inactive => :active }, :available_to => :all
  267.  
  268. end
  269.  
  270.  
  271.  
  272. # --- Permissions --- #
  273.  
  274. def create_permitted?
  275. acting_user.administrator?
  276. end
  277.  
  278. def update_permitted?
  279. acting_user.administrator? ||
  280. (acting_user == self && only_changed?(:email_address, :crypted_password,
  281. :current_password, :password, :password_confirmation))
  282. # Note: crypted_password has attr_protected so although it is permitted to change, it cannot be changed
  283. # directly from a form submission.
  284. end
  285.  
  286. def destroy_permitted?
  287. acting_user.administrator?
  288. end
  289.  
  290. def view_permitted?(field)
  291. true
  292. end
  293.  
  294. end
Add Comment
Please, Sign In to add comment