You are here

function _advpoll_converter_create_nodes in Advanced Poll 7.3

Same name and namespace in other branches
  1. 7 advpoll_converter/advpoll_converter.admin.inc \_advpoll_converter_create_nodes()
  2. 7.2 advpoll_converter/advpoll_converter.admin.inc \_advpoll_converter_create_nodes()

Converts selected polls to Advanced Polls.

1 call to _advpoll_converter_create_nodes()
advpoll_converter_form_submit in advpoll_converter/advpoll_converter.admin.inc
Submit for administrative form.

File

advpoll_converter/advpoll_converter.admin.inc, line 98
Advanced Poll Converter Module Admin Forms and Functions.

Code

function _advpoll_converter_create_nodes($nids) {
  $poll_nodes = db_query("\n    SELECT n.nid,\n      n.title,\n      n.language,\n      n.created,\n      n.status,\n      n.uid,\n      p.runtime,\n      p.active\n    FROM {node} n\n    JOIN {poll} p ON n.nid = p.nid\n    WHERE type='poll' AND n.nid IN (:ids)", array(
    ':ids' => $nids,
  ));
  foreach ($poll_nodes as $record) {

    // Set up base Node.
    $node = new stdClass();
    $node->type = 'advpoll';
    node_object_prepare($node);
    $node->language = $record->language;
    $node->title = $record->title;
    $node->uid = $record->uid;
    $node->status = $record->status;
    $record->language = LANGUAGE_NONE;

    // Fetch poll's choices.
    $poll_choices = db_query("\n      SELECT pc.chid, pc.chtext\n      FROM {poll_choice} pc\n      WHERE pc.nid = :nid", array(
      ':nid' => $record->nid,
    ));
    $choice_by_id = array();
    $choices = array();
    foreach ($poll_choices as $choice) {
      $id = dechex(time() * rand(5, 50));
      $choice_by_id[$choice->chid] = $id;
      $choices[] = array(
        'choice' => $choice->chtext,
        'write_in' => 0,
        'choice_id' => $id,
      );
    }
    $node->advpoll_choice[$record->language] = $choices;
    $node->advpoll_dates[$record->language][0]['value'] = date('Y-m-d 00:00:00', $record->created);
    $end_time = time();
    if ($record->runtime) {
      $end_time = $record->created + $record->runtime;
    }
    else {
      $end_time = $record->created + 60 * 60 * 24 * 365;
    }
    if ($record->active) {
      $node->advpoll_closed[$record->language][0]['value'] = 'open';
    }
    else {
      $node->advpoll_closed[$record->language][0]['value'] = 'close';
    }
    $node->advpoll_dates[$record->language][0]['value2'] = date('Y-m-d 00:00:00', $end_time);
    $node->advpoll_max_choices[$record->language][0]['value'] = 1;
    $node->advpoll_options[$record->language][0]['value'] = 'showvotes';
    $node->advpoll_mode[$record->language][0]['value'] = 'normal';
    $node->advpoll_results[$record->language][0]['value'] = 'aftervote';
    $node->advpoll_behavior[$record->language][0]['value'] = 'approval';
    node_save($node);

    // Save to get nid of newly created node
    // Needed to be able to record associated votes.
    $nid = $node->nid;

    // Update associated votes.
    $poll_votes = db_query("\n      SELECT pv.chid,\n        pv.uid,\n        pv.hostname,\n        pv.timestamp\n        FROM {poll_vote} pv\n      WHERE pv.nid = :nid", array(
      ':nid' => $record->nid,
    ));
    foreach ($poll_votes as $row) {
      $votes = array(
        'entity_type' => 'node',
        'entity_id' => $nid,
        'value' => 1,
        'tag' => $choice_by_id[$row->chid],
        'uid' => $row->uid,
        'vote_source' => $row->hostname,
        'timestamp' => $row->timestamp,
      );
      votingapi_set_votes($votes);
    }
    drupal_set_message(t('A new advanced poll node was created for %title.', array(
      '%title' => $record->title,
    )));
  }
}