reducing app
[onlineproject.git] / online / app / controllers / user_controller.rb
blob66b8cce691f197fd08c5c31ed155e5337951bf65
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"
9   def source_statistic
10     if (params[:f]) && (params[:f].to_i != 0)
11       add_stat(0, VISITED, params[:f] ? params[:f].to_i : 0)
12     end         
13   end
14   
15   def permission
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
20       if @fbuser == nil
21         #getting user info
22         begin
23           #if user has  birthday
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)
29               birthday = ""
30             else
31               birthday = Date.new(dateOfBirth[3].to_i,Months[dateOfBirth[1]],dateOfBirth[2].to_i)
32             end
33           else
34             birthday = ""
35           #end if @userF.birthday != nil
36           end
37           #gathering  affiliations
38           @affiliation  = ""
39           if @userF.affiliations
40             @userF.affiliations.each do |one_affiliation|
41               @affiliation += one_affiliation.name + " | "
42             end
43           end
44           
45           #gathering other params
46           myParams = {
47                         :uid               => @userF.uid,
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
56                       }
57         rescue
58           debug_message("Cannot get information about #{session[:facebook_session].user.uid}",false)
59           render :facebook_error
60         else
61           #saving user into DB
62           @fbuser = User.new(myParams)
63           debug_message("User #{session[:facebook_session].user.uid} was added to DB")
64           @fbuser.save
65           add_stat(@userF.uid, PERMISSION)
66          
67         #end of getting user info
68         end
69       #end if @fbuser == nil
70       end 
71       render :action => "question1"    
72     end
73   end
75   def 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" 
80   
81     end
82   end
84   
85   def question2
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" 
90       
91     end
92   end
94   def 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" 
101     end
102   end
104   def question_processor (question_number)
105     user_has_published_once = false
106     users_array = []
107     session_new = Facebooker::Session.create
108     for i in 0...6
109       if params["friend_sel0#{i}"] != nil && params["friend_sel0#{i}"] != ""
110       # begin  
111         @userFB2 =Facebooker::User.new(params["friend_sel0#{i}"], session_new)
112         users_array << @userFB2 
113         user_has_published_once = true
114         #return
115       #rescue
116         #debug_message("Cannot publish or some problems with getting info from FB",false)
117       #end of if
118       end
119     #end of for
120     end 
121     if user_has_published_once == true
122       @userFB = Facebooker::User.new(session[:facebook_session].user.uid, session_new)
123       user_names = []
124       #for third time
125       self_publishing = ""
126       scenario_themself = ""
127       #preparing all names into array
128       debugger
129       for i in 0..(users_array.size - 1)
130         user_names << users_array[i].first_name
131       end 
132       #creating messages for list of friends
133       for i in 0..(users_array.size - 1)  
134         themessage = ""
135         user_names_new = user_names - [users_array[i].first_name]
136         if user_names_new.size == 0
137           themessage = ""
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]}"
146         end
147         #preparing for the third stage
148         if themessage != ""
149           self_publishing = users_array[i].first_name + ( users_array.size > 2 ? ", ":" and ") + themessage
150         else
151           self_publishing = users_array[i].first_name + ""
152         end
153         if themessage != ""
154           themessage += "."
155         end
156         session[:self_publishing] = self_publishing
157         #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"    
160           
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
165          
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
177         end
178         #end of for i in 0..users_array.size
179       end
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)
183       
184       @userFB2 =Facebooker::User.new(params["friend_sel0#{i}"], session_new)
185       debug_message("Messages was published into stream")
186       
187     # if user_has_published_once == true
188     end
189     return user_has_published_once
190   end
192   def greetings
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)
200         
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)
204       end
205       redirect_to "http://facebook.com"
206     end
207   end
209   def product
210   end
212   def fanpage
213   end
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,
218         :action_links => [
219                 {
220                 :text => action_link_text,
221                 :href => action_link_href}
222                 ],
223         :attachment => {
224                 :name => attachment_name,
225                 :href => attachment_href,
226                 :caption => attachment_caption,
228                 :media => [
229                         {
230                           :type => media_type,
231                           :src =>  media_src,
232                           :href => media_href
233                         }
235                        ]})
236   end