Delete Survey Responses

rgraves - October 16, 2007 - 14:14
Project:Survey
Version:5.x-1.x-dev
Component:User interface
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Is there a way to delete responses from the survey? I don't see one. If not, that would be a useful feature to have.

#1

nachenko - February 7, 2008 - 00:24
Status:active» patch (code needs work)

I added this feature. I'm still testing, but it seems it works. You can only delete one by one, from the "responses" menu. I've added a "delete" link.

It's dirty and ugly, but it works.

If you want to give it a try...

AttachmentSize
survey_delete_link.patch35.46 KB

#2

nachenko - February 7, 2008 - 00:30

Sorry, I'm using Winmerge to create the patch file and this is doing whatever it wants. The patch file is valid but absurd. It's just like Winmerge suddenly thought both files are completely different.

#3

WiredEscape - February 14, 2008 - 07:58

For my first exploration of Modules I thought I'd have a go at the Survey.module to see if I could improve on the delete functionality. I wanted the ability to perform a total purge of all responses or to delete individual responses. Both these delete methods would need to be related to a specific survey.

I started with the two patches that have been provided by mcantelon and nachenko.

My drupal and PHP skills are limited and I'm hoping to get some feedback and suggestions to improve what I have done so far. Help with rolling it into a patch would be great.
---------------------------------------------------------------------------------------

Purge All Responses

2008-Feb-14 update
Now working with no bugs that I can find.

Changes:
-Deletes all survey response records in both the 'survey_responses' and 'survey_fields' tables.
-Checks for and deletes orphan records in both tables. Because Drupal5 does not use database transactions, orphaned records can be created when a specific operation fails during multiple writes or deletes on multiple tables. To compensate for the lack of commit/rollback this function finds and deletes any orphans in either table.
---------------------------
I manually installed the survey_purge.patch by mcantelon. In testing I found that the original survey_maintenance_purge function deleted responses from the survey_responses table but orphaned related records in the survey_fields table. Trimmed out some redundant code and found that the db_fetch_object was throwing errors that I could not solve. Seems to be 'talk' of problems with this function and PHP+5.2 so replaced it with db_fetch_array.

Is there anything you can suggest to improve the survey_maintenance_purge function?
-would like to see "Purge All" as a link on the list response page rather than a separate tab.
-testing?
-?

Plan to wrap it all into one patch.

Tested with Drupal 5.7 and mysql5.2

<?php
 
(note: this line for display purposes only)

   
$items[] = array('path' => 'node/'. $node->nid.'/responses/list', 'title' => t('List Responses'),
            
'callback arguments' => array($node),
            
'type' => MENU_DEFAULT_LOCAL_TASK);
+
+   
$items[] = array('path' => 'node/'.$node->nid.'/purge', 'title' => t('Delete ALL Responses')
+       
'callback' => 'survey_maintenance_purge_prompt', 'access' => user_access('maintain surveys'),
+        
'type' => MENU_LOCAL_TASK, 'weight' => 7);
+
+   
$items[] = array('path' => 'node/'.$node->nid.'/purge/complete', 'title' => t('purge 2'),
+        
'callback' => 'survey_maintenance_purge', 'access' => user_access('maintain surveys'),
+        
'type' => MENU_LOCAL_TASK, 'weight' => 7);
+
   
$items[] = array('path' => 'node/'.$node->nid.'/responses/excel', 'title' => t('Download to Excel'),
        
'callback' => 'survey_excel', 'access' => user_access('maintain surveys'),
        
'callback arguments' => array($node),
        
'type' => MENU_LOCAL_TASK, 'weight' => 1);

(
note: next line for display purposes only)
?>

<?php
 
