You are here

function advpoll_ranking_node_view in Advanced Poll 7.3

Same name and namespace in other branches
  1. 7 advpoll_ranking/advpoll_ranking.module \advpoll_ranking_node_view()
  2. 7.2 advpoll_ranking/advpoll_ranking.module \advpoll_ranking_node_view()

Implements hook_node_view().

Overriding what advanced poll does with the node_view hook for those polls that are either borda or runoff.

File

advpoll_ranking/advpoll_ranking.module, line 20

Code

function advpoll_ranking_node_view($node, $view_mode) {
  if ($node->type == 'advpoll') {
    $data = advpoll_get_data($node);
    if ($data->behavior == 'borda' || $data->behavior == 'runoff' || $data->behavior == 'borda_all' || $data->behavior == 'runoff_all') {
      drupal_add_css(drupal_get_path('module', 'advpoll') . '/css/advpoll.css', array(
        'group' => CSS_DEFAULT,
        'every_page' => TRUE,
      ));

      // Use existing weight if defined.
      $weight = 1;
      if (!empty($node->content['advpoll_choice']['#weight'])) {
        $weight = $node->content['advpoll_choice']['#weight'];
      }

      // Replace the markup for choices with appropriate markup.
      unset($node->content['advpoll_choice']);

      // Check for eligibility to vote
      if (advpoll_user_eligibility($node)) {

        // Print out voting form.
        if ($data->behavior == 'borda' || $data->behavior == 'runoff') {
          $rendered_form = drupal_get_form('advpoll_ranking_choice_form', $data, $node);
          $voteform = '<div class="advpoll-ranking-wrapper">' . drupal_render($rendered_form) . '</div>';
          $node->content['advpoll_choice'] = array(
            '#markup' => $voteform,
            '#weight' => $weight,
          );
        }
        else {
          $rendered_form = drupal_get_form('advpoll_draggable_form', $data, $node);
          $voteform = '<div class="advpoll-ranking-wrapper">' . drupal_render($rendered_form) . '</div>';
          $node->content['advpoll_choice'] = array(
            '#markup' => $voteform,
            '#weight' => $weight,
          );
        }
      }
      else {
        if ($data->behavior == 'borda' || $data->behavior == 'borda_all') {
          $results = advpoll_display_borda_results($node->nid, $data);
        }
        else {
          $results = advpoll_display_runoff_results($node->nid, $data);
        }
        $node->content['advpoll_choice'] = array(
          '#markup' => $results,
          '#weight' => $weight,
        );
      }
    }

    /* JS needs to be present for any ranking poll in case form is re-rendered
     * by vote cancellation.
     */
    drupal_add_js(drupal_get_path('module', 'advpoll_ranking') . '/js/advpoll-ranking.js', 'file');
    drupal_add_js(array(
      'advpoll_ranking' => array(
        'display' => 'TRUE',
      ),
    ), 'setting');
  }
}