/[drupal]/contributions/modules/advpoll/advpoll_convert.module
ViewVC logotype

Contents of /contributions/modules/advpoll/advpoll_convert.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sun Oct 7 22:57:17 2007 UTC (2 years, 1 month ago) by chriskennedy
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
#102139 by me: Provide upgrade path from poll.module.
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_help().
6 */
7 function advpoll_convert_help($section) {
8 switch ($section) {
9 case 'admin/modules#description':
10 return t("Converts a site's standard Drupal polls into Advanced Poll polls.");
11 break;
12 }
13 }
14
15 /**
16 * Implementation of hook_menu().
17 */
18 function advpoll_convert_menu($may_cache) {
19 global $user;
20
21 $items = array();
22
23 if ($may_cache) {
24 $items[] = array(
25 'path' => 'admin/content/advpoll-convert',
26 'title' => t('Advanced Poll Converter'),
27 'callback' => 'advpoll_convert_view',
28 'access' => user_access('administer nodes'),
29 'description' => t("Convert your site's standard Drupal polls into Advanced Poll polls."),
30 );
31 }
32 return $items;
33 }
34
35 function advpoll_convert_view() {
36 $output = '';
37 $output .= drupal_get_form('advpoll_convert_form');
38 return $output;
39 }
40
41 function advpoll_convert_form() {
42 $form = array();
43 $form['note'] = array(
44 '#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>',
45 );
46 $form['convert'] = array(
47 '#type' => 'submit',
48 '#value' => t('Convert'),
49 );
50 return $form;
51 }
52
53 function advpoll_convert_form_submit($form_id, $form_values) {
54 $count = 0;
55 $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'");
56 while ($poll = db_fetch_object($result)) {
57 _advpoll_convert_node($poll);
58 $count++;
59 }
60 drupal_set_message(format_plural($count, '@count poll successfully converted.', '@count polls successfully converted.'));
61 }
62
63 /**
64 * Convert a poll.module poll into an advpoll.module poll (binary mode).
65 *
66 * Note: this will permanently delete the old poll.module data.
67 */
68 function _advpoll_convert_node($poll) {
69 // Create the Advanced Poll entry.
70 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'));
71
72 $time = time();
73
74 // Convert the votes.
75 $votes = array();
76 $result = db_query('SELECT nid, uid, chorder, hostname FROM {poll_votes} WHERE nid = %d', $poll->nid);
77 while ($vote = db_fetch_object($result)) {
78 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 : '');
79
80 // Keep track of the number of real votes for this choice.
81 if (!isset($votes[$vote->chorder])) {
82 $votes[$vote->chorder] = 0;
83 }
84 $votes[$vote->chorder]++;
85 }
86
87 // Convert the choices.
88 $result = db_query('SELECT chtext, chorder, chvotes FROM {poll_choices} WHERE nid = %d', $poll->nid);
89 while ($choice = db_fetch_object($result)) {
90 db_query("INSERT INTO {advpoll_choices} (nid, label, vote_offset) VALUES (%d, '%s', %d)", $poll->nid, $choice->chtext, $choice->chorder);
91
92 // Create place-holder votes for any poll.module manual vote counts.
93 while ($choice->chvotes - $votes[$choice->chorder] > 0) {
94 $ip = 'X-'. $poll->nid .'-'. $choice->chorder .'-'. $choice->chvotes;
95 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, $choice->chorder, 0, $time, $ip);
96 $choice->chvotes--;
97 }
98 }
99
100 // Change the node type.
101 db_query("UPDATE {node} SET type = 'advpoll_binary' WHERE nid = %d", $poll->nid);
102
103 // Clear the old teaser.
104 db_query("UPDATE {node_revisions} SET teaser = '' WHERE nid = %d", $poll->nid);
105
106 // Recalculate the results.
107 votingapi_recalculate_results('advpoll', $poll->nid);
108
109 // Delete the old poll.module data.
110 db_query('DELETE FROM {poll} WHERE nid = %d', $poll->nid);
111 db_query('DELETE FROM {poll_choices} WHERE nid = %d', $poll->nid);
112 db_query('DELETE FROM {poll_votes} WHERE nid = %d', $poll->nid);
113 }

  ViewVC Help
Powered by ViewVC 1.1.2