Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/cadet/assessments.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Cadet.Assessments do
@moduledoc """
Assessments context contains domain logic for assessments management such as
missions, sidequests, paths, etc.
"""
use Cadet, :context

alias Cadet.Assessments.QuestionTypes.MCQQuestion
alias Cadet.Assessments.QuestionTypes.ProgrammingQuestion

# To be uncommented when assessments context is merged
# def create_multiple_choice_question(json_attr) when is_binary(json_attr) do
# %MCQQuestion{}
# |> MCQQuestion.changeset(%{raw_mcqquestion: json_attr})
# end

# def create_multiple_choice_question(attr = %{}) do
# %MCQQuestion{}
# |> MCQQuestion.changeset(attr)
# end

# def create_programming_question(json_attr) when is_binary(json_attr) do
# %ProgrammingQuestion{}
# |> ProgrammingQuestion.changeset(%{raw_programmingquestion: json_attr})
# end

# def create_programming_question(attr = %{}) do
# %ProgrammingQuestion{}
# |> ProgrammingQuestion.changeset(attr)
# end
end
24 changes: 24 additions & 0 deletions lib/cadet/assessments/question_types/library.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Cadet.Assessments.QuestionTypes.Library do
@moduledoc """
The library entity represents a library to be used in a programming question.
"""
use Ecto.Schema

import Ecto.Changeset

embedded_schema do
field(:version, :integer)
field(:globals, {:array, :string})
field(:externals, {:array, :string})
field(:files, {:array, :string})
end

@required_fields ~w(version)a
@optional_fields ~w(globals externals files)a

def changeset(library, params \\ %{}) do
library
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
25 changes: 25 additions & 0 deletions lib/cadet/assessments/question_types/mcq_choice.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Cadet.Assessments.QuestionTypes.MCQChoice do
@moduledoc """
The Assessments.QuestionTypes.MCQChoice entity represents an MCQ Choice.
"""
use Ecto.Schema

import Ecto.Changeset

alias Cadet.Assessments.QuestionTypes.MCQQuestion

embedded_schema do
field(:content, :string)
field(:hint, :string)
field(:is_correct, :boolean)
end

@required_fields ~w(content is_correct)a
@optional_fields ~w(is_correct)a

def changeset(question, params \\ %{}) do
question
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
57 changes: 57 additions & 0 deletions lib/cadet/assessments/question_types/mcq_question.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule Cadet.Assessments.QuestionTypes.MCQQuestion do
@moduledoc """
The Assessments.QuestionTypes.MCQQuestion entity represents an MCQ Question.
It comprises of content and choices.
"""
use Ecto.Schema

import Ecto.Changeset

alias Cadet.Assessments.QuestionTypes.MCQChoice

embedded_schema do
field(:content, :string)
embeds_many(:choices, MCQChoice)
field(:raw_mcqquestion, :string, virtual: true)
end

@required_fields ~w(content)a
@optional_fields ~w(raw_mcqquestion)a

def changeset(question, params \\ %{}) do
question
|> cast(params, @required_fields ++ @optional_fields)
|> put_question()
|> cast_embed(:choices, with: &MCQChoice.changeset/2, required: true)
|> validate_one_correct_answer()
|> validate_required(@required_fields ++ ~w(choices)a)
end

defp put_question(changeset) do
change = get_change(changeset, :raw_mcqquestion)

if change do
json = Poison.decode!(change)

changeset
|> cast(json, @required_fields)
else
changeset
end
end

defp validate_one_correct_answer(changeset) do
changeset
|> validate_change(:choices, fn :choices, choices ->
no_of_correct_choices =
choices
|> Enum.reduce(0, &if(&1.changes && &1.changes[:is_correct], do: &2 + 1, else: &2))

if no_of_correct_choices == 1 do
[]
else
[choices: "Number of correct answer must be one."]
end
end)
end
end
43 changes: 43 additions & 0 deletions lib/cadet/assessments/question_types/programming_question.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Cadet.Assessments.QuestionTypes.ProgrammingQuestion do
@moduledoc """
The ProgrammingQuestion entity represents a Programming question.
"""
use Ecto.Schema

import Ecto.Changeset

alias Cadet.Assessments.QuestionTypes.Library

embedded_schema do
field(:content, :string)
field(:solution_template, :string)
field(:solution_header, :string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optional field, I think it's needed only for path

field(:solution, :string)
embeds_one(:library, Library)
field(:raw_programmingquestion, :string, virtual: true)
end

@required_fields ~w(content solution_template solution)a
@optional_fields ~w(solution_header raw_programmingquestion)a

def changeset(question, params \\ %{}) do
question
|> cast(params, @required_fields ++ @optional_fields)
|> put_programmingquestion()
|> cast_embed(:library, required: true, with: &Library.changeset/2)
|> validate_required(@required_fields)
end

defp put_programmingquestion(changeset) do
change = get_change(changeset, :raw_programmingquestion)

if change do
json = Poison.decode!(change)

changeset
|> cast(json, @required_fields)
else
changeset
end
end
end
18 changes: 18 additions & 0 deletions test/cadet/assessments/question_types/mcq_choice_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Cadet.Assessments.QuestionTypes.MCQChoiceTest do
use Cadet.ChangesetCase, async: true

alias Cadet.Assessments.QuestionTypes.MCQChoice

valid_changesets MCQChoice do
%{content: "asd", is_correct: true}
%{content: "asd", hint: "asd", is_correct: true}
end

invalid_changesets MCQChoice do
%{content: "asd"}
%{hint: "asd"}
%{is_correct: false}
%{content: "asd", hint: "aaa"}
%{content: 1, is_correct: true}
end
end
19 changes: 19 additions & 0 deletions test/cadet/assessments/question_types/mcq_question_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Cadet.Assessments.QuestionTypes.MCQQuestionTest do
use Cadet.ChangesetCase, async: true

alias Cadet.Assessments.QuestionTypes.MCQQuestion

valid_changesets MCQQuestion do
%{content: "asd", choices: [%{content: "asd", is_correct: true}]}

%{
raw_mcqquestion:
"{\"content\":\"asd\",\"choices\":[{\"is_correct\":true,\"content\":\"asd\"}]}"
}
end

invalid_changesets MCQQuestion do
%{content: "asd"}
%{content: "asd", choices: [%{content: "asd", is_correct: false}]}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Cadet.Assessments.QuestionTypes.ProgrammingQuestionTest do
use Cadet.ChangesetCase, async: true

alias Cadet.Assessments.QuestionTypes.ProgrammingQuestion

valid_changesets ProgrammingQuestion do
%{
content: "asd",
solution_template: "asd",
solution: "asd",
library: %{version: 1}
}

%{
raw_programmingquestion:
"{\"solution_template\":\"asd\",\"solution_header\":\"asd\",\"solution\":\"asd\",\"content\":\"asd\",\"library\":{\"version\":1}}"
}
end

invalid_changesets ProgrammingQuestion do
%{content: "asd"}

%{
content: "asd",
solution_template: "asd",
solution_header: "asd",
solution: "asd",
library: %{globals: ["a"]}
}
end
end