forked from snap-cloud/snapcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconference.rb
1247 lines (1111 loc) · 37.9 KB
/
conference.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
# == Schema Information
#
# Table name: conferences
#
# id :bigint not null, primary key
# booth_limit :integer default(0)
# color :string
# custom_css :text
# custom_domain :string
# description :text
# end_date :date not null
# end_hour :integer default(20)
# events_per_week :text
# guid :string not null
# logo_file_name :string
# picture :string
# registration_limit :integer default(0)
# revision :integer default(0), not null
# short_title :string not null
# start_date :date not null
# start_hour :integer default(9)
# ticket_layout :integer default("portrait")
# timezone :string not null
# title :string not null
# use_vdays :boolean default(FALSE)
# use_volunteers :boolean
# use_vpositions :boolean default(FALSE)
# created_at :datetime
# updated_at :datetime
# organization_id :integer
#
# Indexes
#
# index_conferences_on_organization_id (organization_id)
#
# rubocop:disable Metrics/ClassLength
class Conference < ApplicationRecord
include RevisionCount
require 'uri'
serialize :events_per_week, Hash
# Needed to call 'Conference.with_role' in /models/ability.rb
# Dependent destroy will fail as roles#destroy will be cancelled,hence delete_all
resourcify :roles, dependent: :delete_all
default_scope { order('start_date DESC') }
scope :upcoming, -> { where('end_date >= ?', Date.current) }
scope :past, -> { where('end_date < ?', Date.current) }
belongs_to :organization
delegate :code_of_conduct, to: :organization
has_paper_trail ignore: %i[updated_at guid revision events_per_week], meta: { conference_id: :id }
has_and_belongs_to_many :questions
has_one :splashpage, dependent: :destroy
has_one :contact, dependent: :destroy
has_one :registration_period, dependent: :destroy
has_one :email_settings, dependent: :destroy
has_one :program, dependent: :destroy
has_one :venue, dependent: :destroy
delegate :city, :country_name, :rooms, to: :venue, allow_nil: true
delegate :name, :street, to: :venue, prefix: true, allow_nil: true
has_many :ticket_purchases, dependent: :destroy
has_many :physical_tickets, through: :ticket_purchases
has_many :payments, dependent: :destroy
has_many :supporters, through: :ticket_purchases, source: :user
has_many :tickets, dependent: :destroy
has_many :registration_tickets, -> { for_registration }, class_name: 'Ticket'
has_many :resources, dependent: :destroy
has_many :booths, dependent: :destroy
has_many :confirmed_booths, -> { where(state: 'confirmed') }, class_name: 'Booth'
has_many :lodgings, dependent: :destroy
has_many :registrations, dependent: :destroy
has_many :participants, through: :registrations, source: :user
has_many :vdays, dependent: :destroy
has_many :vpositions, dependent: :destroy
has_many :sponsorship_levels, -> { order('position ASC') }, dependent: :destroy
has_many :sponsors, dependent: :destroy
has_many :commercials, as: :commercialable, dependent: :destroy
has_many :subscriptions, dependent: :destroy
has_one :call_for_events, -> { where(cfp_type: 'events') }, through: :program, source: :cfps
has_one :call_for_booths, -> { where(cfp_type: 'booths') }, through: :program, source: :cfps
has_one :call_for_tracks, -> { where(cfp_type: 'tracks') }, through: :program, source: :cfps
has_many :confirmed_tracks, -> { where(state: 'confirmed') }, through: :program, source: :tracks
has_many :highlighted_events,
-> { where(state: :confirmed, is_highlight: true) },
through: :program,
source: :events
has_many :event_types, through: :program
has_many :currency_conversions
has_many :surveys, as: :surveyable, dependent: :destroy do
def for_registration
where(target: targets[:during_registration])
end
def after_conference
where(target: targets[:after_conference])
end
end
accepts_nested_attributes_for :venue
accepts_nested_attributes_for :tickets, allow_destroy: true
accepts_nested_attributes_for :sponsorship_levels, allow_destroy: true
accepts_nested_attributes_for :sponsors, allow_destroy: true
accepts_nested_attributes_for :email_settings
accepts_nested_attributes_for :questions, allow_destroy: true
accepts_nested_attributes_for :vdays, allow_destroy: true
accepts_nested_attributes_for :vpositions, allow_destroy: true
mount_uploader :picture, PictureUploader, mount_on: :logo_file_name
validates :title,
:short_title,
:start_date,
:end_date,
:start_hour,
:end_hour,
:ticket_layout,
:organization,
:timezone, presence: true
validates :short_title, uniqueness: true
validates :short_title, format: { with: /\A[a-zA-Z0-9_-]*\z/ }
validates :registration_limit, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
# This validation is needed since a conference with a start date greater than the end date is not possible
validate :valid_date_range?
validate :valid_times_range?
before_create :generate_guid
before_create :add_color
before_create :create_email_settings
after_create :create_free_ticket
after_update :delete_event_schedules
enum ticket_layout: { portrait: 0, landscape: 1 }
##
# Checks if the user is registered to the conference
#
# ====Args
# * +user+ -> The user we check for
# ====Returns
# * +false+ -> If the user is registered
# * +true+ - If the user isn't registered
def user_registered?(user)
user.present? && registrations.where(user_id: user.id).present?
end
##
# True when there is at least one ticket marked as "registration"
# A user must get a registration ticket before registering.
def registration_ticket_required?
registration_tickets.any?
end
##
# Creates a registration to the conference for the user
#
# ====Args
# * +user+ -> The user we check for
# ====Returns
# * +false+ -> If the user is registered
# * +true+ - If the user isn't registered
def register_user(user)
registration = registrations.new
registration.user = user
if registration.save
MailblusterEditLeadJob.perform_later(
user.id, add_tags: ["#{organization.name}-#{short_title}"]
)
return registration
end
false
end
##
# Delete all EventSchedules that are not in the hours range
# After the conference has been successfully updated
def delete_event_schedules
if saved_change_to_start_hour? || saved_change_to_end_hour?
event_schedules = program.event_schedules.select do |event_schedule|
event_schedule.start_time.hour < start_hour ||
event_schedule.end_time.hour > end_hour ||
(event_schedule.end_time.hour == end_hour && event_schedule.end_time.min > 0)
end
event_schedules.each(&:destroy)
end
end
##
# Checks if the registration for the conference is currently open
#
# ====Returns
# * +false+ -> If the conference dates are not set or today isn't in the
# registration period.
# * +true+ -> If today is in the registration period.
def registration_open?
registration_dates_given? &&
(registration_period.start_date..registration_period.end_date).cover?(Date.current)
end
##
# Checks if the registration dates for the conference are provided
#
# ====Returns
# * +false+ -> If the conference registration dates are not set
# * +true+ -> If conference registration dates are set
def registration_dates_given?
registration_period.present? &&
registration_period.start_date.present? &&
registration_period.end_date.present?
end
##
# Returns an array with the summarized event submissions per week.
#
# ====Returns
# * +Array+ -> e.g. [0, 3, 3, 5] -> first week 0 events, second week 3 events.
def get_submissions_per_week
result = []
if program&.cfp && program&.events
submissions = program.events.select(:week).group(:week).order(:week).count
start_week = program.cfp.start_week
weeks = program.cfp.weeks
result = calculate_items_per_week(start_week, weeks, submissions)
end
result
end
##
# Returns an hash with submitted, confirmed and unconfirmed event submissions
# per week.
#
# ====Returns
# * +Array+ -> e.g. 'Submitted' => [0, 3, 3, 5] -> first week 0 events, second week 3 events.
def get_submissions_data
return [] unless program&.cfp && program&.events
start_week = program.cfp.start_week
get_events_per_week_by_state.collect do |state, values|
if state == 'Submitted'
{
name: 'Submitted',
data: add_week_indices(pad_array_left_kumulative(start_week, values))
}
else
{
name: state,
data: add_week_indices(pad_array_left_not_kumulative(start_week, values))
}
end
end
end
##
# 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
return [] unless registrations &&
registration_period &&
registration_period.start_date &&
registration_period.end_date
reg = registrations.group(:week).order(:week).count
start_week = get_registration_start_week
weeks = registration_weeks
calculate_items_per_week(start_week, weeks, reg)
end
##
# Returns an array with the summarized ticket sales per week.
#
# ====Returns
# * +Array+ -> e.g. [0, 3, 3, 5] -> first week 0, second week 3 tickets sold
def get_tickets_sold_per_week
return [] unless tickets && ticket_purchases && registration_period
tickets_sold = ticket_purchases.paid.group(:week).sum(:quantity)
start_week = get_registration_start_week
weeks = registration_weeks
calculate_items_per_week(start_week, weeks, tickets_sold)
end
##
# Returns an hash with ticket sales by ticket title
# per week.
#
# ====Returns
# * +Array+ -> e.g. 'Free Access' => [0, 3, 3, 5] -> first week 0 tickets sold, second week 3 tickets sold.
def get_tickets_data
return [] unless tickets && ticket_purchases && registration_period
tickets_per_ticket_id_and_week = ticket_purchases.paid.group(:ticket_id, :week).sum(:quantity)
start_week = get_registration_start_week
weeks = registration_weeks
tickets_by_id_per_week = {}
tickets.each do |ticket|
tickets_by_id_per_week[ticket.id] = {}
(start_week...(start_week + weeks)).each do |week|
tickets_by_id_per_week[ticket.id][week] = 0
end
end
tickets_per_ticket_id_and_week.each do |ticket_week, value|
tickets_by_id_per_week[ticket_week[0]][ticket_week[1]] = value
end
tickets_by_id_per_week.collect do |ticket, values|
{
name: Ticket.find(ticket).title,
data: add_week_indices(pad_array_left_not_kumulative(start_week, values))
}
end
end
##
# Calculates how many weeks the registration is.
#
# ====Returns
# * +Integer+ -> start week
def registration_weeks
result = 0
weeks = 0
if registration_period&.start_date &&
registration_period&.end_date
weeks = Date.new(registration_period.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
if program
program.cfp.weeks
else
0
end
end
##
# Calculates the end week of the registration
#
# ====Returns
# * +Integer+ -> start week
def get_registration_start_week
if registration_period
registration_period.start_date.strftime('%W').to_i
else
-1
end
end
##
# Calculates the start week of the registration
#
# ====Returns
# * +Integer+ -> start week
def get_registration_end_week
if registration_period
registration_period.end_date.strftime('%W').to_i
else
-1
end
end
##
# Checks if the conference is pending.
#
# ====Returns
# * +false+ -> If the conference start date is in the past.
# * +true+ -> If the conference start date is in the future.
def pending?
start_date > Date.today
end
##
# Returns a hash with booleans with the required conference options.
#
# ====Returns
# * +hash+ -> true -> filled / false -> missing
def get_status
result = {
registration: registration_date_set?,
cfp: cfp_set?,
venue: venue_set?,
rooms: rooms_set?,
tracks: tracks_set?,
event_types: event_types_set?,
difficulty_levels: difficulty_levels_set?,
splashpage: splashpage&.public?
}
result.update(
process: calculate_setup_progress(result),
short_title: short_title
).with_indifferent_access
end
##
# Returns a hash with user => submissions ordered by submissions for all conferences
#
# ====Returns
# * +hash+ -> user: submissions
def self.get_top_submitter(limit = 5)
submitter = EventUser.select(:user_id).where('event_role = ?', 'submitter').limit(limit).group(:user_id)
counter = submitter.order('count_all desc').count(:all)
calculate_user_submission_hash(submitter, counter)
end
##
# Returns a hash with user => submissions ordered by submissions
#
# ====Returns
# * +hash+ -> user: submissions
def get_top_submitter(limit = 5)
submitter = EventUser.joins(:event).select(:user_id)
.where('event_role = ? and program_id = ?', 'submitter', Conference.find(id).program.id)
.limit(limit).group(:user_id)
counter = submitter.order('count_all desc').count(:all)
Conference.calculate_user_submission_hash(submitter, counter)
end
##
# Returns a hash with event state => {value: count of event states, color: color}.
# The result is calculated over all conferences.
#
# ====Returns
# * +hash+ -> hash
def self.event_distribution
calculate_event_distribution_hash(Event.select(:state).group(:state).count)
end
##
# Returns a hash with event state => {value: count of event states, color: color}
#
# ====Returns
# * +hash+ -> hash
def event_distribution
Conference.calculate_event_distribution_hash(
program.events.select(:state).group(:state).count
)
end
##
# Returns a hash with scheduled vs unscheduled events
# { "Scheduled" => { value: number of confirmed and scheduled events, color: color },
# "Unscheduled" => { value: number of confirmed and unscheduled events, color: color }
#
# ====Returns
# * +hash+ -> hash
def scheduled_event_distribution
confirmed_scheduled_events = program.events.confirmed.scheduled(program.selected_schedule.try(:id))
confirmed_unscheduled_events = program.events.confirmed - confirmed_scheduled_events
scheduled_value = { 'value' => confirmed_scheduled_events.count, 'color' => 'green' }
unscheduled_value = { 'value' => confirmed_unscheduled_events.count, 'color' => 'red' }
{ 'Scheduled' => scheduled_value, 'Unscheduled' => unscheduled_value }
end
##
# Returns a hash with Registration attended vs. Registration not attended
# { "Attended" => { value: number of registration attended, color: color },
# "Not attended" => { value: number of registration not attended, color: color }
#
# ====Returns
# * +hash+ -> hash
def registration_distribution
reg = registrations.includes(:user)
attended_value = { 'value' => reg.where(attended: true).count, 'color' => 'magenta' }
not_attended_value = { 'value' => reg.where.not(attended: true).count, 'color' => 'blue' }
{ 'Attended' => attended_value, 'Not attended' => not_attended_value }
end
##
# Returns a hash with affiliation =>
# {value: count of registration whose user has that affilation, color: color}
# In case that the affiliation is blank, it groups them in None and
# if the number of persons that have an affiliation are less than the 2% of
# the total number of registered people, they are grouped in others.
#
# ====Returns
# * +hash+ -> hash
def affiliation_distribution
counted_affiliations = registrations.joins(:user).group(:affiliation).count
result = {}
i = 1
others = 0
none = 0
counted_affiliations.each do |key, value|
if value < 0.02 * registrations.length
others += value
elsif key.blank?
none += value
else
result[key.capitalize] = { 'value' => value, 'color' => next_color(i) }
i += 1
end
end
if others > 0
result['Others'] = { 'value' => others, 'color' => next_color(i) }
i += 1
end
result['None'] = { 'value' => none, 'color' => next_color(i) } if none > 0
result
end
##
# Returns a hash with per ticket sales => { "Title" => { value: number of tickets sold,
# color: generated from the title using a hash function }, ...}
#
# ====Returns
# * +hash+ -> hash
def tickets_sold_distribution
result = {}
if tickets && ticket_purchases
tickets.each do |ticket|
result[ticket.title] = {
'value' => ApplicationController.helpers.humanized_money(ticket.tickets_sold).delete(',').to_i,
'color' => "\##{Digest::MD5.hexdigest(ticket.title)[0..5]}"
}
end
end
result
end
##
# Returns a hash with per ticket turnover => { "Title" => { value: ticket turnover,
# color: generated from the title using a hash function }, ...}
#
# ====Returns
# * +hash+ -> hash
def tickets_turnover_distribution
result = {}
if tickets && ticket_purchases
tickets.each do |ticket|
result[ticket.title] = {
'value' => ApplicationController.helpers.humanized_money(ticket.tickets_turnover_total(ticket.id)).delete(',').to_i,
'color' => "\##{Digest::MD5.hexdigest(ticket.title)[0..5]}"
}
end
end
result
end
##
# Calculates the overall program minutes
#
# ====Returns
# * +hash+ -> Fixnum minutes
def current_program_minutes
events_grouped = program.events.select(:event_type_id).group(:event_type_id)
events_counted = events_grouped.count
calculate_program_minutes(events_grouped, events_counted)
end
##
# Calculates the overall program hours
#
# ====Returns
# * +Fixnum+ -> Fixnum hours. Example: 1.5 gets rounded to 2. 1.3 gets rounded 1.
def current_program_hours
(current_program_minutes / 60.to_f).round
end
##
# Calculates the overall program minutes since date
#
# ====Returns
# * +hash+ -> Fixnum minutes
def new_program_minutes(date)
events_grouped = program.events.select(:event_type_id).where('created_at > ?', date).group(:event_type_id)
events_counted = events_grouped.count
calculate_program_minutes(events_grouped, events_counted)
end
##
# Calculates the overall program hours since date
#
# ====Returns
# * +Fixnum+ -> Fixnum hours
def new_program_hours(date)
(new_program_minutes(date) / 60.to_f).round
end
##
# Calculates the difficulty level distribution from all events.
#
# ====Returns
# * +hash+ -> difficulty level => {color, value}
def difficulty_levels_distribution(state = nil)
calculate_event_distribution(:difficulty_level_id, :difficulty_level, state)
end
##
# Calculates the event_type distribution from all events.
#
# ====Returns
# * +hash+ -> event_type => {color, value}
def event_type_distribution(state = nil)
calculate_event_distribution(:event_type_id, :event_type, state)
end
##
# Calculates the track distribution from all events.
#
# ====Returns
# * +hash+ -> track => {color, value}
def tracks_distribution(state = nil)
tracks_grouped = if state
program.events.select(:track_id).where('state = ?', state).group(:track_id)
else
program.events.select(:track_id).group(:track_id)
end
tracks_counted = tracks_grouped.count
calculate_track_distribution_hash(tracks_grouped, tracks_counted)
end
##
# Return all pending conferences. If there are no pending conferences, the last two
# past conferences are returned
#
# ====Returns
# * +ActiveRecord+
def self.get_active_conferences_for_dashboard
result = Conference.where('start_date > ?', Time.now)
.select('id, short_title, color, start_date, organization_id')
if result.empty?
result = Conference
.select('id, short_title, color, start_date, organization_id').limit(2)
.order(start_date: :desc)
end
result
end
##
# Return all conferences minus the active conferences
#
# ====Returns
# * +ActiveRecord+
def self.get_conferences_without_active_for_dashboard(active_conferences)
result = Conference.select('id, short_title, color, start_date, organization_id').order(start_date: :desc)
result - active_conferences
end
##
# A list with the three event states submitted, confirmed, unconfirmed with corresponding colors
#
# ====Returns
# * +List+
def self.get_event_state_line_colors
[
{ short_title: 'Submitted', color: 'blue' },
{ short_title: 'Confirmed', color: 'green' },
{ short_title: 'Unconfirmed', color: 'orange' }
]
end
##
# Writes an snapshot of the actual event distribution to the database
# Triggered each every Sunday 11:55 pm form whenever (config/schedule.rb).
#
def self.write_event_distribution_to_db
week = DateTime.now.end_of_week
Conference.where('end_date > ?', Date.today).find_each do |conference|
result = {}
Event.state_machine.states.each do |state|
count = conference.program.events.where('state = ?', state.name).count
result[state.name] = count
end
conference.events_per_week = {} unless conference.events_per_week
# Write to database
conference.events_per_week[week] = result
conference.save!
end
end
##
# Checks if conference is updated for email notifications.
#
# ====Returns
# * +True+ -> If conference is updated and all other parameters are set
# * +False+ -> Either conference is not updated or one or more parameter is not set
def notify_on_dates_changed?
return false unless email_settings.send_on_conference_dates_updated
# do not notify unless one of the dates changed
return false unless saved_change_to_start_date? || saved_change_to_end_date?
# do not notify unless the mail content is set up
email_settings.conference_dates_updated_subject.present? && email_settings.conference_dates_updated_body.present?
end
##
# Checks if registration dates are updated for email notifications.
#
# ====Returns
# * +True+ -> If registration dates is updated and all other parameters are set
# * +False+ -> Either registration date is not updated or one or more parameter is not set
def notify_on_registration_dates_changed?
return false unless email_settings.send_on_conference_registration_dates_updated
# do not notify unless we allow a registration
return false unless registration_period
# do not notify unless one of the dates changed
return false unless registration_period.saved_change_to_start_date? || registration_period.saved_change_to_end_date?
# do not notify unless the mail content is set up
email_settings.conference_registration_dates_updated_subject.present? && email_settings.conference_registration_dates_updated_body.present?
end
##
# Checks if the registration limit has been exceeded
# Additionally, it takes into account the confirmed speakers that haven't registered yet
#
# ====Returns
# * +True+ -> If the registration limit has been reached or exceeded
# * +False+ -> If the registration limit hasn't been exceeded
def registration_limit_exceeded?
registration_limit > 0 && registrations.count + program.speakers.confirmed.unregistered(program.conference).count >= registration_limit
end
# Returns an hexadecimal color given a collection. The returned color changed
# when the number of element in the collection changes and for consecutive
# number of elements it returns highly different colors.
def next_color_for_collection(collection)
# we have different start indices for every collection to generate a
# different color for every of them.
start_index = {
tracks: (program.tracks.count + 1),
levels: (program.difficulty_levels.count + 51),
types: (program.event_types.count + 101)
}
next_color(start_index[collection])
end
# Returns the current day if it is a day of the schedule or nil otherwise
def current_conference_day
day = Time.find_zone(timezone).today
day if (start_date..end_date).cover? day
end
# Returns the number of hours since the conference start hour (9) to the
# current hour, in case that the current hour is beetween the start and the
# end hour (20). Otherwise, returns 0
def hours_from_start_time(start_hour, end_hour)
current_time = Time.find_zone(timezone).now
current_hour = current_time.strftime('%H').to_i
(start_hour..(end_hour - 1)).cover?(current_hour) ? current_hour - start_hour : 0
end
##
#
# ====Returns
# * +True+ -> if accepted booths are equal to the booth limit
# * +False+ -> Accepted booths have not reached the booth limit
def maximum_accepted_booths?
booth_limit > 0 && booths.accepted.count + booths.confirmed.count >= booth_limit
end
##
# Return the current conference object to be used in RevisionCount
#
# ====Returns
# * +ActiveRecord+
def conference
self
end
def to_param
short_title
end
##
# Returns true or false, if the event is already over or not
#
# ====Returns
# * +true+ -> If the event is over
# * +false+ -> If the event is not over yet
def ended?
end_date < Time.current
end
private
# Returns a different html colour for every i and consecutive colors are
# clearly different.
def next_color(i)
'#' + next_color_component(:r, i) + next_color_component(:g, i) + next_color_component(:b, i)
end
# Auxiliar function which is used in next_color and returns each component of
# the color. We make use of big prime numbers to avoid repetition and to make
# consecutive colors clearly different.
def next_color_component(component, i)
big_prime_numbers = { r: 113, g: 67, b: 151 }
(((i * big_prime_numbers[component]) % 239) + 16).to_s(16)
end
after_create do
create_contact
create_program
create_roles
end
##
# Creates free ticket for the conference
# after the conference has been successfully created
# Will create 1 new record for 'free' ticket
def create_free_ticket
tickets.where(title: 'Free Access',
price_cents: 0).first_or_create!(description: 'Get free access tickets for the conference.')
end
##
# Creates the roles of the conference
# after the conference has been successfully created
# Will create 4 new records for roles
def create_roles
Role.where(name: 'organizer',
resource: self).first_or_create(description: 'For the organizers of the conference (who shall have full access)')
Role.where(name: 'cfp', resource: self).first_or_create(description: 'For the members of the CfP team')
Role.where(name: 'info_desk', resource: self).first_or_create(description: 'For the members of the Info Desk team')
Role.where(name: 'volunteers_coordinator',
resource: self).first_or_create(description: 'For the people in charge of volunteers')
end
##
# Checks if start date of the conference is greater than the end date
#
# Reports an error when such a condition is found
def valid_date_range?
errors.add(:start_date, 'is greater than End date') if start_date && end_date && start_date > end_date
end
##
# Checks if start hour of the conference is greater or equal than the end hour
# and that both hours are beetween 0 and 24
#
# Reports an error when such a condition is found
def valid_times_range?
if start_hour && end_hour
errors.add(:start_hour, 'is lower than 0') if start_hour < 0
errors.add(:end_hour, 'is lower or equal than start hour') if end_hour <= start_hour
errors.add(:end_hour, 'is greater than 24') if end_hour > 24
end
end
##
# Calculates the weeks from a start and a end week.
#
# ====Returns
# * +Fixnum+ -> weeks
def weeks(start_week, end_week)
weeks = end_week - start_week + 1
weeks_of_year = Date.new(start_date.year, 12, 31).strftime('%W').to_i
weeks < 0 ? weeks + weeks_of_year : weeks
end
##
# Returns a Hash with the events with the state confirmend / unconfirmed per week.
#
# ====Returns
# * +Hash+ -> e.g. 'Confirmed' => { 3 => 5, 4 => 6 }
def get_events_per_week_by_state
result = {
'Submitted' => {},
'Confirmed' => {},
'Unconfirmed' => {}
}
# Completed weeks
events_per_week.each do |week, values|
week = Date.parse(week) unless week.respond_to?(:strftime)
values.each do |state, value|
next unless %i[confirmed unconfirmed].include?(state)
result[state.to_s.capitalize] = {} unless result[state.to_s.capitalize]
result[state.to_s.capitalize][week.strftime('%W').to_i] = value
end
end
# Actual week
this_week = Date.today.end_of_week.strftime('%W').to_i
result['Confirmed'][this_week] = program.events.where('state = ?', :confirmed).count
result['Unconfirmed'][this_week] = program.events.where('state = ?', :unconfirmed).count
result['Submitted'] = program.events.select(:week).group(:week).count
result['Submitted'][this_week] = program.events.where(week: this_week).count
result
end
##
# Returns an array from the hash values with left padding.
#
# ====Returns
# * +Array+ -> [0, 0, 1, 2, 3, 0, 0]
def pad_array_left_not_kumulative(start_week, hash)
hash = assert_keys_are_continuously(hash)
first_week = hash.keys[0]
left = pad_left(first_week, start_week)
left + hash.values
end
##
# Returns an array from the hash values with left padding.
#
# ====Returns
# * +Array+ -> [0, 0, 1, 2, 3, 3, 3]
def pad_array_left_kumulative(start_week, hash)
hash = assert_keys_are_continuously(hash)
result = cumulative_sum(hash.values)
first_week = hash.keys[0]
left = pad_left(first_week, start_week)
left + result
end
##
# Cumulative sums an array.
#
# ====Returns
# * +Array+ -> [1, 2, 3, 4] --> [1, 3, 7, 11]
def cumulative_sum(array)
sum = 0
array.map { |x| sum += x }
end
##
# Returns the left padding.
#
# ====Returns
# * +Array+
def pad_left(first_week, start_week)
left = []
left = Array.new(first_week - start_week - 1, 0) if first_week > start_week
left
end
##
# Asserts that all keys in the hash are continuously.
# If not, the missing key is inserted with value 0.
#
# ====Returns
# * +Hash+ { 1 => 1, 2 => 0, 3 => 0, 4 => 3 }
def assert_keys_are_continuously(hash)
keys = hash.keys
(keys.min..keys.max).each do |key|
hash[key] = 0 unless hash[key]
end
hash.sort.to_h
end
##
# Returns the progress of the set up conference list in percent
#
# ====Returns
# * +String+ -> Progress in Percent
def calculate_setup_progress(result)
(100 * result.values.count(true) / result.values.count).to_s
end
##
# Checks if there is a difficulty level.
#
# ====Returns
# * +True+ -> One difficulty level or more
# * +False+ -> No diffculty level
def difficulty_levels_set?
program.difficulty_levels.count > 0
end
##
# Checks if there is a difficulty level.
#
# ====Returns
# * +True+ -> One difficulty level or more