(note: this line for display purposes only)
+
/**
+ * Callback: Survey Purge confirmation prompt
+ */
+ function survey_maintenance_purge_prompt () {
$output = ' ';
$output .= 'Are you sure you want to purge ALL responses? (';
$output .= l(t('Yes'), 'node/' . arg(1) . '/purge/complete');
$output .= ') / (';
$output .= l(t('No'), 'node/' . arg(1) . '/edit');
$output .= ')';
+  print
theme('page', $output);
+}
+
/**
+ * Callback: Purge all survey responses
+ */
+ function survey_maintenance_purge() {
+  if (
user_access('maintain surveys')) {
+   
$nid = arg(1);
+   
$result1 = db_query('SELECT rid FROM {survey_responses} WHERE nid=%d', $nid);
+    if (
db_num_rows($result1) >0) { //If no response id #s found, abort delete.
+      $result2 = db_query('DELETE FROM {survey_responses} WHERE nid=%d', $nid);  //delete all records with survey id 'nid'
+        if ($result2) { // If delete from 'survey_responses' successful, delete associated 'survey_fields'.
+          while ($row = db_fetch_array($result1)) { //loop to delete multiple associated 'survey_fields' records
+            $result3 = db_query('DELETE FROM {survey_fields} WHERE rid=%s', $row['rid']); //delete response field record for each row of result1 query
+            if (!$result3) { // if delete failed, create error message, end delete loop, and return to response list page
+              drupal_set_message(t('Error: delete of response fields failed. (m1)'), 'error');
+            }
+          }
+          if (
db_affected_rows($result3) <=0) { // If delete operation successful, create message and back to response list page
+            drupal_set_message(t('Error: delete of response fields failed. (m2)'), 'error');
+          }
+       
drupal_set_message(t('All Responses successfully deleted. (m3)'));
+        } else {
+         
drupal_set_message(t('Error: delete of Response failed. (m4)'), 'error');
+          }
+      } else {
+       
drupal_set_message(t('Error: Response data does not exist. Nothing to delete. (m5)'), 'error');
+        }
+    } else {
+     
drupal_set_message(t('You don\'t have permission to delete a Response (m6)'), 'error');
+      }
// Search and delete for orphaned response fields.
$result4 = db_query('SELECT sf.rid FROM {survey_fields} sf LEFT JOIN {survey_responses} sr ON sf.rid=sr.rid WHERE sr.rid IS NULL');
+  if (
$result4) {
+    if (
db_num_rows($result4)>0) { //If no orphan response fields id #s found, abort delete.
+      while ($row = db_fetch_array($result4)) { // Loop to delete multiple associated 'survey_fields' records.
+        $result5 = db_query('DELETE FROM {survey_fields} WHERE rid=%f', $row['rid']); //delete response field record for each row of result1 query.
+        if (!$result5) { // if delete failed, create error message, end delete loop, and return to response list page.
+          drupal_set_message(t('Error: deletion of orphaned Response fields failed. (m7)'), 'error');
+        }
+      }
+      if (
db_affected_rows($result5)>0) { // If delete operation successful, create message and back to response list page.
+        drupal_set_message(t('Orphaned Response fields successfully deleted (m8).'));
+        }
+    }
+  }
drupal_goto('node/'.$nid.'/responses');
+}

(
note: next line for display purposes only)
?>

---------------------------------------------------------------------------------------

Delete Individual Responses

2008-Feb-14 update
Now working with no bugs that I can find.

Changes:
-Deletes survey response records in both the 'survey_responses' and 'survey_fields' tables.
--------------------------------------
Manually installed the survey_delete_link.patch from nachenko. Had to strip out the duplicate code lines. Also found that the original survey_delete_link.patch function did not delete the related records in the survey_fields table. Added $result = db_query ('DELETE FROM {survey_fields} WHERE rid=%d', $rid);.

What does it need?
-testing?
-?

<?php
 
(note: this line for display purposes only)

+ function
survey_responses($survey, $response_id) {
+    if (
arg(4) == "delete") {
+       
$result = survey_responses_delete ($survey->nid, $response_id);
+        unset (
$response_id);
+    }
    if (
$response_id) {

(
note: nextline for display purposes only)
?>

<?php
 
(note: this line for display purposes only)

+
/**
+ * Callback: Delete individual survey response
+ */
+ function survey_responses_delete ($nid, $rid) {
+  if (
user_access('maintain surveys')) {
+   
$result = db_query ('DELETE FROM {survey_responses} WHERE rid = %d AND nid = %d', $rid, $nid); //Deletes response).
+    if ($result) { //If delete from 'survey_responses' fails, abort delete from 'survey_fields'.
+      $result = db_query ('DELETE FROM {survey_fields} WHERE rid=%d', $rid); //Delete associated records in survey_fields.
+        if ($result) {
+         
drupal_set_message(t('Response Deleted.'));
+         
drupal_goto('node/'.$nid.'/responses'); //Back to responses list page.
+          return $result;
+        } else {
+         
drupal_set_message(t('Error: response fields delete failed.'), 'error');
+          return;
+          }
+    } else {
+     
drupal_set_message(t('Error: response delete failed.'), 'error');
+      return;
+      }
+  } else {
+   
drupal_set_message(t('You don\'t have permission to delete a response'), 'error');
+    return;
+    }
+ }
(
note: next line for display purposes only)
?>

#4

WiredEscape - February 11, 2008 - 06:10
Title:Delete Responses» Delete Survey Responses

Title change

#5

quicksketch - March 22, 2008 - 06:28
Status:patch (code needs work)» closed

Survey module is no longer being maintained, use Webform module instead.

---Closing down issue queue of survey module---

 
 

Drupal is a registered trademark of Dries Buytaert.