Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_form_submissions.php
1 <?php
32 function getInfo($msg)
33 {
34  echo "$msg\n\n";
35  echo "Usage: php system_integrity_form_submissions.php SYSTEM_ROOT [list|delete]\n\n";
36  echo "\tSYSTEM_ROOT: The root path of Matrix system.\n";
37  echo "\tlist|delete: List or delete the Form Submission assets associated with the wrong Form (ref. Bug #4119).\n";
38  echo "\t Default value is 'list'.\n";
39  echo "\n";
40 
41  exit;
42 
43 }//end getInfo()
44 
45 
46 error_reporting(E_ALL);
47 if ((php_sapi_name() != 'cli')) {
48  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
49 }
50 
51 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
52 if (empty($SYSTEM_ROOT)) {
53  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
54  exit();
55 }
56 
57 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
58  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
59  exit();
60 }
61 
62 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
63 
64 require_once $SYSTEM_ROOT.'/core/include/init.inc';
65 $am = $GLOBALS['SQ_SYSTEM']->am;
66 $root_user = $am->getSystemAsset('root_user');
67 $GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user);
68 
69 // Find all Custom Form assets
70 $custom_forms = $am->getChildren(1, 'page_custom_form');
71 
72 $delete = (isset($_SERVER['argv'][2]) && ($_SERVER['argv'][2] == 'delete')) ? TRUE : FALSE;
73 
74 if ($delete) {
75  echo "\nThe following invalid Form Submissions will be deleted:\n";
76 } else {
77  echo "\nThe following invalid Form Submissions will be deleted if you run this script with delete option:\n";
78 }
79 
80 // Total Form Submission number
81 $total_form_submission_count = 0;
82 // The number of Form Submission assets that are in wrong places
83 $invalid_form_submission_count = 0;
84 
85 foreach ($custom_forms as $custom_form_id => $custom_form_details) {
86  // Only process the assets that are not in trash
87  if (!$am->assetInTrash($custom_form_id, TRUE)) {
88  $custom_form = $am->getAsset($custom_form_id);
89  if (is_null($custom_form)) {
90  echo "Warning: the custom form #$custom_form_id does not exist\n";
91  continue;
92  }
93 
94  // Get Form Contents
95  $form = $custom_form->getForm();
96  if (is_null($form)) {
97  echo "Warning: the custom form #$custom_form_id does not have a Form asset under it\n";
98  continue;
99  }
100 
101  // A question can be created directly under Form asset or under its Form Section asset
102  $possible_quesion_id_prefixes = Array($form->id);
103 
104  // Get Form Sections
105  $section_links = $form->getSectionLinks();
106  foreach ($section_links as $link) {
107  $possible_quesion_id_prefixes[] = $link['minorid'];
108  }
109 
110  // Get Submissions folder
111  $submissions_folder = $form->getSubmissionsFolder();
112  if (is_null($submissions_folder)) {
113  echo "Warning: the custom form #$custom_form_id does not have a Submissions folder under it\n";
114  continue;
115  }
116 
117  // Form Submission assets are linked as TYPE 3 under Submission folder if they were submitted by users
118  $form_submission_links = $am->getLinks($submissions_folder->id, SQ_LINK_TYPE_3, 'form_submission', TRUE);
119  foreach ($form_submission_links as $form_submission_link) {
120  $total_form_submission_count++;
121  echo "Processing the form submission $total_form_submission_count...\n";
122 
123  // Get form submission asset
124  $form_submission_id = $form_submission_link['minorid'];
125  $form_submission = $am->getAsset($form_submission_id);
126 
127  // Ensure this is a Form Submission
128  if (is_null($form_submission) || !($form_submission instanceof Form_Submission)) {
129  echo "Warning: the form submission asset #$form_submission_id does not exist\n";
130  continue;
131  }
132 
133  $answers = $form_submission->getAnswers();
134  //a form submission is in right place only if its question id is in the possible question id prefixes
135  $in_right_place = FALSE;
136 
137  // Create a temporary question id for displaying
138  $temp_qid = NULL;
139  if (!empty($answers)) {
140  foreach ($answers as $question_id => $answer) {
141  $question_id_parts = explode(':', $question_id);
142  if (is_null($temp_qid)) {
143  $temp_qid = $question_id;
144  }
145  if (in_array($question_id_parts[0], $possible_quesion_id_prefixes)) {
146  $in_right_place = TRUE;
147  break;
148  }
149  }//end for each answer
150  }//end if empty answers
151 
152  //if the form submission is not in the right place or does not have answers, list or delete it
153  if (!$in_right_place) {
154  $invalid_form_submission_count++;
155  $submission_answer_str = is_null($temp_qid) ? 'There is no answer in the submission.' : "The submission answers the question #$temp_qid which is not under the Form Contents #{$form->id}";
156  echo "The form submission #$form_submission_id is in the wrong place under #{$submissions_folder->id}. $submission_answer_str\n";
157 
158  // Delete the link if this script is run with delete option
159  if ($delete) {
160  echo "The {$invalid_form_submission_count}th invalid form submission asset is being deleted...\n";
161  if ($am->deleteAssetLink($form_submission_link['linkid'], FALSE)) {
162  echo "DELETED: The form submission #$form_submission_id is deleted from the Submissions folder #{$submissions_folder->id}\n";
163  } else {
164  echo "ERROR: The form submission #$form_submission_id can not be deleted from the Submissions folder #{$submissions_folder->id}\n";
165  }//end delete asset link
166  }//end if delete
167  }
168 
169  // Remove the form submission reference so that the gabage collector can delete it from memory because this script can use up all memory available if there are a lot of form submission assets
170  $am->forgetAsset($form_submission, TRUE);
171 
172  }//end foreach form submission
173 
174  // Remove the assets that are not likely to be used again in the script
175  $am->forgetAsset($submissions_folder, TRUE);
176  $am->forgetAsset($form, TRUE);
177  $am->forgetAsset($custom_form, TRUE);
178 
179  }//end if assetInTrash
180 }
181 
182 if ($delete) {
183  echo "\nDone.\n";
184 } else {
185  echo "\nThere are $invalid_form_submission_count invalid form submission assets in the total of $total_form_submission_count.\n";
186 }
187 
188 $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
189 
190 ?>