iassign-integration-functions.js 33 KB

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