integrationFunctions.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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') {
  39. // Student - sending results
  40. str +=
  41. 'gameName:' +
  42. gameName +
  43. '\ngameShape:' +
  44. gameShape +
  45. '\ngameMode:' +
  46. gameMode +
  47. '\ngameOperation:' +
  48. gameOperation +
  49. '\ngameDifficulty:' +
  50. gameDifficulty +
  51. '\nshowFractions:' +
  52. showFractions +
  53. '\nresults:';
  54. for (let i = 0; i < moodleVar.hits.length; i++) {
  55. str +=
  56. '{level=' +
  57. (i + 1) +
  58. ',hits=' +
  59. moodleVar.hits[i] +
  60. ',errors=' +
  61. moodleVar.errors[i] +
  62. ',timeElapsed=' +
  63. moodleVar.time[i] +
  64. '}';
  65. }
  66. } else {
  67. // Professor - creating new assignment
  68. if (!gameName) {
  69. alert(game.lang.error_must_select_game);
  70. return x;
  71. }
  72. moodleVar.hits = [0, 0, 0, 0];
  73. moodleVar.errors = [0, 0, 0, 0];
  74. moodleVar.time = [0, 0, 0, 0];
  75. str +=
  76. 'gameName:' +
  77. gameName +
  78. '\ngameShape:' +
  79. gameShape +
  80. '\ngameMode:' +
  81. gameMode +
  82. '\ngameOperation:' +
  83. gameOperation +
  84. '\ngameDifficulty:' +
  85. gameDifficulty +
  86. '\nshowFractions:' +
  87. showFractions;
  88. }
  89. return str;
  90. }
  91. /** [Functions used by iAssign]
  92. *
  93. * This function must be present if the iMA uses automatic evaluation. <br>
  94. * It is is called by iAssign after the student submits a solution
  95. * and the data is sent to the moodle database.
  96. *
  97. * @returns {number} student's grade for the current assignment : real number between 0.0 and 1.0
  98. */
  99. function getEvaluation() {
  100. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  101. // Student
  102. let i;
  103. for (i = 0; i < moodleVar.hits.length && moodleVar.hits[i] == 1; i++);
  104. const grade = i / 4;
  105. parent.getEvaluationCallback(grade); // Sends grade to moodle db
  106. return grade;
  107. }
  108. }
  109. /** [Functions used by iAssign]
  110. *
  111. * Holds the parameters passed by the iAssign to the iLM via URL.
  112. */
  113. const iLMparameters = {
  114. /**
  115. * This parameter gets the role in which the iLM is being used: <br>
  116. * - if true, the user is creating a new iLM (as professor) <br>
  117. * - if false, the user is solving the iLM (as student), value=false
  118. */
  119. iLM_PARAM_SendAnswer: getParameterByName('iLM_PARAM_SendAnswer'), // Checks if you're student (false) or professor (true)
  120. /**
  121. * This parameter is used when the user is opening an iLM to solve it. <br>
  122. * It holds a URL with the path to the game file (assignment/iLM) created by the professor. <br>
  123. * Example: http://myschool.edu/moodle/mod/iassign/ilm_security.php?id=3&token=b3660dd4de0b0e9bb01fea6cc8f02ccd&view=1
  124. *
  125. * The first parameter, 'token', can be used only once.
  126. * Once the iLM gets the game file, the token is destroied (for security).
  127. */
  128. iLM_PARAM_Assignment: getParameterByName('iLM_PARAM_Assignment'),
  129. /**
  130. * Gets current moodle language.
  131. */
  132. lang: getParameterByName('lang'),
  133. iLM_PARAM_ServerToGetAnswerURL: getParameterByName(
  134. 'iLM_PARAM_ServerToGetAnswerURL'
  135. ),
  136. };
  137. /**
  138. * Makes a GET request for the assignment file
  139. * and sends it to breakString() to treat its content.
  140. */
  141. const getiLMContent = function () {
  142. const url = iLMparameters.iLM_PARAM_Assignment;
  143. if (url == null) {
  144. console.error(
  145. 'Game error: iLMparameters.iLM_PARAM_Assignment empty (File with extension .frc not found).'
  146. );
  147. } else {
  148. const init = { method: 'GET' };
  149. fetch(url, init)
  150. .then((response) => {
  151. if (response.ok) {
  152. if (isDebugMode) console.log('Processing...');
  153. response.text().then((text) => {
  154. breakString(text);
  155. }); // Sends text to be treated
  156. } else {
  157. console.error('Game error: Network response was not ok.');
  158. }
  159. })
  160. .catch((error) => {
  161. console.error(
  162. 'Game error: problem with fetch operation - ' + error.message + '.'
  163. );
  164. });
  165. }
  166. };
  167. /**
  168. * Receives the text from the assignment file,
  169. * breaks the string into a key/value
  170. * and sends it to updateGlobalVariables()
  171. * to update game variables before showing the screen.
  172. *
  173. * @param {string} text content of the .frc file
  174. */
  175. const breakString = function (text) {
  176. let gameInfo = {},
  177. results;
  178. const lines = text.split('\n'); // Break by line
  179. lines.forEach((cur) => {
  180. try {
  181. let line = cur.split(':'); // Break by key:value
  182. if (line[0] != 'results') {
  183. const key = line[0].replace(/^\s+|\s+$/g, ''); // Removes end character
  184. const value = line[1].replace(/^\s+|\s+$/g, '');
  185. gameInfo[key] = value;
  186. } else {
  187. results = line[1].replace(/^\s+|\s+$/g, '');
  188. }
  189. } catch (Error) {
  190. console.error('Game error: sintax error.');
  191. }
  192. });
  193. if (results) {
  194. let i = 1;
  195. const curLevel = results.split('}'); // Remove }
  196. results = { l1: {}, l2: {}, l3: {}, l4: {} };
  197. curLevel.forEach((cur) => {
  198. cur = cur.slice(1); // Remove {
  199. cur = cur.split(','); // Break by line
  200. cur.forEach((cur) => {
  201. try {
  202. if (cur.length != 0) {
  203. let line = cur.split('='); // Break by key=value
  204. const key = line[0].replace(/^\s+|\s+$/g, ''); // Removes end char
  205. const value = line[1].replace(/^\s+|\s+$/g, ''); // Removes end char
  206. results['l' + i][key] = parseInt(value);
  207. }
  208. } catch (Error) {
  209. console.error('Game error: sintax error.');
  210. }
  211. });
  212. i++;
  213. });
  214. }
  215. updateGlobalVariables(gameInfo, results);
  216. };
  217. /**
  218. * Updates game variables before starting the activity, then: <br>
  219. * - calls state 'customMenu' if the assignment WAS NOT previously completed. <br>
  220. * - calls state 'studentReport' otherwise.
  221. *
  222. * @param {object} info game information
  223. * @param {undefined|object} infoResults student answer (if there is any)
  224. */
  225. const updateGlobalVariables = function (infoGame, infoResults) {
  226. // Update game variables to content received from game file
  227. gameName = infoGame['gameName'];
  228. gameShape = infoGame['gameShape'];
  229. gameMode = infoGame['gameMode'];
  230. gameOperation = infoGame['gameOperation'];
  231. gameDifficulty = parseInt(infoGame['gameDifficulty']);
  232. showFractions = infoGame['showFractions'];
  233. // Update default values
  234. curMapPosition = 0;
  235. canGoToNextMapPosition = true;
  236. completedLevels = 0;
  237. // If the assignment WAS previously completed calls 'studentReport' after all is loaded.
  238. if (infoResults) {
  239. // Adds data about the student's report
  240. for (let i = 0; i < moodleVar.hits.length; i++) {
  241. moodleVar.hits[i] = infoResults['l' + (i + 1)].hits;
  242. moodleVar.errors[i] = infoResults['l' + (i + 1)].errors;
  243. moodleVar.time[i] = infoResults['l' + (i + 1)].timeElapsed;
  244. }
  245. game.state.start('studentReport');
  246. } else {
  247. // If assignment WAS NOT previously completed, calls 'customMenu' after all is loaded.
  248. game.state.start('customMenu');
  249. }
  250. };
  251. moodleVar = {
  252. hits: [0, 0, 0, 0],
  253. errors: [0, 0, 0, 0],
  254. time: [0, 0, 0, 0],
  255. };