I wanted to be able to update some values in a submission (like a status field), and discovered that the format used by webform_get_submission isn't directly compatible with webform_submission_update... Why? Enclosed find the fix I used, and if you could add a API (webform_put_submission?) function to do this more cleanly in the future, that would be nice...


function module_statusapprove($nid, $sid) {

// annoying that I have to do an include, when the webform module is already installed and functioning... why isn't these functions loaded by default?
        include_once(drupal_get_path('module', 'webform') ."/webform_submissions.inc");
 
       $submission = webform_get_submission($nid, $sid);
// $submission is an object, not an array  - Is there a good reason to return an object instead of an array here?

// begin custom code here.  We'll change the status field (#15) to Approved. 
      $submission->data[15]['value'][0] = "Approved";
        
//now we want to save the data.  We could avoid all of this, and do direct SQL.  But isn't that WHY we use APIs?

// the problem is evident:  $submission->data is in a different format from what webform_submission_update requires...
      
// one function uses $nid, the other requires a full node passed to it.  Ok, that's easy to solve. 
         $node = node_load($nid);
     
/* $submission->data is in the format:

Array
(
    [1] => Array
        (
            [value] => Array
                (
                    [0] => Actual Value Here
                )

        )

    [2] => Array
        (
            [value] => Array
                (
                    [0] => Actual Value Here
                    [1] => 
                )

        )
       etc.....

But we need massage the data like so:   */
 
        $resubmit = array();        
        foreach ($submission->data as $field => $value) {
          $resubmit[$field]=$value['value'];         
        }

/* ending up with:

Array
(
    [1] => Array
        (
            [0] => Actual Value Here
        )

    [2] => Array
        (
            [0] => Actual Value Here
            [1] => 
        )
    etc...
*/

        webform_submission_update($node, $sid, $resubmit);
        drupal_set_message(t('This submission has been approved'), 'info');
        drupal_goto('node/'. $nid . '/submission/' . $sid);
}

Comments

sethcohn’s picture

Status: Active » Needs review

Since a patch is worth a thousand words....

Suggested API addition to webform_submissions.inc, to complement webform_get_submission :

function webform_put_submission($nid, $sid, $submission);
          $resubmit = array();        
          foreach ($submission->data as $field => $value) {
            $resubmit[$field]=$value['value'];         
          }
          $node = node_load($nid);
          return webform_submission_update($node, $sid, $resubmit);
}
quicksketch’s picture

Status: Needs review » Needs work

Webform already has webform_submission_insert/update, which I don't think we should duplicate with a similarly functioning webform_put_submission(). I think it'd make much more sense to rework webform_get_submission to use the same format as webform_submission_insert/update, rather than the other way around.

Any large API changes like this though necessitate a new version of Webform (3.x), which I'm not sure I want to force upon users after most of them are just now moving to 2.x.

sethcohn’s picture

Ok, so are you saying that a patch that implements "webform_submission_get" (or something similarly named, maybe "select" to fit the update/insert) you _would_ accept?

quicksketch’s picture

Yes, if you can make the existing webform_get_submission() return the same data that webform_submission_update/insert use (the same format as the $submitted variable), that would be the best approach in my opinion.

sethcohn’s picture

Assigned: Unassigned » sethcohn

Haven't forgotten this... just been busy. I'll try and code up something I've got in my head later this week.

quicksketch’s picture

Status: Needs work » Closed (duplicate)

This has finally been fixed in the 3.x version in #277887: Add submission API hooks.