iassign-integration-functions.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. // Aluno está trabalhando em alguma atividade:
  104. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  105. previousContent = data;
  106. prepareActivityToStudent(data);
  107. } else { // Professor está editando uma atividade:
  108. previousContent = data;
  109. prepareActivityToEdit(data);
  110. }
  111. });
  112. }
  113. function prepareActivityToEdit (ilm_cont) {
  114. var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
  115. testCases = content.testcases;
  116. settingsDataTypes = content.settings_data_types;
  117. settingsCommands = content.settings_commands;
  118. settingsFunctions = content.settings_functions;
  119. for (var i = 0; i < testCases.length; i++) {
  120. addTestCase(testCases[i]);
  121. }
  122. if (ilm_cont.split('\n::algorithm::')[1]) {
  123. algorithm_in_ilm = ilm_cont.split('\n::algorithm::')[1].split('\n::logs::')[0];
  124. $("input[name='include_algo']").prop('checked', true);
  125. includePreviousAlgorithm();
  126. renderAlgorithm();
  127. }
  128. }
  129. function includePreviousAlgorithm () {
  130. window.program_obj.functions = JSON.parse(algorithm_in_ilm).functions;
  131. window.program_obj.globals = JSON.parse(algorithm_in_ilm).globals;
  132. window.watchW.watch(window.program_obj.globals, function(){
  133. if (window.insertContext) {
  134. setTimeout(function(){ renderAlgorithm(); }, 300);
  135. window.insertContext = false;
  136. } else {
  137. renderAlgorithm();
  138. }
  139. }, 1);
  140. for (var i = 0; i < window.program_obj.functions.length; i ++) {
  141. window.watchW.watch(window.program_obj.functions[i].parameters_list, function(){
  142. if (window.insertContext) {
  143. setTimeout(function(){ renderAlgorithm(); }, 300);
  144. window.insertContext = false;
  145. } else {
  146. renderAlgorithm();
  147. }
  148. }, 1);
  149. window.watchW.watch(window.program_obj.functions[i].variables_list, function(){
  150. if (window.insertContext) {
  151. setTimeout(function(){ renderAlgorithm(); }, 300);
  152. window.insertContext = false;
  153. } else {
  154. renderAlgorithm();
  155. }
  156. }, 1);
  157. if (window.program_obj.functions[i].is_main) {
  158. window.program_obj.functions[i].name = LocalizedStrings.getUI("start");
  159. }
  160. }
  161. window.watchW.watch(window.program_obj.functions, function(){
  162. if (window.insertContext) {
  163. setTimeout(function(){ renderAlgorithm(); }, 300);
  164. window.insertContext = false;
  165. } else {
  166. renderAlgorithm();
  167. }
  168. }, 1);
  169. }
  170. function prepareActivityToStudent (ilm_cont) {
  171. var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
  172. testCases = content.testcases;
  173. settingsDataTypes = content.settings_data_types;
  174. settingsCommands = content.settings_commands;
  175. settingsFunctions = content.settings_functions;
  176. if (ilm_cont.split('\n::algorithm::')[1]) {
  177. algorithm_in_ilm = ilm_cont.split('\n::algorithm::')[1].split('\n::logs::')[0];
  178. includePreviousAlgorithm();
  179. }
  180. $('.assessment_button').removeClass('disabled');
  181. renderAlgorithm();
  182. }
  183. // Função para organizar se para criação, visualização ou resolução de atividade
  184. function prepareEnvironment () {
  185. if ((iLMparameters.iLM_PARAM_AssignmentURL == "true") && (iLMparameters.iLM_PARAM_SendAnswer == "true")) {
  186. prepareActivityCreation();
  187. }
  188. }
  189. $(document).ready(function() {
  190. // Se iLM_PARAM_SendAnswer for false, então trata-se de resolução de atividade,
  191. // portanto, a "DIV" de resolução é liberada
  192. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  193. //$('.resolucao').css("display","block");
  194. getiLMContent();
  195. $( document ).ready(function() {
  196. $('.div_to_body').mousemove(function(e) {
  197. trackingMatrix.push(adCoords(e, 0));
  198. });
  199. $('.div_to_body').click(function(e) {
  200. trackingMatrix.push(adCoords(e, 1));
  201. });
  202. });
  203. } else {
  204. // Caso não esteja em modo de resolução de atividade, a visualização no momento
  205. // é para a elaboração de atividade:
  206. //$('.elaboracao').css("display","block");
  207. // Se possuir o parâmetro iLMparameters.iLM_PARAM_Assignment, o professor
  208. // está editando uma atividade:
  209. if (iLMparameters.iLM_PARAM_Assignment) {
  210. getiLMContent();
  211. }
  212. }
  213. if (!testCases) {
  214. $('.assessment_button').addClass('disabled');
  215. }
  216. });
  217. // Função para preparar a interface para o professor criar atividade:
  218. function prepareActivityCreation () {
  219. $('.add_accordion').addClass('accordion');
  220. $('.default_visual_title').toggle();
  221. $('.default_visual_title').append('<span>'+LocalizedStrings.getUI('text_teacher_algorithm')+'</span>');
  222. $('.height_100').removeClass('height_100');
  223. $('.main_title').remove();
  224. $('.ui.accordion').addClass('styled');
  225. $('<div class="content_margin"></div>').insertBefore($('.add_accordion').find('.content').find('.div_to_body'));
  226. $('<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>').insertAfter('.content_margin');
  227. 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>');
  228. cases_test_div.insertBefore('.accordion');
  229. 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>');
  230. config_div.insertAfter(cases_test_div);
  231. $('.ui.accordion').accordion();
  232. $('.ui.checkbox').checkbox();
  233. prepareTableSettings(config_div.find('.content'));
  234. prepareTableTestCases(cases_test_div.find('.content'));
  235. }
  236. function prepareTableTestCases (div_el) {
  237. 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>'
  238. + '<tbody class="content_cases"></tbody></table></form>';
  239. div_el.append(table_el);
  240. 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>');
  241. $('.button_add_case').on('click', function(e) {
  242. addTestCase();
  243. });
  244. }
  245. var hist = false;
  246. function addTestCase (test_case = null) {
  247. var new_row = null;
  248. if (test_case) {
  249. var text_row = '';
  250. text_row += '<tr><td class="counter"></td><td class="expandingArea"><textarea rows="'+test_case.input.length+'" name="input" class="text_area_input">';
  251. for (var i = 0; i < test_case.input.length; i ++) {
  252. text_row += test_case.input[i];
  253. if ((i + 1) < test_case.input.length) {
  254. text_row += '\n';
  255. }
  256. }
  257. text_row += '</textarea></td><td class="expandingArea"><textarea rows="'+test_case.output.length+'" name="output" class="text_area_output">';
  258. for (var i = 0; i < test_case.output.length; i ++) {
  259. text_row += test_case.output[i];
  260. if ((i + 1) < test_case.output.length) {
  261. text_row += '\n';
  262. }
  263. }
  264. text_row += '</textarea></td><td class="btn_actions"><div class="ui button_remove_case"><i class="red icon times large"></i></div></td></tr>';
  265. new_row = $(text_row);
  266. } else {
  267. 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>');
  268. }
  269. $('.content_cases').append(new_row);
  270. new_row.find('.button_remove_case').click(function(e) {
  271. new_row.remove();
  272. updateTestCaseCounter();
  273. });
  274. new_row.find('textarea').on('input', function(e) {
  275. var lines = $(this).val().split('\n').length;
  276. $(this).attr('rows', lines);
  277. });
  278. updateTestCaseCounter();
  279. $('.text_area_output').keydown(function(e) {
  280. var code = e.keyCode || e.which;
  281. if (code == 9 && $(this).closest("tr").is(":last-child")) {
  282. hist = true;
  283. addTestCase();
  284. }
  285. });
  286. if (test_case == null) {
  287. if (!hist) {
  288. $( ".content_cases tr:last" ).find('.text_area_input').focus();
  289. } else {
  290. hist = false;
  291. }
  292. }
  293. }
  294. function updateTestCaseCounter () {
  295. var i = 1;
  296. $( ".content_cases" ).find('tr').each(function() {
  297. $( this ).find('.counter').text(i);
  298. i ++;
  299. });
  300. }
  301. function prepareTableSettings (div_el) {
  302. div_el.append('<h4 class="ui header">'+LocalizedStrings.getUI('text_teacher_data_types')+'</h4>');
  303. div_el.append('<form name="settings_data_types"><div class="ui stackable five column grid">'
  304. +'<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>'
  305. +'<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>'
  306. +'<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>'
  307. +'<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>'
  308. +'<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>'
  309. +'</div></form>');
  310. div_el.append('<h4 class="ui header">'+LocalizedStrings.getUI('text_teacher_commands')+'</h4>');
  311. div_el.append('<form name="settings_commands"><div class="ui stackable three column grid">'
  312. +'<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>'
  313. +'<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>'
  314. +'<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>'
  315. +'<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>'
  316. +'<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>'
  317. +'<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>'
  318. +'<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>'
  319. +'<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>'
  320. +'<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>'
  321. +'<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>'
  322. +'</div></form>');
  323. div_el.append('<h4 class="ui header">'+LocalizedStrings.getUI('text_teacher_functions')+'</h4>');
  324. div_el.append('<form name="settings_functions"><div class="ui stackable one column grid">'
  325. +'<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>'
  326. +'<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>'
  327. +'</div></form>');
  328. $('.ui.checkbox').checkbox();
  329. }
  330. function getTrackingLogs () {
  331. var ret = "";
  332. for (var i = 0; i < trackingMatrix.length; i++) {
  333. ret += "\n" + trackingMatrix[i][0] + "," + trackingMatrix[i][1] + "," + trackingMatrix[i][2];
  334. if (trackingMatrix[i][2] === 1) {
  335. ret += ',"' + trackingMatrix[i][3] + '"';
  336. }
  337. }
  338. return ret;
  339. }
  340. // Tracking mouse movements
  341. var trackingMatrix = [];
  342. function adCoords(e, code){
  343. var x = e.pageX;
  344. var y = e.pageY;
  345. if (code === 1) {
  346. return [x, y, code, e.target.classList['value']];
  347. } else {
  348. return [x, y, code];
  349. }
  350. }
  351. $( document ).ready(function() {
  352. if (inIframe()) {
  353. orderIcons();
  354. orderWidth();
  355. }
  356. });
  357. function orderWidth() {
  358. $('.ui.raised.container.segment.div_to_body').css('width', '100%');
  359. $('.ui.one.column.container.segment.ivprog_visual_panel').css('width', '100%');
  360. }
  361. function orderIcons() {
  362. $('.ui.one.column.doubling.stackable.grid.container').css('display', 'none');
  363. $('.only_in_frame').css('display', 'block');
  364. }
  365. function inIframe () {
  366. try {
  367. return window.self !== window.top;
  368. } catch (e) {
  369. return true;
  370. }
  371. }
  372. function full_screen() {
  373. // check if user allows full screen of elements. This can be enabled or disabled in browser config. By default its enabled.
  374. //its also used to check if browser supports full screen api.
  375. if("fullscreenEnabled" in document || "webkitFullscreenEnabled" in document || "mozFullScreenEnabled" in document || "msFullscreenEnabled" in document) {
  376. if(document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled) {
  377. var element = document.getElementById("ui_main_div");
  378. //requestFullscreen is used to display an element in full screen mode.
  379. if("requestFullscreen" in element) {
  380. element.requestFullscreen();
  381. }
  382. else if ("webkitRequestFullscreen" in element) {
  383. element.webkitRequestFullscreen();
  384. }
  385. else if ("mozRequestFullScreen" in element) {
  386. element.mozRequestFullScreen();
  387. }
  388. else if ("msRequestFullscreen" in element) {
  389. element.msRequestFullscreen();
  390. }
  391. }
  392. } else {
  393. $('.expand_button').addClass('disabled');
  394. }
  395. }