Quiz Viewer Source

/* XML-based quiz viewer
Andy Harris, 10/03
Loads up an XML file in my own quiz format
Displays one question at a time
User chooses correct answer with custom radio buttons
Scores quiz and allows user to try again
*/

myXML = new XML();
myXML.onLoad = init;
myXML.ignoreWhite = true;
//change this to use any other test file!
myXML.load("python.xml");
stop();

_root.currentQuestion = 0;

function init(){
  //read in quiz XML store in array of nodes called problems
	
  test = myXML.firstChild;
  _root.problems = test.childNodes;
  _root.response = new Array(_root.problems.length);
  score = 0;
  perc = 0;

  //load up first question
  showQuestion(0);

  //assign function handlers to buttons
  btnNext.onRelease = function(){
    storeResponse();
    _root.currentQuestion++;
    if (_root.currentQuestion >= _root.problems.length - 1){
      _root.currentQuestion = _root.problems.length -1;
    } // end if
    showQuestion(_root.currentQuestion);
  } // end release

  btnPrev.onRelease = function(){
    storeResponse();
    _root.currentQuestion--;
    if (_root.currentQuestion < 0){
      _root.currentQuestion = 0;
    } // end if
    showQuestion(_root.currentQuestion);
  } // end release
  
  btnDone.onRelease = function() {
    //grade the quiz
    storeResponse();
    for (i = 0; i < _root.problems.length; i++){
      correct = _root.problems[i].lastChild.firstChild.toString();
      guess = _root.response[i];
      trace (i + ": " + correct + " " + guess);
      if (guess == correct) {
        score++;
      } // end if
    } // end for loop
    trace ("raw score: " + score);
    perc = score / _root.problems.length * 100;
    trace ("percentage: " + perc);
    _root.gotoAndStop("finished");
  } // end btnDone

  //assign behavior to checkboxes
  for (i = 0; i < 4; i++){
    theChar = String.fromCharCode(i + 65);
    theCheck = eval ("chk" + theChar);
    theCheck.state = "blank";
    theCheck.gotoAndStop("blank");
    theCheck.onMouseUp = function(){
      if (this.hitTest(_root._xmouse, _root._ymouse, false)){
        uncheck();	  
        if (this.state == "blank"){
          this.state = "checked";
        } else {
              this.state = "blank";
        } // end 'blank check if'
        this.gotoAndStop(this.state);
      } // end mouse over me if
    } // end function def 
  } // end for loop
} // end init

function uncheck(){
  //unchecks all the check boxes
  for (i = 0; i < 4; i++){
    theChar = String.fromCharCode(i + 65);
    theCheck = eval ("chk" + theChar);
    theCheck.state = "blank";
    theCheck.gotoAndStop("blank");
  } // end for loop
} // end uncheck

function storeResponse(){
  //looks over the checkboxes and stores only the currently chosen response
  for (i = 0; i < 4; i++){
    theChar = String.fromCharCode(i + 65);
    theCheck = eval ("chk" + theChar);
    if (theCheck.state == "checked"){
      _root.response[_root.currentQuestion] = theChar;
    } // end if
  } // end for loop
} // end storeResponse

function showResponse(){
  //if there is a value in the current element of response, display appropriate check
  uncheck();
  guess = _root.response[_root.currentQuestion];
  if (guess != ""){
    theCheck = eval ("chk" + guess);
    theCheck.state = "checked";
    theCheck.gotoAndStop("checked");
  } // end if
} // end showResponse

function showQuestion(currentQuestion){
  //displays appropriate elements of current question on screen.
  theProb = _root.problems[currentQuestion];
  currentNode = theProb.firstChild;  //question node
  _root.txtQ = currentNode.firstChild.toString();
  currentNode = currentNode.nextSibling;  //answer A
  _root.txtA = currentNode.firstChild.toString();
  currentNode = currentNode.nextSibling;  //answer B
  _root.txtB = currentNode.firstChild.toString();
  currentNode = currentNode.nextSibling;  //answer C
  _root.txtC = currentNode.firstChild.toString();
  currentNode = currentNode.nextSibling;  //answer D
  _root.txtD = currentNode.firstChild.toString();
	
  //reflect response (if it exists) in check boxes
  showResponse();
} // end showQuestion