Skip to content

Commit d90944f

Browse files
committed
fixup! Offer better protections against solution overwriting
Signed-off-by: Yann Regis-Gianas <[email protected]>
1 parent 0d25f20 commit d90944f

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/app/learnocaml_common.ml

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ let mouseover_toggle_signal elt sigvalue setter =
712712
in
713713
Manip.Ev.onmouseover elt hdl
714714
715-
(*
715+
(*
716716
717717
If a user has made no change to a solution for the exercise [id]
718718
for 180 seconds, [check_valid_editor_state id] ensures that there is
@@ -724,24 +724,42 @@ let mouseover_toggle_signal elt sigvalue setter =
724724
student solution when the solution is open in several clients.
725725
726726
*)
727+
let is_synchronized_with_server_callback = ref (fun () -> false)
728+
729+
let is_synchronized_with_server () = !is_synchronized_with_server_callback ()
730+
727731
let check_valid_editor_state id =
728732
let last_changed = ref (Unix.gettimeofday ()) in
729733
fun update_content ->
730734
let update_local_copy checking_time () =
731735
match Learnocaml_local_storage.(retrieve (exercise_state id)) with
732736
| { Answer.mtime; solution; _ } ->
733-
if mtime > checking_time then (
734-
if Js_utils.confirm
735-
[%i "A more recent answer exists on the server. \
736-
Do you want to update the current one?"]
737-
then
738-
update_content solution;
739-
);
740-
Lwt.return ()
737+
if mtime > checking_time then (
738+
let buttons =
739+
if is_synchronized_with_server () then
740+
[
741+
[%i "Fetch from server"],
742+
(fun () -> Lwt.return (update_content solution));
743+
[%i "Ignore & keep editing"],
744+
(fun () -> Lwt.return_unit)
745+
]
746+
else
747+
[
748+
[%i "Ignore & keep editing"],
749+
(fun () -> Lwt.return_unit);
750+
[%i "Fetch from server & overwrite"],
751+
(fun () -> Lwt.return (update_content solution));
752+
]
753+
in
754+
lwt_alert ~title:"Question"
755+
~buttons
756+
[ H.p [H.txt [%i "A more recent answer exists on the server. \
757+
Do you want to fetch the new version?"] ] ]
758+
) else Lwt.return_unit
741759
| exception Not_found -> Lwt.return ()
742760
in
743761
let now = Unix.gettimeofday () in
744-
if now -. !last_changed > 180. then (
762+
if now -. !last_changed > 30. then (
745763
let checking_time = !last_changed in
746764
last_changed := now;
747765
Lwt.async (update_local_copy checking_time)
@@ -1074,7 +1092,7 @@ let setup_prelude_pane ace prelude =
10741092
(fun _ -> state := not !state ; update () ; true) ;
10751093
Manip.appendChildren prelude_pane
10761094
[ prelude_title ; prelude_container ]
1077-
1095+
10781096
let get_token ?(has_server = true) () =
10791097
if not has_server then
10801098
Lwt.return None
@@ -1093,7 +1111,7 @@ let get_token ?(has_server = true) () =
10931111
>|= fun token ->
10941112
Learnocaml_local_storage.(store sync_token) token;
10951113
Some token
1096-
1114+
10971115
module Display_exercise =
10981116
functor (
10991117
Q: sig

src/app/learnocaml_common.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ end
220220

221221
val setup_editor : string -> string -> Ocaml_mode.editor * Ocaml_mode.editor Ace.editor
222222

223+
val is_synchronized_with_server_callback : (unit -> bool) ref
224+
223225
val typecheck :
224226
Learnocaml_toplevel.t ->
225227
'a Ace.editor -> Ocaml_mode.editor -> bool -> unit Lwt.t

src/app/learnocaml_exercise_main.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ module Exercise_link =
8181
]
8282
content
8383
end
84-
85-
module Display = Display_exercise(Exercise_link)
86-
open Display
84+
85+
module Display = Display_exercise(Exercise_link)
86+
open Display
8787

8888
let is_readonly = ref false
8989

@@ -180,6 +180,7 @@ let () =
180180
Tyxml_js.Of_dom.of_iFrame text_iframe ] ;
181181
(* ---- editor pane --------------------------------------------------- *)
182182
let editor, ace = setup_editor id solution in
183+
is_synchronized_with_server_callback := (fun () -> Ace.is_synchronized ace);
183184
let module EB = Editor_button (struct let ace = ace let buttons_container = editor_toolbar end) in
184185
EB.cleanup (Learnocaml_exercise.(access File.template exo));
185186
EB.sync token id (fun () -> Ace.set_synchronized ace) ;

translations/fr.po

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,10 +946,19 @@ msgstr "lors du test de la solution utilisateur"
946946
msgid "Do you want to save your work before closing?"
947947
msgstr "Souhaitez-vous enregistrer votre travail avant de quitter?"
948948

949-
msgid "A more recent answer exists on the server. Do you want to update the current one?"
949+
msgid "A more recent answer exists on the server. Do you want to fetch the new version?"
950950
msgstr ""
951951
"Une version plus récente de cette réponse existe sur le serveur."
952-
"Voulez-vous la télécharger?"
952+
"Voulez-vous télécharger la nouvelle version?"
953+
954+
msgid "Fetch from server"
955+
msgstr "Télécharger depuis le serveur"
956+
957+
msgid "Fetch from server & overwrite"
958+
msgstr "Télécharger depuis le serveur et écraser"
959+
960+
msgid "Ignore & keep editing"
961+
msgstr "Ignorer et continuer d'éditer"
953962

954963
#~ msgid "No description available."
955964
#~ msgstr "Aucune description."

0 commit comments

Comments
 (0)