You are here

function _advpoll_convert_node in Advanced Poll 6.2

Same name and namespace in other branches
  1. 5 advpoll_convert.module \_advpoll_convert_node()
  2. 6.3 advpoll_convert.module \_advpoll_convert_node()
  3. 6 advpoll_convert.module \_advpoll_convert_node()

Convert a poll.module poll into an advpoll.module poll (binary mode).

Note: this will permanently delete the old poll.module data.

1 call to _advpoll_convert_node()
advpoll_convert_form_submit in ./advpoll_convert.module

File

./advpoll_convert.module, line 71
Converts a site's standard Drupal polls into Advanced Poll polls.

Code

function _advpoll_convert_node($poll) {

  // Create the Advanced Poll entry.
  db_query("INSERT INTO {advpoll} (nid, mode, use_list, active, max_choices, algorithm, show_votes, start_date, end_date, writeins, show_writeins, question) VALUES (%d, 'binary', 0, %d, 1, 'plurality', 1, '%s', '%s', 0, 0, '')", $poll->nid, $poll->active, $poll->created, $poll->runtime ? $poll->created + $poll->runtime : 'NULL');
  $time = time();

  // Convert the votes.
  $votes = array();
  $result = db_query('SELECT nid, uid, chorder, hostname FROM {poll_votes} WHERE nid = %d', $poll->nid);
  while ($vote = db_fetch_object($result)) {
    db_query("INSERT INTO {votingapi_vote} (content_type, content_id, value, value_type, tag, uid, timestamp, vote_source) VALUES ('%s', %d, 1, 'option', '%s', %d, %d, '%s')", 'advpoll', $poll->nid, $vote->chorder, $vote->uid, $time, $vote->hostname ? $vote->hostname : '');

    // Keep track of the number of real votes for this choice.
    if (!isset($votes[$vote->chorder])) {
      $votes[$vote->chorder] = 0;
    }
    $votes[$vote->chorder]++;
  }

  // Convert the choices.
  $result = db_query('SELECT chtext, chorder, chvotes FROM {poll_choices} WHERE nid = %d', $poll->nid);
  while ($choice = db_fetch_object($result)) {
    db_query("INSERT INTO {advpoll_choices} (nid, label, weight) VALUES (%d, '%s', %d)", $poll->nid, $choice->chtext, $choice->chorder);

    // Get newest choice id.
    $cid = db_result(db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $poll->nid, $choice->chtext));

    // Switch the vote tag from weight to cid.
    db_query('UPDATE {votingapi_vote} SET tag = %d WHERE content_id = %d AND tag = %d', $cid, $poll->nid, $choice->chorder);

    // Create place-holder votes for any poll.module manual vote counts.
    while ($choice->chvotes - $votes[$choice->chorder] > 0) {
      $ip = 'X-' . $poll->nid . '-' . $cid . '-' . $choice->chvotes;
      db_query("INSERT INTO {votingapi_vote} (content_type, content_id, value, value_type, tag, uid, timestamp, vote_source) VALUES ('%s', %d, 1, 'option', '%s', 0, %d, '%s')", 'advpoll', $poll->nid, $cid, $time, $ip);
      $choice->chvotes--;
    }
  }

  // Change the node type.
  db_query("UPDATE {node} SET type = 'advpoll_binary' WHERE nid = %d", $poll->nid);

  // Clear the old teaser.
  db_query("UPDATE {node_revisions} SET teaser = '' WHERE nid = %d", $poll->nid);

  // Recalculate the results.
  votingapi_recalculate_results('advpoll', $poll->nid);

  // Delete the old poll.module data.
  db_query('DELETE FROM {poll} WHERE nid = %d', $poll->nid);
  db_query('DELETE FROM {poll_choices} WHERE nid = %d', $poll->nid);
  db_query('DELETE FROM {poll_votes} WHERE nid = %d', $poll->nid);
}