compare xml with user responses

1

colleagues. I have this code:

<gabarito>
   <avaliacao tipo="Prova" codigo="01" segmento="Ensino Médio" serie="Pré-Vestibular" questoes="20">
      <disciplina nome="Matemática">
         <questao numero="1" alternativas="A,B,C,D,E">
            <resposta>C</resposta>
         </questao>
         <questao numero="2" alternativas="A,B,C,D,E">
            <resposta>D</resposta>
         </questao>
         <questao numero="3" alternativas="A,B,C,D,E">
            <resposta>A</resposta>
         </questao>
      </disciplina>

      <disciplina nome="Física">
         <questao numero="1" alternativas="A,B,C,D,E">
            <resposta>C</resposta>
         </questao>
         <questao numero="2" alternativas="A,B,C,D,E">
            <resposta>D</resposta>
         </questao>
         <questao numero="3" alternativas="A,B,C,D,E">
            <resposta>A</resposta>
         </questao>
      </disciplina>
   </avaliacao>
</gabarito>

Only students respond to the templates through the system and are stored in the database:

  

A, C, A, A, A, B, C, B, D, A, C, D, B, B, B, A

I would like to compare the students' answers with the feedback, to count and to separate by each discipline. Ex.:

  

Mathematics = > 2 hits; Physics = > 4 right;

Is it possible to do this?

My initial code is this:

foreach($xml->avaliacao->disciplina as $disciplina) {
   $res = $disciplina->attributes();
   if($res["nome"]){
       $questoes = count($xml->avaliacao->disciplina->questao);
       echo $questoes;
   }                                         
}

But I can not make progress ...

    
asked by anonymous 20.04.2016 / 20:45

1 answer

1

I do not quite understand where the correct answers and answers are so that you can determine the number of hits, but I believe this will help you understand how to get the data you want in the file.

What you need to do is go through the disciplines and the questions within each discipline, see the example:

$xml = simplexml_load_file('gabarito.xml');
//percorre as disciplinas
foreach($xml->avaliacao->disciplina as $disciplina) {
    // imprime o nome da disciplina
    echo $disciplina->attributes()->nome.'<br>'; 
    //percorre as questões
    foreach ($disciplina as $questao) { 
        echo $questao->attributes()->numero . ':' . $questao->resposta.'<br>';
    }


}

This code generates the output below according to the data in your file:

Matemática
1:C
2:D
3:A
Física
1:C
2:D
3:A
    
20.04.2016 / 21:52