Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
fix score
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Ceolin committed Jul 30, 2024
1 parent 580a32f commit 46cb051
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 48 deletions.
40 changes: 15 additions & 25 deletions lib/content/course_live/lesson_play.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,31 @@ defmodule ZoonkWeb.Live.LessonPlay do

@impl Phoenix.LiveView
def handle_event("next", %{"selected_option" => selected_option}, socket) when is_nil(socket.assigns.selected_option) do
%{current_user: user, lesson: lesson, current_step: step, step_start: step_start} = socket.assigns
%{current_step: step} = socket.assigns

step_duration = DateTime.diff(DateTime.utc_now(), step_start, :second)
option_id = String.to_integer(selected_option)
selected_option = get_option(step.options, option_id)

attrs = %{
user_id: user.id,
correct: boolean_to_integer(selected_option.correct?),
total: 1,
option_id: option_id,
lesson_id: lesson.id,
step_id: step.id,
duration: step_duration
}

case Content.add_user_selection(attrs) do
{:ok, _} ->
socket =
socket
|> maybe_play_sound_effect(selected_option.correct?)
|> assign(:selected_option, selected_option)

{:noreply, socket}
socket =
socket
|> maybe_play_sound_effect(selected_option.correct?)
|> assign(:selected_option, selected_option)

{:error, _} ->
{:noreply, put_flash(socket, :error, dgettext("courses", "Unable to select option"))}
end
{:noreply, socket}
end

def handle_event("next", params, socket) do
%{current_user: user, lesson: lesson, current_step: current_step, step_start: step_start} = socket.assigns
%{current_user: user, lesson: lesson, current_step: current_step, step_start: step_start, selected_option: selected_option} = socket.assigns
step_duration = DateTime.diff(DateTime.utc_now(), step_start, :second)
next_step = Content.get_next_step(lesson, current_step.order)

attrs = %{
user_id: user.id,
correct: 1,
correct: get_correct_value(selected_option),
total: 1,
lesson_id: lesson.id,
step_id: current_step.id,
option_id: get_selected_option_id(selected_option),
answer: [params["answer"]],
duration: step_duration
}
Expand Down Expand Up @@ -113,6 +97,12 @@ defmodule ZoonkWeb.Live.LessonPlay do

defp get_option(options, option_id), do: Enum.find(options, &(&1.id == option_id))

defp get_selected_option_id(nil), do: nil
defp get_selected_option_id(selected_option), do: selected_option.id

defp get_correct_value(nil), do: 1
defp get_correct_value(selected_option), do: boolean_to_integer(selected_option.correct?)

defp user_selected_wrong_option?(%StepOption{correct?: false} = selected, option) when selected.id == option.id, do: true
defp user_selected_wrong_option?(_selected, _option), do: false

Expand Down
67 changes: 44 additions & 23 deletions test/content/play_view_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ defmodule ZoonkWeb.PlayViewLiveTest do
assert_first_step(lv, lessons)
assert_second_step(lv, lessons)
assert_third_step(lv, lessons)
assert_fourth_step(conn, lv, lessons, course)
assert_fourth_step(lv, lessons)
assert_fifth_step(conn, lv, lessons, course)
end

test "returns an error if trying to play a lesson from another course", %{conn: conn, course: course} do
Expand Down Expand Up @@ -215,20 +216,33 @@ defmodule ZoonkWeb.PlayViewLiveTest do
assert has_element?(lv, ~s|input[id="select-option-#{first_option.id}"]|)
refute has_element?(lv, "textarea")

lv |> form(@select_form, %{selected_option: get_correct_option(step.options)}) |> render_submit()
lv |> form(@select_form, %{selected_option: get_incorrect_option(step.options)}) |> render_submit()

assert has_element?(lv, ~s|div[role="alert"] h3:fl-icontains("well done!")|)
assert has_element?(lv, ~s|div[role="alert"] h4:fl-icontains("feedback 2!")|)
end

# credo:disable-for-next-line Credo.Check.Refactor.ABCSize
defp assert_second_step(lv, lessons) do
lv |> form(@select_form) |> render_submit()

refute has_element?(lv, ~s|blockquote p:fl-icontains("step 1!")|)
assert has_element?(lv, ~s|blockquote p:fl-icontains("step 2!")|)
assert has_element?(lv, ~s|button *:fl-icontains("confirm")|)

step = Enum.at(lessons, 1)
file_url = Storage.get_url(step.image)

assert has_element?(lv, ~s|img[src="#{file_url}"]|)
end

