diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -21,4 +21,23 @@ end end + def site_login + begin + site = Site.find(params[:login][:site_id]) + rescue ActiveRecord::RecordNotFound + site = nil + end + if site==nil + flash[:notice] = 'Wrong site' + redirect_to :controller => 'main', :action => 'login' and return + end + if site.password == params[:login][:password] + session[:site_id] = site.id + redirect_to :controller => 'site', :action => 'index' + else + flash[:notice] = 'Wrong site password' + redirect_to :controller => 'main', :action => 'login' + end + end + end diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -3,7 +3,7 @@ SYSTEM_MODE_CONF_KEY = 'system.mode' before_filter :authenticate, :except => [:index, :login] - before_filter :check_viewability + before_filter :check_viewability, :except => [:index, :login] # # COMMENT OUT: filter in each action instead @@ -23,6 +23,24 @@ reset_session flash[:notice] = saved_notice + # + # These are for site administrator login + # + @countries = Country.find(:all) + @country_select = @countries.collect { |c| [c.name, c.id] } + + @country_select_with_all = [['Any',0]] + @countries.each do |country| + @country_select_with_all << [country.name, country.id] + end + + @site_select = [] + @countries.each do |country| + country.sites.each do |site| + @site_select << ["#{site.name}, #{country.name}", site.id] + end + end + render :action => 'login', :layout => 'empty' end diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb new file mode 100644 --- /dev/null +++ b/app/controllers/site_controller.rb @@ -0,0 +1,35 @@ +class SiteController < ApplicationController + + before_filter :site_admin_authorization + + def index + if @site.started + render :action => 'started', :layout => 'empty' + else + render :action => 'prompt', :layout => 'empty' + end + end + + def start + @site.started = true + @site.start_time = Time.new.gmtime + @site.save + redirect_to :action => 'index' + end + + protected + def site_admin_authorization + if session[:site_id]==nil + redirect_to :controller => 'main', :action => 'login' and return + end + begin + @site = Site.find(session[:site_id], :include => :country) + rescue ActiveRecord::RecordNotFound + @site = nil + end + if @site==nil + redirect_to :controller => 'main', :action => 'login' and return + end + end + +end diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -87,4 +87,5 @@ format.xml { head :ok } end end + end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -63,16 +63,21 @@ def user_title_bar(user) header = '' + time_left = '' # # if the contest is over - if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and - user.site!=nil and user.site.finished? - header = < THE CONTEST IS OVER CONTEST_OVER + end + if user.site!=nil + time_left = ". Time left: #{Time.at(user.site.time_left).gmtime.strftime("%X")}" + end end # @@ -94,7 +99,9 @@ #{user.full_name}
-Current time is #{format_short_time(Time.new.gmtime)} UTC
+Current time is #{format_short_time(Time.new.gmtime)} UTC +#{time_left} +
APIO'08 diff --git a/app/helpers/site_helper.rb b/app/helpers/site_helper.rb new file mode 100644 --- /dev/null +++ b/app/helpers/site_helper.rb @@ -0,0 +1,2 @@ +module SiteHelper +end diff --git a/app/models/site.rb b/app/models/site.rb --- a/app/models/site.rb +++ b/app/models/site.rb @@ -9,6 +9,24 @@ end end + def time_left + contest_time = Configuration['contest.time_limit'] + if tmatch = /(\d+):(\d+)/.match(contest_time) + h = tmatch[1].to_i + m = tmatch[2].to_i + finish_time = self.start_time + h.hour + m.minute + current_time = Time.now.gmtime + + if current_time > finish_time + return current_time - current_time + else + finish_time - current_time + end + else + nil + end + end + def finished? if !self.started return false diff --git a/app/views/main/login.html.haml b/app/views/main/login.html.haml new file mode 100644 --- /dev/null +++ b/app/views/main/login.html.haml @@ -0,0 +1,56 @@ +%h1= Configuration['ui.front.title'] + +%b= Configuration['ui.front.welcome_message'] +%br/ +Please login to see the problem list. +%br/ +%br/ + +- if flash[:notice] + %hr/ + %b= flash[:notice] + %hr/ + +%div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"} + - form_tag :controller => 'login', :action => 'login' do + %table + %tr + %td{:align => "right"} Login: + %td= text_field_tag 'login' + %tr + %td{:align => "right"} Password: + %td= password_field_tag + = submit_tag 'Login' + +%br/ + +%script{:type => 'text/javascript'} + var siteList = new Array(); + - @countries.each do |country| + = "siteList[#{country.id}] = new Array();" + - country.sites.each do |site| + = "siteList[#{country.id}][#{site.id}] = \"#{site.name}\";" + + var allSiteList = new Array(); + - @site_select.each do |sel| + = "allSiteList[#{sel[1]}]=\"#{sel[0]}\";" + +%script{:type => 'text/javascript', :src => '/javascripts/site_update.js'} + +%div{ :style => "border: solid 1px gray; padding: 2px; background: #f0f0f0;"} + %b For Site Administrator. + %br/ + Please select your country and site and login. + .login-box + - form_for :login, nil, :url => {:controller => 'login', :action => 'site_login'} do |f| + Country: + = select :site_country, :id, @country_select_with_all, {}, {:onchange => "updateSiteList();", :onclick => "updateSiteList();" } + Site: + = select :login, :site_id, @site_select + %br/ + Password: + = f.password_field :password + = submit_tag "Site Administrator Login" + +%script{:type => 'text/javascript'} + updateSiteList(); diff --git a/app/views/main/login.rhtml b/app/views/main/login.rhtml deleted file mode 100644 --- a/app/views/main/login.rhtml +++ /dev/null @@ -1,25 +0,0 @@ -

