Skip to content

Language options #1247

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/cadet/assessments/library.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ defmodule Cadet.Assessments.Library do
field(:variant, :string, default: nil)
field(:exec_time_ms, :integer, default: 1000)
field(:globals, :map, default: %{})
field(:language_options, :map, default: %{})
embeds_one(:external, ExternalLibrary, on_replace: :update)
end

@required_fields ~w(chapter)a
@optional_fields ~w(globals variant exec_time_ms)a
@optional_fields ~w(globals variant language_options exec_time_ms)a
@required_embeds ~w(external)a

def changeset(library, params \\ %{}) do
Expand Down
31 changes: 21 additions & 10 deletions lib/cadet/jobs/xml_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ defmodule Cadet.Updater.XMLParser do

@spec process_questions(String.t()) :: {:ok, [map()]} | {:error, String.t()}
defp process_questions(xml) do
default_library = xpath(xml, ~x"//TASK/DEPLOYMENT"e)
default_grading_library = xpath(xml, ~x"//TASK/GRADERDEPLOYMENT"e)
default_library = xpath(xml, ~x"//TASK/PROGRAMMINGLANGUAGE"e)
default_grading_library = xpath(xml, ~x"//TASK/GRADERPROGRAMMINGLANGUAGE"e)

questions_params =
xml
Expand Down Expand Up @@ -270,22 +270,23 @@ defmodule Cadet.Updater.XMLParser do

@spec process_question_library(map(), any(), any()) :: map() | {:error, String.t()}
defp process_question_library(question, default_library, default_grading_library) do
library = xpath(question[:entity], ~x"./DEPLOYMENT"o) || default_library
library = xpath(question[:entity], ~x"./PROGRAMMINGLANGUAGE"o) || default_library

grading_library =
xpath(question[:entity], ~x"./GRADERDEPLOYMENT"o) || default_grading_library || library
xpath(question[:entity], ~x"./GRADERPROGRAMMINGLANGUAGE"o) || default_grading_library ||
library

if library do
question
|> Map.put(:library, process_question_library(library))
|> Map.put(:grading_library, process_question_library(grading_library))
|> Map.put(:library, parse_programming_language(library))
|> Map.put(:grading_library, parse_programming_language(grading_library))
else
{:error, "Missing DEPLOYMENT"}
{:error, "Missing PROGRAMMINGLANGUAGE"}
end
end

@spec process_question_library(any()) :: map()
defp process_question_library(library_entity) do
@spec parse_programming_language(any()) :: map()
defp parse_programming_language(library_entity) do
globals =
library_entity
|> xpath(
Expand All @@ -305,15 +306,25 @@ defmodule Cadet.Updater.XMLParser do
symbols: ~x"./SYMBOL/text()"sl
)

options_list =
library_entity
|> xpath(~x"./OPTION"el, key: ~x"./@key"s, value: ~x"./@value"s)

options_map =
Enum.reduce(options_list, %{}, fn %{key: k, value: v}, acc ->
Map.put(acc, k, v)
end)

library_entity
|> xpath(
~x"."e,
chapter: ~x"./@interpreter"i,
chapter: ~x"./@chapter"i,
exec_time_ms: ~x"./@exectime"oi,
variant: ~x"./@variant"os
)
|> Map.put(:globals, globals)
|> Map.put(:external, external)
|> Map.put(:language_options, options_map)
end

@spec process_charlist(charlist() | nil) :: String.t() | nil
Expand Down
3 changes: 2 additions & 1 deletion lib/cadet_web/helpers/assessments_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule CadetWeb.AssessmentsHelpers do
variant: :variant,
execTimeMs: :exec_time_ms,
globals: :globals,
external: &build_external_library(%{external_library: &1.external})
external: &build_external_library(%{external_library: &1.external}),
languageOptions: :language_options
})
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -347,6 +348,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -398,6 +400,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -1327,6 +1330,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down Expand Up @@ -1368,6 +1372,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"content" => &1.question.question.content,
Expand Down Expand Up @@ -1419,6 +1424,7 @@ defmodule CadetWeb.AdminGradingControllerTest do
"symbols" => &1.question.library.external.symbols
},
"execTimeMs" => &1.question.library.exec_time_ms,
"languageOptions" => %{},
"variant" => &1.question.library.variant
},
"maxXp" => &1.question.max_xp,
Expand Down
Loading