Index: advpoll-form.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/advpoll-form.js,v
retrieving revision 1.1.2.3
diff -u -p -r1.1.2.3 advpoll-form.js
--- advpoll-form.js	30 Jul 2007 20:36:39 -0000	1.1.2.3
+++ advpoll-form.js	1 Sep 2007 19:50:49 -0000
@@ -65,6 +65,19 @@ Drupal.advpoll.updateStartDate = functio
   }
 }
 
+// Update maxChoices and show/hide "display write-ins" option when user checks/
+// unchecks the write-ins box.
+Drupal.advpoll.updateWriteins = function() {
+  if ($("#edit-settings-writeins").attr("checked")) {
+    $(".edit-settings-show-writeins").show();
+    $("#edit-settings-show-writeins").removeAttr("disabled");
+  }
+  else {
+    $(".edit-settings-show-writeins").hide();
+    $("#edit-settings-show-writeins").attr("disabled", "disabled");
+  }
+}
+
 Drupal.advpoll.nodeFormAutoAttach = function() {
   // Hide "need more choices" checkbox
   $("#morechoices").hide();
@@ -73,6 +86,10 @@ Drupal.advpoll.nodeFormAutoAttach = func
   Drupal.advpoll.updateStartDate();
   $("#edit-settings-usestart").click(Drupal.advpoll.updateStartDate);
   
+  // Add behavior when write-in box is (un)checked.
+  Drupal.advpoll.updateWriteins();
+  $("#edit-settings-writeins").click(Drupal.advpoll.updateWriteins);
+  
   // Insert Remove links
   $('<a class="remove-choice">' + Drupal.settings.advPoll.remove + '</a>').insertAfter("input.choices");
   Drupal.advpoll.removeChoiceClick();
Index: advpoll-vote.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/advpoll-vote.js,v
retrieving revision 1.1.2.7
diff -u -p -r1.1.2.7 advpoll-vote.js
--- advpoll-vote.js	1 Dec 2006 15:40:49 -0000	1.1.2.7
+++ advpoll-vote.js	1 Sep 2007 19:50:49 -0000
@@ -52,8 +52,25 @@ Drupal.advpoll.nodeVoteAutoAttach = func
   Drupal.advpoll.attachVoteAjax();
 }
 
+Drupal.advpoll.handleWriteins = function() {
+  // Toggle display of the write-in text box for radios/checkboxes.
+  $("#vote_choices input").click(function() {
+    var isLast = $(this).val() == $("#vote_choices input:last").val();
+    var type = $(this).attr("type"); 
+    // The logic here is tricky but intentional.
+    if (isLast || type == "radio") {
+      $("#writein-choice")[(isLast && (type == "radio" || $(this).attr("checked"))) ? "show" : "hide"]();
+    }
+  });
+  // Toggle display of the write-in text box for select boxes.
+  $("#vote_choices select:last").click(function() {
+    $("#writein-choice")[$(this).val() > 0 ? "show" : "hide"]();
+  });
+};
+
 if (Drupal.jsEnabled) {
   $(document).ready(function(){  
     Drupal.advpoll.nodeVoteAutoAttach();
+    Drupal.advpoll.handleWriteins();
   });
 };
Index: advpoll.css
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/advpoll.css,v
retrieving revision 1.2.2.3
diff -u -p -r1.2.2.3 advpoll.css
--- advpoll.css	30 Jul 2007 20:36:39 -0000	1.2.2.3
+++ advpoll.css	1 Sep 2007 19:50:49 -0000
@@ -10,3 +10,7 @@ a.remove-choice {
   font-size: 0.85em;
   cursor:pointer;
 }
+
+html.js #writein-choice {
+  display: none;
+}
Index: advpoll.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/advpoll.install,v
retrieving revision 1.5.2.11
diff -u -p -r1.5.2.11 advpoll.install
--- advpoll.install	12 Aug 2007 03:19:21 -0000	1.5.2.11
+++ advpoll.install	1 Sep 2007 19:50:49 -0000
@@ -20,6 +20,8 @@ function advpoll_install() {
         algorithm VARCHAR(100),
         showvotes tinyint,
         startdate int unsigned,
+        writeins tinyint NOT NULL default '0',
+        show_writeins tinyint NOT NULL default '0',
         PRIMARY KEY (nid)
       ) /*!40100 DEFAULT CHARACTER SET utf8 */");
 
