-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathstatic_controller.rb
More file actions
90 lines (76 loc) · 3.06 KB
/
Copy pathstatic_controller.rb
File metadata and controls
90 lines (76 loc) · 3.06 KB
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
# The controller for actions related to home page
class StaticController < ApplicationController
skip_before_action :authenticate_user!, :authenticate_user_from_token!
def privacy; end
def home
@hide_search_box = true
@resources = []
if TeSS::Config.solr_enabled
enabled = []
enabled.append(Event) if feature_enabled?('events')
enabled.append(Material) if feature_enabled?('materials')
enabled.append(Collection) if feature_enabled?('collections')
enabled.each do |resource|
@resources += resource.search_and_filter(nil, '', { 'max_age' => '1 month' },
sort_by: 'new', per_page: 5).results
end
end
@resources = @resources.sort_by(&:created_at).reverse
@content_providers = set_content_providers
@featured_trainer = set_featured_trainer
@events = set_upcoming_events
@materials = set_latest_materials
@count_strings = set_count_strings
end
def showcase
@container_class = 'showcase-container container-fluid'
end
def set_featured_trainer
return nil unless TeSS::Config.site.dig('home_page', 'featured_trainer')
srand(Date.today.beginning_of_day.to_i)
trainers = Trainer.joins(:user).order(:id)
f_trainers = trainers.where.not(users: { image_file_size: nil })
trainers = f_trainers.exists? ? f_trainers : trainers
trainers.sample(TeSS::Config.site.dig('home_page', 'featured_trainer'))
end
def set_content_providers
ContentProvider
.from_verified_users
.where.not(image_file_size: nil)
.sample(24)
end
def set_latest_materials
n_materials = TeSS::Config.site.dig('home_page', 'latest_materials')
return [] unless n_materials
Material.search_and_filter(
nil,
'',
{},
sort_by: 'new',
per_page: 10 * n_materials
)&.results&.group_by(&:content_provider_id)&.map { |_p_id, p_materials| p_materials&.first }&.first(n_materials)
end
def set_upcoming_events
n_events = TeSS::Config.site.dig('home_page', 'upcoming_events')
return [] unless n_events
Event.search_and_filter(
nil,
'',
{ 'start' => "#{Date.tomorrow.beginning_of_day}/" },
sort_by: 'early',
per_page: 5 * n_events
).results.group_by(&:content_provider_id).map { |_p_id, p_events| p_events.first }.first(n_events)
end
def set_count_strings
count_strings = {}
return count_strings if Space.current_space.default? && !TeSS::Config.site.dig('home_page', 'counters')
count_strings['events'] = Space.current_space.events.where.not(end: nil).where('events.end > ?', Time.zone.now).count
count_strings['last_month_events'] = Space.current_space.events.where('events.created_at > ?', 1.month.ago).count
count_strings['materials'] = Space.current_space.materials.count
count_strings['workflows'] = Space.current_space.workflows.count
count_strings['learning_paths'] = Space.current_space.learning_paths.count
count_strings['content_providers'] = ContentProvider.count
count_strings['trainers'] = Trainer.count
count_strings
end
end