Skip to content

Commit

Permalink
feat: add exercise-score command for learn-ocaml-client; fix: modifia…
Browse files Browse the repository at this point in the history
…ction required for server-config, logout, launch an exercise from moodle
  • Loading branch information
Fixiss committed Jun 30, 2021
1 parent 9a7dd00 commit 0570038
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
19 changes: 8 additions & 11 deletions src/main/learnocaml_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,13 @@ module Logout = struct

let logout logout_args =
let path = if logout_args.local then ConfigFile.local_path else ConfigFile.user_path in
let rm path = if (Sys.file_exists path)
then let () = Sys.remove path in
Printf.eprintf "Configuration removed to %s.\n%!" path
else Printf.eprintf "there is no file named %s.\n%!" path in
rm path;
if (Sys.file_exists path)
then
begin
Sys.remove path;
Printf.eprintf "Configuration removed from %s.\n%!" path
end
else Printf.eprintf "Cannot remove %s : no such file or directory.\n%!" path;
Lwt.return 0

let man = man "delete current configuration file."
Expand Down Expand Up @@ -1261,12 +1263,7 @@ module Exercise_score = struct
let ezjsonm = (Json_encoding.construct (assoc string)
scores)
in
let json =
match ezjsonm with
| `O _ | `A _ as json -> json
| _ -> assert false
in
Ezjsonm.to_channel ~minify:false stdout json;
Ezjsonm.value_to_channel ~minify:false stdout ezjsonm;
Lwt.return 0)
|None -> Lwt.fail_with "You must provide a token"

Expand Down
32 changes: 21 additions & 11 deletions src/server/learnocaml_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,9 @@ module Request_handler = struct
~expiration:(`Max_age (Int64.of_int 60))
~path:"/"
("token", Token.to_string token)] in
if (List.exists (fun a -> match a with
|("custom_exercise", _) -> true
|_ -> false) params)
then let exercise = List.assoc "custom_exercise" params in
lwt_ok @@ Redirect { code=`See_other; url="/exercises/"^exercise^"/#tab%3Dtext"; cookies }
else lwt_ok @@ Redirect { code=`See_other; url="/"; cookies }
match List.assoc_opt "custom_exercise" params with
| Some exercise -> lwt_ok @@ Redirect { code=`See_other; url="/exercises/"^exercise^"/#tab%3Dtext"; cookies }
| None -> lwt_ok @@ Redirect { code=`See_other; url="/"; cookies }
else
Token_index.OauthIndex.get_current_secret !sync_dir >>= fun secret ->
let hmac = generate_hmac secret csrf_token id in
Expand Down Expand Up @@ -959,23 +956,36 @@ module Request_handler = struct
| Api.Upgrade _ ->
lwt_fail (`Forbidden, "Users with passwords are disabled on this instance.")

| Api.Server_config _ when config.ServerData.use_passwd ->
respond_json cache (("use_passwd", true)::[])
| Api.Server_config _ ->
respond_json cache (("use_passwd", false)::[])
respond_json cache [("use_passwd", config.ServerData.use_passwd)]

| Api.Exercise_score token ->
Save.get token >>= fun save ->
Exercise.Index.get () >>= fun exercises ->
let results = match save with
| Some save ->
SMap.map
(fun st -> Answer.(st.grade))
save.Save.all_exercise_states
| _ -> SMap.empty in

let rec find_names exs = match exs with
| ((id,_)::tail) -> id::(find_names tail)
| _ -> [] in

let names = match exercises with
| Learnocaml_data.Exercise.Index.Groups exs -> find_names exs
| Learnocaml_data.Exercise.Index.Exercises exs -> find_names exs in

if SMap.is_empty results then
respond_json cache (("exercise1", "grade1")::("execise2", "grade2")::[])
respond_json cache []
else
respond_json cache (("exercise2", "grade2")::("execise1", "grade1")::[])
let rec grade_list exs = match exs with
| [] -> []
| ex_name::tail -> match SMap.find ex_name results with
| Some grade -> (ex_name, string_of_int (grade))::(grade_list tail)
| None -> (ex_name, "N/A")::(grade_list tail) in
respond_json cache (grade_list names)

| Api.Return _ ->
let make_cookie = Cohttp.Cookie.Set_cookie_hdr.make
Expand Down
2 changes: 1 addition & 1 deletion src/state/learnocaml_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ module Conversions (Json: JSON_CODEC) = struct

| Server_config () -> json J.(J.assoc J.bool)

| Exercise_score _ -> json (J.list J.(obj2 (req "exercise" string) (req "grade" string)))
| Exercise_score _ -> json J.(J.assoc J.string)

| Return _ -> str

Expand Down

0 comments on commit 0570038

Please sign in to comment.