function poll_update

7.x poll.module poll_update($node)
6.x poll.module poll_update($node)

Implementation of hook_update().

File

drupal-6.x/modules/poll/poll.module, line 442
Enables your site to capture votes on different topics in the form of multiple choice questions.

Code

function poll_update($node) {
  // Update poll settings.
  db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);

  // Clean poll choices.
  db_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid);

  // Poll choices come in the same order with the same numbers as they are in
  // the database, but some might have an empty title, which signifies that
  // they should be removed. We remove all votes to the removed options, so
  // people who voted on them can vote again.
  $new_chorder = 0;
  foreach ($node->choice as $old_chorder => $choice) {
    $chvotes = isset($choice['chvotes']) ? (int) $choice['chvotes'] : 0;
    $chtext = $choice['chtext'];

    if (!empty($chtext)) {
      db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $new_chorder);
      if ($new_chorder != $old_chorder) {
        // We can only remove items in the middle, not add, so
        // new_chorder is always <= old_chorder, making this safe.
        db_query("UPDATE {poll_votes} SET chorder = %d WHERE nid = %d AND chorder = %d", $new_chorder, $node->nid, $old_chorder);
      }
      $new_chorder++;
    }
    else {
      db_query("DELETE FROM {poll_votes} WHERE nid = %d AND chorder = %d", $node->nid, $old_chorder);
    }
  }
}