You are here

function quiz_browser_ahah in Quiz 6.4

Same name and namespace in other branches
  1. 7 quiz.admin.inc \quiz_browser_ahah()

AHAH handler for the question browser

1 string reference to 'quiz_browser_ahah'
quiz_menu in ./quiz.module
Implementation of hook_menu().

File

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

Code

function quiz_browser_ahah() {

  // Prepare to get the form from cache
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_id = $_POST['form_build_id'];

  // Make sure the form exists
  if (!($form = form_get_cache($form_id, $form_state))) {
    form_set_error('form_token', t("Validation error, please try again. If this error persists, please contact the site administrator."));
    $output = theme('status_messages');
    print drupal_to_js(array(
      'status' => TRUE,
      'data' => $output,
    ));
    exit;
  }

  // If filter or pager has been used
  $replace_all = drupal_strlen($_POST['browser']['table']['add_to_get']) > 0;

  // Prepare to submit the form
  $args = $form['#parameters'];
  $form_id = array_shift($args);

  // We will run some of the submit handlers so we need to disable redirecting.
  $form['#redirect'] = FALSE;

  // We need to process the form, prepare for that by setting a few internals
  // variables.
  $form['#post'] = $_POST;
  $form['#programmed'] = FALSE;
  $form_state['post'] = $_POST;
  $form_state['#from_ahah'] = TRUE;

  // $_REQUEST is used by drupal core. We need to remove data recieved from ajax.
  foreach ($_POST as $key => $value) {
    unset($_REQUEST[$key]);
  }
  $_POST = array();

  // We don't want any validation errors to show up. This isn't a real submit.
  _quiz_skip_validation($form);
  _quiz_browser_store_filters($form_state['post']['browser']['table']['filters']);

  // Build, validate and submit the form.
  drupal_process_form($form_id, $form, $form_state);

  //Remove any status messages the processing resulted in...

  //theme('status_messages');

  // Get the form the ajax call resulted in
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  // We store a copy of the browser part of the form, and remove it from the main form.
  // This way we can render the form without the browser, and render the browser separatly
  $b_form = $form['question_list']['browser'];
  unset($form['question_list']['browser']);

  /*
   * $output is the string we will send to ajax, and that will be added to the page.
   * We are doing this in a special way so way have to call javascript functions to
   * update the DOM.
   */
  $output = '<script>var quizNewBuildId = ' . drupal_to_js($form['#build_id']) . ';';

  // We remove all visible questions from the quiz. We only want to render the hidden questions.
  foreach ($form['question_list']['stayers'] as $key => $value) {
    if (isset($value['#value']) && $value['#value'] == 1) {
      unset($form['question_list']['titles'][$key]);
    }
  }

  // We count the number of hidden questions
  $num_hiddens = 0;
  if (is_array($form['question_list']['titles'])) {
    foreach ($form['question_list']['titles'] as $key => $value) {
      if (preg_match('/^[0-9]+-[0-9]+/', $key)) {
        $num_hiddens++;
      }
    }
  }

  // We render what is left of the question list
  $sub_form_html = drupal_render($form['question_list']);

  // We pick the table rows holding the hidden questions
  $q_rows_html = _quiz_get_last_table_rows($sub_form_html, $num_hiddens);
  $output .= ' var qRowsHidden = ' . drupal_to_js($q_rows_html) . ';';

  // We replace the entire browser. This is done when the browser is sorted
  if ($replace_all) {

    // Make sure the description and test is removed
    unset($b_form['#type']);
    unset($b_form['#value']);
    $rendered_browser = drupal_render($b_form['table']);
    $output .= ' var renderedBrowser = ' . drupal_to_js(theme('status_messages') . $rendered_browser) . ';';

    // Have js(jQuery) replace the browser and change the form build id in the DOM
    $output .= ' Quiz.replaceBrowser(renderedBrowser, quizNewBuildId);';

    // Add the hidden questions to the question list
    $output .= ' Quiz.addQuestions(qRowsHidden);</script>';
  }
  else {

    // Send status messages, new build id for the form and the new question row to the javascript:
    $b_rows = _quiz_get_browser_content(drupal_render($b_form), 'quiz-question-browser-row');
    $b_pager = drupal_render($b_form['table']['pager']);
    $output .= ' var quizBrowserRows = ' . drupal_to_js($b_rows) . ';';
    $output .= ' var quizBrowserPager = ' . drupal_to_js($b_pager) . ';';
    $output .= ' Quiz.addBrowserRows(quizBrowserRows, quizNewBuildId, quizBrowserPager);';
    $output .= ' Quiz.addQuestions(qRowsHidden);</script>';
    $output .= ' ' . theme('status_messages');
  }
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}