From 8302d2079f27eb9201c521b43d96457bacc5a59d Mon Sep 17 00:00:00 2001
From: coderintherye <kobrien@kiva.org>
Date: Wed, 27 May 2015 11:54:53 -0700
Subject: [PATCH] maybe allow editing submission author

---
 sites/all/modules/webform/webform.module | 76 ++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/sites/all/modules/webform/webform.module b/sites/all/modules/webform/webform.module
index 6b94c22..5a5613b 100644
--- a/sites/all/modules/webform/webform.module
+++ b/sites/all/modules/webform/webform.module
@@ -510,6 +510,9 @@ function webform_permission() {
     'delete own webform submissions' => array(
       'title' => t('Delete own webform submissions'),
     ),
+    'change submission author' => array(
+      'title' => t('Change submission author'),
+    ),
   );
 }
 
@@ -2020,6 +2023,64 @@ function webform_client_form($form, &$form_state, $node, $submission, $is_draft
         '#weight' => 10,
       );
     }
+   // Check if the form already submitted and there are already author and date values.
+   if (isset($submission->name) && !empty($submission->name)) {
+     $default_user = $submission->name;
+   }
+   else {
+     $default_user = $user->name;
+   }
+   if (isset($submission->submitted) && !empty($submission->submitted)) {
+     $default_date = $submission->submitted;
+   }
+   else {
+     $default_date = time();
+   }
+    
+    // Create the admin fieldset only if editing an already existing submission, or on the last page of a new submission.
+   if (isset($submission->sid) || $page_num == $page_count) {
+     // Check if the current page is a submission page and that user has 'change submission author' or 'administer users' permissions.
+     if (arg(2) == 'submission' && (user_access('change submission author') || user_access('administer users'))) {
+       $form['admin'] = array(
+       '#type' => 'fieldset',
+       '#title' => t('Administration'),
+       '#collapsible' => 1,
+       '#collapsed' => 1,
+       '#weight' => -200,
+       );
+       // Add Authored by textfield to the form.
+       $form['admin']['author'] = array(
+         '#type' => 'textfield',
+         '#title' => t('Authored by'),
+         '#size' => 30,
+         '#maxlength' => 60,
+         '#autocomplete_path' => 'user/autocomplete',
+         '#default_value' => $default_user,
+         '#weight' => 0,
+       );
+       // Add Authored on textfield to the form.
+       $form['admin']['date'] = array(
+         '#type' => 'textfield',
+         '#parents' => array( 0 => 'date' ),
+         '#title' => t('Authored on'),
+         '#size' => 20,
+         '#maxlength' => 25,
+         '#default_value' => date('Y-m-d H:i', $default_date),
+         '#weight' => 1,
+       );
+       // Check if a editing an already existing submission.
+       if (isset($submission->sid)) {
+         // Add separate submit button for the author and date fields .
+         $form['admin']['submit'] = array(
+           '#type' => 'button',
+           '#value' => t('Submit Author/Date'),
+           '#weight' => 2,
+             '#executes_submit_callback' => TRUE,
+            '#submit' => array('_webform_author_date_submit'),
+         );
+        }
+     }
+   }
   }
 
   return $form;
@@ -2251,6 +2312,11 @@ function webform_client_form_validate($form, &$form_state) {
   // Run all #element_validate and #required checks. These are skipped initially
   // by setting #validated = TRUE on all components when they are added.
   _webform_client_form_validate($form, $form_state);
+
+  // Check that the author exists.
+  if (!$account = user_load(array('name' => $form_state['values']['author']))) {
+    form_set_error('author', t('Please specify a valid author.'));
+  }
 }
 
 /**
@@ -2484,6 +2550,16 @@ function webform_client_form_submit($form, &$form_state) {
     $submission->data = $new_data + $submission->data;
   }
 
+  // Check if submission author has changed.
+  if (isset($form_state['values']['author']) && !empty($form_state['values']['author'])) {
+    $submitted_user = user_load(array('name' => $form_state['values']['author']));
+    $submission->uid = $submitted_user->uid;
+  }
+  // Check if submission date has changed.
+  if (isset($form_state['values']['date']) && !empty($form_state['values']['date'])) {
+    $submission->submitted = strtotime($form_state['values']['date']);
+  }
+
   // If there is no data to be saved (such as on a multipage form with no fields
   // on the first page), process no further. Submissions with no data cannot
   // be loaded from the database as efficiently, so we don't save them at all.
