You are here

scale.install in Quiz 6.6

Sponsored by: Norwegian Centre for Telemedicine Code: falcon

Scale Install (a quiz question type)

File

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

/**
 * Sponsored by: Norwegian Centre for Telemedicine
 * Code: falcon
 * 
 * @file
 * Scale Install (a quiz question type)
 */

/**
 * Implementation of hook_install()
 */
function scale_install() {
  drupal_install_schema('scale');
  _scale_insert_collection(array(
    'Allways',
    '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',
    'unimportant',
  ));
}

/**
 * Implementation of hook_schema().
 */
function scale_schema() {

  // what answers a node has.
  $schema['quiz_scale_node_properties'] = array(
    '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(
    'fields' => array(
      'answer_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'result_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'question_nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'question_vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'result_id',
      'question_nid',
      'question_vid',
    ),
  );

  /*
   * Stores answer collection id.
   * Pos = 0 means the collection won't be available as a predefined collection set.
   * Pos > 0 is the collections position in the predefined collection list.
   */
  $schema['quiz_scale_answer_collection'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'for_all' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $schema['quiz_scale_user'] = array(
    '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(
    '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;
}
function scale_update_6400() {
  drupal_rebuild_theme_registry();
  return array();
}

/**
 * Implementation of hook_uninstall()
 */
function scale_uninstall() {
  drupal_uninstall_schema('scale');
  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."));
}
function _scale_insert_collection($answers) {
  $sql = 'INSERT INTO {quiz_scale_answer_collection}
         (for_all) VALUES(%d)';
  db_query($sql, 1);
  $collection_id = db_last_insert_id('{quiz_scale_answer_collection}', 'id');
  $sql = 'INSERT INTO {quiz_scale_user}
         (uid, answer_collection_id) VALUES(%d, %d)';
  db_query($sql, 1, $collection_id);
  for ($i = 0; $i < count($answers); $i++) {
    $sql = 'INSERT INTO {quiz_scale_answer}
     		(answer_collection_id, answer)
     		VALUES(%d, \'%s\')';
    db_query($sql, $collection_id, $answers[$i]);
  }
}

Functions

Namesort descending Description
scale_install Implementation of hook_install()
scale_schema Implementation of hook_schema().
scale_uninstall Implementation of hook_uninstall()
scale_update_6400
_scale_insert_collection