1 class UserController < ApplicationController
3 ensure_application_is_installed_by_facebook_user
4 before_filter :source_statistic
5 FB_app_link = "http://apps.facebook.com/friendshipchallenge"
6 Promotion1 = "Take the Nokia E72 friendship challenge now to know your friends better and win up to 6 Nokia E72 devices."
7 Pic_link = "http://img.gsmarena.com/vv/bigpic/nokia-e72.jpg"
8 Feed_action = "Know better your friends"
10 if (params[:f]) && (params[:f].to_i != 0)
11 add_stat(0, VISITED, params[:f] ? params[:f].to_i : 0)
16 @userF = session[:facebook_session].user
17 if @userF.has_permission?("publish_stream") && session[:facebook_session].user.has_permission?("email")
18 @fbuser = User.find(:first, :conditions => ["uid = ?", @userF.uid])
19 #if user does not exist yet in DB inserting in DB
24 if @userF.birthday != nil
25 birthday_mask = /(\w+)\ (\d+)\, (\d+)/
26 dateOfBirth = birthday_mask.match(@userF.birthday)
27 #if birthday ok for us
28 if (Months[$1] == nil)
31 birthday = Date.new(dateOfBirth[3].to_i,Months[dateOfBirth[1]],dateOfBirth[2].to_i)
35 #end if @userF.birthday != nil
37 #gathering affiliations
39 if @userF.affiliations
40 @userF.affiliations.each do |one_affiliation|
41 @affiliation += one_affiliation.name + " | "
45 #gathering other params
48 :birthday => birthday,
49 :relation => @userF.relationship_status,
50 :gender => @userF.sex,
51 :first_name => @userF.first_name,
52 :last_name => @userF.last_name,
53 :status => "WAS NOT SET YET" ,
54 :hometown_location => (@userF.hometown_location != nil) ? @userF.hometown_location.country.to_s + "/"+ @userF.hometown_location.state.to_s + "/" + @userF.hometown_location.city.to_s : "",
55 :affiliations => @affiliation
58 debug_message("Cannot get information about #{session[:facebook_session].user.uid}",false)
59 render :facebook_error
62 @fbuser = User.new(myParams)
63 debug_message("User #{session[:facebook_session].user.uid} was added to DB")
65 add_stat(@userF.uid, PERMISSION)
67 #end of getting user info
69 #end if @fbuser == nil
71 render :action => "question1"
76 user_has_published_once = false
77 user_has_published_once = question_processor 1
78 if user_has_published_once == true
79 render :action => "greetings"
86 user_has_published_once = false
87 user_has_published_once = question_processor 2
88 if user_has_published_once == true
89 render :action => "question3"
95 user_has_published_once = false
96 user_has_published_once = question_processor 3
97 if user_has_published_once == true
98 session_new = Facebooker::Session.create
99 @userFB = Facebooker::User.new(session[:facebook_session].user.uid, session_new)
100 render :action => "greetings"
104 def question_processor (question_number)
105 user_has_published_once = false
107 session_new = Facebooker::Session.create
109 if params["friend_sel0#{i}"] != nil && params["friend_sel0#{i}"] != ""
111 @userFB2 =Facebooker::User.new(params["friend_sel0#{i}"], session_new)
112 users_array << @userFB2
113 user_has_published_once = true
116 #debug_message("Cannot publish or some problems with getting info from FB",false)
121 if user_has_published_once == true
122 @userFB = Facebooker::User.new(session[:facebook_session].user.uid, session_new)
126 scenario_themself = ""
127 #preparing all names into array
129 for i in 0..(users_array.size - 1)
130 user_names << users_array[i].first_name
132 #creating messages for list of friends
133 for i in 0..(users_array.size - 1)
135 user_names_new = user_names - [users_array[i].first_name]
136 if user_names_new.size == 0
138 elsif user_names_new.size == 1
139 themessage = "#{user_names_new[0]}"
140 elsif user_names_new.size == 2
141 themessage = "#{user_names_new[0]} and #{user_names_new[1]}"
142 elsif user_names_new.size == 3
143 themessage = "#{user_names_new[0]}, #{user_names_new[1]} and #{user_names_new[2]}"
144 elsif user_names_new.size == 4
145 themessage = "#{user_names_new[0]}, #{user_names_new[1]},#{user_names_new[2]} and #{user_names_new[3]}"
147 #preparing for the third stage
149 self_publishing = users_array[i].first_name + ( users_array.size > 2 ? ", ":" and ") + themessage
151 self_publishing = users_array[i].first_name + ""
156 session[:self_publishing] = self_publishing
158 if question_number == 1
159 scenario_friend = "#{users_array[i].first_name}, I chat with you the most. #{themessage != "" ? "I also picked" : ""} #{themessage} Let's stay in touch"
161 scenario_themself = "selected #{@userFB.sex == "female" ? "her": "his"} top #{user_names.size()} friend#{user_names.size() == 1 ? "" : "s"} #{@userFB.sex == "female" ? "she": "he"} chat the most to win Nokia E72: #{self_publishing}. How well do you know your friends? Answer and win now!"
162 debug_message scenario_friend
163 publishing(@userFB,users_array[i],scenario_friend,Feed_action,FB_app_link,Promotion1,FB_app_link,Promotion1,'image',Pic_link,FB_app_link)
164 elsif question_number == 2
166 scenario_friend = "#{users_array[i].first_name}, you are one of my #{user_names.size()} person in the first row whom I would invite to my Birthday. #{themessage != "" ? "I also picked" : ""} #{themessage} Let's stay in touch"
167 scenario_themself = "selected #{@userFB.sex == "female" ? "her": "his"} top #{user_names.size()} friend#{user_names.size() == 1 ? "" : "s"} whom #{@userFB.sex == "female" ? "she": "he"} would invite to Birthday in the first raw to win Nokia E72: #{self_publishing}. How well do you know your friends? Answer and win now!"
168 debug_message scenario_friend
169 publishing(@userFB,users_array[i],scenario_friend,Feed_action,FB_app_link,Promotion1,FB_app_link,Promotion1,'image',Pic_link,FB_app_link)
170 elsif question_number == 3
171 p "Publishing: > > > > >"
172 scenario_friend = "#{users_array[i].first_name}, you are one of my #{user_names.size()} most sporty friends. #{themessage != "" ? "I also picked" : ""} #{themessage} Let's stay in touch"
173 scenario_themself = "selected #{@userFB.sex == "female" ? "her": "his"} top sporty #{user_names.size()} friend#{user_names.size() == 1 ? "" : "s"} to win Nokia E72: #{self_publishing}. How well do you know your friends? Answer and win now!"
174 debug_message scenario_friend
175 publishing(@userFB,users_array[i],scenario_friend,Feed_action,FB_app_link,Promotion1,FB_app_link,Promotion1,'image',Pic_link,FB_app_link)
176 #end of if question_number == 1
178 #end of for i in 0..users_array.size
180 p "Use sex: #{@userFB.sex}"
181 debug_message scenario_themself
182 publishing(@userFB,@userFB,scenario_themself,Feed_action,FB_app_link,Promotion1,FB_app_link,Promotion1,'image',Pic_link,FB_app_link)
184 @userFB2 =Facebooker::User.new(params["friend_sel0#{i}"], session_new)
185 debug_message("Messages was published into stream")
187 # if user_has_published_once == true
189 return user_has_published_once
193 debug_message "Now session[:self_publishing] = '#{session[:self_publishing]}'"
194 if params[:mybutton] != nil
195 if params[:mybutton] == "Publish"
196 session_new = Facebooker::Session.create
197 @userFB = Facebooker::User.new(session[:facebook_session].user.uid, session_new)
198 random_friend_num = rand(@userFB.friends.size)
199 @userFB2 = Facebooker::User.new(@userFB.friends[random_friend_num].uid, session_new)
201 scenario_themself = "#{@userFB2.first_name}, I picked up #{params[:self_publishing]} as my top friends to win Nokia E72. Sorry I didn't pick you, but you can try your luck here."
202 debug_message scenario_themself
203 publishing(@userFB,@userFB,scenario_themself,Feed_action,FB_app_link,Promotion1,FB_app_link,Promotion1,'image',Pic_link,FB_app_link)
205 redirect_to "http://facebook.com"
215 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)
216 user_from.publish_to(user_to,
217 :message => main_message,
220 :text => action_link_text,
221 :href => action_link_href}
224 :name => attachment_name,
225 :href => attachment_href,
226 :caption => attachment_caption,