iassignHelpers.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { setTestCases, getTestCases } from "../visualUI/functions";
  2. import { generate } from "../visualUI/code_generator";
  3. import { IVProgAssessment } from "../assessment/ivprogAssessment";
  4. import { TestConsole } from "./testConsole";
  5. import { parseLogs } from "./../services/userLog";
  6. import { LocalizedStrings } from './../services/localizedStringsService';
  7. function parseActivityData (data) {
  8. let algorithm_in_ilm = null;
  9. if (data.split('\n::algorithm::')[1]) {
  10. algorithm_in_ilm = data.split('\n::algorithm::')[1].split('\n::logs::')[0];
  11. const logs = data.split('\n::algorithm::')[1].split('\n::logs::')[1];
  12. if (logs != null) {
  13. parseLogs(logs);
  14. }
  15. }
  16. let content;
  17. try {
  18. content = JSON.parse(data.split('\n::algorithm::')[0]);
  19. content['algorithm_in_ilm'] = algorithm_in_ilm;
  20. } catch (e) {
  21. $('.ui.height_100.add_accordion').dimmer({
  22. closable: false
  23. });
  24. $('.dimmer_content_message h3').html(LocalizedStrings.getUI('text_message_error_activity_file'));
  25. $('.dimmer_content_message button').text(LocalizedStrings.getUI('text_message_error_activity_reload'));
  26. $('.dimmer_content_message').css('display', 'block');
  27. $('.ui.height_100.add_accordion').dimmer('add content', '.dimmer_content_message');
  28. $('.ui.height_100.add_accordion').dimmer('show');
  29. console.error(e);
  30. }
  31. return content;
  32. }
  33. export function prepareActivityToStudentHelper (ilm_cont) {
  34. const content = parseActivityData(ilm_cont);
  35. const testCases = content.testcases;
  36. setTestCases(testCases);
  37. let prog_type = null;
  38. if (content.settings_programming_type) {
  39. prog_type = content.settings_programming_type[0].value;
  40. } else {
  41. prog_type = "visual";
  42. }
  43. return {
  44. settingsProgrammingType: prog_type,
  45. settingsDataTypes: content.settings_data_types,
  46. settingsCommands: content.settings_commands,
  47. settingsFunctions: content.settings_functions,
  48. algorithmInIlm: content.algorithm_in_ilm,
  49. settingsFilter: content.settings_filter
  50. }
  51. }
  52. export function autoEval (originalData, callback) {
  53. const code = generate();
  54. const original = parseActivityData(originalData);
  55. if (code == null) {
  56. return callback(-1);
  57. } else {
  58. if (!compareTestcases(original.testcases, getTestCases())) {
  59. return callback(-2);
  60. }
  61. const autoAssessment = new IVProgAssessment(code, getTestCases(), new TestConsole([]));
  62. autoAssessment.runTest().then( grade => callback(grade)).catch(err => {
  63. console.log(err);
  64. callback(0);
  65. });
  66. }
  67. }
  68. function compareTestcases (original, student) {
  69. if (original.length != student.length) {
  70. return false;
  71. }
  72. for (let i = 0; i < original.length; ++i) {
  73. const elementO = original[i];
  74. const elementS = student[i];
  75. if(!compareArray(elementO.input, elementS.input)) {
  76. return false;
  77. }
  78. if(!compareArray(elementO.output, elementS.output)) {
  79. return false;
  80. }
  81. }
  82. }
  83. function compareArray (a, b) {
  84. for (let i = 0; i < a.length; ++i) {
  85. const elementA = a[i];
  86. const elementB = b[i];
  87. if (elementA != elementB) {
  88. return false;
  89. }
  90. }
  91. }