Index: quiz.module
===================================================================
--- quiz.module	1 Mar 2007 07:21:15 -0000	1.8.2.1
+++ quiz.module	1 Mar 2007 20:44:42 -0000
@@ -1,5 +1,5 @@
 <?php
-/* $Id$ */
+/* $Id: quiz.module,v 1.8.2.1 2007/03/01 07:21:15 irf Exp $ */
 
 include(drupal_get_path('module', 'quiz').'/quiz_datetime.inc');
 
@@ -10,6 +10,12 @@ include(drupal_get_path('module', 'quiz'
  * This module allows the creation of interactive quizzes for site visitors
  */
 
+/**
+ * Arg for quiz
+ * TODO: Refactor so all calls to arg() are passed constants
+ */
+define('QUIZ_NID_ARG', 1);
+
 /*
  * Define question statuses...
  */
@@ -83,13 +89,13 @@ function quiz_menu($may_cache) {
   }
   else {
     drupal_add_css(drupal_get_path('module', 'quiz') .'/quiz.css', 'module', 'all');
-    if (arg(0) == 'node' && is_numeric(arg(1))) {
-      $node = node_load(arg(1));
+    if (arg(0) == 'node' && is_numeric(arg(QUIZ_NID_ARG))) {
+      $node = node_load(arg(QUIZ_NID_ARG));
       if ($node->type == 'quiz') {
 
         // Menu item for creating adding questions to quiz
         $items[] = array(
-          'path' => 'node/'. arg(1) .'/questions',
+          'path' => 'node/'. arg(QUIZ_NID_ARG) .'/questions',
           'title' => t('add questions'),
           'callback' => 'quiz_questions',
           'access' => user_access('create quiz'),
@@ -97,7 +103,7 @@ function quiz_menu($may_cache) {
 
         // Menu item for quiz taking interface
         $items[] = array(
-          'path' => 'node/'. arg(1) .'/quiz/start',
+          'path' => 'node/'. arg(QUIZ_NID_ARG) .'/quiz/start',
           'title' => t('take quiz', array('%quiz' => $quiz_name)),
           'callback' => 'quiz_take_quiz',
           'access' => user_access('access quiz'),
@@ -106,7 +112,7 @@ function quiz_menu($may_cache) {
     }
     else {
       $items[] = array(
-        'path' => 'user/'.arg(1).'/myresults',
+        'path' => 'user/'.arg(QUIZ_NID_ARG).'/myresults',
         'title' => t('my results'),
         'callback' => 'quiz_get_user_results',
         'access' => user_access('user results'),
@@ -442,11 +448,11 @@ function quiz_load($node) {
 function quiz_view(&$node, $teaser = FALSE, $page = FALSE) {
   if (!$teaser && user_access('create quiz')) {
     $node = node_prepare(&$node,$teaser);
-    //$node->body = theme('quiz_view', &$node, $teaser, $page);
-  } 
+    $node->content['body']['#value'] = theme('quiz_view', &$node, $teaser, $page);
+  }
   elseif(!$teaser && !user_access('create quiz')) {
     $node = node_prepare(&$node,$teaser);
-    //$node->body .= theme('quiz_availability', $node);
+    $node->content['body']['#value'] .= theme('quiz_view', &$node, $teaser, $page);
   }
   return $node;
 }
@@ -509,17 +515,25 @@ function theme_quiz_view(&$node, $teaser
   // Format Quiz Dates
   $output .= '<h3>'. t('%quiz start/end', array('%quiz' => $quiz_name)) .'</h3>';
   if(!$node->quiz_always){
+
+    // if we are previewing, make sure the dates are timestamps and not form arrays
+    if (is_array($node->quiz_open)) {
+      quiz_translate_form_date(&$node, 'quiz_open');
+    }
+    if (is_array($node->quiz_close)) {
+      quiz_translate_form_date(&$node, 'quiz_close');
+    }
+
+    // format the availability info
     $output .= "<p>" . format_date($node->quiz_open) . " &mdash; " . format_date($node->quiz_close) ."</p>";
     $output .= "<p><strong>". t('Days %quiz live for', array('%quiz' => $quiz_name)) ."</strong> " . floor((strtotime($node->close) - strtotime($node->open)) / 60 / 60 / 24) . "</p>";
-
     $remaining = floor(($node->quiz_close - time()) / 60 / 60 / 24);
     $remaining = ($remaining < 0)?"Expired":$remaining;
     $output .= "<p><strong>Days remaining:</strong> " . $remaining . "</p>";
-
     $elapsed = floor((time() - $node->quiz_open) / 60 / 60 / 24);
     $elapsed = ($elapsed < 0)?(-$elapsed)." days to go":$elapsed;
     $output .= "<p><strong>Days since start:</strong> " . $elapsed . "</p>";
-  } 
+  }
   else {
     $output .= '<p>'. t('This Quiz is always available.') .'</p>'."\n";
   }
@@ -555,9 +569,11 @@ function theme_quiz_view(&$node, $teaser
   }
 
   // Format quiz questions
-  $output .= '<h3>'. t('%quiz Questions', array('%quiz' => $quiz_name)) .'</h3>';
-  $questions = _quiz_get_questions();
-  $output .= theme('quiz_question_table', $questions);
+  if(is_numeric(arg(QUIZ_NID_ARG))){
+    $output .= '<h3>'. t('%quiz Questions', array('%quiz' => $quiz_name)) .'</h3>';
+    $questions = _quiz_get_questions();
+    $output .= theme('quiz_question_table', $questions);
+  }
   return $output;
 }
 
@@ -607,8 +623,8 @@ function quiz_get_user_results() {
 function quiz_take_quiz() {
   global $user;
   $quiz_name = check_plain(variable_get('quiz_name', 'quiz'));
-  if (arg(0) == 'node' && is_numeric(arg(1)) && user_access('access quiz')) {
-    if ($quiz = node_load(arg(1))) {
+  if (arg(0) == 'node' && is_numeric(arg(QUIZ_NID_ARG)) && user_access('access quiz')) {
+    if ($quiz = node_load(arg(QUIZ_NID_ARG))) {
       if (!isset($_SESSION['quiz_'.$quiz->nid]['quiz_questions'])) {
 
         // First time running through quiz
@@ -1032,7 +1048,7 @@ function _quiz_taxonomy_select($value = 
  *   Array of questions
  */
 function _quiz_get_questions() {
-  $quiz = node_load(arg(1));
+  $quiz = node_load(arg(QUIZ_NID_ARG));
   $questions = array();
   if (!empty($quiz->nid)) {
     // Retrieve list of questions
@@ -1042,6 +1058,7 @@ function _quiz_get_questions() {
     WHERE n.nid = q.question_nid
     AND n.nid = nr.nid
     AND q.quiz_nid = %d", $quiz->nid);
+
     // Create questions array
     while ($node = db_fetch_object($result)) {
       $questions[] = quiz_node_map($node);
@@ -1078,7 +1095,7 @@ function quiz_questions_form() {
     $terms = array();
   }
 
-  $quiz = node_load(arg(1));
+  $quiz = node_load(arg(QUIZ_NID_ARG));
 
   // Set page title
   drupal_set_title(check_plain($quiz->title));
@@ -1193,7 +1210,7 @@ function quiz_questions() {
 function quiz_questions_form_submit($form_id, $values) {
   $quiz_name = check_plain(variable_get('quiz_name', 'quiz'));
   // load the node
-  $quiz = node_load(arg(1));
+  $quiz = node_load(arg(QUIZ_NID_ARG));
   // Update quiz with selected question options
   if (!quiz_update_questions($values['question_status'])) {
     form_set_error('', t('Either no questions were selected, or there was a problem updating your %quiz. Please try again.', array('%quiz' => $quiz_name)));
@@ -1303,7 +1320,7 @@ function quiz_node_map($node) {
  *   True if update was a success, false if three was a problem
  */
 function quiz_update_questions($questions) {
-  $quiz = node_load(arg(1));
+  $quiz = node_load(arg(QUIZ_NID_ARG));
   $return = true;
 
   if (!empty($questions)) {
@@ -1657,9 +1674,10 @@ function theme_quiz_question_table($ques
     $rows[] = array(
       $status_descriptions[$question->status],
       check_markup($question->question),
-      $question->type);
+      $question->type,
+      l('Edit', 'node/' . $question->nid . '/edit'));
   }
-  $header = array(t('Status'), t('Question'), t('Type'));
+  $header = array(t('Status'), t('Question'), t('Type'), t('Edit'));
 
   if (isset($rows)) {
     $output .= theme('table', $header, $rows);
