You are here

function _advpoll_insert_choices in Advanced Poll 5

Same name and namespace in other branches
  1. 6.3 advpoll.module \_advpoll_insert_choices()
  2. 6 advpoll.module \_advpoll_insert_choices()
  3. 6.2 advpoll.module \_advpoll_insert_choices()

Insert/update the choices for a poll.

Note: we pass choice data via _POST to allow for dynamic addition of choices. Drupal 6 AHAH support will let us switch to a clean implementation.

2 calls to _advpoll_insert_choices()
advpoll_insert in ./advpoll.module
Implementation of hook_insert().
advpoll_update in ./advpoll.module
Implementation of hook_update().

File

./advpoll.module, line 1176
Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.

Code

function _advpoll_insert_choices($nid, $choices) {
  $node = node_load($nid);
  $weight = 0;
  $seen_ids = array();
  foreach ($choices as $index => $choice) {
    if ($choice['label'] != '') {

      // Mark this choice id as being seen.
      $seen_ids[$index] = 1;
      if (!isset($node->choice[$index])) {
        db_query("INSERT INTO {advpoll_choices} (nid, label, weight) VALUES (%d, '%s', %d)", $nid, $choice['label'], $weight);
      }
      else {
        db_query("UPDATE {advpoll_choices} SET label = '%s', weight = %d WHERE cid = %d", $choice['label'], $weight, $index);
      }
      $weight++;
    }
  }

  // Delete any choices that were removed (either dynamically or by submitting
  // a blank label).
  if (isset($node->choice)) {
    foreach ($node->choice as $id => $choice) {
      if (!isset($seen_ids[$id])) {
        db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $id);
        drupal_set_message(t('Deleted choice %label', array(
          '%label' => $choice['label'],
        )), 'status');

        // We could potentially also delete any votes for this choice, but let's
        // leave them in the database so that one can go back and check if anyone
        // voted for a deleted choice.
      }
    }
  }
}