3

I have a Drupal site that uses the Webform module. It has many options for downloading the results via the Drupal interface but it doesn't have anything out the box to get the data programmatically (JSON ect).

Is this possible? Authentication will still be require i.e only Drupal users for the site should be able to access this information via the JSON request.

2 Answers 2

3

You might have to create your own. It would probably be worth submitting back as a new module too.

All you would have to do is implement hook_menu() to add your menu callback. In hook menu you can set access arguments to make sure no one case use it unless they have a particular permission.

You can add your own custom permission using hook_permission().

Then the callback function could use webform_get_submissions() to load the submissions for a given webform and return them using drupal_json_output()

You could pass in a webform nid argument either via the url, using something like 'get-webform-submissions-json/%node' as your menu path, which would automatically validate the nid and load the node for you. For more information on that see http://drupal.org/node/224170 You could also just pass it in as a query parameter and use $_GET['nid'] in your callback function, however then you need to validate it yourself.

1

using the accepted answer as a reference i wrote the following piece of code which I know is not perfect but should get you started:

/**
 * Implementation of hook_menu().
*/

function survey_json_exporter_menu() {
$items['survey_json_exporter'] = array(
    'access callback'   => true, // available to all
    'page callback' => 'survey_json_exporter',
    'delivery callback' => 'drupal_json_output'
    );

return $items;
}

function survey_json_exporter($form, &$fstate)
{
   module_load_include('inc', 'webform', 'includes/webform.submissions');

   $returningJSON = new stdClass();

   $Surveys = [];

   //TODO - > Loop All Surveys.
   $submissions = webform_get_submissions(70535);

   // Load components for each node.
   $surveycomponents = db_select('webform_component')
     ->fields('webform_component')
     ->condition('nid', 70535)
     ->orderBy('weight')
     ->orderBy('name')
     ->execute()
     ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);

   foreach ($submissions as &$submission) {
       $submissionObj = new stdClass();
       $submissionObj->SubmissionID = $submission->sid;
       $submissionObj->SerialNumber = $submission->serial;
       $submissionObj->DateSubmitted = $submission->submitted;

       $SumbissionQA = [];
      while ($surveyanswer = current($submission->data)) {
        $componentName = $surveycomponents[key($submission->data)]['name'];
        $submissionSurveyAnswers = new stdClass();
        $submissionSurveyAnswers->Question = $componentName;
        $submissionSurveyAnswers->Answer = $surveyanswer[0];
        $SumbissionQA[] = $submissionSurveyAnswers;
        next($submission->data);
    }

    $submissionObj->SubmissionData =$SumbissionQA;
    $Surveys[]=$submissionObj;
}

$returningJSON->Surveys = $Surveys;

return drupal_json_output($returningJSON);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.