Class: TeamsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/teams_controller.rb

Overview

TeamsController

Constant Summary

Constant Summary

Constants inherited from ApplicationController

ApplicationController::SupportedClients

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#absolutize_path, #clean_summary

Methods included from Authorization

included, ri?, #role_included?, #set_promotion_for_public_request

Instance Method Details

- (Array) index

Get all of the teams for the current competition and promotion

URL

/teams [GET]

Response Codes
200 OK

Successfully retrieved all Team objects

# Example response
 [
     {
         "team": {
             "row_id": "65.team.196",
             "is_official": true,
             "name": "API Test Team",
             "rank_group": "00001",
             "avg_daily_goals_reached": 1,
             "name_group": "A",
             "updated_at": "2012-03-28T12:48:42Z",
             "motto": "Please work!",
             "rank": 1,
             "id": 196,
             "total_minutes": 205,
             "user_id": 65,
             "avg_minutes": 42.83,
             "total_daily_goals_reached": 12,
             "score": 33.83
         }
     }
 ]

Returns:

  • (Array)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/teams_controller.rb', line 35

def index
  # Try to get the current competition by default
  cc = @promotion.current_competition

  items = []
  unless cc.nil?
    sql = "SELECT\nt.id,\nt.name,\nt.motto,\nt.status,\nIF(t.status = 1, @rownum := @rownum + 1, NULL) AS 'rownum',\nIF(t.status = 1, @rank := IF(COALESCE(t.score,0.0) = @previous,@rank,@rownum), NULL) AS 'ranking',\nIF(t.status = 1, @previous := COALESCE(t.score,0.0), t.score) AS 'score',\nt.avg_daily_goals_reached,\nt.avg_minutes,\nt.total_daily_goals_reached,\nt.total_minutes,\nt.updated_at\nFROM teams t, (select @rownum := 0, @rank := 0, @previous := NULL) a\nWHERE t.competition_id = \#{cc.id} AND t.status = 1\nORDER BY t.score DESC\n"
    
    Team.find_by_sql(sql).each do |item|
      items << {
        :team => {
          :row_id => unique_id_for_item('team',item.id),
          :id => item.id,
          :user_id => @user.id,
          :name => decode_entities(item.name),
          :motto => decode_entities(item.motto),
          :is_official => item.status == 1 ? true : false,
          :score => item.score.to_f,
          :avg_daily_goals_reached => item.avg_daily_goals_reached.to_f,
          :avg_minutes => item.avg_minutes.to_f,
          :total_daily_goals_reached => item.total_daily_goals_reached.to_f,
          :total_minutes => item.total_minutes.to_f,
          :rank => item.ranking,
          :rank_group => item.ranking.nil? ? nil : "#{item.ranking.to_s.rjust(5,'0')}",
          :name_group => (item.name.strip.upcase.gsub(/^THE /,'').gsub(/[^a-zA-Z0-9]/,'').first || '').gsub(/[0-9]/,'#'),
          :updated_at => item.updated_at
        }
      }
    end
  end
  respond_with(items)

rescue
  head :bad_request
end

- (Array) show

Get the team members (who’ve accepted) for a give team_id. If the request team happens to be the user’s own team, the produce stats will be available as well (such as, total_fruit_points). Otherwise, these fields will be null.

URL

/teams/:id/members [GET]

Response Codes
200 OK

Successfully retrieved all TeamMember objects

# Example response
 {
     "team": {
         "row_id": "65.team.196",
        "name": "Example Team",
         "rank_group": "00001",
         "avg_daily_goals_reached": 1,
         "name_group": "A",
         "updated_at": "2012-03-28T12:48:42Z",
         "motto": "Clever motto here",
         "rank": 1,
         "id": 196,
         "total_minutes": 205,
         "user_id": 65,
         "avg_minutes": 1.83,
         "total_daily_goals_reached": 4,
         "members": [
             {
                "team_member": {
                     "row_id": "65.team_membership.1165",
                     "total_capped_exercise_minutes": 0,
                     "user_id": 66,
                     "last_name": "Doe",
                     "team_id": 196,
                     "photo_path": "http://example.getfitonroute66.com/images/default-avatar.jpg",
                     "email": "john@doe.com",
                     "first_name": "John"
                 }
             },
             {
                 "team_member": {
                     "row_id": "65.team_membership.1162",
                     "total_capped_exercise_minutes": 1234,
                     "user_id": 67,
                     "last_name": "Doe",
                     "team_id": 196,
                     "photo_path": "http://example.getfitonroute66.com/images/default-avatar.jpg",
                     "email": "jane@doe.com",
                     "first_name": "Jane"
                 }
             },
             {
                 "team_member": {
                     "row_id": "65.team_membership.1164",
                     "total_capped_exercise_minutes": 0,
                     "user_id": 68,
                     "last_name": "Doe",
                     "team_id": 196,
                     "photo_path": "http://example.getfitonroute66.com/images/default-avatar.jpg",
                     "email": "johnny@doe.com",
                     "first_name": "Johnny"
                 }
             },
             {
                 "team_member": {
                     "row_id": "65.team_membership.1163",
                     "total_capped_exercise_minutes": 55,
                     "user_id": 69,
                     "last_name": "Doe",
                     "team_id": 196,
                     "photo_path": "http://example.getfitonroute66.com/images/default-avatar.jpg",
                     "email": "janie@doe.com",
                     "first_name": "Janie"
                 }
             }
         ],
         "score": 2.83
     }
 }

