/[drupal]/contributions/modules/survey_webform_migrate/survey_webform_migrate.install
ViewVC logotype

Contents of /contributions/modules/survey_webform_migrate/survey_webform_migrate.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sun Mar 23 00:23:35 2008 UTC (20 months ago) by quicksketch
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Adding survey-webform migration module
1 <?php
2 // $Id: survey.install,v 1.1 2006/07/13 04:47:23 walkah Exp $
3
4 /**
5 * Implementation of hook_install()
6 */
7 function survey_webform_migrate_install() {
8 switch ($GLOBALS['db_type']) {
9 case 'mysql':
10 case 'mysqli':
11 $survey_result = db_query('SELECT * FROM {survey} s');
12
13 if (!$survey_result) {
14 drupal_set_message(t('Survey module is not currently installed on your system, perhaps the conversion has already been run. If so, remove the Survey-Webform Migrate module from your system.'), 'error');
15 if (module_exists('webform')) {
16 drupal_set_message(t('Visit the <a href="!url">Webform Content page</a> for a list of all webforms.', array('!url' => url('admin/content/webform'))));
17 }
18 return;
19 }
20
21 while ($survey = db_fetch_object($survey_result)) {
22 $node = node_load($survey->nid);
23
24 /* Step 1. Convert the node to a webform. */
25 $node->type = 'webform';
26 $node->webform = array(
27 'confirmation' => !empty($node->result_page) ? 'internal:'. $node->result_page : '',
28 'email' => $node->email,
29 'email_from_name' => 'default',
30 'email_from_address' => 'default',
31 'email_subject' => 'default',
32 'submit_limit' => -1,
33 );
34 foreach ($node->form->fields as $field) {
35 $component = array(
36 'name' => $field->title,
37 'form_key' => $field->ffid,
38 'cid' => $field->ffid,
39 'pid' => 0,
40 'value' => '',
41 'weight' => $field->weight,
42 'mandatory' => $field->required,
43 'extra' => array('description' => $field->explanation),
44 );
45 switch ($field->type) {
46 case 'textfield':
47 $component['type'] = 'textfield';
48 break;
49 case 'password':
50 $component['type'] = 'textfield';
51 break;
52 case 'textarea':
53 $component['type'] = 'textarea';
54 break;
55
56 case 'select':
57 $component['extra']['aslist'] = 'Y';
58 // No break.
59 case 'checkboxes';
60 // No break.
61 case 'checkbox';
62 $component['extra']['multiple'] = ($field->type == 'select' && !$field->multiple) ? 'N' : 'Y';
63 // No break.
64 case 'radios':
65 $component['type'] = 'select';
66 $component['extra']['items'] = str_replace(array(':', ';'), array('|', "\n"), $field->options);
67 break;
68
69 case 'file':
70 $component['type'] = 'file';
71 break;
72 }
73 $node->webform['components'][$field->ffid] = $component;
74 }
75 node_save($node);
76
77 /* Step 2. Convert results to webform submissions. */
78 $results = db_query('SELECT * FROM {survey_responses} r INNER JOIN {survey_fields} f ON r.rid = f.rid INNER JOIN {form_fields} ff ON ff.ffid = f.ffid WHERE r.nid = %d ORDER BY r.rid, f.ffid ASC', $node->nid);
79 $current_rid = 0;
80 while ($result = db_fetch_object($results)) {
81 // Start a new result ID.
82 if ($current_rid != $result->rid) {
83 $sid = db_next_id('{webform_submissions}_sid');
84 db_query("INSERT INTO {webform_submissions} (nid, sid, uid, submitted, remote_addr) VALUES (%d, %d, %d, %d, '%s')", $result->nid, $sid, $result->uid, $result->created, '127.0.0.1');
85 $current_rid = $result->rid;
86 }
87
88 // Update the value to match webform storage.
89 switch ($result->type) {
90 case 'checkbox':
91 $result->value = $result->value ? $result->options : '';
92 break;
93 }
94
95 // Insert a single row for common data (checkboxes are not marked as multiple for some reason).
96 if (!$result->multiple && $result->type != 'checkboxes') {
97 db_query("INSERT INTO {webform_submitted_data} (nid, sid, cid, no, data) VALUES (%d, %d, %d, %d, '%s')", $result->nid, $sid, $result->ffid, 0, $result->value);
98 }
99
100 // Loop through multivalue data and insert a row for each value.
101 else {
102 $values = explode(';', $result->value);
103 foreach ($values as $key => $value) {
104 db_query("INSERT INTO {webform_submitted_data} (nid, sid, cid, no, data) VALUES (%d, %d, %d, %d, '%s')", $result->nid, $sid, $result->ffid, $key, $value);
105 }
106 }
107 }
108 }
109 }
110
111 // Survey doesn't uninstall it's tables.
112 db_query('DROP TABLE {forms}');
113 db_query('DROP TABLE {form_fields}');
114 db_query('DROP TABLE {survey}');
115 db_query('DROP TABLE {survey_fields}');
116 db_query('DROP TABLE {survey_responses}');
117
118 drupal_set_message(t('All survey nodes have been converted to Webforms. Visit the <a href="!url">Webform Content page</a> for a list of all webforms.', array('!url' => url('admin/content/webform'))));
119 drupal_set_message(t('Survey, Forms, and Survey-Webform Migration modules have been uninstalled. Please remove these modules from your server.'), 'warning');
120 }

  ViewVC Help
Powered by ViewVC 1.1.2