You are here

function _webform_submission_serial_next_value in Webform 7.4

Returns the next serial number for a given node and increments it.

Return the serial number to be used in the next submission and saves the number after that as the subsequent serial number.

Parameters

int $nid: The nid of the node.

Return value

int The next value of the serial number.

1 call to _webform_submission_serial_next_value()
webform_submission_insert in includes/webform.submissions.inc
Insert a webform submission entry in the database.

File

./webform.module, line 5376
This module provides a simple way to create forms and questionnaires.

Code

function _webform_submission_serial_next_value($nid) {

  // Use a transaction with SELECT ... FOR UPDATE to lock the row between
  // the SELECT and the UPDATE, ensuring that multiple Webform submissions
  // at the same time do not have duplicate numbers. FOR UPDATE must be inside
  // a transaction. The return value of db_transaction() must be assigned or the
  // transaction will commit immediately. The transaction will commit when
  // $transaction goes out-of-scope.
  $transaction = db_transaction();

  // Get the value stored in this webform as the next_serial value.
  $next_serial = db_select('webform', 'w')
    ->forUpdate()
    ->fields('w', array(
    'next_serial',
  ))
    ->condition('nid', $nid)
    ->execute()
    ->fetchField();

  // The value must be greater than $next_serial and any existing serial number.
  $next_serial = max($next_serial, _webform_submission_serial_next_value_used($nid));

  // Store the value after $next_value for use in the subsequent submission.
  db_update('webform')
    ->fields(array(
    'next_serial' => $next_serial + 1,
  ))
    ->condition('nid', $nid)
    ->execute();
  return $next_serial;
}