commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / bin / deprecated / RespondentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 public function __construct() {
44 $config = CRM_Core_Config::singleton();
45
46 //this does not return on failure
47 require_once 'CRM/Utils/System.php';
48 require_once 'CRM/Utils/Hook.php';
49
50 CRM_Utils_System::authenticateScript(TRUE);
51
52 //log the execution time of script
53 CRM_Core_Error::debug_log_message('RespondentProcessor.php');
54 }
55
56 public function releaseRespondent() {
57 require_once 'CRM/Core/PseudoConstant.php';
58 require_once 'CRM/Campaign/BAO/Survey.php';
59 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
60 $reserveStatusId = array_search('Scheduled', $activityStatus);
61 $surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType();
62 $surveyActivityTypesIds = array_keys($surveyActivityTypes);
63
64 //retrieve all survey activities related to reserve action.
65 $releasedCount = 0;
66 if ($reserveStatusId && !empty($surveyActivityTypesIds)) {
67 $query = '
68 SELECT activity.id as id,
69 activity.activity_date_time as activity_date_time,
70 survey.id as surveyId,
71 survey.release_frequency as release_frequency
72 FROM civicrm_activity activity
73 INNER JOIN civicrm_survey survey ON ( survey.id = activity.source_record_id )
74 WHERE activity.is_deleted = 0
75 AND activity.status_id = %1
76 AND activity.activity_type_id IN ( ' . implode(', ', $surveyActivityTypesIds) . ' )';
77 $activity = CRM_Core_DAO::executeQuery($query, array(1 => array($reserveStatusId, 'Positive')));
78 $releasedIds = array();
79 while ($activity->fetch()) {
80 if (!$activity->release_frequency) {
81 continue;
82 }
83 $reservedSeconds = CRM_Utils_Date::unixTime($activity->activity_date_time);
84 $releasedSeconds = $activity->release_frequency * 24 * 3600;
85 $totalReservedSeconds = $reservedSeconds + $releasedSeconds;
86 if ($totalReservedSeconds < time()) {
87 $releasedIds[$activity->id] = $activity->id;
88 }
89 }
90
91 //released respondent.
92 if (!empty($releasedIds)) {
93 $query = '
94 UPDATE civicrm_activity
95 SET is_deleted = 1
96 WHERE id IN ( ' . implode(', ', $releasedIds) . ' )';
97 CRM_Core_DAO::executeQuery($query);
98 $releasedCount = count($releasedIds);
99 }
100 }
101
102 echo "<br /><br />Number of respondents released = {$releasedCount}";
103 }
104
105 }
106
107 $obj = new CRM_RespondentProcessor();
108 echo "Releasing..";
109 $obj->releaseRespondent();
110 echo "<br /><br />Respondent Release Done";