<?php

/**
 * The module file for simple_mcq.
 *
 * @file
 * An alternate Multichoice type question for Quiz module.
 */

/**
 * Implements hook_help().
 */
function simple_mcq_help($path, $args) {
  if ($path == 'admin/help#simple_mcq') {
    return t("<p>This module provides a Simple Multiple choice question question type for Quiz.</p>");
  }
}

/**
 * Implements hook_quiz_question_info().
 */
function simple_mcq_quiz_question_info() {
  return array(
    'simple_mcq' => array(
      'name' => t('Simple MCQ'),
      'description' => t('This provides simple multiple choice questions for use by the Quiz module.'),
      'question provider' => 'SimpleMCQQuestion',
      'response provider' => 'SimpleMCQResponse',
      'module' => 'quiz_question',
    ),
  );
}

/**
 * Implements hook_config().
 */
function simple_mcq_config() {
  $form['simple_mcq_def_num_of_alts'] = array(
    '#type' => 'textfield',
    '#title' => t('Default number of alternatives'),
    '#default_value' => variable_get('simple_mcq_def_num_of_alts', 4),
  );
  $form['#validate'][] = 'simple_mcq_config_validate';
  return $form;
}

/**
 * Validate the simple_mcq config form values
 */
function simple_mcq_config_validate($form, $form_state) {
  if (!_quiz_is_int($form_state['values']['simple_mcq_def_num_of_alts'], 2, 50)) {
    form_set_error('simple_mcq_def_num_of_alts', t('The default number of alternatives must be between 2 and 20'));
  }
}

/**
 * Implements hook_theme().
 */
function simple_mcq_theme($existing, $type, $theme, $path) {
  $path = drupal_get_path('module', 'simple_mcq') . '/theme';
  return array(
    'simple_mcq_creation_form' => array(
      'render element' => 'form',
      'path' => $path,
      'file' => 'simple_mcq.theme.inc',
    ),
    'simple_mcq_answer_node_view' => array(
      'variables' => array(
        'alternatives' => NULL,
        'show_correct' => NULL,
      ),
      'path' => $path,
      'file' => 'simple_mcq.theme.inc',
    ),
    'simple_mcq_response' => array(
      'variables' => array(
        'data' => array(),
      ),
      'path' => $path,
      'file' => 'simple_mcq.theme.inc',
    ),
    'simple_mcq_alternative_creation' => array(
      'render element' => 'form',
      'path' => $path,
      'template' => 'simple_mcq_alternative_creation',
    ),
    'simple_mcq_answering_form' => array(
      'render element' => 'form',
      'path' => $path,
      'template' => 'simple_mcq_answering_form',
    ),
    'simple_mcq_alternative' => array(
      'render element' => 'form',
      'path' => $path,
      'template' => 'simple_mcq_alternative',
    ),
  );
}

/**
 * ajax callback function used when adding alternatives to the node-form
 */
function simple_mcq_add_alternative_ajax_callback($form, &$form_state) {
  $i = 0;
  while (isset($form['alternatives'][$i])) {
    $i++;
  }
  return $form['alternatives'][$i - 1];
}

/**
 * Submit handler used when adding more alternatives to the node-form
 */
function simple_mcq_more_choices_submit($form, &$form_state) {
  // Set the form to rebuild and run submit handlers.
  if (!empty($form['node']->nid)) {
    node_form_submit_build_node($form, $form_state);
  }

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

  // Make the changes we want to the form state.
  if ($form_state['values']['alternatives']['simple_mcq_add_alternative']) {
    // We add 3 if js is disabled. 1 if the adding is done using ahah
    $n = $_GET['q'] == 'system/ajax' ? 1 : 3;
    $form_state['choice_count'] = $exists + $n;
  }
  $form_state['rebuild'] = TRUE;
}

/**
 * Recursive helper function to set the validated property. (Taken from the skip validation module.)
 *
 * @param &$elements
 *   The elements that are currently being processed.
 */
function _simple_mcq_skip_validation(&$elements) {
  $elements['#validated'] = TRUE;
  foreach (element_children($elements) as $key) {
    _simple_mcq_skip_validation($elements[$key]);
  }
}

/**
 * Implements hook_field_extra_fields().
 */
function simple_mcq_field_extra_fields() {
  $extra['node']['simple_mcq'] = array(
    'form' => array(
      'alternatives' => array(
        'label' => t('Alternatives'),
        'description' => t('Alternatives for simple_mcq'),
        'weight' => -4,
      ),
    ),
  );
  return $extra;
}
