You are here

function advpoll_node_presave in Advanced Poll 7.2

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

File

./advpoll.module, line 293

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);
      $lang = $node->language;
      if ($results) {

        // get all existing choices in the node
        if (!isset($node->advpoll_choice[$lang])) {
          $lang = 'und';
        }
        $choices = $node->advpoll_choice[$lang];
        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);
      }
    }
  }
}