1 class UserController < ApplicationController
3 ensure_application_is_installed_by_facebook_user
4 before_filter :source_statistic
5 before_filter :loading_static
6 Feed_action = "Take the Nokia E72 Friendship Challenge"
9 @time_counter = Time.now
10 @time_counter0 = Time.now
11 if (params[:f]) && (params[:f].to_i != 0)
12 add_stat(0, VISITED, params[:f] ? params[:f].to_i : 0)
17 @statics = Static.find(:first)
19 render :facebook_error
23 def processing_phraze (text_to_process, owner_name, friends_names,gender = "",number_of_friends = 0)
24 text = text_to_process
25 text = text.gsub("{owner_name}",owner_name)
26 text = text.gsub("{friends_names}",friends_names)
27 text = text.gsub("{number_of_friends}",number_of_friends.to_s)
28 text = text.gsub("{his/her}", gender == "female" ? "her": "his")
29 text = text.gsub("{he/she}",gender == "female" ? "she": "he")
30 text = text.gsub("{him/her}",gender == "female" ? "her": "him")
33 #text = text.gsub("{friends_names}",friends_names)
36 @userF = session[:facebook_session].user
37 if @userF.has_permission?("publish_stream") && session[:facebook_session].user.has_permission?("email")
38 @fbuser = User.find(:first, :conditions => ["uid = ?", @userF.uid])
40 if @fbuser && @fbuser.deleted
41 debug_message("User #{session[:facebook_session].user.uid} was already exist but he was in deleted status. So now he is totally deleted and will be added soon")
45 #if user does not exist yet in DB inserting in DB
50 if @userF.birthday != nil
51 birthday_mask = /(\w+)\ (\d+)\, (\d+)/
52 dateOfBirth = birthday_mask.match(@userF.birthday)
53 #if birthday ok for us
54 if (Months[$1] == nil)
57 birthday = Date.new(dateOfBirth[3].to_i,Months[dateOfBirth[1]],dateOfBirth[2].to_i)
61 #end if @userF.birthday != nil
63 #gathering affiliations
65 if @userF.affiliations
66 @userF.affiliations.each do |one_affiliation|
67 @affiliation += one_affiliation.name + " | "
71 #gathering other params
74 :birthday => birthday,
75 :relation => @userF.relationship_status,
76 :gender => @userF.sex,
77 :first_name => @userF.first_name,
78 :last_name => @userF.last_name,
79 :status => "WAS NOT SET YET" ,
80 :hometown_location => (@userF.hometown_location != nil) ? @userF.hometown_location.country.to_s + "/"+ @userF.hometown_location.state.to_s + "/" + @userF.hometown_location.city.to_s : "",
81 :affiliations => @affiliation,
82 :usermail => @userF.email
86 debug_message("Cannot get information about #{session[:facebook_session].user.uid}",false)
87 render :facebook_error
90 @fbuser = User.new(myParams)
91 debug_message("User #{session[:facebook_session].user.uid} was added to DB")
93 add_stat(@userF.uid, PERMISSION)
95 #end of getting user info
97 #end if @fbuser == nil
99 today_question_select 0
100 @question_number = "question1"
101 render :action => @question_number
105 def today_question_select number
107 today = Date.new(today_now.year,today_now.month,today_now.day)
108 today_questions = Stage.find(:all, :conditions => ["date = ?", today ] )
109 @today_question = today_questions[number]
110 if @today_question[number].blank?
111 today_questions = Stage.find(:all, :order => "date DESC" )
112 @today_question = today_questions[number]
113 if @today_question.blank?
114 render :facebook_error
122 today_question_select 0
123 user_has_published_once = false
124 user_has_published_once = question_processor 1
125 if user_has_published_once == true
126 @next_step = "question2"
127 render :action => "greetings"
133 today_question_select 1
134 user_has_published_once = false
135 user_has_published_once = question_processor 2
136 if user_has_published_once == true
137 @next_step = "question3"
138 render :action => "greetings"
144 today_question_select 2
145 user_has_published_once = false
146 user_has_published_once = question_processor 3
147 if user_has_published_once == true
148 @next_step = "question1"
149 render :action => "greetings"
153 def question_processor (question_number)
154 time_counter ("before of question_processor time was tooken")
156 user_has_published_once = false
158 session_new = Facebooker::Session.create
160 if params["friend_sel0#{i}"] != nil && params["friend_sel0#{i}"] != ""
161 @userFB2 =Facebooker::User.new(params["friend_sel0#{i}"], session_new)
162 time_counter ("Creating one user")
163 users_array << @userFB2
164 user_has_published_once = true
170 if user_has_published_once == true
173 users_array.each do |player|
178 time_counter ("Batching first names")
180 users_array.each do |player|
183 time_counter ("Showing result")
184 feeds_to_publish = []
186 @userFB = Facebooker::User.new(session[:facebook_session].user.uid, session_new)
187 time_counter ("Creating self user")
188 @user_local = User.find(:first, :conditions => ["uid = ?", session[:facebook_session].user.uid])
189 #@userFB.sex = session[:facebook_session].user.sex
190 time_counter ("Setting sex")
194 scenario_themself = ""
195 #preparing all names into array
197 for i in 0..(users_array.size - 1)
198 user_names << users_array[i].first_name
201 time_counter ("Getting first_name")
202 #creating messages for list of friends
203 for i in 0..(users_array.size - 1)
205 user_names_new = user_names - [users_array[i].first_name]
206 if user_names_new.size == 0
208 elsif user_names_new.size == 1
209 themessage = "#{user_names_new[0]}"
210 elsif user_names_new.size == 2
211 themessage = "#{user_names_new[0]} and #{user_names_new[1]}"
212 elsif user_names_new.size == 3
213 themessage = "#{user_names_new[0]}, #{user_names_new[1]} and #{user_names_new[2]}"
214 elsif user_names_new.size == 4
215 themessage = "#{user_names_new[0]}, #{user_names_new[1]},#{user_names_new[2]} and #{user_names_new[3]}"
217 #preparing for the third stage
219 self_publishing = users_array[i].first_name + ( users_array.size > 2 ? ", ":" and ") + themessage
221 self_publishing = users_array[i].first_name + ""
226 session[:self_publishing] = self_publishing
228 if question_number == 1
229 scenario_friend = processing_phraze(@today_question.feed_useraction, users_array[i].first_name, themessage,users_array[i].sex,user_names.size())
230 scenario_themself = processing_phraze(@today_question.feed_selfaction, @user_local.first_name, self_publishing,@user_local.gender,user_names.size())
232 debug_message scenario_friend
233 new_params = Hash.new(0)
235 :user_from => @userFB,
236 :user_to => users_array[i],
237 :main_message => scenario_friend
239 :action_link_text => Feed_action,
240 :action_link_href => FB_app_link + "?f=#{FROM_FEEDS}",
241 :attachment_name => @today_question.feedtext1,
242 :attachment_href => FB_app_link + "?f=#{FROM_FEEDS}",
243 :attachment_caption => @today_question.feedtext2,
244 :media_type => 'image',
245 :media_src => Current_site + @today_question.feedpic.url(:normal),
246 :media_href => FB_app_link + "?f=#{FROM_FEEDS}"
249 feeds_to_publish.push new_params
250 time_counter ("Was added to hash")
252 #end of for i in 0..users_array.size
255 @user_local.friends_published += (user_names.size + 1)
258 add_stat(0, PUBLISHED, user_names.size + 1)
260 new_params = Hash.new(0)
262 :user_from => @userFB,
264 :main_message => scenario_themself,
266 feeds_to_publish.push new_params
269 feeds_to_publish.each {|one|
270 #publishing(one[:user_from],one[:user_to],one[:main_message],Feed_action,FB_app_link + "?f=#{FROM_FEEDS}",processing_phraze(@today_question.feedtext1, @user_local.first_name, self_publishing,@user_local.gender,user_names.size ) ,FB_app_link + "?f=#{FROM_FEEDS}",processing_phraze(@today_question.feedtext2, @user_local.first_name, self_publishing,@user_local.gender,user_names.size ),'image',Current_site + @today_question.feedpic.url(:normal),FB_app_link + "?f=#{FROM_FEEDS}")
273 time_counter ("Session batch")
275 debug_message scenario_themself
276 # if user_has_published_once == true
278 return user_has_published_once
282 debug_message "Now session[:self_publishing] = '#{session[:self_publishing]}'"
283 if params[:fanbutton] != nil
284 redirect_to "http://www.facebook.com/pages/Chatting-and-Emailing-with-Nokia/10150115816675015"
287 if params[:mybutton] != nil
288 p "params[:next_step] = #{params[:next_step]}"
289 @question_number = params[:next_step]
290 case @question_number
292 today_question_select 0
294 today_question_select 1
296 today_question_select 2
299 render :action => @question_number
311 def publishing(user_from,user_to,main_message,action_link_text,action_link_href,attachment_name,attachment_href,attachment_caption,media_type,media_src,media_href)
312 user_from.publish_to(user_to,
313 :message => main_message,
316 :text => action_link_text,
317 :href => action_link_href}
320 :name => attachment_name,
321 :href => attachment_href,
322 :caption => attachment_caption,
334 def time_counter (object)
335 the_time = Time.now - @time_counter
336 @time_counter = Time.now
337 p "#{object}: #{the_time.to_s}"