You are here

function _quiz_question_browser_form in Quiz 7

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.4 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 1247
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_question_browser',
  );
  $browser =& $form['table'];

  // Ajax use this field to send extra query strings to drupal
  $browser['add_to_get'] = array(
    '#type' => 'hidden',
    '#default_value' => '',
  );

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

  //$question_type_list = implode('\', \'', array_keys($question_types));

  // 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'] = 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',
    ),
  );

  /*
    // Sql to get the questions for the question browser...
    $sql = 'SELECT n.nid, n.type, n.vid, n.title, n.changed, u.name
  FROM {node} n
  LEFT OUTER JOIN {users} u
  ON n.uid = u.uid
  WHERE n.type IN (\'' . $question_type_list . '\') AND n.nid NOT IN (
    SELECT child_nid
    FROM {quiz_node_relationship}
    WHERE parent_vid = %d
  )' . $filter_sql;//tablesort_sql($browser['#header']);
  */
  $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')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $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');
  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');
    $filter_sql .= $changed_timestamps[$_SESSION[$pre . 'changed']]['sql'];
  }
  $query
    ->limit(10);
  $query
    ->orderByHeader($browser['#header']);

  //$results = $query->fetchAll();

  /* We use a custom query for the pager to vastly improve performance if there are lots of db hits...
     This custom query doesn't find the total number of hits, thus the "last" link in the pager is hidden. */

  /*
    $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
    $num = 25;
    $pager_sql = 'SELECT COUNT(*)
            FROM (
           ' // db_rewrite_sql doesn't work on the entire query because of a bug...
           . ('SELECT n.nid FROM {node} n
            LEFT OUTER JOIN {users} u ON n.uid = u.uid
            WHERE n.type IN (\'' . $question_type_list . '\')') . '
  AND n.nid NOT IN (
    SELECT child_nid
    FROM {quiz_node_relationship}
    WHERE parent_vid = %d
  )' . $filter_sql . " LIMIT 0, " . max($page * $num + $num * 7, $num * 10) . ") tbl";
  */

  //$res = pager_query(db_rewrite_sql($sql), $num, 0, $pager_sql, $filter_params);

  //$res = db_query($sql);//, $num, 0, $pager_sql, $filter_params);

  // build data part of form

  //while ($res_o = $query->fetch()) {
  $options = array();
  foreach ($query
    ->execute() as $res_o) {
    $id = $res_o->nid . '-' . $res_o->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;
    $options[$id] = check_plain($res_o->title);
    $browser['changed'][$id]['#value'] = format_date($res_o->changed, 'short');
    $browser['types'][$id]['#value'] = $question_types[$res_o->type]['name'];
    $browser['names'][$id]['#value'] = check_plain($res_o->name);
  }
  $browser['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>',
  );
  $browser['ahah_target_all_end'] = array(
    '#markup' => '</div>',
  );
  return $form;
}