private function isSurveyDuplicated survey account survey_check new Ap

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
private function isSurveyDuplicated(&$survey, $account){
$survey_check = new Application_Model_Survey($this->db);
$result = $survey_check->checkSurveyExist($survey->getSubject(), $survey->getTag(), $survey->getPronoun());
if($result) {
/* so there is the same survey already in the database
* -instead fo creating new - add recipient to this survey
* the survey is now loaded in $survey_check object
*/
$new_survey_responders = $survey->getResponderEmails();
$existing_survey_reposnders = $survey_check->getResponderEmails();
$responders_to_add = array();
foreach($new_survey_responders as $new){
if(!in_array($new, $existing_survey_reposnders)) {
$responders_to_add[] = $new;
}
}
$all_responders = array_merge($existing_survey_reposnders, $responders_to_add);
//if there is no new emails to add to survey, then it's look like this is duplicated request
//so do nothing with this and prevent to send duplicated confirmation
if(count($responders_to_add) <= 0) {
return true;
} else {
//check if there are still place to add new recipients according to account max recipients constant
if($account->getAccountMaxRecipients() >= count($all_responders)) {
//IF THERE IS PLACE then add new recipients to existing survey and mark as duplicated, so the confirmation email will not be send twice
foreach($responders_to_add as $responder){
$survey_check->addResponder($responder);
}
$survey_check->save();
return true;
} else {
//IF THERE ARE NO PLACE FOR NEW RESPONDERS, then new survey should be created - so return this as not duplicated
return false;
}
}
$survey = $survey_check;
return true;
} else {
return false;
}
}