<?php

/**
 * @file
 * Evaluation question type for the Quiz module.
 *
 * Allows the creation of evaluation questions for use when getting users to
 * self evaluate or to create a survey.
 */

/**
 * Implements hook_quiz_question_info().
 */
function quiz_evaluation_quiz_question_info() {
  return array(
    'quiz_evaluation' => array(
      'name' => t('Quiz evaluation question'),
      'description' => t('Quiz questions that allow a user to choose from a scale.'),
      'question provider' => 'EvaluationQuestion',
      'response provider' => 'EvaluationResponse',
      'module' => 'quiz_question', // All wrapper functions are in that module
    ),
  );
}

/**
 * Implements hook_user_cancel().
 *
 * The user account is being deleted so we also delete any responces they might
 * have entered into our questions.
 */
function quiz_evaluation_user_cancel($edit, $account, $method) {
  db_delete('quiz_evaluation_user_score')
    ->condition('rid', $account->rid)
    ->execute();
}

/**
 * Get all ranges.
 *
 * Get all of the ranges available (along with the max and min scores) for the
 * quiz. These are only really to be used on the administration form.
 *
 * @return
 *    An array of ranges including the min and max score for each.
 */
function _quiz_evaluation_get_ranges() {
  $results = db_query("SELECT range_id, SUBSTR(MAX(CONCAT(LPAD(score,6,'0'),value)),7) as min, SUBSTR(MIN(CONCAT(LPAD(score,6,'0'),value)),7) as max
FROM {quiz_evaluation_range}
GROUP BY range_id;")->fetchAll();
  return $results;
}

/**
 * Ajax callback function used when adding alternatives to the node-form.
 *
 * @param $form
 *    The form as created by EvaluationQuiz::getCreationForm().
 * @param $form_state
 *    The current form state.
 *
 * @return
 *    The latest evaluation item from the form.
 */
function quiz_evaluation_add_alternative_ajax_callback($form, &$form_state) {
  $i = 0;
  while (isset($form['quiz_evaluation_answers'][$i])) {
    $i++;
  }
  return $form['quiz_evaluation_answers'][$i - 1];
}

/**
 * Submit handler used when adding more alternatives to the node-form
 *
 * @param $form
 *    The form as created by EvaluationQuiz::getCreationForm().
 * @param $form_state
 *    The current form state.
 */
function quiz_evaluation_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['quiz_evaluation_answers'][$exists])) {
    $exists++;
  }

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

/**
 * Implements hook_theme().
 */
function quiz_evaluation_theme($existing, $type, $theme, $path) {
  return array(
    'quiz_evaluation_answering' => array(
      'render element' => 'form',
      'path' => drupal_get_path('module', 'quiz_evaluation') . '/theme',
      'file' => 'quiz-evaluation.theme.inc',
    ),
    'quiz_evaluation_report_form' => array(
      'render element' => 'form',
      'path' => $path . '/theme',
      'template' => 'quiz-report-form',
    ),
  );
}

/**
 *
 * @param $range_id
 *    The range ID to be looked up.
 *
 * @return
 *    An associative array containing:
 *    - value_id: The ID of the current range item.
 *    - range_id: The range ID.
 *    - value: The value of the range item.
 *    - score: The score of the range item.
 */
function _quiz_evaluation_range_lookup($range_id) {
  $results = db_query("SELECT value_id, range_id, value, score FROM {quiz_evaluation_range} WHERE range_id = :range_id ORDER BY score DESC;", array(':range_id' => $range_id))->fetchAll();
  return $results;
}

/**
 * Implements hook_help().
 */
function quiz_evaluation_help($path, $args) {
  if ($path == 'admin/help#quiz_evaluation') {
    return t('This module provides a mutli-scale question type for Quiz. It may be used to construct evaluation questions or to construct a survey.');
  }
}

/**
 * Implements hook_config().
 *
 * Required to stop 500 error when visiting /admin/quiz/settings/questions_settings
 */
function quiz_evaluation_config() {
  return FALSE;
}