Commit 960ea6bc authored by Denis Chenu's avatar Denis Chenu

[feature] cron/task type in command line (sendMailCronType)

parent d8f80c40
...@@ -12,6 +12,7 @@ Only partial changelog, [commit history](https://framagit.org/SondagePro-LimeSur ...@@ -12,6 +12,7 @@ Only partial changelog, [commit history](https://framagit.org/SondagePro-LimeSur
- Batch size by surveys - Batch size by surveys
- Different batch size for reminder and invitation by surveys - Different batch size for reminder and invitation by surveys
- Allow debug, simulate and disable in command - Allow debug, simulate and disable in command
- Adding cron/task type for specific send
### Fix ### Fix
......
...@@ -25,6 +25,7 @@ Allow to send token email (invite or reminder) via PHP cli . This allow to use c ...@@ -25,6 +25,7 @@ Allow to send token email (invite or reminder) via PHP cli . This allow to use c
- Minimum delay between each reminders - Minimum delay between each reminders
- Same and more in each surveys setting - Same and more in each surveys setting
- If plugin validate the email (this allow only one email by token) - If plugin validate the email (this allow only one email by token)
- The cron type list to be allowed in survey settings
- To test the plugin you need to call it via PHP Cli `php yourlimesurveydir/application/commands/console.php plugin cron --interval=1` (remind: it send email in this way) - To test the plugin you need to call it via PHP Cli `php yourlimesurveydir/application/commands/console.php plugin cron --interval=1` (remind: it send email in this way)
- This line can be added in your crontab or Task Scheduler - This line can be added in your crontab or Task Scheduler
...@@ -35,6 +36,7 @@ The plugin accept optionnal parameters in the command line ...@@ -35,6 +36,7 @@ The plugin accept optionnal parameters in the command line
- **debug** level from 0 : nothing is shown except errors to 3 all action are shown sendMailCronDebug=3 (integer, default 1) - **debug** level from 0 : nothing is shown except errors to 3 all action are shown sendMailCronDebug=3 (integer, default 1)
- **simulate** action done by the plugin : don't send any email or update token : sendMailCronSimulate=1 (boolean, default false) - **simulate** action done by the plugin : don't send any email or update token : sendMailCronSimulate=1 (boolean, default false)
- **disable** all action done by the plugin in command line : sendMailCronDisable=1 (boolean, default false) - **disable** all action done by the plugin in command line : sendMailCronDisable=1 (boolean, default false)
- **cronType** of actual task : sendMailCronType=test (string|null, default null)
Some example Some example
...@@ -70,9 +72,9 @@ Plugin use 2 system for logging : ...@@ -70,9 +72,9 @@ Plugin use 2 system for logging :
## Home page & Copyright ## Home page & Copyright
- HomePage <http://extensions.sondages.pro/sendmailcron/> - HomePage <http://extensions.sondages.pro/sendmailcron/>
- Copyright © 2016 Denis Chenu <http://sondages.pro> - Copyright © 2016-2017 Denis Chenu <http://sondages.pro>
- Copyright © 2016 AXA Insurance (Gulf) B.S.C. <http://www.axa-gulf.com> - Copyright © 2016 AXA Insurance (Gulf) B.S.C. <http://www.axa-gulf.com>
- Copyright © 2016 Extract Recherche Marketing <http://www.extractmarketing.com> - Copyright © 2016-2017 Extract Recherche Marketing <http://www.extractmarketing.com>
- Licence : GNU Affero General Public License <https://www.gnu.org/licenses/gpl-3.0.html> - Licence : GNU Affero General Public License <https://www.gnu.org/licenses/gpl-3.0.html>
## Support ## Support
......
...@@ -38,6 +38,10 @@ class sendMailCron extends PluginBase ...@@ -38,6 +38,10 @@ class sendMailCron extends PluginBase
* @var boolean disable plugin (via command line) * @var boolean disable plugin (via command line)
*/ */
private $disable=false; private $disable=false;
/**
* @var string actual cron type
*/
private $cronType;
/** /**
* @var int currentBatchSize Count for this batch * @var int currentBatchSize Count for this batch
...@@ -112,6 +116,22 @@ class sendMailCron extends PluginBase ...@@ -112,6 +116,22 @@ class sendMailCron extends PluginBase
'label'=>"Validate email before try to send it.", 'label'=>"Validate email before try to send it.",
'default'=>0, 'default'=>0,
), ),
'cronTypes' => array(
'type'=>'text',
'label'=>"Cron/task type.",
'help'=>"One type by line, use only alphanumeric caracter, you can use <code>,</code> or <code>;</code> as separator.",
'default'=>'',
),
'cronTypeNone' => array(
'type'=>'checkbox',
'value'=>1,
'htmlOption'=>array(
'uncheckValue'=>0
),
'label'=>"Survey without cron type use empty cron type only.",
'help'=>"If a survey don't have type: you can choose if it sended for each cron or only if no type is set",
'default'=>1,
),
); );
/** /**
* Add function to be used in cron event * Add function to be used in cron event
...@@ -125,6 +145,21 @@ class sendMailCron extends PluginBase ...@@ -125,6 +145,21 @@ class sendMailCron extends PluginBase
$this->subscribe('newSurveySettings'); $this->subscribe('newSurveySettings');
} }
public function saveSettings($settings)
{
if(isset($settings['cronTypes']))
{
$cronTypes = preg_split('/\r\n|\r|\n|,|;/', $settings['cronTypes']);
$cronTypes = array_map(
function($cronType) { return preg_replace("/[^0-9a-zA-Z]/i", '', $cronType); },
$cronTypes
);
$cronTypes = array_filter($cronTypes);
$settings['cronTypes']=implode( "|",$cronTypes);
}
parent::saveSettings($settings);
}
/** /**
* set actual url when activate * set actual url when activate
*/ */
...@@ -258,6 +293,34 @@ class sendMailCron extends PluginBase ...@@ -258,6 +293,34 @@ class sendMailCron extends PluginBase
'current'=>$this->get('dayOfWeekToSend', 'Survey', $iSurveyId,array()), 'current'=>$this->get('dayOfWeekToSend', 'Survey', $iSurveyId,array()),
), ),
); );
if($this->get('cronTypes', null, null)){
$availCronTypes=$this->get('cronTypes',null,null,$this->settings['cronTypes']['default']);
$availCronTypes=explode('|', $availCronTypes);
$cronTypesOtions=array();
foreach($availCronTypes as $cronType){
$cronTypesOtions[$cronType]=$cronType;
}
$emptyLabel=$this->get('cronTypeNone',null,null,$this->settings['cronTypeNone']['default']) ? $this->translate->gT("Only for no cron type") : $this->translate->gT("For all cron type");
$aSettings['cronTypes']=array(
'type'=>'select',
'htmlOptions'=>array(
'multiple'=>'multiple',
'empty'=>$emptyLabel,
),
'selectOptions'=>array(
'placeholder' =>$emptyLabel,
'allowClear'=> true,
'width'=>'100%',
),
'controlOptions'=>array(
'class'=>'search-100',
),
'label'=>$this->translate->gT("Type of emailing"),
'options'=>$cronTypesOtions,
'current'=>$this->get('cronTypes', 'Survey', $iSurveyId,array()),
);
}
if($oSurvey->anonymized!="Y"){ if($oSurvey->anonymized!="Y"){
$aSettings['reminderOnlyTo']=array( $aSettings['reminderOnlyTo']=array(
'type'=>'select', 'type'=>'select',
...@@ -272,6 +335,7 @@ class sendMailCron extends PluginBase ...@@ -272,6 +335,7 @@ class sendMailCron extends PluginBase
'current'=>$this->get('reminderOnlyTo', 'Survey', $iSurveyId,''), 'current'=>$this->get('reminderOnlyTo', 'Survey', $iSurveyId,''),
); );
} }
$event->set("surveysettings.{$this->id}", array( $event->set("surveysettings.{$this->id}", array(
'name' => get_class($this), 'name' => get_class($this),
'settings' => $aSettings 'settings' => $aSettings
...@@ -321,6 +385,7 @@ class sendMailCron extends PluginBase ...@@ -321,6 +385,7 @@ class sendMailCron extends PluginBase
$iSurvey=$oSurvey->sid; $iSurvey=$oSurvey->sid;
if(tableExists("{{tokens_{$iSurvey}}}")) if(tableExists("{{tokens_{$iSurvey}}}"))
{ {
/* By maxEmail */
$maxEmail=$this->getSetting('maxEmail','survey',$iSurvey,""); $maxEmail=$this->getSetting('maxEmail','survey',$iSurvey,"");
if($maxEmail===""){ if($maxEmail===""){
$maxEmail=$this->getSetting('maxEmail',null,null,$this->settings['maxEmail']['default']); $maxEmail=$this->getSetting('maxEmail',null,null,$this->settings['maxEmail']['default']);
...@@ -329,19 +394,49 @@ class sendMailCron extends PluginBase ...@@ -329,19 +394,49 @@ class sendMailCron extends PluginBase
$this->sendMailCronLog("sendMailByCron for {$iSurvey} deactivated",2); $this->sendMailCronLog("sendMailByCron for {$iSurvey} deactivated",2);
continue; continue;
} }
$this->sendMailCronLog("sendMailByCron for {$iSurvey}",1); /* By day of week */
if($maxBatchSize && $maxBatchSize<=$this->currentBatchSize){
$this->sendMailCronLog("sendMailByCron deactivated for {$iSurvey} due to batch size",1);
continue;
}
$dayOfWeekToSend=$this->getSetting('dayOfWeekToSend','Survey', $iSurvey,"[]"); $dayOfWeekToSend=$this->getSetting('dayOfWeekToSend','Survey', $iSurvey,"[]");
$aDayOfWeekToSend=array_filter(json_decode($dayOfWeekToSend)); $aDayOfWeekToSend=array_filter(json_decode($dayOfWeekToSend));
$todayNum=(string)self::dateShifted(date("Y-m-d H:i:s"),"N"); $todayNum=(string)self::dateShifted(date("Y-m-d H:i:s"),"N");
if(!empty($aDayOfWeekToSend) && !in_array($todayNum,$aDayOfWeekToSend)){ if(!empty($aDayOfWeekToSend) && !in_array($todayNum,$aDayOfWeekToSend)){
$this->sendMailCronLog("sendMailByCron deactivated for {$iSurvey} for today",1); $this->sendMailCronLog("sendMailByCron deactivated for {$iSurvey} for today",1);
continue; continue;
} }
/* By cron type */
$availCronTypes=$this->getSetting('cronTypes',null,null,"");
$availCronTypes=explode('|', $availCronTypes);
if(!empty($availCronTypes)){
$surveyCronTypes=$this->getSetting('cronTypes','Survey',$iSurvey,"");
if($surveyCronTypes){
$surveyCronTypes=json_decode($surveyCronTypes);
} else {
$surveyCronTypes=array();
if(!$this->getSetting('cronTypeNone',null,null,$this->settings['cronTypeNone']['default']) && $this->cronType){
$surveyCronTypes=array($this->cronType);
} else {
$surveyCronTypes=array();
}
}
$surveyCronTypes=array_intersect($surveyCronTypes,$availCronTypes);
if($this->cronType){
if(!in_array($this->cronType,$surveyCronTypes)){
$this->sendMailCronLog("sendMailByCron deactivated for {$iSurvey} for this cron type ({$this->cronType})",1);
continue;
}
} else {
if(!empty($surveyCronTypes)){
$this->sendMailCronLog("sendMailByCron deactivated for {$iSurvey} for no cron type",1);
continue;
}
}
}
$this->sendMailCronLog("sendMailByCron for {$iSurvey}",1);
if($maxBatchSize && $maxBatchSize<=$this->currentBatchSize){
$this->sendMailCronLog("sendMailByCron deactivated for {$iSurvey} due to batch size",1);
continue;
}
Yii::app()->setConfig('surveyID',$iSurvey); Yii::app()->setConfig('surveyID',$iSurvey);
// Fill some information for this // Fill some information for this
$this->currentSurveyBatchSize=0; $this->currentSurveyBatchSize=0;
...@@ -741,7 +836,7 @@ class sendMailCron extends PluginBase ...@@ -741,7 +836,7 @@ class sendMailCron extends PluginBase
} }
if($oSetting && !is_null($oSetting->value)) if($oSetting && !is_null($oSetting->value))
{ {
return trim(stripslashes($oSetting->value),'"'); return trim($oSetting->value,'"');
} }
else else
return $default; return $default;
...@@ -755,7 +850,13 @@ class sendMailCron extends PluginBase ...@@ -755,7 +850,13 @@ class sendMailCron extends PluginBase
$this->settings['baseUrl']['default']= Yii::app()->request->getBaseUrl(); $this->settings['baseUrl']['default']= Yii::app()->request->getBaseUrl();
$this->settings['scriptUrl']['default']= Yii::app()->request->getScriptUrl(); $this->settings['scriptUrl']['default']= Yii::app()->request->getScriptUrl();
} }
return parent::getPluginSettings($getValues); $pluginSettings= parent::getPluginSettings($getValues);
if($getValues){
if($pluginSettings['cronTypes']){
$pluginSettings['cronTypes']['current']=implode("\n",explode("|",$pluginSettings['cronTypes']['current']));
}
}
return $pluginSettings;
} }
/** /**
...@@ -776,6 +877,19 @@ class sendMailCron extends PluginBase ...@@ -776,6 +877,19 @@ class sendMailCron extends PluginBase
if(substr($arg, 0, strlen("sendMailCronDisable="))=="sendMailCronDisable="){ if(substr($arg, 0, strlen("sendMailCronDisable="))=="sendMailCronDisable="){
$this->disable=boolval(substr($arg,strlen("sendMailCronDisable="))); $this->disable=boolval(substr($arg,strlen("sendMailCronDisable=")));
} }
if(substr($arg, 0, strlen("sendMailCronType="))=="sendMailCronType="){
$cronType=trim(substr($arg,strlen("sendMailCronType=")));
$availCronTypes=$this->getSetting('cronTypes',null,null,"");
$availCronTypes=explode('|', $availCronTypes);
if($cronType && !in_array($cronType,$availCronTypes)){
$this->sendMailCronLog("invalid cronType : $cronType, plugin is disable",0);
$this->disable=true;
} else {
$this->cronType=$cronType;
}
//$this->disable=boolval(substr($arg,strlen("sendMailCronDisable=")));
}
} }
} }
...@@ -798,6 +912,7 @@ class sendMailCron extends PluginBase ...@@ -798,6 +912,7 @@ class sendMailCron extends PluginBase
$this->sendMailCronFinalLog($aCountMail); $this->sendMailCronFinalLog($aCountMail);
return true; return true;
} }
if($this->getSetting('maxSurveyBatchSize','Survey', $iSurvey) && $this->getSetting('maxSurveyBatchSize','Survey', $iSurvey) <= $this->currentSurveyBatchSize){ if($this->getSetting('maxSurveyBatchSize','Survey', $iSurvey) && $this->getSetting('maxSurveyBatchSize','Survey', $iSurvey) <= $this->currentSurveyBatchSize){
$stillToSend=$aCountMail['total']*2-array_sum($aCountMail); $stillToSend=$aCountMail['total']*2-array_sum($aCountMail);
$this->sendMailCronLog("Batch survey size achieved for {$iSurvey} during {$sType}. {$stillToSend} email to send at next batch.",1); $this->sendMailCronLog("Batch survey size achieved for {$iSurvey} during {$sType}. {$stillToSend} email to send at next batch.",1);
......
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