Current section

Files

Jump to
ami lib ami quiz.ex
Raw

lib/ami/quiz.ex

defmodule Ami.Quiz do
use Ecto.Schema
import Ecto.{
Query,
Changeset
}
alias Ami.{
Repo,
Lesson,
Course,
Score,
Question,
Answer
}
schema "quizzes" do
@timestamps_opts [type: :naive_datetime_usec]
field(:title, :string)
field(:is_graded, :boolean)
field(:is_pre, :boolean)
field(:is_final, :boolean)
field(:multiple_attempt, :boolean)
field(:created_at, :utc_datetime_usec)
field(:updated_at, :utc_datetime_usec)
field(:instruction, :string)
field(:submit_confirmed, :boolean, default: false, virtual: true)
belongs_to(:lesson, Lesson)
# has_one(:course, Course)
has_many(:scores, Score)
has_many(:questions, Question, on_delete: :delete_all)
has_many(:answers, through: [:questions, :answers])
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [
:title,
:is_graded,
:is_pre,
:is_final,
:created_at,
:updated_at,
:instruction,
:multiple_attempt,
:lesson_id
])
|> validate_quiz_type(params)
# |> validate_questions_answers(params)
|> cast_assoc(:questions)
end
def changeset_with_new_question(struct, params \\ %{}) do
struct
|> add_new_question
|> cast(params, [
:title,
:is_graded,
:is_pre,
:is_final,
:created_at,
:updated_at,
:instruction,
:multiple_attempt,
:lesson_id
])
|> validate_quiz_type(params)
# |> validate_questions_answers(params)
|> cast_assoc(:questions)
end
def add_new_question(struct) do
Repo.preload(struct, :questions)
ext_questions = struct.questions ++ [%Question{answers: [%Answer{}, %Answer{}, %Answer{}, %Answer{}, %Answer{}]}]
Map.replace!(struct, :questions, ext_questions)
end
def struct_for_quiz_wizard() do
%__MODULE__{
questions: [ %Question{answers: [ %Answer{}, %Answer{}, %Answer{}, %Answer{}, %Answer{} ]} ]
}
end
def correct_answers(quiz) do
Repo.preload(quiz, [:answers]).answers
|> Enum.filter(&(&1.correct_answer == true))
end
# def preload_quiz(struct, lesson_id) do
# struct
# |> Repo.preload(
# quizzes:
# from(q in __MODULE__, where: q.lesson_id == ^lesson_id)
# )
# end
@doc """
Check if choosen options between is_pre, is_graded, is_final, can_be_retaken make sense
- cannot be is_pre and is_graded
- cannot be is pre and is_final
"""
defp validate_quiz_type(changeset, params) do
cond do
params["is_pre"] == "true" && params["is_graded"] == "true" ->
add_error(
changeset,
:is_pre,
"A pre-assessment quiz cannot be graded."
)
params["is_pre"] == "true" && params["is_final"] == "true" ->
add_error(
changeset,
:is_pre,
"A pre-assessment quiz cannot be a final quiz."
)
params["is_pre"] == "true" && params["is_final"] == "true" && params["is_graded"] == "true" ->
add_error(
changeset,
:is_pre,
"A pre-assessment quiz cannot be a graded and a final quiz."
)
true ->
changeset
end
end
@doc """
"""
def validate_questions_answers(changeset, %{"id" => _} = params) do
Enum.each(params["questions"], fn ({idx, %{"answer_type" => answ_type, "answers" => answers_map, "id" => question_id}}) ->
case answ_type do
"0" ->
question =
Repo.get!(Question, question_id)
|> Repo.preload([:answers])
current_correct_answer =
Enum.find(question.answers, fn answ -> answ.correct_answer end)
curr_correctansw_param =
Enum.find(answers_map, fn {idx, answer_map} -> String.to_integer(answer_map["id"]) == current_correct_answer.id end)
maybe_new_correctansws =
Enum.filter(answers_map, fn({idx, %{"correct_answer" => correct_answ, "id" => answ_id}}) -> correct_answ == "true" end)
if length(maybe_new_correctansws) > 0 do
case length(maybe_new_correctansws) do
x = 1 ->
# Compare the existing correct answer with the potential new ones
changeset
end
end
end
end)
changeset
end
def validate_questions_answers(changeset, params) do
changeset
end
end