<?PHP
#I parse the page sent back by NCMS in .htNCMSsend.inc.php into the snoopy questions.

function startElement($parser, $name, $attrs)
{
  global $depth;
  global $questionnum;
  /*
  echo "\n";
  for ($i = 0; $i < $depth[$parser]; $i++)
  {
    echo "  ";
  }
    echo "$name";
    $depth[$parser]++;
    if (count($attrs)>0) echo "\n".print_r($attrs,TRUE);
  */
    
    if ($name == "Question")
    {
      global $enrollee_id;
      $question=$attrs["Question"];
      $_SESSION['questions']['questions'][++$questionnum]['question']=$question;
      $query="insert into questions set "
        ."enrollee_id=$enrollee_id"
        .",question='".mysql_escape_string($question)."'";
    }
    if ($name == "Answer")
    {
      if ($questionnum == 0)
      {
        reporterror ("parsing an answer when we don't have a question");
      }
      else
      {
        $num=$attrs["AnswerNumber"];
        $answer=$attrs["Answer"];
        $_SESSION['questions']['questions'][$questionnum]['answers'][$num]=$answer;
        if ($attrs["IsCorrect"] == "True")
        {
          $_SESSION['questions']['questions'][$questionnum]['correct']=$num;
        }
      }
    }
}

function endElement($parser, $name) 
{
    global $depth;
    $depth[$parser]--;
}

function characterData($parser, $data) 
{
    if (strlen(trim($data)) > 0) echo "=<b>$data</b>";
}

function defaultData($parser,$data)
{
if (strlen($data)>0) echo "<FONT color=\"red\">$data</FONT>";
}

#############################   Actual parsing of the xml file

$questionnum=0;
$res=file_get_contents($snoopyfile);
if (strlen($res) == 0)
{
  reportError("The NCMS file, $snoopyfile, was empty.");
}
else
{
  $par = xml_parser_create();
  xml_set_element_handler($par, "startElement", "endElement");
  ##xml_set_character_data_handler($par, "characterData");
  ##xml_set_default_handler($par, "defaultData");
  xml_parser_set_option($par,XML_OPTION_CASE_FOLDING,0);
  xml_parse($par,$res,TRUE);
  xml_parser_free($par);
  $_SESSION['questions']['general']['numquestions']=$questionnum;
  if ($questionnum == 0)
  {
    buggywuggy("No questions came from NCMS.");
    loggywoggy("No questions from NCMS with this reponse:".file_get_contents($snoopyfile));
    reportError("No questions from NCMS.");
    reportError($url);
    reportError($res);
  }
}
?>
