View source
<?php
function advpoll_convert_help($section) {
switch ($section) {
case 'admin/modules#description':
return t("Converts a site's standard Drupal polls into Advanced Poll polls.");
break;
}
}
function advpoll_convert_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/content/advpoll-convert',
'title' => t('Advanced Poll Converter'),
'callback' => 'advpoll_convert_view',
'access' => user_access('administer nodes'),
'description' => t("Convert your site's standard Drupal polls into Advanced Poll polls."),
);
}
return $items;
}
function advpoll_convert_view() {
$output = '';
$output .= drupal_get_form('advpoll_convert_form');
return $output;
}
function advpoll_convert_form() {
$form = array();
$form['note'] = array(
'#value' => '<p>' . t('This will convert all standard Drupal polls into Advanced Poll polls.') . '</p><div class="messages warning">' . t('Warning: this action cannot be reversed. Please backup your database before converting.') . '</div>',
);
$form['convert'] = array(
'#type' => 'submit',
'#value' => t('Convert'),
);
return $form;
}
function advpoll_convert_form_submit($form_id, $form_values) {
$count = 0;
$result = db_query("SELECT n.nid, n.created, p.runtime, p.active FROM {node} n JOIN {poll} p ON n.nid = p.nid WHERE type = 'poll'");
while ($poll = db_fetch_object($result)) {
_advpoll_convert_node($poll);
$count++;
}
drupal_set_message(format_plural($count, '@count poll successfully converted.', '@count polls successfully converted.'));
}
function _advpoll_convert_node($poll) {
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();
$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} (vote_id, content_type, content_id, value, value_type, tag, uid, timestamp, hostname) VALUES (%d, '%s', %d, 1, 'option', '%s', %d, %d, '%s')", db_next_id('{votingapi_vote}'), 'advpoll', $poll->nid, $vote->chorder, $vote->uid, $time, $vote->hostname ? $vote->hostname : '');
if (!isset($votes[$vote->chorder])) {
$votes[$vote->chorder] = 0;
}
$votes[$vote->chorder]++;
}
$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);
$cid = db_result(db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $poll->nid, $choice->chtext));
db_query('UPDATE {votingapi_vote} SET tag = %d WHERE content_id = %d AND tag = %d', $cid, $poll->nid, $choice->chorder);
while ($choice->chvotes - $votes[$choice->chorder] > 0) {
$ip = 'X-' . $poll->nid . '-' . $cid . '-' . $choice->chvotes;
db_query("INSERT INTO {votingapi_vote} (vote_id, content_type, content_id, value, value_type, tag, uid, timestamp, hostname) VALUES (%d, '%s', %d, 1, 'option', '%s', 0, %d, '%s')", db_next_id('{votingapi_vote}'), 'advpoll', $poll->nid, $cid, $time, $ip);
$choice->chvotes--;
}
}
db_query("UPDATE {node} SET type = 'advpoll_binary' WHERE nid = %d", $poll->nid);
db_query("UPDATE {node_revisions} SET teaser = '' WHERE nid = %d", $poll->nid);
votingapi_recalculate_results('advpoll', $poll->nid);
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);
}