Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-11-14-11-21-50
[civicrm-core.git] / bin / deprecated / RespondentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28
29 /*
30 * This file check and update the survey respondents.
31 *
32 */
33
34 require_once '../civicrm.config.php';
35 require_once 'CRM/Core/Config.php';
36
37 /**
38 * Class CRM_RespondentProcessor
39 */
40 class CRM_RespondentProcessor {
41 /**
42 *
43 */
44 function __construct() {
45 $config = CRM_Core_Config::singleton();
46
47 //this does not return on failure
48 require_once 'CRM/Utils/System.php';
49 require_once 'CRM/Utils/Hook.php';
50
51 CRM_Utils_System::authenticateScript(TRUE);
52
53 //log the execution time of script
54 CRM_Core_Error::debug_log_message('RespondentProcessor.php');
55 }
56
57 public function releaseRespondent() {
58 require_once 'CRM/Core/PseudoConstant.php';
59 require_once 'CRM/Campaign/BAO/Survey.php';
60 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
61 $reserveStatusId = array_search('Scheduled', $activityStatus);
62 $surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType();
63 $surveyActivityTypesIds = array_keys($surveyActivityTypes);
64
65 //retrieve all survey activities related to reserve action.
66 $releasedCount = 0;
67 if ($reserveStatusId && !empty($surveyActivityTypesIds)) {
68 $query = '
69 SELECT activity.id as id,
70 activity.activity_date_time as activity_date_time,
71 survey.id as surveyId,
72 survey.release_frequency as release_frequency
73 FROM civicrm_activity activity
74 INNER JOIN civicrm_survey survey ON ( survey.id = activity.source_record_id )
75 WHERE activity.is_deleted = 0
76 AND activity.status_id = %1
77 AND activity.activity_type_id IN ( ' . implode(', ', $surveyActivityTypesIds) . ' )';
78 $activity = CRM_Core_DAO::executeQuery($query, array(1 => array($reserveStatusId, 'Positive')));
79 $releasedIds = array();
80 while ($activity->fetch()) {
81 if (!$activity->release_frequency) {
82 continue;
83 }
84 $reservedSeconds = CRM_Utils_Date::unixTime($activity->activity_date_time);
85 $releasedSeconds = $activity->release_frequency * 24 * 3600;
86 $totalReservedSeconds = $reservedSeconds + $releasedSeconds;
87 if ($totalReservedSeconds < time()) {
88 $releasedIds[$activity->id] = $activity->id;
89 }
90 }
91
92 //released respondent.
93 if (!empty($releasedIds)) {
94 $query = '
95 UPDATE civicrm_activity
96 SET is_deleted = 1
97 WHERE id IN ( ' . implode(', ', $releasedIds) . ' )';
98 CRM_Core_DAO::executeQuery($query);
99 $releasedCount = count($releasedIds);
100 }
101 }
102
103 echo "<br /><br />Number of respondents released = {$releasedCount}";
104 }
105 }
106
107 $obj = new CRM_RespondentProcessor();
108 echo "Releasing..";
109 $obj->releaseRespondent();
110 echo "<br /><br />Respondent Release Done";
111
112