Skip to content

Commit b34adc0

Browse files
committed
Add infinite loop research prompt
1 parent cbd4ce7 commit b34adc0

File tree

8 files changed

+136
-5
lines changed

8 files changed

+136
-5
lines changed

lib/cadet/accounts/course_registration.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Cadet.Accounts.CourseRegistration do
1010
schema "course_registrations" do
1111
field(:role, Role)
1212
field(:game_states, :map)
13+
field(:agreed_to_research, :boolean)
1314

1415
belongs_to(:group, Group)
1516
belongs_to(:user, User)
@@ -19,7 +20,7 @@ defmodule Cadet.Accounts.CourseRegistration do
1920
end
2021

2122
@required_fields ~w(user_id course_id role)a
22-
@optional_fields ~w(game_states group_id)a
23+
@optional_fields ~w(game_states group_id agreed_to_research)a
2324

2425
def changeset(course_registration, params \\ %{}) do
2526
course_registration

lib/cadet/accounts/course_registrations.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,14 @@ defmodule Cadet.Accounts.CourseRegistrations do
183183
Repo.delete(course_reg)
184184
end
185185
end
186+
187+
def update_research_agreement(course_reg, agreed_to_research) do
188+
course_reg
189+
|> CourseRegistration.changeset(%{agreed_to_research: agreed_to_research})
190+
|> Repo.update()
191+
|> case do
192+
result = {:ok, _} -> result
193+
{:error, changeset} -> {:error, {:bad_request, full_error_messages(changeset)}}
194+
end
195+
end
186196
end

lib/cadet_web/controllers/user_controller.ex

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ defmodule CadetWeb.UserController do
103103
end
104104
end
105105

106+
def update_research_agreement(conn, %{"agreedToResearch" => agreed_to_research}) do
107+
course_reg = conn.assigns[:course_reg]
108+
109+
case CourseRegistrations.update_research_agreement(course_reg, agreed_to_research) do
110+
{:ok, %{}} ->
111+
text(conn, "OK")
112+
113+
{:error, {status, message}} ->
114+
conn
115+
|> put_status(status)
116+
|> text(message)
117+
end
118+
end
119+
106120
swagger_path :index do
107121
get("/v2/user")
108122

@@ -151,6 +165,27 @@ defmodule CadetWeb.UserController do
151165
response(200, "OK")
152166
end
153167

168+
swagger_path :update_research_agreement do
169+
put("/v2/courses/:course_id/user/research_agreement")
170+
summary("Update the user's agreement to the anonymized collection of programs for research")
171+
security([%{JWT: []}])
172+
consumes("application/json")
173+
174+
parameters do
175+
course_id(:path, :integer, "the user's course id", required: true)
176+
177+
agreedToResearch(
178+
:body,
179+
:boolean,
180+
"whether the user has agreed to participate in the research",
181+
required: true
182+
)
183+
end
184+
185+
response(200, "OK")
186+
response(400, "Bad Request")
187+
end
188+
154189
def swagger_definitions do
155190
%{
156191
IndexInfo:
@@ -234,6 +269,11 @@ defmodule CadetWeb.UserController do
234269
Schema.ref(:UserGameStates),
235270
"States for user's game, including users' game progress, settings and collectibles.\n"
236271
)
272+
273+
agreed_to_research(
274+
:boolean,
275+
"Whether the user as agreed to participate in the collection of anonymized data for research purposes."
276+
)
237277
end
238278
end,
239279
CourseConfiguration:

lib/cadet_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ defmodule CadetWeb.Router do
9191

9292
get("/user", UserController, :get_course_reg)
9393
put("/user/game_states", UserController, :update_game_states)
94+
put("/user/research_agreement", UserController, :update_research_agreement)
9495

9596
get("/config", CoursesController, :index)
9697
end

lib/cadet_web/views/user_view.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ defmodule CadetWeb.UserView do
8383
story: :story,
8484
playStory: :play_story?
8585
}),
86-
gameStates: latest.game_states
86+
gameStates: latest.game_states,
87+
agreedToResearch: latest.agreed_to_research
8788
}
8889
end
8990
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Cadet.Repo.Migrations.AddResearchAgreementToggle do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:course_registrations) do
6+
add(:agreed_to_research, :boolean, null: true)
7+
end
8+
end
9+
end

