--- quiz.module	2006-09-15 15:50:24.000000000 -0500
+++ quiz.module	2006-09-15 15:48:48.000000000 -0500
@@ -347,12 +347,17 @@ function quiz_view(&$node, $teaser = FAL
 function quiz_take_quiz() {
   global $user;
 
-  if (arg(0) == 'node' && is_numeric(arg(1)) && user_access('access quizzes')) {
+  // make sure the user can access quizzes
+  if(!user_access('access quizzes')){
+    drupal_access_denied();
+  }
+
+  // proceed if we can load the node
+  if (arg(0) == 'node' && is_numeric(arg(1))) {
     if ($quiz = node_load(arg(1))) {
 
+      // First time running through quiz
       if (!isset($_SESSION['quiz_questions'])) {
-
-        // First time running through quiz
         if ($rid = quiz_start_actions($user->uid, $quiz->nid)) {
 
           // Create question list
@@ -375,34 +380,83 @@ function quiz_take_quiz() {
         db_query("REPLACE {quiz_question_results} VALUES(%d, %d, '%s')", $_SESSION['rid'], $former_question->nid, serialize($result));
       }
 
-      // Check if at the end of quiz
+      // Check to see if we need to load the next question
       if (!empty($_SESSION['quiz_questions'])) {
-
-        // Load the next question
         $question_node = node_load(array('nid' => $_SESSION['quiz_questions'][0]));
-
-        $node->body = module_invoke($question_node->type, 'render_question', $question_node);
-
+        $question = module_invoke($question_node->type, 'render_question', $question_node);
+        return theme('quiz_question', $quiz, $question);
       }
-      else {
-
-        // At the end of quiz...
-        
-        //First - update the result to show we have finished.
-        db_query("UPDATE {quiz_result} SET time_end = NOW() WHERE rid = %d", $_SESSION['rid']);
+      
+      // Otherwise we are at the end of the quiz
+      
+      //First - update the result to show we have finished.
+      db_query("UPDATE {quiz_result} SET time_end = NOW() WHERE rid = %d", $_SESSION['rid']);
         
-        //display results and remove session variables
-        $node->body = "Your score: " .quiz_calculate_score($_SESSION['rid']);
-        unset($_SESSION['quiz_questions']);
-        unset($_SESSION['rid']);
-
-      }
-    }
-    else {
-      drupal_not_found();
+      //display results and remove session variables
+      $score = quiz_calculate_score($_SESSION['rid']);
+      unset($_SESSION['quiz_questions']);
+      unset($_SESSION['rid']);
+      return theme('quiz_result', $quiz, $score);
+      
     }
-    return $node->body; // TODO: Can this be THEMED?
   }
+  drupal_not_found();
+}
+
+/**
+ * Theme function for the quiz questions
+ * 
+ * @param $node
+ *   The node object
+ * @param $question
+ *   The html generated for the question
+ * @return html
+ */
+function theme_quiz_question($quiz, $question){
+  
+  $output = '';
+    
+  // Set the title (doing it here allows for theme developers to customize it)
+  drupal_set_title(check_plain($quiz->title));
+
+  // Display the number of question remaining
+  $remaining = 0;
+  if(is_array($_SESSION['quiz_questions'])){
+    $remaining = count($_SESSION['quiz_questions']);
+  }
+  $current = $quiz->number_of_questions - $remaining + 1;
+  $output .= '<div class="quiz_question_count">'. t('Question %current of %total', array('%current' => $current, '%total' => $quiz->number_of_questions)) .'</div><br />'."\n";
+
+  // Display the html that was generated by the module invoke
+  $output .= $question;
+    
+  return $output;
+  
+}
+
+/**
+ * Theme function for the quiz results
+ * 
+ * @param $node
+ *   The node object
+ * @param $score
+ *   The array holding the score
+ * @return html
+ * */
+function theme_quiz_result($quiz, $score){
+
+  $output = '';
+  
+  // Set the title (doing it here allows for theme developers to customize it)
+  drupal_set_title(check_plain($quiz->title) . t(' results'));
+  
+  // Display the score
+  $output .= '<div class="quiz_score">';
+  $output .= t('You got %num_correct out of %question_count correct. Your score: %percentage_score%.', array('%num_correct' => $score['num_correct'], '%question_count' => $score['question_count'], '%percentage_score' => $score['percentage_score']));
+  $output .= '</div>'."\n";
+
+  return $output;
+
 }
 
 /**
@@ -442,27 +496,46 @@ function quiz_start_actions($uid, $nid) 
  *
  * @param $rid
  *   Quiz result ID
- * @return integer
- *   Total score
+ * @return array
+ *   Contains three elements: question_count, num_correct and percentage_score
  */
 function quiz_calculate_score($rid) {
-  $score = 0;
+  
+  // initialize our variables
+  $question_count = 0;
+  $num_correct = 0;
+  $percentage_score = 0;
+
+  // Get the results from the database
   $result = db_query("SELECT
                        qqr.answer answer,
                        qqr.question_nid qnid,
                        n.type type
                      FROM {quiz_question_results} qqr, {node} n
                      WHERE qqr.result_rid = %d AND n.nid = qqr.question_nid", $rid);
-  
-  
+
+  // calculate the result from each and build the number of correct answers
   while($r = db_fetch_array($result)) {
+  	$question_count++;
     $r['answer'] = unserialize($r['answer']);
     $s = module_invoke($r['type'], 'calculate_result', $r['answer']['answers'], $r['answer']['tried']);
-    
-    $score += $s;
-    $r['score'] = $s;
+    $num_correct += $s;
+    $r['score'] = $s; // I think this is legacy
+  }
+
+  // calculate the percentage score
+  if($question_count > 0){
+    $percentage_score = round(($num_correct / $question_count) * 100, 0);
   }
+  
+  // build the score array
+  $score = array(
+    'question_count' => $question_count,
+    'num_correct' => $num_correct, 
+    'percentage_score' => $percentage_score,
+  );
 
+  // return the array 
   return $score;
 }
 
