You are here

course.install in Course 7.2

course.install Install and update functions for Courses.

File

course.install
View source
<?php

/**
 * @file course.install
 * Install and update functions for Courses.
 */

/**
 * Implements hook_install().
 *
 * Install a default content type, set some defaults.
 */
function course_install() {
  module_load_include('module', 'node', 'node');
  $type = array(
    'type' => 'course',
    'name' => 'Course',
    'module' => 'node',
    'has_title' => 1,
    'title_label' => 'Title',
    'has_body' => 1,
    'body_label' => 'Description',
    'description' => 'A <em>course</em> containing Drupal learning objects.',
    'custom' => TRUE,
    'modified' => TRUE,
    'locked' => FALSE,
    'help' => '',
    'min_word_count' => '',
    'base' => 'node_content',
  );
  $type = (object) node_type_set_defaults($type);
  node_type_save($type);
  node_types_rebuild();
  menu_rebuild();
  variable_set('course_use_course', 1);
  $enrollment_type = entity_create('course_enrollment_type', array(
    'type' => 'course_enrollment',
    'label' => t('Course'),
  ));
  $enrollment_type
    ->save();
  variable_set('course_default_enrollment_type', 'course_enrollment');
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array(
    'access course',
  ));
}

/**
 * Implements hook_uninstall().
 */
function course_uninstall() {
}

/**
 * Implements hook_schema().
 */
