-
Notifications
You must be signed in to change notification settings - Fork 482
/
home_controller.rb
137 lines (118 loc) · 4.52 KB
/
home_controller.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
class HomeController < ApplicationController
include UsersHelper
before_action :authenticate_user!, only: :gallery_activities
# Don't require an authenticity token on set_locale because we post to that
# action from publicly cached page without a valid token. The worst case impact
# is that an attacker could change a user's language if they fooled them into
# clicking on a link.
skip_before_action :verify_authenticity_token, only: 'set_locale'
# The terms_and_privacy page gets loaded in an iframe on the signup page, so skip
# clearing the sign up tracking variables
skip_before_action :clear_sign_up_session_vars, only: [:terms_and_privacy]
def set_locale
set_locale_cookie(params[:locale]) if params[:locale]
if params[:i18npath]
redirect_to "/#{params[:i18npath]}"
elsif params[:user_return_to]
redirect_to URI.parse(params[:user_return_to].to_s).path
else
redirect_to '/'
end
rescue URI::InvalidURIError
redirect_to '/'
end
def home_insert
if current_user
render 'index', layout: false, formats: [:html]
else
render text: ''
end
end
def health_check
render text: 'healthy!'
end
GALLERY_PER_PAGE = 5
# Signed in student, with an assigned course/script: redirect to course overview page
# Note: the student will be redirected to the course or script in which they
# most recently made progress, which may not be an assigned course or script.
# Signed in student or teacher, without an assigned course/script: redirect to /home
# Signed out: redirect to /courses
def index
if current_user
if current_user.student? && current_user.assigned_course_or_script? && !account_takeover_in_progress? && current_user.primary_script
# Send students in course experiments (such as the subgoals experiment)
# to the right place when they end up on the wrong version of their script.
#
# In the future, when primary_script selects the script the student is
# assigned to rather then where they last made progress, this check can
# be removed.
alternate_script = current_user.primary_script.alternate_script(current_user)
if alternate_script
redirect_to script_path(alternate_script)
return
end
redirect_to script_path(current_user.primary_script)
else
redirect_to '/home'
end
else
clear_takeover_session_variables
redirect_to '/courses'
end
end
# Signed in: render home page
# Signed out: redirect to sign in
def home
authenticate_user!
init_homepage
render 'home/index'
end
def gallery_activities
if current_user
@gallery_activities =
current_user.gallery_activities.order(id: :desc).page(params[:page]).per(GALLERY_PER_PAGE)
end
render partial: 'home/gallery_content'
end
def certificate_link_test
render 'certificate_link_test', formats: [:html]
end
# This static page combines TOS and Privacy partials all in one page
# for easy printing.
def terms_and_privacy
render partial: 'home/tos_and_privacy'
end
# This static page contains the teacher announcements for US and non-US visitors.
def teacher_announcements
render template: 'api/teacher_announcement', layout: false
end
private
def init_homepage
@is_english = request.language == 'en'
if current_user
@gallery_activities =
current_user.gallery_activities.order(id: :desc).page(params[:page]).per(GALLERY_PER_PAGE)
@force_race_interstitial = params[:forceRaceInterstitial]
@force_school_info_interstitial = params[:forceSchoolInfoInterstitial]
@sections = current_user.sections.map(&:summarize)
@student_sections = current_user.sections_as_student.map(&:summarize)
# Students and teachers will receive a @top_course for their primary
# script, so we don't want to include that script (if it exists) in the
# regular lists of recent scripts.
exclude_primary_script = true
@recent_courses = current_user.recent_courses_and_scripts(exclude_primary_script)
script = current_user.primary_script
if script
script_level = current_user.next_unpassed_progression_level(script)
end
if script && script_level
@top_course = {
assignableName: data_t_suffix('script.name', script[:name], 'title'),
lessonName: script_level.stage.localized_title,
linkToOverview: script_path(script),
linkToLesson: script_next_path(script, 'next')
}
end
end
end
end