1,432c1,455
< <?php
< // $Id: survey.module,v 1.18 2007/03/11 04:16:41 walkah Exp $
< 
< /**
<  * Implementation of hook_help
<  */
< function survey_help($section = '') {
<   switch ($section) {
<     case 'admin/help#survey':
<       $output = '<p>'. t('The survey module allows users to create surveys to be completed by site visitors. Survey submissions are stored in the database, can be downloaded to Excel, and can generate an e-mail notification of each survey submission for the survey administrator.  Surveys are valuable for gathering information about a community effectively.') .'</p>';
<       $output .= '<p>'. t('Creating a survey is a two-step process.  First create a new survey.  A new survey requires title, thank-you page, survey notification e-mail, and intro text; attachments are optional.  Once the survey is created, there are six tabs for managing the survey: view, edit, outline, track, form, and responses.') .'</p>';
<       $output .= '<p>'. t('From the edit tab, select <em>add survey question</em> to add questions.  The outline feature allows you to include survey responses in the book hierarchy.  Track allows you to see time, referrer, user, and operations in a log so that you can monitor how users are coming to your survey. Finally, when users have responded to your survey, responses can be seen on the survey page. This module requires the forms module to create survey forms.') .'</p>';
<       $output .= t('<p>You can</p>
< <ul>
< <li>create a survey at <a href="%node-add-survey" title = "create survey">create content &gt;&gt; survey.</a></li>
< <li>adminsiter surveys at <a href="%admin-node-configure-types-survey" title = "administer survey">administer &gt;&gt; content types &gt;&gt; survey.</a></li>
< ', array('%node-add-survey' => url('node/add/survey'), '%admin-node-configure-types-survey' => url('admin/node/configure/types/survey'))) .'</ul>';
<       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%survey">Survey page</a>.', array('%survey' => 'http://www.drupal.org/handbook/modules/survey/')) .'</p>';
<       return $output;
<   }
< }
< 
< /**
<  * Implementation of hook_node_info
<  */
< function survey_node_info () {
<   return array(
<     'survey' => array(
<       'name' => t('Survey'),
<       'module' => 'survey',
<       'description' => t("A survey allows you to create a form for use on the site to collect data")));
< }
< 
< /**
<  * Implementation of hook_perm
<  */
< function survey_perm() {
<   return array('maintain surveys', 'submit surveys');
< }
< 
< /**
<  * Implementation of hook_menu
<  */
< function survey_menu($may_cache) {
<   $items = array();
<   if ($may_cache) {
<     $items[] = array('path' => 'node/add/survey', 
<                      'title' => t('survey'),
<                      'access' => user_access('maintain surveys'));
<     $items[] = array('path' => 'survey/submit', 'title' => t('survey submission'),
<                      'callback' => 'survey_submit_response',
<                      'access' => user_access('submit surveys'),
<                      'type' => MENU_CALLBACK);
<   }
<   else {
<     if (user_access('maintain surveys') && arg(0) == 'node' && is_numeric(arg(1))) {
<       $node = node_load(arg(1));
<       if ($node->nid && $node->type == 'survey') {
<         $items[] = array('path' => 'node/'.$node->nid.'/form', 'title' => t('Form'),
<                          'callback' => 'survey_fields', 'access' => user_access('maintain surveys'),
<                          'callback arguments' => array($node),
<                          'type' => MENU_LOCAL_TASK, 'weight' => 3);
<         $items[] = array('path' => 'node/'. $node->nid.'/form/list', 'title' => t('List'),
<                          'callback arguments' => array($node),
<                          'type' => MENU_DEFAULT_LOCAL_TASK);
<         $items[] = array('path' => 'node/'.$node->nid.'/form/edit', 'title' => t('Add field'),
<                          'callback' => 'survey_fields_edit', 'access' => user_access('maintain surveys'),
<                          'callback arguments' => array($node, arg(4)),
<                          'type' => MENU_LOCAL_TASK, 'weight' => 4);
<         $items[] = array('path' => 'node/'.$node->nid.'/form/delete', 'title' => t('Delete field'),
<                          'callback' => 'survey_fields_delete', 'access' => user_access('maintain surveys'),
<                          'callback arguments' => array($node, arg(4)),
<                          'type' => MENU_CALLBACK);
<         
<         $items[] = array('path' => 'node/'.$node->nid.'/responses', 'title' => t('Responses'),
<                          'callback' => 'survey_responses', 'access' => user_access('maintain surveys'),
<                          'callback arguments' => array($node, arg(3)),
<                          'type' => MENU_LOCAL_TASK, 'weight' => 5);
<         $items[] = array('path' => 'node/'. $node->nid.'/responses/list', 'title' => t('List'),
<                          'callback arguments' => array($node),
<                          'type' => MENU_DEFAULT_LOCAL_TASK);
<         $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);
<       }
<     }
<   }
<   return $items;
< }
< 
< /**
<  * Implementation of hook_access
<  */
< function survey_access($op, $node) {
<   global $user;
< 
<   if ($op == 'create' &&  user_access('maintain surveys')) {
<     return TRUE;
<   }
< 
<   if ($op == 'update' && user_access('maintain surveys')) {
<     return TRUE;
<   }
<   
<   if ($op == 'delete' && user_access('maintain surveys')) {
<     return TRUE;
<   }
< }
< 
< /**
<  * Impelementation of hook_validate
<  */
< function survey_validate(&$node) {
<   if ($node->email) {
<     $emails = explode(',', $node->email);
<     foreach ($emails as $email) {
<       if (!valid_email_address(trim($email))) {
<         form_set_error('email', t('Please specify a valid email address, or leave the field blank'));
<       }
<     }
<   }
< }
< 
< /**
<  * Implementation of hook_form
<  */
< function survey_form(&$node) {
< 
<   $form = array();
< 
<   $form['title'] = 
<       array( 
<             '#type' => 'textfield', 
<             '#title' => t('Title'), 
<             '#required' => TRUE, 
<             '#default_value' => $node->title, 
<             '#weight' => -5 
<             ); 
< 
<   $form['intro_text']['body'] = 
<       array(
<             '#type' => 'textarea',
<             '#title' => t('Intro Text'),
<             '#default_value' =>  $node->body,
<             '#description' => t("Intro text."),
<             '#cols' =>  60,
<             '#rows' =>  5,
<             );
< 
<   $form['intro_text']['filter'] = filter_form($node->format);
< 
<   $form['result_page'] = 
<       array(
<             '#type' => 'textfield', 
<             '#title' => t('Path for "thank you" page'), 
<             '#default_value' =>  $node->result_page, 
<             '#description' => t("This page is displayed after the form is submitted.  If you are not using clean URLs, specify the part after '?q='.  If unsure, specify nothing."),
<             '#size' => 70, 
<             '#maxlength' => 70, 
<             ); 
< 
<   $form['email'] = 
<       array(
<             '#type' => 'textfield', 
<             '#title' => t('Email address'), 
<             '#default_value' =>  $node->email, 
<             '#description' => t("This email address will receive a copy of each survey submission. Multiple addresses can be specified as a comma-separated list."),
<             '#size' => 70, 
<             '#maxlength' => 70, 
<             ); 
< 
<   $form['fid'] = array('#type' => 'hidden', '#value' => $node->fid); 
< 
<   return $form;
< }
< 
< /**
<  * Implementation of hook_view
<  */
< function survey_view(&$node, $teaser = 0, $page = 0) {
<   $node = node_prepare($node, $teaser);
<   $node->readmore = TRUE;
<   if ($page) {
<     $node->content['survey'] = array(
<       '#value' => survey_response(array('nid' => $node->nid)),
<       '#weight' => 1);
<   }
<   return $node;
< }
< 
< function survey_response($values) {
<   $output = drupal_get_form('survey_response_form', $values);
<   return $output;
< }
< 
< function survey_response_form($values) {
<   $node = node_load($values['nid']);
<   $form = forms_get_form($node->form);
<   $form['nid'] = array('#type' => 'hidden', '#default_value' => $values['nid']);
<   $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
< 
<   return $form;
< }
< 
< /**
<  * Implementation of hook_load
<  */
< function survey_load($node) {
<   $survey = db_fetch_object(db_query("SELECT * FROM {survey} WHERE nid=%d", $node->nid));
<   $survey->form = forms_load(array('fid' => $survey->fid));
< 
<   return $survey;
< }
< 
< function survey_insert(&$node) {
<   // first thing to do is create a form to associate to this survey
<   $node->fid = forms_create('survey');
<   db_query("INSERT INTO {survey} (nid, fid, email, result_page) VALUES (%d, %d, '%s', '%s')",
<            $node->nid, $node->fid, $node->email, $node->result_page);
< }
< 
< function survey_update(&$node) {
<   db_query("UPDATE {survey} SET email='%s', result_page='%s' WHERE nid=%d", $node->email, $node->result_page, $node->nid);
< }
< 
< function survey_delete(&$node) {
<   forms_delete($node->form);
<   db_query("DELETE FROM {survey} WHERE nid=%d", $node->nid);
< }
< 
< /**
<  * Show the fields for a survey
<  */
< function survey_fields() {
<   $nid = arg(1);
<   $node = node_load(array('nid' => $nid));
< 
<   foreach ($node->form->fields as $field) {
<     $rows[] = array(array('data' => $field->title),
<                     array('data' => $field->type),
<                     array('data' => l(t('edit'), 'node/'.$nid.'/form/edit/'.$field->ffid)),
<                     array('data' => l(t('delete'), 'node/'.$nid.'/form/delete/'.$field->ffid)));
<   }
<   if ($rows) {
<     $output = theme('table', array(t('field'), t('type'), '&nbsp;', '&nbsp;'), $rows);
<   }
<   else {
<     $output = t('No fields defined');
<   }
<   return $output;
< }
< 
< function survey_fields_edit($node, $ffid = NULL) {
<   if ($ffid) {
<     $field = db_fetch_object(db_query("SELECT * FROM {form_fields} WHERE ffid=%d", $ffid));
<   }
<   else {
<     $field->fid = db_result(db_query("SELECT fid FROM {survey} WHERE nid=%d", $node->nid));
<   }
< 
<   $field->nid = $node->nid;
<   $content = drupal_get_form('survey_fields_edit_form', $field);
<   
<   return $content;
< }
< 
< function survey_fields_edit_form($field) {
<   $form = forms_field_form($field);
<   $form['nid'] = array('#type' => 'hidden', '#default_value' => $field->nid);
<   $form['submit'] = array('#type' => 'submit', '#value' => t('Save field'));
< 
<   return $form;
< }
< 
< function survey_fields_edit_form_submit($form_id, $form_values) {
<   forms_save_field($form_values);
<   return 'node/'. arg(1) .'/form';
< }
< 
< /**
<  * shows confirmation for deletion of survey field
<  */
< function survey_fields_delete($node, $ffid) {
<   return drupal_get_form('survey_fields_delete_form', $ffid);
< }
< 
< function survey_fields_delete_form($ffid) {
<   $field = db_fetch_object(db_query("SELECT * FROM {form_fields} WHERE ffid=%d", $ffid));
<   $form['ffid'] = array('#type' => 'value' , '#value' => $ffid);
<   $nid = db_result(db_query("SELECT s.nid FROM {survey} s INNER JOIN {forms} f ON s.fid=f.fid INNER JOIN {form_fields} ff ON f.fid=ff.fid WHERE ff.ffid=%d", $ffid));
<   return confirm_form($form,
<                       t("Really delete %field?", array('%field' => $field->title)),
<                       'node/'. $nid .'/form');
<                       
<                       
< }
< 
< /**
<  * deletes a given field
<  */
< function survey_fields_delete_form_submit($form_id, $form_values) {
<   $ffid = $form_values['ffid'];
<   $field = db_fetch_object(db_query("SELECT * FROM {form_fields} WHERE ffid=%d", $ffid));
<   $nid = db_result(db_query("SELECT s.nid FROM {survey} s INNER JOIN {forms} f ON s.fid=f.fid INNER JOIN {form_fields} ff ON f.fid=ff.fid WHERE ff.ffid=%d", $ffid));
<   forms_delete_field($field);
<   return 'node/'.$nid.'/form';
< }
< 
< function survey_response_form_submit($form_id, $form_values) {
<   global $user;
<   $survey = node_load($form_values['nid']);
<   $error = forms_validate($survey->form, $form_values);
<   if (!$error) {
<     $responses = array();
<     $rid = db_next_id("{survey_responses}_rid");
<     db_query("INSERT INTO {survey_responses} (rid, nid, uid, created) VALUES (%d, %d, %d, %d)", $rid, $survey->nid, $user->uid, time());
<     foreach ($survey->form->fields as $field) {
<       if ($form_values[$field->name]) {
<         if (is_array($form_values[$field->name])) {
<           $value = implode(';', $form_values[$field->name]);
<         }
<         else {
<           $value = $form_values[$field->name];
<         }
<         $responses[$field->title] = $value;
<         db_query("INSERT INTO {survey_fields} (rid, ffid, value) VALUES (%d, %d, '%s')", $rid, $field->ffid, $value);
<       }
<     }
< 
<     if ($survey->email) {
<       $subject = t('%survey submission', array('%survey' => $survey->title));
<       foreach ($responses as $key => $value) {
<         $body.= $key . " : ";
<         $body.= $value . "\n\n";
<       }
<       $body.= "-----\n";
<       $body.= t("Submitted by %name on %date\n", array('%name' => $user->name . ' ('.url('user/'.$user->uid, NULL, NULL, TRUE) .')', '%date' => format_date(time())));
< 
<       $from = variable_get('site_mail', ini_get('sendmail_from'));
<       drupal_mail('survey', $survey->email, $subject, $body, $from);
<     }
<     
<     if ($survey->result_page) {
<       drupal_goto($survey->result_page);
<     }
<   
<     drupal_set_message(t('Thank you for your submission'), 'success');
<     drupal_goto('node/'.$survey->nid);
<   }
< 
<   // If an error occurred, reprint the page with the POST info
<   return node_view($survey, FALSE, TRUE, TRUE);
< }
< 
< function survey_responses($survey, $response_id) {
<   if ($response_id) {
<     $response = db_fetch_object(db_query("SELECT * FROM {survey_responses} WHERE rid=%d", $response_id));
<     $content = '<div>' . t('submitted by') . ' ' . theme('username', user_load(array('uid' => $response->uid)));
<     $content.= ' on ' . format_date($response->created).'</div>';
< 
<     $res = db_query("SELECT f.title, r.value FROM {survey_fields} r INNER JOIN {form_fields} f ON f.ffid=r.ffid WHERE r.rid=%d ORDER BY f.weight", $response->rid);
<     while ($field = db_fetch_object($res)) {
<       $content.= '<div><strong>' . $field->title . ': </strong>' . $field->value . '</div>';
<     }
<   }
<   else {
<     $header = array(t('Submitted by'), t('Date'), '');
<     $res = pager_query("SELECT u.uid, u.name, r.created, r.rid FROM {users} u INNER JOIN {survey_responses} r ON r.uid=u.uid WHERE r.nid=%d ORDER BY created DESC", 50, 0, NULL, $survey->nid);
<     while ($response = db_fetch_object($res)) {
<       $rows[] = array(theme('username', $response), format_date($response->created),
<                       l(t('view'), 'node/'.$survey->nid.'/responses/'.$response->rid));
<     }
<     if ($pager = theme('pager', NULL, 50, 0)) {
<       $rows[] = array(array('data' => $pager, 'colspan' => 3));
<     }
< 
<     $content = theme('table', $header, $rows);
< 
<   }
<   return $content;
< }
< 
< function survey_excel($survey) {
<   if (!$survey->nid) {
<     drupal_not_found();
<   }
<   $res = db_query("SELECT u.name, r.*, f.* FROM {survey_responses} r INNER JOIN {survey_fields} f ON f.rid=r.rid INNER JOIN {form_fields} ff ON ff.ffid=f.ffid LEFT JOIN {users} u ON r.uid=u.uid WHERE r.nid=%d ORDER BY r.rid ASC, ff.weight ASC", $survey->nid);
<   
<   $header = array(t('Submitted by'), t('Date'));
<   foreach ($survey->form->fields as $field) {
<     $header[] = $field->title;
<   }
< 
<   $rows = array();
<   $row = array();
<   $rid = 0;
<   while ($response = db_fetch_object($res)) {
<     if ($response->rid != $rid) {
<       $fields = $survey->form->fields;
<       if (count($row) > 0) {
<         $rows[] = $row;
<       }
<       $rid = $response->rid;
<       if (!$response->name) {
<         $name = variable_get("anonymous", "Anonymous");
<       }
<       else {
<         $name = $response->name;
<       }
<       $row = array($name, format_date($response->created));
<     }
<     $field = array_shift($fields);
<     while ($field->ffid != $response->ffid) {
<       $row[] = '';
<       $field = array_shift($fields);
<     }
<     $row[] = $response->value;
<   }
<   $rows[] = $row;
< 
<   header('Content-type: application/vnd.ms-excel');
<   header('Content-Disposition: attachment; filename="survey_results.xls"');
<   $output = '<html>';
<   $output .= '<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"></head>';
<   $output .= '<body>';
<   $output .= theme('table', $header, $rows);
<   $output .= "</body></html>";
< 
<   print $output;
<   exit;
< }
---
> <?php
> // $Id: survey.module,v 1.18 2007/03/11 04:16:41 walkah Exp $
> 
> /**
>  * Implementation of hook_help
>  */
> function survey_help($section = '') {
>   switch ($section) {
>     case 'admin/help#survey':
>       $output = '<p>'. t('The survey module allows users to create surveys to be completed by site visitors. Survey submissions are stored in the database, can be downloaded to Excel, and can generate an e-mail notification of each survey submission for the survey administrator.  Surveys are valuable for gathering information about a community effectively.') .'</p>';
>       $output .= '<p>'. t('Creating a survey is a two-step process.  First create a new survey.  A new survey requires title, thank-you page, survey notification e-mail, and intro text; attachments are optional.  Once the survey is created, there are six tabs for managing the survey: view, edit, outline, track, form, and responses.') .'</p>';
>       $output .= '<p>'. t('From the edit tab, select <em>add survey question</em> to add questions.  The outline feature allows you to include survey responses in the book hierarchy.  Track allows you to see time, referrer, user, and operations in a log so that you can monitor how users are coming to your survey. Finally, when users have responded to your survey, responses can be seen on the survey page. This module requires the forms module to create survey forms.') .'</p>';
>       $output .= t('<p>You can</p>
> <ul>
> <li>create a survey at <a href="%node-add-survey" title = "create survey">create content &gt;&gt; survey.</a></li>
> <li>adminsiter surveys at <a href="%admin-node-configure-types-survey" title = "administer survey">administer &gt;&gt; content types &gt;&gt; survey.</a></li>
> ', array('%node-add-survey' => url('node/add/survey'), '%admin-node-configure-types-survey' => url('admin/node/configure/types/survey'))) .'</ul>';
>       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%survey">Survey page</a>.', array('%survey' => 'http://www.drupal.org/handbook/modules/survey/')) .'</p>';
>       return $output;
>   }
> }
> 
> /**
>  * Implementation of hook_node_info
>  */
> function survey_node_info () {
>   return array(
>     'survey' => array(
>       'name' => t('Survey'),
>       'module' => 'survey',
>       'description' => t("A survey allows you to create a form for use on the site to collect data")));
> }
> 
> /**
>  * Implementation of hook_perm
>  */
> function survey_perm() {
>   return array('maintain surveys', 'submit surveys');
> }
> 
> /**
>  * Implementation of hook_menu
>  */
> function survey_menu($may_cache) {
>   $items = array();
>   if ($may_cache) {
>     $items[] = array('path' => 'node/add/survey', 
>                      'title' => t('survey'),
>                      'access' => user_access('maintain surveys'));
>     $items[] = array('path' => 'survey/submit', 'title' => t('survey submission'),
>                      'callback' => 'survey_submit_response',
>                      'access' => user_access('submit surveys'),
>                      'type' => MENU_CALLBACK);
>   }
>   else {
>     if (user_access('maintain surveys') && arg(0) == 'node' && is_numeric(arg(1))) {
>       $node = node_load(arg(1));
>       if ($node->nid && $node->type == 'survey') {
>         $items[] = array('path' => 'node/'.$node->nid.'/form', 'title' => t('Form'),
>                          'callback' => 'survey_fields', 'access' => user_access('maintain surveys'),
>                          'callback arguments' => array($node),
>                          'type' => MENU_LOCAL_TASK, 'weight' => 3);
>         $items[] = array('path' => 'node/'. $node->nid.'/form/list', 'title' => t('List'),
>                          'callback arguments' => array($node),
>                          'type' => MENU_DEFAULT_LOCAL_TASK);
>         $items[] = array('path' => 'node/'.$node->nid.'/form/edit', 'title' => t('Add field'),
>                          'callback' => 'survey_fields_edit', 'access' => user_access('maintain surveys'),
>                          'callback arguments' => array($node, arg(4)),
>                          'type' => MENU_LOCAL_TASK, 'weight' => 4);
>         $items[] = array('path' => 'node/'.$node->nid.'/form/delete', 'title' => t('Delete field'),
>                          'callback' => 'survey_fields_delete', 'access' => user_access('maintain surveys'),
>                          'callback arguments' => array($node, arg(4)),
>                          'type' => MENU_CALLBACK);
>         
>         $items[] = array('path' => 'node/'.$node->nid.'/responses', 'title' => t('Responses'),
>                          'callback' => 'survey_responses', 'access' => user_access('maintain surveys'),
>                          'callback arguments' => array($node, arg(3)),
>                          'type' => MENU_LOCAL_TASK, 'weight' => 5);
>         $items[] = array('path' => 'node/'. $node->nid.'/responses/list', 'title' => t('List'),
>                          'callback arguments' => array($node),
>                          'type' => MENU_DEFAULT_LOCAL_TASK);
>         $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);
>       }
>     }
>   }
>   return $items;
> }
> 
> /**
>  * Implementation of hook_access
>  */
> function survey_access($op, $node) {
>   global $user;
> 
>   if ($op == 'create' &&  user_access('maintain surveys')) {
>     return TRUE;
>   }
> 
>   if ($op == 'update' && user_access('maintain surveys')) {
>     return TRUE;
>   }
>   
>   if ($op == 'delete' && user_access('maintain surveys')) {
>     return TRUE;
>   }
> }
> 
> /**
>  * Impelementation of hook_validate
>  */
> function survey_validate(&$node) {
>   if ($node->email) {
>     $emails = explode(',', $node->email);
>     foreach ($emails as $email) {
>       if (!valid_email_address(trim($email))) {
>         form_set_error('email', t('Please specify a valid email address, or leave the field blank'));
>       }
>     }
>   }
> }
> 
> /**
>  * Implementation of hook_form
>  */
> function survey_form(&$node) {
> 
>   $form = array();
> 
>   $form['title'] = 
>       array( 
>             '#type' => 'textfield', 
>             '#title' => t('Title'), 
>             '#required' => TRUE, 
>             '#default_value' => $node->title, 
>             '#weight' => -5 
>             ); 
> 
>   $form['intro_text']['body'] = 
>       array(
>             '#type' => 'textarea',
>             '#title' => t('Intro Text'),
>             '#default_value' =>  $node->body,
>             '#description' => t("Intro text."),
>             '#cols' =>  60,
>             '#rows' =>  5,
>             );
> 
>   $form['intro_text']['filter'] = filter_form($node->format);
> 
>   $form['result_page'] = 
>       array(
>             '#type' => 'textfield', 
>             '#title' => t('Path for "thank you" page'), 
>             '#default_value' =>  $node->result_page, 
>             '#description' => t("This page is displayed after the form is submitted.  If you are not using clean URLs, specify the part after '?q='.  If unsure, specify nothing."),
>             '#size' => 70, 
>             '#maxlength' => 70, 
>             ); 
> 
>   $form['email'] = 
>       array(
>             '#type' => 'textfield', 
>             '#title' => t('Email address'), 
>             '#default_value' =>  $node->email, 
>             '#description' => t("This email address will receive a copy of each survey submission. Multiple addresses can be specified as a comma-separated list."),
>             '#size' => 70, 
>             '#maxlength' => 70, 
>             ); 
> 
>   $form['fid'] = array('#type' => 'hidden', '#value' => $node->fid); 
> 
>   return $form;
> }
> 
> /**
>  * Implementation of hook_view
>  */
> function survey_view(&$node, $teaser = 0, $page = 0) {
>   $node = node_prepare($node, $teaser);
>   $node->readmore = TRUE;
>   if ($page) {
>     $node->content['survey'] = array(
>       '#value' => survey_response(array('nid' => $node->nid)),
>       '#weight' => 1);
>   }
>   return $node;
> }
> 
> function survey_response($values) {
>   $output = drupal_get_form('survey_response_form', $values);
>   return $output;
> }
> 
> function survey_response_form($values) {
>   $node = node_load($values['nid']);
>   $form = forms_get_form($node->form);
>   $form['nid'] = array('#type' => 'hidden', '#default_value' => $values['nid']);
>   $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
> 
>   return $form;
> }
> 
> /**
>  * Implementation of hook_load
>  */
> function survey_load($node) {
>   $survey = db_fetch_object(db_query("SELECT * FROM {survey} WHERE nid=%d", $node->nid));
>   $survey->form = forms_load(array('fid' => $survey->fid));
> 
>   return $survey;
> }
> 
> function survey_insert(&$node) {
>   // first thing to do is create a form to associate to this survey
>   $node->fid = forms_create('survey');
>   db_query("INSERT INTO {survey} (nid, fid, email, result_page) VALUES (%d, %d, '%s', '%s')",
>            $node->nid, $node->fid, $node->email, $node->result_page);
> }
> 
> function survey_update(&$node) {
>   db_query("UPDATE {survey} SET email='%s', result_page='%s' WHERE nid=%d", $node->email, $node->result_page, $node->nid);
> }
> 
> function survey_delete(&$node) {
>   forms_delete($node->form);
>   db_query("DELETE FROM {survey} WHERE nid=%d", $node->nid);
> }
> 
> /**
>  * Show the fields for a survey
>  */
> function survey_fields() {
>   $nid = arg(1);
>   $node = node_load(array('nid' => $nid));
> 
>   foreach ($node->form->fields as $field) {
>     $rows[] = array(array('data' => $field->title),
>                     array('data' => $field->type),
>                     array('data' => l(t('edit'), 'node/'.$nid.'/form/edit/'.$field->ffid)),
>                     array('data' => l(t('delete'), 'node/'.$nid.'/form/delete/'.$field->ffid)));
>   }
>   if ($rows) {
>     $output = theme('table', array(t('field'), t('type'), '&nbsp;', '&nbsp;'), $rows);
>   }
>   else {
>     $output = t('No fields defined');
>   }
>   return $output;
> }
> 
> function survey_fields_edit($node, $ffid = NULL) {
>   if ($ffid) {
>     $field = db_fetch_object(db_query("SELECT * FROM {form_fields} WHERE ffid=%d", $ffid));
>   }
>   else {
>     $field->fid = db_result(db_query("SELECT fid FROM {survey} WHERE nid=%d", $node->nid));
>   }
> 
>   $field->nid = $node->nid;
>   $content = drupal_get_form('survey_fields_edit_form', $field);
>   
>   return $content;
> }
> 
> function survey_fields_edit_form($field) {
>   $form = forms_field_form($field);
>   $form['nid'] = array('#type' => 'hidden', '#default_value' => $field->nid);
>   $form['submit'] = array('#type' => 'submit', '#value' => t('Save field'));
> 
>   return $form;
> }
> 
> function survey_fields_edit_form_submit($form_id, $form_values) {
>   forms_save_field($form_values);
>   return 'node/'. arg(1) .'/form';
> }
> 
> /**
>  * shows confirmation for deletion of survey field
>  */
> function survey_fields_delete($node, $ffid) {
>   return drupal_get_form('survey_fields_delete_form', $ffid);
> }
> 
> function survey_fields_delete_form($ffid) {
>   $field = db_fetch_object(db_query("SELECT * FROM {form_fields} WHERE ffid=%d", $ffid));
>   $form['ffid'] = array('#type' => 'value' , '#value' => $ffid);
>   $nid = db_result(db_query("SELECT s.nid FROM {survey} s INNER JOIN {forms} f ON s.fid=f.fid INNER JOIN {form_fields} ff ON f.fid=ff.fid WHERE ff.ffid=%d", $ffid));
>   return confirm_form($form,
>                       t("Really delete %field?", array('%field' => $field->title)),
>                       'node/'. $nid .'/form');
>                       
>                       
> }
> 
> /**
>  * deletes a given field
>  */
> function survey_fields_delete_form_submit($form_id, $form_values) {
>   $ffid = $form_values['ffid'];
>   $field = db_fetch_object(db_query("SELECT * FROM {form_fields} WHERE ffid=%d", $ffid));
>   $nid = db_result(db_query("SELECT s.nid FROM {survey} s INNER JOIN {forms} f ON s.fid=f.fid INNER JOIN {form_fields} ff ON f.fid=ff.fid WHERE ff.ffid=%d", $ffid));
>   forms_delete_field($field);
>   return 'node/'.$nid.'/form';
> }
> 
> function survey_response_form_submit($form_id, $form_values) {
>   global $user;
>   $survey = node_load($form_values['nid']);
>   $error = forms_validate($survey->form, $form_values);
>   if (!$error) {
>     $responses = array();
>     $rid = db_next_id("{survey_responses}_rid");
>     db_query("INSERT INTO {survey_responses} (rid, nid, uid, created) VALUES (%d, %d, %d, %d)", $rid, $survey->nid, $user->uid, time());
>     foreach ($survey->form->fields as $field) {
>       if ($form_values[$field->name]) {
>         if (is_array($form_values[$field->name])) {
>           $value = implode(';', $form_values[$field->name]);
>         }
>         else {
>           $value = $form_values[$field->name];
>         }
>         $responses[$field->title] = $value;
>         db_query("INSERT INTO {survey_fields} (rid, ffid, value) VALUES (%d, %d, '%s')", $rid, $field->ffid, $value);
>       }
>     }
> 
>     if ($survey->email) {
>       $subject = t('%survey submission', array('%survey' => $survey->title));
>       foreach ($responses as $key => $value) {
>         $body.= $key . " : ";
>         $body.= $value . "\n\n";
>       }
>       $body.= "-----\n";
>       $body.= t("Submitted by %name on %date\n", array('%name' => $user->name . ' ('.url('user/'.$user->uid, NULL, NULL, TRUE) .')', '%date' => format_date(time())));
> 
>       $from = variable_get('site_mail', ini_get('sendmail_from'));
>       drupal_mail('survey', $survey->email, $subject, $body, $from);
>     }
>     
>     if ($survey->result_page) {
>       drupal_goto($survey->result_page);
>     }
>   
>     drupal_set_message(t('Thank you for your submission'), 'success');
>     drupal_goto('node/'.$survey->nid);
>   }
> 
>   // If an error occurred, reprint the page with the POST info
>   return node_view($survey, FALSE, TRUE, TRUE);
> }
> 
> function survey_responses($survey, $response_id) {
> 	// Function modified to call delete an item if required
> 	if (arg(4) == "delete") {
> 		$result = survey_responses_delete ($survey->nid, $response_id);
> 		unset ($response_id);
> 	}
>   if ($response_id) {
>     $response = db_fetch_object(db_query("SELECT * FROM {survey_responses} WHERE rid=%d", $response_id));
>     $content = '<div>' . t('submitted by') . ' ' . theme('username', user_load(array('uid' => $response->uid)));
>     $content.= ' '.t('on').' ' . format_date($response->created).'</div>';
> 
>     $res = db_query("SELECT f.title, r.value FROM {survey_fields} r INNER JOIN {form_fields} f ON f.ffid=r.ffid WHERE r.rid=%d ORDER BY f.weight", $response->rid);
>     while ($field = db_fetch_object($res)) {
>       $content.= '<div><strong>' . $field->title . ': </strong>' . $field->value . '</div>';
>     }
>   }
>   else {
>     $header = array(t('Submitted by'), t('Date'), '', '');
>     $res = pager_query("SELECT u.uid, u.name, r.created, r.rid FROM {users} u INNER JOIN {survey_responses} r ON r.uid=u.uid WHERE r.nid=%d ORDER BY created DESC", 50, 0, NULL, $survey->nid);
>     while ($response = db_fetch_object($res)) {
> 			$row = array(theme('username', $response), format_date($response->created),
>       	l(t('view'), 'node/'.$survey->nid.'/responses/'.$response->rid)
> 			);
> 			if (user_access('maintain surveys')) {
> 				$row[] = l(t('delete'), 'node/'.$survey->nid.'/responses/'.$response->rid.'/delete');
> 			}
> 			$rows[] = $row;
>     }
>     if ($pager = theme('pager', NULL, 50, 0)) {
>       $rows[] = array(array('data' => $pager, 'colspan' => 3));
>     }
> 
>     $content = theme('table', $header, $rows);
> 
>   }
>   return $content;
> }
> 
> /*
>  * Function that deletes a 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);
> 		return $result;
> 	} else {
> 		drupal_set_message(t('You don\'t have permission to delete a response'), 'error');
> 		return;
> 	}
> }
> 
> function survey_excel($survey) {
>   if (!$survey->nid) {
>     drupal_not_found();
>   }
>   $res = db_query("SELECT u.name, r.*, f.* FROM {survey_responses} r INNER JOIN {survey_fields} f ON f.rid=r.rid INNER JOIN {form_fields} ff ON ff.ffid=f.ffid LEFT JOIN {users} u ON r.uid=u.uid WHERE r.nid=%d ORDER BY r.rid ASC, ff.weight ASC", $survey->nid);
>   
>   $header = array(t('Submitted by'), t('Date'));
>   foreach ($survey->form->fields as $field) {
>     $header[] = $field->title;
>   }
> 
>   $rows = array();
>   $row = array();
>   $rid = 0;
>   while ($response = db_fetch_object($res)) {
>     if ($response->rid != $rid) {
>       $fields = $survey->form->fields;
>       if (count($row) > 0) {
>         $rows[] = $row;
>       }
>       $rid = $response->rid;
>       if (!$response->name) {
>         $name = variable_get("anonymous", "Anonymous");
>       }
>       else {
>         $name = $response->name;
>       }
>       $row = array($name, format_date($response->created));
>     }
>     $field = array_shift($fields);
>     while ($field->ffid != $response->ffid) {
>       $row[] = '';
>       $field = array_shift($fields);
>     }
>     $row[] = $response->value;
>   }
>   $rows[] = $row;
> 
>   header('Content-type: application/vnd.ms-excel');
>   header('Content-Disposition: attachment; filename="survey_results.xls"');
>   $output = '<html>';
>   $output .= '<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"></head>';
>   $output .= '<body>';
>   $output .= theme('table', $header, $rows);
>   $output .= "</body></html>";
> 
>   print $output;
>   exit;
> }
