<?php

/**
 * @file
 * Install, update and uninstall functions for the questionnaire_question module.
 */

/**
 * Implements hook_schema().
 */
function questionnaire_schema() {
  $schema['questionnaire'] = array(
    'description' => 'The additional Questionnaire data',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The primary identifier for a node.',
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The primary identifier for a node revision.',
      ),
      'qstatus' => array(
        'type' => 'text',
        'size' => 'normal',
        'not null' => FALSE,
        'description' => 'The Status of the Questionnaire.',
      ),
    ),
    'primary key' => array('nid', 'vid'),
  );
  return $schema;
}

/**
 * Returns a structured array defining the fields created by this content type.
 *
 * @return
 *  An associative array specifying the fields we wish to add to our
 *  new questionnaire type.
 */
function _questionnaire_installed_fields() {
  $t = get_t();
  $fields = array();

  // Exported field: 'field_collection_item-field_questionnaire_questions-field_questionnaire_qendpoint'.
  $fields['field_collection_item-field_questionnaire_questions-field_questionnaire_qendpoint'] = array(
    'field_config' => array(
      'active' => '1',
      'cardinality' => '1',
      'deleted' => '0',
      'entity_types' => array(),
      'field_name' => 'field_questionnaire_qendpoint',
      'foreign keys' => array(),
      'indexes' => array(
        'value' => array(
          0 => 'value',
        ),
      ),
      'locked' => '0',
      'module' => 'list',
      'settings' => array(
        'allowed_values' => array(
          0 => '',
          1 => '',
        ),
        'allowed_values_function' => '',
      ),
      'translatable' => '0',
      'type' => 'list_boolean',
    ),
    'field_instance' => array(
      'bundle' => 'field_questionnaire_questions',
      'default_value' => array(
        0 => array(
          'value' => 0,
        ),
      ),
      'deleted' => '0',
      'description' => 'This is the last question of this branch of questions. If the user answers this question, she is automatically removed from this Questionnaire and available to participate in the next one.
(This Question therefore may also be a Thank You notice type of Question.',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'list',
          'settings' => array(),
          'type' => 'list_default',
          'weight' => 1,
        ),
      ),
      'entity_type' => 'field_collection_item',
      'field_name' => 'field_questionnaire_qendpoint',
      'label' => 'EndPoint',
      'required' => 0,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'options',
        'settings' => array(
          'display_label' => 1,
        ),
        'type' => 'options_onoff',
        'weight' => '2',
      ),
    ),
  );

  // Exported field: 'field_collection_item-field_questionnaire_questions-field_questionnaire_qparent'.