test/cadet/accounts/course_registration_test.exs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ defmodule Cadet.Accounts.CourseRegistrationTest do
4343
course2: course2
4444
} do
4545
assert_changeset(%{user_id: user1.id, course_id: course2.id, role: :admin}, :valid)
46-
assert_changeset(%{user_id: user2.id, course_id: course1.id, role: :student}, :valid)
46+
47+
assert_changeset(
48+
%{user_id: user2.id, course_id: course1.id, role: :student, agreed_to_research: true},
49+
:valid
50+
)
4751

4852
# assert_changeset(%{user_id: user2.id, course_id: course2.id, role: :staff, group_id: group.id}, :valid)
4953
end
@@ -328,4 +332,43 @@ defmodule Cadet.Accounts.CourseRegistrationTest do
328332
CourseRegistrations.delete_course_registration(10_000)
329333
end
330334
end
335+
336+
describe "update_research_agreement" do
337+
setup do
338+
student1 = insert(:course_registration, %{role: :student})
339+
student2 = insert(:course_registration, %{role: :student, agreed_to_research: false})
340+
341+
{:ok, %{student1: student1, student2: student2}}
342+
end
343+
344+
test "succeeds when field is initially nil", %{student1: student1} do
345+
assert is_nil(
346+
CourseRegistration
347+
|> where(id: ^student1.id)
348+
|> Repo.one()
349+
|> Map.fetch!(:agreed_to_research)
350+
)
351+
352+
CourseRegistrations.update_research_agreement(student1, true)
353+
354+
assert CourseRegistration
355+
|> where(id: ^student1.id)
356+
|> Repo.one()
357+
|> Map.fetch!(:agreed_to_research) == true
358+
end
359+
360+
test "succeeds when field is initially not nil", %{student2: student2} do
361+
assert CourseRegistration
362+
|> where(id: ^student2.id)
363+
|> Repo.one()
364+
|> Map.fetch!(:agreed_to_research) == false
365+
366+
CourseRegistrations.update_research_agreement(student2, true)
367+
368+
assert CourseRegistration
369+
|> where(id: ^student2.id)
370+
|> Repo.one()
371+
|> Map.fetch!(:agreed_to_research) == true
372+
end
373+
end
331374
end

test/cadet_web/controllers/user_controller_test.exs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ defmodule CadetWeb.UserControllerTest do
8888
"xp" => 110,
8989
"maxXp" => question.max_xp,
9090
"gameStates" => %{},
91-
"story" => nil
91+
"story" => nil,
92+
"agreedToResearch" => nil
9293
},
9394
"courseConfiguration" => %{
9495
"enableAchievements" => true,
@@ -387,7 +388,8 @@ defmodule CadetWeb.UserControllerTest do
387388
"xp" => 110,
388389
"maxXp" => question.max_xp,
389390
"gameStates" => %{},
390-
"story" => nil
391+
"story" => nil,
392+
"agreedToResearch" => nil
391393
},
392394
"courseConfiguration" => %{
393395
"enableAchievements" => true,
@@ -466,5 +468,29 @@ defmodule CadetWeb.UserControllerTest do
466468
end
467469
end
468470

471+
describe "PUT /v2/courses/{course_id}/user/research_agreement" do
472+
@tag authenticate: :student
473+
test "success, updating research agreement", %{conn: conn} do
474+
user = conn.assigns.current_user
475+
course_id = conn.assigns.course_id
476+
477+
params = %{
478+
"agreedToResearch" => true
479+
}
480+
481+
assert is_nil(
482+
Repo.get_by(CourseRegistration, course_id: course_id, user_id: user.id)
483+
|> Map.fetch!(:agreed_to_research)
484+
)
485+
486+
conn
487+
|> put(build_url(course_id) <> "/research_agreement", params)
488+
|> response(200)
489+
490+
updated_cr = Repo.get_by(CourseRegistration, course_id: course_id, user_id: user.id)
491+
assert updated_cr.agreed_to_research == true
492+
end
493+
end
494+
469495
defp build_url(course_id), do: "/v2/courses/#{course_id}/user"
470496
end

0 commit comments

Comments
 (0)