@@ -33,6 +35,7 @@ function advpoll_install() {
         nid int(10) NOT NULL,
         label text NOT NULL,
         vote_offset int(2) unsigned default NULL,
+        writein tinyint NOT NULL default '0',
         PRIMARY KEY (nid, vote_offset),
         KEY vote_offset (vote_offset)
       ) /*!40100 DEFAULT CHARACTER SET utf8 */");
@@ -50,6 +53,8 @@ function advpoll_install() {
         algorithm varchar(100),
         showvotes smallint,
         startdate integer,
+        writeins smallint NOT NULL DEFAULT '0',
+        show_writeins smallint NOT NULL DEFAULT '0',
         PRIMARY KEY (nid)
       )");
 
@@ -63,6 +68,7 @@ function advpoll_install() {
         nid integer NOT NULL,
         label text NOT NULL,
         vote_offset smallint DEFAULT NULL,
+        writein smallint NOT NULL DEFAULT '0',
         PRIMARY KEY (nid, vote_offset)
       )");
       db_query("CREATE INDEX {advpoll_choices}_vote_offset_idx ON {advpoll_choices} (vote_offset)");
@@ -143,3 +149,24 @@ function advpoll_update_2() {
   }
   return $ret;
 }
+
+/**
+ * Add columns for write-in support.
+ */
+function advpoll_update_3() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {advpoll} ADD writeins TINYINT NOT NULL DEFAULT '0'");
+      $ret[] = update_sql("ALTER TABLE {advpoll} ADD show_writeins TINYINT NOT NULL DEFAULT '0'");
+      $ret[] = update_sql("ALTER TABLE {advpoll_choices} ADD writein TINYINT NOT NULL DEFAULT '0'");
+      break;
+    case 'pgsql':
+      db_add_column($ret, 'advpoll', 'writeins', 'smallint', array('default' => 0, 'not null' => TRUE));
+      db_add_column($ret, 'advpoll', 'show_writeins', 'smallint', array('default' => 0, 'not null' => TRUE));
+      db_add_column($ret, 'advpoll_choices', 'writein', 'smallint', array('default' => 0, 'not null' => TRUE));
+      break;    
+    }
+    return $ret;
+}
Index: advpoll.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/advpoll.module,v
retrieving revision 1.21.2.57
diff -u -p -r1.21.2.57 advpoll.module
--- advpoll.module	27 Aug 2007 07:45:25 -0000	1.21.2.57
+++ advpoll.module	1 Sep 2007 19:50:49 -0000
@@ -234,6 +234,22 @@ function advpoll_form($node, $form_value
     '#tree' => TRUE,
   );
 
