Merge pull request #4950 from atif-shaikh/CRM-15826
[civicrm-core.git] / CRM / Campaign / Form / Task / Release.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 */
70 public function preProcess() {
71 $this->_interviewToRelease = $this->get('interviewToRelease');
72 if ($this->_interviewToRelease) {
73 //user came from interview form.
74 foreach (array(
75 'surveyId',
76 'contactIds',
77 'interviewerId',
78 ) as $fld) {
79 $this->{"_$fld"} = $this->get($fld);
80 }
81
82 if (!empty($this->_contactIds)) {
83 $this->assign('totalSelectedContacts', count($this->_contactIds));
84 }
85 }
86 else {
87 parent::preProcess();
88 //get the survey id from user submitted values.
89 $this->_surveyId = CRM_Utils_Array::value('campaign_survey_id', $this->get('formValues'));
90 $this->_interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->get('formValues'));
91 }
92
93 if (!$this->_surveyId) {
94 CRM_Core_Error::statusBounce(ts("Please search with 'Survey', to apply this action."));
95 }
96 if (!$this->_interviewerId) {
97 CRM_Core_Error::statusBounce(ts('Missing Interviewer contact.'));
98 }
99 if (!is_array($this->_contactIds) || empty($this->_contactIds)) {
100 CRM_Core_Error::statusBounce(ts('Could not find respondents to release.'));
101 }
102
103 $surveyDetails = array();
104 $params = array('id' => $this->_surveyId);
105 $this->_surveyDetails = CRM_Campaign_BAO_Survey::retrieve($params, $surveyDetails);
106
107 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
108 $statusIds = array();
109 foreach (array(
110 'Scheduled',
111 ) as $name) {
112 if ($statusId = array_search($name, $activityStatus)) {
113 $statusIds[] = $statusId;
114 }
115 }
116 //fetch the target survey activities.
117 $this->_surveyActivities = CRM_Campaign_BAO_Survey::voterActivityDetails($this->_surveyId,
118 $this->_contactIds,
119 $this->_interviewerId,
120 $statusIds
121 );
122 if (count($this->_surveyActivities) < 1) {
123 CRM_Core_Error::statusBounce(ts('We could not found respondent for this survey to release.'));
124 }
125
126 $this->assign('surveyTitle', $surveyDetails['title']);
127
128 //append breadcrumb to survey dashboard.
129 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
130 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
131 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
132 }
133
134 //set the title.
135 CRM_Utils_System::setTitle(ts('Release Respondents'));
136 }
137
138 /**
139 * Build the form object
140 *
141 *
142 * @return void
143 */
144 public function buildQuickForm() {
145
146 $this->addDefaultButtons(ts('Release Respondents'), 'done');
147 }
148
149 public function postProcess() {
150 $deleteActivityIds = array();
151 foreach ($this->_contactIds as $cid) {
152 if (array_key_exists($cid, $this->_surveyActivities)) {
153 $deleteActivityIds[] = $this->_surveyActivities[$cid]['activity_id'];
154 }
155 }
156
157 //set survey activites as deleted = true.
158 if (!empty($deleteActivityIds)) {
159 $query = 'UPDATE civicrm_activity SET is_deleted = 1 WHERE id IN ( ' . implode(', ', $deleteActivityIds) . ' )';
160 CRM_Core_DAO::executeQuery($query);
161
162 $status = array(ts("%1 respondent(s) have been released.", array(1 => count($deleteActivityIds))));
163 if (count($this->_contactIds) > count($deleteActivityIds)) {
164 $status[] = ts("%1 respondents did not release.",
165 array(1 => (count($this->_contactIds) - count($deleteActivityIds)))
166 );
167 }
168 CRM_Core_Session::setStatus(implode('&nbsp;', $status), '', 'info');
169 }
170 }
171 }