You are here

quiz.theme.inc in Quiz 8.6

Same filename and directory in other branches
  1. 8.4 quiz.theme.inc
  2. 8.5 quiz.theme.inc
  3. 7.5 quiz.theme.inc

quiz.theme.inc Quiz theme functions.

File

quiz.theme.inc
View source
<?php

/**
 * @file quiz.theme.inc
 * Quiz theme functions.
 */

/**
 * Theme a progress indicator for use during a quiz.
 *
 * @param $question_number
 *   The position of the current question in the sessions' array.
 * @param $num_of_question
 *   The number of questions for this quiz as returned by
 *   quiz_get_number_of_questions().
 *
 * @return
 *   Themed html.
 *
 * @ingroup themeable
 */
function theme_quiz_progress($variables) {
  drupal_add_js(drupal_get_path('module', 'quiz') . '/js/quiz.jumper.js');
  $output = '';
  $output .= '<div id="quiz-progress">';
  $text = 'Page <span id="quiz-question-number">!x</span> of <span id="quiz-num-questions">@y</span>';
  if ($variables['allow_jumping']) {

    // Show jump form.
    if ($variables['pager']) {
      $output .= theme('quiz_pager', array(
        'siblings' => \Drupal::config('quiz.settings')
          ->get('pager_siblings', 5),
        'current' => $variables['current'],
        'total' => count($variables['questions']),
      ));
    }
    else {
      $selectbox = drupal_get_form('quiz_jumper_form', $variables['questions'], $variables['current']);
      $output .= t($text, array(
        '!x' => drupal_render($selectbox),
        '@y' => count($variables['questions']),
      ));
    }
  }
  else {

    // Just text.
    $output .= t($text, array(
      '!x' => $variables['current'],
      '@y' => count($variables['questions']),
    ));
  }
  $output .= '</div>' . "\n";
  return $output;
}

/**
 * Help us with special pagination.
 *
 * Why not the Drupal theme_pager()?
 *
 * It uses query strings. We have access on each menu argument (quiz question
 * number) so we unfortunately cannot use it.
 */
function _quiz_pagination_helper($total, $perpage = NULL, $current = NULL, $siblings = NULL) {
  $result = array();
  if (isset($total, $perpage) === TRUE) {
    $result = range(1, ceil($total / $perpage));
    if (isset($current, $siblings) === TRUE) {
      if (($siblings = floor($siblings / 2) * 2 + 1) >= 1) {
        $result = array_slice($result, max(0, min(count($result) - $siblings, intval($current) - ceil($siblings / 2))), $siblings);
      }
    }
  }
  return $result;
}

/**
 * Theme the quiz pager.
 */
function theme_quiz_pager($variables) {
  $total = $variables['total'];
  $current = $variables['current'];
  $siblings = $variables['siblings'];
  $items = array();
  $nid = arg(1);
  $items[] = array(
    'class' => array(
      'pager-first',
    ),
    'data' => l(t('first'), "quiz/{$nid}/take/1"),
  );
  foreach (_quiz_pagination_helper($total, 1, $current, $siblings) as $i) {
    if ($i == $current) {
      $items[] = array(
        'class' => array(
          'pager-current',
        ),
        'data' => $i,
      );
    }
    else {
      $items[] = array(
        'class' => array(
          'pager-item',
        ),
        'data' => l($i, "quiz/{$nid}/take/{$i}"),
      );
    }
  }
  $items[] = array(
    'class' => array(
      'pager-last',
    ),
    'data' => l(t('last'), "quiz/{$nid}/take/{$total}"),
  );
  return theme('item_list', array(
    'items' => $items,
    'attributes' => array(
      'class' => array(
        'pager',
      ),
    ),
  ));
}

/**
 * Themes a categorized quiz form.
 */
function theme_quiz_categorized_form($variables) {
  $form = $variables['form'];
  $output = '';
  $rows = array();
  foreach ($form as $key => &$existing) {
    if (!is_numeric($key)) {
      continue;
    }
    $cols = array();
    $cols[] = drupal_render($existing['name']);
    $cols[] = drupal_render($existing['number']);
    $cols[] = drupal_render($existing['max_score']);
    $cols[] = drupal_render($existing['remove']);
    $cols[] = drupal_render($existing['weight']);
    $rows[] = array(
      'data' => $cols,
      'class' => array(
        'draggable',
      ),
    );
  }
  if (!empty($rows)) {
    $header = array(
      t('Category'),
      t('Number of questions'),
      t('Max score per question'),
      t('Remove'),
      t('Weight'),
    );
    drupal_add_tabledrag('existing-terms', 'order', 'sibling', 'term-weight', NULL, NULL, TRUE);
    $output .= theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => 'existing-terms',
      ),
    ));
  }
  $output .= drupal_render_children($form);
  drupal_add_js("(function (\$) {\n     Drupal.behaviors.quiz_categorized = {\n     attach: function(context) {\n      \$('#browse-for-term:not(.quiz-processed)').click(function(event) {\n        event.preventDefault();\n        \$('#edit-term').focus().val('*').trigger('keyup');\n      }).addClass('quiz-processed');\n      \$('#edit-term').click(function(){\n        if (\$(this).val() == '*') {\n          \$(this).val('');\n        }\n      });\n    }};}(jQuery));", array(
    'type' => 'inline',
    'group' => JS_DEFAULT,
  ));
  return $output;
}

Functions

Namesort descending Description
theme_quiz_categorized_form Themes a categorized quiz form.
theme_quiz_pager Theme the quiz pager.
theme_quiz_progress Theme a progress indicator for use during a quiz.
_quiz_pagination_helper Help us with special pagination.