function course_schema() {
  $schema = array();
  $schema['course_report'] = array(
    'description' => "Stores users' course reports.",
    'fields' => array(
      'crid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Course report record ID.',
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Drupal course node.',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Drupal user ID.',
      ),
      'date_completed' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
        'description' => 'Date completed.',
      ),
      'updated' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Date this record was updated.',
      ),
      'grade_result' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => FALSE,
        'description' => 'Course grade received.',
      ),
      'section' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "Key of current section.",
      ),
      'section_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "Name of current section.",
      ),
      'complete' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Flag to mark this course as complete.',
      ),
      'data' => array(
        'serialize' => TRUE,
        'type' => 'text',
        'description' => 'Extra serialized course report data.',
        'size' => 'medium',
      ),
      'coid' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Identifier for the course object.',
      ),
    ),
    'primary key' => array(
      'crid',
    ),
    'unique keys' => array(
      'uk' => array(
        'nid',
        'uid',
      ),
    ),
    'indexes' => array(
      'date_completed' => array(
        'date_completed',
      ),
    ),
  );
  $schema['course_enrollment'] = array(
    'description' => 'Stores enrollment records.',
    'fields' => array(
      'eid' => array(
        'description' => 'Primary Key: The eid of the enrollment.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The nid of the course node',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The uid of the user',
      ),
      'enrollmenttype' => array(
        'type' => 'varchar',
        'length' => '128',
        'not null' => FALSE,
        'default' => '',
        'description' => 'The type of enrollment, if applicable',
      ),
      'status' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The enrollment status.',
      ),
      'created' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The time this enrollment was created.',
      ),
      'timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'The time the user started the course.',
      ),
      'enroll_end' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'Date enrollment ends.',
      ),
      'code' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The access code used to enroll.',
      ),
      'user_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The user type while in the context of this enrollment.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this enrollment type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'serialize' => TRUE,
        'type' => 'text',
        'description' => "Extra serialized enrollment data.",
        'size' => 'medium',
      ),
      'updated' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Date this record was updated.',
      ),
    ),
    'primary key' => array(
      'eid',
    ),
    'unique keys' => array(
      'nid_uid' => array(
        'nid',
        'uid',
      ),
    ),
  );
  $schema['course_node'] = array(
    'description' => 'Stores course node settings.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Course node ID.',
      ),
      'outline' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Course outline display handler.',
      ),
      'credits' => array(
        'type' => 'numeric',
        'description' => 'Course credit.',
        'precision' => 6,
        'scale' => 2,
      ),
      'open' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'Date course opens.',
      ),
      'close' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'Date course closes.',
      ),
      'duration' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Amount of time a user has to access this course.',
      ),
      'external_id' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'External ID of the course.',
      ),
      'enrollment_type' => array(
        'description' => 'The machine-readable name of this enrollment type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  $schema['course_outline'] = array(
    'description' => "Stores users' course reports",
    'fields' => array(
      'coid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Unique identifier for the course object record.',
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'nid.',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The implementing module name (course_quiz etc)",
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The item title",
      ),
      'object_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The course object key as defined by hook_course_handlers()",
      ),
      'enabled' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'is this item enabled?',
      ),
      'info' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The item info",
      ),
      'instance' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The object instance',
      ),
      'required' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'is this item required?',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => FALSE,
        'default' => 0,
        'description' => 'Order in course outline.',
      ),
      'hidden' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Visibility in course utline',
      ),
      'duration' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Amount of time in seconds a user has to access this object.',
      ),
      'uuid' => array(
        'type' => 'varchar',
        'length' => 36,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The Universally Unique Identifier.',
      ),
      'data' => array(
        'description' => 'Extra serialized course object data.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'coid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
      'instance' => array(
        'instance',
      ),
    ),
    'unique keys' => array(
      'uuid' => array(
        'uuid',
      ),
    ),
  );
  $schema['course_outline_fulfillment'] = array(
    'description' => "Stores users' course reports",
    'fields' => array(
      'cofid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Unique identifier for the course object fufillment record.',
      ),
      'coid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Identifier for the course object.',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'uid of {user}.',
      ),
      'complete' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Is this item complete?',
      ),
      'grade_result' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => FALSE,
        'description' => 'Object grade received.',
      ),
      'date_started' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
        'default' => NULL,
        'description' => 'Date object was started.',
      ),
      'date_completed' => array(
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
        'default' => NULL,
        'description' => 'Date object was completed.',
      ),
      'info' => array(
        'type' => 'text',
        'serialize' => TRUE,
        'description' => 'Extra serialized course object fulfillment enrollment data.',
      ),
      'uuid' => array(
        'type' => 'varchar',
        'length' => 36,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The Universally Unique Identifier.',
      ),
      'instance' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The fulfillment instance',
      ),
    ),
    'primary key' => array(
      'cofid',
    ),
    'unique keys' => array(
      'coid_uid' => array(
        'coid',
        'uid',
      ),
      'uuid' => array(
        'uuid',
      ),
    ),
    'indexes' => array(
      'coid' => array(
        'coid',
      ),
      'date_completed' => array(
        'date_completed',
      ),
      'instance' => array(
        'instance',
      ),
    ),
  );
  $schema['course_enrollment_type'] = array(
    'description' => 'Stores information about all defined profile types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique enrollment type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this enrollment type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this enrollment 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;
}

/**
 * Delete enrollments for users who don't exist anymore.
 */
function course_update_6121() {
  $ret = array();
  $sql = "DELETE ce.* FROM {course_enrolment} ce LEFT JOIN {users} u ON (ce.uid = u.uid) WHERE u.uid IS NULL";
  $ret[] = array();
  $ret[] = array(
    'success' => TRUE,
    'query' => db_affected_rows() . ' old enrollments deleted',
  );

  // 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.');
}

/**
 * Migrate and remove old fields that won't be used anymore.
 */
function course_update_6122() {
  $ret = array();

  // Migrate any passing grade to serialized storage.
  $sql = "SELECT * FROM {course_outline}";
  $result = db_query($sql);
  while ($row = $result
    ->fetch()) {
    $row->data = unserialize($row->data);
    if (!is_array($row->data)) {
      $row->data = array();
    }
    $row->data['passing_grade'] = $row->passing_grade;
    $row->data = serialize($row->data);
    db_update('course_outline')
      ->fields(array(
      'data' => $row->data,
    ))
      ->condition('snid', $row->snid)
      ->execute();
  }
  $deletes = array(
    array(
      'course_node',
      'outline_custom_titles',
    ),
    array(
      'course_outline',
      'graded',
    ),
    array(
      'course_outline',
      'passing_grade',
    ),
    array(
      'course_outline',
      'payment_required',
    ),
  );
  foreach ($deletes as $delete) {
    $table = $delete[0];
    $column = $delete[1];
    if (db_field_exists($table, $column)) {
      db_drop_field($table, $column);
    }
  }

  // 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.');
}

