You are here

function _quiz_question_browser_form in Quiz 6.4

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \_quiz_question_browser_form()
  2. 7 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 1219
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(
      '#type' => 'markup',
      '#value' => 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,
  );
  $form['table'] = array(
    '#theme' => 'quiz_question_browser',
  );
  $browser =& $form['table'];

  // Ahah targets
  $browser['ahah_target'] = array(
    '#type' => 'markup',
    '#value' => '<DIV ID = "ahah-target"></DIV>',
  );
  $browser['ahah_target_all'] = array(
    '#type' => 'markup',
    '#value' => '<DIV ID = "all-ahah-target">',
  );

  // 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);

  // Create filter part of sql.
  $filter_params = array(
    $quiz->vid,
  );

  // Get filter sql, and add filter params to $filter_params
  $filter_sql = _quiz_question_browser_prepare_filter_sql($filter_params);
  $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' => 'title',
      'class' => 'quiz-browser-header-title',
    ),
    array(
      'data' => t('Type'),
      'field' => 'type',
      'class' => 'quiz-browser-header-type',
    ),
    array(
      'data' => t('Changed'),
      'field' => 'changed',
      'sort' => 'desc',
      'class' => 'quiz-browser-header-changed',
    ),
    array(
      'data' => t('Username'),
      'field' => 'name',
      'class' => 'quiz-browser-header-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']);

  /* 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('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);

  // build data part of form
  $options = array();
  while ($res_o = db_fetch_object($res)) {
    $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, 'small');
    $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' => 'quiz-browser-checkbox',
    ),
    '#default_value' => $questions,
  );
  $browser['pager'] = array(
    '#value' => '<DIV ID ="browser-pager">' . theme('pager', NULL, $num) . '</DIV>',
  );
  $browser['ahah_target_all_end'] = array(
    '#type' => 'markup',
    '#value' => '</DIV>',
  );
  return $form;
}