You are here

function multichoice_add_alternative_ahah in Quiz 6.4

ahah callback function used when adding alternatives to the node-form

1 string reference to 'multichoice_add_alternative_ahah'
multichoice_menu in question_types/multichoice/multichoice.module
Implementation of hook_menu

File

question_types/multichoice/multichoice.module, line 161
The main file for multichoice.

Code

function multichoice_add_alternative_ahah() {
  include_once 'modules/node/node.pages.inc';
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];

  // Get the form from the cache.
  $form = form_get_cache($form_build_id, $form_state);

  // Get values have been sent using the form cache
  if (isset($form['get']['#value']) && is_array($form['get']['#value'])) {
    $get = $form['get']['#value'];
    foreach ($get as $key => $value) {
      $_GET[$key] = $value;
    }
  }
  $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;

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

  // This call recreates the form relying solely on the form_state that the
  // drupal_process_form set up.
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  // Count the alternatives
  $i = 0;
  while (isset($form['alternatives'][$i])) {
    $i++;
  }
  $i--;

  // Render the new output.
  $new_choice = $form['alternatives'][$i];
  $new_choice['#collapsed'] = FALSE;
  $output = form_get_errors() ? '' : drupal_render($new_choice);
  $status = theme('status_messages');
  drupal_json(array(
    'status' => TRUE,
    'data' => $status . $output,
  ));
}