You are here

function _quiz_question_browser_form in Quiz 7.4

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \_quiz_question_browser_form()
  2. 6.4 quiz.admin.inc \_quiz_question_browser_form()
  3. 7 quiz.admin.inc \_quiz_question_browser_form()

Creates the browser part of the quiz_questions_form

Parameters

$hidden_questions: Array where we add the questions in the browser

$questions: Questions already added to the question list(array)

$form_state: FAPI form_state(array)

$quiz: Quiz node(object)

Return value

form FAPI form(array)

1 call to _quiz_question_browser_form()
quiz_questions_form in ./quiz.admin.inc
Handles "manage questions" tab.

File

./quiz.admin.inc, line 1221
Administrator interface for Quiz module.

Code

function _quiz_question_browser_form(&$hidden_questions, $questions, $form_state, $quiz, $question_types) {
  if (!is_array($question_types) || count($question_types) == 0) {
    return $form['no_questions'] = array(
      '#markup' => t('No question types are enabled'),
    );
  }
  $form = array(
    '#type' => 'fieldset',
    '#title' => t('Browse for questions to add'),
    '#description' => t('Mark all the questions you want to add.') . ' ' . t('You can filter questions by using the textfields and select boxes.') . ' ' . t('You can sort by pressing the table headers.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#prefix' => '<div id="quiz-browser-target">',
    '#suffix' => '</div>',
  );
  $form['table'] = array(
    '#theme' => 'quiz_browser',
  );
  $browser =& $form['table'];

  // Ajax use this field to send extra query strings to drupal
  $browser['add_to_get'] = array(
    '#type' => 'hidden',
    '#default_value' => '',
    '#attributes' => array(
      'id' => 'edit-browser-table-add-to-get',
    ),
  );
  $browser['header'] = array(
    '#theme' => 'quiz_questions_browser_header',
  );
  $browser['body'] = array(
    '#theme' => 'quiz_questions_browser_body',
  );

  //Build filter part of form:
  _quiz_question_browser_add_filter_fields($browser['header'], $question_types, $quiz);

  // Add querystring recieved via ajax to the $_GET array...
  if (isset($form_state['values'])) {
    _quiz_add_to_get($form_state['values']['browser']['table']['add_to_get']);
  }

  // Browsers table header
  $browser['header']['#header'] = array(
    NULL,
    array(
      'data' => t('Title'),
      'field' => 'n.title',
    ),
    array(
      'data' => t('Type'),
      'field' => 'n.type',
    ),
    array(
      'data' => t('Changed'),
      'field' => 'n.changed',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Username'),
      'field' => 'u.name',
    ),
  );
  $child_nid = db_query('SELECT child_nid FROM {quiz_node_relationship}
      WHERE parent_vid = :parent_vid', array(
    ':parent_vid' => $quiz->vid,
  ))
    ->fetchCol();
  $query = db_select('node', 'n');
  $query
    ->fields('n', array(
    'nid',
    'type',
    'vid',
    'title',
    'changed',
  ));
  $query
    ->fields('u', array(
    'name',
  ));
  $query
    ->leftJoin('users', 'u', 'n.uid = u.uid');
  $query
    ->condition('type', array_keys($question_types), 'IN');
  $query
    ->condition('n.language', array(
    $quiz->language,
    LANGUAGE_NONE,
  ));
  if (count($child_nid)) {
    $query
      ->condition('nid', $child_nid, 'NOT IN');
  }

  // Apply filter conditions
  // _quiz_question_browser_prepare_filter_sql($query);
  $pre = 'quiz_question_browser_';
  $changed_timestamps = _quiz_get_interval_timestamps('changed');
  $filter_sql = '';
  if (isset($_SESSION[$pre . 'title']) && !empty($_SESSION[$pre . 'title'])) {
    $query
      ->condition('n.title', '%' . db_like($_SESSION[$pre . 'title']) . '%', 'LIKE');
  }
  if (isset($_SESSION[$pre . 'name']) && !empty($_SESSION[$pre . 'name'])) {
    $query
      ->condition('u.name', '%' . db_like($_SESSION[$pre . 'name']) . '%', 'LIKE');
  }
  if (isset($_SESSION[$pre . 'type']) && $_SESSION[$pre . 'type'] !== '0') {
    $query
      ->condition('type', array(
      $_SESSION[$pre . 'type'],
    ), 'IN');
  }
  if (isset($_SESSION[$pre . 'changed'])) {
    $changed_timestamps = _quiz_get_interval_timestamps('changed');
    if ($changed_timestamps[$_SESSION[$pre . 'changed']][0]) {
      $query
        ->condition('changed', $changed_timestamps[$_SESSION[$pre . 'changed']][0], '>');
    }
    if ($changed_timestamps[$_SESSION[$pre . 'changed']][1]) {
      $query
        ->condition('changed', $changed_timestamps[$_SESSION[$pre . 'changed']][1], '<');
    }
  }

  // Clone query for hidden questions block.
  $hidden_questions_query = clone $query;
  $query = $query
    ->extend('PagerDefault')
    ->extend('TableSort')
    ->orderByHeader($browser['header']['#header'])
    ->limit(20);
  $options = array();
  foreach ($query
    ->execute() as $res_o) {
    $id = $res_o->nid . '-' . $res_o->vid;
    $options[$id] = check_plain($res_o->title);
    $browser['body']['changed'][$id]['#value'] = format_date($res_o->changed, 'short');
    $browser['body']['types'][$id]['#value'] = $question_types[$res_o->type]['name'];
    $browser['body']['names'][$id]['#value'] = check_plain($res_o->name);
  }

  // Fill in hidden questions block.
  foreach ($hidden_questions_query
    ->execute() as $res_ob) {
    $id = $res_ob->nid . '-' . $res_ob->vid;

    // Add $id to hidden_questions, this way quiz_questions_form knows that it has to add a invisible row for this question.
    $hidden_questions[] = $id;
  }
  $browser['body']['titles'] = array(
    '#title' => t('Titles'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#attributes' => array(
      'class' => array(
        'quiz-browser-checkbox',
      ),
    ),
    '#default_value' => $questions,
  );
  $browser['pager'] = array(
    '#markup' => '<div id="browser-pager">' . theme('pager', array(
      'tags' => NULL,
    )) . '</div>',
  );
  return $form;
}