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

Alternate query

parent 42c97565
......@@ -147,63 +147,68 @@ function framaforms_cron(){
* Parameters for adjusting the expiration and deletion period,
* 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.
$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...")
$cron_frequency = "+1 hour"; //amount of time between two cron runs. Should be in plain letters (ie "-1 week", "-2 months...")
$cron_last = date('Y/m/d/ h:m:s', variable_get('cron_last')); // the last time the cron task was run.
$cron_limit = date('Y/m/d/ h:m:s', strtotime($cron_frequency)); // the time when the cron should be run again.
$now = date('Y/m/d/ h:m:s', time());
$framaforms_cron_last = variable_get('framaforms_cron_last'); // the last time the cron task was run.
$cron_limit = new DateTime();
$cron_limit->setTimestamp($framaforms_cron_last);
$cron_limit->modify($cron_frequency);
$now = new DateTime();
// if the cron was executed less than 24h ago, do nothing.
if(strtotime($cron_last) > strtotime($cron_limit)){
if($now < $cron_limit){
return;
}
// else, proceed to 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,
* and insert them into framaforms_expired table
*/
$transaction = db_transaction();
try{
db_query(sprintf("
insert into framaforms_expired (nid)
select node.nid
from node
inner join field_data_field_form1_expiration
on field_data_field_form1_expiration.entity_id = node.nid
where node.type='form1'
and field_data_field_form1_expiration.field_form1_expiration_value <= date_trunc('day', NOW() + interval'%s')
on conflict do nothing;
", $expiration_period), array());
// Limit query to batch of 1000 rows in order to avoid errors
$results = db_query("
INSERT INTO framaforms_expired (nid)
(SELECT n.nid
FROM node n
INNER JOIN field_data_field_form1_expiration fex
ON fex.entity_id = n.nid
AND fex.field_form1_expiration_value <= date_trunc('day', NOW() + interval'{$expiration_period}')
AND fex.entity_id NOT IN
(SELECT nid FROM framaforms_expired)
LIMIT 1000)
ON CONFLICT DO NOTHING;
", array());
}
catch(Exception $e){
$transaction->rollback();
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 */
$transaction = db_transaction();
try{
$results = db_query("
select framaforms_expired.nid, users.mail, node.title
from framaforms_expired
inner join node
on framaforms_expired.nid = node.nid
inner join users
on users.uid = node.uid
where framaforms_expired.notified = 0"
, array() ) ;
// Notify the user calling hook_mail;
SELECT fex.nid, u.mail, n.title
FROM framaforms_expired fex
INNER JOIN node n
ON fex.nid = n.nid
INNER JOIN users u
ON u.uid = n.uid
WHERE fex.notified = 0
LIMIT 1000;
", array() ) ;
// Loop through each result to notify user calling hook_mail
foreach ($results as $record){
watchdog($watchdog_topic, "Sending email to " . (string)$record->mail .
" because of expired webform node (nid = " . (string)$record->nid . ")"
......@@ -235,15 +240,15 @@ function framaforms_cron(){
* If it was, delete the entry from the framaforms_expired table.
*/
update_modified_nodes();
delete_expired_nodes($expiration_period);
update_modified_nodes($expiration_period);
delete_expired_nodes($deletion_period);
}
/**
* Sends an email by calling drupal_mail to a user,
* 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";
$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
<div><em>&laquo; La route est longue, mais la voie est libre. &raquo;</em></div>
");
$expiration_date = date("d/m/Y", strtotime('+'.$expiration_period));
$delete_date = date("d/m/Y", strtotime('+'.$deletion_period));
$expiration_date = new DateTime();
$expiration_date->modify("+".$expiration_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';
......@@ -280,9 +288,6 @@ function notify_user_of_expiration($record, $expiration_period='2 weeks', $delet
'subject' => $mail_subject,
'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);
return $message['send'];
......@@ -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
* "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($watchdog_topic, 'Deleting forms from framaforms_expired whose expiration date was pushed back by the user.');
$transaction = db_transaction();
try{
$modified_nodes = db_query('
select node.nid
from node
inner join field_data_field_form1_expiration
on field_data_field_form1_expiration.entity_id = node.nid
inner join framaforms_expired
on node.nid=framaforms_expired.nid
where field_data_field_form1_expiration.field_form1_expiration_value
> to_timestamp(framaforms_expired.date_notified) + interval\'2 weeks\'
', array() );
$modified_nodes = db_query("
SELECT node.nid
FROM node
INNER JOIN field_data_field_form1_expiration
ON field_data_field_form1_expiration.entity_id = node.nid
INNER JOIN framaforms_expired
ON node.nid=framaforms_expired.nid
WHERE field_data_field_form1_expiration.field_form1_expiration_value
> to_timestamp(framaforms_expired.date_notified) + interval'{$expiration_period}'
", array() );
$nid_changed= 0 ;
foreach($modified_nodes as $node){
$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')
->condition('nid', (int)$nid_changed)
->execute() ;
......@@ -332,14 +335,16 @@ function update_modified_nodes(){
* by this function.
*/
function delete_expired_nodes($deletion_period='9 weeks'){
$deleted_nodes = [];
watchdog('framaforms', "Deleting forms {$deletion_period} after notification.");
$deleted_nodes = array();
$transaction = db_transaction();
try{
$query = db_query(sprintf("
select framaforms_expired.nid
from framaforms_expired
where notified=1
where notified = 1
and to_timestamp(framaforms_expired.date_notified) < date_trunc('day', NOW() - interval'%s') ;
", $deletion_period)
);
......@@ -354,11 +359,12 @@ function delete_expired_nodes($deletion_period='9 weeks'){
->condition('nid', $record->nid)
->execute();
$deleted_nodes[] = $record->nid;
array_push($deleted_nodes, $record->nid);
}
$nb_deleted_nodes = sizeof($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;
}
watchdog("framaforms", "No expired webform to delete.");
......@@ -368,4 +374,3 @@ function delete_expired_nodes($deletion_period='9 weeks'){
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