844d2777-e8fb-c5d6-5335-f8d67aeabbb8_script.lsl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle quiz chair
  4. // Allows SL users to take Moodle quizzes in-world
  5. // Part of the Sloodle project (www.sloodle.org)
  6. //
  7. // Copyright (c) 2006-8 Sloodle (various contributors)
  8. // Released under the GNU GPL
  9. //
  10. // Contributors:
  11. // Edmund Edgar
  12. // Peter R. Bloomfield
  13. // Paul Preibisch - Fire Centaur in SL
  14. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  15. integer doRepeat = 0; // whether we should run through the questions again when we're done
  16. integer doDialog = 1; // whether we should ask the questions using dialog rather than chat
  17. integer doPlaySound = 1; // whether we should play sound
  18. integer doRandomize = 1; // whether we should ask the questions in random order
  19. string sloodleserverroot = "";
  20. integer sloodlecontrollerid = 0;
  21. string sloodlepwd = "";
  22. integer sloodlemoduleid = 0;
  23. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  24. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  25. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  26. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  27. integer eof = FALSE; // Have we reached the end of the configuration data?
  28. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  29. 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.
  30. integer SLOODLE_CHANNEL_AVATAR_IGNORE = -1639279999;
  31. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  32. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  33. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  34. // numbers used when talking to linked choice objects
  35. integer SLOODLE_CHANNEL_QUIZ_MULTIPLE_MY_NUMBER = -1639270062;
  36. integer SLOODLE_CHANNEL_QUIZ_MULTIPLE_QUESTION = -1639270063;
  37. integer SLOODLE_CHANNEL_QUIZ_MULTIPLE_CORRECT = -1639270064;
  38. integer SLOODLE_CHANNEL_QUIZ_MULTIPLE_INCORRECT = -1639270065;
  39. integer SLOODLE_CHANNEL_QUIZ_MULTIPLE_CHOICE_SELECTED = -1639270066;
  40. list choice_links = [2,3,4,5,6,7]; // a list of the link numbers of objects used as choices.
  41. integer is_waiting_for_answer = 0;
  42. integer listener_id;
  43. string SLOODLE_OBJECT_TYPE = "quiz-1.0";
  44. string SLOODLE_EOF = "sloodleeof";
  45. string sloodle_quiz_url = "/mod/sloodle/mod/quiz-1.0/linker.php";
  46. key httpquizquery = NULL_KEY;
  47. float request_timeout = 20.0;
  48. // ID and name of the current quiz
  49. integer quizid = -1;
  50. string quizname = "";
  51. // This stores the list of question ID's (global ID's)
  52. list question_ids = [];
  53. integer num_questions = 0;
  54. // Identifies the active question number (index into question_ids list)
  55. // (Next question will always be this value +1)
  56. integer active_question = -1;
  57. // Identifies which question we are currently requesting (index into question_ids list)
  58. integer requesting_question = -1;
  59. // Number of the loaded 'next' question (corresponds to 'active_question')
  60. integer qloaded_next = -1;
  61. // Text and type of the current and next question
  62. string qtext_current = "";
  63. string qtype_current = "";
  64. string qtext_next = "";
  65. string qtype_next = "";
  66. // Lists of option information for the current question
  67. list opids_current = []; // IDs
  68. list optext_current = []; // Texts
  69. list opgrade_current = []; // Grades
  70. list opfeedback_current = []; // Feedback if this option is selected
  71. // Lists of option information for the next question
  72. list opids_next = []; // IDs
  73. list optext_next = []; // Texts
  74. list opgrade_next = []; // Grades
  75. list opfeedback_next = []; // Feedback if this option is selected
  76. // Avatar currently operating quiz
  77. key toucher = NULL_KEY;
  78. // The lowest point of the char
  79. float lowestvector = 0.0;
  80. // Store's the user's cumulative score on an attempt.
  81. // (only used for reporting it at the end)
  82. float totalscore = 0.0;
  83. ///// FUNCTIONS /////
  84. /******************************************************************************************************************************
  85. * sloodle_error_code -
  86. * Author: Paul Preibisch
  87. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  88. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  89. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  90. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  91. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  92. *******************************************************************************************************************************/
  93. sloodle_error_code(string method, key avuuid,integer statuscode){
  94. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  95. }
  96. sloodle_debug(string msg)
  97. {
  98. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  99. }
  100. // Configure by receiving a linked message from another script in the object
  101. // Returns TRUE if the object has all the data it needs
  102. integer sloodle_handle_command(string str)
  103. {
  104. list bits = llParseString2List(str,["|"],[]);
  105. integer numbits = llGetListLength(bits);
  106. string name = llList2String(bits,0);
  107. string value1 = "";
  108. string value2 = "";
  109. if (numbits > 1) value1 = llList2String(bits,1);
  110. if (numbits > 2) value2 = llList2String(bits,2);
  111. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  112. else if (name == "set:sloodlepwd") {
  113. // The password may be a single prim password, or a UUID and a password
  114. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  115. else sloodlepwd = value1;
  116. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  117. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  118. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  119. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  120. else if (name == "set:sloodlerepeat") doRepeat = (integer)value1;
  121. else if (name == "set:sloodlerandomize") doRandomize = (integer)value1;
  122. else if (name == "set:sloodledialog") doDialog = (integer)value1;
  123. else if (name == "set:sloodleplaysound") doPlaySound = (integer)value1;
  124. else if (name == SLOODLE_EOF) eof = TRUE;
  125. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  126. }
  127. // Checks if the given agent is permitted to user this object
  128. // Returns TRUE if so, or FALSE if not
  129. integer sloodle_check_access_use(key id)
  130. {
  131. // Check the access mode
  132. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  133. return llSameGroup(id);
  134. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  135. return TRUE;
  136. }
  137. // Assume it's owner mode
  138. return (id == llGetOwner());
  139. }
  140. // Query the server for the identified question (request by global question ID)
  141. key request_question( integer qid )
  142. {
  143. // Request the identified question from Moodle
  144. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  145. body += "&sloodlepwd=" + sloodlepwd;
  146. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  147. body += "&sloodleuuid=" + (string)toucher;
  148. body += "&sloodleavname=" + llEscapeURL(llKey2Name(toucher));
  149. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  150. body += "&ltq=" + (string)qid;
  151. key newhttp = llHTTPRequest(sloodleserverroot + sloodle_quiz_url, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  152. llSetTimerEvent(0.0);
  153. llSetTimerEvent(request_timeout);
  154. return newhttp;
  155. }
  156. // Notify the server of a response
  157. notify_server(string qtype, integer questioncode, integer responsecode)
  158. {
  159. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  160. body += "&sloodlepwd=" + sloodlepwd;
  161. body += "&q=" + (string)quizid;
  162. body += "&sloodleuuid=" + (string)toucher;
  163. body += "&sloodleavname=" + llEscapeURL(llKey2Name(toucher));
  164. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  165. body += "&resp" + (string)questioncode + "_=" + (string)responsecode;
  166. body += "&questionids=" + (string)questioncode;
  167. body += "&resp" + (string)questioncode+"_submit=Submit";
  168. body += "&timeup=" + "0"; //(string)timeup; // Wasn't being initialised anywhere
  169. body += "&action=notify";
  170. // llHTTPRequest(sloodleserverroot + sloodle_quiz_url, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  171. }
  172. // Ask the current question
  173. ask_current_question()
  174. {
  175. llSay(0, qtext_current);
  176. llSetText(qtext_current,<255,0,0>,1.0);
  177. integer x;
  178. for (x=0; x < llGetListLength(optext_current); x++) {
  179. integer link = llList2Integer(choice_links,x);
  180. llMessageLinked(link, SLOODLE_CHANNEL_QUIZ_MULTIPLE_QUESTION, llList2String(optext_current, x), NULL_KEY );
  181. //llWhisper(0, " - " + llList2String(qoptiontexts_current, x) );
  182. }
  183. is_waiting_for_answer = 1; //TODO: Check if I need this...
  184. }
  185. send_answers()
  186. {
  187. integer i;
  188. for (i=0; i<llGetListLength(question_ids); i++) {
  189. integer scorechange = llList2Integer(opgrade_current, i);
  190. string feedback = llList2String(opfeedback_current, i);
  191. if (scorechange > 0) {
  192. llMessageLinked(llList2Integer(choice_links,i),SLOODLE_CHANNEL_QUIZ_MULTIPLE_CORRECT,feedback,NULL_KEY);
  193. } else {
  194. llMessageLinked(llList2Integer(choice_links,i),SLOODLE_CHANNEL_QUIZ_MULTIPLE_INCORRECT,feedback,NULL_KEY);
  195. }
  196. }
  197. is_waiting_for_answer = 0;
  198. }
  199. // tell each link which number it is
  200. assign_numbers()
  201. {
  202. integer i;
  203. for (i=0;i<llGetListLength(choice_links);i++) {
  204. integer j = i+1;
  205. llMessageLinked(llList2Integer(choice_links,i),SLOODLE_CHANNEL_QUIZ_MULTIPLE_MY_NUMBER,(string)j,NULL_KEY);
  206. }
  207. }
  208. // Play a sound as audio feedback
  209. play_sound(float multiplier)
  210. {
  211. // Do nothing if sound is disabled
  212. if (doPlaySound == 0) return;
  213. string sound_file;
  214. float volume;
  215. // Determine what our sound file and volume should be
  216. if (multiplier > 0) {
  217. sound_file = "Correct";
  218. } else {
  219. sound_file = "Incorrect";
  220. multiplier = multiplier * -1;
  221. }
  222. // Cap our volume
  223. if (multiplier > 1) {
  224. volume = 1.0;
  225. } else {
  226. volume = (float)multiplier;
  227. }
  228. // Make sure the sound file exists, and then play it
  229. if (llGetInventoryType(sound_file) == INVENTORY_SOUND) llPlaySound(sound_file,multiplier);
  230. }
  231. // Move the chair up or down as visual feedback
  232. move_vertical(float multiplier)
  233. {
  234. vector position = llGetPos();
  235. position.z += 0.5 * multiplier;
  236. llSetPos(position);
  237. }
  238. // Move the Quiz Chair back to the starting position
  239. move_to_start()
  240. {
  241. vector position = llGetPos();
  242. position.z = lowestvector;
  243. llSetPos(position);
  244. }
  245. // Report completion to the user
  246. finish_quiz()
  247. {
  248. llSetText("", <0.0,0.0,0.0>, 0.0);
  249. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "complete:noscore", [], NULL_KEY, "quiz");
  250. // Clear the big nasty chunks of data
  251. optext_current = [];
  252. opfeedback_current = [];
  253. optext_next = [];
  254. opfeedback_next = [];
  255. // Notify the server that the attempt was finished
  256. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  257. body += "&sloodlepwd=" + sloodlepwd;
  258. body += "&q=" + (string)quizid;
  259. body += "&sloodleuuid=" + (string)toucher;
  260. body += "&sloodleavname=" + llEscapeURL(llKey2Name(toucher));
  261. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  262. body += "&finishattempt=1";
  263. // llHTTPRequest(sloodleserverroot + sloodle_quiz_url, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  264. }
  265. // Reinitialise (e.g. after one person has finished an attempt)
  266. reinitialise()
  267. {
  268. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "resetting", [], NULL_KEY, "");
  269. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  270. llResetScript();
  271. }
  272. ///// TRANSLATION /////
  273. // Link message channels
  274. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  275. // Translation output methods
  276. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  277. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  278. 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.
  279. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  280. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  281. // Send a translation request link message
  282. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  283. {
  284. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  285. }
  286. ///// ----------- /////
  287. ///// STATES /////
  288. // Waiting on initialisation
  289. default
  290. {
  291. state_entry()
  292. {
  293. // Starting again with a new configuration
  294. llSetText("", <0.0,0.0,0.0>, 0.0);
  295. isconfigured = FALSE;
  296. eof = FALSE;
  297. // Reset our data
  298. sloodleserverroot = "";
  299. sloodlepwd = "";
  300. sloodlecontrollerid = 0;
  301. sloodlemoduleid = 0;
  302. sloodleobjectaccessleveluse = 0;
  303. sloodleserveraccesslevel = 0;
  304. doRepeat = 1;
  305. doDialog = 1;
  306. doPlaySound = 1;
  307. doRandomize = 1;
  308. }
  309. link_message( integer sender_num, integer num, string str, key id)
  310. {
  311. // Check the channel
  312. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  313. // Split the message into lines
  314. list lines = llParseString2List(str, ["\n"], []);
  315. integer numlines = llGetListLength(lines);
  316. integer i = 0;
  317. for (; i < numlines; i++) {
  318. isconfigured = sloodle_handle_command(llList2String(lines, i));
  319. }
  320. // If we've got all our data AND reached the end of the configuration data, then move on
  321. if (eof == TRUE) {
  322. if (isconfigured == TRUE) {
  323. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  324. state ready;
  325. } else {
  326. // Go all configuration but, it's not complete... request reconfiguration
  327. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  328. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  329. eof = FALSE;
  330. }
  331. }
  332. }
  333. }
  334. touch_start(integer num_detected)
  335. {
  336. // Attempt to request a reconfiguration
  337. if (llDetectedKey(0) == llGetOwner()) {
  338. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  339. }
  340. }
  341. }
  342. // Ready state - waiting for a user to climb aboard!
  343. state ready
  344. {
  345. state_entry()
  346. {
  347. assign_numbers();
  348. }
  349. touch_start(integer c) {
  350. toucher = llDetectedKey(0);
  351. // Make sure the given avatar is allowed to use this object
  352. if (toucher != llGetOwner()) {
  353. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "nopermission:use", [llKey2Name(toucher)], NULL_KEY, "");
  354. toucher = NULL_KEY;
  355. return;
  356. }
  357. listener_id = llListen(SLOODLE_CHANNEL_AVATAR_DIALOG,"",toucher,"");
  358. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [SLOODLE_CHANNEL_AVATAR_DIALOG, "1", "X"], "pileonmenu:start", ["1", "X"], toucher, "quiz");
  359. }
  360. listen(integer channel, string name, key id, string message) {
  361. if (id == toucher) {
  362. if (message == "1") {
  363. // Start the quiz
  364. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "starting", [llKey2Name(toucher)], NULL_KEY, "quiz");
  365. // Make sure it's the owner who is the toucher.
  366. // (Just makes sure nobody else touched the object just before the state change).
  367. toucher = llGetOwner();
  368. state check_quiz;
  369. }
  370. llListenRemove(listener_id);
  371. }
  372. }
  373. }
  374. // Fetching the general quiz data
  375. state check_quiz
  376. {
  377. state_entry()
  378. {
  379. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "fetchingquiz", [], NULL_KEY, "quiz");
  380. // Clear existing data
  381. quizname = "";
  382. question_ids = [];
  383. num_questions = 0;
  384. active_question = -1;
  385. qtext_current = "";
  386. qtype_current = "";
  387. qtext_next = "";
  388. qtype_next = "";
  389. opids_current = [];
  390. optext_current = [];
  391. opgrade_current = [];
  392. opfeedback_current = [];
  393. opids_next = [];
  394. optext_next = [];
  395. opgrade_next = [];
  396. opfeedback_next = [];
  397. // Request the quiz data from Moodle
  398. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  399. body += "&sloodlepwd=" + sloodlepwd;
  400. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  401. body += "&sloodleuuid=" + (string)toucher;
  402. body += "&sloodleavname=" + llEscapeURL(llKey2Name(toucher));
  403. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  404. httpquizquery = llHTTPRequest(sloodleserverroot + sloodle_quiz_url, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  405. llSetTimerEvent(0.0);
  406. llSetTimerEvent((float)request_timeout);
  407. }
  408. state_exit()
  409. {
  410. llSetTimerEvent(0.0);
  411. }
  412. timer()
  413. {
  414. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httptimeout", [], NULL_KEY, "");
  415. state ready;
  416. }
  417. http_response(key id, integer status, list meta, string body)
  418. {
  419. // Is this the response we are expecting?
  420. if (id != httpquizquery) return;
  421. httpquizquery = NULL_KEY;
  422. // Make sure the response was OK
  423. if (status != 200) {
  424. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  425. state default;
  426. }
  427. // Split the response into several lines
  428. list lines = llParseStringKeepNulls(body, ["\n"], []);
  429. integer numlines = llGetListLength(lines);
  430. body = "";
  431. list statusfields = llParseStringKeepNulls(llList2String(lines,0), ["|"], []);
  432. integer statuscode = llList2Integer(statusfields, 0);
  433. // Was it an error code?
  434. if (statuscode == -10301) {
  435. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noattemptsleft", [llKey2Name(toucher)], NULL_KEY, "");
  436. state ready;
  437. return;
  438. } else if (statuscode == -10302) {
  439. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noquestions", [], NULL_KEY, "");
  440. state ready;
  441. return;
  442. } else if (statuscode <= 0) {
  443. //sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  444. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  445. // Check if an error message was reported
  446. if (numlines > 1) sloodle_debug(llList2String(lines, 1));
  447. state ready;
  448. return;
  449. }
  450. // We shouldn't need the status line anymore... get rid of it
  451. statusfields = [];
  452. // Go through each line of the response
  453. integer i;
  454. for (i = 1; i < numlines; i++) {
  455. // Extract and parse the current line
  456. string thislinestr = llList2String(lines, i);
  457. list thisline = llParseString2List(thislinestr,["|"],[]);
  458. string rowtype = llList2String( thisline, 0 );
  459. // Check what type of line this is
  460. if ( rowtype == "quiz" ) {
  461. // Get the quiz ID and name
  462. quizid = (integer)llList2String(thisline, 4);
  463. quizname = llList2String(thisline, 2);
  464. } else if ( rowtype == "quizpages" ) {
  465. // Extract the list of questions ID's
  466. list question_ids_str = llCSV2List(llList2String(thisline, 3));
  467. num_questions = llGetListLength(question_ids_str);
  468. integer qiter = 0;
  469. question_ids = [];
  470. // Store all our question IDs
  471. for (qiter = 0; qiter < num_questions; qiter++) {
  472. question_ids += [(integer)llList2String(question_ids_str, qiter)];
  473. }
  474. // Are we to randomize the order of the questions?
  475. if (doRandomize) question_ids = llListRandomize(question_ids, 1);
  476. active_question = 0;
  477. }
  478. }
  479. // Make sure we have all the data we need
  480. if (quizname == "" || num_questions == 0) {
  481. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noquestions", [], NULL_KEY, "quiz");
  482. state default;
  483. return;
  484. }
  485. // Report the status to the user
  486. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "ready", [quizname], NULL_KEY, "quiz");
  487. state quizzing;
  488. }
  489. on_rez(integer par)
  490. {
  491. llResetScript();
  492. }
  493. changed(integer change)
  494. {
  495. // reinitialise();
  496. }
  497. }
  498. // Dummy state -- goes straight back into the quiz
  499. state repeat_quiz
  500. {
  501. state_entry()
  502. {
  503. state quizzing;
  504. }
  505. on_rez(integer par)
  506. {
  507. llResetScript();
  508. }
  509. changed(integer change)
  510. {
  511. reinitialise();
  512. }
  513. }
  514. // Running the quiz
  515. state quizzing
  516. {
  517. on_rez(integer param)
  518. {
  519. llResetScript();
  520. }
  521. state_entry()
  522. {
  523. llSetText("", <0.0,0.0,0.0>, 0.0);
  524. // Make sure we have some questions
  525. if (num_questions == 0) {
  526. sloodle_debug("No questions - cannot run quiz.");
  527. state default;
  528. return;
  529. }
  530. // Listen for answers coming in from the avatar
  531. listener_id = llListen(SLOODLE_CHANNEL_AVATAR_DIALOG, "", toucher, "");
  532. // Start from the beginning
  533. active_question = 0;
  534. requesting_question = 0;
  535. httpquizquery = request_question(llList2Integer(question_ids, requesting_question));
  536. }
  537. state_exit()
  538. {
  539. llSetTimerEvent(0.0);
  540. }
  541. listen(integer channel, string name, key id, string message)
  542. {
  543. if (channel != SLOODLE_CHANNEL_AVATAR_DIALOG) return;
  544. if (channel != SLOODLE_CHANNEL_AVATAR_DIALOG) return;
  545. if (id != toucher) return;
  546. if (message == "X") {
  547. llListenRemove(listener_id);
  548. return;
  549. }
  550. if (is_waiting_for_answer == 1 && message == "1") { // "Answer"
  551. llListenRemove(listener_id);
  552. send_answers();
  553. return;
  554. }
  555. if (message == "2") { // "End quiz"
  556. // Yes - finish off
  557. finish_quiz();
  558. // Do we want to repeat the quiz?
  559. if (doRepeat) {
  560. state repeat_quiz;
  561. } else {
  562. // If we are waiting for an answer then send the current answers before finishing
  563. if (is_waiting_for_answer == 1) send_answers();
  564. state ready;
  565. }
  566. return;
  567. }
  568. if (is_waiting_for_answer == 0 && message == "1") { // "Next"
  569. llListenRemove(listener_id);
  570. // Are we are at the end of the quiz?
  571. if ((active_question + 1) >= num_questions) {
  572. // Yes - finish off
  573. finish_quiz();
  574. // Do we want to repeat the quiz?
  575. if (doRepeat) {
  576. state repeat_quiz;
  577. } else {
  578. // If we are waiting for an answer then send the current answers before finishing
  579. if (is_waiting_for_answer == 1) send_answers();
  580. state ready;
  581. }
  582. return;
  583. }
  584. // Advance to the next question
  585. active_question++;
  586. // Has our 'next' question been loaded?
  587. if (qloaded_next == active_question) {
  588. // Yes
  589. // Transfer all our 'next' question data into the 'current' question variables
  590. qtext_current = qtext_next;
  591. qtype_current = qtype_next;
  592. opids_current = opids_next;
  593. optext_current = optext_next;
  594. opgrade_current = opgrade_next;
  595. opfeedback_current = opfeedback_next;
  596. // Ask the current question, and request the next (if there is one)
  597. ask_current_question();
  598. if ((active_question + 1) < num_questions) {
  599. requesting_question = active_question + 1;
  600. httpquizquery = request_question(llList2Integer(question_ids, requesting_question));
  601. }
  602. } else {
  603. // No - still waiting on our next question.
  604. // It is now technically our 'current' question, so the http_response will automatically ask it when it arrives.
  605. }
  606. return;
  607. }
  608. sloodle_debug("do not understand message "+message+ " in state quizzing");
  609. }
  610. timer()
  611. {
  612. // There has been a timeout of the HTTP request
  613. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httptimeout", [], NULL_KEY, "");
  614. llSetTimerEvent(0.0);
  615. }
  616. touch_start(integer c) {
  617. toucher = llDetectedKey(0);
  618. if (toucher == llGetOwner()) {
  619. listener_id = llListen(SLOODLE_CHANNEL_AVATAR_DIALOG,"",toucher,"");
  620. if (is_waiting_for_answer == 1) {
  621. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [SLOODLE_CHANNEL_AVATAR_DIALOG, "1", "2", "X"], "pileonmenu:answer", ["1", "2", "X"], toucher, "quiz");
  622. } else {
  623. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [SLOODLE_CHANNEL_AVATAR_DIALOG, "1", "2", "X"], "pileonmenu:next", ["1", "2", "X"], toucher, "quiz");
  624. }
  625. }
  626. }
  627. http_response(key request_id, integer status, list metadata, string body)
  628. {
  629. // This response will always contain question data.
  630. // If the current question is being loaded, then ask it right away, and load the next.
  631. // If the next question is being loaded, then just store it.
  632. // It will be made current and asked whenever the current one gets answered.
  633. // If the user ever gets ahead of our loading, then they will be waiting on the 'current' question.
  634. // As soon as that is loaded, it will get asked.
  635. // Is this the response we are expecting?
  636. if (request_id != httpquizquery) return;
  637. httpquizquery = NULL_KEY;
  638. llSetTimerEvent(0.0);
  639. // Make sure the response was OK
  640. if (status != 200) {
  641. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  642. state default;
  643. }
  644. // Split the response into several lines
  645. list lines = llParseStringKeepNulls(body, ["\n"], []);
  646. integer numlines = llGetListLength(lines);
  647. body = "";
  648. list statusfields = llParseStringKeepNulls(llList2String(lines,0), ["|"], []);
  649. integer statuscode = llList2Integer(statusfields, 0);
  650. // Was it an error code?
  651. if (statuscode == -331) {
  652. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "nopermission:use", [llKey2Name(toucher)], NULL_KEY, "");
  653. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "resetting", [], NULL_KEY, "");
  654. state default;
  655. return;
  656. } else if (statuscode == -10301) {
  657. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noattemptsleft", [llKey2Name(toucher)], NULL_KEY, "");
  658. return;
  659. } else if (statuscode == -10302) {
  660. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noquestions", [], NULL_KEY, "");
  661. return;
  662. } else if (statuscode <= 0) {
  663. //sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  664. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  665. // Check if an error message was reported
  666. if (numlines > 1) sloodle_debug(llList2String(lines, 1));
  667. return;
  668. }
  669. // Are we loading the current question?
  670. integer iscurrent = (active_question == requesting_question);
  671. // Go through each line of the response
  672. integer i = 0;
  673. for (i = 0; i < numlines; i++) {
  674. // Extract and parse the current line
  675. list thisline = llParseString2List(llList2String(lines, i),["|"],[]);
  676. string rowtype = llList2String( thisline, 0 );
  677. // Check what type of line this is
  678. if ( rowtype == "question" ) {
  679. // Grab the question information and reset the options
  680. if (iscurrent) {
  681. qtext_current = llList2String(thisline, 4);
  682. qtype_current = llList2String(thisline, 7);
  683. opids_current = [];
  684. optext_current = [];
  685. opgrade_current = [];
  686. opfeedback_current = [];
  687. // Make sure it's a valid question type
  688. if (qtype_current != "multichoice") {
  689. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "invalidtype", [qtype_current], NULL_KEY, "quiz");
  690. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "resetting", [], NULL_KEY, "");
  691. state default;
  692. return;
  693. }
  694. } else {
  695. qloaded_next = requesting_question;
  696. qtext_next = llList2String(thisline, 4);
  697. qtype_next = llList2String(thisline, 7);
  698. opids_next = [];
  699. optext_next = [];
  700. opgrade_next = [];
  701. opfeedback_next = [];
  702. // Make sure it's a valid question type
  703. if (qtype_current != "multichoice") {
  704. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "invalidtype", [qtype_next], NULL_KEY, "quiz");
  705. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "resetting", [], NULL_KEY, "");
  706. state default;
  707. return;
  708. }
  709. }
  710. } else if ( rowtype == "questionoption" ) {
  711. // Add this option to the appropriate place
  712. if (iscurrent) {
  713. opids_current += [(integer)llList2String(thisline, 2)];
  714. optext_current += [llList2String(thisline, 4)];
  715. opgrade_current += [(float)llList2String(thisline, 5)];
  716. opfeedback_current += [llList2String(thisline, 6)];
  717. } else {
  718. opids_next += [(integer)llList2String(thisline, 2)];
  719. optext_next += [llList2String(thisline, 4)];
  720. opgrade_next += [(float)llList2String(thisline, 5)];
  721. opfeedback_next += [llList2String(thisline, 6)];
  722. }
  723. }
  724. }
  725. // Our response now depends on whether or not we just loaded the current question
  726. if (iscurrent) {
  727. // Just loaded the current question.
  728. // Is there another question after this one?
  729. if ((active_question + 1) < num_questions) {
  730. // Yes - load it
  731. requesting_question = active_question + 1;
  732. httpquizquery = request_question(llList2Integer(question_ids, requesting_question));
  733. }
  734. // Automatically ask this question
  735. ask_current_question();
  736. }
  737. }
  738. changed(integer change)
  739. {
  740. // reinitialise();
  741. }
  742. }
  743. // Please leave the following line intact to show where the script lives in Git:
  744. // SLOODLE LSL Script Git Location: mod/quiz_pile_on-1.0/objects/default/assets/sloodle_mod_quiz_pile_on-1.0.lslp