integrationFunctions.js 9.9 KB

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