Download The demexp Book - Linux

Transcript
Callback vote callback is called when the user clicks on the “Vote” button. It gets the vote
choice from the “My vote” list widget and calls the needed RPCs on the server. In any case, the
“Vote” window is destroyed and a message is displayed in the browser window.
146a
hvote.ml 142ai+≡
/ 145d 146b .
let vote_callback ui backends client cookie user_msg q_id pref clerk
update_callback () =
let f () =
let model : #GTree.model = backends.myvote#store in
let vote = ref [] in
let add_choice _ iter =
let r_id = model#get ˜row:iter ˜column:backends.myvote#col_id in
vote := r_id :: !vote;
false (* do not stop, continue walking the store *) in
backends.myvote#store#foreach add_choice;
vote := List.rev !vote;
let display_result_msg user_ret delegate_ret type_str =
let msg = result_msg user_ret delegate_ret type_str in
user_msg msg in
(match backends.vote_type with
| User ->
let user_ret =
Demexp.V1.vote client (cookie, q_id, Array.of_list !vote) in
display_result_msg user_ret rt_ok (s_ "user");
record_voted_question clerk q_id user_ret rt_ok
| Delegate ->
let delegate_ret = vote_as_delegate client q_id pref !vote in
display_result_msg rt_ok delegate_ret (s_ "delegate");
record_voted_question clerk q_id rt_ok delegate_ret
| User_and_delegate ->
let delegate_ret = vote_as_delegate client q_id pref !vote in
let user_ret =
Demexp.V1.vote client (cookie, q_id, Array.of_list !vote) in
display_result_msg user_ret delegate_ret (s_ "user & delegate");
record_voted_question clerk q_id user_ret delegate_ret);
update_callback (); (* refresh browser display *)
ui#toplevel#destroy () in
MiscUI.handle_network_error f () ()
Function get question details returns the description and responses of question of identifier q id.
146b
hvote.ml 142ai+≡
/ 146a 147 .
let get_question_details client cookie cache q_id =
let ret = Cache.question_info cache client (cookie, q_id, 1) in
if ret.question_info_rc <> rt_ok then
raise (Misc.Display_error
(Printf.sprintf
(f_ "Unable to load information on question:%d : %s")
q_id (Misc.string_of_return_code ret.question_info_rc)));
if Array.length ret.question_info <> 1 then
raise (Misc.Display_error
(Printf.sprintf
(f_ "Invalid array length for question_info:%d : %d")
q_id (Array.length ret.question_info)));
let q_desc = ret.question_info.(0).q_desc in
let q_responses =
Array.to_list (Array.mapi (fun i r -> (i, r.r_info_desc))
ret.question_info.(0).q_info_responses) in
(q_desc, q_responses)
146