Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
f5721b07 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f5721b07 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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. | |
783f84c9 | 31 | * |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once '../civicrm.config.php'; | |
35 | require_once 'CRM/Core/Config.php'; | |
4e87860d EM |
36 | |
37 | /** | |
38 | * Class CRM_RespondentProcessor | |
39 | */ | |
6a488035 | 40 | class CRM_RespondentProcessor { |
4e87860d EM |
41 | /** |
42 | * | |
43 | */ | |
6a488035 TO |
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 = ' | |
783f84c9 | 69 | SELECT activity.id as id, |
6a488035 TO |
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 | |
783f84c9 | 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 | |
6a488035 TO |
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 |