/*
 * We will need this when we get branches in our campaigns, but not for now
 * Same will be true for filters. (Where previous ansers = 1)
 *
 *  $fields['field_collection_item-field_questionnaire_questions-field_questionnaire_qparent'] = array(
    'field_config' => array(
      'active' => '1',
      'cardinality' => '1',
      'deleted' => '0',
      'entity_types' => array(),
      'field_name' => 'field_questionnaire_qparent',
      'foreign keys' => array(
        'node' => array(
          'columns' => array(
            'target_id' => 'nid',
          ),
          'table' => 'node',
        ),
      ),
      'indexes' => array(
        'target_id' => array(
          0 => 'target_id',
        ),
      ),
      'locked' => '0',
      'module' => 'entityreference',
      'settings' => array(
        'handler' => 'base',
        'handler_settings' => array(
          'behaviors' => array(
            'views-select-list' => array(
              'status' => 0,
            ),
          ),
          'target_bundles' => array(
            'field_questionnaire_questions' => 'field_questionnaire_questions',
          ),
        ),
        'target_type' => 'field_collection_item',
      ),
      'translatable' => '0',
      'type' => 'entityreference',
    ),
    'field_instance' => array(
      'bundle' => 'field_questionnaire_questions',
      'default_value' => NULL,
      'deleted' => '0',
      'description' => 'Select the Question that comes before this one. Obviously, the first question of the campaign does not have this field filled in.',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'entityreference',
          'settings' => array(
            'link' => FALSE,
          ),
          'type' => 'entityreference_label',
          'weight' => 3,
        ),
      ),
      'entity_type' => 'field_collection_item',
      'field_name' => 'field_questionnaire_qparent',
      'label' => 'Parent',
      'required' => 0,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'options',
        'settings' => array(),
        'type' => 'options_buttons',
        'weight' => '4',
      ),
    ),
  );
*/
  // Exported field: 'field_collection_item-field_questionnaire_questions-field_questionnaire_qq'.
  $fields['field_collection_item-field_questionnaire_questions-field_questionnaire_qq'] = array(
    'field_config' => array(
      'active' => '1',
      'cardinality' => '1',
      'deleted' => '0',
      'entity_types' => array(),
      'field_name' => 'field_questionnaire_qq',
      'foreign keys' => array(
        'node' => array(
          'columns' => array(
            'target_id' => 'nid',
          ),
          'table' => 'node',
        ),
      ),
      'indexes' => array(
        'target_id' => array(
          0 => 'target_id',
        ),
      ),
      'locked' => '0',
      'module' => 'entityreference',
      'settings' => array(
        'handler' => 'base',
        'handler_settings' => array(
          'behaviors' => array(
            'views-select-list' => array(
              'status' => 0,
            ),
          ),
          'sort' => array(
            'type' => 'none',
          ),
          'target_bundles' => array(
            'questionnaire_question' => 'questionnaire_question',
          ),
        ),
        'target_type' => 'node',
      ),
      'translatable' => '0',
      'type' => 'entityreference',
    ),
    'field_instance' => array(
      'bundle' => 'field_questionnaire_questions',
      'default_value' => NULL,
      'deleted' => '0',
      'description' => 'Select the question',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'entityreference',
          'settings' => array(
            'link' => FALSE,
          ),
          'type' => 'entityreference_label',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'field_collection_item',
      'field_name' => 'field_questionnaire_qq',
      'label' => 'Question',
      'required' => 1,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'entityreference',
        'settings' => array(
          'match_operator' => 'CONTAINS',
          'path' => '',
          'size' => '60',
        ),
        'type' => 'entityreference_autocomplete',
        'weight' => '1',
      ),
    ),
  );


  // Exported field: 'node-questionnaire-field_questionnaire_questions'.
  $fields['node-questionnaire-field_questionnaire_questions'] = array(
    'field_config' => array(
      'active' => '1',
      'cardinality' => '-1',
      'deleted' => '0',
      'entity_types' => array(),
      'field_name' => 'field_questionnaire_questions',
      'foreign keys' => array(),
      'indexes' => array(),
      'locked' => '0',
      'module' => 'field_collection',
      'settings' => array(
        'hide_blank_items' => 1,
        'path' => '',
      ),
      'translatable' => '0',
      'type' => 'field_collection',
    ),
    'field_instance' => array(
      'bundle' => 'questionnaire',
      'default_value' => NULL,
      'deleted' => '0',
      'description' => 'Add as many Questions as you want. Make sure your question tree always ends with a question marked as EndPoint.',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'field_collection',
          'settings' => array(
            'add' => 'Add',
            'delete' => 'Delete',
            'description' => TRUE,
            'edit' => 'Edit',
            'view_mode' => 'full',
          ),
          'type' => 'field_collection_view',
          'weight' => 3,
        ),
        'teaser' => array(
          'label' => 'above',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'field_questionnaire_questions',
      'label' => 'Questions',
      'required' => 0,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 0,
        'module' => 'field_collection',
        'settings' => array(),
        'type' => 'field_collection_embed',
        'weight' => '3',
      ),
    ),
  );

  return $fields;
}



/**
 * Implements hook_install().
 */
function questionnaire_install() {

  // Ensure the blog node type is available.
  node_types_rebuild();

/*
  $t = get_t();

  $question = array(
    'type' => 'questionnaire_question',
    'name' => $t('Question'),
    'base' => 'node_content',
    'description' => $t('This is a Question to be asked in a Questionnaire.'),
    'title_label' => $t('Question'),
	  'custom' => TRUE,
  );

  $content_type = node_type_set_defaults($question);


  node_type_save($content_type);
*/
  // Create all the fields we are adding to our content type.
  // http://api.drupal.org/api/function/field_create_field/7
  $instance['entity_type'] = 'node';
  $instance['bundle'] = 'questionnaire';
  foreach (_questionnaire_installed_fields() as $field) {
    field_create_field($field['field_config']);
    field_create_instance($field['field_instance']);
  }

  // Create all the instances for our fields.
  // http://api.drupal.org/api/function/field_create_instance/7
/*
 foreach (_questionnaire_installed_instances() as $instance) {
    watchdog ('questionnaire', 'create field ' . $instance['field_name']);
  }
*/
  variable_set('questionnaire_question_isquestionnaire_questionnaire', TRUE);
}


/**
 * Implements hook_uninstall().
 */
function questionnaire_uninstall() {
  // Gather all the example content that might have been created while this
  // module was enabled.  Simple selects still use db_query().
  // http://api.drupal.org/api/function/db_query/7
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'questionnaire'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }

  // Delete all the nodes at once
  // http://api.drupal.org/api/function/node_delete_multiple/7
  node_delete_multiple($nids);

  // Loop over each of the fields defined by this module and delete
  // all instances of the field, their data, and the field itself.
  // http://api.drupal.org/api/function/field_delete_field/7
  foreach (_questionnaire_installed_fields() as $field) {
    field_delete_field($field['field_config']['field_name']);
  }

  // Loop over any remaining field instances attached to the node_example
  // content type (such as the body field) and delete them individually.
  // http://api.drupal.org/api/function/field_delete_field/7
  $instances = field_info_instances('node', 'questionnaire');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }
  // Delete our content type
  // http://api.drupal.org/api/function/node_type_delete/7
  // node_type_delete('questionnaire');

  // Purge all field infromation
  // http://api.drupal.org/api/function/field_purge_batch/7
  field_purge_batch(1000);


 variable_del('questionnaire_question_isquestionnaire_questionnaire');

}



