Closed (duplicate)
Project:
Webform
Version:
5.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Reporter:
Created:
2 Jul 2008 at 20:27 UTC
Updated:
7 Feb 2010 at 01:41 UTC
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
Comment #1
sethcohn commentedSince a patch is worth a thousand words....
Suggested API addition to webform_submissions.inc, to complement webform_get_submission :
Comment #2
quicksketchWebform 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.
Comment #3
sethcohn commentedOk, 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?
Comment #4
quicksketchYes, 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.
Comment #5
sethcohn commentedHaven't forgotten this... just been busy. I'll try and code up something I've got in my head later this week.
Comment #6
quicksketchThis has finally been fixed in the 3.x version in #277887: Add submission API hooks.