finish_quiz.lslp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. /* finish_quiz
  4. Copyright (c) 2006-9 Sloodle (various contributors)
  5. Released under the GNU GPL
  6. This files lists all the status codes we use for sloodle.
  7. They have been written in LSL format so that you can plunk them into your source
  8. code if needed.
  9. Contributors:
  10. Edmund Edgar
  11. Paul Preibisch
  12. */
  13. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651;
  14. integer doRepeat = 0; // whether we should run through the questions again when we're done
  15. integer doDialog = 1; // whether we should ask the questions using dialog rather than chat
  16. integer doPlaySound = 1; // whether we should play sound
  17. integer doRandomize = 1; // whether we should ask the questions in random order
  18. string sloodleserverroot = "";
  19. integer sloodlecontrollerid = 0;
  20. string sloodlepwd = "";
  21. integer sloodlemoduleid = 0;
  22. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  23. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  24. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  25. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  26. integer eof = FALSE; // Have we reached the end of the configuration data?
  27. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  28. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343; // an arbitrary channel the sloodle scripts will use to talk to each other. Doesn't atter what it is, as long as the same thing is set in the sloodle_slave script.
  29. integer SLOODLE_CHANNEL_AVATAR_IGNORE = -1639279999;
  30. integer SLOODLE_CHANNEL_QUIZ_START_FOR_AVATAR = -1639271102; //Tells us to start a quiz for the avatar, if possible.; Ordinary quiz chair will have a second script that detects and avatar sitting on it and sends it. Awards-integrated version waits for a game ID to be set before doing this.
  31. integer SLOODLE_CHANNEL_QUIZ_STARTED_FOR_AVATAR = -1639271103; //Sent by main quiz script to tell UI scripts that quiz has started for avatar with key
  32. integer SLOODLE_CHANNEL_QUIZ_COMPLETED_FOR_AVATAR = -1639271104; //Sent by main quiz script to tell UI scripts that quiz has finished for avatar with key, with x/y correct in string
  33. integer SLOODLE_CHANNEL_QUESTION_ASKED_AVATAR = -1639271105; //Sent by main quiz script to tell UI scripts that question has been asked to avatar with key. String contains question ID + "|" + question text
  34. integer SLOODLE_CHANNEL_QUESTION_ANSWERED_AVATAR = -1639271106; //Sent by main quiz script to tell UI scripts that question has been answered by avatar with key. String contains selected option ID + "|" + option text + "|"
  35. integer SLOODLE_CHANNEL_QUIZ_LOADING_QUESTION = -1639271107;
  36. integer SLOODLE_CHANNEL_QUIZ_LOADED_QUESTION = -1639271108;
  37. integer SLOODLE_CHANNEL_QUIZ_LOADING_QUIZ = -1639271109;
  38. integer SLOODLE_CHANNEL_QUIZ_LOADED_QUIZ = -1639271110;
  39. integer SLOODLE_CHANNEL_QUIZ_GO_TO_STARTING_POSITION = -1639271111;
  40. integer SLOODLE_CHANNEL_QUIZ_ASK_QUESTION_CHAT = -1639271125; // Tells the question handler scripts to ask the question with the ID in str to the avatar with key VIA CHAT.
  41. integer SLOODLE_CHANNEL_QUIZ_ASK_QUESTION_TEXT_BOX=-1639277001;//asks via a text box
  42. integer SLOODLE_CHANNEL_QUIZ_ASK_QUESTION_DIALOG = -1639271126; // Tells the question handler scripts to ask the question with the ID in str to the avatar with key VIA DIALOG.
  43. integer SLOODLE_CHANNEL_ANSWER_SCORE_FOR_AVATAR = -1639271113; // Tells anyone who might be interested that we scored the answer. Score in string, avatar in key.
  44. integer SLOODLE_CHANNEL_QUIZ_STATE_ENTRY_DEFAULT = -1639271114; //mod quiz script is in state DEFAULT
  45. integer SLOODLE_CHANNEL_QUIZ_STATE_ENTRY_READY = -1639271115; //mod quiz script is in state READY
  46. integer SLOODLE_CHANNEL_QUIZ_STATE_ENTRY_LOAD_QUIZ_FOR_USER = -1639271116; //mod quiz script is in state CHECK_QUIZ
  47. integer SLOODLE_CHANNEL_QUIZ_STATE_ENTRY_QUIZZING = -1639271117; //mod quiz script is in state quizzing
  48. integer SLOODLE_CHANNEL_QUIZ_NO_PERMISSION_USE= -1639271118; //user has tried to use the chair but doesnt have permission to do so.
  49. integer SLOODLE_CHANNEL_QUIZ_STOP_FOR_AVATAR = -1639271119; //Tells us to STOP a quiz for the avatar
  50. integer SLOODLE_CHANNEL_QUIZ_SUCCESS_NOTHING_MORE_TO_DO_WITH_AVATAR= -1639271122;
  51. integer SLOODLE_CHANNEL_QUIZ_FAILURE_NOTHING_MORE_TO_DO_WITH_AVATAR= -1639271123;
  52. integer SLOODLE_CHANNEL_QUIZ_ERROR_INVALID_QUESION = -1639271121; //
  53. integer SLOODLE_CHANNEL_QUIZ_ERROR_ATTEMPTS_LEFT= -1639271123; //
  54. integer SLOODLE_CHANNEL_QUIZ_ERROR_NO_QUESTIONS= -1639271124; //
  55. integer SLOODLE_CHANNEL_QUIZ_FINISH_QUIZ=-1639277002;
  56. list server_requests;
  57. ///// COLORS ////
  58. vector RED =<1.00000, 0.00000, 0.00000>;
  59. vector ORANGE=<1.00000, 0.43763, 0.02414>;
  60. vector YELLOW=<1.00000, 1.00000, 0.00000>;
  61. vector GREEN=<0.00000, 1.00000, 0.00000>;
  62. vector BLUE=<0.00000, 0.00000, 1.00000>;
  63. vector BABYBLUE=<0.00000, 1.00000, 1.00000>;
  64. vector PINK=<1.00000, 0.00000, 1.00000>;
  65. vector PURPLE=<0.57338, 0.25486, 1.00000>;
  66. vector BLACK= <0.00000, 0.00000, 0.00000>;
  67. vector WHITE= <1.00000, 1.00000, 1.00000>;
  68. ///// TRANSLATION /////
  69. // Link message channels
  70. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  71. // Translation output methods
  72. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  73. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  74. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  75. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  76. string SLOODLE_TRANSLATE_DIALOG = "dialog"; // Recipient avatar should be identified in link message keyval. At least 2 output parameters: first the channel number for the dialog, and then 1 to 12 button label strings.
  77. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  78. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  79. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  80. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  81. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  82. string SLOODLE_OBJECT_TYPE = "quiz-1.0";
  83. string SLOODLE_EOF = "sloodleeof";
  84. string sloodle_quiz_url = "/mod/sloodle/mod/quiz-1.0/linker.php";
  85. key httpquizquery = NULL_KEY;
  86. float request_timeout = 20.0;
  87. string sloodlehttpvars;
  88. // ID and name of the current quiz
  89. integer quizid = -1;
  90. string quizname = "";
  91. integer num_questions = 0;
  92. //quiz id
  93. integer quiz_id;
  94. string quiz_name;
  95. // Stores the number of questions the user got correct on a given attempt
  96. integer askquestionscontinuously=0;
  97. integer correctToContinue=0; //must get question correct before next question is asked
  98. ///// FUNCTIONS /////
  99. debug (string message ){
  100. list params = llGetPrimitiveParams ([PRIM_MATERIAL ]);
  101. if ( llList2Integer (params ,0)==PRIM_MATERIAL_FLESH ){
  102. llOwnerSay(llGetScriptName ()+": " +message );
  103. }
  104. }
  105. /******************************************************************************************************************************
  106. * sloodle_error_code -
  107. * Author: Paul Preibisch
  108. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  109. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  110. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  111. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  112. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  113. * Params: a message from the server to use if there is none listed in the linker script.
  114. *******************************************************************************************************************************/
  115. sloodle_error_code(string method, key avuuid,integer statuscode, string msg){
  116. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode+"|"+(string)msg, NULL_KEY);
  117. }
  118. sloodle_debug(string msg){
  119. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  120. }
  121. // Configure by receiving a linked message from another script in the object
  122. // Returns TRUE if the object has all the data it needs
  123. integer sloodle_handle_command(string str){
  124. list bits = llParseString2List(str,["|"],[]);
  125. integer numbits = llGetListLength(bits);
  126. string name = llList2String(bits,0);
  127. string value1 = "";
  128. string value2 = "";
  129. if (numbits > 1) value1 = llList2String(bits,1);
  130. if (numbits > 2) value2 = llList2String(bits,2);
  131. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  132. else if (name == "set:sloodlepwd") {
  133. // The password may be a single prim password, or a UUID and a password
  134. if (value2 != "") {
  135. sloodlepwd = value1 + "|" + value2;
  136. }
  137. else {
  138. sloodlepwd = value1;
  139. }
  140. }
  141. else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  142. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  143. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  144. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  145. else if (name == "set:sloodlerepeat") doRepeat = (integer)value1;
  146. else if (name == "set:sloodlerandomize") doRandomize = (integer)value1;
  147. else if (name == "set:sloodleplaysound") doPlaySound = (integer)value1;
  148. else if (name == "set:sloodleaskquestionscontinuously") askquestionscontinuously= (integer)value1;
  149. else if (name == "set:correctToContinue") correctToContinue = (integer)value1;
  150. else if (name == SLOODLE_EOF) eof = TRUE;
  151. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  152. }
  153. // Send a translation request link message
  154. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch){
  155. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  156. }
  157. default{
  158. on_rez(integer start_param) {
  159. llResetScript();
  160. }
  161. state_entry(){
  162. isconfigured = FALSE;
  163. eof = FALSE;
  164. // Reset our configuration data
  165. sloodleserverroot = "";
  166. sloodlepwd = "";
  167. sloodlecontrollerid = 0;
  168. sloodlemoduleid = 0;
  169. sloodleobjectaccessleveluse = 0;
  170. sloodleserveraccesslevel = 0;
  171. doRepeat = 1;
  172. doDialog = 1;
  173. doPlaySound = 1;
  174. doRandomize = 1;
  175. //tell other scripts we are in the default state.
  176. }
  177. link_message( integer sender_num, integer num, string str, key id){
  178. // Check the channel for configuration messages
  179. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  180. // Split the message into lines
  181. list lines = llParseString2List(str, ["\n"], []);
  182. integer numlines = llGetListLength(lines);
  183. integer i = 0;
  184. for (i=0; i < numlines; i++) {
  185. isconfigured = sloodle_handle_command(llList2String(lines, i));
  186. }
  187. // If we've got all our data AND reached the end of the configuration data (eof), then move on
  188. if (eof == TRUE) {
  189. if (isconfigured == TRUE) {
  190. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  191. state ready;
  192. } else {
  193. // Go all configuration but, it's not complete... request reconfiguration
  194. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [llGetScriptName()], NULL_KEY, "");
  195. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  196. eof = FALSE;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. state ready{
  203. on_rez(integer start_param) {
  204. llResetScript();
  205. }
  206. state_entry(){
  207. sloodlehttpvars = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  208. sloodlehttpvars += "&sloodlepwd=" + sloodlepwd;
  209. sloodlehttpvars += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  210. sloodlehttpvars += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  211. }
  212. link_message(integer sender_num, integer num, string str, key user_key){
  213. if (num == SLOODLE_CHANNEL_QUIZ_COMPLETED_FOR_AVATAR) {
  214. // Notify the server that the attempt was finished
  215. string body = sloodlehttpvars;
  216. body += "&finishattempt=1";
  217. body += "&sloodleuuid=" + (string)user_key;
  218. body += "&sloodleavname=" + llEscapeURL(llKey2Name(user_key));
  219. body += "&sloodlerequestdesc="+"FINISH_QUIZ";
  220. server_requests+=llHTTPRequest(sloodleserverroot + sloodle_quiz_url, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  221. }else
  222. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  223. // Is it a reset command?
  224. if (str == "do:reset") {
  225. llResetScript();
  226. }
  227. return;
  228. }
  229. }
  230. http_response(key request_id, integer status, list metadata, string body) {
  231. integer placeinlist=llListFindList(server_requests, [request_id]);
  232. if (placeinlist!=-1){
  233. debug("finish quiz response: "+body);
  234. server_requests= llDeleteSubList(server_requests, placeinlist, placeinlist);
  235. }else {
  236. return;
  237. }
  238. }
  239. }
  240. // Please leave the following line intact to show where the script lives in Git:
  241. // SLOODLE LSL Script Git Location: mod/quiz-1.0/objects/multi_user_quiz/assets/finish_quiz.lslp