You are here

scale.install in Quiz 7.5

Scale questions install file.

File

question_types/scale/scale.install
View source
<?php

/**
 * @file
 * Scale questions install file.
 */

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

  // Add body field to scale node.
  quiz_question_add_body_field('scale');
  _scale_insert_collection(array(
    'Always',
    'Very often',
    'Some times',
    'Rarely',
    'Very rarely',
    'Never',
  ));
  _scale_insert_collection(array(
    'Excellent',
    'Very good',
    'Good',
    'Ok',
    'Poor',
    'Very poor',
  ));
  _scale_insert_collection(array(
    'Totally agree',
    'Agree',
    'Not sure',
    'Disagree',
    'Totally disagree',
  ));
  _scale_insert_collection(array(
    'Very important',
    'Important',
    'Moderately important',
    'Less important',
    'Least important',
  ));
  variable_set('node_options_scale', array(
    'status',
  ));
}

/**
 * Implements hook_schema().
 */
function scale_schema() {
  $schema['quiz_scale_node_properties'] = array(
    'description' => 'Properties specific to this question type. Holds information about what answer collection this node uses',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'answer_collection_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'nid',
      'vid',
    ),
  );

  // Stores the users answers to a question.
  $schema['quiz_scale_user_answers'] = array(
    'description' => 'Store the users answers',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'answer_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'result_answer_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'result_answer_id' => array(
        'result_answer_id',
      ),
    ),
  );
  $schema['quiz_scale_answer_collection'] = array(
    'description' => 'Answer collection ids and properties',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'for_all' => array(
        'description' => '1 for global presets, 0 otherwise',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $schema['quiz_scale_user'] = array(
    'description' => 'User settings, store what answer collections the user have as presets',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'answer_collection_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'uid',
      'answer_collection_id',
    ),
  );

  /*
   * Holds each answer in the answer_collections.
   */
  $schema['quiz_scale_answer'] = array(
    'description' => 'Holds all the possible answers and what answer collections they belong to',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'answer_collection_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'answer' => array(
        'type' => 'text',
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function scale_uninstall() {
  variable_del('scale_max_num_of_alts');
  cache_clear_all('variables', 'cache');
  drupal_set_message(t('The Scale module has been uninstalled. Scale nodes may still exist, but they will not function properly.'));
}

/**
 * Inserts answer collections into the database.
 *
 * @param array $answers
 *   Array of answers to include in the answer collection.
 */
function _scale_insert_collection($answers) {

  // Save the collection as a global preset.
  $collection_id = db_insert('quiz_scale_answer_collection')
    ->fields(array(
    'for_all' => 1,
  ))
    ->execute();

  // Save the collections as a preset collection for user 1.
  db_insert('quiz_scale_user')
    ->fields(array(
    'answer_collection_id' => $collection_id,
    'uid' => 1,
  ))
    ->execute();
  $insert = db_insert('quiz_scale_answer')
    ->fields(array(
    'answer_collection_id',
    'answer',
  ));
  $values = array();

  // Save the answers belonging to the collection.
  for ($i = 0; $i < count($answers); $i++) {
    $values = array(
      'answer_collection_id' => $collection_id,
      'answer' => $answers[$i],
    );
    $insert
      ->values($values);
  }
  $insert
    ->execute();
}

/**
 * Normalize data storage.
 */
function scale_update_7500() {
  db_drop_primary_key('quiz_scale_user_answers');
  db_add_field('quiz_scale_user_answers', 'result_answer_id', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => FALSE,
  ));
  db_query("UPDATE {quiz_scale_user_answers} qtua\n    INNER JOIN {quiz_node_results_answers} qnra ON (qtua.question_nid = qnra.question_nid\n    AND qtua.question_vid = qnra.question_vid\n    AND qtua.result_id = qnra.result_id)\n    SET qtua.result_answer_id = qnra.result_answer_id");
  db_drop_field('quiz_scale_user_answers', 'result_id');
  db_drop_field('quiz_scale_user_answers', 'question_nid');
  db_drop_field('quiz_scale_user_answers', 'question_vid');
  db_add_unique_key('quiz_scale_user_answers', 'result_answer_id', array(
    'result_answer_id',
  ));
}
function scale_update_7501() {
  db_drop_primary_key('quiz_scale_user_answers');
  db_add_field('quiz_scale_user_answers', 'id', array(
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ), array(
    'primary key' => array(
      'id',
    ),
  ));
}

Functions

Namesort descending Description
scale_install Implements hook_install().
scale_schema Implements hook_schema().
scale_uninstall Implements hook_uninstall().
scale_update_7500 Normalize data storage.
scale_update_7501
_scale_insert_collection Inserts answer collections into the database.