/**
 * Add date started field to to fulfillment, add duration to course outline
 * object, remove type field.
 */
function course_update_6124() {
  $ret = array();
  if (!db_field_exists('course_outline_fulfillment', 'date_started')) {
    db_add_field('course_outline_fulfillment', 'date_started', array(
      'type' => 'int',
      'not null' => FALSE,
      'unsigned' => TRUE,
      'default' => NULL,
      'description' => 'Date object was started.',
    ));
  }
  db_add_field('course_outline', 'duration', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Amount of time in seconds a user has to access this object.',
  ));
  db_drop_field('course_node', 'type');

  // 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.');
}

/**
 * Add indexes to course report table.
 */
function course_update_6125() {
  $ret = array();
  db_add_index('course_report', 'nid', array(
    'nid',
  ));
  db_add_index('course_report', 'uid', array(
    'uid',
  ));

  // 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.');
}

/**
 * Delete old fulfillment duplicates. Add indexes to outline and fulfillment
 * table. Change grade_result to signed.
 */
function course_update_6126() {
  $ret = array();

  // This does the same thing as the code it replaced except won't run out of
  // memory. @kludge Does not work with MySQL 5.7+.
  db_query('alter ignore table {course_outline_fulfillment} add unique snid_uid (snid,uid)');
  db_change_field('course_outline_fulfillment', 'grade_result', 'grade_result', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => FALSE,
    'default' => 0,
    'description' => 'grade_result',
  ));

  // 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.');
}

/**
 * Change primary key in course_report to `crid` (course report ID).
 */
function course_update_6127() {
  $ret = array();

  // Remove serialness of field.
  db_change_field('course_report', 'id', 'id', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ));

  // Drop primary key.
  db_drop_primary_key('course_report');

  // Rename field, add back serialness.
  db_change_field('course_report', 'id', 'crid', array(
    'type' => 'serial',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ), array(
    'primary key' => array(
      'crid',
    ),
  ));

  // 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.');
}

/**
 * Change open and close to integer.
 */
function course_update_6128() {
  $ret = array();
  db_change_field('course_node', 'open', 'open', array(
    'type' => 'int',
    'unsigned' => TRUE,
  ));
  db_change_field('course_node', 'close', 'close', array(
    'type' => 'int',
    'unsigned' => TRUE,
  ));

  // 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.');
}

/**
 * Remove old attendance column. Using course objects now.
 */
function course_update_6129() {
  $ret = array();
  $ret = db_drop_field('course_node', 'attendance');

  // 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.');
}

/**
 * Add `hidden` column to course_outline, add `created` column to
 * course_enrollment. Add serialized `data` column to course_outline.
 */
function course_update_6130() {
  $ret = array();
  if (!db_field_exists('course_outline', 'hidden')) {
    db_add_field('course_outline', 'hidden', array(
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
      'unsigned' => TRUE,
      'default' => 0,
    ));
  }
  if (!db_field_exists('course_enrolment', 'created')) {
    db_add_field('course_enrolment', 'created', array(
      'type' => 'int',
      'not null' => TRUE,
      'unsigned' => TRUE,
      'default' => 0,
    ));
  }
  if (!db_field_exists('course_outline', 'data')) {
    db_add_field('course_outline', 'data', array(
      'type' => 'text',
      'serialize' => TRUE,
    ));
  }

  // 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.');
}

/**
 * Change fulfillment info to serialized.
 */
function course_update_6131() {
  $ret = array();
  db_change_field('course_outline_fulfillment', 'info', 'info', array(
    'type' => 'text',
  ));

  // 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.');
}

/**
 * Remove old lms settings. Add new course outline display handler settings.
 */
