You are here

function webform_update_7427 in Webform 7.4

Add database columns for submission completed and modified timestamps.

Sites with many submissions may wish to use drush to execute this update.

File

./webform.install, line 2272
Webform module install/schema hooks.

Code

function webform_update_7427() {

  // Create new timestamp columns.
  $specs = array(
    'completed' => array(
      'description' => 'Timestamp when the form was submitted as complete (not draft).',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ),
    'modified' => array(
      'description' => 'Timestamp when the form was last saved (complete or draft).',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ),
  );
  foreach ($specs as $field_name => $spec) {
    if (!db_field_exists('webform_submissions', $field_name)) {
      db_add_field('webform_submissions', $field_name, $spec);
    }
  }

  // In a site with many submissions, the db_update may timeout. Start a
  // transaction to ensure atomic action.
  $transaction = db_transaction();

  // Copy submitted to completed for non-draft submissions.
  db_update('webform_submissions')
    ->expression('completed', 'submitted')
    ->condition('is_draft', 0)
    ->execute();

  // Commit the update.
  unset($transaction);

  // Start another transaction.
  $transaction = db_transaction();
  db_update('webform_submissions')
    ->expression('modified', 'submitted')
    ->execute();

  // Commit the update.
  unset($transaction);

  // Clear the views cache since to see the completed and modified views fields.
  cache_clear_all('*', 'cache_views', TRUE);
  return t('Webform submissions were updated with completed and modified timestamps.');
}