integrationFunctions.js 8.8 KB

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