<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>iMA tester</title>
  <link href="./css/bootstrap.min.css" rel="stylesheet">
  <link href="./css/style.css" rel="stylesheet">
</head>

<!--
   -------------------------------------------------------------------------------
  JavaScript code: starting point
  This JavaScript code could be inserted in any LMS to get the iLM content and send it to the server.
  In this example it use a single "applet" instance, here from iHanoi.
 -->
<script Language="JavaScript">
  //<![CDATA[
  var strAnswer = '';
  var evaluationResult = '';
  var comment = '';

  function submit_iMA_Answer () {
    // alert('./ex_pt/index.html: window.frames=' + window.frames);
    // alert('./ex_pt/index.html: window.frames.iLM=' + window.frames.iLM);
    /* DEBUG
    var strFrames = "";
    var arrFrames = parent.document.getElementsByTagName("IFRAME");
    for (var i=0; i<arrFrames.length; i++) {
      strFrames += ', ' + arrFrames[i].name; // if (arrFrames[i].contentWindow === window) alert("yay!");
      } // acho 'window.frames' de nome 'iLM'!!!
    alert('./ex_pt/index.html: window.frames=' + strFrames);
    */
    window.frames.iLM.getEvaluation(); // ./ivprogh/js/iassign-integration-functions.js
    // window.frames.iLM.runCodeAssessment();
    var strAnswer = window.frames.iLM.getAnswer();
    alert('./ex_pt/index.html: getAnswer()=' + strAnswer);

    /*
    var docForm = document.formEnvio;
    // Prepared to be used with more than 1 "applet'
    var exercise_answer = new Array(3); // answer: to get the learner answer
    var exercise_value = new Array(3);  // value from the iLM automatic evaluator
    // var doc = document.formEnvio; // ----------------------------------------------------------
    // var doc = javascript:window.jsAnalyseAnswer();
    try {
      alert('submit_iMA_Answer(): evaluationResult=' + evaluationResult);
      exercise_answer[0] = doc.getAnswer();    // answer: it must be first (usually the iLM first get the answer to compute the evaluation grade)
      exercise_value[0] = doc.getEvaluation(); // value from the iLM automatic evaluator
    } catch (Exception) { alert("Error!"); }
    docForm.iLM_POST_Archive_0.value = exercise_answer[0]; // Content coming from the iLM
    docForm.iLM_POST_Value_0.value = exercise_value[0];    // The activity evaluation coming from the iLM
    // This is used to help the developer to debug his iLM
    // It presents the file content that will be registered.
    alert('The file activity value is: ' + exercise_value[0] + '\nThe activity content is:\n' + exercise_answer[0]);
    //uncomment_this docForm.submit(); // envia de fato o formulario
    //uncomment_this return true;
    */
    }

  function getEvaluationCallback (evaluation) {
    evaluationResult = evaluation;
    strAnswer = window.frames.iLM.getAnswer();
    alert('getEvaluationCallback(...)' + evaluation + ', strAnswer=' + strAnswer);
    comment = document.formEnvio.submission_comment.value;
    //leo
    if ((strAnswer==null || strAnswer=='' || strAnswer==-1) && (comment==null || comment=='')) { // undefined
      alert('Activity sent without content.'); // 'Activity sent without content.'
      return false; // error...
      }
    else {
      //leo alert('getEvaluationCallback: enviando evaluationResult=' + evaluation + ', strAnswer=' + strAnswer);
      document.formEnvio.iLM_PARAM_ArchiveContent.value = strAnswer;
      document.formEnvio.iLM_PARAM_ActivityEvaluation.value = evaluationResult;
      document.formEnvio.submit();
      return true; // success
      }
    }

    function openSolution (url_file, el_button) {
        $(el_button).parent().find('iframe').attr('src', './ivprogh/index.html?iLM_PARAM_AssignmentURL=true&iLM_PARAM_Assignment=' + url_file + '&iLM_PARAM_SendAnswer=false&lang=pt');
      }
//]]>
</script>
<!--
  JavaScript code: final -------------------------------------------------------------------------------
-->

<body>

  <main role="main">
    <div class="jumbotron ivprogh-desc">
      <div class="container">
        <h1> Ambiente de testes para iMA</h1>
      </div>
    </div>
    <div class="container example-large-div autoeval-descript">
      <div data-toggle="collapse" class="pointer" href="#area-autoeval-descript" aria-expanded="false" aria-controls="area-autoeval-descript"><span class="octicon octicon-info"></span> Entenda o funcionamento do testador de iMA</div>
      <div id="area-autoeval-descript" class="collapse">
        <p>Descrição do funcionamento do Ambiente de testes para iMA</p>
        <p>
          para utilizar este arcabouço no ubuntu você terá que desabilitar a proteção de Cross Origin, para usuários de chrome utilize o comando
          "google-chrome --disable-web-security --user-data-dir='caminho para a pasta do seu iMA'"
        </p>
      </div>
    </div>
    <div class="container example-large-div">
      <div class="text-count-example">
        <span class="octicon octicon-rocket"></span> Teste aqui seu iMA
      </div>

      <!-- caminho para iMA-->
      <label> Caminho para o seu iMA: </label>
      <input type="text" size="30" id="iLMsrcField" value="./ivprogh/index.html" required>
      <button type="button" class="btn btn-primary" onclick="updateIMA()"><span class="octicon octicon-sync"></span></button>

      <form>
        Autoria<input type="radio" name="sendAnswer" value="true" onclick="authorMode=true;">
        Resposta<input type="radio" name="sendAnswer" value="false" checked onclick="authorMode=false;">
      </form>

      <!-- caminho para atividade-->
      <label> Caminho para a atividade: </label>
      <input type="text" size="30" id="activitySrc" value="./ex_pt/exerc/exemplo_1_ler_e_imprimir_inteiro.ivph" required>
      <button type="button" onclick="updateActivity()" class="btn btn-primary">
        <span class="octicon octicon-sync"></span>
      </button>

      <div class="container text-center">
        <form name='formEnvio' id='formEnvio' method='post' \ action='#' \ enctype='multipart/form-data'>
        </form>
        <div class="embed-responsive embed-responsive-16by9">
          <iframe name="iLM" id="iLM" class="embed-responsive-item" src="./ivprogh/index.html?iLM_PARAM_AssignmentURL=true&iLM_PARAM_Assignment=./ex_pt/exerc/exemplo_1_ler_e_imprimir_inteiro.ivph&iLM_PARAM_SendAnswer=false" allowfullscreen></iframe><br />
        </div>

        <input type='button' name='Submit' value='Enviar resposta' class="btn btn-warning" title='Clique aqui para submeter sua resolução' onclick='javascript:window.submit_iMA_Answer();' />
      </div>

      <!--  <div class="container text-center">
            <div class="embed-responsive embed-responsive-16by9">
              <iframe name="iLM" id="iLM" class="embed-responsive-item"
                      src="./stub.html"
                      allowfullscreen></iframe><br/>
            </div>
          </div>-->
  </main>
  <footer class="container text-center">
    <a href="https://www.ime.usp.br/~igormf/ima-tutorial/">Tutorial iMA</a> |
    <a href="http://usp.br/line">LInE</a> |
    <a href="http://www.matematica.br">iMática</a>
  </footer>

  <script src="./js/testFunctions.js"></script>
  <script src="./js/jquery.min.js"></script>
  <script src="./js/bootstrap.bundle.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/octicons/3.5.0/octicons.min.css">
  <script type="text/javascript">
    $('.example-content').on('shown.bs.collapse', function() {
      this.parentNode.scrollIntoView();
    });
  </script>

</body>

</html>