integrationFunctions.js 8.9 KB

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