-
Notifications
You must be signed in to change notification settings - Fork 56
Implement Assessment.QuestionType entities #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
57e9d86
Implement question types
indocomsoft 1ea3562
Add tests
indocomsoft a7bf83f
Add library to ProgrammingQuestion
indocomsoft 7c27cfc
Remove debug output and mix format
indocomsoft 9b9662d
Fix according to code review
indocomsoft 2ff4f04
Merge branch 'master' into question-types
indocomsoft d65c3b6
Merge branch 'master' into question-types
indocomsoft bc26b81
Merge branch 'master' into question-types
indocomsoft 95df5f1
Bypass coveralls
indocomsoft d488816
Comment out assessments context to allow merging
indocomsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
43
lib/cadet/assessments/question_types/programming_question.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
19
test/cadet/assessments/question_types/mcq_question_test.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
31 changes: 31 additions & 0 deletions
31
test/cadet/assessments/question_types/programming_question_test.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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