iassignHelpers.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  50. }
  51. export function autoEval (originalData, callback) {
  52. const code = generate();
  53. const original = parseActivityData(originalData);
  54. if (code == null) {
  55. return callback(-1);
  56. } else {
  57. if (!compareTestcases(original.testcases, getTestCases())) {
  58. return callback(-2);
  59. }
  60. const autoAssessment = new IVProgAssessment(code, getTestCases(), new TestConsole([]));
  61. autoAssessment.runTest().then( grade => callback(grade)).catch(err => {
  62. console.log(err);
  63. callback(0);
  64. });
  65. }
  66. }
  67. function compareTestcases (original, student) {
  68. if (original.length != student.length) {
  69. return false;
  70. }
  71. for (let i = 0; i < original.length; ++i) {
  72. const elementO = original[i];
  73. const elementS = student[i];
  74. if(!compareArray(elementO.input, elementS.input)) {
  75. return false;
  76. }
  77. if(!compareArray(elementO.output, elementS.output)) {
  78. return false;
  79. }
  80. }
  81. }
  82. function compareArray (a, b) {
  83. for (let i = 0; i < a.length; ++i) {
  84. const elementA = a[i];
  85. const elementB = b[i];
  86. if (elementA != elementB) {
  87. return false;
  88. }
  89. }
  90. }