1
0

iassign-integration-functions.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // Função para ler parâmetros informados pelo iTarefa via URL
  2. // Apesar de não ser obrigatório, será muito útil para capturar os parâmetros
  3. function getParameterByName (name, defaultReturn = null) {
  4. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  5. return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : defaultReturn;
  6. }
  7. // Criando um object com os parâmetros informados pelo iTarefa
  8. // Observe que para cada parâmetro, é realizada a chamada do método getParameterByName, implementado acima
  9. var iLMparameters = {
  10. iLM_PARAM_ServerToGetAnswerURL: getParameterByName("iLM_PARAM_ServerToGetAnswerURL"),
  11. iLM_PARAM_SendAnswer: getParameterByName("iLM_PARAM_SendAnswer"),
  12. iLM_PARAM_AssignmentURL: getParameterByName("iLM_PARAM_AssignmentURL"),
  13. iLM_PARAM_Assignment: getParameterByName("iLM_PARAM_Assignment"),
  14. lang: getParameterByName("lang", "pt")
  15. };
  16. // Set the lang parameter to the localStorage for easy access
  17. // and no dependency to the global scope, avoind future 'strict mode' problems
  18. //localStorage.setItem('ivprog.lang', iLMparameters.lang);
  19. // Função chamada pelo iTarefa quando o professor finaliza a criação da atividade
  20. // ou quando o aluno finaliza a resolução do exercício
  21. // O retorno é um JSON com os dados do exercício ou da resolução
  22. // Esse retorno será armazenado no banco de dados do Moodle, pelo iTarefa
  23. function getAnswer () {
  24. // Se o parâmetro "iLM_PARAM_SendAnswer" for false,
  25. // então trata-se de resolução de atividade
  26. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  27. // Montar o retorno com a resposta do aluno
  28. var contentToSend = previousContent.split("\n::algorithm::")[0];
  29. contentToSend += '\n::algorithm::\n';
  30. contentToSend += JSON.stringify(window.program_obj);
  31. contentToSend += '\n::logs::';
  32. contentToSend += getTrackingLogs();
  33. return contentToSend;
  34. } else {
  35. // Montar o retorno com a criação da atividade do professor
  36. var ret = ' { ' + prepareTestCases()
  37. + ',\n"settings_data_types": \n' + JSON.stringify($('form[name="settings_data_types"]').serializeArray())
  38. + ',\n"settings_commands": \n' + JSON.stringify($('form[name="settings_commands"]').serializeArray())
  39. + ',\n"settings_functions": \n' + JSON.stringify($('form[name="settings_functions"]').serializeArray())
  40. + ' } ';
  41. if ($("input[name='include_algo']").is(':checked')) {
  42. ret += '\n::algorithm::\n';
  43. ret += JSON.stringify(window.program_obj);
  44. }
  45. return ret;
  46. }
  47. }
  48. function prepareTestCases () {
  49. var ret = ' \n "testcases" : [ '
  50. var test_cases_array = $('form[name="test_cases"]').serializeArray();
  51. for (var i = 0; i < test_cases_array.length; i = i + 2) {
  52. ret += '\n{ ';
  53. ret += '\n "input": [';
  54. var inps = test_cases_array[i].value.match(/[^\r\n]+/g);
  55. if (inps) {
  56. for (var j = 0; j < inps.length; j++) {
  57. ret += '"' + inps[j] + '"';
  58. if ((j + 1) < inps.length) {
  59. ret += ', ';
  60. }
  61. }
  62. }
  63. ret += '], \n "output": [';
  64. var outs = test_cases_array[i+1].value.match(/[^\r\n]+/g);
  65. if (outs) {
  66. for (var j = 0; j < outs.length; j++) {
  67. ret += '"' + outs[j] + '"';
  68. if ((j + 1) < outs.length) {
  69. ret += ', ';
  70. }
  71. }
  72. }
  73. ret += ']';
  74. ret += '\n}'
  75. if ((i + 2) < test_cases_array.length) {
  76. ret += ',';
  77. }
  78. }
  79. ret += '\n] ';
  80. return ret;
  81. }
  82. // Função chamada pelo iTarefa para receber a nota do aluno na atividade
  83. // O retorno é um valor entre 0.0 e 1.0
  84. function getEvaluation () {
  85. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  86. // A chamada do método abaixo é obrigatória!
  87. // Observe que a chamada parte do iLM para o iTarefa
  88. //parent.getEvaluationCallback(window.studentGrade);
  89. runCodeAssessment();
  90. }
  91. }
  92. var testCases = null;
  93. var settingsDataTypes = null;
  94. var settingsCommands = null;
  95. var settingsFunctions = null;
  96. var algorithm_in_ilm = null;
  97. var previousContent = null;
  98. // Função para que o iMA leia os dados da atividade fornecidos pelo iTarefa
  99. function getiLMContent () {
  100. // O parâmetro "iLM_PARAM_Assignment" fornece o URL do endereço que deve ser
  101. // requisitado via AJAX para a captura dos dados da atividade
  102. $.get(iLMparameters.iLM_PARAM_Assignment, function (data) {
  103. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  104. previousContent = data;
  105. prepareActivityToStudent(data);
  106. } else {
  107. }
  108. });
  109. }
  110. function prepareActivityToStudent (ilm_cont) {
  111. var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
  112. testCases = content.testcases;
  113. settingsDataTypes = content.settings_data_types;
  114. settingsCommands = content.settings_commands;
  115. settingsFunctions = content.settings_functions;
  116. if (ilm_cont.split('\n::algorithm::')[1]) {
  117. algorithm_in_ilm = ilm_cont.split('\n::algorithm::')[1].split('\n::logs::')[0];
  118. window.program_obj.functions = JSON.parse(algorithm_in_ilm).functions;
  119. window.program_obj.globals = JSON.parse(algorithm_in_ilm).globals;
  120. window.watchW.watch(window.program_obj.globals, function(){
  121. if (window.insertContext) {
  122. setTimeout(function(){ renderAlgorithm(); }, 300);
  123. window.insertContext = false;
  124. } else {
  125. renderAlgorithm();
  126. }
  127. }, 1);
  128. for (var i = 0; i < window.program_obj.functions.length; i ++) {
  129. window.watchW.watch(window.program_obj.functions[i].parameters_list, function(){
  130. if (window.insertContext) {
  131. setTimeout(function(){ renderAlgorithm(); }, 300);
  132. window.insertContext = false;
  133. } else {
  134. renderAlgorithm();
  135. }
  136. }, 1);
  137. window.watchW.watch(window.program_obj.functions[i].variables_list, function(){
  138. if (window.insertContext) {
  139. setTimeout(function(){ renderAlgorithm(); }, 300);
  140. window.insertContext = false;
  141. } else {
  142. renderAlgorithm();
  143. }
  144. }, 1);
  145. }
  146. window.watchW.watch(window.program_obj.functions, function(){
  147. if (window.insertContext) {
  148. setTimeout(function(){ renderAlgorithm(); }, 300);
  149. window.insertContext = false;
  150. } else {
  151. renderAlgorithm();
  152. }
  153. }, 1);
  154. }
  155. $('.assessment_button').removeClass('disabled');
  156. renderAlgorithm();
  157. }
  158. // Função para organizar se para criação, visualização ou resolução de atividade
  159. function prepareEnvironment () {
  160. if ((iLMparameters.iLM_PARAM_AssignmentURL == "true") && (iLMparameters.iLM_PARAM_SendAnswer == "true")) {
  161. prepareActivityCreation();
  162. }
  163. }
  164. $(document).ready(function() {
  165. // Se iLM_PARAM_SendAnswer for false, então trata-se de resolução de atividade,
  166. // portanto, a "DIV" de resolução é liberada
  167. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  168. //$('.resolucao').css("display","block");
  169. getiLMContent();
  170. $( document ).ready(function() {
  171. $('.div_to_body').mousemove(function(e) {
  172. trackingMatrix.push(adCoords(e, 0));
  173. });
  174. $('.div_to_body').click(function(e) {
  175. trackingMatrix.push(adCoords(e, 1));
  176. });
  177. });
  178. } else {
  179. // Caso não esteja em modo de resolução de atividade, a visualização no momento
  180. // é para a elaboração de atividade:
  181. //$('.elaboracao').css("display","block");
  182. }
  183. if (!testCases) {
  184. $('.assessment_button').addClass('disabled');
  185. }
  186. });
  187. // Função para preparar a interface para o professor criar atividade:
  188. function prepareActivityCreation () {
  189. $('.add_accordion').addClass('accordion');
  190. $('.default_visual_title').toggle();
  191. $('.default_visual_title').append('<span>'+LocalizedStrings.getUI('text_teacher_algorithm')+'</span>');
  192. $('.height_100').removeClass('height_100');
  193. $('.main_title').remove();
  194. $('.ui.accordion').addClass('styled');
  195. $('<div class="ui checkbox"><input type="checkbox" name="include_algo" class="include_algo" tabindex="0" class="hidden"><label>'+LocalizedStrings.getUI('text_teacher_algorithm_include')+'</label></div>').insertBefore('.content_margin');
  196. var cases_test_div = $('<div class="ui accordion styled"><div class="active title"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('text_teacher_test_case')+'</div><div class="active content"></div></div>');
  197. cases_test_div.insertBefore('.accordion');
  198. var config_div = $('<div class="ui accordion styled"><div class="title"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('text_teacher_config')+'</div><div class="content"></div></div>');
  199. config_div.insertAfter(cases_test_div);
  200. $('.ui.accordion').accordion();
  201. $('.ui.checkbox').checkbox();
  202. prepareTableSettings(config_div.find('.content'));
  203. prepareTableTestCases(cases_test_div.find('.content'));
  204. }
  205. function prepareTableTestCases (div_el) {
  206. var table_el = '<form name="test_cases"><table class="ui blue table"><thead><tr><th width="30px">#</th><th>'+LocalizedStrings.getUI('text_teacher_test_case_input')+'</th><th>'+LocalizedStrings.getUI('text_teacher_test_case_output')+'</th><th width="80px">'+LocalizedStrings.getUI('text_teacher_test_case_actions')+'</th></tr></thead>'
  207. + '<tbody class="content_cases"></tbody></table></form>';
  208. div_el.append(table_el);
  209. div_el.append('<button class="ui teal labeled icon button button_add_case"><i class="plus icon"></i>'+LocalizedStrings.getUI('text_teacher_test_case_add')+'</button>');
  210. $('.button_add_case').on('click', function(e) {
  211. addTestCase();
  212. });
  213. }
  214. var hist = false;
  215. function addTestCase () {
  216. var new_row = $('<tr><td class="counter"></td><td class="expandingArea"><textarea rows="1" name="input" class="text_area_input"></textarea></td><td class="expandingArea"><textarea rows="1" name="output" class="text_area_output"></textarea></td><td class="btn_actions"><div class="ui button_remove_case"><i class="red icon times large"></i></div></td></tr>');
  217. $('.content_cases').append(new_row);
  218. new_row.find('.button_remove_case').click(function(e) {
  219. new_row.remove();
  220. updateTestCaseCounter();
  221. });
  222. $('textarea').on('input', function(e) {
  223. var lines = $(this).val().split('\n').length;
  224. $(this).attr('rows', lines);
  225. });
  226. updateTestCaseCounter();
  227. $('.text_area_output').keydown(function(e) {
  228. var code = e.keyCode || e.which;
  229. if (code == 9 && $(this).closest("tr").is(":last-child")) {
  230. hist = true;
  231. addTestCase();
  232. }
  233. });
  234. if (!hist) {
  235. $( ".content_cases tr:last" ).find('.text_area_input').focus();
  236. } else {
  237. hist = false;
  238. }
  239. }
  240. function updateTestCaseCounter () {
  241. var i = 1;
  242. $( ".content_cases" ).find('tr').each(function() {
  243. $( this ).find('.counter').text(i);
  244. i ++;
  245. });
  246. }
  247. function prepareTableSettings (div_el) {
  248. div_el.append('<h4 class="ui header">'+LocalizedStrings.getUI('text_teacher_data_types')+'</h4>');
  249. div_el.append('<form name="settings_data_types"><div class="ui stackable five column grid">'
  250. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="integer_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('integer')+'</label></div></div>'
  251. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="real_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('real')+'</label></div></div>'
  252. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="text_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text')+'</label></div></div>'
  253. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="boolean_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('boolean')+'</label></div></div>'
  254. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="void_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('void')+'</label></div></div>'
  255. +'</div></form>');
  256. div_el.append('<h4 class="ui header">'+LocalizedStrings.getUI('text_teacher_commands')+'</h4>');
  257. div_el.append('<form name="settings_commands"><div class="ui stackable three column grid">'
  258. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_read" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_read_var')+'</label></div></div>'
  259. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_write" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_write_var')+'</label></div></div>'
  260. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_comment" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_comment')+'</label></div></div>'
  261. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_attribution" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_attribution')+'</label></div></div>'
  262. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_functioncall" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_functioncall')+'</label></div></div>'
  263. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_iftrue" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_iftrue')+'</label></div></div>'
  264. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_repeatNtimes" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_repeatNtimes')+'</label></div></div>'
  265. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_while" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_whiletrue')+'</label></div></div>'
  266. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_dowhile" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_dowhiletrue')+'</label></div></div>'
  267. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="commands_switch" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_switch')+'</label></div></div>'
  268. +'</div></form>');
  269. div_el.append('<h4 class="ui header">'+LocalizedStrings.getUI('text_teacher_functions')+'</h4>');
  270. div_el.append('<form name="settings_functions"><div class="ui stackable one column grid">'
  271. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="functions_creation" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_teacher_create_functions')+'</label></div></div>'
  272. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="functions_move" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_teacher_create_movement_functions')+'</label></div></div>'
  273. +'</div></form>');
  274. $('.ui.checkbox').checkbox();
  275. }
  276. function getTrackingLogs () {
  277. var ret = "";
  278. for (var i = 0; i < trackingMatrix.length; i++) {
  279. ret += "\n" + trackingMatrix[i][0] + "," + trackingMatrix[i][1] + "," + trackingMatrix[i][2];
  280. if (trackingMatrix[i][2] === 1) {
  281. ret += ',"' + trackingMatrix[i][3] + '"';
  282. }
  283. }
  284. return ret;
  285. }
  286. // Tracking mouse movements
  287. var trackingMatrix = [];
  288. function adCoords(e, code){
  289. var x = e.pageX;
  290. var y = e.pageY;
  291. if (code === 1) {
  292. return [x, y, code, e.target.classList['value']];
  293. } else {
  294. return [x, y, code];
  295. }
  296. }
  297. $( document ).ready(function() {
  298. if (inIframe()) {
  299. orderIcons();
  300. }
  301. });
  302. function orderIcons() {
  303. $('.ui.one.column.doubling.stackable.grid.container').css('display', 'none');
  304. $('.only_in_frame').css('display', 'block');
  305. }
  306. function inIframe () {
  307. try {
  308. return window.self !== window.top;
  309. } catch (e) {
  310. return true;
  311. }
  312. }
  313. function full_screen() {
  314. // check if user allows full screen of elements. This can be enabled or disabled in browser config. By default its enabled.
  315. //its also used to check if browser supports full screen api.
  316. if("fullscreenEnabled" in document || "webkitFullscreenEnabled" in document || "mozFullScreenEnabled" in document || "msFullscreenEnabled" in document) {
  317. if(document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled) {
  318. var element = document.getElementById("ui_main_div");
  319. //requestFullscreen is used to display an element in full screen mode.
  320. if("requestFullscreen" in element) {
  321. element.requestFullscreen();
  322. }
  323. else if ("webkitRequestFullscreen" in element) {
  324. element.webkitRequestFullscreen();
  325. }
  326. else if ("mozRequestFullScreen" in element) {
  327. element.mozRequestFullScreen();
  328. }
  329. else if ("msRequestFullscreen" in element) {
  330. element.msRequestFullscreen();
  331. }
  332. }
  333. } else {
  334. $('.expand_button').addClass('disabled');
  335. }
  336. }