Schedule divisions and workshops table

This commit is contained in:
Godwin
2017-08-20 16:04:39 -07:00
parent c1f9b5db82
commit ad3136a5cb
13 changed files with 614 additions and 280 deletions
+90 -44
View File
@@ -1,5 +1,5 @@
class ApplicationController < BaseController
protect_from_forgery with: :exception, :except => [:do_confirm, :js_error, :admin_update]
protect_from_forgery with: :exception, except: [:do_confirm, :js_error, :admin_update]
before_filter :application_setup
after_filter :capture_page_info
@@ -392,13 +392,13 @@ class ApplicationController < BaseController
def get_scheule_data(do_analyze = true)
conference = @this_conference || @conference
@meals = Hash[(conference.meals || {}).map{ |k, v| [k.to_i, v] }].sort.to_h
@events = Event.where(:conference_id => conference.id).order(start_time: :asc)
@workshops = Workshop.where(:conference_id => conference.id).order(start_time: :asc)
@events = Event.where(conference_id: conference.id).order(start_time: :asc)
@workshops = Workshop.where(conference_id: conference.id).order(start_time: :asc)
@locations = {}
get_block_data
@schedule = {}
schedule = {}
day_1 = conference.start_date.wday
@workshop_blocks.each_with_index do |info, block|
@@ -407,11 +407,11 @@ class ApplicationController < BaseController
day_diff += 7 if day_diff < 0
day = (conference.start_date + day_diff.days).to_date
time = info['time'].to_f
@schedule[day] ||= { times: {}, locations: {} }
@schedule[day][:times][time] ||= {}
@schedule[day][:times][time][:type] = :workshop
@schedule[day][:times][time][:length] = info['length'].to_f
@schedule[day][:times][time][:item] = { block: block, workshops: {} }
schedule[day] ||= { times: {}, locations: {} }
schedule[day][:times][time] ||= {}
schedule[day][:times][time][:type] = :workshop
schedule[day][:times][time][:length] = info['length'].to_f
schedule[day][:times][time][:item] = { block: block, workshops: {} }
end
end
@@ -423,9 +423,9 @@ class ApplicationController < BaseController
day_diff += 7 if day_diff < 0
day = (conference.start_date + day_diff.days).to_date
if block.present? && @schedule[day].present? && @schedule[day][:times].present? && @schedule[day][:times][block['time'].to_f].present?
@schedule[day][:times][block['time'].to_f][:item][:workshops][workshop.event_location_id] = { workshop: workshop, status: { errors: [], warnings: [], conflict_score: nil } }
@schedule[day][:locations][workshop.event_location_id] ||= workshop.event_location if workshop.event_location.present?
if block.present? && schedule[day].present? && schedule[day][:times].present? && schedule[day][:times][block['time'].to_f].present?
schedule[day][:times][block['time'].to_f][:item][:workshops][workshop.event_location_id] = { workshop: workshop, status: { errors: [], warnings: [], conflict_score: nil } }
schedule[day][:locations][workshop.event_location_id] ||= workshop.event_location if workshop.event_location.present?
end
end
end
@@ -433,50 +433,50 @@ class ApplicationController < BaseController
@meals.each do |time, meal|
day = meal['day'].to_date
time = meal['time'].to_f
@schedule[day] ||= {}
@schedule[day][:times] ||= {}
@schedule[day][:times][time] ||= {}
@schedule[day][:times][time][:type] = :meal
@schedule[day][:times][time][:length] = (meal['length'] || 1.0).to_f
@schedule[day][:times][time][:item] = meal
schedule[day] ||= {}
schedule[day][:times] ||= {}
schedule[day][:times][time] ||= {}
schedule[day][:times][time][:type] = :meal
schedule[day][:times][time][:length] = (meal['length'] || 1.0).to_f
schedule[day][:times][time][:item] = meal
end
@events.each do |event|
if event.present? && event.start_time.present? && event.end_time.present?
day = event.start_time.midnight.to_date
time = event.start_time.hour.to_f + (event.start_time.min / 60.0)
@schedule[day] ||= {}
@schedule[day][:times] ||= {}
@schedule[day][:times][time] ||= {}
@schedule[day][:times][time][:type] = :event
@schedule[day][:times][time][:length] = (event.end_time - event.start_time) / 3600.0
@schedule[day][:times][time][:item] = event
schedule[day] ||= {}
schedule[day][:times] ||= {}
schedule[day][:times][time] ||= {}
schedule[day][:times][time][:type] = :event
schedule[day][:times][time][:length] = (event.end_time - event.start_time) / 3600.0
schedule[day][:times][time][:item] = event
end
end
@schedule = @schedule.sort.to_h
@schedule.each do |day, data|
@schedule[day][:times] = data[:times].sort.to_h
schedule = schedule.sort.to_h
schedule.each do |day, data|
schedule[day][:times] = data[:times].sort.to_h
end
@schedule.each do |day, data|
schedule.each do |day, data|
last_event = nil
data[:times].each do |time, time_data|
if last_event.present?
@schedule[day][:times][last_event][:next_event] = time
schedule[day][:times][last_event][:next_event] = time
end
last_event = time
end
@schedule[day][:num_locations] = (data[:locations] || []).size
schedule[day][:num_locations] = (data[:locations] || []).size
end
@schedule.deep_dup.each do |day, data|
schedule.deep_dup.each do |day, data|
data[:times].each do |time, time_data|
if time_data[:next_event].present? || time_data[:length] > @this_conference.schedule_interval
span = @this_conference.schedule_interval
length = time_data[:next_event].present? ? time_data[:next_event] - time : time_data[:length]
while span < length
@schedule[day][:times][time + span] ||= {
schedule[day][:times][time + span] ||= {
type: (span >= time_data[:length] ? :empty : :nil),
length: @this_conference.schedule_interval
}
@@ -486,16 +486,20 @@ class ApplicationController < BaseController
end
end
@schedule = @schedule.sort.to_h
# schedule = schedule.sort.to_h
@schedule.each do |day, data|
@schedule[day][:times] = data[:times].sort.to_h
@schedule[day][:locations] ||= {}
schedule.each do |day, data|
# @schedule[day] = [{}]
# division = 0
# schedule[day][:num_locations] = schedule[day][:num_locations]
# schedule[day][:times] = data[:times].sort.to_h
schedule[day][:times] = data[:times].sort.to_h
schedule[day][:locations] ||= {}
# # sort the locations by name
schedule[day][:locations] = schedule[day][:locations].sort_by { |event_id, event| event.present? ? event.title.downcase : '' }.to_h
# sort the locations by name
@schedule[day][:locations] = @schedule[day][:locations].sort_by { |event_id, event| event.present? ? event.title.downcase : '' }.to_h
# add an empty block if no workshops are scheduled on this day yet
@schedule[day][:locations][0] = :add if do_analyze || @schedule[day][:locations].empty?
# # add an empty block if no workshops are scheduled on this day yet
# schedule[day][:locations][0] = :add if do_analyze || schedule[day][:locations].empty?
if do_analyze
data[:times].each do |time, time_data|
@@ -512,8 +516,8 @@ class ApplicationController < BaseController
workshop_i.active_facilitators.each do |facilitator_i|
workshop_j.active_facilitators.each do |facilitator_j|
if facilitator_i.id == facilitator_j.id
@schedule[day][:times][time][:status] ||= {}
@schedule[day][:times][time][:item][:workshops][ids[j]][:status][:errors] << {
schedule[day][:times][time][:status] ||= {}
schedule[day][:times][time][:item][:workshops][ids[j]][:status][:errors] << {
name: :common_facilitator,
facilitator: facilitator_i,
workshop: workshop_i,
@@ -533,7 +537,7 @@ class ApplicationController < BaseController
amenities = JSON.parse(location.amenities || '[]').map &:to_sym
needs.each do |need|
@schedule[day][:times][time][:item][:workshops][ids[i]][:status][:errors] << {
schedule[day][:times][time][:item][:workshops][ids[i]][:status][:errors] << {
name: :need_not_available,
need: need,
location: location,
@@ -555,13 +559,55 @@ class ApplicationController < BaseController
end
end
@schedule[day][:times][time][:item][:workshops][ids[i]][:status][:conflict_score] = (interests & (workshop_i.interested.map { | u | u.user_id })).length
schedule[day][:times][time][:item][:workshops][ids[i]][:status][:conflict_score] = (interests & (workshop_i.interested.map { | u | u.user_id })).length
end
end
end
end
end
end
@schedule = {}
schedule.sort.to_h.each do |day, data|
@schedule[day] = []
division = 0
# @schedule[day][division] = data
locations = nil
@schedule[day][division] = {}
@schedule[day][division][:times] = {}
# @schedule[day][division][:times] = data[:times]
# # sort the locations by name
# @schedule[day][:locations] = schedule[day][:locations].sort_by { |event_id, event| event.present? ? event.title.downcase : '' }.to_h
# # add an empty block if no workshops are scheduled on this day yet
# schedule[day][:locations][0] = :add if do_analyze || schedule[day][:locations].empty?
# last_time_data = nil
data[:times].each do |time, time_data|
if time_data[:type] == :workshop && time_data[:item].present? && time_data[:item][:workshops].present?
if !locations.nil? && ((locations.keys - time_data[:item][:workshops].keys) | (time_data[:item][:workshops].keys - locations.keys)).length > 0
# data[:locations]
# xxx
@schedule[day][division][:locations] = locations.deep_dup
@schedule[day][division][:locations][0] = :add if do_analyze || locations.empty?
locations = data[:locations].select { |id, l| time_data[:item][:workshops][id].present? }
division += 1
@schedule[day][division] = {}
@schedule[day][division][:times] = {}
else
locations = data[:locations].select { |id, l| time_data[:item][:workshops][id].present? }
end
end
# last_time_data = time_data
@schedule[day][division][:times][time] = time_data
end
locations ||= data[:locations]
@schedule[day][division][:locations] = locations
@schedule[day][division][:locations][0] = :add if do_analyze || locations.empty?
@schedule[day][division][:num_locations] = locations.length
end
end
protected
@@ -253,46 +253,7 @@ class ConferenceAdministrationController < ApplicationController
format.xlsx { render xlsx: '../conferences/stats', filename: "stats-#{DateTime.now.strftime('%Y-%m-%d')}" }
end
else
if params[:sort_column]
col = params[:sort_column].to_sym
@excel_data[:data].sort_by! do |row|
value = row[col]
if row[:raw_values].key?(col)
value = if row[:raw_values][col].is_a?(TrueClass)
't'
elsif row[:raw_values][col].is_a?(FalseClass)
''
else
row[:raw_values][col]
end
elsif value.is_a?(City)
value = value.sortable_string
end
if value.nil?
case @excel_data[:column_types][col]
when :datetime, [:date, :day]
value = Date.new
when :money
value = 0
else
value = ''
end
end
value
end
if params[:sort_dir] == 'up'
@sort_dir = :up
@excel_data[:data].reverse!
end
@sort_column = col
else
@sort_column = :name
end
sort_data(params[:sort_column], params[:sort_dir], :name)
end
@registration_count = @registrations.size
@@ -324,6 +285,22 @@ class ConferenceAdministrationController < ApplicationController
end
end
def administrate_workshops
get_workshops(true)
if request.format.xlsx?
logger.info "Generating stats.xls"
return respond_to do |format|
format.xlsx { render xlsx: '../conferences/stats', filename: "workshops-#{DateTime.now.strftime('%Y-%m-%d')}" }
end
else
sort_data(params[:sort_column], params[:sort_dir], :name)
end
if request.xhr?
render html: view_context.html_table(@excel_data, view_context.registrations_table_options)
end
end
def administrate_check_in
sort_weight = {
checked_in: 5,
@@ -769,6 +746,179 @@ class ConferenceAdministrationController < ApplicationController
end
end
def get_workshops(html_format = false, id = nil, conference = @this_conference)
@workshops = conference.workshops.sort_by { |w| w.title.downcase }
@excel_data = {
columns: [
:title,
:owner,
:locale,
:date,
:info,
:notes,
:facilitators
] + User.AVAILABLE_LANGUAGES.map { |l| "language_#{l}".to_sym } +
Workshop.all_needs.map { |n| "need_#{n}".to_sym } + [
:theme,
:space
] + (User.AVAILABLE_LANGUAGES - [I18n.locale]).map { |l| "title_#{l}".to_sym } +
(User.AVAILABLE_LANGUAGES - [I18n.locale]).map { |l| "info_#{l}".to_sym },
column_types: {
title: :bold,
date: :datetime,
info: :text,
notes: :text,
owner: :email
},
keys: {
title: 'forms.labels.generic.title',
owner: 'roles.workshops.facilitator.creator',
locale: 'articles.conference_registration.terms.Preferred_Languages',
info: 'forms.labels.generic.info',
date: 'workshop.created_at',
notes: 'forms.labels.generic.notes',
facilitators: 'roles.workshops.facilitator.facilitator',
theme: 'articles.workshops.headings.theme',
space: 'articles.workshops.headings.space'
},
data: []
}
@excel_data[:key_vars] = {}
User.AVAILABLE_LANGUAGES.each do |l|
@excel_data[:keys]["language_#{l}".to_sym] = "languages.#{l}"
if l != I18n.locale
@excel_data[:keys]["title_#{l}".to_sym] = 'translate.content.item_translation'
@excel_data[:key_vars]["title_#{l}".to_sym] = { language: view_context.language_name(l), item: I18n.t('forms.labels.generic.title') }
@excel_data[:keys]["info_#{l}".to_sym] = 'translate.content.item_translation'
@excel_data[:key_vars]["info_#{l}".to_sym] = { language: view_context.language_name(l), item: I18n.t('forms.labels.generic.info') }
@excel_data[:column_types]["info_#{l}".to_sym] = :text
end
end
Workshop.all_needs.each do |n|
@excel_data[:keys]["need_#{n}".to_sym] = "workshop.options.needs.#{n}"
end
@workshops.each do |w|
if w.present?
if id.nil? || id == w.id
owner = User.find(w.creator)
facilitators = w.collaborators.map { |f| User.find(f) }
data = {
id: w.id,
title: w.title,
info: view_context.strip_tags(w.info),
notes: view_context.strip_tags(w.notes),
owner: owner.name,
locale: w.locale.present? ? (view_context.language_name w.locale) : '',
date: w.created_at ? w.created_at.strftime("%F %T") : '',
facilitators: facilitators.map { |f| f.name }.join(', '),
theme: w.theme && Workshop.all_themes.include?(w.theme.to_sym) ? I18n.t("workshop.options.theme.#{w.theme}") : w.theme,
space: w.space && Workshop.all_spaces.include?(w.space.to_sym) ? I18n.t("workshop.options.space.#{w.space}") : '',
raw_values: {
info: w.info,
owner: owner.email,
notes: w.notes,
locale: w.locale,
facilitators: facilitators.map { |f| f.email }.join(', '),
theme: w.theme,
space: w.space
},
html_values: {
}
}
languages = JSON.parse(w.languages || '[]').map &:to_sym
User.AVAILABLE_LANGUAGES.each do |l|
in_language = ((languages || []).include? l.to_sym)
data["language_#{l}".to_sym] = (in_language ? I18n.t('articles.conference_registration.questions.bike.yes') : '')
data[:raw_values]["language_#{l}".to_sym] = in_language
if l != I18n.locale
data["title_#{l}".to_sym] = w.get_column_for_locale!(:title, l, false)
data["info_#{l}".to_sym] = view_context.strip_tags(w.get_column_for_locale!(:info, l, false))
data[:raw_values]["info_#{l}".to_sym] = w.get_column_for_locale!(:info, l, false)
end
end
needs = JSON.parse(w.needs || '[]').map &:to_sym
Workshop.all_needs.each do |n|
in_need = ((needs || []).include? n.to_sym)
data["need_#{n}".to_sym] = (in_need ? I18n.t('articles.conference_registration.questions.bike.yes') : '')
data[:raw_values]["need_#{n}".to_sym] = in_need
end
@excel_data[:data] << data
end
end
end
if html_format
@column_options = {
locale: I18n.backend.enabled_locales.map { |l| [(view_context.language_name l), l] },
theme: Workshop.all_themes.map { |t| [I18n.t("workshop.options.theme.#{t}"), t] },
space: Workshop.all_spaces.map { |s| [I18n.t("workshop.options.space.#{s}"), s] }
}
@column_options[:theme] += ((conference.workshops.map { |w| w.theme }) - Workshop.all_themes.map(&:to_s)).uniq.map { |t| [t, t] }
User.AVAILABLE_LANGUAGES.each do |l|
@column_options["language_#{l}".to_sym] = [
[I18n.t("articles.conference_registration.questions.bike.yes"), true]
]
end
Workshop.all_needs.each do |n|
@column_options["need_#{n}".to_sym] = [
[I18n.t("articles.conference_registration.questions.bike.yes"), true]
]
end
end
end
def sort_data(col, sort_dir, default_col)
if col
col = col.to_sym
@excel_data[:data].sort_by! do |row|
value = row[col]
if row[:raw_values].key?(col)
value = if row[:raw_values][col].is_a?(TrueClass)
't'
elsif row[:raw_values][col].is_a?(FalseClass)
''
elsif @excel_data[:column_types][col] == :text
view_context.strip_tags(row[:raw_values][col] || '').downcase
else
row[:raw_values][col]
end
elsif value.is_a?(City)
value = value.sortable_string
end
if value.nil?
case @excel_data[:column_types][col]
when :datetime, [:date, :day]
value = Date.new
when :money
value = 0
else
value = ''
end
end
value
end
if sort_dir == 'up'
@sort_dir = :up
@excel_data[:data].reverse!
end
@sort_column = col
else
@sort_column = default_col
end
end
def get_housing_data
@hosts = {}
@guests = {}
@@ -1255,6 +1405,85 @@ class ConferenceAdministrationController < ApplicationController
return nil
end
def admin_update_workshops
if params[:button] == 'update'
workshop = Workshop.where(
id: params[:key].to_i,
conference_id: @this_conference.id
).limit(1).first
params.each do |key, value|
case key.to_sym
when :owner
user = User.get(value.strip)
user_role = WorkshopFacilitator.where(user_id: user.id, workshop_id: workshop.id).first || WorkshopFacilitator.new(user_id: user.id, workshop_id: workshop.id)
owner_role = WorkshopFacilitator.where(role: :creator, workshop_id: workshop.id).first
if !owner_role || owner_role.user_id != user.id
owner_role.role = :collaborator
user_role.role = :creator
owner_role.save!
user_role.save!
end
when :facilitators
ids = []
value.split(/[\s,;]+/).each do |email|
user = User.get(email)
ids << user.id
user_role = WorkshopFacilitator.where(user_id: user.id, workshop_id: workshop.id).first || WorkshopFacilitator.new(user_id: user.id, workshop_id: workshop.id)
unless user_role.role == 'creator' || user_role.role == 'collaborator'
user_role.role = 'collaborator'
user_role.save
end
end
WorkshopFacilitator.where("workshop_id = ? AND role = ? AND user_id NOT IN (?)", workshop.id, 'collaborator', ids).destroy_all
when :title, :locale, :date, :info, :notes, :theme, :space
workshop.send("#{key}=", value.present? ? value : nil)
else
if key.start_with?('language_')
l = key.split('_').last.to_sym
languages = JSON.parse(workshop.languages || '[]').map &:to_sym
if User.AVAILABLE_LANGUAGES.include? l
if value.present?
languages |= [l]
else
languages -= [l]
end
workshop.languages = languages.to_json
end
elsif key.start_with?('need_')
n = key.split('_').last.to_sym
needs = JSON.parse(workshop.needs || '[]').map &:to_sym
if Workshop.all_needs.include? n
if value.present?
needs |= [n]
else
needs -= [n]
end
workshop.needs = needs.to_json
end
elsif key.start_with?('title_')
l = key.split('_').last.to_sym
workshop.set_column_for_locale(:title, l, value)
elsif key.start_with?('info_')
l = key.split('_').last.to_sym
workshop.set_column_for_locale(:info, l, value)
end
end
end
workshop.save!
get_workshops(true, params[:key].to_i)
options = view_context.workshops_table_options
options[:html] = true
render html: view_context.excel_rows(@excel_data, {}, options)
else
do_404
end
return nil
end
def admin_update_check_in
unless params[:button] == 'cancel'
user_id = params[:user_id]
@@ -1478,7 +1707,7 @@ class ConferenceAdministrationController < ApplicationController
end
(params[:title] || {}).each do |locale, value|
event.set_column_for_locale(:title, locale, html_value(value), current_user.id) if value != event._title(locale) && view_context.strip_tags(value).strip.present?
event.set_column_for_locale(:title, locale, html_value(value), current_user.id) if value != event._title(locale) && view_context.strip.present?
end
event.save
@@ -1555,8 +1784,18 @@ class ConferenceAdministrationController < ApplicationController
@time = @workshop_blocks[@block]['time'].to_f
@day = (Date.parse params[:day])
@location = params[:location]
@division = params[:division].to_i
@event_location = @location.present? && @location.to_i > 0 ? EventLocation.find(@location.to_i) : nil
@schedule ||= {}
@schedule[@day] ||= {}
@schedule[@day][@division] ||= []
@schedule[@day][@division][:times] ||= {}
@schedule[@day][@division][:times][@time] ||= {}
@schedule[@day][@division][:times][@time][:item] ||= {}
@schedule[@day][@division][:times][@time][:item][:workshops] || {}
@invalid_locations = @schedule[@day][@division.to_i][:times][@time][:item][:workshops].keys
@workshops.sort { |a, b| a.title.downcase <=> b.title.downcase }.each do |workshop|
@ordered_workshops[workshop.id] = workshop
end