浏览代码

Merge branch 'fixIlmIntegration' of LInE/ivprog into master

Lucas de Souza 5 年之前
父节点
当前提交
db8b64d5ad
共有 5 个文件被更改,包括 180 次插入45 次删除
  1. 71 43
      js/iassign-integration-functions.js
  2. 5 1
      js/main.js
  3. 33 0
      js/util/iassignHelpers.js
  4. 59 0
      js/util/testConsole.js
  5. 12 1
      js/visualUI/functions.js

+ 71 - 43
js/iassign-integration-functions.js

@@ -12,6 +12,7 @@ var iLMparameters = {
     iLM_PARAM_SendAnswer: getParameterByName("iLM_PARAM_SendAnswer"),
     iLM_PARAM_AssignmentURL: getParameterByName("iLM_PARAM_AssignmentURL"),
     iLM_PARAM_Assignment: getParameterByName("iLM_PARAM_Assignment"),
+    iLM_PARAM_TeacherAutoEval: getParameterByName("iLM_PARAM_TeacherAutoEval"),
     lang: getParameterByName("lang", "pt")
 };
 
@@ -114,7 +115,7 @@ function getEvaluation () {
 }
 
 
-var testCases = null;
+//var testCases = null;
 var settingsDataTypes = null;
 var settingsCommands = null;
 var settingsFunctions = null;
