Index: modules/nmoderation/nmoderation.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/nmoderation/nmoderation.module,v
retrieving revision 1.22
diff -u -r1.22 nmoderation.module
--- modules/nmoderation/nmoderation.module	25 Apr 2005 12:25:25 -0000	1.22
+++ modules/nmoderation/nmoderation.module	6 Mar 2006 21:05:26 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: nmoderation.module,v 1.22 2005/04/25 12:25:25 weitzman Exp $
+// $Id: nmoderation.module,v 1.6 2006/01/16 07:59:15 eaton Exp $
 
 /**
  * @file
@@ -33,8 +33,6 @@
       'callback' => 'nmoderation_vote_settings', 'access' => $access,'type' => MENU_LOCAL_TASK);
 
     $access = user_access('moderate nodes');
-    $items[] = array('path' => 'nmoderation/add', 'title' => t('add node moderation vote'),
-      'callback' => 'nmoderation_uservote_add', 'access' => $access, 'type' => MENU_CALLBACK);
     $items[] = array('path' => 'nmoderation/edit', 'title' => t('edit node moderation vote'),
       'callback' => 'nmoderation_uservote_edit', 'access' => $access, 'type' => MENU_CALLBACK);
     $items[] = array('path' => 'nmoderation/delete', 'title' => t('delete node moderation vote'),
@@ -59,7 +57,7 @@
 function nmoderation_set_head() {
   global $base_url;
   if (user_access('access content')) {
-    $output[] = "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"$base_url/modules/nmoderation/nmoderation.css\" />";
+    $output[] = '<link rel="stylesheet" type="text/css" media="screen" href="'. drupal_get_path('module','nmoderation') . '/nmoderation.css" />';
     return $output;
   }
 }
@@ -72,16 +70,6 @@
 }
 
 function nmoderation_matrix_settings() {
-  if ($edit = $_POST['edit']) {
-    db_query('DELETE FROM {nmoderation_roles} ');
-    foreach ($edit as $role_id => $votes) {
-      foreach ($votes as $mid => $value) {
-        $sql = "('$mid', '$role_id', '". ($value ? $value : 0) ."')";
-        db_query('INSERT INTO {nmoderation_roles} (mid, rid, value) VALUES '. $sql);
-      }
-    }
-    drupal_set_message(t('the vote values have been saved.'));
-  }
   $output .= '<h3>Node Moderation vote/value matrix</h3>';
   $result = db_query("SELECT r.rid, r.name FROM {role} r, {permission} p WHERE r.rid = p.rid AND p.perm LIKE '%moderate nodes%'");
   $role_names = array();
@@ -94,108 +82,234 @@
     $mod_roles[$role->rid][$role->mid] = $role->value;
   }
 
-  $header = array_merge(array(t('votes')), array_values($role_names));
-
   $result = db_query('SELECT mid, vote FROM {nmoderation_votes} ORDER BY weight');
   while ($vote = db_fetch_object($result)) {
-    $row = array($vote->vote);
+    $form['votes'][$vote->mid]['vote'] = array(
+      '#type' => 'markup',
+      '#value' => $vote->vote,
+    );
     foreach (array_keys($role_names) as $rid) {
-      $row[] = array('data' => form_textfield(NULL, "$rid][$vote->mid", $mod_roles[$rid][$vote->mid], 4, 3));
+      $form['votes'][$vote->mid][$rid] = array(
+        '#type' => 'textfield',
+        '#default_value' => $mod_roles[$rid][$vote->mid],
+        '#size' => 3,
+        '#maxlength' => 4,
+      );
+    }
+  }
+
+  $form['#tree'] = TRUE; // make sure we preserve all hierarchial values.
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit votes')
+  );
+
+  // Stick a few extra bits into the $form array for the theming function to use
+  $form['role_names'] = array(
+    '#type' => 'value',
+    '#value' => $role_names,
+  );
+
+  $output .= drupal_get_form('nmoderation_matrix_form', $form);
+  return $output;
+}
+
+function nmoderation_matrix_form_submit($form_id, $form_values) {
+  // db_query('DELETE FROM {nmoderation_roles} ');
+  foreach (element_children($form_values['votes']) as $mid) {
+    foreach (element_children($form_values['votes'][$mid]) as $rid) {
+    	$value = $form_values['votes'][$mid][$rid];
+      $sql = "('$mid', '$rid', '". ($value ? $value : 0) ."')";
+      db_query('INSERT INTO {nmoderation_roles} (mid, rid, value) VALUES '. $sql);
+    }
+  }
+
+  drupal_set_message(t('the vote values have been saved.'));
+}
+
+function theme_nmoderation_matrix_form(&$form) {
+  $role_names = $form['role_names']['#value'];
+  $header = array_merge(array(t('votes')), array_values($role_names));
+
+  foreach (element_children($form['votes']) as $mid) {
+    $row = array();
+    foreach (element_children($form['votes'][$mid]) as $rid) {
+      $row[] = form_render($form['votes'][$mid][$rid]);
     }
     $rows[] = $row;
   }
+
   if (!$rows) {
     $rows[] = array(array('data' => t('No votes have been defined.'), 'colspan' => '5'));
+    unset($form['submit']);
   }
 
-  $output .= theme('table', $header, $rows);
-  if ($rows) { $output .= '<br />'. form_submit(t('Submit votes')); }
-
-  return form($output);
+  $output = theme('table', $header, $rows);
+  $output .= form_render($form);
+  return $output;
 }
 
 /**
  * Menu callback; allows admin to set default scores for different roles.
  */
 function nmoderation_role_settings() {
+  $output .= '<h3>Initial node scores</h3>';
+  
+  $start_values = variable_get('nmoderation_roles', array());
+  
+  $result = db_query("SELECT r.rid, r.name FROM {role} r, {permission} p WHERE r.rid = p.rid AND p.perm LIKE '%moderate nodes%' ORDER BY r.rid");
+  while ($role = db_fetch_object($result)) {
+    $form['role'][$role->rid] = array(
+      '#title' => $role->name,
+      '#type' => 'textfield',
+      '#default_value' => $start_values[$role->rid],
+      '#size' => 3,
+      '#maxlength' => 4,
+    );
+  }
 
-  $edit = $_POST['edit'];
+  $form['#tree'] = TRUE; // make sure we preserve all hierarchial values.
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save scores')
+  );
 
