You are here

function _advpoll_converter_create_nodes in Advanced Poll 7

Same name and namespace in other branches
  1. 7.3 advpoll_converter/advpoll_converter.admin.inc \_advpoll_converter_create_nodes()
  2. 7.2 advpoll_converter/advpoll_converter.admin.inc \_advpoll_converter_create_nodes()
1 call to _advpoll_converter_create_nodes()
advpoll_converter_form_submit in advpoll_converter/advpoll_converter.admin.inc

File

advpoll_converter/advpoll_converter.admin.inc, line 96

Code

function _advpoll_converter_create_nodes($nids) {
  $poll_nodes = db_query("SELECT n.nid, n.title, n.language, n.created, n.status, n.uid,\n                        p.runtime, p.active FROM {node} 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("SELECT pc.chid, pc.chtext FROM poll_choice pc 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("SELECT pv.chid, pv.uid, pv.hostname, pv.timestamp FROM poll_vote pv where pv.nid = :nid", array(
      ':nid' => $record->nid,
    ));
    $votes = array();
    foreach ($poll_votes as $row) {
      $votes = array(
        'entity_type' => 'advpoll',
        '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, $criteria = NULL);
    }
    drupal_set_message(t('A new advanced poll node was created for %title.', array(
      '%title' => $record->title,
    )));
  }
}