function course_update_6132() {
  $ret = array();

  // Delete old lms variables.
  $types = node_type_get_names();
  foreach (array_keys($types) as $type) {
    variable_del("default_lms_{$type}");
  }

  // Migrate old lms column.
  db_change_field('course_node', 'lms', 'outline', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // 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.');
}

/**
 * Change schema, so all literal date fields can be NULL with no default.
 */
function course_update_6133() {
  $change = array();
  $change['course_enrolment'][] = 'created';
  $change['course_enrolment'][] = 'timestamp';
  $change['course_enrolment'][] = 'enrol_end';
  $change['course_node'][] = 'open';
  $change['course_node'][] = 'close';
  $change['course_node'][] = 'duration';
  $type = array(
    'type' => 'int',
    'not null' => FALSE,
    'unsigned' => TRUE,
    'default' => NULL,
  );
  foreach ($change as $table => $fields) {
    foreach ($fields as $field) {

      // Change schema definition.
      db_change_field($table, $field, $field, $type);

      // Change values to NULL where 0 was stored.
      $sql = "UPDATE {{$table}} SET {$field} = NULL WHERE {$field} = 0";
      db_query($sql);
    }
  }
  return t('Let date fields be NULL.');
}

/**
 * Give course objects and fulfillments UUIDs.
 */
function course_update_uuid() {
  $uuid_field = array(
    'type' => 'varchar',
    'length' => 36,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The Universally Unique Identifier.',
  );
  db_add_field('course_outline', 'uuid', $uuid_field);
  db_add_field('course_outline_fulfillment', 'uuid', $uuid_field);
  module_enable(array(
    'uuid',
  ));
  $sql = "SELECT * FROM {course_outline}";
  $result = db_query($sql);
  while ($row = $result
    ->fetch()) {
    db_query("update {course_outline} set uuid = :uuid where coid = :coid", array(
      ':uuid' => uuid_generate(),
      ':coid' => $row->coid,
    ));
  }
  $sql = "SELECT * FROM {course_outline_fulfillment}";
  $result = db_query($sql);
  while ($row = $result
    ->fetch()) {
    db_query("update {course_outline_fulfillment} set uuid = :uuid where cofid = :cofid", array(
      ':uuid' => uuid_generate(),
      ':cofid' => $row->cofid,
    ));
  }
  db_add_unique_key('course_outline', 'uuid', array(
    'uuid',
  ));
  db_add_unique_key('course_outline_fulfillment', 'uuid', array(
    'uuid',
  ));
  return t('Gave course objects and fulfillments UUIDs.');
}

/**
 * Naming conventions update.
 */
function course_update_6135() {
  $ret = array();

  // Change primary keys to int (so we can drop the primary key).
  db_change_field('course_outline', 'snid', 'snid', array(
    'type' => 'int',
  ));
  db_change_field('course_outline_fulfillment', 'sfid', 'sfid', array(
    'type' => 'int',
  ));

  // Drop old PK.
  db_drop_primary_key('course_outline');
  db_drop_primary_key('course_outline_fulfillment');

  // Drop affected indexes.
  db_drop_unique_key('course_outline_fulfillment', 'snid_uid');
  db_drop_index('course_outline_fulfillment', 'snid');

  // Rename columns, add back PK.
  db_change_field('course_outline', 'snid', 'coid', array(
    'type' => 'serial',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ), array(
    'primary key' => array(
      'coid',
    ),
  ));
  db_change_field('course_outline_fulfillment', 'sfid', 'cofid', array(
    'type' => 'serial',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ), array(
    'primary key' => array(
      'cofid',
    ),
  ));
  db_change_field('course_outline_fulfillment', 'snid', 'coid', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
  ));

  // Change requirement_type/requirement_component fields.
  db_change_field('course_outline', 'requirement_type', 'module', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
  db_change_field('course_outline', 'requirement_component', 'object_type', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // Add keys back.
  db_add_unique_key('course_outline_fulfillment', 'coid_uid', array(
    'coid',
    'uid',
  ));
  db_add_index('course_outline_fulfillment', 'coid', array(
    'coid',
  ));

  // Update crid to match schema.
  db_change_field('course_report', 'crid', 'crid', array(
    'type' => 'serial',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ));
  course_update_uuid();
  return t('Updated schema naming conventions.');
}

/**
 * Remove duplicate field that was never used.
 */
function course_update_6136() {
  $ret = array();
  if (db_field_exists('course_outline', 'config')) {
    db_drop_field('course_outline', 'config');
  }
  return t('Remove duplicate field that was never used.');
}

/**
 * Rename schema instances of "enrol" to "enroll".
 */
function course_update_7000() {

  // Set all enrol_end to 0 to fix truncation warning.
  db_query('update {course_enrolment} set enrol_end = 0 where enrol_end is null');
  db_change_field('course_enrolment', 'enrol_end', 'enroll_end', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Date enrollment ends.',
  ));
  db_rename_table('course_enrolment', 'course_enrollment');
  return t('Renamed schema instances of "enrol" to "enroll".');
}

/**
 * Migrate old duration value.
 */
function course_update_7001() {
  db_update('course_node')
    ->expression('duration', 'duration*86400')
    ->execute();
  return t('Migrated old duration values.');
}

/**
 * Change grade_result field to NULL to allow for empty grades.
 */
function course_update_7002() {
  db_change_field('course_outline_fulfillment', 'grade_result', 'grade_result', array(
    'type' => 'int',
    'not null' => FALSE,
    'unsigned' => TRUE,
    'description' => 'Course grade received.',
  ));
  return t('Changed grade_result field to NULL to allow for empty grades.');
}

/**
 * Install the new course_enrollment_type table for 7.x-1.3.
 */
function course_update_7130() {
  db_create_table('course_enrollment_type', array(
    'description' => 'Stores information about all defined profile types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique enrollment type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this enrollment type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this enrollment 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_add_field('course_enrollment', 'type', array(
    'description' => 'The machine-readable name of this enrollment type.',
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
  ));
  db_add_field('course_node', 'enrollment_type', array(
    'description' => 'The machine-readable name of this enrollment type.',
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => '',
  ));
  drupal_get_schema(NULL, TRUE);
  $course_enrollment_type = array(
    'type' => 'course_enrollment',
    'label' => t('Course'),
  );
  drupal_write_record('course_enrollment_type', $course_enrollment_type);
  variable_set('course_default_enrollment_type', 'course_enrollment');
  db_update('course_node')
    ->fields(array(
    'enrollment_type' => 'course_enrollment',
  ))
    ->execute();
  db_update('course_enrollment')
    ->fields(array(
    'type' => 'course_enrollment',
  ))
    ->execute();
  return t('Created tables and updated data for course enrollment bundles.');
}

/**
 * Add a coid column to the course report table.
 */
function course_update_7131() {
  db_add_field('course_report', 'coid', array(
    'type' => 'int',
    'not null' => FALSE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'Identifier for the course object.',
  ));
  return t('Added coid field to course_report table.');
}

/**
 * Fix grade_result fields.
 */
function course_update_7132() {

  // Fix grade_result columns.
  db_change_field('course_report', 'grade_result', 'grade_result', array(
    'type' => 'int',
    'not null' => FALSE,
    'unsigned' => FALSE,
    'description' => 'Course grade received.',
  ));
  db_change_field('course_outline_fulfillment', 'grade_result', 'grade_result', array(
    'type' => 'int',
    'not null' => FALSE,
    'unsigned' => FALSE,
    'description' => 'Object grade received.',
  ));
  return t('Fixed grade_result fields.');
}

/**
 * Add a serialized data field on enrollments.
 */
function course_update_7133() {
  db_add_field('course_enrollment', 'data', array(
    'serialize' => TRUE,
    'type' => 'text',
    'description' => 'Extra serialized enrollment data.',
    'size' => 'medium',
  ));
  return t('Added data field on enrollments.');
}

/**
 * Clear registry.
 */
function course_update_7134() {
  registry_rebuild();
}

/**
 * Update format for grade-based access
 */
function course_update_7135() {
  $sql = "SELECT * FROM {course_outline}";
  $result = db_query($sql);
  while ($row = $result
    ->fetch()) {
    $data = unserialize($row->data);
    if (isset($data['plugins']['access']['grade']['course_grade']) && (!empty($data['plugins']['access']['grade']['course_grade']) || $data['plugins']['access']['grade']['course_grade'] == '0')) {

      // Get old value
      $course_grade = $data['plugins']['access']['grade']['course_grade'];

      // Remove old value
      unset($data['plugins']['access']['grade']['course_grade']);

      // Set new format
      $data['plugins']['access']['grade']['course_grade_range'] = array(
        'low' => $course_grade,
        'high' => 100,
      );
      $row->data = serialize($data);

      // Save
      db_update('course_outline')
        ->fields(array(
        'data' => $row->data,
      ))
        ->condition('coid', $row->coid)
        ->execute();
    }
  }
  return t('Updated format for grade-based access.');
}

/**
 * Add indexes for completion dates.
 */
function course_update_7136() {
  if (!db_index_exists('course_report', 'date_completed')) {
    db_add_index('course_report', 'date_completed', array(
      'date_completed',
    ));
  }
  if (!db_index_exists('course_outline_fulfillment', 'date_completed')) {
    db_add_index('course_outline_fulfillment', 'date_completed', array(
      'date_completed',
    ));
  }
}

/**
 * Grant the "access course" permission to the authenticated user. This
 * permission was not checked before so we are adding it for a clean upgrade
 * path.
 */
function course_update_7137() {
  drupal_set_message('The "access course" permission has been added to the authenticated user role because it was not checked in previous releases of Course.', 'warning');
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array(
    'access course',
  ));
}

