You are here

function opigno_poll_app_node_load in Opigno Poll App 7

Implements hook_node_load().

File

./opigno_poll_app.module, line 101
Module hooks.

Code

function opigno_poll_app_node_load($nodes, $types) {
  global $user;
  if (in_array('poll', $types)) {
    foreach ($nodes as $node) {
      if (!user_access('vote on polls')) {

        /**
         * This part is a verbatim copy from poll_load().
         */
        $poll = db_query("SELECT runtime, active FROM {poll} WHERE nid = :nid", array(
          ':nid' => $node->nid,
        ))
          ->fetchObject();
        if (empty($poll)) {
          $poll = new stdClass();
        }
        if ($poll->active && !empty($node->og_group_ref)) {
          $access = FALSE;
          foreach ($node->og_group_ref as $lang => $items) {
            foreach ($items as $item) {
              if ($access = og_user_access('node', $item['target_id'], 'vote on polls')) {
                break 2;
              }
            }
          }
          if ($access) {
            if ($user->uid) {

              // If authenticated, find existing vote based on uid.
              $poll->vote = db_query('SELECT chid FROM {poll_vote} WHERE nid = :nid AND uid = :uid', array(
                ':nid' => $node->nid,
                ':uid' => $user->uid,
              ))
                ->fetchField();
              if (empty($poll->vote)) {
                $poll->vote = -1;
                $poll->allowvotes = TRUE;
              }
            }
            elseif (!empty($_SESSION['poll_vote'][$node->nid])) {

              // Otherwise the user is anonymous. Look for an existing vote in the
              // user's session.
              $poll->vote = $_SESSION['poll_vote'][$node->nid];
            }
            else {

              // Finally, query the database for an existing vote based on anonymous
              // user's hostname.
              $poll->allowvotes = !db_query("SELECT 1 FROM {poll_vote} WHERE nid = :nid AND hostname = :hostname AND uid = 0", array(
                ':nid' => $node->nid,
                ':hostname' => ip_address(),
              ))
                ->fetchField();
            }
          }
        }
        foreach ($poll as $key => $value) {
          $nodes[$node->nid]->{$key} = $value;
        }
      }
    }
  }
}