# credo:disable-for-next-line Credo.Check.Refactor.ABCSize
defp assert_third_step(lv, lessons) do
lv |> form(@select_form) |> render_submit()

refute has_element?(lv, ~s|blockquote p:fl-icontains("step 1!")|)
refute has_element?(lv, ~s|blockquote p:fl-icontains("step 2!")|)
assert has_element?(lv, ~s|blockquote p:fl-icontains("step 3!")|)
assert has_element?(lv, ~s|button *:fl-icontains("confirm")|)

step = Enum.at(lessons, 2)
first_option = hd(step.options)

assert has_element?(lv, ~s|input[id="select-option-#{first_option.id}"]|)
Expand All @@ -238,57 +252,64 @@ defmodule ZoonkWeb.PlayViewLiveTest do
assert has_element?(lv, ~s|div[role="alert"] h4:fl-icontains("feedback 2!")|)
end

defp assert_third_step(lv, lessons) do
# credo:disable-for-next-line Credo.Check.Refactor.ABCSize
defp assert_fourth_step(lv, lessons) do
lv |> form(@select_form) |> render_submit()

refute has_element?(lv, ~s|blockquote p:fl-icontains("step 1!")|)
refute has_element?(lv, ~s|blockquote p:fl-icontains("step 2!")|)
assert has_element?(lv, ~s|blockquote p:fl-icontains("step 3!")|)
refute has_element?(lv, ~s|blockquote p:fl-icontains("step 3!")|)
assert has_element?(lv, ~s|blockquote p:fl-icontains("step 4!")|)
assert has_element?(lv, ~s|button *:fl-icontains("confirm")|)

step = Enum.at(lessons, 2)
file_url = Storage.get_url(step.image)
step = Enum.at(lessons, 3)
first_option = hd(step.options)

assert has_element?(lv, ~s|img[src="#{file_url}"]|)
assert has_element?(lv, ~s|input[id="select-option-#{first_option.id}"]|)

lv |> form(@select_form, %{selected_option: get_correct_option(step.options)}) |> render_submit()

assert has_element?(lv, ~s|div[role="alert"] h3:fl-icontains("well done!")|)
end

defp assert_fourth_step(conn, lv, lessons, course) do
defp assert_fifth_step(conn, lv, lessons, course) do
lv |> form(@select_form) |> render_submit()

assert has_element?(lv, ~s|blockquote p:fl-icontains("step 4!")|)
assert has_element?(lv, ~s|blockquote p:fl-icontains("step 5!")|)
assert has_element?(lv, ~s|button *:fl-icontains("next step")|)

step = Enum.at(lessons, 3)
step = Enum.at(lessons, 4)

assert {:ok, updated_lv, _html} =
lv
|> form(@select_form)
|> render_submit()
|> follow_redirect(conn, ~p"/c/#{course.slug}/#{step.lesson_id}/completed")

assert has_element?(updated_lv, "h1", "Good!")
assert has_element?(updated_lv, "span", "7.5")
assert has_element?(updated_lv, "p", "You got 3 out of 4 answers right.")
assert has_element?(updated_lv, "h1", "Not bad!")
assert has_element?(updated_lv, "span", "6.0")
assert has_element?(updated_lv, "p", "You got 3 out of 5 answers right.")
end

defp generate_steps(lesson) do
Enum.each(1..4, fn order ->
Enum.each(1..5, fn order ->
content = "step #{order}!"
image = if order == 3, do: "img.png"
kind = if order == 4, do: :readonly, else: :quiz
image = if order == 2, do: "img.png"
kind = if order == 5, do: :readonly, else: :quiz

step = lesson_step_fixture(%{lesson_id: lesson.id, kind: kind, content: content, image: image, order: order})

# Make sure it works even when the last step doesn't have options.
unless order == 4, do: generate_options(step, order)
unless order == 5, do: generate_options(step, order)
end)
end

defp generate_options(_step, 3), do: nil
defp generate_options(_step, 2), do: nil

defp generate_options(step, _step_order) do
Enum.each(1..4, fn order ->
Enum.each(1..5, fn order ->
image = if order == 2, do: "img.png"
feedback = unless order == 1, do: "feedback #{order}!"
feedback = unless order == 4, do: "feedback #{order}!"
correct? = order == 1 or order == 4

step_option_fixture(%{lesson_step_id: step.id, correct?: correct?, image: image, feedback: feedback, title: "option #{order}!"})
Expand Down

0 comments on commit 46cb051

Please sign in to comment.