You are here

function advpoll_node_presave in Advanced Poll 7

Same name and namespace in other branches
  1. 7.3 advpoll.module \advpoll_node_presave()
  2. 7.2 advpoll.module \advpoll_node_presave()

File

./advpoll.module, line 277

Code

function advpoll_node_presave($node) {

  // Scrub any votes that are orphaned when a choice is removed via the edit page.
  if ($node->type === 'advpoll') {

    // get votes for this node
    if (isset($node->nid)) {
      $criteria = array();
      $criteria['entity_id'] = $node->nid;
      $criteria['entity_type'] = 'advpoll';
      $results = votingapi_select_votes($criteria);
      if ($results) {

        // get all existing choices in the node
        $choices = $node->advpoll_choice[$node->language];
        foreach ($choices as $choice) {
          $ids[] = $choice['choice_id'];
        }
        $noMatch = array();
        foreach ($results as $vote) {
          if (!in_array($vote['tag'], $ids)) {
            $noMatch[] = $vote;
          }
        }
        votingapi_delete_votes($noMatch);
      }
    }
  }
}