+  $form['settings']['writeins'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Allow user to cast a write-in vote'),
+    '#default_value' => isset($node->writeins)? $node->writeins : 0,
+    '#description' => t("Allow voters with the 'add write-ins' permission to write-in up to one choice each."),
+  );
+
+  $form['settings']['show_writeins'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Display write-in votes as choices for future voters'),
+    '#default_value' => isset($node->show_writeins)? $node->show_writeins : 0,
+    '#description' => t("Allow voters to see and choose from previous voters' write-in votes."),
+    '#prefix' => '<div class="edit-settings-show-writeins">',
+    '#suffix' => '</div>',
+  );
+
   $max_choice_list = array();
   for ($i = 0; $i <= $choices; $i++) {
     $max_choice_list[$i] = ($i == 0? 'No limit' : $i);
@@ -422,7 +438,7 @@ function advpoll_help($section) {
 function advpoll_load($node) {
   global $user;
   $poll = db_fetch_object(db_query("SELECT * FROM {advpoll} WHERE nid = %d", $node->nid));
-  $result = db_query("SELECT vote_offset, label FROM {advpoll_choices} WHERE nid = %d ORDER BY vote_offset", $node->nid);
+  $result = db_query("SELECT vote_offset, label, writein FROM {advpoll_choices} WHERE nid = %d ORDER BY vote_offset", $node->nid);
   while ($choice = db_fetch_array($result)) {
     $poll->choice[$choice['vote_offset']] = $choice;
   }
@@ -838,7 +854,7 @@ function advpoll_node_info() {
  * Implementation of hook_perm().
  */
 function advpoll_perm() {
-  return array('create polls', 'delete polls', 'view polls', 'vote on polls', 'cancel own vote', 'administer polls', 'inspect all votes');
+  return array('create polls', 'delete polls', 'view polls', 'vote on polls', 'cancel own vote', 'administer polls', 'inspect all votes', 'add write-ins');
 }
 
 /**
@@ -860,7 +876,7 @@ function advpoll_update($node) {
     $node->settings['active'] = _advpoll_calculate_active($node);
   }
 
-  db_query("UPDATE {advpoll} SET active=%d, runtime=%d, maxchoices=%d, algorithm='%s', uselist=%d, showvotes=%d, startdate='%s' WHERE nid = %d", $node->settings['active'], $node->settings['runtime'], $node->settings['maxchoices'], $node->settings['algorithm'], $node->settings['uselist'], $node->settings['showvotes'], $node->settings['usestart'] ? _advpoll_create_startdate($node) : 'NULL', $node->nid);
+  db_query("UPDATE {advpoll} SET active=%d, runtime=%d, maxchoices=%d, algorithm='%s', uselist=%d, showvotes=%d, startdate='%s', writeins=%d, show_writeins=%d WHERE nid = %d", $node->settings['active'], $node->settings['runtime'], $node->settings['maxchoices'], $node->settings['algorithm'], $node->settings['uselist'], $node->settings['showvotes'], $node->settings['usestart'] ? _advpoll_create_startdate($node) : 'NULL', $node->settings['writeins'], $node->settings['show_writeins'], $node->nid);
 
   _advpoll_insert_choices($node->nid);
   votingapi_recalculate_results('advpoll', $node->nid);
@@ -903,7 +919,7 @@ function _advpoll_insert_choices($nid) {
   $i = 1;
   foreach ($_POST['choice'] as $choice) {
     if ($choice['label'] != '') {
-      db_query("INSERT INTO {advpoll_choices} (nid, label, vote_offset) VALUES (%d, '%s', %d)", $nid, $choice['label'], $i++);
+      db_query("INSERT INTO {advpoll_choices} (nid, label, vote_offset, writein) VALUES (%d, '%s', %d, 0)", $nid, $choice['label'], $i++);
     }
   }
 }
@@ -930,7 +946,7 @@ function advpoll_insert($node) {
     $node->settings['active'] = _advpoll_calculate_active($node);
   }
 
-  db_query("INSERT INTO {advpoll} (nid, mode, uselist, active, runtime, maxchoices, algorithm, showvotes, startdate) VALUES (%d, '%s', %d, %d, %d, %d, '%s', %d, '%s')", $node->nid, $mode, $node->settings['uselist'], $node->settings['active'], $node->settings['runtime'], $node->settings['maxchoices'], $node->settings['algorithm'], $node->settings['showvotes'], $node->settings['usestart'] ? _advpoll_create_startdate($node) : 'NULL');
+  db_query("INSERT INTO {advpoll} (nid, mode, uselist, active, runtime, maxchoices, algorithm, showvotes, startdate, writeins, show_writeins) VALUES (%d, '%s', %d, %d, %d, %d, '%s', %d, '%s', %d, %d)", $node->nid, $mode, $node->settings['uselist'], $node->settings['active'], $node->settings['runtime'], $node->settings['maxchoices'], $node->settings['algorithm'], $node->settings['showvotes'], $node->settings['usestart'] ? _advpoll_create_startdate($node) : 'NULL', $node->settings['writeins'], $node->settings['show_writeins']);
 
   // Insert the choices
   _advpoll_insert_choices($node->nid);
@@ -1024,6 +1040,7 @@ function advpoll_submit(&$node) {
  * Implementation of hook_view().
  */
 function advpoll_view($node, $teaser = FALSE, $page = FALSE) {
+  drupal_add_css(drupal_get_path('module', 'advpoll') .'/advpoll.css', 'module');
   if ($node->in_preview) {
     // Previewing a node, so display voting form instead of results.
     $mode = _advpoll_get_mode($node->type);
@@ -1069,7 +1086,7 @@ function theme_advpoll_results($title, $
   $output = '<div class="poll">';
   if ($results) {
     $output .= $results;
-    $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>';
+    $output .= '<div class="total">'. t('Total voters: %votes', array('%votes' => $votes)) .'</div>';
   }
   else {
     $output .= '<p>'. t('No votes have been recorded for this poll.') .'</p>';
@@ -1111,6 +1128,26 @@ function _advpoll_vote_response($node, $
   $msg = t('Your vote was registered.');
   // Ajax response
   if ($form_values['ajax']) {
+    // Unset the array of choices so duplicates aren't shown.
+    unset($node->choice);
+    // Get all choices from database. This is necessary to get information about
+    // newly submitted write-in choices.
+    $result = db_query("SELECT vote_offset, label, writein FROM {advpoll_choices} WHERE nid = %d ORDER BY vote_offset", $node->nid);
+    while ($choice = db_fetch_array($result)) {
+      $node->choice[$choice['vote_offset']] = $choice;
+    }
+    // Update the number of choices.
+    $node->choices = count($poll->choice);
+    // Get updated total number of votes from database.
+    $result = db_query("SELECT value FROM {votingapi_cache} where content_type='advpoll' AND content_id=%d AND tag='_advpoll' AND function='total_votes'", $node->nid);
+    if (db_num_rows($result) > 0) {
+      $cache = db_fetch_object($result);
+      $node->votes = $cache->value;
+    }
+    else {
+      $node->votes = 0;
+    }
+
     list($node->voted, $node->cancel_vote) = _advpoll_user_voted($node->nid);
     $ajax_output .= advpoll_view_results($node, NULL, NULL);
     // Remove linebreaks as they will break jQuery's insert-HTML methods
@@ -1161,13 +1198,19 @@ function advpoll_cancel($nid) {
         db_query("DELETE FROM {votingapi_vote} WHERE content_id=%d and hostname = '%s' AND uid=0", $node->nid, $host);
         votingapi_recalculate_results('advpoll', $nid);
       }
+
+      $mode = _advpoll_get_mode($node->type);
+      $function = 'advpoll_cancel_'. $mode;
+      if (function_exists($function)) {
+        $function($node, $user_vote);
+      }
       drupal_set_message(t('Your vote was canceled.'));
     }
     else {
       drupal_set_message(t("You are not allowed to cancel an invalid choice."), 'error');
     }
     drupal_goto('node/'. $nid);
-   }  
+  }  
   else {
     drupal_not_found();
   }
@@ -1245,3 +1288,87 @@ function _advpoll_form_set_error($name =
     return form_set_error('choice[', $message);
   }
 }
+
+/**
+ * Voting form validation logic specific to writeins. This has been abstracted 
+ * away from includes in the modes directory.
+ */
+function _advpoll_writeins_voting_form_validate($node, $writein_option, $writein_text, $ajax) {
+  // Do write-in specific checks if write-ins are enabled and user has permission.
+  if ($node->writeins && user_access('add write-ins')) {
+    // Something is in the write-in textbox.
+    if ($writein_text) {
+      $writein_choice_lower = strtolower($writein_text);
+      foreach ($node->choice as $i => $value) {
+        // Check that user isn't writing in an existing visible choice. (User is
+        // writing in an existing choice and either write-ins are all being 
+        // displayed or the existing choice is not a write-in).
+        if ((strtolower($val['label']) == $writein_choice_lower) && ($node->show_writeins || !$value['writein'])) {
+          _advpoll_form_set_error('writein_choice', t("A write-in vote can not be for an existing choice. Select the choice's option instead."), $ajax);
+        }
+      }
+    }
+
+    // The write-in option is selected and there is nothing in the write-in textbox.
+    if ($writein_option && !$writein_text) {
+      _advpoll_form_set_error('writein_choice', t("If the 'write-in' option is selected, a choice must be written in."), $ajax);
+    }
+
+    // The write-in option is not selected, but there is something in the write-in textbox.
+    if (!$writein_option && $writein_text) {
+      _advpoll_form_set_error('writein_choice', t("If a choice is written in, the 'write-in' option must be selected."), $ajax);
+    }
+  }
+}
+
+/**
+ * Voting form submission logic specific to writeins. This has been abstracted 
+ * away from includes in the modes directory.
+ */
+function _advpoll_writeins_voting_form_submit($node, $form_values, &$vote, $vote_value) {
+  // A write-in vote is being made.
+  if ($form_values['writein_choice']) {
+    // Check if someone has previously voted for this choice.
+    $result = db_query("SELECT vote_offset FROM {advpoll_choices} WHERE nid = %d AND LOWER(label) = LOWER('%s')", $node->nid, $form_values['writein_choice']);
+    // If there's more than one match, redo the query, being more exact.
+    if (db_num_rows($result) > 1) {
+      $result = db_query("SELECT vote_offset FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $node->nid, $form_values['writein_choice']);      
+    }
+    // If there is at least one match, add a vote for the first one returned. It
+	  // should be rare to find more than one choice for any one node with a given
+	  // label.
+    if (db_num_rows($result)) {
+      $obj = db_fetch_object($result);
+      $existing_vote_offset = $obj->vote_offset;
+      // Set a vote
+      unset($temp);
+      $temp->value = $vote_value;
+      $temp->tag = $existing_vote_offset;
+      $temp->value_type = 'option';
+      $vote[] = $temp;
+    }
+    // This write-in choice has not been previously voted for.
+    else {
+      // Get last vote offset for this node.
+      $result = db_query("SELECT MAX(vote_offset) AS last_vote_offset FROM {advpoll_choices} WHERE nid = %d", $node->nid);
+      $obj = db_fetch_object($result);
+      // Set the last tag value
+      $last_vote_offset = $obj->last_vote_offset;
+      // Default value
+      if (!$last_vote_offset) {
+        // Start at one rather than 0 due to Drupal FormAPI.
+        $last_vote_offset = 1;
+      }
+
+      // Insert new choice into node.
+      db_query("INSERT INTO {advpoll_choices} (nid, label, vote_offset, writein) VALUES (%d, '%s', %d, 1)", $node->nid, check_plain($form_values['writein_choice']), $last_vote_offset + 1);
+      
+      // Add vote
+      unset($temp);
+      $temp->value = $vote_value;
+      $temp->tag = $last_vote_offset + 1;
+      $temp->value_type = 'option';
+      $vote[] = $temp;
+    }
+  }
+}
Index: modes/binary.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/modes/binary.inc,v
retrieving revision 1.7.2.23
diff -u -p -r1.7.2.23 binary.inc
--- modes/binary.inc	6 Aug 2007 08:47:19 -0000	1.7.2.23
+++ modes/binary.inc	1 Sep 2007 19:50:49 -0000
@@ -33,13 +33,24 @@ function advpoll_voting_binary_form(&$no
     // If previewing check the format against the current users permissions.
     $check = $node->in_preview;
     foreach ($node->choice as $i => $choice) {
-      // Don't show blank choices
-      if ($choice['label']) {
+      // Don't show blank choices or write-in votes if the setting is disabled.
+      if ($choice['label'] && ($node->show_writeins || !$choice['writein'])) {
          $list[$i] = _advpoll_choice_markup($choice['label'], $node->format, $check);
       }
     }
+    // Add write-in checkbox/radio if write-ins are enabled and user has permission.
+    if ($node->writeins && user_access('add write-ins')) {
+      $list[$i + 1] = t('(write-in)');
+      $form['writein_key'] = array(
+        '#type' => 'value',
+        '#value' => $i + 1,
+      );
+    }
+
     $form['choice'] = array(
       '#options' => $list,
+      '#prefix' => '<div id="vote_choices">',
+      '#suffix' => '</div>',
     );
 
     if ($node->in_preview) {
@@ -59,6 +70,17 @@ function advpoll_voting_binary_form(&$no
     }
   }
 
+  // Add write-in text field if write-ins are enabled and user has permission.
+  if ($node->writeins && user_access('add write-ins')) {
+    $form['writein_choice'] = array (
+      '#prefix' => '<div id="writein-choice">',
+      '#suffix' => '</div>',
+      '#type' => 'textfield',
+      '#title' => t('Write-in vote'),
+      '#size' => 25,
+    );
+  }
+
   $form['nid'] = array(
     '#type' => 'hidden',
     '#value' => $node->nid,
@@ -150,23 +172,33 @@ function advpoll_calculate_results_binar
 function advpoll_voting_binary_form_submit($form_id, $form_values) {
   $vote = array();
   $node = node_load($form_values['nid']);
+
+  // Do submission specific to writeins.
+  _advpoll_writeins_voting_form_submit($node, $form_values, $vote, 1);
+
   if ($node->maxchoices == 1) {
     // Plurality voting
-    $temp->value = 1;
-    $temp->tag = $form_values['choice'];
-    $temp->value_type = 'option';
-    $vote[] = $temp;
+    // Ignore write-in choice that has already been taken care of.
+    if (!$form_values['choice'][$form_values['writein_key']]) {
+      $temp->value = 1;
+      $temp->tag = $form_values['choice'];
+      $temp->value_type = 'option';
+      $vote[] = $temp;
+    }
   }
   else {
     // Approval voting
     foreach ($form_values['choice'] as $choice => $selected) {
-      unset($temp);
-      $temp->value = $choice;
-      if ($selected) {
-        $temp->value_type = 'option';
-        $temp->tag = $choice;
-        $temp->value = 1;
-        $vote[] = $temp;
+      // Ignore write-in choice that has already been taken care of.
+      if ($choice != $form_values['writein_key']) {
+        unset($temp);
+        $temp->value = $choice;
+        if ($selected) {
+          $temp->value_type = 'option';
+          $temp->tag = $choice;
+          $temp->value = 1;
+          $vote[] = $temp;
+        }
       }
     }
   }
@@ -189,6 +221,11 @@ function advpoll_voting_binary_form_vali
     _advpoll_form_set_error('choice[', t('You are not allowed to vote in this poll.'), $ajax);
   }
 
+  // Whether the write-in option is selected. This is calculated differently for
+  // radio buttons and checkboxes.
+  $writein_option = false;
+  $writein_text = $form_values['writein_key'] ? $form_values['writein_choice'] : '';
+
   // Check if user has already voted
   list($voted, $cancel_vote) = _advpoll_user_voted($node->nid);
   if ($voted) {
@@ -197,7 +234,14 @@ function advpoll_voting_binary_form_vali
 
   if ($node->maxchoices == 1) {
     // Plurality voting
-    if (!array_key_exists($form_values['choice'], $node->choice)) {
+    // Write-ins are enabled, user has permission, and it's the write-in option.
+    if ($node->writeins && user_access('add write-ins') && ($form_values['choice'] == $form_values['writein_key'])) {
+      // Set the flag to true for additional checks.
+      $writein_option = true;
+    }
+    // The choice is invalid (not between 0 and the write-in key).
+    elseif (!($form_values['choice'] > 0 ) && ($form_values['choice'] < $form_values['writein_key'])) {
+      // Nothing is selected.
       _advpoll_form_set_error('choice[', t('At least one choice must be selected.'), $ajax);
     }
   }
@@ -210,6 +254,14 @@ function advpoll_voting_binary_form_vali
         $numchoices++;
       }
     }
+
+    // Write-ins are enabled, user has permission, and the write-in box is checked.
+    if ($node->writeins && user_access('add write-ins') && $form_values['choice'][$form_values['writein_key']]) {
+      // Add one to number of choices for check on min/max boxes checked.
+      $numchoices++;
+      // Set the flag to true for additional checks.
+      $writein_option = true;
+    }
   
     // Too many choices ranked
     if ($node->maxchoices != 0 && $numchoices > $node->maxchoices) {
@@ -235,3 +287,27 @@ function theme_advpoll_voting_binary_for
   $output .= "</div>\n";
   return $output;
 }
+
+/**
+ * Hook to handle a cancelled vote for a binary poll.
+ */
+function advpoll_cancel_binary($node, $user_vote) {
+  // Remove choice if this was the last vote for a write-in.
+  if ($node->writeins) {
+    $recalculate = FALSE;
+    foreach ($user_vote as $vote) {
+      if ($node->choice[$vote->tag]['writein']) {
+        // Check if there are any other votes for this write-in.
+        $count = db_result(db_query('SELECT COUNT(1) FROM {votingapi_vote} WHERE content_id = %d AND tag = %d', $node->nid, $vote->tag));
+        if ($count == 0) {
+          // Delete the write-in because no one else voted for it.
+          db_query('DELETE FROM {advpoll_choices} WHERE vote_offset = %d AND nid = %d', $vote->tag, $node->nid);
+          $recalculate = TRUE;
+        }
+      }
+    }
+    if ($recalculate) {
+      votingapi_recalculate_results('advpoll', $nid);
+    }
+  }
+}
Index: modes/ranking.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/modes/ranking.inc,v
retrieving revision 1.8.2.28
diff -u -p -r1.8.2.28 ranking.inc
--- modes/ranking.inc	10 Aug 2007 06:38:06 -0000	1.8.2.28
+++ modes/ranking.inc	1 Sep 2007 19:50:49 -0000
@@ -64,11 +64,13 @@ function advpoll_voting_ranking_form(&$n
       '#tree' => TRUE,
       // XXX: Workaround for FormAPI bug in PHP 4, see http://drupal.org/node/86657
       '#type' => 'checkboxes',
+      '#prefix' => '<div id="vote_choices">',
+      '#suffix' => '</div>',
     );
 
     foreach ($node->choice as $key => $choice) {
-      // Don't show blank choices
-      if ($choice['label']) {
+      // Don't show blank choices or write-in votes if the setting is disabled.
+      if ($choice['label'] && ($node->show_writeins || !$choice['writein'])) {
         $form['choice'][$key] = array(
           '#type' => 'select',
           '#title' => _advpoll_choice_markup($choice['label'], $node->format),
@@ -76,6 +78,31 @@ function advpoll_voting_ranking_form(&$n
         );
       }
     }
+
+    // Add write-in select box if write-ins are enabled and user has permission.
+    if ($node->writeins && user_access('add write-ins')) {
+      $form['choice'][$key + 1] = array (
+        '#type' => 'select',
+        '#title' => t('(write-in)'),
+        '#options' => $choices,
+      );
+      // Key index of the write-in option
+      $form['writein_key'] = array(
+        '#type' => 'value',
+        '#value' => $key + 1,
+      );
+    }
+  }
+
+  // Add write-in text field if write-ins are enabled and user has permission.
+  if ($node->writeins && user_access('add write-ins')) {
+    $form['writein_choice'] = array (
+      '#prefix' => '<div id="writein-choice">',
+      '#suffix' => '</div>',
+      '#type' => 'textfield',
+      '#title' => t('Write-in vote'),
+      '#size' => 25,
+    );
   }
 
   $form['nid'] = array(
@@ -600,19 +627,26 @@ function _advpoll_calculate_instantrunof
  */
 function advpoll_voting_ranking_form_submit($form_id, $form_values) {
   $vote = array();
+  $node = node_load($form_values['nid']);
+
+  // Do submission specific to writeins.
+  _advpoll_writeins_voting_form_submit($node, $form_values, $vote, $form_values['choice'][$form_values['writein_key']]);
+
   foreach ($form_values['choice'] as $choice => $rank) {
-    unset($temp);
-    $temp->value = $rank;
-    // A zero value indicates they didn't rank that choice
-    if ($temp->value != 0) {
-      $temp->value_type = 'option';
-      $temp->tag = $choice;
-      $vote[] = $temp;
+    // Ignore write-in choice that has already been taken care of.
+    if ($choice != $form_values['writein_key']) {
+      unset($temp);
+      $temp->value = $rank;
+      // A zero value indicates they didn't rank that choice.
+      if ($temp->value != 0) {
+        $temp->value_type = 'option';
+        $temp->tag = $choice;
+        $vote[] = $temp;
+      }
     }
   }
 
   votingapi_set_vote('advpoll', $form_values['nid'], $vote);
-  $node = node_load($form_values['nid']);
   _advpoll_vote_response($node, $form_values);
 }
 
@@ -633,6 +667,10 @@ function advpoll_voting_ranking_form_val
     _advpoll_form_set_error('choice[', t('You are not allowed to vote in this poll.'), $ajax);
   }
 
+  // Whether the write-in option is selected.
+  $writein_option = false;
+  $writein_text = $form_values['writein_key'] ? $form_values['writein_choice'] : '';
+
   // Check if user has already voted
   list($voted, $cancel_vote) = _advpoll_user_voted($node->nid);
   if ($voted) {
@@ -643,9 +681,18 @@ function advpoll_voting_ranking_form_val
   $setvalues = array();
   
   $numchoices = 0;
+
+  // Write-ins are enabled, user has permission, and the write-in box is checked.
+  if ($node->writeins && user_access('add write-ins') && $form_values['choice'][$form_values['writein_key']]) {
+    // Increment the choices counter by one.
+    $numchoices++;
+    // Set the flag to true for additional checks.
+    $writein_option = TRUE;
+  }
+
   foreach ($node->choice as $key => $choice) {
     
-    // Count the number of choices that are ranked
+    // Count the number of choices that are ranked.
     if ($form_values['choice'][$key]) {
       $numchoices++;
     }
@@ -655,11 +702,32 @@ function advpoll_voting_ranking_form_val
     // Check range
     if ($intvalue > count($node->choice) || $intvalue < 0) {
       // TODO: clean up this error message
-      $message = "Illegal rank for choice $key: $intvalue (min: 1, max: ". count($node->choice) .')';
+      $message = "Illegal rank for choice $key: $intvalue (min: 1, max: "
+        . ($writein_option ? count($node->choice) + 1 : count($node->choice)) .')';
       _advpoll_form_set_error('choice]['. $key, $message, $ajax);
     }
   }
-  
+
+  // Write-ins are enabled, user has permission, and the write-in box is checked.
+  if ($writein_option) {
+    $intvalue = intval($form_values['choice'][$form_values['writein_key']]);
+    // Mark this value as seen
+    $setvalues[$intvalue]++;
+    // Check range
+    if ($intvalue > count($node->choice) || $intvalue < 0) {
+      // TODO: clean up this error message
+      $msg = "Illegal rank for the write-in choice: $intvalue (min: 1, max: "
+        . count($node->choice) . ')';
+      if ($ajax) {
+        $errors[] = $msg;
+      }
+      else {
+        form_set_error('choice][', $msg);
+      }
+      $ok = FALSE;
+    }
+  }
+
   // Too many choices ranked
   if ($node->maxchoices != 0 && $numchoices > $node->maxchoices) {
     $message = t('%num choices were selected but only %max are allowed.', array('%num' => $numchoices, '%max' => $node->maxchoices));
@@ -672,13 +740,17 @@ function advpoll_voting_ranking_form_val
     _advpoll_form_set_error('choice', t('At least one choice must be selected.'), $ajax);
   }
 
-  // Check that multiple choices are not set to the same value
+  // Check that multiple choices are not set to the same value.
   foreach ($setvalues as $val => $count) {
     if ($val != 0 && $count > 1) {
       $message = t('Multiple choices given the rank of %value.', array('%value' => $val));
       _advpoll_form_set_error('choice', $message, $ajax);
     }
   }
+
+  // Do validation specific to writeins.
+  _advpoll_writeins_voting_form_validate($node, $writein_option, $writein_text, $ajax);
+
 }
 
 /**