Returns:

  • (Array)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/controllers/teams_controller.rb', line 166

def show
  
  # Try to get the current competition by default
  cc = @promotion.current_competition
  team = cc.teams.find_by_id(params[:id], :conditions => "competition_id = #{cc.id}") #scope to the current competition

  items = []
  unless cc.nil?
    sql = "SELECT\nid,\nname,\nmotto,\nstatus,\navg_daily_goals_reached,\navg_minutes,\ntotal_daily_goals_reached,\ntotal_minutes,\nranking,\nscore,\nupdated_at\nFROM (SELECT\nt.id,\nt.name,\nt.motto,\nt.status,\nIF(t.status = 1, @rownum := @rownum + 1, NULL) AS 'rownum',\nIF(t.status = 1, @rank := IF(COALESCE(t.score,0.0) = @previous,@rank,@rownum), NULL) AS 'ranking',\nIF(t.status = 1, @previous := COALESCE(t.score,0.0), t.score) AS 'score',\nIF(t.status = 1, @previous := COALESCE(t.avg_daily_goals_reached,0.0), t.avg_daily_goals_reached) AS 'avg_daily_goals_reached',\nIF(t.status = 1, @previous := COALESCE(t.avg_minutes,0.0), t.avg_minutes) AS 'avg_minutes',\nIF(t.status = 1, @previous := COALESCE(t.total_daily_goals_reached,0.0), t.total_daily_goals_reached) AS 'total_daily_goals_reached',\nIF(t.status = 1, @previous := COALESCE(t.total_minutes,0.0), t.total_minutes) AS 'total_minutes',\nt.updated_at\nFROM teams t, (select @rownum := 0, @rank := 0, @previous := NULL) a\nWHERE t.competition_id = \#{cc.id} AND t.status = 1\nORDER BY t.score DESC\n) x\nWHERE x.id = \#{team.id}\n"
    
    t = Team.find_by_sql(sql).first  #find by sql always returns an array, if this fails it means we didn't find the team specified
    my_team_id = @user.team ? @user.team.id : 0

    members = []
    unless team.nil?
      sql = "SELECT\ntm.id,\ntm.user_id,\ntm.team_id,\nct.first_name,\nct.last_name,\nIF(tm.team_id = \#{my_team_id}, ct.email, NULL) AS 'email',\nIF(tm.team_id = \#{my_team_id}, COALESCE(SUM(e.capped_exercise),0), NULL) AS 'exercise_points'\nFROM team_memberships tm\nINNER JOIN contacts ct ON ct.contactable_type = 'User' AND ct.contactable_id = tm.user_id\nINNER JOIN competitions c ON c.id = tm.competition_id\nINNER JOIN trips t ON t.user_id = tm.user_id\nINNER JOIN profiles p ON p.trip_id = t.id\nINNER JOIN entries e ON e.trip_id = t.id AND e.logged_on between c.competition_starts_on AND c.competition_starts_on + interval c.length_of_competition day-1\nWHERE tm.team_id = \#{team.id} AND tm.status = 1\nGROUP by tm.user_id\nORDER BY ct.last_name, ct.first_name, tm.id\n"
      
      TeamMembership.find_by_sql(sql).each do |tm|
        path = tm.user.has_photo ? "/images/user/#{tm.id}_versioned_#{tm.user.trip.profile.photo_version}.jpg" : "/images/default-avatar.jpg"
        members << {
          :team_member => {
            :row_id => unique_id_for_item('team_membership',tm.id),
            :user_id => tm.user_id,
            :team_id => tm.team_id,
            :first_name => tm.first_name,
            :last_name => tm.last_name,
            :email => tm.email,
            :total_capped_exercise_minutes => tm.user.trip.entries.sum(:capped_exercise, :conditions => {:is_logged => true}),
            :photo_path => absolutize_path(path)
          }
        }
      end
    end
    
    item = {
        :team => {
          :row_id => unique_id_for_item('team',t.id),
          :id => t.id,
          :user_id => @user.id,
          :name => decode_entities(t.name),
          :motto => decode_entities(t.motto),
          :score => t.score.to_f,
          :avg_daily_goals_reached => t.avg_daily_goals_reached.to_f,
          :avg_minutes => t.avg_minutes.to_f,
          :total_daily_goals_reached => t.total_daily_goals_reached.to_f,
          :total_minutes => t.total_minutes.to_f,           
          :rank => t.ranking.nil? ? nil : t.ranking.to_i,
          :rank_group => t.ranking.nil? ? nil : "#{t.ranking.to_s.rjust(5,'0')}",
          :name_group => (t.name.strip.upcase.gsub(/^THE /,'').gsub(/[^a-zA-Z0-9]/,'').first || '').gsub(/[0-9]/,'#'),
          :updated_at => t.updated_at,
          :members => members
        }
      }
    
    respond_with(item)
  end
rescue
  head :bad_request
end