poll.install 5.9 KB

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