cms/drupal/modules/poll/poll.install
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Install, update and uninstall functions for the poll module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Implements hook_schema().
       
    10  */
       
    11 function poll_schema() {
       
    12   $schema['poll'] = array(
       
    13     'description' => 'Stores poll-specific information for poll nodes.',
       
    14     'fields' => array(
       
    15       'nid' => array(
       
    16         'type' => 'int',
       
    17         'unsigned' => TRUE,
       
    18         'not null' => TRUE,
       
    19         'default' => 0,
       
    20         'description' => "The poll's {node}.nid.",
       
    21       ),
       
    22       'runtime' => array(
       
    23         'type' => 'int',
       
    24         'not null' => TRUE,
       
    25         'default' => 0,
       
    26         'description' => 'The number of seconds past {node}.created during which the poll is open.',
       
    27       ),
       
    28       'active' => array(
       
    29         'type' => 'int',
       
    30         'unsigned' => TRUE,
       
    31         'not null' => TRUE,
       
    32         'default' => 0,
       
    33         'description' => 'Boolean indicating whether or not the poll is open.',
       
    34       ),
       
    35     ),
       
    36     'primary key' => array('nid'),
       
    37     'foreign keys' => array(
       
    38       'poll_node' => array(
       
    39         'table' => 'node',
       
    40         'columns' => array('nid' => 'nid'),
       
    41       ),
       
    42     ),
       
    43   );
       
    44 
       
    45   $schema['poll_choice'] = array(
       
    46     'description' => 'Stores information about all choices for all {poll}s.',
       
    47     'fields' => array(
       
    48       'chid' => array(
       
    49         'type' => 'serial',
       
    50         'unsigned' => TRUE,
       
    51         'not null' => TRUE,
       
    52         'description' => 'Unique identifier for a poll choice.',
       
    53       ),
       
    54       'nid' => array(
       
    55         'type' => 'int',
       
    56         'unsigned' => TRUE,
       
    57         'not null' => TRUE,
       
    58         'default' => 0,
       
    59         'description' => 'The {node}.nid this choice belongs to.',
       
    60       ),
       
    61       'chtext' => array(
       
    62         'type' => 'varchar',
       
    63         'length' => 128,
       
    64         'not null' => TRUE,
       
    65         'default' => '',
       
    66         'description' => 'The text for this choice.',
       
    67         'translatable' => TRUE,
       
    68       ),
       
    69       'chvotes' => array(
       
    70         'type' => 'int',
       
    71         'not null' => TRUE,
       
    72         'default' => 0,
       
    73         'description' => 'The total number of votes this choice has received by all users.',
       
    74       ),
       
    75       'weight' => array(
       
    76         'type' => 'int',
       
    77         'not null' => TRUE,
       
    78         'default' => 0,
       
    79         'description' => 'The sort order of this choice among all choices for the same node.',
       
    80       ),
       
    81     ),
       
    82     'indexes' => array(
       
    83       'nid' => array('nid'),
       
    84     ),
       
    85     'primary key' => array('chid'),
       
    86     'foreign keys' => array(
       
    87       'choice_node' => array(
       
    88         'table' => 'node',
       
    89         'columns' => array('nid' => 'nid'),
       
    90       ),
       
    91     ),
       
    92   );
       
    93 
       
    94   $schema['poll_vote'] = array(
       
    95     'description' => 'Stores per-{users} votes for each {poll}.',
       
    96     'fields' => array(
       
    97       'chid' => array(
       
    98         'type' => 'int',
       
    99         'unsigned' => TRUE,
       
   100         'not null' => TRUE,
       
   101         'description' => "The {users}'s vote for this poll.",
       
   102       ),
       
   103       'nid' => array(
       
   104         'type' => 'int',
       
   105         'unsigned' => TRUE,
       
   106         'not null' => TRUE,
       
   107         'description' => 'The {poll} node this vote is for.',
       
   108       ),
       
   109       'uid' => array(
       
   110         'type' => 'int',
       
   111         'unsigned' => TRUE,
       
   112         'not null' => TRUE,
       
   113         'default' => 0,
       
   114         'description' => 'The {users}.uid this vote is from unless the voter was anonymous.',
       
   115       ),
       
   116       'hostname' => array(
       
   117         'type' => 'varchar',
       
   118         'length' => 128,
       
   119         'not null' => TRUE,
       
   120         'default' => '',
       
   121         'description' => 'The IP address this vote is from unless the voter was logged in.',
       
   122       ),
       
   123       'timestamp' => array(
       
   124         'type' => 'int',
       
   125         'not null' => TRUE,
       
   126         'default' => 0,
       
   127         'description' => 'The timestamp of the vote creation.',
       
   128       ),
       
   129     ),
       
   130     'primary key' => array('nid', 'uid', 'hostname'),
       
   131     'foreign keys' => array(
       
   132       'poll_node' => array(
       
   133         'table' => 'node',
       
   134         'columns' => array('nid' => 'nid'),
       
   135       ),
       
   136       'voter' => array(
       
   137         'table' => 'users',
       
   138         'columns' => array('uid' => 'uid'),
       
   139       ),
       
   140     ),
       
   141     'indexes' => array(
       
   142       'chid'     => array('chid'),
       
   143       'hostname' => array('hostname'),
       
   144       'uid' => array('uid'),
       
   145     ),
       
   146   );
       
   147 
       
   148   return $schema;
       
   149 }
       
   150 
       
   151 /**
       
   152  * Use the poll_choice primary key to record votes in poll_votes rather than
       
   153  * the choice order. Rename chorder to weight.
       
   154  *
       
   155  * Rename {poll_choices} table to {poll_choice} and {poll_votes} to {poll_vote}.
       
   156  */
       
   157 function poll_update_7001() {
       
   158   // Add chid column and convert existing votes.
       
   159   db_add_field('poll_votes', 'chid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
       
   160   db_add_index('poll_votes', 'chid', array('chid'));
       
   161   db_update('poll_votes')
       
   162     ->expression('chid', Database::getConnection()->prefixTables('COALESCE((SELECT chid FROM {poll_choices} c WHERE {poll_votes}.chorder = c.chorder AND {poll_votes}.nid = c.nid), 0)'))
       
   163     ->execute();
       
   164   // Delete invalid votes.
       
   165   db_delete('poll_votes')->condition('chid', 0)->execute();
       
   166   // Remove old chorder column.
       
   167   db_drop_field('poll_votes', 'chorder');
       
   168 
       
   169   // Change the chorder column to weight in poll_choices.
       
   170   db_change_field('poll_choices', 'chorder', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
       
   171 
       
   172   db_rename_table('poll_votes', 'poll_vote');
       
   173   db_rename_table('poll_choices', 'poll_choice');
       
   174 }
       
   175 
       
   176 /**
       
   177  * Add timestamp field to {poll_vote}.
       
   178  */
       
   179 function poll_update_7002() {
       
   180   $field = array(
       
   181     'type' => 'int',
       
   182     'not null' => TRUE,
       
   183     'default' => 0,
       
   184   );
       
   185   db_add_field('poll_vote', 'timestamp', $field);
       
   186 }
       
   187 
       
   188 /**
       
   189  * Change the weight column to normal int.
       
   190  */
       
   191 function poll_update_7003() {
       
   192   db_change_field('poll_choice', 'weight', 'weight', array(
       
   193     'type' => 'int',
       
   194     'not null' => TRUE,
       
   195     'default' => 0,
       
   196     'description' => 'The sort order of this choice among all choices for the same node.',
       
   197   ));
       
   198 }
       
   199 
       
   200 /**
       
   201  * @addtogroup updates-7.x-extra
       
   202  * @{
       
   203  */
       
   204 
       
   205 /**
       
   206  * Update the database to match the schema.
       
   207  */
       
   208 function poll_update_7004() {
       
   209   // Remove field default.
       
   210   db_change_field('poll_vote', 'chid', 'chid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
       
   211 }
       
   212 
       
   213 /**
       
   214  * @} End of "addtogroup updates-7.x-extra".
       
   215  */