<%= Configuration['ui.front.title'] %>

- -<%= Configuration['ui.front.welcome_message'] %>
-Please login to see the problem list.

- -<% if flash[:notice] %> -
-<%= flash[:notice] %> -
-<% end %> - -
-<% form_tag :controller => 'login', :action => 'login' do %> - - - - - - - -
Login:<%= text_field_tag 'login' %>
Password:<%= password_field_tag %>
- <%= submit_tag 'Login' %> -<% end %> -
- diff --git a/app/views/site/prompt.html.haml b/app/views/site/prompt.html.haml new file mode 100644 --- /dev/null +++ b/app/views/site/prompt.html.haml @@ -0,0 +1,22 @@ +%h2 + Contest Administration for site: + = "#{@site.name}, #{@site.country.name}" + + +Current time at the server is += "#{format_short_time(Time.new.gmtime)} UTC" +(please += link_to 'refresh', :action => 'index' +to update) +%br/ +%br/ + +- form_tag :action => 'start' do + When you're ready, you can click the button below to start the contest. + %br/ + Please make sure that the contestants are ready. + After the contest is started, it cannot be paused or stopped. + %br/ + = submit_tag 'Start the Contest.' + + diff --git a/app/views/site/started.html.haml b/app/views/site/started.html.haml new file mode 100644 --- /dev/null +++ b/app/views/site/started.html.haml @@ -0,0 +1,29 @@ +%h2 + Contest Administration for site: + = "#{@site.name}, #{@site.country.name}" + +- curr_time = Time.new.gmtime + +- if @site.finished? + %h3 Contest ended. +- else + %h3 Contest started. + +Current time at the server is += "#{format_short_time(curr_time)} UTC" +(please += link_to 'refresh', :action => 'index' +to update) +%br/ +%br/ + +The contest at your site has been started at += "#{format_short_time(@site.start_time)} UTC" + +%br/ + +%h3 + Time left: + = "#{Time.at(@site.time_left).gmtime.strftime("%X")}" + + diff --git a/config/routes.rb b/config/routes.rb --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,5 @@ ActionController::Routing::Routes.draw do |map| map.resources :announcements - map.resources :sites # The priority is based upon order of creation: first created -> highest priority. diff --git a/public/javascripts/site_update.js b/public/javascripts/site_update.js new file mode 100644 --- /dev/null +++ b/public/javascripts/site_update.js @@ -0,0 +1,32 @@ +function updateSiteList() { + currentCountry = document.getElementById("site_country_id").value; + + sites = siteList[currentCountry]; + siteSelect = document.getElementById("login_site_id"); + old_len = siteSelect.length; + // clear old box + for(i=0; i