You are here

function quiz_results_mr_browser_ahah in Quiz 6.4

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

AHAH handler for the results browser

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

File

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

Code

function quiz_results_mr_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['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 internal variables.
  $form['#post'] = $_POST;
  $form['#programmed'] = FALSE;
  $form_state['post'] = $_POST;

  // $_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_results_mr_store_filters($form_state['post']['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['table'];
  unset($form['table']);

  /*
   * $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 we have to call javascript functions to
   * update the DOM.
   */
  $output = '<script>var quizNewBuildId = ' . drupal_to_js($form['#build_id']) . ';';

  // 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);
    $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); </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-results-browser-row');
    $b_pager = drupal_render($b_form['pager']);
    $output .= ' var quizBrowserRows = ' . drupal_to_js($b_rows) . ';';
    $output .= ' var quizBrowserPager = ' . drupal_to_js($b_pager) . ';';
    $output .= ' Quiz.addBrowserRows(quizBrowserRows, quizNewBuildId, quizBrowserPager);';
    $output .= ' </script>';
    $output .= ' ' . theme('status_messages');
  }
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}