From 0570038e10d0f8cd9aa3207d8b809a13ac43912e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Segond?= Date: Wed, 30 Jun 2021 13:03:26 +0200 Subject: [PATCH] feat: add exercise-score command for learn-ocaml-client; fix: modifiaction required for server-config, logout, launch an exercise from moodle --- src/main/learnocaml_client.ml | 19 ++++++++----------- src/server/learnocaml_server.ml | 32 +++++++++++++++++++++----------- src/state/learnocaml_api.ml | 2 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/main/learnocaml_client.ml b/src/main/learnocaml_client.ml index 639b5d839..f7d5b34d8 100644 --- a/src/main/learnocaml_client.ml +++ b/src/main/learnocaml_client.ml @@ -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." @@ -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" diff --git a/src/server/learnocaml_server.ml b/src/server/learnocaml_server.ml index e492fc56c..f1e08d534 100644 --- a/src/server/learnocaml_server.ml +++ b/src/server/learnocaml_server.ml @@ -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 @@ -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 diff --git a/src/state/learnocaml_api.ml b/src/state/learnocaml_api.ml index 74abbb027..7fae069b6 100644 --- a/src/state/learnocaml_api.ml +++ b/src/state/learnocaml_api.ml @@ -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