poll.inc

Tracking 7.x-2.x branch
  1. drupal
    1. 6 contributions/ctools/page_manager/plugins/tasks/poll.inc
    2. 7 contributions/migrate/plugins/destinations/poll.inc
    3. 7 contributions/ctools/page_manager/plugins/tasks/poll.inc

Support for poll nodes.

Each poll node will have multiple choices, and multiple votes. It's not practical to bring this information in through your migration's source query, you need to pull it separately in prepareRow():

...
$this->addFieldMapping('active')
     ->defaultValue(1);
$this->addFieldMapping('runtime', 'seconds_to_run')
$this->addFieldMapping('choice', 'src_choices')
     ->description('src_choices populated in prepareRow()');
$this->addFieldMapping('votes', 'src_votes')
     ->description('src_votes populated in prepareRow()');
...
public function prepareRow($row);
  $choices =  Database::getConnection('default', 'legacy')
              ->select('src_poll_choice', 'c')
              ->fields('c', array('choice_label', 'choice_order', 'choice_total'))
              ->condition('c.choiceid', $row->src_contentid);
              ->execute();
  $row->src_choices = array();
  foreach ($choices as $choice) {
    $row->src_choices[] = array(
      'chtext' => $choice->choice_label,
      'chvotes' => $choice->choice_total,
      'weight' => $choice->choice_order,
    );
  }
  // Note that we won't know until much later what the chid is for each
  // choice, so it's best to tie the votes to choices by text.
  $query =  Database::getConnection('default', 'legacy')
            ->select('src_poll_vote', 'v')
            ->fields('v', array('choice_uid', 'hostname', 'timestamp'))
            ->condition('v.choiceid', $row->src_contentid);
  $votes = $query->innerJoin('src_poll_choice', 'c', 'v.choice_id=c.choice_id')
           ->fields('c', array('choice_label'))
           ->execute();
  $row->src_votes = array();
  foreach ($votes as $vote) {
    $row->src_votes[] = array(
      'chtext' => $choice->choice_label,
      'uid' => $choice->choice_uid,
      'hostname' => $choice->hostname,
      'timestamp' => $choice->timestamp,
    );
  }
  return TRUE;
}

Classes

NameDescription
MigratePollEntityHandler

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Support for poll nodes.
  5. *
  6. * Each poll node will have multiple choices, and multiple votes. It's
  7. * not practical to bring this information in through your migration's
  8. * source query, you need to pull it separately in prepareRow():
  9. *
  10. * @code
  11. * ...
  12. * $this->addFieldMapping('active')
  13. * ->defaultValue(1);
  14. * $this->addFieldMapping('runtime', 'seconds_to_run')
  15. * $this->addFieldMapping('choice', 'src_choices')
  16. * ->description('src_choices populated in prepareRow()');
  17. * $this->addFieldMapping('votes', 'src_votes')
  18. * ->description('src_votes populated in prepareRow()');
  19. * ...
  20. * public function prepareRow($row);
  21. * $choices = Database::getConnection('default', 'legacy')
  22. * ->select('src_poll_choice', 'c')
  23. * ->fields('c', array('choice_label', 'choice_order', 'choice_total'))
  24. * ->condition('c.choiceid', $row->src_contentid);
  25. * ->execute();
  26. * $row->src_choices = array();
  27. * foreach ($choices as $choice) {
  28. * $row->src_choices[] = array(
  29. * 'chtext' => $choice->choice_label,
  30. * 'chvotes' => $choice->choice_total,
  31. * 'weight' => $choice->choice_order,
  32. * );
  33. * }
  34. * // Note that we won't know until much later what the chid is for each
  35. * // choice, so it's best to tie the votes to choices by text.
  36. * $query = Database::getConnection('default', 'legacy')
  37. * ->select('src_poll_vote', 'v')
  38. * ->fields('v', array('choice_uid', 'hostname', 'timestamp'))
  39. * ->condition('v.choiceid', $row->src_contentid);
  40. * $votes = $query->innerJoin('src_poll_choice', 'c', 'v.choice_id=c.choice_id')
  41. * ->fields('c', array('choice_label'))
  42. * ->execute();
  43. * $row->src_votes = array();
  44. * foreach ($votes as $vote) {
  45. * $row->src_votes[] = array(
  46. * 'chtext' => $choice->choice_label,
  47. * 'uid' => $choice->choice_uid,
  48. * 'hostname' => $choice->hostname,
  49. * 'timestamp' => $choice->timestamp,
  50. * );
  51. * }
  52. * return TRUE;
  53. * }
  54. * @endcode
  55. */
  56. class MigratePollEntityHandler extends MigrateDestinationHandler {
  57. public function __construct() {
  58. $this->registerTypes(array('node'));
  59. }
  60. public function fields($entity_type, $bundle) {
  61. if ($bundle == 'poll') {
  62. $fields = array(
  63. 'active' => t('Active status'),
  64. 'runtime' => t('How long the poll runs for in seconds'),
  65. 'choice' => t('Choices. Each choice is an array with chtext, chvotes, and weight keys.'),
  66. 'votes' => t('Votes. Each vote is an array with chid (or chtext), uid, hostname, and timestamp keys'),
  67. );
  68. }
  69. else {
  70. $fields = array();
  71. }
  72. return $fields;
  73. }
  74. public function complete($entity, stdClass $row) {
  75. if ($entity->type == 'poll') {
  76. // Update settings overridden by !user_access('administer nodes') check in
  77. // poll_insert().
  78. db_update('poll')
  79. ->fields(array('active' => $entity->active))
  80. ->condition('nid', $entity->nid)
  81. ->execute();
  82. // Update vote summary count, again overridden by
  83. // !user_access('administer nodes') check in poll_insert().
  84. foreach ($row->choice as $choice) {
  85. // Have no mapping tracking for chid, so assume choice text is unique.
  86. db_update('poll_choice')
  87. ->fields(array('chvotes' => $choice['chvotes'], 'weight' => $choice['weight']))
  88. ->condition('nid', $entity->nid)
  89. ->condition('chtext', $choice['chtext'])
  90. ->execute();
  91. }
  92. // Insert actual votes.
  93. foreach ($row->votes as $vote) {
  94. $chid = $vote['chid'];
  95. if (!isset($chid)) {
  96. $result = db_select('poll_choice', 'pc')
  97. ->fields('pc', array('chid'))
  98. ->condition('pc.nid', $entity->nid)
  99. ->condition('pc.chtext', $vote['chtext'])
  100. ->execute();
  101. $chid = $result->fetchField();
  102. }
  103. db_insert('poll_vote')
  104. ->fields(array(
  105. 'chid' => $chid,
  106. 'nid' => $entity->nid,
  107. 'uid' => $vote['uid'],
  108. 'hostname' => $vote['hostname'],
  109. 'timestamp' => $vote['timestamp'],
  110. ))
  111. ->execute();
  112. }
  113. }
  114. }
  115. }