sloodle_mod_choice-1.0.lslp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Choice (for Sloodle 0.3)
  4. // Allows avatars to interact graphically with a Moodle choice.
  5. //
  6. // Copyright (c) 2007-8 Sloodle
  7. // Released under the GNU GPL
  8. //
  9. // Contributors:
  10. // Peter R. Bloomfield
  11. // Paul Preibisch (Fire Centaur in SL)
  12. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  13. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  14. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  15. integer SLOODLE_CHANNEL_OBJECT_CHOICE = -1639270051;
  16. string SLOODLE_CHOICE_LINKER = "/mod/sloodle/mod/choice-1.0/linker.php";
  17. string SLOODLE_EOF = "sloodleeof";
  18. string SLOODLE_OBJECT_TYPE = "choice-1.0";
  19. // Choice commands
  20. // Update the specified option. Followed by "|num|text|colour|count|prop"
  21. // - num is a local option identifier
  22. // - text is the caption to display for this option
  23. // - colour is a colour vector (cast to a string)
  24. // - count is the number selected so far (or -1 if we don't want to display any)
  25. // - prop is the proportion of maximum size to show (between 0 and 1)
  26. string SLOODLE_CHOICE_UPDATE_OPTION = "do:updateoption";
  27. // Update the choice text. Followed by "|text"
  28. string SLOODLE_CHOICE_UPDATE_TEXT = "do:updatetext";
  29. // Select the specified option. Followed by "|num" (num is a local option identifier).
  30. // The UUID of the toucher should be passed as the key value.
  31. string SLOODLE_CHOICE_SELECT_OPTION = "do:selectoption";
  32. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  33. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  34. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  35. string sloodleserverroot = "";
  36. string sloodlepwd = "";
  37. integer sloodlecontrollerid = 0;
  38. integer sloodlemoduleid = 0;
  39. integer sloodlerefreshtime = 600; // Number of seconds between automatic refreshes
  40. integer sloodlerelative = FALSE; // Should the results be displayed in a relativistic way?
  41. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  42. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  43. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  44. integer eof = FALSE; // Have we reached the end of the configuration data?
  45. key httpstatus = NULL_KEY; // Request to check status of choice
  46. list httpselect = []; // Requests to make selections
  47. string choicetext = ""; // The choice text... i.e. the question being asked.
  48. list optionids = []; // List of IDs of available options (integers)
  49. list optiontexts = []; // List of option texts (strings)
  50. list optionselections = []; // Number of times each option has been selected (integers)
  51. integer numunanswered = -1; // Number of people who have not answered yet
  52. // A list of colors for the options (note: after running out of colours, the list will wrap around)
  53. list optioncolours = [
  54. <1.0, 0.0, 0.0>, // Red
  55. <0.0, 1.0, 0.0>, // Green
  56. <0.0, 0.0, 1.0>, // Blue
  57. <1.0, 1.0, 0.0>, // Yellow
  58. <1.0, 0.0, 1.0>, // Magenta
  59. <0.0, 1.0, 1.0>, // Cyan
  60. <0.5, 0.0, 0.0>, // Dark red
  61. <0.0, 0.5, 0.0>, // Dark green
  62. <0.0, 0.0, 0.5>, // Dark blue
  63. <1.0, 0.5, 0.0>, // Orange
  64. <0.5, 0.0, 0.5>, // Purple
  65. <0.0, 0.5, 0.5> // Indigo
  66. ];
  67. ///// TRANSLATION /////
  68. // Link message channels
  69. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  70. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  71. // Translation output methods
  72. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  73. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  74. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  75. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  76. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  77. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  78. 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.
  79. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  80. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  81. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  82. // Send a translation request link message
  83. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  84. {
  85. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  86. }
  87. ///// ----------- /////
  88. ///// FUNCTIONS /////
  89. /******************************************************************************************************************************
  90. * sloodle_error_code -
  91. * Author: Paul Preibisch
  92. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  93. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  94. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  95. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  96. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  97. *******************************************************************************************************************************/
  98. sloodle_error_code(string method, key avuuid,integer statuscode){
  99. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  100. }
  101. sloodle_debug(string msg)
  102. {
  103. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  104. }
  105. // Configure by receiving a linked message from another script in the object
  106. // Returns TRUE if the object has all the data it needs
  107. integer sloodle_handle_command(string str)
  108. {
  109. list bits = llParseString2List(str,["|"],[]);
  110. integer numbits = llGetListLength(bits);
  111. string name = llList2String(bits,0);
  112. string value1 = "";
  113. string value2 = "";
  114. if (numbits > 1) value1 = llList2String(bits,1);
  115. if (numbits > 2) value2 = llList2String(bits,2);
  116. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  117. else if (name == "set:sloodlepwd") {
  118. // The password may be a single prim password, or a UUID and a password
  119. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  120. else sloodlepwd = value1;
  121. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  122. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  123. else if (name == "set:sloodlerefreshtime") sloodlerefreshtime = (integer)value1;
  124. else if (name == "set:sloodlerelative") sloodlerelative = (integer)value1;
  125. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  126. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  127. else if (name == SLOODLE_EOF) eof = TRUE;
  128. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  129. }
  130. // Checks if the given agent is permitted to user this object
  131. // Returns TRUE if so, or FALSE if not
  132. integer sloodle_check_access_use(key id)
  133. {
  134. // Check the access mode
  135. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  136. return llSameGroup(id);
  137. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  138. return TRUE;
  139. }
  140. // Assume it's owner mode
  141. return (id == llGetOwner());
  142. }
  143. // Gets the colour for a specified option (local option number)
  144. vector get_option_colour(integer num)
  145. {
  146. // Make sure we don't run out of colours
  147. num = num % llGetListLength(optioncolours);
  148. return llList2Vector(optioncolours, num);
  149. }
  150. // Send a reset command to all options
  151. send_reset()
  152. {
  153. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_CHOICE, "do:reset", NULL_KEY);
  154. }
  155. // Send an update to all parts of the display
  156. send_update()
  157. {
  158. // Update the choice text itself
  159. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_CHOICE, SLOODLE_CHOICE_UPDATE_TEXT + "|" + choicetext, NULL_KEY);
  160. // We want to determine the value that represents a full bar on the graph.
  161. // In relative mode, this is the maximum number of selections for a given option (so everything is relative to that).
  162. // Otherwise, it is the total number of people who have selected or could have selected.
  163. integer fullbar = 0;
  164. integer num_options = llGetListLength(optionids);
  165. integer i = 0;
  166. integer numsels = 0;
  167. for (i = 0; i < num_options; i++) {
  168. // Get the number of selections for this option
  169. numsels = llList2Integer(optionselections, i);
  170. // Relative mode?
  171. if (sloodlerelative) {
  172. // Store the maximum
  173. if (numsels > fullbar) fullbar = numsels;
  174. } else {
  175. // Add to the total
  176. fullbar += numsels;
  177. }
  178. }
  179. // Add the number who have not answered yet, if necessary
  180. if (sloodlerelative == 0 && numunanswered > 0) fullbar += numunanswered;
  181. // Go through each option we have
  182. string data = "";
  183. for (i = 0; i < num_options; i++) {
  184. // Send all the data
  185. data = SLOODLE_CHOICE_UPDATE_OPTION;
  186. data += "|" + (string)i;
  187. data += "|" + llList2String(optiontexts, i);
  188. data += "|" + (string)get_option_colour(i);
  189. data += "|" + (string)llList2Integer(optionselections, i);
  190. // Do we have results to add?
  191. numsels = llList2Integer(optionselections, i);
  192. if (numsels > 0 && fullbar > 0) {
  193. // At this point, all results are relative to the 'fullbar' value
  194. data += "|" + (string)((float)numsels / (float)fullbar);
  195. } else {
  196. data += "|0.0";
  197. }
  198. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_CHOICE, data, NULL_KEY);
  199. }
  200. }
  201. // Request an update of the choice status.
  202. // Returns the HTTP key.
  203. key request_status()
  204. {
  205. // Send a request to select the option
  206. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  207. body += "&sloodlepwd=" + sloodlepwd;
  208. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  209. return llHTTPRequest(sloodleserverroot + SLOODLE_CHOICE_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  210. }
  211. ///// STATES /////
  212. // Default state - waiting for configuration
  213. default
  214. {
  215. state_entry()
  216. {
  217. // Starting again with a new configuration
  218. llSetText("", <0.0,0.0,0.0>, 0.0);
  219. isconfigured = FALSE;
  220. eof = FALSE;
  221. // Reset our data
  222. sloodleserverroot = "";
  223. sloodlepwd = "";
  224. sloodlecontrollerid = 0;
  225. sloodlemoduleid = 0;
  226. sloodlerefreshtime = 0;
  227. sloodleobjectaccessleveluse = 0;
  228. sloodleserveraccesslevel = 0;
  229. // Reset our display
  230. send_reset();
  231. }
  232. link_message( integer sender_num, integer num, string str, key id)
  233. {
  234. // Check the channel
  235. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  236. // Split the message into lines
  237. list lines = llParseString2List(str, ["\n"], []);
  238. integer numlines = llGetListLength(lines);
  239. integer i = 0;
  240. for (; i < numlines; i++) {
  241. isconfigured = sloodle_handle_command(llList2String(lines, i));
  242. }
  243. // If we've got all our data AND reached the end of the configuration data, then move on
  244. if (eof == TRUE) {
  245. if (isconfigured == TRUE) {
  246. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  247. state ready;
  248. } else {
  249. // Go all configuration but, it's not complete... request reconfiguration
  250. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  251. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  252. eof = FALSE;
  253. }
  254. }
  255. }
  256. }
  257. touch_start(integer num_detected)
  258. {
  259. // Attempt to request a reconfiguration
  260. if (llDetectedKey(0) == llGetOwner()) {
  261. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  262. }
  263. }
  264. }
  265. state ready
  266. {
  267. on_rez( integer param)
  268. {
  269. state default;
  270. }
  271. state_entry()
  272. {
  273. // Start by requesting an update
  274. httpstatus = request_status();
  275. httpselect = [];
  276. // If we've been given a refresh timer, then use it
  277. llSetTimerEvent(0.0);
  278. if (sloodlerefreshtime > 0) {
  279. // Validate the value - shouldn't check more often than every 10 seconds
  280. if (sloodlerefreshtime < 10) sloodlerefreshtime = 10;
  281. llSetTimerEvent((float)sloodlerefreshtime);
  282. }
  283. }
  284. timer()
  285. {
  286. // Request another status update
  287. httpstatus = request_status();
  288. }
  289. touch_start(integer total_number)
  290. {
  291. // Request another status update (but only if it this exact prim which was touched)
  292. if (llDetectedLinkNumber(0) == llGetLinkNumber()) {
  293. httpstatus = request_status();
  294. }
  295. }
  296. http_response(key id, integer status, list meta, string body)
  297. {
  298. // Is this a status update?
  299. if (id == httpstatus) {
  300. httpstatus = NULL_KEY;
  301. //sloodle_debug("HTTP response: " + body);
  302. // Make sure the HTTP response was OK
  303. if (status != 200) {
  304. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  305. return;
  306. }
  307. // Parse the response data
  308. list lines = llParseStringKeepNulls(body, ["\n"], []);
  309. integer numlines = llGetListLength(lines);
  310. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  311. integer statuscode = (integer)llList2String(statusfields, 0);
  312. // Did an error occur?
  313. if (statuscode <= 0) {
  314. //sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  315. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  316. sloodle_debug(body);
  317. return;
  318. }
  319. // Make sure we have enough body data
  320. if (numlines < 5) {
  321. sloodle_debug("Not enough response data.");
  322. return;
  323. }
  324. // Extract the necessary choice data
  325. choicetext = llList2String(lines, 1) + "\n\"" + llList2String(lines, 2) + "\"";
  326. numunanswered = (integer)llList2String(lines, 4);
  327. // (There is more data provided that we could use later)
  328. // Determine how many options there are.
  329. // If there are a different number now than we already had, then reset all our options
  330. integer numoptions = numlines - 5;
  331. if (numoptions != llGetListLength(optionids)) {
  332. send_reset();
  333. }
  334. // Reset our data
  335. optionids = [];
  336. optiontexts = [];
  337. optionselections = [];
  338. // Go through each option
  339. integer i = 5;
  340. list fields = [];
  341. for (; i < numlines; i++) {
  342. // Parse the option data
  343. fields = llParseStringKeepNulls(llList2String(lines, i), ["|"], []);
  344. if (llGetListLength(fields) >= 3) {
  345. optionids += [(integer)llList2String(fields, 0)];
  346. optiontexts += [llList2String(fields, 1)];
  347. optionselections += [(integer)llList2String(fields, 2)];
  348. }
  349. }
  350. sloodle_debug("Number of options received: " + (string)llGetListLength(optionids));
  351. // Update all our components
  352. send_update();
  353. return;
  354. }
  355. // Is this a selection response?
  356. integer pos = llListFindList(httpselect, [id]);
  357. if (pos >= 0) {
  358. httpselect = llDeleteSubList(httpselect, pos, pos);
  359. // Make sure the HTTP response was OK
  360. if (status != 200) {
  361. sloodle_debug("HTTP request failed with status code " + (string)status);
  362. return;
  363. }
  364. // Parse the response
  365. list lines = llParseStringKeepNulls(body, ["\n"], []);
  366. integer numlines = llGetListLength(lines);
  367. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  368. integer numstatusfields = llGetListLength(statusfields);
  369. if (numstatusfields < 7) return;
  370. // Extract the relevant data
  371. integer statuscode = (integer)llList2String(statusfields, 0);
  372. key uuid = (key)llList2String(statusfields, 6);
  373. string name = llKey2Name(uuid);
  374. // Did an error occur?
  375. if (statuscode == -10011) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noupdate", [name], NULL_KEY, "choice");
  376. else if (statuscode == -10012) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "maxselections", [name], NULL_KEY, "choice");
  377. else if (statuscode == -10013) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "notopen", [name], NULL_KEY, "choice");
  378. else if (statuscode == -10014) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "closed", [name], NULL_KEY, "choice");
  379. else if (statuscode == -10016) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "selectionerror", [name], NULL_KEY, "choice");
  380. else if (statuscode <= 0) {
  381. // sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  382. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  383. }
  384. if (statuscode <= 0) {
  385. sloodle_debug(body);
  386. return;
  387. }
  388. // What was the nature of the selection?
  389. if (statuscode == 10012) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "selectionupdated", [name], NULL_KEY, "choice");
  390. else if (statuscode == 10013) sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "selectionalreadymade", [name], NULL_KEY, "choice");
  391. else sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "selectionmade", [name], NULL_KEY, "choice");
  392. // Make sure to update the status
  393. httpstatus = request_status();
  394. return;
  395. }
  396. }
  397. link_message(integer sender_num, integer num, string sval, key kval)
  398. {
  399. // Check the channel
  400. if (num == SLOODLE_CHANNEL_OBJECT_CHOICE) {
  401. // Parse the string
  402. list parts = llParseString2List(sval, ["|"], []);
  403. integer numparts = llGetListLength(parts);
  404. string cmd = llList2String(parts, 0);
  405. // Check the command
  406. if (cmd == SLOODLE_CHOICE_SELECT_OPTION && kval != NULL_KEY && numparts > 1) {
  407. // Determine which option ID is being selected
  408. integer optionnum = (integer)llList2String(parts, 1);
  409. if (optionnum < 0 || optionnum >= llGetListLength(optionids)) return;
  410. integer optionid = llList2Integer(optionids, optionnum);
  411. string name = llKey2Name(kval);
  412. sloodle_debug("Selecting option ID " + (string)optionid + " for " + name);
  413. // Send a request to select the option
  414. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  415. body += "&sloodlepwd=" + sloodlepwd;
  416. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  417. body += "&sloodleuuid=" + (string)kval;
  418. body += "&sloodleavname=" + llEscapeURL(name);
  419. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  420. body += "&sloodleoptionid=" + (string)optionid;
  421. key newhttp = llHTTPRequest(sloodleserverroot + SLOODLE_CHOICE_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  422. httpselect += [newhttp];
  423. }
  424. } else if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  425. if (sval == "do:reset") llResetScript();
  426. }
  427. }
  428. }
  429. // Please leave the following line intact to show where the script lives in Git:
  430. // SLOODLE LSL Script Git Location: mod/choice-1.0/objects/default/assets/sloodle_mod_choice-1.0.lslp