Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_submission_management.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_management.inc';
19 
31 {
32 
33 
39  {
40  $this->Asset_Management($pm);
41 
42  $this->vars = Array(
43  'id' => Array(
44  'added' => '0.0.1',
45  'type' => 'text',
46  'default' => '',
47  ),
48  'attributes' => Array(
49  'added' => '0.0.1',
50  'type' => 'serialise',
51  'default' => Array(),
52  'description' => 'array with keys "answers", "summary", "extra_data"',
53  ),
54  'complete' => Array(
55  'added' => '0.2',
56  'type' => 'boolean',
57  'default' => FALSE,
58  'description' => 'TRUE if submission complete, FALSE if incomplete and not yet submitted.',
59  ),
60  'submitted' => Array(
61  'added' => '0.8',
62  'type' => 'datetime',
63  'default' => '---------- --:--:--',
64  'parameters' => Array(
65  'allow_null' => TRUE,
66  'show' => Array('d','m','y','h','i'),
67  ),
68  'description' => 'The date the form submission was submitted to Matrix',
69  ),
70  'captcha_status' => Array(
71  'added' => '0.2',
72  'type' => 'boolean',
73  'default' => FALSE,
74  'description' => 'TRUE if a CAPTCHA has been correctly submitted for this form so far.',
75  ),
76  'xml' => Array(
77  'added' => '0.2',
78  'type' => 'text',
79  'default' => '',
80  ),
81  'current_page' => Array(
82  'added' => '0.3',
83  'type' => 'int',
84  'default' => 0,
85  'description' => 'The page the user last saw on this submission, updated with the current page when a user jumps back and forth through the form',
86  'is_admin' => FALSE,
87  ),
88  'latest_page' => Array(
89  'added' => '0.3',
90  'type' => 'int',
91  'default' => 0,
92  'description' => 'The last page the user saw - ie. the page furthest along the line that has been seen, irrespective of whether a user backtracked to a previous page',
93  'is_admin' => FALSE,
94  ),
95  );
96 
97  }//end constructor
98 
99 
108  function _upgrade($current_version)
109  {
110  // first do things as normal
111  $res = parent::_upgrade($current_version);
112 
113  if (version_compare($current_version, '0.8', '<')) {
114  echo('UPGRADING FORM SUBMISSION FROM '.$current_version."\n");
115  // Upgrade the form submission
116  // Form submission will now have a new attribute called submitted
117  // This field will be the datetime the submission was completed
118  // This corrects Bug #4000 -
119  // The submission uses the created attribute on the asset,
120  // which is inaccurate on multipage forms where the user can
121  // save and return at another time. One shot forms are unaffected
122 
123  $count = 0;
124 
125  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
126  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
127 
128  pre_echo('Updating Form Submissions...');
129 
130  // get all submission assets
131  $submission_ids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form_submission', FALSE, TRUE);
132 
133  // update them
134  foreach ($submission_ids as $id => $type) {
135  $submission = $GLOBALS['SQ_SYSTEM']->am->getAsset($id);
136  $complete = $submission->attr('complete');
137  $submitted = $submission->attr('submitted');
138 
139  // Set the default
140  $date = $submission->created;
141 
142  // Only worry about null submitted dates and completed submissions
143  if ($complete && $submitted == '---------- --:--:--') {
144  // Try and find the form
145  $answers = $submission->attr('attributes');
146  if (!isset($answers['answers'])) {
147  $form_id = 0;
148  } else {
149  // Default to nothing unless something is found
150  $form_id = 0;
151 
152  $section_assetids = Array();
153  foreach ($answers['answers'] as $assetid => &$data) {
154  $assetid_bits = explode(':', $assetid, 2);
155  $real_assetid = $assetid_bits[0];
156 
157  // Only use this asset ID if we haven't seen it before
158  if (array_search($real_assetid, $section_assetids) === FALSE) {
159  $section_assetids[] = $real_assetid;
160 
161  // check that the real asset ID actually exists before sending to getParents
162  $real_asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($real_assetid), 'asset', FALSE, 'name');
163  if (empty($real_asset_info) === FALSE) {
164  $form_id = key($GLOBALS['SQ_SYSTEM']->am->getParents($real_assetid, 'form', FALSE));
165  break;
166  }
167  }
168  }
169 
170  }//end if
171 
172  // No form found cannot proceed
173  if (!empty($form_id)) {
174  $form = $GLOBALS['SQ_SYSTEM']->am->getAsset($form_id, '');
175  $sub_folder = $form->getSubmissionsFolder('submissions_folder');
176 
177  // On upgrading, the best we can do is look for update time for the link.
178  // Failing that, we will just have to use the created date again.
179  $sql = 'SELECT updated FROM sq_ast_lnk WHERE link_type=:type AND minorid=:minorid AND majorid=:majorid';
180  try {
181  $query = MatrixDAL::preparePdoQuery($sql);
182  MatrixDAL::bindValueToPdo($query, 'type', SQ_LINK_TYPE_3);
183  MatrixDAL::bindValueToPdo($query, 'minorid', $submission->id);
184  MatrixDAL::bindValueToPdo($query, 'majorid', $sub_folder->id);
185  $result = MatrixDAL::executePdoOne($query);
186  } catch (Exception $e) {
187  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
188  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
189 
190  pre_echo('UPGRADE FAILED - Database Error: '.$e->getMessage());
191  return FALSE;
192  }
193 
194  if (!empty($result)) {
195  $date = strtotime($result);
196  }//end if
197  // Point the POST var to the current submission id
198  $_POST['SQ_FORM_'.$form->id.'_SUBMISSION'] = $submission->id;
199  $submission->setAttrValue('xml', $form->getXML($date));
200  }//end if
201 
202  // Save the date
203  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
204  $submission->setAttrValue('submitted', ts_iso8601($date));
205  $count++;
206  if (!$submission->saveAttributes()) {
207  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
208  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
209 
210  pre_echo('UPGRADE FAILED - Could not save attributes');
211  return FALSE;
212  }//end if
213  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
214  }//end if
215 
216  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($submission, TRUE);
217  unset($submission);
218  }//end foreach
219 
220  pre_echo('Updated - '.$count.' form submission(s)...');
221 
222  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
223  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
224 
225  pre_echo('FORM SUBMISSIONS SUCCESSFULLY UPGRADED TO VERSION 0.8');
226  }//end if
227  return TRUE;
228 
229  }//end _upgrade()
230 
231 
232 }//end class
233 ?>