Inconsistency in webform data.

fumanchu182 - November 20, 2008 - 00:35
Project:webform report
Version:5.x-2.0
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

Hello,

I have found discrepancies in the data in a CSV files that get generated via Webform Reports. The bug I found was with the query that would collect the data from the database and would be out of sync on large scale databases and or sites that have a webform get hit hundreds of times a minute.

/**
* Return all the submissions for a particular node.
*
* @param $nid
*   The node ID for which submissions are being fetched.
* @param $header
*   If the results of this fetch will be used in a sortable
*   table, pass the array header of the table.
* @param $uid
*   Optional; the user ID to filter the submissions by.
* @return $submissions
*   An array of submissions matching your filters.
*/
function webform_get_submissions($nid, $header = NULL, $uid = NULL) {
  $query = 'SELECT s.*, sd.cid, sd.no, sd.data, u.name, u.mail, u.status '.
           'FROM {webform_submissions} s '.
           'LEFT JOIN {webform_submitted_data} sd ON sd.sid = s.sid '.
           'LEFT JOIN {users} u ON u.uid = s.uid '.
           'WHERE sd.nid = %d';

  if ($uid) {
    $query .= ' AND u.uid = %d';
  }

  if (is_array($header)) {
    $query .= tablesort_sql($header);
  }
 
  // patch by anthony to preserve data
  $query .= ' ORDER BY sid';

  $res = db_query($query, $nid, $uid);
  $submissions = array();
  $previous = array();

  // Outer loop: iterate for each submission.
  while ($row = db_fetch_object($res)) {
    if ($row->sid != $previous) {
      $submissions[$row->sid] = new stdClass();
      $submissions[$row->sid]->sid = $row->sid;
      $submissions[$row->sid]->submitted = $row->submitted;
      $submissions[$row->sid]->remote_addr = $row->remote_addr;
      $submissions[$row->sid]->uid = $row->uid;
      $submissions[$row->sid]->name = $row->name;
      $submissions[$row->sid]->status = $row->status;
    }
    $submissions[$row->sid]->data[$row->cid]['value'][$row->no] = $row->data;
    $previous = $row->sid;
  }
  return $submissions;
}

The fix is to have an ordered result set. The most logical was to set the order to sort by sid since those are what keep component answers in webform_submitted_data and webform_submissions together in the join. This can be done by adding   $query .= ' ORDER BY sid'; I hope this helps out anyone else that is having CSV issues.

 
 

Drupal is a registered trademark of Dries Buytaert.