Addin statistic and some changes so far
[onlineproject.git] / online / app / controllers / user_controller.rb
blob973eb58d3812e2620da4a12035e9ce43ed03c249
1 class UserController < ApplicationController
3   ensure_application_is_installed_by_facebook_user
4   before_filter :source_statistic 
5    
6   def source_statistic
7     if (params[:f]) && (params[:f].to_i != 0)
8       add_stat(0, VISITED, params[:f] ? params[:f].to_i : 0)
9     end         
10   end
12   def permission
13     @userF = session[:facebook_session].user
14     if @userF.has_permission?("publish_stream") && session[:facebook_session].user.has_permission?("email")
15       @fbuser = User.find(:first, :conditions => ["uid = ?", @userF.uid])
16       #if user does not exist yet in DB inserting in DB
17       if @fbuser == nil
18         #getting user info
19         begin
20           #if user has  birthday
21           if @userF.birthday != nil
22             birthday_mask = /(\w+)\ (\d+)\, (\d+)/
23             dateOfBirth = birthday_mask.match(@userF.birthday)
24             #if birthday ok for us
25             if (Months[$1] == nil)
26               birthday = ""
27             else
28               birthday = Date.new(dateOfBirth[3].to_i,Months[dateOfBirth[1]],dateOfBirth[2].to_i)
29             end
30           else
31             birthday = ""
32           #end if @userF.birthday != nil
33           end
34           #gathering  affiliations
35           @affiliation  = ""
36           if @userF.affiliations
37             @userF.affiliations.each do |one_affiliation|
38               @affiliation += one_affiliation.name + " | "
39             end
40           end
41           
42           #gathering other params
43           myParams = {
44                         :uid               => @userF.uid,
45                         :birthday          => birthday,
46                         :relation          => @userF.relationship_status,
47                         :gender            => @userF.sex,
48                         :first_name        => @userF.first_name,
49                         :last_name         => @userF.last_name,
50                         :status            => "WAS NOT SET YET" ,
51                         :hometown_location => (@userF.hometown_location != nil) ? @userF.hometown_location.country.to_s + "/"+  @userF.hometown_location.state.to_s + "/" + @userF.hometown_location.city.to_s : "",
52                         :affiliations      => @affiliation
53                       }
54         rescue
55           debug_message("Cannot get information about #{session[:facebook_session].user.uid}",false)
56           render :facebook_error
57         else
58           #saving user into DB
59           @fbuser = User.new(myParams)
60           debug_message("User #{session[:facebook_session].user.uid} was added to DB")
61           @fbuser.save
62           add_stat(@userF.uid, PERMISSION)
63          
64         #end of getting user info
65         end
66       #end if @fbuser == nil
67       end 
68       render :action => "question1"    
69     end
70   end
72   def question1
73     user_has_published_once = false
74     user_has_published_once = question_processor
75     if user_has_published_once
76       render :action => "question2" 
77   
78     end
79   end
81   
82   def question2
83     user_has_published_once = false
84     user_has_published_once = question_processor
85     if user_has_published_once
86       render :action => "question3" 
87       
88     end
89   end
91   def question3
92     user_has_published_once = false
93     user_has_published_once = question_processor
94     if user_has_published_once
95       render :action => "greetings" 
96     end
97   end
100   def question_processor 
101     user_has_published_once = false
102     for i in 0...6
103       if params["friend_sel0#{i}"] != nil && params["friend_sel0#{i}"] != ""
104       # begin  
105         #preparing for public first feed
106         session_new = Facebooker::Session.create
107         if !user_has_published_once
108           @userFB = Facebooker::User.new(session[:facebook_session].user.uid, session_new)
109         end
110         #debugger
111         @userFB2 =Facebooker::User.new(params["friend_sel0#{i}"], session_new)
112         #publishing first message
113         #p @today_quiz.feedpic.url(:normal)
115         publishing (@userFB,@userFB,"selected his top 5 friends: #{@userFB2.first_name}, X, Y, Z, W to win Nokia E72. How well do you know your friends? Answer and win now!","Take the Nokia E72 challenge","http://foo.com","2Take the Nokia E72 friendship challenge now to win up to 6 Nokia E72 devices","http://foo2.com","3Take the Nokia E72 friendship challenge now to win up to 6 Nokia E72 devices",'image',"http://www.finalsense.com/news/image/mobile/nokia-6124classic.jpg","http://foo3.com")
116         
117         #publishing (@userFB,@userFB2,"#{@userFB2.first_name}, you are one of my best friend here. I also picked  X, Y, Z, W. Let's stay in youch","Take the Nokia E72 challenge","http://foo.com","2Take the Nokia E72 friendship challenge now to win up to 6 Nokia E72 devices","http://foo2.com","3Take the Nokia E72 friendship challenge now to win up to 6 Nokia E72 devices",'image',"http://www.finalsense.com/news/image/mobile/nokia-6124classic.jpg","http://foo3.com")
119         debug_message("Message was published into stream")
120         user_has_published_once = true
121         #return
122       #rescue
123         #debug_message("Cannot publish or some problems with getting info from FB",false)
124       #end of if
125       end
126     #end of for
127     end 
128     return user_has_published_once
129   end
131   def greetings
132   end
134   def product
135   end
137   def fanpage
138   end
140    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)
141     user_from.publish_to(user_to,
142         :message => main_message,
143         :action_links => [
144                 {
145                 :text => action_link_text,
146                 :href => action_link_href}
147                 ],
148         :attachment => {
149                 :name => attachment_name,
150                 :href => attachment_href,
151                 :caption => attachment_caption,
153                 :media => [
154                         {
155                           :type => media_type,
156                           :src =>  media_src,
157                           :href => media_href
158                         }
160                        ]})
161   end