You are here

certificate.install in Certificate 8.3

Install the Certificate module.

File

certificate.install
View source
<?php

/**
 * Install the Certificate module.
 * @file
 */

/**
 * Implements hook_schema().
 */
function certificate_schema() {
  $schema['certificate_node_settings'] = array(
    'description' => 'Stores per-node template settings.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'orientation' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  $schema['certificate_node'] = array(
    'description' => 'Stores per-node certificate settings.',
    'fields' => array(
      'cnid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Unique identifier for the per-node / per-type template setting.',
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Course node ID.',
      ),
      'mapper' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The mapper to use.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The value to match on.',
      ),
      'template' => array(
        'type' => 'varchar',
        'not null' => TRUE,
        'default' => '',
        'length' => 256,
        'description' => 'The certificate to deliver on a match.',
      ),
    ),
    'primary key' => array(
      'cnid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
    ),
  );
  $schema['certificate'] = array(
    'description' => 'Stores certificate information.',
    'fields' => array(
      'cid' => array(
        'description' => 'Primary key',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'Certificate revision ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'title' => array(
        'description' => 'Title',
        'type' => 'varchar',
        'length' => '128',
      ),
      'name' => array(
        'description' => 'Machine Name',
        'type' => 'varchar',
        'length' => '128',
      ),
      'type' => array(
        'description' => 'Certificate type',
        'type' => 'varchar',
        'length' => '255',
      ),
      'orientation' => array(
        'description' => 'Certificate orientation',
        'type' => 'varchar',
        'length' => '255',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'cid',
    ),
    'foreign keys' => array(
      'certificate_revision' => array(
        'table' => 'certificate_revision',
        'columns' => array(
          'vid' => 'vid',
        ),
      ),
    ),
  );
  $schema['certificate_revision'] = array(
    'description' => 'Holds information about Certificate.',
    'fields' => array(
      'cid' => array(
        'description' => 'Certificate ID',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'Certificate revision ID',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'Title',
        'type' => 'varchar',
        'length' => '128',
      ),
      'name' => array(
        'description' => 'Machine Name',
        'type' => 'varchar',
        'length' => '128',
      ),
      'orientation' => array(
        'description' => 'Certificate orientation',
        'type' => 'varchar',
        'length' => '255',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the credit awarded was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the credit awarded was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'vid',
    ),
    'foreign keys' => array(
      'certificate' => array(
        'table' => 'certificate',
        'columns' => array(
          'cid' => 'cid',
        ),
      ),
    ),
  );
  $schema['certificate_snapshots'] = array(
    'description' => 'Stores snapshots of users certificate for a particular course.',
    'fields' => array(
      'csid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Unique identifier for the per-user / per-course certificate snapshot.',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => '{user}.uid of the u.',
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => '{node}.nid of the node.',
      ),
      'cid' => array(
        'type' => 'varchar',
        'not null' => TRUE,
        'default' => '',
        'length' => 256,
        'description' => 'Template name used for generation.',
      ),
      'date' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
        'description' => 'Date the certificate was generated.',
      ),
      'snapshot' => array(
        'description' => 'The generated content of the {certificate_snapshots}.',
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'csid',
    ),
  );
  $schema['certificate_type'] = array(
    'description' => 'Stores information about all defined certificate types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique certificate type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this certificate type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this certificate type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 *
 * This hook is called the first time the module is installed. Unless it is
 * explicitly uninstalled, disabling and re-enabling will not trigger this hook
 * a second time.
 */
function certificate_install() {

  // Set certificate variables.
  variable_set('node_options_certificate', array(
    0 => 'revision',
  ));
  variable_set('comment_certificate', 0);

  // Add a body field to Certificate entity
  $body = field_info_field('certificate_body');
  $body_inst = field_info_instance('certificate', 'certificate_body', 'certificate');
  if (empty($body)) {
    $field = array(
      'field_name' => 'certificate_body',
      'type' => 'text_with_summary',
      'entity_types' => array(
        'certificate',
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($body_inst)) {
    $instance = array(
      'field_name' => 'certificate_body',
      'entity_type' => 'certificate',
      'bundle' => 'certificate',
      'label' => 'Body',
      'widget' => array(
        'type' => 'text_textarea_with_summary',
        'weight' => 4,
      ),
      'settings' => array(
        'display_summary' => TRUE,
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'text_default',
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'text_summary_or_trimmed',
        ),
      ),
    );
    field_create_instance($instance);
  }

  // Default certificate bundle.
  $certificate_type = entity_create('certificate_type', array(
    'type' => 'certificate',
    'label' => t('Certificate'),
  ));
  $certificate_type
    ->save();
}

/**
 * Implements hook_uninstall().
 */
function certificate_uninstall() {
  cache_clear_all('certificate:*', 'cache', TRUE);
  db_delete('variable')
    ->condition('name', "certificate_%", "LIKE")
    ->execute();
  variable_del('node_options_certificate');
  variable_del('comment_certificate');
}

/**
 * Implements hook_requirements().
 */
function certificate_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    if (variable_get('print_pdf_pdf_tool', '') == '') {
      $requirements['certificate_pdf_tool'] = array(
        'title' => t('Certificate PDF generation tool must be selected'),
        'description' => t('Certificates will not display before selecting a PDF generation tool in !link.', array(
          '!link' => l('Printer, e-mail and PDF versions', 'admin/config/user-interface/print/pdf'),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}

/**
 * Add mapper column for more than one mapping per node.
 */
function certificate_update_6101() {
  $ret = array();
  db_add_field('certificate_node', 'mapper', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'description' => 'The mapper to use.',
    'default' => '',
  ));
  db_query("UPDATE {certificate_node} SET mapper = 'content_profile'");

  // hook_update_N() no longer returns a $ret array. Instead, return
  // nothing or a translated string indicating the update ran successfully.
  // See http://drupal.org/node/224333#update_sql.
  return t('TODO Add a descriptive string here to show in the UI.');
}

/**
 * Convert field groups.
 */
function certificate_update_6102() {
  $fieldgroups = variable_get('certificate_field_groups', array());
  $new_fieldgroups = array();
  foreach (array_keys($fieldgroups) as $title) {
    $new_fieldgroups[str_replace(' ', '_', $title)] = $title;
  }
  variable_set('certificate_field_groups', $new_fieldgroups);
}

/**
 * Change template field to allow signed integers, to allow for global and no
 * certificate map values.
 */
function certificate_update_7001() {
  db_change_field('certificate_node', 'template', 'template', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => FALSE,
    'default' => 0,
    'description' => 'The certificate node ID to deliver on a match.',
  ));
  return t('Changed template field to allow signed integers.');
}

/**
 * Add a cid column to the snapshots table.
 */
function certificate_update_7002() {
  db_add_field('certificate_snapshots', 'cid', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'Template ID used for generation.',
  ));
}

/**
 * Build the Certificate entity table
 * Add default fields
 */
function certificate_update_7003() {
  if (!module_exists('field')) {
    throw new DrupalUpdateException('Delaying update - enable field module.');
  }
  if (!db_table_exists('certificate')) {
    $schema['certificate'] = array(
      'description' => 'Stores certificate information.',
      'fields' => array(
        'cid' => array(
          'description' => 'Primary key',
          'type' => 'serial',
          'not null' => TRUE,
        ),
        'vid' => array(
          'description' => 'Certificate revision ID',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
        'title' => array(
          'description' => 'Title',
          'type' => 'varchar',
          'length' => '128',
        ),
        'name' => array(
          'description' => 'Machine Name',
          'type' => 'varchar',
          'length' => '128',
        ),
        'type' => array(
          'description' => 'Certificate type',
          'type' => 'varchar',
          'length' => '255',
        ),
        'orientation' => array(
          'description' => 'Certificate orientation',
          'type' => 'varchar',
          'length' => '255',
        ),
        'status' => array(
          'type' => 'int',
          'not null' => TRUE,
          // Set the default to ENTITY_CUSTOM without using the constant as it is
          // not safe to use it at this point.
          'default' => 0x1,
          'size' => 'tiny',
          'description' => 'The exportable status of the entity.',
        ),
        'module' => array(
          'description' => 'The name of the providing module if the entity has been defined in code.',
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
      ),
      'primary key' => array(
        'cid',
      ),
      'foreign keys' => array(
        'certificate_revision' => array(
          'table' => 'certificate_revision',
          'columns' => array(
            'vid' => 'vid',
          ),
        ),
      ),
    );
    db_create_table('certificate', $schema['certificate']);
  }
  if (!db_table_exists('certificate_revision')) {
    $schema['certificate_revision'] = array(
      'description' => 'Holds information about Certificate.',
      'fields' => array(
        'cid' => array(
          'description' => 'Certificate ID',
          'type' => 'int',
          'not null' => TRUE,
        ),
        'vid' => array(
          'description' => 'Certificate revision ID',
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'title' => array(
          'description' => 'Title',
          'type' => 'varchar',
          'length' => '128',
        ),
        'name' => array(
          'description' => 'Machine Name',
          'type' => 'varchar',
          'length' => '128',
        ),
        'type' => array(
          'description' => 'Certificate type',
          'type' => 'varchar',
          'length' => '255',
        ),
        'orientation' => array(
          'description' => 'Certificate orientation',
          'type' => 'varchar',
          'length' => '255',
        ),
        'created' => array(
          'description' => 'The Unix timestamp when the credit awarded was created.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ),
        'changed' => array(
          'description' => 'The Unix timestamp when the credit awarded was most recently saved.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
      'primary key' => array(
        'vid',
      ),
      'foreign keys' => array(
        'certificate' => array(
          'table' => 'certificate',
          'columns' => array(
            'cid' => 'cid',
          ),
        ),
      ),
    );
    db_create_table('certificate_revision', $schema['certificate_revision']);
  }

  // Add a body field to Certificate entity
  $body = field_info_field('certificate_body');
  $body_inst = field_info_instance('certificate', 'certificate_body', 'certificate');
  if (empty($body)) {
    $field = array(
      'field_name' => 'certificate_body',
      'type' => 'text_with_summary',
      'entity_types' => array(
        'certificate',
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($body_inst)) {
    $instance = array(
      'field_name' => 'certificate_body',
      'entity_type' => 'certificate',
      'bundle' => 'certificate',
      'label' => 'Body',
      'widget' => array(
        'type' => 'text_textarea_with_summary',
        'weight' => 4,
      ),
      'settings' => array(
        'display_summary' => TRUE,
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'text_default',
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'text_summary_or_trimmed',
        ),
      ),
    );
    field_create_instance($instance);
  }
}

/**
 * Change mapping field into a varchar for machine name mapping.
 */
function certificate_update_7004() {
  db_change_field('certificate_node', 'template', 'template', array(
    'type' => 'varchar',
    'not null' => TRUE,
    'default' => '',
    'length' => 256,
    'description' => 'The certificate to deliver on a match.',
  ));
  db_change_field('certificate_snapshots', 'cid', 'cid', array(
    'type' => 'varchar',
    'not null' => TRUE,
    'default' => '',
    'length' => 256,
    'description' => 'Template name used for generation.',
  ));
}

/**
 * Migrate node certificates to entities.
 */
function certificate_update_7005() {
  module_enable(array(
    'entity',
  ));
  $nodes = node_load_multiple(array(), array(
    'type' => 'certificate',
  ));
  $certificate_settings = db_select('certificate_node_settings', 'c')
    ->fields('c')
    ->execute()
    ->fetchAllKeyed(0, 1);
  foreach ($nodes as $node) {

    /* @var $certificate Entity */
    $certificate = entity_create('certificate', array(
      'cid' => $node->nid,
      'title' => $node->title,
      'name' => $node->nid,
      'certificate_body' => $node->body,
      'orientation' => $certificate_settings[$node->nid],
      'type' => 'certificate',
    ));
    $certificate
      ->save();
    node_delete($node->nid);
  }
  return t('Migrated certificates. New machine names are the original node IDs.');
}

/**
 * Add certificate types.
 */
function certificate_update_7006() {
  $schema = array();
  $schema['certificate_type'] = array(
    'description' => 'Stores information about all defined certificate types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique certificate type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this certificate type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this certificate type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x1,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  db_create_table('certificate_type', $schema['certificate_type']);
  drupal_get_schema(NULL, TRUE);
  $certificate_type = array(
    'type' => 'certificate',
    'label' => t('Certificate'),
  );
  drupal_write_record('certificate_type', $certificate_type);
  db_update('certificate')
    ->fields(array(
    'type' => 'certificate',
  ))
    ->execute();
  db_drop_field('certificate_revision', 'type');
  drupal_get_schema(NULL, TRUE);
  entity_info_cache_clear();
}

/**
 * Add indexes for sites with many certificate templates and mappings.
 */
function certificate_update_7007() {
  if (!db_index_exists('certificate_node', 'nid')) {
    db_add_index('certificate_node', 'nid', array(
      'nid',
    ));
  }
}

Functions

Namesort descending Description
certificate_install Implements hook_install().
certificate_requirements Implements hook_requirements().
certificate_schema Implements hook_schema().
certificate_uninstall Implements hook_uninstall().
certificate_update_6101 Add mapper column for more than one mapping per node.
certificate_update_6102 Convert field groups.
certificate_update_7001 Change template field to allow signed integers, to allow for global and no certificate map values.
certificate_update_7002 Add a cid column to the snapshots table.
certificate_update_7003 Build the Certificate entity table Add default fields
certificate_update_7004 Change mapping field into a varchar for machine name mapping.
certificate_update_7005 Migrate node certificates to entities.
certificate_update_7006 Add certificate types.
certificate_update_7007 Add indexes for sites with many certificate templates and mappings.