iassign-integration-functions.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. iLM_PARAM_TeacherAutoEval: getParameterByName("iLM_PARAM_TeacherAutoEval"),
  15. lang: getParameterByName("lang", "pt")
  16. };
  17. // Set the lang parameter to the localStorage for easy access
  18. // and no dependency to the global scope, avoind future 'strict mode' problems
  19. //localStorage.setItem('ivprog.lang', iLMparameters.lang);
  20. function removeCollapseValue (command) {
  21. if (command.collapsed) {
  22. delete command.collapsed;
  23. }
  24. if (command.type == 'iftrue') {
  25. if (command.commands_block)
  26. for (var i = 0; i < command.commands_block.length; i++) {
  27. removeCollapseValue(command.commands_block[i]);
  28. }
  29. if (command.commands_else)
  30. for (var i = 0; i < command.commands_else.length; i++) {
  31. removeCollapseValue(command.commands_else[i]);
  32. }
  33. } else if (command.type == 'repeatNtimes'
  34. || command.type == 'whiletrue'
  35. || command.type == 'dowhiletrue' ) {
  36. if (command.commands_block)
  37. for (var i = 0; i < command.commands_block.length; i++) {
  38. removeCollapseValue(command.commands_block[i]);
  39. }
  40. }
  41. }
  42. // Função chamada pelo iTarefa quando o professor finaliza a criação da atividade
  43. // ou quando o aluno finaliza a resolução do exercício
  44. // O retorno é um JSON com os dados do exercício ou da resolução
  45. // Esse retorno será armazenado no banco de dados do Moodle, pelo iTarefa
  46. function getAnswer () {
  47. // Remover o colapsar usado localmente:
  48. if (window.program_obj) {
  49. for (var i = 0; i < window.program_obj.functions.length; i++) {
  50. for (var j = 0; j < window.program_obj.functions[i].commands.length; j++) {
  51. removeCollapseValue(window.program_obj.functions[i].commands[j]);
  52. }
  53. }
  54. }
  55. // Se o parâmetro "iLM_PARAM_SendAnswer" for false,
  56. // então trata-se de resolução de atividade
  57. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  58. // Montar o retorno com a resposta do aluno
  59. var contentToSend = previousContent.split("\n::algorithm::")[0];
  60. contentToSend += '\n::algorithm::';
  61. if (settingsProgrammingTypes == "textual") {
  62. contentToSend += ivprogCore.CodeEditor.getCode();
  63. } else {
  64. contentToSend += JSON.stringify(window.program_obj, function(key, value) {
  65. if (key == 'dom_object') {
  66. return;
  67. }
  68. return value;
  69. });
  70. }
  71. contentToSend += '\n::logs::';
  72. contentToSend += getTrackingLogs();
  73. return contentToSend;
  74. } else {
  75. // Montar o retorno com a criação da atividade do professor
  76. var ret = ' { ' + prepareTestCases()
  77. + ',\n"settings_programming_type": \n' + JSON.stringify($('form[name="settings_programming_type"]').serializeArray())
  78. + ',\n"settings_data_types": \n' + JSON.stringify($('form[name="settings_data_types"]').serializeArray())
  79. + ',\n"settings_commands": \n' + JSON.stringify($('form[name="settings_commands"]').serializeArray())
  80. + ',\n"settings_functions": \n' + JSON.stringify($('form[name="settings_functions"]').serializeArray())
  81. + ',\n"settings_filter": \n' + JSON.stringify($('form[name="settings_filter"]').serializeArray())
  82. + ' } ';
  83. if ($("input[name='include_algo']").is(':checked')) {
  84. ret += '\n::algorithm::';
  85. ret += JSON.stringify(window.program_obj, function(key, value) {
  86. if (key == 'dom_object') {
  87. return;
  88. }
  89. return value;
  90. });
  91. }
  92. return ret;
  93. }
  94. }
  95. function prepareTestCases () {
  96. var ret = ' \n "testcases" : [ '
  97. var test_cases_array = $('form[name="test_cases"]').serializeArray();
  98. console.log(test_cases_array);
  99. for (var i = 0; i < test_cases_array.length; i = i + 2) {
  100. ret += '\n{ ';
  101. ret += '\n "input": [';
  102. var inps = test_cases_array[i].value.match(/[^\r\n]+/g);
  103. if (inps) {
  104. for (var j = 0; j < inps.length; j++) {
  105. ret += '"' + inps[j] + '"';
  106. if ((j + 1) < inps.length) {
  107. ret += ', ';
  108. }
  109. }
  110. }
  111. ret += '], \n "output": [';
  112. var outs = test_cases_array[i+1].value.match(/[^\r\n]+/g);
  113. console.log(outs);
  114. if (outs) {
  115. for (var j = 0; j < outs.length; j++) {
  116. console.log("output -> ",outs[j]);
  117. ret += '"' + outs[j] + '"';
  118. if ((j + 1) < outs.length) {
  119. ret += ', ';
  120. }
  121. }
  122. }
  123. ret += ']';
  124. ret += '\n}'
  125. if ((i + 2) < test_cases_array.length) {
  126. ret += ',';
  127. }
  128. }
  129. ret += '\n] ';
  130. return ret;
  131. }
  132. // Função chamada pelo iTarefa para receber a nota do aluno na atividade
  133. // O retorno é um valor entre 0.0 e 1.0
  134. function getEvaluation () {
  135. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  136. // A chamada do método abaixo é obrigatória!
  137. // Observe que a chamada parte do iLM para o iTarefa
  138. //parent.getEvaluationCallback(window.studentGrade);
  139. var canRunAssessment = runCodeAssessment();
  140. if(canRunAssessment === -1) {
  141. parent.getEvaluationCallback(-1);
  142. }
  143. }
  144. }
  145. //var testCases = null
  146. var settingsDataTypes = null;
  147. var settingsCommands = null;
  148. var settingsFunctions = null;
  149. var settingsProgrammingTypes = null;
  150. var settingsFilter = null;
  151. var algorithm_in_ilm = null;
  152. var previousContent = null;
  153. // Função para que o iMA leia os dados da atividade fornecidos pelo iTarefa
  154. function getiLMContent () {
  155. // O parâmetro "iLM_PARAM_Assignment" fornece o URL do endereço que deve ser
  156. // requisitado via AJAX para a captura dos dados da atividade
  157. $.get(iLMparameters.iLM_PARAM_Assignment, function (data) {
  158. //professor invocou a avaliação automática dos exercícios do bloco
  159. if (iLMparameters.iLM_PARAM_TeacherAutoEval != null) {
  160. teacherAutoEval(data);
  161. //não deve exibir nenhuma interface...
  162. return;
  163. } else if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  164. // Aluno está trabalhando em alguma atividade:
  165. previousContent = data;
  166. prepareActivityToStudent(data);
  167. } else { // Professor está editando uma atividade:
  168. previousContent = data;
  169. prepareActivityToEdit(data);
  170. }
  171. window.block_render = false;
  172. renderAlgorithm();
  173. });
  174. }
  175. function prepareActivityToEdit (ilm_cont) {
  176. //var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
  177. // Ver arquivo js/util/iassignHelpers.js
  178. var content = ivprogCore.prepareActivityToStudentHelper(ilm_cont).getOrElse(null);
  179. if(!content) {
  180. showInvalidData();
  181. return;
  182. }
  183. var testCases = ivprogCore.getTestCases();
  184. settingsProgrammingTypes = content.settingsProgrammingType;
  185. settingsDataTypes = content.settingsDataTypes;
  186. settingsCommands = content.settingsCommands;
  187. settingsFunctions = content.settingsFunctions;
  188. settingsFilter = content.settingsFilter;
  189. for (var i = 0; i < testCases.length; i++) {
  190. addTestCase(testCases[i]);
  191. }
  192. if (content.algorithmInIlm != null) {
  193. algorithm_in_ilm = content.algorithmInIlm;
  194. $("input[name='include_algo']").prop('checked', true);
  195. includePreviousAlgorithm();
  196. renderAlgorithm();
  197. }
  198. ivprogTextualOrVisual();
  199. if (settingsFilter && settingsFilter[0]) {
  200. blockAllEditingOptions();
  201. }
  202. }
  203. function parsePreviousAlgorithm () {
  204. window.program_obj.functions = JSON.parse(algorithm_in_ilm).functions;
  205. window.program_obj.globals = JSON.parse(algorithm_in_ilm).globals;
  206. }
  207. function includePreviousAlgorithm () {
  208. if (settingsProgrammingTypes == "textual") {
  209. return;
  210. }
  211. parsePreviousAlgorithm();
  212. window.watchW.watch(window.program_obj.globals, function(){
  213. if (window.insertContext) {
  214. setTimeout(function(){ renderAlgorithm(); }, 300);
  215. window.insertContext = false;
  216. } else {
  217. renderAlgorithm();
  218. }
  219. }, 1);
  220. for (var i = 0; i < window.program_obj.functions.length; i ++) {
  221. window.watchW.watch(window.program_obj.functions[i].parameters_list, function(){
  222. if (window.insertContext) {
  223. setTimeout(function(){ renderAlgorithm(); }, 300);
  224. window.insertContext = false;
  225. } else {
  226. renderAlgorithm();
  227. }
  228. }, 1);
  229. window.watchW.watch(window.program_obj.functions[i].variables_list, function(){
  230. if (window.insertContext) {
  231. setTimeout(function(){ renderAlgorithm(); }, 300);
  232. window.insertContext = false;
  233. } else {
  234. renderAlgorithm();
  235. }
  236. }, 1);
  237. if (window.program_obj.functions[i].is_main) {
  238. window.program_obj.functions[i].name = LocalizedStrings.getUI("start");
  239. }
  240. }
  241. window.watchW.watch(window.program_obj.functions, function(){
  242. if (window.insertContext) {
  243. setTimeout(function(){ renderAlgorithm(); }, 300);
  244. window.insertContext = false;
  245. } else {
  246. renderAlgorithm();
  247. }
  248. }, 1);
  249. }
  250. function prepareActivityToStudent (ilm_cont) {
  251. // Ver arquivo js/util/iassignHelpers.js
  252. var content = ivprogCore.prepareActivityToStudentHelper(ilm_cont).getOrElse(null);
  253. if(!content) {
  254. showInvalidData();
  255. return;
  256. }
  257. // Casos de testes agora são delegados ao tratamento apropriado pela função acima
  258. // var testCases = content.testcases;
  259. settingsProgrammingTypes = content.settingsProgrammingType;
  260. settingsDataTypes = content.settingsDataTypes;
  261. settingsCommands = content.settingsCommands;
  262. settingsFunctions = content.settingsFunctions;
  263. settingsFilter = content.settingsFilter;
  264. if (content.algorithmInIlm != null) {
  265. algorithm_in_ilm = content.algorithmInIlm;
  266. includePreviousAlgorithm();
  267. }
  268. $('.assessment_button').removeClass('disabled');
  269. renderAlgorithm();
  270. ivprogTextualOrVisual();
  271. if (settingsFilter && settingsFilter[0]) {
  272. blockAllEditingOptions();
  273. }
  274. }
  275. // Função para organizar se para criação, visualização ou resolução de atividade
  276. function prepareEnvironment () {
  277. $('.div_to_body').click(function(e) {
  278. // trackingMatrix.push(adCoords(e, 1));
  279. ivprogCore.registerClick(e.pageX, e.pageY, e.target.classList['value']);
  280. });
  281. // Se iLM_PARAM_SendAnswer for false, então trata-se de resolução de atividade,
  282. // portanto, a "DIV" de resolução é liberada
  283. if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
  284. //$('.resolucao').css("display","block");
  285. getiLMContent();
  286. // $('.div_to_body').mousemove(function(e) {
  287. // trackingMatrix.push(adCoords(e, 0));
  288. // });
  289. // $('.div_to_body').click(function(e) {
  290. // // trackingMatrix.push(adCoords(e, 1));
  291. // ivprogCore.registerClick(e.pageX, e.pageY, e.target.classList['value']);
  292. // });
  293. } else if (iLMparameters.iLM_PARAM_Assignment) {
  294. // Caso não esteja em modo de resolução de atividade, a visualização no momento
  295. // é para a elaboração de atividade:
  296. //$('.elaboracao').css("display","block");
  297. // Se possuir o parâmetro iLMparameters.iLM_PARAM_Assignment, o professor
  298. // está editando uma atividade:
  299. getiLMContent();
  300. } else {
  301. renderAlgorithm();
  302. }
  303. if ((iLMparameters.iLM_PARAM_AssignmentURL == "true") && (iLMparameters.iLM_PARAM_SendAnswer == "true")) {
  304. prepareActivityCreation();
  305. }
  306. }
  307. function blockAllEditingOptions () {
  308. if ((iLMparameters.iLM_PARAM_AssignmentURL == "true") && (iLMparameters.iLM_PARAM_SendAnswer == "true")) {
  309. return;
  310. }
  311. $('.add_global_button').addClass('disabled');
  312. $('.move_function').addClass('disabled');
  313. $('.add_function_button').addClass('disabled');
  314. $('.add_var_button_function .ui.icon.button.purple').addClass('disabled');
  315. $('.add_var_button_function').addClass('disabled');
  316. $('.menu_commands').addClass('disabled');
  317. $('.global_type').addClass('disabled');
  318. $('.editing_name_var').addClass('disabled');
  319. $('.span_value_variable').addClass('disabled');
  320. $('.remove_global').addClass('disabled');
  321. $('.ui.icon.ellipsis.vertical.inverted').addClass('disabled');
  322. $('.alternate_constant').addClass('disabled');
  323. $('.remove_variable').addClass('disabled');
  324. $('.add_global_matrix_column').addClass('disabled');
  325. $('.remove_global_matrix_column').addClass('disabled');
  326. $('.add_global_matrix_line').addClass('disabled');
  327. $('.remove_global_matrix_line').addClass('disabled');
  328. $('.add_global_vector_column').addClass('disabled');
  329. $('.remove_global_vector_column').addClass('disabled');
  330. $('.add_expression').addClass('disabled');
  331. $('.add_parentheses').addClass('disabled');
  332. $('.remove_function_button').addClass('disabled');
  333. $('.button_remove_command').addClass('disabled');
  334. $('.command_drag').addClass('disabled');
  335. $('.simple_add').addClass('disabled');
  336. $('.add_parameter_button').addClass('disabled');
  337. $('.parameter_div_edit').addClass('disabled');
  338. $('.function_name_div_updated').addClass('disabled');
  339. $('.value_rendered').addClass('disabled');
  340. $('.var_name').addClass('disabled');
  341. $('.variable_rendered').addClass('disabled');
  342. $('.dropdown').addClass('disabled');
  343. $('.remove_parameter').addClass('disabled');
  344. $('.ui.dropdown.global_type.disabled').css('opacity', '1');
  345. $('.ui.dropdown.variable_type.disabled').css('opacity', '1');
  346. $('.ui.dropdown.function_return.disabled').css('opacity', '1');
  347. $('.ui.dropdown.parameter_type.disabled').css('opacity', '1');
  348. ivprogCore.CodeEditor.disable(true);
  349. }
  350. function ivprogTextualOrVisual () {
  351. if (settingsProgrammingTypes) {
  352. if (settingsProgrammingTypes == "textual") {
  353. $('.ivprog_visual_panel').css('display', 'none');
  354. $('.ivprog_textual_panel').css('display', 'block');
  355. $('.ivprog_textual_panel').removeClass('loading');
  356. $('.visual_coding_button').removeClass('active');
  357. $('.textual_coding_button').addClass('active');
  358. $('.visual_coding_button').addClass('disabled');
  359. let textual_code = algorithm_in_ilm;
  360. if(!textual_code) {
  361. textual_code = ivprogCore.LocalizedStrings.getUI("initial_program_code");
  362. textual_code = textual_code.replace(/\\n/g,"\n");
  363. textual_code = textual_code.replace(/\\t/g,"\t");
  364. }
  365. ivprogCore.CodeEditor.setCode(textual_code);
  366. ivprogCore.CodeEditor.disable(false);
  367. }
  368. if (settingsProgrammingTypes == "visual") {
  369. }
  370. }
  371. }
  372. function iassingIntegration () {
  373. // Disable by default...
  374. $('.assessment_button').addClass('disabled');
  375. prepareEnvironment();
  376. if (inIframe()) {
  377. orderIcons();
  378. orderWidth();
  379. }
  380. }
  381. // Função para preparar a interface para o professor criar atividade:
  382. function prepareActivityCreation () {
  383. var menuTab = $('<div class="ui top attached tabular menu">'
  384. + '<a class="item active" data-tab="testcases">' + LocalizedStrings.getUI('text_teacher_test_case') + '</a>'
  385. + '<a class="item" data-tab="algorithm">' + LocalizedStrings.getUI('text_teacher_algorithm') + '</a>'
  386. + '<a class="item" data-tab="settings">' + LocalizedStrings.getUI('text_teacher_config') + '</a>'
  387. + '</div>'
  388. + '<div class="ui bottom attached tab segment active tab_test_cases" data-tab="testcases"></div>'
  389. + '<div class="ui bottom attached tab segment tab_algorithm" data-tab="algorithm"></div>'
  390. + '<div class="ui bottom attached tab segment tab_settings" data-tab="settings"></div>');
  391. menuTab.insertBefore('.add_accordion');
  392. $('.tabular.menu .item').tab();
  393. $('.main_title').remove();
  394. $('.ui.accordion').addClass('styled');
  395. $('<div class="content_margin"></div>').insertBefore($('.add_accordion').find('.content').find('.div_to_body'));
  396. $('<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');
  397. var cases_test_div = $('<div></div>');
  398. $('.tab_test_cases').append(cases_test_div);
  399. var config_div = $('<div></div>');
  400. $('.tab_settings').append(config_div);
  401. $('.ui.checkbox').checkbox();
  402. $('.tab_algorithm').append($('.add_accordion'));
  403. prepareTableSettings(config_div);
  404. prepareTableTestCases(cases_test_div);
  405. if (inIframe()) {
  406. $('.ui.styled.accordion').css('width', '96%');
  407. }
  408. }
  409. function prepareTableTestCases (div_el) {
  410. 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>'
  411. + '<tbody class="content_cases"></tbody></table></form>';
  412. div_el.append(table_el);
  413. var table_buttons = '<table class="table_buttons"><tr><td>'
  414. + '<button class="ui teal labeled icon button button_add_case"><i class="plus icon"></i>'+LocalizedStrings.getUI('text_teacher_test_case_add')+'</button>'
  415. + '</td><td class="right_align">'
  416. + '<button class="ui orange labeled icon button button_generate_outputs"><i class="sign-in icon"></i>'+LocalizedStrings.getUI('text_teacher_generate_outputs')+'</button>'
  417. + '</td></tr></table>';
  418. div_el.append(table_buttons);
  419. div_el.append($('<div class="ui basic modal"><div class="content"><p>Olá</p></div><div class="actions"><div class="ui green ok inverted button">Fechar</div></div></div>'));
  420. $('.button_add_case').on('click', function(e) {
  421. addTestCase();
  422. });
  423. $('.button_generate_outputs').on('click', function(e) {
  424. generateOutputs();
  425. });
  426. }
  427. function showAlert (msg) {
  428. $('.ui.basic.modal .content').html('<h3>'+msg+'</h3>');
  429. $('.ui.basic.modal').modal('show');
  430. }
  431. function generateOutputs () {
  432. if (window.program_obj.functions.length == 1 && window.program_obj.functions[0].commands.length == 0) {
  433. showAlert(LocalizedStrings.getUI('text_teacher_generate_outputs_algorithm'));
  434. return;
  435. }
  436. // código:
  437. var code_teacher = window.generator();
  438. // array com as entradas já inseridas:
  439. var test_cases = JSON.parse(prepareTestCases().replace('"testcases" :', ''));
  440. ivprogCore.autoGenerateTestCaseOutput(code_teacher, test_cases).catch(function (error) {
  441. showAlert("Houve um erro durante a execução do seu programa: "+error.message);
  442. });
  443. }
  444. function outputGenerated (test_cases) {
  445. var fields = $('.text_area_output');
  446. /*for (var i = 0; i < test_cases.length; i++) {
  447. $(fields[i]).val('');
  448. for (var j = 0; j < test_cases[i].output.length; j++) {
  449. $(fields[i]).val($(fields[i]).val() + test_cases[i].output[j]);
  450. if (j < test_cases[i].output.length - 1) {
  451. $(fields[i]).val($(fields[i]).val() + '\n');
  452. }
  453. }
  454. $(fields[i]).attr('rows', test_cases[i].output.length);
  455. }*/
  456. animateOutput(fields, test_cases, 0);
  457. }
  458. function animateOutput (list, test_cases, index) {
  459. if (list.length == index) return;
  460. $(list[index]).val('');
  461. for (var j = 0; j < test_cases[index].output.length; j++) {
  462. console.log(test_cases[index].output[j].charCodeAt(0));
  463. $(list[index]).val($(list[index]).val() + test_cases[index].output[j]);
  464. if (j < test_cases[index].output.length - 1) {
  465. $(list[index]).val($(list[index]).val() + '\n');
  466. }
  467. }
  468. $(list[index]).attr('rows', test_cases[index].output.length);
  469. $(list[index]).effect('highlight', null, 50, function() {
  470. animateOutput(list, test_cases, index + 1);
  471. });
  472. }
  473. var hist = false;
  474. function addTestCase (test_case = null) {
  475. var new_row = null;
  476. if (test_case) {
  477. var text_row = '';
  478. text_row += '<tr><td class="counter"></td><td class="expandingArea"><textarea rows="'+test_case.input.length+'" name="input" class="text_area_input">';
  479. for (var i = 0; i < test_case.input.length; i ++) {
  480. text_row += test_case.input[i];
  481. if ((i + 1) < test_case.input.length) {
  482. text_row += '\n';
  483. }
  484. }
  485. text_row += '</textarea></td><td class="expandingArea"><textarea rows="'+test_case.output.length+'" name="output" class="text_area_output">';
  486. for (var i = 0; i < test_case.output.length; i ++) {
  487. text_row += test_case.output[i];
  488. if ((i + 1) < test_case.output.length) {
  489. text_row += '\n';
  490. }
  491. }
  492. text_row += '</textarea></td><td class="btn_actions"><div class="ui button_remove_case"><i class="red icon times large"></i></div></td></tr>';
  493. new_row = $(text_row);
  494. } else {
  495. 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>');
  496. }
  497. $('.content_cases').append(new_row);
  498. new_row.find('.button_remove_case').click(function(e) {
  499. new_row.remove();
  500. updateTestCaseCounter();
  501. });
  502. new_row.find('textarea').on('input', function(e) {
  503. var lines = $(this).val().split('\n').length;
  504. $(this).attr('rows', lines);
  505. });
  506. updateTestCaseCounter();
  507. $('.text_area_output').keydown(function(e) {
  508. var code = e.keyCode || e.which;
  509. if (code == 9 && $(this).closest("tr").is(":last-child")) {
  510. hist = true;
  511. addTestCase();
  512. }
  513. });
  514. if (test_case == null) {
  515. if (!hist) {
  516. $( ".content_cases tr:last" ).find('.text_area_input').focus();
  517. } else {
  518. hist = false;
  519. }
  520. }
  521. }
  522. function updateTestCaseCounter () {
  523. var i = 1;
  524. $( ".content_cases" ).find('tr').each(function() {
  525. $( this ).find('.counter').text(i);
  526. ++i;
  527. });
  528. }
  529. function prepareTableSettings (div_el) {
  530. div_el.append('<div class="ui segment settings_topic"><h3 class="ui header"><i class="window maximize outline icon"></i><div class="content">'+LocalizedStrings.getUI('text_config_programming')+'</div></h3>'
  531. +'<div class="content content_segment_settings"><form name="settings_programming_type"><div class="ui stackable five column grid">'
  532. +'<div class="column"><div class="ui radio"><input type="radio" name="programming_type" id="programming_textual" value="textual" tabindex="0" class="hidden small"><label for="programming_textual">'+LocalizedStrings.getUI('text_config_programming_textual')+'</label></div></div>'
  533. +'<div class="column"><div class="ui radio"><input type="radio" name="programming_type" id="programming_visual" value="visual" checked tabindex="0" class="hidden small"><label for="programming_visual">'+LocalizedStrings.getUI('text_config_programming_visual')+'</label></div></div>'
  534. +'</div></form></div></div>');
  535. div_el.append('<div class="ui segment settings_topic"><h3 class="ui header"><i class="qrcode icon"></i><div class="content">'+LocalizedStrings.getUI('text_teacher_data_types')+'</div></h3>'
  536. +'<div class="content content_segment_settings"><form name="settings_data_types"><div class="ui stackable five column grid">'
  537. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="integer_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('type_integer')+'</label></div></div>'
  538. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="real_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('type_real')+'</label></div></div>'
  539. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="text_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('type_text')+'</label></div></div>'
  540. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="boolean_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('type_boolean')+'</label></div></div>'
  541. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="void_data_type" checked tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('type_void')+'</label></div></div>'
  542. +'</div></form></div></div>');
  543. div_el.append('<div class="ui segment settings_topic"><h3 class="ui header"><i class="code icon"></i><div class="content">'+LocalizedStrings.getUI('text_teacher_commands')+'</div></h3>'
  544. +'<div class="content content_segment_settings"><form name="settings_commands"><div class="ui stackable three column grid">'
  545. +'<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>'
  546. +'<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>'
  547. +'<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>'
  548. +'<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>'
  549. +'<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>'
  550. +'<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>'
  551. +'<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>'
  552. +'<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>'
  553. +'<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>'
  554. +'<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>'
  555. +'</div></form></div></div>');
  556. div_el.append('<div class="ui segment settings_topic"><h3 class="ui header"><i class="terminal icon"></i><div class="content">'+LocalizedStrings.getUI('text_teacher_functions')+'</div></h3>'
  557. +'<div class="content content_segment_settings"><form name="settings_functions"><div class="ui stackable one column grid">'
  558. +'<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>'
  559. +'<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>'
  560. +'</div></form></div></div>');
  561. div_el.append('<div class="ui segment settings_topic"><h3 class="ui header"><i class="filter icon"></i><div class="content">'+LocalizedStrings.getUI('text_teacher_filter')+'</div><i class="circular inverted teal question icon"></i></h3>'
  562. +'<div class="content content_segment_settings"><form name="settings_filter"><div class="ui stackable one column grid">'
  563. +'<div class="column"><div class="ui checkbox"><input type="checkbox" name="filter_active" tabindex="0" class="hidden small"><label>'+LocalizedStrings.getUI('text_teacher_filter_active')+'</label></div></div>'
  564. +'</div></form></div></div>');
  565. $('.circular.inverted.teal.question.icon').popup({
  566. content : LocalizedStrings.getUI("text_teacher_filter_help"),
  567. delay: {
  568. show: 750,
  569. hide: 0
  570. }
  571. });
  572. $('.ui.checkbox').checkbox();
  573. }
  574. function getTrackingLogs () {
  575. return ivprogCore.getLogsAsString();
  576. // var ret = "";
  577. // for (var i = 0; i < trackingMatrix.length; ++i) {
  578. // ret += "\n" + trackingMatrix[i][0] + "," + trackingMatrix[i][1] + "," + trackingMatrix[i][2];
  579. // if (trackingMatrix[i][3] === 1) {
  580. // ret += ',' + trackingMatrix[i][3] + ',"' + trackingMatrix[i][4] + '"';
  581. // }
  582. // }
  583. // return ret;
  584. }
  585. // Tracking mouse movements
  586. // var trackingMatrix = [];
  587. /* function adCoords(e, code){
  588. var x = e.pageX;
  589. var y = e.pageY;
  590. if (code === 1) {
  591. return [new Date().getTime(), x, y, code, e.target.classList['value']];
  592. } else {
  593. return [x, y, code];
  594. }
  595. } */
  596. // $( document ).ready(function() {
  597. // if (inIframe()) {
  598. // orderIcons();
  599. // orderWidth();
  600. // }
  601. // renderAlgorithm();
  602. // });
  603. function orderWidth() {
  604. $('.ui.raised.container.segment.div_to_body').css('width', '100%');
  605. $('.ui.one.column.container.segment.ivprog_visual_panel').css('width', '100%');
  606. }
  607. function orderIcons() {
  608. $('.ui.one.column.doubling.stackable.grid.container').css('display', 'none');
  609. $('.only_in_frame').css('display', 'block');
  610. }
  611. function inIframe () {
  612. try {
  613. return window.self !== window.top;
  614. } catch (e) {
  615. return true;
  616. }
  617. }
  618. function full_screen() {
  619. // check if user allows full screen of elements. This can be enabled or disabled in browser config. By default its enabled.
  620. //its also used to check if browser supports full screen api.
  621. if("fullscreenEnabled" in document || "webkitFullscreenEnabled" in document || "mozFullScreenEnabled" in document || "msFullscreenEnabled" in document) {
  622. if(document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled) {
  623. var element = document.getElementById("ui_main_div");
  624. //requestFullscreen is used to display an element in full screen mode.
  625. if("requestFullscreen" in element) {
  626. element.requestFullscreen();
  627. }
  628. else if ("webkitRequestFullscreen" in element) {
  629. element.webkitRequestFullscreen();
  630. }
  631. else if ("mozRequestFullScreen" in element) {
  632. element.mozRequestFullScreen();
  633. }
  634. else if ("msRequestFullscreen" in element) {
  635. element.msRequestFullscreen();
  636. }
  637. }
  638. } else {
  639. $('.expand_button').addClass('disabled');
  640. }
  641. }
  642. function getAutoEvalOriginalData () {
  643. return parent.getAutoEvalOriginalData();
  644. }
  645. function teacherAutoEval (data) {
  646. previousContent = data;
  647. // Ver arquivo js/util/iassignHelpers.js
  648. var content = ivprogCore.prepareActivityToStudentHelper(data).getOrElse(null);
  649. if(!content) {
  650. showInvalidData();
  651. return;
  652. }
  653. // Casos de testes agora são delegados ao tratamento apropriado pela função acima
  654. // var testCases = content.testcases;
  655. settingsProgrammingTypes = content.settingsProgrammingType;
  656. settingsDataTypes = content.settingsDataTypes;
  657. settingsCommands = content.settingsCommands;
  658. settingsFunctions = content.settingsFunctions;
  659. settingsFilter = content.settingsFilter;
  660. if (content.algorithmInIlm != null) {
  661. algorithm_in_ilm = content.algorithmInIlm;
  662. parsePreviousAlgorithm();
  663. var originalData = getAutoEvalOriginalData();
  664. ivprogCore.autoEval(originalData, parent.postResultAutoEval);
  665. }
  666. ivprogTextualOrVisual();
  667. if (settingsFilter && settingsFilter[0]) {
  668. blockAllEditingOptions();
  669. }
  670. }
  671. function displayGrade(grade) {
  672. alert(grade);
  673. }
  674. function showInvalidData () {
  675. $('.ui.height_100.add_accordion').dimmer({
  676. closable: false
  677. });
  678. $('.dimmer_content_message h3').html(LocalizedStrings.getUI('text_message_error_activity_file'));
  679. $('.dimmer_content_message button').text(LocalizedStrings.getUI('text_message_error_activity_reload'));
  680. $('.dimmer_content_message').css('display', 'block');
  681. $('.ui.height_100.add_accordion').dimmer('add content', '.dimmer_content_message');
  682. $('.ui.height_100.add_accordion').dimmer('show');
  683. $('.dimmer_content_message button').on('click', function(e) {
  684. window.parent.location.reload()
  685. })
  686. }
  687. function showInvalidFile () {
  688. $('.ui.height_100.add_accordion').dimmer({
  689. closable: true
  690. });
  691. $('.dimmer_content_message h3').html(LocalizedStrings.getUI('text_message_error_upload_file'));
  692. $('.dimmer_content_message button').text(LocalizedStrings.getUI('text_message_error_upload_close'));
  693. $('.dimmer_content_message').css('display', 'block');
  694. $('.ui.height_100.add_accordion').dimmer('add content', '.dimmer_content_message');
  695. $('.ui.height_100.add_accordion').dimmer('show');
  696. $('.dimmer_content_message button').on('click', function(e) {
  697. $('.ui.height_100.add_accordion').dimmer('hide');
  698. })
  699. }