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