/**
 * Add an index on "instance".
 */
function course_update_7138() {
  if (!db_index_exists('course_outline', 'instance')) {
    db_add_index('course_outline', 'instance', array(
      'instance',
    ));
  }
  return t('Added index on "instance" column.');
}

/**
 * Add a new instance to fulfillments. Change existing instance column.
 */
function course_update_7139() {
  db_add_field('course_outline_fulfillment', 'instance', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'description' => 'The object instance',
  ));
  db_change_field('course_outline', 'instance', 'instance', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'description' => 'The fulfillment instance',
  ));
  return t('Added index on "instance" column. Changed existing instance field to a varchar for 3rd party systems.');
}

/**
 * Add an index on fulfillment "instance".
 */
function course_update_7140() {
  if (!db_index_exists('course_outline_fulfillment', 'instance')) {
    db_add_index('course_outline_fulfillment', 'instance', array(
      'instance',
    ));
  }
  return t('Added index on fulfillment "instance" column.');
}

/**
 * Remove duplicate indexes.
 */
function course_update_7141() {
  db_drop_index('course_report', 'nid');
  db_drop_index('course_report', 'uid');
  db_drop_index('course_enrollment', 'nid');
  db_drop_index('course_enrollment', 'uid');
}

/**
 * Update some fields to boolean.
 */
