integrationFunctions.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************************
  2. * LInE - Free Education, Private Data - http://www.usp.br/line
  3. *
  4. * This code is used EXCLUSIVELY when iFractions is runnign inside Moodle via iAssign
  5. * as an iLM (interactive learning module) and the global variable moodle=true.
  6. *
  7. * More about iAssign:
  8. * http://200.144.254.107/git/LInE/iassign
  9. *
  10. * More about creating iLM for iAssign (and the functions in this file):
  11. * https://www.ime.usp.br/~igormf/ima-tutorial/ (in pt-BR)
  12. *
  13. *************************************************************************************/
  14. /** [Functions used by iAssign]
  15. *
  16. * The iLM will be included in the HTML page as an iFrame,
  17. * therefore some parameters are going to be passed by the iAssign to the iLM via URL. <br>
  18. * This method will read these parameters.
  19. */
  20. function getParameterByName(name) {
  21. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  22. return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : null;
  23. };
  24. /** [Functions used by iAssign]
  25. *
  26. * This function is automaticaly called by iAssign in two different times: <br>
  27. *
  28. * - When a PROFESSOR finishes creating an new iLM and clicks "save". <br>
  29. * Returns: the iLM created (aka the values set by the professor in text form). <br>
  30. *
  31. * - When a STUDENT finishes solving an assignment and clicks "send". <br>
  32. * Returns: data about the student's progress (aka the student's progress in text form). <br>
  33. *
  34. * @returns {string} the data that will be received by iAssign and stored on the database (a game file with extension .frc)
  35. */
  36. function getAnswer() {
  37. let str = '';
  38. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') { // Student - sending results
  39. str += 'gameTypeString:' + gameTypeString
  40. + '\ngameShape:' + gameShape
  41. + '\ngameMode:' + gameMode
  42. + '\ngameOperation:' + gameOperation
  43. + '\ngameDifficulty:' + gameDifficulty
  44. + '\nfractionLabel:' + fractionLabel
  45. + '\nresults:';
  46. for (let i = 0; i < moodleVar.hits.length; i++) {
  47. str += '{level=' + (i + 1)
  48. + ',hits=' + moodleVar.hits[i]
  49. + ',errors=' + moodleVar.errors[i]
  50. + ',timeElapsed=' + moodleVar.time[i]
  51. + '}';
  52. }
  53. } else { // Professor - creating new assignment
  54. if (!gameType) {
  55. alert(game.lang.error_must_select_game);
  56. return x;
  57. }
  58. moodleVar.hits = [0, 0, 0, 0];
  59. moodleVar.errors = [0, 0, 0, 0];
  60. moodleVar.time = [0, 0, 0, 0];
  61. str += 'gameTypeString:' + gameTypeString
  62. + '\ngameShape:' + gameShape
  63. + '\ngameMode:' + gameMode
  64. + '\ngameOperation:' + gameOperation
  65. + '\ngameDifficulty:' + gameDifficulty
  66. + '\nfractionLabel:' + fractionLabel;
  67. }
  68. return str;
  69. };
  70. /** [Functions used by iAssign]
  71. *
  72. * This function must be present if the iMA uses automatic evaluation. <br>
  73. * It is is called by iAssign after the student submits a solution
  74. * and the data is sent to the moodle database.
  75. *
  76. * @returns {number} student's grade for the current assignment : real number between 0.0 and 1.0
  77. */
  78. function getEvaluation() {
  79. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') { // Student
  80. let i;
  81. for (i = 0; i < moodleVar.hits.length && moodleVar.hits[i] == 1; i++);
  82. const grade = i / 4;
  83. parent.getEvaluationCallback(grade); // Sends grade to moodle db
  84. return grade;
  85. }
  86. };
  87. /** [Functions used by iAssign]
  88. *
  89. * Holds the parameters passed by the iAssign to the iLM via URL.
  90. */
  91. const iLMparameters = {
  92. /**
  93. * This parameter gets the role in which the iLM is being used: <br>
  94. * - if true, the user is creating a new iLM (as professor) <br>
  95. * - if false, the user is solving the iLM (as student), value=false
  96. */
  97. iLM_PARAM_SendAnswer: getParameterByName("iLM_PARAM_SendAnswer"), // Checks if you're student (false) or professor (true)
  98. /**
  99. * This parameter is used when the user is opening an iLM to solve it. <br>
  100. * It holds a URL with the path to the game file (assignment/iLM) created by the professor. <br>
  101. * Example: http://myschool.edu/moodle/mod/iassign/ilm_security.php?id=3&token=b3660dd4de0b0e9bb01fea6cc8f02ccd&view=1
  102. *
  103. * The first parameter, 'token', can be used only once.
  104. * Once the iLM gets the game file, the token is destroied (for security).
  105. */
  106. iLM_PARAM_Assignment: getParameterByName("iLM_PARAM_Assignment"),
  107. /**
  108. * Gets current moodle language.
  109. */
  110. lang: getParameterByName("lang"),
  111. iLM_PARAM_ServerToGetAnswerURL: getParameterByName("iLM_PARAM_ServerToGetAnswerURL"),
  112. iLM_PARAM_ServerToGetAnswerURL: getParameterByName("iLM_PARAM_ServerToGetAnswerURL")
  113. };
  114. /**
  115. * Makes a GET request for the assignment file
  116. * and sends it to breakString() to treat its content.
  117. */
  118. const getiLMContent = function () {
  119. const url = iLMparameters.iLM_PARAM_Assignment;
  120. if (url == null) {
  121. console.error("Game error: iLMparameters.iLM_PARAM_Assignment empty (File with extension .frc not found).");
  122. } else {
  123. const init = { method: 'GET' };
  124. fetch(url, init)
  125. .then(response => {
  126. if (response.ok) {
  127. if (debugMode) console.log('Processing...');
  128. response.text().then(text => { breakString(text); }); // Sends text to be treated
  129. } else {
  130. console.error("Game error: Network response was not ok.")
  131. }
  132. })
  133. .catch(error => {
  134. console.error('Game error: problem with fetch operation - ' + error.message + '.');
  135. });
  136. }
  137. };
  138. /**
  139. * Receives the text from the assignment file,
  140. * breaks the string into a key/value
  141. * and sends it to updateGlobalVariables()
  142. * to update game variables before showing the screen.
  143. *
  144. * @param {string} text content of the .frc file
  145. */
  146. const breakString = function (text) {
  147. let gameInfo = {}, results;
  148. const lines = text.split('\n'); // Break by line
  149. lines.forEach(cur => {
  150. try {
  151. let line = cur.split(':'); // Break by key:value
  152. if (line[0] != 'results') {
  153. const key = line[0].replace(/^\s+|\s+$/g, ''); // Removes end character
  154. const value = line[1].replace(/^\s+|\s+$/g, '');
  155. gameInfo[key] = value;
  156. } else {
  157. results = line[1].replace(/^\s+|\s+$/g, '');
  158. }
  159. } catch (Error) { console.error('Game error: sintax error.'); }
  160. });
  161. if (results) {
  162. let i = 1;
  163. const curLevel = results.split('}'); // Remove }
  164. results = { l1: {}, l2: {}, l3: {}, l4: {} };
  165. curLevel.forEach(cur => {
  166. cur = cur.slice(1); // Remove {
  167. cur = cur.split(','); // Break by line
  168. cur.forEach(cur => {
  169. try {
  170. if (cur.length != 0) {
  171. let line = cur.split('='); // Break by key=value
  172. const key = line[0].replace(/^\s+|\s+$/g, ''); // Removes end char
  173. const value = line[1].replace(/^\s+|\s+$/g, ''); // Removes end char
  174. results['l' + i][key] = parseInt(value);
  175. }
  176. } catch (Error) { console.error('Game error: sintax error.'); }
  177. });
  178. i++;
  179. });
  180. }
  181. updateGlobalVariables(gameInfo, results);
  182. };
  183. /**
  184. * Updates game variables before starting the activity, then: <br>
  185. * - calls state 'customMenu' if the assignment WAS NOT previously completed. <br>
  186. * - calls state 'studentReport' otherwise.
  187. *
  188. * @param {object} info game information
  189. * @param {undefined|object} infoResults student answer (if there is any)
  190. */
  191. const updateGlobalVariables = function (info, infoResults) {
  192. // Update game variables to content received from game file
  193. gameTypeString = info['gameTypeString'];
  194. gameShape = info['gameShape'];
  195. gameMode = info['gameMode'];
  196. gameOperation = info['gameOperation'];
  197. gameDifficulty = parseInt(info['gameDifficulty']);
  198. fractionLabel = info['fractionLabel'];
  199. // Update default values
  200. mapPosition = 0;
  201. mapMove = true;
  202. completedLevels = 0;
  203. // If the assignment WAS previously completed calls 'studentReport' after all is loaded.
  204. if (infoResults) {
  205. // Adds data about the student's report
  206. for (let i = 0; i < moodleVar.hits.length; i++) {
  207. moodleVar.hits[i] = infoResults['l' + (i + 1)].hits;
  208. moodleVar.errors[i] = infoResults['l' + (i + 1)].errors;
  209. moodleVar.time[i] = infoResults['l' + (i + 1)].timeElapsed;
  210. }
  211. game.state.start('studentReport');
  212. } else { // If assignment WAS NOT previously completed, calls 'customMenu' after all is loaded.
  213. game.state.start('customMenu');
  214. }
  215. };
  216. const moodleVar = {
  217. hits: [0, 0, 0, 0],
  218. errors: [0, 0, 0, 0],
  219. time: [0, 0, 0, 0]
  220. };