Major changes after Nina discussions
[onlineproject.git] / online / app / controllers / user_controller.rb
blob1d1bb82010217494f33bbfff0c0335ff7e7d782a
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"
8   def source_statistic
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)
13     end         
14   end
15   
16   def loading_static    
17     @statics = Static.find(:first)
18     if @statics == nil
19       render :facebook_error
20     end
21   end
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")
32     return text 
33     #text = text.gsub("{friends_names}",friends_names)
34   end 
35   def permission
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])
39       
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")
42         @fbuser.destroy
43         @fbuser = nil
44       end
45       #if user does not exist yet in DB inserting in DB
46       if @fbuser == nil
47         #getting user info
48         begin
49           #if user has  birthday
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)
55               birthday = ""
56             else
57               birthday = Date.new(dateOfBirth[3].to_i,Months[dateOfBirth[1]],dateOfBirth[2].to_i)
58             end
59           else
60             birthday = ""
61           #end if @userF.birthday != nil
62           end
63           #gathering  affiliations
64           @affiliation  = ""
65           if @userF.affiliations
66             @userF.affiliations.each do |one_affiliation|
67               @affiliation += one_affiliation.name + " | "
68             end
69           end
70           
71           #gathering other params
72           myParams = {
73                         :uid               => @userF.uid,
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
83                     
84                       }
85         rescue
86           debug_message("Cannot get information about #{session[:facebook_session].user.uid}",false)
87           render :facebook_error
88         else
89           #saving user into DB
90           @fbuser = User.new(myParams)
91           debug_message("User #{session[:facebook_session].user.uid} was added to DB")
92           @fbuser.save
93           add_stat(@userF.uid, PERMISSION)
94          
95         #end of getting user info
96         end
97       #end if @fbuser == nil
98       end 
99       today_question_select 0
100       @question_number = "question1"
101       render :action => @question_number    
102     end
103   end
104   
105   def today_question_select number
106     today_now = Time.now  
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
115       end
116     end 
117   end
119   def question1
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"  
128     end
129   end
131   def question2
132     
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"  
139     end
140   end
142   def question3
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" 
150     end
151   end
153   def question_processor (question_number)
154     time_counter ("before of question_processor time was tooken")
156     user_has_published_once = false
157     users_array = []
158     session_new = Facebooker::Session.create
159     for i in 0...6
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
165       #end of if
166       end
167     #end of for
168     end 
169     
170     if user_has_published_once == true
172       session_new.batch do 
173         users_array.each do |player| 
174           player.first_name
175         end 
176       end 
178       time_counter ("Batching first names")
179   
180       users_array.each do |player|
181         p player.first_name 
182       end
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")
191       user_names = []
192       #for third time
193       self_publishing = ""
194       scenario_themself = ""
195       #preparing all names into array
196       
197       for i in 0..(users_array.size - 1)
198         user_names << users_array[i].first_name
199       end 
200    
201       time_counter ("Getting first_name")
202       #creating messages for list of friends
203       for i in 0..(users_array.size - 1)  
204         themessage = ""
205         user_names_new = user_names - [users_array[i].first_name]
206         if user_names_new.size == 0
207           themessage = ""
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]}"
216         end
217         #preparing for the third stage
218         if themessage != ""
219           self_publishing = users_array[i].first_name + ( users_array.size > 2 ? ", ":" and ") + themessage
220         else
221           self_publishing = users_array[i].first_name + ""
222         end
223         if themessage != ""
224           themessage += ""
225         end
226         session[:self_publishing] = self_publishing
227         #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) 
234           new_params = { 
235                   :user_from   => @userFB,
236                   :user_to   => users_array[i],
237                   :main_message => scenario_friend
238 =begin
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}"
247 =end
248                   }
249           feeds_to_publish.push new_params
250           time_counter ("Was added to hash")
251         end 
252       #end of for i in 0..users_array.size
253       end
254       
255       @user_local.friends_published += (user_names.size + 1)
256       @user_local.save
258       add_stat(0, PUBLISHED, user_names.size + 1)
259       
260       new_params = Hash.new(0) 
261       new_params = { 
262                   :user_from   => @userFB,
263                   :user_to   => @userFB,
264                   :main_message => scenario_themself,   
265                   }
266       feeds_to_publish.push new_params
267       
268       session_new.batch do 
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}")
271         }
272       end
273       time_counter ("Session batch") 
274       
275       debug_message scenario_themself  
276     # if user_has_published_once == true
277     end
278     return user_has_published_once
279   end
281   def greetings
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"
285     end
287     if params[:mybutton] != nil
288       p "params[:next_step] = #{params[:next_step]}"
289       @question_number = params[:next_step]
290       case @question_number 
291         when "question1"
292           today_question_select 0
293         when "question2"
294           today_question_select 1
295         when "question3"
296           today_question_select 2
297       end
299       render :action => @question_number  
300     end 
301   end
303   def product
304   end
306   def fanpage
307   end
308   
309   def facebook_error
310   end
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,
314         :action_links => [
315                 {
316                 :text => action_link_text,
317                 :href => action_link_href}
318                 ],
319         :attachment => {
320                 :name => attachment_name,
321                 :href => attachment_href,
322                 :caption => attachment_caption,
324                 :media => [
325                         {
326                           :type => media_type,
327                           :src =>  media_src,
328                           :href => media_href
329                         }
331                        ]})
332   end
333   
334   def time_counter (object)
335     the_time = Time.now - @time_counter
336     @time_counter = Time.now
337     p "#{object}: #{the_time.to_s}"
338   end