Implements registrations over time chart

This commit is contained in:
Chrisbr 2014-05-31 13:23:41 +02:00
parent c3d3440745
commit 70c7f78024
11 changed files with 355 additions and 47 deletions

View file

@ -7,19 +7,29 @@ class CallForPapers < ActiveRecord::Base
validates_presence_of :start_date, :end_date, :hard_deadline
validates :rating, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 10 }
def self.max_weeks
all = CallForPapers.all
result = [0]
all.each do |cfp|
result.push(cfp.end_week - cfp.start_week)
end
result.max
##
# Calculates how many weeks the call for paper is.
#
# ====Returns
# * +Integer+ -> start week
def weeks
result = end_week - start_week + 1
weeks = Date.new(start_week.year, 12, 31).strftime('%W').to_i
result < 0 ? result + weeks : result
end
##
# Calculates the end week of the cfp
#
# ====Returns
def start_week
start_date.strftime('%W').to_i
end
##
# Calculates the end week of the cfp
#
# ====Returns
def end_week
end_date.strftime('%W').to_i
end

View file

@ -126,31 +126,87 @@ class Conference < ActiveRecord::Base
##
# Returns an array with the summarized event submissions per week.
# ====Params:
# * +weeks+ -> Integer with number of weeks. This is necessary to compare conferences.
#
# ====Returns
# * +Array+ -> e.g. [0, 3, 3, 5] -> first week 0 events, second week 3 events.
def get_submissions_per_week(weeks)
def get_submissions_per_week
result = []
if call_for_papers && events
submissions = events.select('id, created_at').group_by { |t| t.week }
start_week = call_for_papers.start_week
sum = 0
result = calculate_items_per_week(start_week, submissions)
end
result.empty? ? [0] : result
end
(0..weeks - 1).each do |week|
if submissions["#{week + start_week}"]
sum += submissions["#{week + start_week}"].length
end
result.push(sum)
end
else
result = Array.new(weeks, 0)
##
# Returns an array with the summarized registrations per week.
#
# ====Returns
# * +Array+ -> e.g. [0, 3, 3, 5] -> first week 0, second week 3 registrations
def get_registrations_per_week
result = []
if registrations &&
registration_start_date &&
registration_end_date
reg = registrations.select('id, created_at').group_by { |r| r.week }
start_week = get_registration_start_week
result = calculate_items_per_week(start_week, reg)
end
result.empty? ? [0] : result
end
##
# Calculates how many weeks the registration is.
#
# ====Returns
# * +Integer+ -> start week
def registration_weeks
result = 0
weeks = 0
if registration_start_date && registration_end_date
weeks = Date.new(registration_start_date.year, 12, 31).
strftime('%W').to_i
result = get_registration_end_week - get_registration_start_week + 1
end
result < 0 ? result + weeks : result
end
##
# Calculates how many weeks call for papers is.
#
# ====Returns
# * +Integer+ -> weeks
def cfp_weeks
result = 0
if call_for_papers
result = call_for_papers.weeks
end
result
end
##
# Calculates the end week of the registration
#
# ====Returns
# * +Integer+ -> start week
def get_registration_start_week
registration_start_date.strftime('%W').to_i
end
##
# Calculates the start week of the registration
#
# ====Returns
# * +Integer+ -> start week
def get_registration_end_week
registration_end_date.strftime('%W').to_i
end
##
# Checks if the conference is pending.
#
@ -189,4 +245,25 @@ class Conference < ActiveRecord::Base
self.guid = guid
end
##
# Calculates items per week from a hash.
#
# ====Returns
# * +Array+ -> e.g. [0, 3, 3, 5] -> first week 0, second week 3 registrations
def calculate_items_per_week(start_week, items)
sum = 0
result = []
items.each_with_index do |(key, value), index|
# Padding
if index == 0
result = Array.new(key - start_week, 0)
elsif key > (index + start_week)
result += Array.new(key - (start_week + index + 1), sum)
end
sum += value.length
result.push(sum)
end
result
end
end

View file

@ -175,7 +175,7 @@ class Event < ActiveRecord::Base
end
def week
created_at.strftime('%W')
created_at.strftime('%W').to_i
end
private

View file

@ -33,4 +33,8 @@ class Registration < ActiveRecord::Base
delegate :tshirt, :to => :person
alias_attribute :other_needs, :other_special_needs
def week
created_at.strftime('%W').to_i
end
end