Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_submission.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
31 class Form_Submission extends Asset
32 {
33 
34 
41  function Form_Submission($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  return $this->Asset($assetid);
45 
46  }//end constructor
47 
48 
55  public function canCloneLink()
56  {
57  return FALSE;
58 
59  }//end isDeletableLink()
60 
61 
70  function getAnswer($id)
71  {
72  $a = $this->attr('attributes');
73  if (isset($a['answers'][$id]['answer'])) {
74  return $a['answers'][$id]['answer'];
75  }
76  return FALSE;
77 
78  }//end getAnswer()
79 
80 
89  function getExtraData($id)
90  {
91  $a = $this->attr('attributes');
92  if (isset($a['answers'][$id]['extra_data'])) {
93  return $a['answers'][$id]['extra_data'];
94  }
95  return Array();
96 
97  }//end getExtraData()
98 
99 
108  function getName($id)
109  {
110  $a = $this->attr('attributes');
111  if (isset($a['answers'][$id]['name'])) {
112  return $a['answers'][$id]['name'];
113  }
114  return FALSE;
115 
116  }//end getName()
117 
118 
128  function setAnswer($id, $value)
129  {
130  $a = $this->attr('attributes');
131  if (empty($a['answers'])) $a['answers'] = Array();
132 
133  $a['answers'][$id]['answer'] = $value;
134 
135  if (!$this->setAttrValue('attributes', $a)) {
136  return FALSE;
137  }
138 
139  return TRUE;
140 
141  }//end setAnswer()
142 
143 
153  function setExtraData($id, Array $data=NULL)
154  {
155  $a = $this->attr('attributes');
156  if (empty($a['answers'])) $a['answers'] = Array();
157 
158  $a['answers'][$id]['extra_data'] = $data;
159 
160  if (!$this->setAttrValue('attributes', $a)) {
161  return FALSE;
162  }
163 
164  return TRUE;
165 
166  }//end setAnswer()
167 
168 
177  function setError($error)
178  {
179  $a = $this->attr('attributes');
180  if (!isset($a['is_error']) || !$a['is_error']) {
181  $a['is_error'] = TRUE;
182  }
183  if (!isset($a['errors'])) $a['errors'] = Array();
184  $a['errors'][] = $error;
185 
186  if (!$this->setAttrValue('attributes', $a)) {
187  return FALSE;
188  }
189  return TRUE;
190 
191  }//end setError()
192 
193 
200  function getErrors()
201  {
202  $a = $this->attr('attributes');
203  return array_get_index($a, 'errors', Array());
204 
205  }//end getErrors()
206 
207 
214  function flushErrors()
215  {
216  $a = $this->attr('attributes');
217  $a['is_error'] = FALSE;
218  $a['errors'] = Array();
219 
220  if (!$this->setAttrValue('attributes', $a)) {
221  return FALSE;
222  }
223 
224  return TRUE;
225 
226  }//end flushErrors()
227 
228 
235  function isError()
236  {
237  $a = $this->attr('attributes');
238  return (isset($a['is_error']) && ($a['is_error'])) ? TRUE : FALSE;
239 
240  }//end isError()
241 
242 
249  function getAnswers()
250  {
251  $a = $this->attr('attributes');
252  return array_get_index($a, 'answers', Array());
253 
254  }//end getAnswers()
255 
256 
267  function setSummary($question_id, $name, $value)
268  {
269  $a = $this->attr('attributes');
270 
271  if (empty($a['summary'])) $a['summary'] = Array();
272 
273  $a['summary'][$question_id]['answer'] = $value;
274  $a['summary'][$question_id]['name'] = $name;
275 
276  if (!$this->setAttrValue('attributes', $a)) {
277  return FALSE;
278  }
279 
280  return TRUE;
281 
282  }//end setSummary()
283 
284 
294  function getSummary($q_id=NULL, $value='answer')
295  {
296  $a = $this->attr('attributes');
297  if (!$a || !isset($a['summary'])) return FALSE;
298 
299  if (is_null($q_id)) {
300  return $a['summary'];
301  } else {
302  return $a['summary'][$q_id][$value];
303  }
304 
305  }//end getSummary()
306 
307 
319  function saveAttributes($dont_run_updated=FALSE, $log_message=TRUE)
320  {
321  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
322  $val = parent::saveAttributes($dont_run_updated, $log_message);
323  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
324 
325  return $val;
326 
327  }//end saveAttributes()
328 
329 
346  function incrementVersion($number='micro', $update_parents=TRUE)
347  {
348  return parent::incrementVersion($number, FALSE);
349 
350  }//end incrementVersion()
351 
352 
359  function printFrontend()
360  {
361  if (isset($_GET['q']) && $_GET['q'] && (SQ_IN_BACKEND || SQ_IN_LIMBO)) {
362  // Download a file upload, but only if we are on the backend somewhere
363  $question_assetid = $_GET['q'];
364  $question_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($question_assetid, '', TRUE);
365 
366  if ($question_asset && ($question_asset instanceof Form_Question_Type_File_Upload)) {
367  $extra_data = $this->getExtraData($question_assetid);
368  if (!empty($extra_data['filesystem_path'])) {
369  $filepath = $extra_data['filesystem_path'];
370  if (is_file($filepath)) {
371  require SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
372  $type = array_get_index($standards_lists_mime_types, get_file_type($filepath), 'application/octet-stream');
373 
374  header('Content-type: '.$type);
375  header('Content-disposition: attachment; filename="'.(basename($filepath)).'"');
376  header('Content-length: '.filesize($filepath));
377  echo file_get_contents($filepath);
378  }
379  }
380  }
381 
382  exit(0);
383  } else {
384  parent::printFrontend();
385  }
386 
387  }//end printFrontend()
388 
389 
396  function printBody()
397  {
398  $summary = $this->attr('attributes');
399  $summary = $summary['summary'];
400  ?>
401  <table>
402  <thead>
403  <tr>
404  <th>Question</th>
405  <th>Answer</th>
406  </tr>
407  </thead>
408  <tbody>
409  <?php
410  foreach ($summary as $item) {
411  ?>
412  <tr>
413  <td><?php echo $item['name']; ?></td>
414  <td><?php echo $item['answer']; ?></td>
415  </tr>
416  <?php
417  }
418  ?>
419  </tbody>
420  </table>
421  <?php
422 
423  }//end printBody()
424 
425 
440  function getKeywordReplacement($keyword)
441  {
442  $matches = Array();
443  $original_keyword = $keyword;
444  $keyword = parse_keyword($keyword, $modifiers);
445 
446  if (preg_match('/question\_name\_q([0-9]+)/', $keyword, $matches)) {
447  $attrs = $this->attr('attributes');
448  if (!isset($attrs['summary'])) return '';
449 
450  foreach ($attrs['summary'] as $name => $value) {
451  $q_ids = explode(':q', $name);
452  if ($q_ids[1] == $matches[1]) {
453  apply_keyword_modifiers($value['name'], $modifiers, Array('assetid' => $this->id));
454  return $value['name'];
455  }
456  }
457  return '';
458  }
459 
460  if (preg_match('/question\_name\_(([0-9]+)\_q([0-9]+))/', $keyword, $matches)) {
461  $attrs = $this->attr('attributes');
462  if (!isset($attrs['summary'])) return '';
463 
464  $elt = array_get_index($attrs['summary'], $matches[2].':q'.$matches[3], Array('name' => ''));
465  apply_keyword_modifiers($elt['name'], $modifiers, Array('assetid' => $this->id));
466  return $elt['name'];
467  }
468 
469  if (preg_match('/question\_answer\_q([0-9]+)/', $keyword, $matches)) {
470  $attrs = $this->attr('attributes');
471  if (!isset($attrs['summary'])) return '';
472 
473  foreach ($attrs['summary'] as $name => $value) {
474  $q_ids = explode(':q', $name);
475  if ($q_ids[1] == $matches[1]) {
476  apply_keyword_modifiers($value['answer'], $modifiers, Array('assetid' => $this->id));
477  return $value['answer'];
478  }
479  }
480  return '';
481  }
482 
483  if (preg_match('/question\_answer\_(([0-9]+)\_q([0-9]+))/', $keyword, $matches)) {
484  $attrs = $this->attr('attributes');
485  if (!isset($attrs['summary'])) return '';
486 
487  $elt = array_get_index($attrs['summary'], $matches[2].':q'.$matches[3], Array('answer' => ''));
488  apply_keyword_modifiers($elt['answer'], $modifiers, Array('assetid' => $this->id));
489  return $elt['answer'];
490  }
491 
492  if (preg_match('/question\_answer\_key\_q([0-9]+)/', $keyword, $matches)) {
493  $attrs = $this->attr('attributes');
494  foreach ($attrs['answers'] as $name => $value) {
495  $q_ids = explode(':q', $name);
496  if ($q_ids[1] == $matches[1]) {
497  if (!is_scalar($value['answer'])) {
498  $value = array_get_index($attrs['summary'], $name, Array('answer' => ''));
499  }
500  apply_keyword_modifiers($value['answer'], $modifiers, Array('assetid' => $this->id));
501  return $value['answer'];
502  }
503  }
504  return '';
505  }
506 
507  //do keyword replacement for question types that have both keys and values in the answers such as select or country question types
508  if (preg_match('/question\_answer\_key\_(([0-9]+)\_q([0-9]+))/', $keyword, $matches)) {
509  $attrs = $this->attr('attributes');
510  $elt = array_get_index($attrs['answers'], $matches[2].':q'.$matches[3], Array('answer' => ''));
511  //return the summary if the answers key is not a scalar value such as tickbox list question type
512  if (!is_scalar($elt['answer'])) {
513  $elt = array_get_index($attrs['summary'], $matches[2].':q'.$matches[3], Array('answer' => ''));
514  }
515  apply_keyword_modifiers($elt['answer'], $modifiers, Array('assetid' => $this->id));
516  return $elt['answer'];
517  }
518 
519  // if we are falling back on parent to get the keyword replacement make
520  // sure we pass the whole keyword i.e. including the modifier(s) if any
521  return parent::getKeywordReplacement($original_keyword);
522 
523  }//end getKeywordReplacement()
524 
525 
541  {
542  $res = parent::getAvailableKeywords();
543  $res['question_name_X_qY'] = 'The name of the question with ID X:qY';
544  $res['question_answer_X_qY'] = 'The answer to the question with ID X:qY';
545  $res['question_answer_key_X_qY'] = 'The answer key to the question with ID X:qY';
546  $res['submission_ip_address'] = 'IP address that created this submission';
547  return $res;
548 
549  }//end getAvailableKeywords()
550 
551 
558  function getIP()
559  {
560  $a = $this->attr('attributes');
561  if (isset($a['misc']['ip'])) return $a['misc']['ip'];
562  return FALSE;
563 
564  }//end getIP()
565 
566 
575  function setIP($ip)
576  {
577  $a = $this->attr('attributes');
578  if (empty($a['mics'])) $a['misc'] = Array();
579 
580  $a['misc']['ip'] = $ip;
581 
582  if (!$this->setAttrValue('attributes', $a)) {
583  return FALSE;
584  }
585 
586  return TRUE;
587 
588  }//end setIP()
589 
590 
598  {
599  return $this->getIP();
600 
601  }//end getSubmissionIpAddressKeywordReplacement()
602 
603 
611  function _getAllowedLinks()
612  {
613  $page_links = Array(
614  SQ_LINK_NOTICE => Array(
615  'file' => Array(
616  'card' => 'M',
617  'exclusive' => FALSE,
618  ),
619  ),
620  );
621  return $page_links;
622 
623  }//end _getAllowedLinks()
624 
625 
626 }//end class
627 ?>