You are here

function multichoice_remove_alternative_submit in Quiz 7.5

AJAX submit handler used when removing alternatives from the question edit form.

1 string reference to 'multichoice_remove_alternative_submit'
MultichoiceQuestion::getCreationForm in question_types/multichoice/multichoice.classes.inc
Implementation of getCreationForm().

File

question_types/multichoice/multichoice.module, line 174
Multiplechoice question type for the Quiz module.

Code

function multichoice_remove_alternative_submit($form, &$form_state) {
  $def_num_of_alts = variable_get('multichoice_def_num_of_alts', 2);
  $choice_count = $form_state['choice_count'];
  if ($choice_count <= $def_num_of_alts) {
    drupal_set_message(t('Multiple choice must have at least !num alternatives.', array(
      '!num' => $def_num_of_alts,
    )), 'error');
  }
  else {
    $button = $form_state['triggering_element'];
    $delta = $button['#delta'];
    $input =& $form_state['input']['alternatives'];

    // Get IDs of choices and store it in the $input array.
    for ($i = 0; $i < $choice_count; $i++) {
      $input[$i]['id'] = $form_state['node']->alternatives[$i]['id'];
    }

    // Remove alternative by delta and reorder alternatives.
    unset($input[$delta]);
    for ($i = $delta; $i < $choice_count; $i++) {
      $next = $i + 1;
      if (isset($input[$next])) {
        $input[$i] = $input[$next];
      }
      else {
        unset($input[$i]);
      }
    }

    // Update form state.
    $form_state['choice_count'] = $choice_count - 1;
    $form_state['node']->alternatives = $input;
  }
  $form_state['rebuild'] = TRUE;
}