Commit 9208fc79 authored by theo lem's avatar theo lem
Browse files

Alternate query

parent 42c97565
...@@ -147,63 +147,68 @@ function framaforms_cron(){ ...@@ -147,63 +147,68 @@ function framaforms_cron(){
* Parameters for adjusting the expiration and deletion period, * Parameters for adjusting the expiration and deletion period,
* as well as the cron frequency. * as well as the cron frequency.
*/ */
$expiration_period = '2 weeks'; //amount of time between the notification of the user and the actual expiration of the webform. $cron_frequency = "+1 hour"; //amount of time between two cron runs. Should be in plain letters (ie "-1 week", "-2 months...")
$deletion_period = '9 weeks'; //amount of time between the notification of the user and the deletion of data from the DB.
$cron_frequency = "-1 day"; //amount of time between two cron runs. Should be in plain letters (ie "-1 week", "-2 months...")
$framaforms_cron_last = variable_get('framaforms_cron_last'); // the last time the cron task was run.
$cron_last = date('Y/m/d/ h:m:s', variable_get('cron_last')); // the last time the cron task was run. $cron_limit = new DateTime();
$cron_limit = date('Y/m/d/ h:m:s', strtotime($cron_frequency)); // the time when the cron should be run again. $cron_limit->setTimestamp($framaforms_cron_last);
$now = date('Y/m/d/ h:m:s', time()); $cron_limit->modify($cron_frequency);
$now = new DateTime();
// if the cron was executed less than 24h ago, do nothing. // if the cron was executed less than 24h ago, do nothing.
if(strtotime($cron_last) > strtotime($cron_limit)){ if($now < $cron_limit){
return; return;
} }
// else, proceed to cron. // else, proceed to cron.
watchdog($watchdog_topic, "Running framaforms cron."); watchdog($watchdog_topic, "Running framaforms cron.");
variable_set('cron_last', strtotime($now)); variable_set('framaforms_cron_last', $now->getTimestamp());
// TODO : switch these to global variables
$expiration_period = "2 weeks";
$deletion_period = "9 weeks";
/* Get all webforms that will expired between now and 2 weeks in the future, /* Get all webforms that will expired between now and 2 weeks in the future,
* and insert them into framaforms_expired table * and insert them into framaforms_expired table
*/ */
$transaction = db_transaction(); $transaction = db_transaction();
try{ try{
db_query(sprintf(" // Limit query to batch of 1000 rows in order to avoid errors
insert into framaforms_expired (nid) $results = db_query("
select node.nid INSERT INTO framaforms_expired (nid)
from node (SELECT n.nid
inner join field_data_field_form1_expiration FROM node n
on field_data_field_form1_expiration.entity_id = node.nid INNER JOIN field_data_field_form1_expiration fex
where node.type='form1' ON fex.entity_id = n.nid
and field_data_field_form1_expiration.field_form1_expiration_value <= date_trunc('day', NOW() + interval'%s') AND fex.field_form1_expiration_value <= date_trunc('day', NOW() + interval'{$expiration_period}')
on conflict do nothing; AND fex.entity_id NOT IN
", $expiration_period), array()); (SELECT nid FROM framaforms_expired)
LIMIT 1000)
ON CONFLICT DO NOTHING;
", array());
} }
catch(Exception $e){ catch(Exception $e){
$transaction->rollback(); $transaction->rollback();
watchdog($watchdog_topic, $e); watchdog($watchdog_topic, $e);
// begin new transaction in order to avoid the whole cron to fail
$transaction = db_transaction();
} }
/* Get all the webforms that are about to expire and whose owner wasn't yet notified */ /* Get all the webforms that are about to expire and whose owner wasn't yet notified */
$transaction = db_transaction();
try{ try{
$results = db_query(" $results = db_query("
select framaforms_expired.nid, users.mail, node.title SELECT fex.nid, u.mail, n.title
from framaforms_expired FROM framaforms_expired fex
inner join node INNER JOIN node n
on framaforms_expired.nid = node.nid ON fex.nid = n.nid
inner join users INNER JOIN users u
on users.uid = node.uid ON u.uid = n.uid
where framaforms_expired.notified = 0" WHERE fex.notified = 0
, array() ) ; LIMIT 1000;
", array() ) ;
// Loop through each result to notify user calling hook_mail
// Notify the user calling hook_mail;
foreach ($results as $record){ foreach ($results as $record){
watchdog($watchdog_topic, "Sending email to " . (string)$record->mail . watchdog($watchdog_topic, "Sending email to " . (string)$record->mail .
" because of expired webform node (nid = " . (string)$record->nid . ")" " because of expired webform node (nid = " . (string)$record->nid . ")"
...@@ -235,15 +240,15 @@ function framaforms_cron(){ ...@@ -235,15 +240,15 @@ function framaforms_cron(){
* If it was, delete the entry from the framaforms_expired table. * If it was, delete the entry from the framaforms_expired table.
*/ */
update_modified_nodes(); update_modified_nodes($expiration_period);
delete_expired_nodes($expiration_period); delete_expired_nodes($deletion_period);
} }
/** /**
* Sends an email by calling drupal_mail to a user, * Sends an email by calling drupal_mail to a user,
* notifying him.her of the upcoming webform expiration. * notifying him.her of the upcoming webform expiration.
*/ */
function notify_user_of_expiration($record, $expiration_period='2 weeks', $deletion_period='9 weeks'){ function notify_user_of_expiration($record, $expiration_period="2 weeks", $deletion_period="9 weeks"){
$watchdog_topic = "framaforms"; $watchdog_topic = "framaforms";
$mail_subject = t("Framaforms : Votre formulaire " .(string)$record->title . " expirera dans deux semaines."); $mail_subject = t("Framaforms : Votre formulaire " .(string)$record->title . " expirera dans deux semaines.");
...@@ -269,9 +274,12 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet ...@@ -269,9 +274,12 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet
<div><em>&laquo; La route est longue, mais la voie est libre. &raquo;</em></div> <div><em>&laquo; La route est longue, mais la voie est libre. &raquo;</em></div>
"); ");
$expiration_date = new DateTime();
$expiration_date = date("d/m/Y", strtotime('+'.$expiration_period)); $expiration_date->modify("+".$expiration_period);
$delete_date = date("d/m/Y", strtotime('+'.$deletion_period)); $delete_date = clone $expiration_date;
$delete_date->modify($deletion_period);
$delete_date = $delete_date->getTimestamp();
$expiration_date = $expiration_date->getTimestamp();
$node_url = url('node/' . $record->nid, array('absolute' => true, 'alias' => false)) . '/edit'; $node_url = url('node/' . $record->nid, array('absolute' => true, 'alias' => false)) . '/edit';
...@@ -280,9 +288,6 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet ...@@ -280,9 +288,6 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet
'subject' => $mail_subject, 'subject' => $mail_subject,
'headers' => array() 'headers' => array()
); );
watchdog($watchdog_topic, "Sending following email to " . (string)$record->mail . " : " . $body);
$message = drupal_mail('framaforms', 'expired_webform', (string)$record->mail, user_preferred_language($user), $mail_params); $message = drupal_mail('framaforms', 'expired_webform', (string)$record->mail, user_preferred_language($user), $mail_params);
return $message['send']; return $message['send'];
...@@ -290,30 +295,28 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet ...@@ -290,30 +295,28 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet
/** /**
* Looksup webform nodes in the database that were marked as * Looksup webform nodes in the database that were marked as
* "expired" but whose expiration date * got changed by the user. * "expired" but whose expiration date got changed by the user.
*/ */
function update_modified_nodes(){ function update_modified_nodes($expiration_period='9 weeks'){
$watchdog_topic='framaforms'; $watchdog_topic='framaforms';
watchdog($watchdog_topic, 'Deleting forms from framaforms_expired whose expiration date was pushed back by the user.');
$transaction = db_transaction(); $transaction = db_transaction();
try{ try{
$modified_nodes = db_query(' $modified_nodes = db_query("
select node.nid SELECT node.nid
from node FROM node
inner join field_data_field_form1_expiration INNER JOIN field_data_field_form1_expiration
on field_data_field_form1_expiration.entity_id = node.nid ON field_data_field_form1_expiration.entity_id = node.nid
inner join framaforms_expired INNER JOIN framaforms_expired
on node.nid=framaforms_expired.nid ON node.nid=framaforms_expired.nid
where field_data_field_form1_expiration.field_form1_expiration_value WHERE field_data_field_form1_expiration.field_form1_expiration_value
> to_timestamp(framaforms_expired.date_notified) + interval\'2 weeks\' > to_timestamp(framaforms_expired.date_notified) + interval'{$expiration_period}'
', array() ); ", array() );
$nid_changed= 0 ; $nid_changed= 0 ;
foreach($modified_nodes as $node){ foreach($modified_nodes as $node){
$nid_changed = $node->nid; $nid_changed = $node->nid;
watchdog($watchdog_topic, "Updating framaforms_expired, deleting the modified webform " .
(string)$node->nid . " from the framaforms_expired table.");
db_delete('framaforms_expired') db_delete('framaforms_expired')
->condition('nid', (int)$nid_changed) ->condition('nid', (int)$nid_changed)
->execute() ; ->execute() ;
...@@ -332,14 +335,16 @@ function update_modified_nodes(){ ...@@ -332,14 +335,16 @@ function update_modified_nodes(){
* by this function. * by this function.
*/ */
function delete_expired_nodes($deletion_period='9 weeks'){ function delete_expired_nodes($deletion_period='9 weeks'){
$deleted_nodes = []; watchdog('framaforms', "Deleting forms {$deletion_period} after notification.");
$deleted_nodes = array();
$transaction = db_transaction(); $transaction = db_transaction();
try{ try{
$query = db_query(sprintf(" $query = db_query(sprintf("
select framaforms_expired.nid select framaforms_expired.nid
from framaforms_expired from framaforms_expired
where notified=1 where notified = 1
and to_timestamp(framaforms_expired.date_notified) < date_trunc('day', NOW() - interval'%s') ; and to_timestamp(framaforms_expired.date_notified) < date_trunc('day', NOW() - interval'%s') ;
", $deletion_period) ", $deletion_period)
); );
...@@ -354,11 +359,12 @@ function delete_expired_nodes($deletion_period='9 weeks'){ ...@@ -354,11 +359,12 @@ function delete_expired_nodes($deletion_period='9 weeks'){
->condition('nid', $record->nid) ->condition('nid', $record->nid)
->execute(); ->execute();
$deleted_nodes[] = $record->nid; array_push($deleted_nodes, $record->nid);
} }
$nb_deleted_nodes = sizeof($deleted_nodes);
if(!empty($deleted_nodes)){ if(!empty($deleted_nodes)){
watchdog("framaforms", "Deleted following expired webforms : " . explode($deleted_nodes)); watchdog("framaforms", "Deleted following expired webforms ({$nb_deleted_nodes} in total) : " . explode($deleted_nodes));
return; return;
} }
watchdog("framaforms", "No expired webform to delete."); watchdog("framaforms", "No expired webform to delete.");
...@@ -367,5 +373,4 @@ function delete_expired_nodes($deletion_period='9 weeks'){ ...@@ -367,5 +373,4 @@ function delete_expired_nodes($deletion_period='9 weeks'){
$transaction->rollback(); $transaction->rollback();
watchdog($watchdog_topic, $e); watchdog($watchdog_topic, $e);
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment