Merge branch 'master' into findById
[civicrm-core.git] / CRM / Campaign / Form / Task / Release.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 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality to add contacts for
38 * voter reservation.
39 */
40 class CRM_Campaign_Form_Task_Release extends CRM_Campaign_Form_Task {
41
42 /**
43 * Survet id
44 *
45 * @var int
46 */
47 protected $_surveyId;
48
49 /**
50 * Number of voters
51 *
52 * @var int
53 */
54 protected $_interviewerId;
55
56 /**
57 * Survey details
58 *
59 * @var object
60 */
61 protected $_surveyDetails;
62
63 protected $_surveyActivities;
64
65 /**
66 * Build all the data structures needed to build the form
67 *
68 * @return void
69 * @access public
70 */ function preProcess() {
71 $this->_interviewToRelease = $this->get('interviewToRelease');
72 if ($this->_interviewToRelease) {
73 //user came from interview form.
74 foreach (array(
75 'surveyId', 'contactIds', 'interviewerId') as $fld) {
76 $this->{"_$fld"} = $this->get($fld);
77 }
78
79 if (!empty($this->_contactIds)) {
80 $this->assign('totalSelectedContacts', count($this->_contactIds));
81 }
82 }
83 else {
84 parent::preProcess();
85 //get the survey id from user submitted values.
86 $this->_surveyId = CRM_Utils_Array::value('campaign_survey_id', $this->get('formValues'));
87 $this->_interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->get('formValues'));
88 }
89
90 if (!$this->_surveyId) {
91 CRM_Core_Error::statusBounce(ts("Please search with 'Survey', to apply this action."));
92 }
93 if (!$this->_interviewerId) {
94 CRM_Core_Error::statusBounce(ts('Missing Interviewer contact.'));
95 }
96 if (!is_array($this->_contactIds) || empty($this->_contactIds)) {
97 CRM_Core_Error::statusBounce(ts('Could not find respondents to release.'));
98 }
99
100 $surveyDetails = array();
101 $params = array('id' => $this->_surveyId);
102 $this->_surveyDetails = CRM_Campaign_BAO_Survey::retrieve($params, $surveyDetails);
103
104 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
105 $statusIds = array();
106 foreach (array(
107 'Scheduled') as $name) {
108 if ($statusId = array_search($name, $activityStatus)) {
109 $statusIds[] = $statusId;
110 }
111 }
112 //fetch the target survey activities.
113 $this->_surveyActivities = CRM_Campaign_BAO_Survey::voterActivityDetails($this->_surveyId,
114 $this->_contactIds,
115 $this->_interviewerId,
116 $statusIds
117 );
118 if (count($this->_surveyActivities) < 1) {
119 CRM_Core_Error::statusBounce(ts('We could not found respondent for this survey to release.'));
120 }
121
122 $this->assign('surveyTitle', $surveyDetails['title']);
123
124 //append breadcrumb to survey dashboard.
125 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
126 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
127 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
128 }
129
130 //set the title.
131 CRM_Utils_System::setTitle(ts('Release Respondents'));
132 }
133
134 /**
135 * Build the form object
136 *
137 * @access public
138 *
139 * @return void
140 */
141 function buildQuickForm() {
142
143 $this->addDefaultButtons(ts('Release Respondents'), 'done');
144 }
145
146 function postProcess() {
147 $deleteActivityIds = array();
148 foreach ($this->_contactIds as $cid) {
149 if (array_key_exists($cid, $this->_surveyActivities)) {
150 $deleteActivityIds[] = $this->_surveyActivities[$cid]['activity_id'];
151 }
152 }
153
154 //set survey activites as deleted = true.
155 if (!empty($deleteActivityIds)) {
156 $query = 'UPDATE civicrm_activity SET is_deleted = 1 WHERE id IN ( ' . implode(', ', $deleteActivityIds) . ' )';
157 CRM_Core_DAO::executeQuery($query);
158
159 $status = array(ts("%1 respondent(s) have been released.", array(1 => count($deleteActivityIds))));
160 if (count($this->_contactIds) > count($deleteActivityIds)) {
161 $status[] = ts("%1 respondents did not release.",
162 array(1 => (count($this->_contactIds) - count($deleteActivityIds)))
163 );
164 }
165 CRM_Core_Session::setStatus(implode('&nbsp;', $status), '', 'info');
166 }
167 }
168 }
169