@@ -127,8 +128,13 @@ function getiLMContent () {
     // O parâmetro "iLM_PARAM_Assignment" fornece o URL do endereço que deve ser
     // requisitado via AJAX para a captura dos dados da atividade
     $.get(iLMparameters.iLM_PARAM_Assignment, function (data) {
-        // Aluno está trabalhando em alguma atividade:
-        if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
+        //professor invocou a avaliação automática dos exercícios do bloco
+        if (iLMparameters.iLM_PARAM_TeacherAutoEval == 'true') {
+            teacherAutoEval(data);
+            //não deve exibir nenhuma interface...
+            return;
+        } else if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
+            // Aluno está trabalhando em alguma atividade:
             previousContent = data;
             prepareActivityToStudent(data);
         } else { // Professor está editando uma atividade:
@@ -142,27 +148,33 @@ function getiLMContent () {
 }
 
 function prepareActivityToEdit (ilm_cont) {
-    var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
-    testCases = content.testcases;
-    settingsDataTypes = content.settings_data_types;
-    settingsCommands = content.settings_commands;
-    settingsFunctions = content.settings_functions;
+    //var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
+    // Ver arquivo js/util/iassignHelpers.js
+    var content = ivprogCore.prepareActivityToStudentHelper(ilm_cont);
+    var testCases = ivprogCore.getTestCases();
+    settingsDataTypes = content.settingsDataTypes;
+    settingsCommands = content.settingsCommands;
+    settingsFunctions = content.settingsFunctions;
 
     for (var i = 0; i < testCases.length; i++) {
         addTestCase(testCases[i]);
     }
 
-    if (ilm_cont.split('\n::algorithm::')[1]) {
-        algorithm_in_ilm = ilm_cont.split('\n::algorithm::')[1].split('\n::logs::')[0];
+    if (content.algorithmInIlm != null) {
+        algorithm_in_ilm = content.algorithmInIlm;
         $("input[name='include_algo']").prop('checked', true);
         includePreviousAlgorithm();
         renderAlgorithm();
     }
 }
 
-function includePreviousAlgorithm () {
+function parsePreviousAlgorithm () {
     window.program_obj.functions = JSON.parse(algorithm_in_ilm).functions;
     window.program_obj.globals = JSON.parse(algorithm_in_ilm).globals;
+}
+
+function includePreviousAlgorithm () {
+    parsePreviousAlgorithm();
 
     window.watchW.watch(window.program_obj.globals, function(){
       if (window.insertContext) {
@@ -207,14 +219,16 @@ function includePreviousAlgorithm () {
 }
 
 function prepareActivityToStudent (ilm_cont) {
-    var content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
-    testCases = content.testcases;
-    settingsDataTypes = content.settings_data_types;
-    settingsCommands = content.settings_commands;
-    settingsFunctions = content.settings_functions;
-
-    if (ilm_cont.split('\n::algorithm::')[1]) {
-        algorithm_in_ilm = ilm_cont.split('\n::algorithm::')[1].split('\n::logs::')[0];
+    // Ver arquivo js/util/iassignHelpers.js
+    var content = ivprogCore.prepareActivityToStudentHelper(ilm_cont);
+    // Casos de testes agora são delegados ao tratamento apropriado pela função acima
+    // var testCases = content.testcases;
+    settingsDataTypes = content.settingsDataTypes;
+    settingsCommands = content.settingsCommands;
+    settingsFunctions = content.settingsFunctions;
+
+    if (content.algorithmInIlm != null) {
+        algorithm_in_ilm = content.algorithmInIlm;
         includePreviousAlgorithm();
     }
     $('.assessment_button').removeClass('disabled');
@@ -230,38 +244,36 @@ function prepareEnvironment () {
 
 $(document).ready(function() {
 
+    // Disable by default...
+    $('.assessment_button').addClass('disabled');
+
     // Se iLM_PARAM_SendAnswer for false, então trata-se de resolução de atividade,
     // portanto, a "DIV" de resolução é liberada
     if (iLMparameters.iLM_PARAM_SendAnswer == 'false') {
         //$('.resolucao').css("display","block");
         getiLMContent();
 
-
-        $( document ).ready(function() {
-            $('.div_to_body').mousemove(function(e) {
-                trackingMatrix.push(adCoords(e, 0));
-            });
-
-            $('.div_to_body').click(function(e) {
-                trackingMatrix.push(adCoords(e, 1));                    
-            });
-
+        $('.div_to_body').mousemove(function(e) {
+            trackingMatrix.push(adCoords(e, 0));
         });
 
-    } else {
+        $('.div_to_body').click(function(e) {
+            trackingMatrix.push(adCoords(e, 1));                    
+        });
+    } else if (iLMparameters.iLM_PARAM_Assignment) {
         // Caso não esteja em modo de resolução de atividade, a visualização no momento
         // é para a elaboração de atividade:
         //$('.elaboracao').css("display","block");
 
         // Se possuir o parâmetro iLMparameters.iLM_PARAM_Assignment, o professor
         // está editando uma atividade:
-        if (iLMparameters.iLM_PARAM_Assignment) {
-            getiLMContent();
-        }
+        getiLMContent();
+    } else {
+        renderAlgorithm();
     }
-
-    if (!testCases) {
-        $('.assessment_button').addClass('disabled');
+    if (inIframe()) {
+        orderIcons();
+        orderWidth();
     }
 
 });
@@ -445,14 +457,14 @@ function adCoords(e, code){
     }
 }
 
-$( document ).ready(function() {
+// $( document ).ready(function() {
 
-    if (inIframe()) {
-        orderIcons();
-        orderWidth();
-    }
-    renderAlgorithm();
-});
+//     if (inIframe()) {
+//         orderIcons();
+//         orderWidth();
+//     }
+//     renderAlgorithm();
+// });
 
 function orderWidth() {
     $('.ui.raised.container.segment.div_to_body').css('width', '100%');
@@ -497,4 +509,20 @@ function full_screen() {
     } else {
         $('.expand_button').addClass('disabled');
     }
+}
+
+function teacherAutoEval (data) {
+    // Ver arquivo js/util/iassignHelpers.js
+    var content = ivprogCore.prepareActivityToStudentHelper(data);
+    // Casos de testes agora são delegados ao tratamento apropriado pela função acima
+    // var testCases = content.testcases;
+    settingsDataTypes = content.settingsDataTypes;
+    settingsCommands = content.settingsCommands;
+    settingsFunctions = content.settingsFunctions;
+
+    if (content.algorithmInIlm != null) {
+        algorithm_in_ilm = content.algorithmInIlm;
+        parsePreviousAlgorithm();
+        ivprogCore.autoEval(parent.getEvaluationCallback);
+    }
 }

+ 5 - 1
js/main.js

@@ -1,9 +1,10 @@
 import { runner } from './runner';
 import { initVisualUI, addFunctionChangeListener,
   addGlobalChangeListener, removeFunctionListener,
-  removeGlobalListener } from './visualUI/functions';
+  removeGlobalListener, getTestCases } from './visualUI/functions';
 import * as LocalizedStringsService from './services/localizedStringsService';
 import { i18nHelper } from "./services/i18nHelper";
+import { prepareActivityToStudentHelper, autoEval } from "./util/iassignHelpers";
 
 const i18n = i18nHelper.i18n
 const LocalizedStrings = LocalizedStringsService.getInstance();
@@ -15,6 +16,9 @@ export {
   addGlobalChangeListener,
   removeFunctionListener,
   removeGlobalListener,
+  getTestCases,
+  autoEval,
+  prepareActivityToStudentHelper,
   LocalizedStrings,
   i18n
 }

+ 33 - 0
js/util/iassignHelpers.js

@@ -0,0 +1,33 @@
+import { setTestCases, getTestCases } from "../visualUI/functions";
+import { generate } from "../visualUI/code_generator";
+import { IVProgAssessment } from "../assessment/ivprogAssessment";
+import { TestConsole } from "./testConsole";
+
+export function prepareActivityToStudentHelper (ilm_cont) {
+  const content = JSON.parse(ilm_cont.split('\n::algorithm::')[0]);
+  const testCases = content.testcases;
+  setTestCases(testCases);
+  const settingsDataTypes = content.settings_data_types;
+  const settingsCommands = content.settings_commands;
+  const settingsFunctions = content.settings_functions;
+  let algorithm_in_ilm = null;
+  if (ilm_cont.split('\n::algorithm::')[1]) {
+      algorithm_in_ilm = ilm_cont.split('\n::algorithm::')[1].split('\n::logs::')[0];
+  }
+  return {
+    settingsDataTypes: settingsDataTypes,
+    settingsCommands: settingsCommands,
+    settingsFunctions: settingsFunctions,
+    algorithmInIlm: algorithm_in_ilm
+  }
+}
+
+export function autoEval (callback) {
+  const code = generate();
+  if (code == null) {
+    return callback(-1);
+  } else {
+    const autoAssessment = new IVProgAssessment(code, getTestCases(), new TestConsole([]));
+    autoAssessment.runTest().then( grade => callback(grade)).catch(err => console.log(err))
+  }
+}

+ 59 - 0
js/util/testConsole.js

@@ -0,0 +1,59 @@
+import { DOMConsole } from "../io/domConsole";
+import { getInstance } from "../services/localizedStringsService";
+
+const LocalizedStrings = getInstance();
+
+export class TestConsole {
+
+
+  constructor (inputList) {
+    this.index = 0;
+    this.inputList = inputList;
+    this.list = [];
+  }
+
+  write (text) {
+    this._appendText(text, DOMConsole.USER);
+  }
+
+  info (text) {
+    this._appendText(text, DOMConsole.INFO);
+  }
+
+  err (text) {
+    this._appendText(text, DOMConsole.ERR);
+  }
+
+  _appendText (text) {
+    this.list.push(text);
+  }
+
+
+  getClassForType (type) {
+    switch (type) {
+      case DOMConsole.USER:
+        return "ivprog-term-userText";
+      case DOMConsole.INFO:
+        return "ivprog-term-info";
+      case DOMConsole.ERR:
+        return "ivprog-term-error";
+    }
+  }
+
+  requestInput (callback) {
+    if(this.index < this.inputList.length) {      
+      callback(this.inputList[this.index]);
+      this.index++;
+    } else {
+      throw new Error(LocalizedStrings.getError("exceeded_input_request"));
+    }
+  }
+
+  sendOutput (text) {
+    const output = ""+text;
+    output.split("\n").forEach(t => {
+      t = t.replace(/\t/g,'&#9;');
+      this.write(t)
+    });
+  }
+}

+ 12 - 1
js/visualUI/functions.js

@@ -25,6 +25,7 @@ var ivprog_version = VersionInfo.version;
 const globalChangeListeners = [];
 const functionsChangeListeners = [];
 let domConsole = null;
+let _testCases = [];
 window.studentGrade = null;
 window.LocalizedStrings = LocalizedStrings;
 const program = new Models.Program();
@@ -723,6 +724,16 @@ export function initVisualUI () {
   $('.main_title h2').prop('title', LocalizedStrings.getUI('text_ivprog_description'));
 }
 
+export function setTestCases (testCases) {
+  _testCases = testCases;
+}
+
+export function getTestCases () {
+  // Deep clone of test cases to avoid unauthorized modification
+  // TODO: It may be not possible to use this once custom test are fully implemented 
+  return JSON.parse(JSON.stringify(_testCases));
+}
+
 var is_iassign = false;
 
 $( document ).ready(function() {
@@ -858,7 +869,7 @@ function runCodeAssessment () {
   if(domConsole == null)
     domConsole = new DOMConsole("#ivprog-term");
   $("#ivprog-term").slideDown(500);
-  const runner = new IVProgAssessment(strCode, testCases, domConsole);
+  const runner = new IVProgAssessment(strCode, _testCases, domConsole);
 
   runner.runTest().then(grade => {
     if (!is_iassign) {