function course_update_7142() {
  db_change_field('course_report', 'complete', 'complete', array(
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'Flag to mark this course as complete.',
  ));
  db_change_field('course_outline', 'required', 'required', array(
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'is this item required?',
  ));
}

/**
 * Rebuild registry.
 */
function course_update_7143() {
}

/**
 * Update empty "updated" dates.
 */
function course_update_7144() {
  db_add_field('course_enrollment', 'updated', array(
    'type' => 'int',
    'not null' => FALSE,
    'unsigned' => TRUE,
    'default' => NULL,
    'description' => 'Date this record was updated.',
  ));
  db_query('UPDATE {course_enrollment} SET updated = created');
  db_query('UPDATE {course_report} cr INNER JOIN {course_enrollment} ce ON (ce.uid = cr.uid AND ce.nid = cr.nid) SET cr.updated = COALESCE(cr.date_completed, ce.timestamp, ce.created) WHERE cr.updated IS NULL');
}

/**
 * Update certain dates to be NULLable.
 */
function course_update_7145() {
  db_change_field('course_enrollment', 'timestamp', 'timestamp', [
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => FALSE,
    'description' => 'The time the user started the course.',
  ]);
  db_query('UPDATE {course_enrollment} SET timestamp = NULL WHERE timestamp = 0');

  // Records should never be 0, these are likely orphaned if they weren't
  // fixed by course_update_7144();
  db_query('UPDATE {course_enrollment} SET updated = 0 WHERE updated IS NULL');
  db_change_field('course_enrollment', 'updated', 'updated', [
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'description' => 'Date this record was updated.',
  ]);
  db_change_field('course_enrollment', 'enroll_end', 'enroll_end', [
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => FALSE,
    'description' => 'Date enrollment ends.',
  ]);
  db_query('UPDATE {course_enrollment} SET enroll_end = NULL WHERE enroll_end = 0');

  // Records should never be NULL, these are likely orphaned if they weren't
  // fixed by course_update_7144();
  db_query('UPDATE {course_report} SET updated = 0 WHERE updated IS NULL');
  db_change_field('course_report', 'updated', 'updated', [
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'description' => 'Date this record was updated.',
  ]);
}

