View source
<?php
function advpoll_install() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("CREATE TABLE {advpoll} (\n nid int NOT NULL,\n quorum int NOT NULL default '0',\n mode varchar(32) NOT NULL,\n use_list tinyint default '0',\n active tinyint default '1',\n max_choices int unsigned NOT NULL default '0',\n algorithm VARCHAR(100),\n show_votes tinyint NOT NULL default '1',\n start_date int NOT NULL default '0',\n end_date int NOT NULL default '0',\n writeins tinyint NOT NULL default '0',\n show_writeins tinyint NOT NULL default '0',\n question varchar(255) NOT NULL default '',\n PRIMARY KEY (nid)\n ) /*!40100 DEFAULT CHARACTER SET utf8 */");
db_query("CREATE TABLE {advpoll_electoral_list} (\n nid int(10) NOT NULL,\n uid int(10) NOT NULL,\n PRIMARY KEY (nid, uid)\n ) /*!40100 DEFAULT CHARACTER SET utf8 */");
db_query("CREATE TABLE {advpoll_choices} (\n cid int unsigned NOT NULL auto_increment,\n nid int(10) NOT NULL,\n label text NOT NULL,\n weight int unsigned NOT NULL,\n writein tinyint NOT NULL default '0',\n PRIMARY KEY (cid),\n KEY nid_(nid, weight)\n ) /*!40100 DEFAULT CHARACTER SET utf8 */");
break;
case 'pgsql':
db_query("CREATE TABLE {advpoll} (\n nid integer NOT NULL,\n quorum integer NOT NULL default '0',\n mode varchar(32) NOT NULL,\n use_list smallint default '0',\n active smallint default '1',\n max_choices integer NOT NULL default '0',\n algorithm varchar(100),\n show_votes smallint NOT NULL default '1',\n start_date integer NOT NULL default '0',\n end_date integer NOT NULL default '0',\n writeins smallint NOT NULL default '0',\n show_writeins smallint NOT NULL default '0',\n question varchar(255) NOT NULL default '',\n PRIMARY KEY (nid)\n )");
db_query("CREATE TABLE {advpoll_electoral_list} ( \n nid integer NOT NULL,\n uid integer NOT NULL,\n PRIMARY KEY (nid, uid) \n )");
db_query("CREATE TABLE {advpoll_choices} (\n cid serial,\n nid integer NOT NULL,\n label text NOT NULL,\n weight smallint NOT NULL,\n writein smallint NOT NULL default '0',\n PRIMARY KEY (cid)\n )");
db_query('CREATE INDEX {advpoll_choices}_nid_weight_idx ON {advpoll_choices} (nid, weight)');
break;
}
}
function advpoll_uninstall() {
include_once './' . drupal_get_path('module', 'advpoll') . '/advpoll.module';
$variables = array(
'choices',
'max_choices',
'algorithm',
'runtime',
'writeins',
'show_writeins',
'electoral_list',
'show_votes',
'view_results',
'use_question',
);
foreach (_advpoll_list_modes() as $mode) {
node_type_delete('advpoll_' . $mode['name']);
foreach ($variables as $variable) {
variable_del('advpoll_' . $variable . '_advpoll_' . $mode['name']);
}
}
db_query("DELETE FROM {votingapi_vote} WHERE content_type = 'advpoll'");
db_query("DELETE FROM {votingapi_cache} WHERE content_type = 'advpoll'");
$result = db_query('SELECT nid FROM {advpoll}');
while ($obj = db_fetch_object($result)) {
node_delete($obj->nid);
}
db_query('DROP TABLE {advpoll}');
db_query('DROP TABLE {advpoll_electoral_list}');
db_query('DROP TABLE {advpoll_choices}');
}
function advpoll_update_1() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
db_change_column($ret, 'advpoll', 'quorum', 'quorum', 'integer', array(
'not null' => TRUE,
'default' => 0,
));
break;
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE quorum quorum int(10) NOT NULL default '0'");
break;
}
return $ret;
}
function advpoll_update_2() {
$ret = array();
$variables = array();
$variables[] = array(
'name' => 'advpoll_electoral_list',
'old_name' => 'advpoll_default_electoral_list',
);
$variables[] = array(
'name' => 'advpoll_view_results',
'old_name' => 'advpoll_view_results',
);
$types = node_get_types();
foreach ($variables as $variable) {
if (!is_null($value = variable_get($variable['old_name'], NULL))) {
foreach ($types as $type) {
if ($type->module == 'advpoll') {
variable_set($variable['name'] . '_' . $type->type, $value);
}
}
}
variable_del($variable['old_name']);
}
return $ret;
}
function advpoll_update_3() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE startdate startdate int NOT NULL default '0'");
$ret[] = update_sql("UPDATE {advpoll} SET startdate = 0 WHERE startdate IS NULL");
$ret[] = update_sql("ALTER TABLE {advpoll} ADD COLUMN enddate int NOT NULL default '0' AFTER startdate");
$ret[] = update_sql("UPDATE {advpoll} SET enddate = startdate + runtime WHERE runtime != 0");
$ret[] = update_sql("ALTER TABLE {advpoll} DROP COLUMN runtime");
break;
case 'pgsql':
db_change_column($ret, 'advpoll', 'startdate', 'startdate', 'integer', array(
'not null' => TRUE,
'default' => '0',
));
$ret[] = update_sql("UPDATE {advpoll} SET startdate = 0 WHERE startdate IS NULL");
db_add_column($ret, 'advpoll', 'enddate', 'integer', array(
'not null' => TRUE,
'default' => '0',
));
$ret[] = update_sql("UPDATE {advpoll} SET enddate = startdate + runtime WHERE runtime != 0");
db_drop_column($ret, 'advpoll', 'runtime');
break;
}
return $ret;
}
function advpoll_update_4() {
$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;
}
function advpoll_update_5() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE uselist use_list tinyint default '0'");
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE startdate start_date int NOT NULL default '0'");
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE enddate end_date int NOT NULL default '0'");
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE maxchoices max_choices int NOT NULL default '0'");
$ret[] = update_sql("ALTER TABLE {advpoll} CHANGE showvotes show_votes tinyint NOT NULL default '1'");
break;
case 'pgsql':
db_change_column($ret, 'advpoll', 'uselist', 'use_list', 'smallint', array(
'default' => '0',
));
db_change_column($ret, 'advpoll', 'startdate', 'start_date', 'integer', array(
'not null' => TRUE,
'default' => '0',
));
db_change_column($ret, 'advpoll', 'enddate', 'end_date', 'integer', array(
'not null' => TRUE,
'default' => '0',
));
db_change_column($ret, 'advpoll', 'maxchoices', 'max_choices', 'integer', array(
'not null' => TRUE,
'default' => '0',
));
db_change_column($ret, 'advpoll', 'showvotes', 'show_votes', 'smallint', array(
'not null' => TRUE,
'default' => '1',
));
break;
}
$content_types = array(
'advpoll_binary',
'advpoll_ranking',
);
$settings = array(
'maxchoices' => 'max_choices',
'showvotes' => 'show_votes',
);
foreach ($content_types as $type) {
foreach ($settings as $old => $new) {
$old_name = 'advpoll_' . $old . '_' . $type;
if (!is_null($value = variable_get($old_name, NULL))) {
variable_set('advpoll_' . $new . '_' . $type, $value);
variable_del($old_name);
}
}
}
$ret[] = update_sql("UPDATE {blocks} SET delta = 'latest_poll' WHERE delta = 'mostrecent'");
$ret[] = update_sql("UPDATE {votingapi_cache} SET function = 'view_score' WHERE function = 'viewscore' AND content_type = 'advpoll'");
$ret[] = update_sql("UPDATE {votingapi_cache} SET function = 'raw_score' WHERE function = 'rawscore' AND content_type = 'advpoll'");
return $ret;
}
function advpoll_update_6() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {advpoll} ADD question varchar(255) NOT NULL default ''");
break;
case 'pgsql':
db_add_column($ret, 'advpoll', 'question', 'varchar(255)', array(
'not null' => TRUE,
'default' => "''",
));
break;
}
return $ret;
}
function advpoll_update_7() {
$ret = array();
$ret[] = update_sql("UPDATE {node} SET type = 'advpoll_binary' WHERE type = 'advpoll-binary'");
$ret[] = update_sql("UPDATE {node} SET type = 'advpoll_ranking' WHERE type = 'advpoll-ranking'");
return $ret;
}
function advpoll_update_8() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql('ALTER TABLE {advpoll_choices} CHANGE vote_offset weight int NOT NULL');
$ret[] = update_sql('ALTER TABLE {advpoll_choices} DROP INDEX vote_offset');
$ret[] = update_sql('ALTER TABLE {advpoll_choices} DROP PRIMARY KEY');
$ret[] = update_sql('ALTER TABLE {advpoll_choices} ADD cid int unsigned NOT NULL auto_increment PRIMARY KEY FIRST');
break;
case 'pgsql':
db_change_column($ret, 'advpoll_choices', 'vote_offset', 'weight', 'smallint', array(
'not null' => TRUE,
));
db_add_column($ret, 'advpoll_choices', 'cid', 'serial', array(
'not null' => TRUE,
));
$ret[] = update_sql('ALTER TABLE {advpoll_choices} ADD PRIMARY KEY (cid)');
break;
}
$result = db_query('SELECT nid, cid, weight FROM {advpoll_choices}');
while ($choice = db_fetch_object($result)) {
db_query('UPDATE {votingapi_vote} SET tag = %d WHERE content_id = %d AND tag = %d', $choice->cid, $choice->nid, $choice->weight);
db_query('UPDATE {votingapi_cache} SET tag = %d WHERE content_id = %d AND tag = %d', $choice->cid, $choice->nid, $choice->weight);
}
return $ret;
}