Skip to content
question.install 9.16 KiB
Newer Older
codepoet's avatar
codepoet committed
<?php
// $Id$
/**
 * @file
 * install file for Question module.
 *
 * Most of the definition of the node type is in this module.
 */

/**
 * Implements hook_schema().
 */

function question_schema() {
  $schema['question_queue'] = array(
    'description' => 'Stores items in a question queue.',  
    'fields' => array(
      'qid' => array(
        'description' => 'The unique id assigned to this question.',
        'type' => 'serial', // auto-increment field
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'question' => array(
        'description' => 'The question text.',
        'type' => 'text',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('qid'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
codepoet's avatar
codepoet committed
function question_install() {
  // Get the name of our localization function for translation during install
  $t = get_t();

  // Do not allow Drupal to define the default permissions set for this node type.
  // We will control the access to question/answers manually using hook_node_access().
  variable_set('node_permissions_question', 0);
  
  // Define the Question node type.
  $question_node = array(
    'type' => 'question',
    'name' => $t('Question'),
    'base' => 'node_content',
    'description' => $t('This is the Question node type.'),
  );
  // Set any node type defaults not explicitly declared above.
  $content_type = node_type_set_defaults($question_node);

  // Save the node type
  node_type_save($content_type);

  // Load the instance of the body field for our content type
  $body_instance = field_info_instance('node', 'body', 'question');
  
  // Delete the body field instance
  field_delete_instance($body_instance);

  // Create all the fields for a Question node
  foreach (question_fields() as $field) {
    field_create_field($field);
  }

  // Attach an instance of each field to the Question node type
  foreach (question_field_instances() as $instance) {
    $instance['bundle'] = $question_node['type'];
    field_create_instance($instance);
/**
 * Returns an array of all fields created by Question module.
 *
 * This is inside its own function so that it can be used in both hook_install()
 * and hook_uninstall().
 */
function question_fields() {
  return array(
    'question_question' => array(
      'field_name' => 'question_question',
      'type'        => 'text_long',
    ),
    'question_answer' => array(
      'field_name'  => 'question_answer',
      'type'        => 'text_long',
    ),
  );
/**
 * Returns an array of all instances created by this content type.
 * The instance tells Drupal which widget to use when entering data into a field
 * and how to display it in different view modes.
 * 
 * This is inside its own function so that it can be used in both hook_install()
 * and hook_uninstall().
 * 
 * @see http://api.drupal.org/api/group/field_structs/7
 */
function question_field_instances() {
  $t = get_t();
  return array(
    'question' => array(
      'field_name'  => 'question_question',
      'label'       => $t('Question'),
      'widget'      => array(
        'type'    => 'text_textarea',
        'weight' => -4,
      ),
      'display' => array(
        'full' => array(
          'label' => 'above',
          'settings' => array(),
          'weight' => -4,
        ),
        'teaser' => array(
          'label' => 'inline',
          'settings' => array(),
          'weight' => -4,
        ),
      ),
    ),
    'answer' => array(
      'field_name'  => 'question_answer',
      'label'       => $t('Answer'),
      'widget'      => array(
        'type'    => 'text_textarea',
        'weight' => -3,
      ),
      'display' => array(
        'full' => array(
          'label' => 'above',
          'settings' => array(),
          'weight' => -3,
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'hidden',
          'settings' => array(),
          'weight' => -3,
        ),
      ),
    ),
  );
}

/**
 * Implementation of hook_uninstall().
 *
 */
codepoet's avatar
codepoet committed
function question_uninstall() {

  // Gather all the nodes created with the Question node type
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'question'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
  node_delete_multiple($nids); // Delete all the nodes at once

  // Loop over and delete all of the field instances attached to the Question node type
  // These will probably be the same as the fields defined by this module, but may not be
  $instances = field_info_instances('node', 'question');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }
  
  // Loop over and delete the fields defined by this module
  // This will remove all instances of the field from any nodetype to which they are attached
  foreach (array_keys(question_fields()) as $field) {
    field_delete_field($field);
  }

  // Delete the Question node type
  node_type_delete('question');

  // Purge all field information
  field_purge_batch(1000);
}

function question_update_1() {
  $ret = array();
  db_add_primary_key($ret, 'question_node', array('nid'));
  db_add_primary_key($ret, 'question_queue', array('qid'));
  return $ret;

function question_update_6001() {
  $ret = array();
  db_drop_primary_key($ret, 'question_queue');
  return $ret;
}

function question_update_6002() {
  $ret = array();
  db_change_field(
    $ret,           //Results
    'question_queue',         //Table name
    'qid',            //Old column name
    'qid',            //New column name
    array('type' => 'serial', 'not null' => TRUE),  //Field spec
    array('primary key' => array('qid'))    //Index spec
  );
  return $ret;
}

function question_update_7001() {
  
  // No longer used for access control
  variable_del('question_require_registered');
  
  // Rename the question_thanks variable to something more meaningful
  variable_set('question_ask_form_redirect', variable_get('question_thanks'));
  
  // This is the function used to upgrade taxonomy terms from D6 taxonomy_term_node table to D7 fields
  /*
   *   // Since we are upgrading from Drupal 6, we know that only
  // field_sql_storage.module will be enabled.
  $field = field_info_field($field['field_name']);
  $data_table = _field_sql_storage_tablename($field);
  $revision_table = _field_sql_storage_revision_tablename($field);
  $etid = _field_sql_storage_etid('node');
  $value_column = $field['field_name'] . '_value';
  $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'delta', $value_column);

  // This is a multi-pass update. On the first call we need to initialize some
  // variables.
  if (!isset($sandbox['total'])) {
    $sandbox['last'] = 0;
    $sandbox['count'] = 0;

    $query = db_select('taxonomy_term_node', 't');
    $sandbox['total'] = $query->countQuery()->execute()->fetchField();
    $found = (bool) $sandbox['total'];
  }
  else {
    // We do each pass in batches of 1000, this should result in a
    // maximum of 2000 insert queries each operation.
    $batch = 1000 + $sandbox['last'];

    // Query and save data for the current revision.
    $result = db_query_range('SELECT td.tid, tn.nid, td.weight, tn.vid, n2.type, n2.created, n2.sticky FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid INNER JOIN {node} n2 ON tn.nid = n2.nid INNER JOIN {node} n ON tn.vid = n.vid AND td.vid = :vocabulary_id ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid), $sandbox['last'], $batch);
    $deltas = array();
    foreach ($result as $record) {
      $found = TRUE;
      $sandbox['count'] += 1;
      // Start deltas from 0, and increment by one for each
      // term attached to a node.
      $deltas[$record->nid] = isset($deltas[$record->nid]) ? ++$deltas[$record->nid] : 0;
      $values = array($etid, $record->nid, $record->vid, $record->type, $deltas[$record->nid], $record->tid);
      db_insert($data_table)->fields($columns)->values($values)->execute();

      // Update the {taxonomy_index} table.
      db_insert('taxonomy_index')
        ->fields(array('nid', 'tid', 'sticky', 'created',))
        ->values(array($record->nid, $record->tid, $record->sticky, $record->created))
        ->execute();
    }

    // Query and save data for all revisions.
    $result = db_query('SELECT td.tid, tn.nid, td.weight, tn.vid, n.type FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid AND td.vid = :vocabulary_id INNER JOIN {node} n ON tn.nid = n.nid ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid), $sandbox['last'][$batch]);
    $deltas = array();
    foreach ($result as $record) {
      $found = TRUE;
      $sandbox['count'] += 1;
      // Start deltas at 0, and increment by one for each term attached to a revision.
      $deltas[$record->vid] = isset($deltas[$record->vid]) ? ++$deltas[$record->vid] : 0;
      $values = array($etid, $record->nid, $record->vid, $record->type, $deltas[$record->vid], $record->tid);
      db_insert($revision_table)->fields($columns)->values($values)->execute();
    }
    $sandbox['last'] = $batch;
  }
  if (!$found) {
   db_drop_table('taxonomy_term_node');
  }
  */
  
  // db_drop_table('question_node');
}