/**
 * Delete orphaned course report records.
 */
function course_update_7146() {
  $result = db_query('DELETE cr.* FROM {course_report} cr
    LEFT JOIN {course_enrollment} ce ON (ce.uid = cr.uid AND ce.nid = cr.nid)
    WHERE ce.eid IS NULL');
  return t('Deleted @rows orphaned records.', array(
    '@rows' => $result
      ->rowCount(),
  ));
}

Functions

Namesort descending Description
course_install Implements hook_install().
course_schema Implements hook_schema().
course_uninstall Implements hook_uninstall().
course_update_6121 Delete enrollments for users who don't exist anymore.
course_update_6122 Migrate and remove old fields that won't be used anymore.
course_update_6124 Add date started field to to fulfillment, add duration to course outline object, remove type field.
course_update_6125 Add indexes to course report table.
course_update_6126 Delete old fulfillment duplicates. Add indexes to outline and fulfillment table. Change grade_result to signed.
course_update_6127 Change primary key in course_report to `crid` (course report ID).
course_update_6128 Change open and close to integer.
course_update_6129 Remove old attendance column. Using course objects now.
course_update_6130 Add `hidden` column to course_outline, add `created` column to course_enrollment. Add serialized `data` column to course_outline.
course_update_6131 Change fulfillment info to serialized.
course_update_6132 Remove old lms settings. Add new course outline display handler settings.
course_update_6133 Change schema, so all literal date fields can be NULL with no default.
course_update_6135 Naming conventions update.
course_update_6136 Remove duplicate field that was never used.
course_update_7000 Rename schema instances of "enrol" to "enroll".
course_update_7001 Migrate old duration value.
course_update_7002 Change grade_result field to NULL to allow for empty grades.
course_update_7130 Install the new course_enrollment_type table for 7.x-1.3.
course_update_7131 Add a coid column to the course report table.
course_update_7132 Fix grade_result fields.
course_update_7133 Add a serialized data field on enrollments.
course_update_7134 Clear registry.
course_update_7135 Update format for grade-based access
course_update_7136 Add indexes for completion dates.
course_update_7137 Grant the "access course" permission to the authenticated user. This permission was not checked before so we are adding it for a clean upgrade path.
course_update_7138 Add an index on "instance".
course_update_7139 Add a new instance to fulfillments. Change existing instance column.
course_update_7140 Add an index on fulfillment "instance".
course_update_7141 Remove duplicate indexes.
course_update_7142 Update some fields to boolean.
course_update_7143 Rebuild registry.
course_update_7144 Update empty "updated" dates.
course_update_7145 Update certain dates to be NULLable.
course_update_7146 Delete orphaned course report records.
course_update_uuid Give course objects and fulfillments UUIDs.