Hi,

I try to display simple webform submission result on /user/xx page and allow all roles except anonymous user could see (pure read only). Just hope somebody tips me what I miss.

- I have limited one user one submission, and the user only could edit / delete his own submission.
- Assume the system will require all users filling up their webform.
- I have cloned 4 authenticated user roles (these 4 roles exactly the same permission as authenticated user), so there are 6 roles in drupal.
- I applied the patches (webform-273837-69.patch & webform.module_14.patch).
- I set up two webform (node/34 & node/33). Supposed the names of 4 roles are A,B,C,D, A and B could submit node/34 and C and D could only submit node/33.
- I tried to implement hook_user() (it obviously does not work):

/**
 * Implementation of hook_user().
 */
function webform_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'form':    
      break;
      
    case 'view':
      global $user;
      $submission = array();
      $submission_count = 0;
      
      // Get a count of previous submissions by this user.
      if ($user->uid && (user_access('access own webform submissions') || user_access('access webform results') || user_access('access webform submissions') || (user_access('access own webform results') && $user->uid == $node->uid))) {
        $submission_count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE uid = %d', $user->uid));
      }
      // Render the form and generate the output.
      $form = drupal_get_form('webform_client_form_'. $node->nid, $node, $submission, FALSE, FALSE);
     
      $account->content['webform'] = array('#value' => $form, '#weight' => 1);
      break;

    case 'submit':
      break;
  }
}

Comments

zanhsieh’s picture

Err..

$submission_count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE uid = %d', $user->uid));

should change to:

$submission = db_result(db_query('SELECT s.sid, s.nid, s.uid, sd.no, sd.data, c.form_key, c.name, c.type, c.value, c.extra FROM {webform_submissions} s inner join {webform_submitted_data} sd on (s.sid = sd.sid and s.nid = sd.nid) inner join {webform_component} c on (sd.cid = c.cid and sd.nid = c.nid) where uid = %d', $user->uid));

My bad.

djalloway’s picture

I do not have a direct answer for you, but I will offer that I have been able to get this type of feature working using a few things.
Latest versions of the following.

  1. Webform
  2. Views
  3. The following patch #273837: Views Integration for Webform

And here is what I did.

  • I created a new View using the Webform submissions type that the above Patch provides
  • Added a Relationship to the User table under the relationship list.
  • Added a Relationship to the Node table under the relationship list.
  • Setup a User ID Argument under the arguments list.
  • Configured the User ID Argument to validate by User ID and only show if it has results for that user.
  • Choose the fields I wanted to display from the Fields list.
  • Set the Display type to "Table", for a nice tabular view.
  • Added a Page Display
  • Set the path of the Page display to "user/%/submissions"
  • Set the menu of the Page display to "TAB" and the title to "My submissions"

Now I have a new tab on my User Account page that says "My submissions" that will show that specific users submissions.
Viola.

If it sounds a bit complicated I understand, but it's the only way i've been able to get user specific Webform data on the My account page.

zanhsieh’s picture

@djalloway:

Wow! Thanks a lot for help.

On webform.module, there are two places need to change:

First one:

function webform_submission_access($node, $submission, $op = 'view', $account = NULL) {
  global $user;
  $account = isset($account) ? $account : $user;

  switch ($op) {
    case 'view':
      return user_access('access webform results') || (user_access('access own webform submissions') && ($account->uid == $submission->uid)) || (user_access('access own webform results') && $user->uid == $node->uid);

should change to

function webform_submission_access($node, $submission, $op = 'view', $account = NULL) {
  global $user;
  $account = isset($account) ? $account : $user;

  switch ($op) {
    case 'view':
      return user_access('access webform results') || user_access('access webform submissions') || (user_access('access own webform submissions') && ($account->uid == $submission->uid)) || (user_access('access own webform results') && $user->uid == $node->uid);

Second one:

/**
 * Implementation of hook_perm().
 */
function webform_perm() {
  return array('create webforms', 'edit own webforms', 'edit webforms', 'access webform results', 'access own webform results', 'clear webform results', 'access own webform submissions', 'edit own webform submissions', 'edit webform submissions', 'use PHP for additional processing');
}

Should change to:

/**
 * Implementation of hook_perm().
 */
function webform_perm() {
  return array('create webforms', 'edit own webforms', 'edit webforms', 'access webform results', 'access own webform results', 'clear webform results', 'access own webform submissions', 'access webform submissions', 'edit own webform submissions', 'edit webform submissions', 'use PHP for additional processing');
}

People intend to use the solution above should apply the code and then assign the 'access webform submission' right to the user role.

Meanwhile before djalloway replied to my request, I implemented hook_user() interface:

/**
 * Implementation of hook_user().
 */

function webform_user($op, &$edit, &$account, $category = NULL) {
 
  switch ($op) {
    case 'view':
      $result = db_query('SELECT s.sid, s.nid, s.uid, sd.no, sd.data, c.name, c.type, c.value, c.extra FROM {webform_submissions} s INNER JOIN {webform_submitted_data} sd ON (s.sid = sd.sid AND s.nid = sd.nid) INNER JOIN {webform_component} c ON (sd.cid = c.cid AND sd.nid = c.nid) WHERE (c.type="select" OR c.type="textfield" OR c.type = "textarea" OR c.type = "email" OR c.type = "file") AND s.uid = %d ORDER BY c.cid', $account->uid);
      $test = array();
      $answers = array();
      $theanswer = '';
      $output = '';
      $pre='';
      while($q = db_fetch_object($result)) {
        switch($q->type) {
          case 'select':
            $test = unserialize($q->extra);        
            $temps = split("[\n]", $test['items']);
            foreach ($temps as $temp) {
              $temp = split("[\|]", $temp);
              $answers[$temp[0]] = $temp[1];
              //var_dump($temp[0]);
            }
            $theanswer = $answers[$q->data];
            //var_dump($theanswer);
            //return;
            break;          
          case 'textfield':
          case 'textarea':
          case 'email':
          default:
            $theanswer = $q->data;
            break;
          //case 'file':
          }
          if ($q->name != $pre) $output.='<br /><strong>'.($pre=$q->name).':</strong><br />';
          $output.=($theanswer.'<br />');
      }
      $account->content['webform_show_submission'] = array(
        '#type'   => 'markup',        
        '#value'  => t('<h3>Submission</h3>'.substr($output,6).'<br />'), 
        '#weight' => -1,
      );
  }
}

Pure text mode output to individual user profile page.

rcrowe’s picture

I am a Drupal newbie and am trying to do a similar thing. I have a feedback form made with the Web Form module that I'd like to display the results from on a page. Anonymous users should be able to view it. I have installed the Views Webform module, and have made a View page for the feedback. The relationship is Webform Submissions: node and I have a filter for the node for this particular form. I'd like to have the results display on the page, along with the date they were submitted. But, when I try to add fields the only ones available are the date submitted and a link to the results. Is there a way I can display the feedback text instead of just a link? You can see the page at http://yoga.rhettcrowe.com/feedback-results.

quicksketch’s picture

Status: Active » Closed (fixed)

You cannot display individual values because no implementation has been written to show the data from the webform_submitted_data table. See #680386: Views integration for the webform_submitted_data table. The custom theming above is outside the scope of help provided in the Webform queue, so if you decide to go that way you're on your own.