You are here

function course_certificate_update_6101 in Course 7.2

Same name and namespace in other branches
  1. 6 modules/course_certificate/course_certificate.install \course_certificate_update_6101()
  2. 7 modules/course_certificate/course_certificate.install \course_certificate_update_6101()

Create certificate course objects at the end of a course for courses that had the "certificate" checkbox checked. Drop the old field.

1 call to course_certificate_update_6101()
course_certificate_install in modules/course_certificate/course_certificate.install
Implements hook_install().

File

modules/course_certificate/course_certificate.install, line 20
Install, update and uninstall functions for the course_certificate module.

Code

function course_certificate_update_6101() {
  $ret = array();
  drupal_get_schema(NULL, TRUE);
  drupal_load('module', 'uuid');
  if (db_table_exists('course_node') && db_field_exists('course_node', 'certificate')) {
    $idfield = db_field_exists('course_outline', 'snid') ? 'snid' : 'coid';
    $modulefield = $idfield == 'snid' ? 'requirement_type' : 'module';

    // Find courses that had certificate checked, but no course object.
    $sql = "SELECT * FROM {course_node} cn\n      INNER JOIN {course_outline} co ON (cn.nid = co.nid AND co.{$modulefield} = 'course_certificate')\n      where cn.certificate and co.{$idfield} is null";
    $result = db_query($sql);
    while ($row = $result
      ->fetch()) {
      $object = array(
        'module' => 'course_certificate',
        'object_type' => 'certificate',
        'nid' => $row->nid,
        'enabled' => 1,
        'required' => 0,
        'hidden' => 0,
        'title' => 'Certificate',
        'weight' => 999,
        'uuid' => uuid_generate(),
      );
      drupal_write_record('course_outline', $object);
    }
    db_drop_field('course_node', 'certificate');
  }
  return t('Created new certificate objects.');
}