<?php


/**
 * @file
 * The installer file for truefalse question type. *
 */

/**
 * Implements hook_install().
 */
function truefalse_install() {
  // Add body field to true false node
  quiz_question_add_body_field('truefalse');

  if (!db_table_exists('quiz_truefalse_node')) {
    // TODO The drupal_(un)install_schema functions are called automatically in D7.
    // drupal_install_schema('truefalse')
  }
  else {
    // TODO Please review the conversion of this statement to the D7 database API syntax.
    /* db_query("UPDATE {system} SET name = 'truefalse' WHERE name = 'true_false'") */
    db_update('system')
      ->fields(array(
        'name' =>  'truefalse',
      ))
      ->condition('name', 'true_false')
      ->execute();
    // TODO Please review the conversion of this statement to the D7 database API syntax.
    /* db_query("UPDATE {node} SET type = 'truefalse' WHERE type = 'true_false'") */
    db_update('node')
      ->fields(array(
        'type' =>  'truefalse',
      ))
      ->condition('type', 'true_false')
      ->execute();
    // TODO Please review the conversion of this statement to the D7 database API syntax.
    /* db_query("UPDATE {system} SET type = 'truefalse' WHERE type = 'true_false'") */
    db_update('system')
      ->fields(array(
        'type' =>  'truefalse',
      ))
      ->condition('type', 'true_false')
      ->execute();
    // TODO Please review the conversion of this statement to the D7 database API syntax.
    /* db_query("UPDATE {taxonomy_vocabulary_node_type} SET type = 'truefalse' WHERE type = 'true_false'") */
    /*
    db_update('taxonomy_vocabulary_node_type')
  ->fields(array(
    'type' =>  'truefalse',
  ))
  ->condition('type', 'true_false')
  ->execute();
  */
  }
  variable_set('node_options_truefalse', array('status'));
  cache_clear_all('autoload:', 'cache');
}

/**
 * Implements hook_uninstall().
 */
function truefalse_uninstall() {
  // TODO The drupal_(un)install_schema functions are called automatically in D7.
  // drupal_uninstall_schema('truefalse')
}

/**
 * Implements hook_schema().
 */
function truefalse_schema() {

  //////////////////////
  // TRUE/FALSE tables.
  //////////////////////

  // Extensions to nodes for T/F
  $schema['quiz_truefalse_node'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'correct_answer' => array(
        'description' => 'The correct answer for the user to give. 1 for true.',
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'tiny',
        'default' => 0,
      ),
      'feedback' => array(
        'type' => 'text',
      ),
    ),
    'primary key' => array(
      'vid', 'nid'
    ),
  );

  // Place to store user answers for T/F questions
  $schema['quiz_truefalse_user_answers'] = array(
    'fields' => array(
      'question_nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'question_vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'result_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'answer' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'tiny',
        'default' => 0,
      ),
      'score' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'result_id', 'question_nid', 'question_vid',
    ),
  );

  return $schema;
}
