integrationFunctions.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 += 'gameType:' + gameType
  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 += 'gameType:' + gameType
  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. };
  113. /**
  114. * Makes a GET request for the assignment file
  115. * and sends it to breakString() to treat its content.
  116. */
  117. const getiLMContent = function () {
  118. const url = iLMparameters.iLM_PARAM_Assignment;
  119. if (url == null) {
  120. console.error("Game error: iLMparameters.iLM_PARAM_Assignment empty (File with extension .frc not found).");
  121. } else {
  122. const init = { method: 'GET' };
  123. fetch(url, init)
  124. .then(response => {
  125. if (response.ok) {
  126. if (debugMode) console.log('Processing...');
  127. response.text().then(text => { breakString(text); }); // Sends text to be treated
  128. } else {
  129. console.error("Game error: Network response was not ok.")
  130. }
  131. })
  132. .catch(error => {
  133. console.error('Game error: problem with fetch operation - ' + error.message + '.');
  134. });
  135. }
  136. };
  137. /**
  138. * Receives the text from the assignment file,
  139. * breaks the string into a key/value
  140. * and sends it to updateGlobalVariables()
  141. * to update game variables before showing the screen.
  142. *
  143. * @param {string} text content of the .frc file
  144. */
  145. const breakString = function (text) {
  146. let gameInfo = {}, results;
  147. const lines = text.split('\n'); // Break by line
  148. lines.forEach(cur => {
  149. try {
  150. let line = cur.split(':'); // Break by key:value
  151. if (line[0] != 'results') {
  152. const key = line[0].replace(/^\s+|\s+$/g, ''); // Removes end character
  153. const value = line[1].replace(/^\s+|\s+$/g, '');
  154. gameInfo[key] = value;
  155. } else {
  156. results = line[1].replace(/^\s+|\s+$/g, '');
  157. }
  158. } catch (Error) { console.error('Game error: sintax error.'); }
  159. });
  160. if (results) {
  161. let i = 1;
  162. const curLevel = results.split('}'); // Remove }
  163. results = { l1: {}, l2: {}, l3: {}, l4: {} };
  164. curLevel.forEach(cur => {
  165. cur = cur.slice(1); // Remove {
  166. cur = cur.split(','); // Break by line
  167. cur.forEach(cur => {
  168. try {
  169. if (cur.length != 0) {
  170. let line = cur.split('='); // Break by key=value
  171. const key = line[0].replace(/^\s+|\s+$/g, ''); // Removes end char
  172. const value = line[1].replace(/^\s+|\s+$/g, ''); // Removes end char
  173. results['l' + i][key] = parseInt(value);
  174. }
  175. } catch (Error) { console.error('Game error: sintax error.'); }
  176. });
  177. i++;
  178. });
  179. }
  180. updateGlobalVariables(gameInfo, results);
  181. };
  182. /**
  183. * Updates game variables before starting the activity, then: <br>
  184. * - calls state 'customMenu' if the assignment WAS NOT previously completed. <br>
  185. * - calls state 'studentReport' otherwise.
  186. *
  187. * @param {object} info game information
  188. * @param {undefined|object} infoResults student answer (if there is any)
  189. */
  190. const updateGlobalVariables = function (infoGame, infoResults) {
  191. // Update game variables to content received from game file
  192. gameType = infoGame['gameType'];
  193. gameShape = infoGame['gameShape'];
  194. gameMode = infoGame['gameMode'];
  195. gameOperation = infoGame['gameOperation'];
  196. gameDifficulty = parseInt(infoGame['gameDifficulty']);
  197. fractionLabel = infoGame['fractionLabel'];
  198. // Update default values
  199. mapPosition = 0;
  200. mapMove = true;
  201. completedLevels = 0;
  202. // If the assignment WAS previously completed calls 'studentReport' after all is loaded.
  203. if (infoResults) {
  204. // Adds data about the student's report
  205. for (let i = 0; i < moodleVar.hits.length; i++) {
  206. moodleVar.hits[i] = infoResults['l' + (i + 1)].hits;
  207. moodleVar.errors[i] = infoResults['l' + (i + 1)].errors;
  208. moodleVar.time[i] = infoResults['l' + (i + 1)].timeElapsed;
  209. }
  210. game.state.start('studentReport');
  211. } else { // If assignment WAS NOT previously completed, calls 'customMenu' after all is loaded.
  212. game.state.start('customMenu');
  213. }
  214. };
  215. const moodleVar = {
  216. hits: [0, 0, 0, 0],
  217. errors: [0, 0, 0, 0],
  218. time: [0, 0, 0, 0]
  219. };