integrationFunctions.js 8.8 KB

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