-  $output .= '<h3>Initial node scores</h3>';
+  $output .= drupal_get_form('nmoderation_role_form', $form);
+  return $output;
+}
 
-  if ($edit) {
-    variable_set('nmoderation_roles', $edit);
-    drupal_set_message(t('the node moderation scores have been saved.'));
-  }
+function nmoderation_role_form_validate($form_id, $form_values) {
+	// make sure everything is numeric/int
+}
 
-  $start_values = variable_get('nmoderation_roles', array());
-  $result = db_query("SELECT r.rid, r.name FROM {role} r, {permission} p WHERE r.rid = p.rid AND p.perm LIKE '%moderate nodes%'");
+function nmoderation_role_form_submit($form_id, $form_values) {
+	  $new_values = array();
+	  foreach (element_children($form_values['role']) as $rid) {
+      $new_values[$rid] = $form_values['role'][$rid]['#value'];
+    }
+    variable_set('nmoderation_roles', $new_values);
+    drupal_set_message(t('the node moderation scores have been saved.'));
+}
 
+function theme_nmoderation_role_form(&$form) {
   $header = array(t('user role'), t('initial score'));
 
-  while ($role = db_fetch_object($result)) {
-    $rows[] = array($role->name, array('data' => form_textfield(NULL, $role->rid, $start_values[$role->rid], 4, 3), 'align' => 'center'));
+  $rows = array();
+  foreach (element_children($form['role']) as $rid) {
+    $row = array();
+    $row[] = $form['role'][$rid]['#title'];
+    unset($form['role'][$rid]['#title']);
+    $row[] = form_render($form['role'][$rid]);
+    $rows[] = $row;
   }
 
-  $output .= theme('table', $header, $rows);
-  $output .= '<br />'. form_submit(t('Save scores'));
-
-  return form($output);
+  $output = theme('table', $header, $rows);
+  $output .= form_render($form);
+  return $output;
 }
 
 /**
  * Menu callback; displays page for assigning names to vote values.
  */
-function nmoderation_vote_settings($mid = 0) {
-  $op   = $_POST['op'];
-  $edit = $_POST['edit'];
-
-  if ($op == t('Save vote')) {
-    db_query("UPDATE {nmoderation_votes} SET vote = '%s', weight = %d WHERE mid = %d", $edit['vote'], $edit['weight'], $mid);
-    $mid = 0; // zero it out so we return to the overview.
-    drupal_set_message(t('the vote has been saved.'));
-  }
-  else if ($op == t('Delete vote')) {
-    db_query('DELETE FROM {nmoderation_votes} WHERE mid = %d', $mid);
-    db_query('DELETE FROM {nmoderation_roles} WHERE mid = %d', $mid);
-    $mid = 0; // zero it out so we return to the overview.
-    drupal_set_message(t('the vote has been deleted.'));
-  }
-  else if ($op == t('Add new vote')) {
-    db_query("INSERT INTO {nmoderation_votes} (vote, weight) VALUES ('%s', %d)", $edit['vote'], $edit['weight']);
-    $mid = 0; // zero it out so we return to the overview.
-    drupal_set_message(t('the vote has been added.'));
-  }
-
+function nmoderation_vote_settings() {
   $output .= '<h3>'. t('Node moderation votes overview') .'</h3>';
 
   // load up and show any vote types previously defined.
-  $header = array(t('votes'), t('weight'), t('operations'));
   $result = db_query('SELECT mid, vote, weight FROM {nmoderation_votes} ORDER BY weight');
   while ($vote = db_fetch_object($result)) {
-    $rows[] = array($vote->vote, array('data' => $vote->weight), array('data' => l(t('edit'), "admin/nmoderation/votes/$vote->mid")));
-  }
-  if (!$rows) {
-    $rows[] = array(array('data' => t('No vote types have been defined.'), 'colspan' => '3'));
-  }
-  $output .= theme('table', $header, $rows);
-
-  if ($mid) { // if we're not saving, deleting, or adding, we must be editing, so prefill the form fields.
-    $vote = db_fetch_object(db_query('SELECT vote, weight FROM {nmoderation_votes} WHERE mid = %d', $mid));
-  }
-
-  $output .= '<br /><h3>'. (isset($mid) ? 'Edit' : 'Add new') .' node moderation option</h3>';
-  $form .= form_textfield(t('Vote'), 'vote', $vote->vote, 32, 64, t('The name of this vote.  Example: "off topic", "excellent", "sucky".'));
-  $form .= form_textfield(t('Weight'), 'weight', $vote->weight, 32, 64, t('Used to order votes in the voting box; heavier sink.'));
-  if ($mid) {
-    $form .= form_submit(t('Save vote'));
-    $form .= form_submit(t('Delete vote'));
-  }
-  else {
-    $form .= form_submit(t('Add new vote'));
+		$form['old'][$vote->mid]['vote'] = array(
+		  '#type' => 'textfield',
+		  '#default_value' => $vote->vote,
+		  '#size' => 32,
+		  '#maxlength' => 64,
+		);
+		
+		$form['old'][$vote->mid]['weight'] = array(
+		  '#type' => 'textfield',
+		  '#default_value' => $vote->weight,
+		  '#size' => 32,
+		  '#maxlength' => 64,
+		);
+
+		$form['old'][$vote->mid]['delete'] = array(
+		  '#type' => 'checkbox',
+		  '#default_value' => 0,
+		  '#return_value' => 1,
+		);
+  }
+  
+  // Add a set of fields for new votes.
+  $form['new']['vote'] = array(
+	 '#type' => 'textfield',
+	 '#default_value' => $vote->vote,
+	 '#size' => 32,
+	 '#maxlength' => 64,
+	);
+		
+	$form['new']['weight'] = array(
+	  '#type' => 'textfield',
+	  '#default_value' => $vote->weight,
+	  '#size' => 32,
+	  '#maxlength' => 64,
+	);
+
+  $form['#tree'] = TRUE; // make sure we preserve all hierarchial values.
+	$form[] = array(
+	  '#type' => 'submit',
+	  '#value' => t('Save changes'),
+	);
+
+	$output = drupal_get_form('nmoderation_vote_form', $form);
+	return $output;
+}
+
+function nmoderation_vote_form_validate($form_id, $form_values) {
+	// make sure $weight is an int and $vote is markup clean, no injections.
+}
+
+function nmoderation_vote_form_submit($form_id, $form_values) {
+	// loop through the fields for old votes, update or delete as necessary.
+  foreach (element_children($form_values['old']) as $mid) {
+  	if ($form_values['old'][$mid]['delete'] == 1) {
+   		db_query('DELETE FROM {nmoderation_votes} WHERE mid = %d', $mid);
+    	db_query('DELETE FROM {nmoderation_roles} WHERE mid = %d', $mid);
+  	}
+  	else {
+    	db_query("UPDATE {nmoderation_votes} SET vote = '%s', weight = %d WHERE mid = %d", $form_values['old'][$mid]['vote'], $form_values['old'][$mid]['weight'], $mid);
+  	}
+  }	
+  
+	// if a new vote has been added, insert it.
+	if ($form_values['new']['vote'] != '') {
+    db_query("INSERT INTO {nmoderation_votes} (vote, weight) VALUES ('%s', %d)", $form_values['new']['vote'], $form_values['new']['weight']);
+	}
+
+  drupal_set_message(t('the changes have been saved.'));
+  drupal_goto('admin/nmoderation/votes'); // do this because we want the form completely rebuilt.
+}
+
+function theme_nmoderation_vote_form(&$form) {
+  $header = array(t('votes'), t('weight'), t('delete'));
+  
+  // render the existing-vote fields.
+  foreach (element_children($form['old']) as $mid) {
+    $row = array();
+    $row[] = form_render($form['old'][$mid]['vote']);
+    $row[] = form_render($form['old'][$mid]['weight']);
+    $row[] = form_render($form['old'][$mid]['delete']);
+    $rows[] = $row;
   }
 
-  return $output . form($form);
+  // render the add-a-vote fields.
+  $row = array();
+  $row[] = form_render($form['new']['vote']);
+  $row[] = form_render($form['new']['weight']);
+  $row[] = '';
+  $rows[] = $row;
+  
+  $output .= theme('table', $header, $rows);
+  $output .= form_render($form);
+  return $output;
 }
 
 function nmoderation_get_vote($uid, $gid, $realm = 'node_nid') {
@@ -222,17 +336,100 @@
   }
 }
 
-// TODO disallow adds on unwanted node types, out of interval, self
-function nmoderation_uservote_add($nid) {
+function nmoderation_get_moderation_form($nid, $vote, $title= NULL) {
+  global $user;
+  static $votes;
+
+  if (user_access('moderate nodes')) {
+    if (!isset($votes) && $user->roles) {
+      $result = db_query('SELECT mid, vote FROM {nmoderation_votes} ORDER BY weight ASC');
+      $votes = array();
+      while ($dbvote = db_fetch_object($result)) {
+        $votes[$dbvote->mid] = $dbvote->vote;
+      }
+    }
+    if ($votes) {
+			$form['mid'] = array(
+			  '#type' => 'select',
+			  '#title' => t('Moderate Post'),
+			  '#default_value' => $vote->mid,
+			  '#options' => $votes,
+			  '#description' => t('Your vote contributes to whether or not this post appears on the home page.'),
+			);
+			
+			$form['nid'] = array(
+			  '#type' => 'hidden',
+			  '#value' => $nid,
+			);
+
+			if ($vote) {
+				$form['uvid'] = array(
+				  '#type' => 'hidden',
+				  '#value' => $vote->uvid,
+				);
+			}
+
+			if ($title) {
+			  $form['submit'] = array(
+			    '#type' => 'submit',
+			    '#value' => $title,
+			  );
+			}
+      return $form;
+    }
+  }
+}
+
+function nmoderation_moderation_form_validate($form_id, $form_values) {
+	// THIS IS PROBABLY UNECESSARY, BUT WE'LL KEEP IT FOR NOW.
+}
+
+function nmoderation_moderation_form_submit($form_id, $form_values) {
   global $user;
-  $edit = $_POST['edit'];
-  $mid = $edit['mid'];
-  $value = nmoderation_get_value($mid);
-  $sql = "INSERT INTO {nmoderation_uservotes} (uid, mid, gid, value, realm) VALUES ($user->uid, $mid, $nid, $value, 'node_nid')";
-  db_query($sql);
-  nmoderation_set_promote($nid);
-  drupal_set_message(t('Vote added.'));
-  drupal_goto($edit['destination']);
+  $nid = $form_values['nid'];
+  $mid = $form_values['mid'];
+	$uvid = $form_values['uvid'];
+
+	if ($uvid) {
+		// There's already a vote, we're editing.
+		$sql = 'SELECT uid, gid, mid FROM {nmoderation_uservotes} WHERE uvid = %d';
+		$vote = db_fetch_object(db_query($sql, $uvid));
+
+    if ($vote->uid != $user->uid) {
+      $account = user_load(array('uid' => $vote->uid));
+    }
+
+    $value = nmoderation_get_value($mid, $account);
+    $sql = 'UPDATE {nmoderation_uservotes} SET mid = %d, value = %d WHERE uvid = %d';
+    db_query($sql, $mid, $value, $uvid);
+    nmoderation_set_promote($vote->gid);
+    drupal_set_message(t('Vote edited.'));
+	}
+	else {
+		// No existing vote, let's inesrt one.
+	  $value = nmoderation_get_value($mid);
+	  $sql = "INSERT INTO {nmoderation_uservotes} (uid, mid, gid, value, realm) VALUES ($user->uid, $mid, $nid, $value, 'node_nid')";
+	  db_query($sql);
+	  nmoderation_set_promote($nid);
+	  drupal_set_message(t('Vote added.'));
+	}
+	
+	// If a hidden destination field has been added to the form,
+	// redirect to it. This allows some pages to redirect to a
+	// 'thanks for voting' page while others simply refresh.
+	if ($form_values['destination']) {
+		drupal_goto($form_values['destination']);
+	}
+	else {
+		drupal_goto($_GET['q']);
+	}
+}
+
+function theme_nmoderation_moderation_form($form) {
+	// We aren't actually going to do anything fancy, but it's here
+	// in case folks want to override it for a given theme.
+	$output = form_render($form);
+	return $output;
 }
 
 // if you pass in an $account object, it must have $uid and $roles elements
@@ -248,34 +445,32 @@
 
 function nmoderation_uservote_edit($uvid) {
   global $user;
-  $edit = $_POST['edit'];
-  $sql = 'SELECT uid, gid, mid FROM {nmoderation_uservotes} WHERE uvid = %d';
+  $sql = 'SELECT uvid, uid, gid, mid FROM {nmoderation_uservotes} WHERE uvid = %d';
   $vote = db_fetch_object(db_query($sql, $uvid));
 
   // may only edit own vote if you are not an admin
   if ($user->uid == $vote->uid || user_access('administer node moderation votes')) {
+    $node = node_load($vote->gid);
+    $form = nmoderation_get_moderation_form($node->nid, $vote, t('Edit vote'));
+    
     if ($vote->uid != $user->uid) {
-      $account = user_load(array('uid' => $vote->uid));
-      $output = form_item(t('Voter'), format_name($account));
-    }
-    if ($_POST['op'] == t('Edit vote')) {
-      $value = nmoderation_get_value($edit['mid'], $account);
-      $sql = 'UPDATE {nmoderation_uservotes} SET mid = %d, value = %d WHERE uvid = %d';
-      db_query($sql, $edit['mid'], $value, $uvid);
-      nmoderation_set_promote($vote->gid);
-      drupal_set_message(t('Vote edited.'));
-      $dest = $edit['destination'] ? $edit['destination'] : 'nmoderation/voted';
-      drupal_goto($dest);
-    }
-    else {
-      $sql = "SELECT title FROM {node} WHERE nid = %d";
-      $title = db_result(db_query($sql, $vote->gid));
-      $output .= form_item(t('Title'), $title);
-      $frm = nmoderation_form($vote->mid);
-      $frm .= form_submit(t('Edit vote'));
-      $output .= form($frm, 'post', url("nmoderation/edit/$uvid"));
-      return $output;
-    }
+		  $form['username'] = array(
+		    '#type' => 'markup',
+		    '#title' => t('Voter'),
+		    '#value' => theme('username', $account),
+		    '#weight' => -2,
+		  );
+  	}
+    
+	  $form['title'] = array(
+	    '#type' => 'markup',
+	    '#title' => t('Title'),
+	    '#value' => $node->title,
+		  '#weight' => -1,
+	  );
+
+    $output .= drupal_get_form('nmoderation_moderation_form', $form);
+    return $output;
   }
   else {
     drupal_access_denied();
@@ -283,29 +478,36 @@
 }
 
 function nmoderation_uservote_delete($uvid) {
-  global $user;
-  $edit = $_POST['edit'];
-  if ($_POST['op'] == t('Delete vote')) {
+  $form['uvid'] = array('#type' => 'value', '#value' => $uvid);
+  $output = confirm_form(
+  	'nmoderation_delete_confirm',
+  	$form,
+    t('Are you sure you want to delete this vote?'),
+    $_GET['destination'] ? $_GET['destination'] : 'nmoderation/voted',
+    t('This action cannot be undone.'),
+    t('Delete Vote'),
+    t('Cancel') 
+  );
+  return $output;
+}
+
+function nmoderation_delete_confirm_submit($form_id, $form_values) {
+	global $user;
+  if ($form_values['confirm']) {
     $sql = 'SELECT uid, gid FROM {nmoderation_uservotes} WHERE uvid = %d';
     $vote = db_fetch_object(db_query($sql, $uvid));
-    // may only delete own vote if you are not an admin
+
     if ($user->uid == $vote->uid || user_access('administer node moderation votes')) {
-      $sql = "DELETE FROM {nmoderation_uservotes} WHERE uvid=%d";
-      db_query($sql, $uvid);
-      drupal_set_message(t('Vote deleted.'));
-      nmoderation_set_promote($vote->gid);
-      $dest = $edit['destination'] ? $edit['destination'] : 'nmoderation/voted';
-      drupal_goto($dest);
+	    $sql = "DELETE FROM {nmoderation_uservotes} WHERE uvid=%d";
+	    db_query($sql, $form_values['uvid']);
+	    drupal_set_message(t('Vote deleted.'));
+	    nmoderation_set_promote($vote->gid);
+	    drupal_goto('nmoderation/voted');
     }
     else {
       drupal_access_denied();
     }
   }
-  else {
-    $frm = form_item(t('Confirmation'), t('Are you sure you want to delete this vote?'));
-    $frm .= form_button(t('Delete vote'));
-    return form($frm);
-  }
 }
 
 // will either promote or unpublish or do nothing, depending on the curent score for this node
@@ -313,7 +515,7 @@
   $sql = "SELECT SUM(value) AS score FROM {nmoderation_uservotes} WHERE gid = $gid AND realm = '$realm'";
   $result = db_query($sql);
   $row = db_fetch_object($result);
-  $node = node_load(array('nid' => $gid));
+  $node = node_load($gid);
   // have to load terms in order that they don't get lost upon saving the node.
   $node->taxonomy = array_keys(taxonomy_node_get_terms($gid));
   if ($row->score < variable_get('nmoderation_threshold_unpublish', 4)) {
@@ -336,7 +538,7 @@
   );
   $cols = 2;
   $title = t('my unmoderated nodes');
-  $types = implode("','", variable_get('nmoderation_node_types', drupal_map_assoc(node_list())));
+  $types = implode("','", variable_get('nmoderation_node_types', node_get_types()));
   $sql = "SELECT n.nid, n.created, u.name, n.title, n.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {nmoderation_uservotes} nmuv ON n.nid = nmuv.gid AND nmuv.realm = '$realm' AND nmuv.uid = $user->uid WHERE n.type IN ('$types') AND n.status = 1 AND ISNULL(nmuv.uvid)";
   $min = time()-24*3600*variable_get('nmoderation_vote_interval', 7);
   $sql .= " AND n.created > $min";
@@ -346,7 +548,7 @@
   $sql .= tablesort_sql($header);
   $result = pager_query(db_rewrite_sql($sql), 50);
   while ($vote = db_fetch_object($result)) {
-    $rows[] = array(l($vote->title, "node/$vote->nid"), format_name($vote), format_date($vote->created, 'small'));
+    $rows[] = array(l($vote->title, "node/$vote->nid"), theme('username', $vote), format_date($vote->created, 'small'));
   }
   if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
     $rows[] = array(array('data' => $pager, 'colspan' => $cols));
@@ -370,7 +572,7 @@
   $sql .= tablesort_sql($header);
   $result = pager_query(db_rewrite_sql($sql), 50);
   while ($vote = db_fetch_object($result)) {
-    $row = array(l($vote->title, "node/$vote->nid"), format_name($vote), format_date($vote->created, 'small'), $vote->vote);
+    $row = array(l($vote->title, "node/$vote->nid"), theme('username', $vote), format_date($vote->created, 'small'), $vote->vote);
     if (nmoderation_is_votable($vote)) {
       $row[] = l(t('edit'), "nmoderation/edit/$vote->uvid");
       $row[] = l(t('delete'), "nmoderation/delete/$vote->uvid");
@@ -401,7 +603,7 @@
   $sql .= tablesort_sql($header);
   $result = pager_query(db_rewrite_sql($sql), 50);
   while ($vote = db_fetch_object($result)) {
-    $rows[] = array(l($vote->title, "node/$vote->nid"), format_date($vote->created, 'small'), format_name($vote), $vote->vote, $vote->value, l(t('edit'), "nmoderation/edit/$vote->uvid"), l(t('delete'), "nmoderation/delete/$vote->uvid"));
+    $rows[] = array(l($vote->title, "node/$vote->nid"), format_date($vote->created, 'small'), theme('username', $vote), $vote->vote, $vote->value, l(t('edit'), "nmoderation/edit/$vote->uvid"), l(t('delete'), "nmoderation/delete/$vote->uvid"));
   }
   if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
     $rows[] = array(array('data' => $pager, 'colspan' => $cols));
@@ -422,13 +624,13 @@
   $sql .= tablesort_sql($header);
   $result = pager_query($sql, 50, NULL, NULL, $gid);
   while ($vote = db_fetch_object($result)) {
-    $rows[] = array(format_name($vote), $vote->vote, $vote->value, l(t('edit'), "nmoderation/edit/$vote->uvid"), l(t('delete'), "nmoderation/delete/$vote->uvid"));
+    $rows[] = array(theme('username', $vote), $vote->vote, $vote->value, l(t('edit'), "nmoderation/edit/$vote->uvid"), l(t('delete'), "nmoderation/delete/$vote->uvid"));
   }
   if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
     $rows[] = array(array('data' => $pager, 'colspan' => $cols));
   }
   $output .= theme('table', $header, $rows);
-  $node = node_load(array('nid' => $gid));
+  $node = node_load($gid);
   drupal_set_title($title);
   return $output;
 }
@@ -438,11 +640,38 @@
   $unpublish = drupal_map_assoc(array(-1, -2, -3, -4, -5, -6, -7, -8, -8, -10, -11, -12, -13, -14, -15, -20, -25, -30));
   $interval = drupal_map_assoc(range(1,60));
 
-  $output .= form_select(t('Node Types'), 'nmoderation_node_types', variable_get('nmoderation_node_types', drupal_map_assoc(node_list())), drupal_map_assoc(node_list()), t('Select the node types upon which accept node moderation votes.'), 0, 1);
-  $output .= form_select(t('Promote threshold'), 'nmoderation_threshold_promote', variable_get('nmoderation_threshold_promote', 4), $promote, t('When a post exceeds this moderation score, it is immediately <strong>promoted to the front page</strong>.'));
-  $output .= form_select(t('Unpublish threshold'), 'nmoderation_threshold_unpublish', variable_get('nmoderation_threshold_unpublish', -2), $unpublish, t('When a post drops below this score, it is immediately <strong>unpublished</strong>.'));
-  $output .= form_select(t('Voting Interval'), 'nmoderation_vote_interval', variable_get('nmoderation_vote_interval', 7), $interval, t('The number of days since the creation of a node when we allow voting.'));
-  return $output;
+	$form['nmoderation_node_types'] = array(
+	  '#type' => 'select',
+	  '#title' => t('Node Types'),
+	  '#default_value' => variable_get('nmoderation_node_types', node_get_types()),
+	  '#options' => node_get_types(),
+	  '#description' => t('Select the node types upon which accept node moderation votes.'),
+	  '#extra' => 0,
+	  '#multiple' => 1,
+	);
+	$form['nmoderation_threshold_promote'] = array(
+	  '#type' => 'select',
+	  '#title' => t('Promote threshold'),
+	  '#default_value' => variable_get('nmoderation_threshold_promote', 4),
+	  '#options' => $promote,
+	  '#description' => t('When a post exceeds this moderation score, it is immediately <strong>promoted to the front page</strong>.'),
+	);
+	$form['nmoderation_threshold_unpublish'] = array(
+	  '#type' => 'select',
+	  '#title' => t('Unpublish threshold'),
+	  '#default_value' => variable_get('nmoderation_threshold_unpublish', -2),
+	  '#options' => $unpublish,
+	  '#description' => t('When a post drops below this score, it is immediately <strong>unpublished</strong>.'),
+	);
+	$form['nmoderation_vote_interval'] = array(
+	  '#type' => 'select',
+	  '#title' => t('Voting Interval'),
+	  '#default_value' => variable_get('nmoderation_vote_interval', 7),
+	  '#options' => $interval,
+	  '#description' => t('The number of days since the creation of a node when we allow voting.'),
+	);
+
+  return $form;
 }
 
 
@@ -462,26 +691,20 @@
 function nmoderation_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   switch ($op) {
     case 'view':
-        if ($page && nmoderation_is_votable($node) && in_array($node->type, variable_get('nmoderation_node_types', drupal_map_assoc(node_list())))) {
+        if ($page && nmoderation_is_votable($node) && in_array($node->type, variable_get('nmoderation_node_types', node_get_types()))) {
           global $user;
           $vote = nmoderation_get_vote($user->uid, $node->nid);
 
           if ($vote->uvid) {
-            $btn = t('Edit vote');
-            $action = "edit/$vote->uvid";
+            $title = t('Edit vote');
           }
           else {
-            $btn = t('Add vote');
-            $action = "add/$node->nid";
+            $title = t('Add vote');
           }
-          if ($frm = nmoderation_form($vote->mid)) {
-            $frm .= form_hidden('destination', $_GET['q']);
-            $frm .= form_button($btn);
-            $node->body .= form($frm, 'post', url("nmoderation/$action"), array('class' => 'nmoderation-form'));
+          if ($frm = nmoderation_get_moderation_form($node->nid, $vote, $title)) {
+            $node->body .= drupal_get_form('nmoderation_moderation_form', $frm);
           }
         }
         break;
   }
 }
-
-?>
