Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_manage_incomplete_submissions.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
19 require_once SQ_FUDGE_PATH.'/db_extras/db_extras.inc';
20 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
21 
48 {
49 
50 
57  function __construct($assetid=0)
58  {
59  parent::__construct($assetid);
60 
61  }//end constructor
62 
63 
75  function create(&$link)
76  {
77  require_once SQ_CORE_PACKAGE_PATH.'/system/system_asset_fns.inc';
78  if (!system_asset_fns_create_pre_check($this)) {
79  return FALSE;
80  }
81  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
82  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
83 
84  if ($linkid = parent::create($link)) {
85  if (!system_asset_fns_create_cleanup($this)) {
86  $linkid = FALSE;
87  }
88  }
89 
90  if ($linkid) {
91  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
92  } else {
93  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
94  }
95 
96  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
97  return $linkid;
98 
99  }//end create()
100 
101 
111  function _getName($short_name=FALSE)
112  {
113  return $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name');
114 
115  }//end _getName()
116 
117 
124  function canClone()
125  {
126  return FALSE;
127 
128  }//end canClone()
129 
130 
142  function morph($new_type_code)
143  {
144  trigger_localised_error('CRON0024', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
145  return FALSE;
146 
147  }//end morph()
148 
149 
159  function canDelete()
160  {
161  return FALSE;
162 
163  }//end canDelete()
164 
165 
183  function _exec(&$msg)
184  {
185  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
186 
187  $assetids = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('form', FALSE);
188 
189  // Store our list of assets that we have previously warned about
190  $filepath = $this->data_path.'/warning_assets.php';
191  if (is_file($filepath)) {
192  $warning_assetids = require_once($filepath);
193  if (!is_array($warning_assetids)) {
194  $warning_assetids = Array();
195  }
196  } else {
197  $warning_assetids = Array();
198  }
199 
200  foreach ($assetids as $assetid) {
201  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
202 
203  // Need to get the form URL from its parent wrapper page, as the
204  // Form Contents asset (which is the actual form) has no URL
205  $parent_assetids = $GLOBALS['SQ_SYSTEM']->am->getDependantParents($assetid);
206  // Value can't be passed directly to reset function
207  $parent_assetid = reset($parent_assetids);
208 
209  $form_url = $GLOBALS['SQ_SYSTEM']->am->getAssetURL($parent_assetid);
210 
211  // Get the pending accounts group, if it exists
212  $incomplete_subs_folder = $asset->getSubmissionsFolder('incomplete_submissions');
213 
214  if ($incomplete_subs_folder) {
215  // Remove incomplete submissions that have expired
216  $remove_period = $asset->attr('submission_expiry_time');
217 
218  if ($remove_period > 0) {
219  $date_sql = db_extras_todate(MatrixDAL::getDbType(), ':created', FALSE);
220 
221  $sql = 'SELECT DISTINCT
222  a.assetid
223  FROM sq_ast a
224  JOIN sq_ast_lnk l
225  ON a.assetid = l.minorid
226  WHERE
227  l.majorid = :majorid AND
228  a.created < '.$date_sql;
229 
230  $query = MatrixDAL::preparePdoQuery($sql);
231  MatrixDAL::bindValueToPdo($query, 'majorid', $incomplete_subs_folder->id);
232  MatrixDAL::bindValueToPdo($query, 'created', ts_iso8601(time() - $remove_period));
233  $submission_assetids = MatrixDAL::executePdoAssoc($query);
234 
235  foreach($submission_assetids as $submission_record) {
236  $submission_assetid = $submission_record['assetid'];
237  $submission_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($submission_assetid);
238 
239  $GLOBALS['SQ_SYSTEM']->am->trashAsset($submission_assetid);
240  $submission_asset->delete(FALSE);
241 
242  // Don't need the incomplete attachments anymore, cleanup....
243  $path = $asset->data_path.'/incomplete_attachments/s'.$submission_assetid;
244  if (is_dir($path)) {
245  delete_directory($path);
246  }//end if
247 
248  unset($submission_asset);
249  }
250  }
251 
252  // From those that are left, see which ones have exceeded the
253  // warning period.
254  $warning_period = $asset->attr('submission_warning_time');
255 
256  if ($warning_period > 0) {
257  $email_format = $asset->getAttribute('warning_email_format');
258  $date_sql = db_extras_todate(MatrixDAL::getDbType(), ':created', FALSE);
259 
260  $sql = 'SELECT DISTINCT
261  a.assetid
262  FROM sq_ast a
263  JOIN sq_ast_lnk l
264  ON a.assetid = l.minorid
265  WHERE
266  l.majorid = :majorid AND
267  a.created < '.$date_sql;
268 
269  $query = MatrixDAL::preparePdoQuery($sql);
270  MatrixDAL::bindValueToPdo($query, 'majorid', $incomplete_subs_folder->id);
271  MatrixDAL::bindValueToPdo($query, 'created', ts_iso8601(time() - $warning_period));
272  $submission_assetids = MatrixDAL::executePdoAll($query);
273 
274  foreach($submission_assetids as $submission_record) {
275  $submission_assetid = $submission_record['assetid'];
276  $submission_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($submission_assetid);
277 
278  if(empty($submission_asset)) continue;
279 
280  $created_userid = $submission_asset->created_userid;
281 
282  if (!isset($warning_assetids[$submission_assetid])) {
283  // Get the Email Format's value afresh, and add the
284  // submission's user to the list
285  $previous_value = $current_value = unserialize($email_format->value);
286  $current_value['to_assetids'][] = $created_userid;
287 
288  // Add keyword replacements
289  $keywords = $email_format->getKeywords();
290  $replacements = Array();
291 
292  foreach ($keywords as $keyword) {
293  $replacement = NULL;
294 
295  switch ($keyword) {
296  case 'form_submission_url_id' :
297  $replacement = $submission_assetid;
298  break;
299 
300  case 'form_total_pages' :
301  $replacement = $asset->getTotalPages();
302  break;
303 
304  case 'return_to_form_url_current' :
305  $replacement = $form_url.'?SQ_FORM_'.$asset->id.'_SUBMISSION='.$submission_assetid.'&SQ_FORM_'.$asset->id.'_PAGE='.$submission_asset->attr('current_page');
306  break;
307 
308  case 'return_to_form_url_latest' :
309  $replacement = $form_url.'?SQ_FORM_'.$asset->id.'_SUBMISSION='.$submission_assetid.'&SQ_FORM_'.$asset->id.'_PAGE='.$submission_asset->attr('latest_page');
310  break;
311 
312  default:
313  if (substr($keyword, 0, 11) === 'form_asset_') {
314  // Get the 'asset_*' keyword from the form PARENT page
315  $sub_keyword = substr($keyword, 11);
316  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_assetid);
317  if(empty($parent_asset)) continue;
318  $replacement = $parent_asset->getKeywordReplacement('asset_'.$sub_keyword);
319  } else if (substr($keyword, 0, 11) === 'user_asset_') {
320  // Get the 'asset_*' keyword from the created user
321  $sub_keyword = substr($keyword, 11);
322  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($created_userid);
323  if(empty($user)) continue;
324  $replacement = $user->getKeywordReplacement('asset_'.$sub_keyword);
325  } else if (substr($keyword, 0, 17) === 'submission_asset_') {
326  // Get the 'asset_*' keyword from the submission
327  $sub_keyword = substr($keyword, 17);
328  $replacement = $submission_asset->getKeywordReplacement('asset_'.$sub_keyword);
329  }
330 
331  }//end switch on keyword
332 
333  if ($replacement !== NULL) {
334  $replacements[$keyword] = $replacement;
335  }
336  }//end foreach keyword
337 
338  $email_format->setValue(serialize($current_value));
339  $email_format->sendMail($replacements);
340  $email_format->setValue(serialize($previous_value));
341 
342  $warning_assetids[$submission_assetid] = TRUE;
343  }
344 
345  unset($submission_asset);
346  }
347  }
348 
349  unset($asset);
350 
351  }//end if
352  }//end foreach
353 
354  // Clear out those assets from warning list that no longer exist, or
355  // are no longer incomplete
356  if (!empty($warning_assetids)) {
357 
358  $asset_attrs = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('complete', 'form_submission', array_keys($warning_assetids));
359 
360  foreach ($warning_assetids as $warning_assetid => $dummy) {
361  if (!isset($asset_attrs[$warning_assetid])) {
362  // Submission does not exist anymore - perhaps completed
363  // but we are not logging submissions from its parent form
364  unset($warning_assetids[$warning_assetid]);
365 
366  } else if ($asset_attrs[$warning_assetid] === 1) {
367  // Submission exists but is complete
368  unset($warning_assetids[$warning_assetid]);
369 
370  }
371 
372  }//end foreach
373 
374  }
375 
376  // recache our warning list
377  create_directory($this->data_path);
378  string_to_file('<'.'?php return '.var_export($warning_assetids, TRUE).'?'.'>', $filepath);
379  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
380 
381  return SQ_CRON_JOB_COMPLETED;
382 
383  }//end _exec()
384 
385 
396  function run()
397  {
398  $res = parent::run();
399  if ($res & SQ_CRON_JOB_REMOVE) {
400  $res = $res - SQ_CRON_JOB_REMOVE;
401  }
402  return $res;
403 
404  }//end run()
405 
406 
407 }//end class
408 
409 ?>