Merge pull request #15237 from civicrm/5.18
[civicrm-core.git] / CRM / Campaign / Form / Task / Release.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 ([
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 = [];
99 $params = ['id' => $this->_surveyId];
100 $this->_surveyDetails = CRM_Campaign_BAO_Survey::retrieve($params, $surveyDetails);
101
102 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
103 $statusIds = [];
104 foreach (['Scheduled'] as $name) {
105 if ($statusId = array_search($name, $activityStatus)) {
106 $statusIds[] = $statusId;
107 }
108 }
109 //fetch the target survey activities.
110 $this->_surveyActivities = CRM_Campaign_BAO_Survey::voterActivityDetails($this->_surveyId,
111 $this->_contactIds,
112 $this->_interviewerId,
113 $statusIds
114 );
115 if (count($this->_surveyActivities) < 1) {
116 CRM_Core_Error::statusBounce(ts('We could not found respondent for this survey to release.'));
117 }
118
119 $this->assign('surveyTitle', $surveyDetails['title']);
120
121 //append breadcrumb to survey dashboard.
122 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
123 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
124 CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey(s)'), 'url' => $url]]);
125 }
126
127 //set the title.
128 CRM_Utils_System::setTitle(ts('Release Respondents'));
129 }
130
131 /**
132 * Build the form object.
133 */
134 public function buildQuickForm() {
135
136 $this->addDefaultButtons(ts('Release Respondents'), 'done');
137 }
138
139 public function postProcess() {
140 $deleteActivityIds = [];
141 foreach ($this->_contactIds as $cid) {
142 if (array_key_exists($cid, $this->_surveyActivities)) {
143 $deleteActivityIds[] = $this->_surveyActivities[$cid]['activity_id'];
144 }
145 }
146
147 //set survey activities as deleted = true.
148 if (!empty($deleteActivityIds)) {
149 $query = 'UPDATE civicrm_activity SET is_deleted = 1 WHERE id IN ( ' . implode(', ', $deleteActivityIds) . ' )';
150 CRM_Core_DAO::executeQuery($query);
151
152 if ($deleteActivityIds) {
153 $status = ts("Respondent has been released.", [
154 'count' => count($deleteActivityIds),
155 'plural' => '%count respondents have been released.',
156 ]);
157 CRM_Core_Session::setStatus($status, ts('Released'), 'success');
158 }
159
160 if (count($this->_contactIds) > count($deleteActivityIds)) {
161 $status = ts('1 respondent did not release.',
162 [
163 'count' => (count($this->_contactIds) - count($deleteActivityIds)),
164 'plural' => '%count respondents did not release.',
165 ]
166 );
167 CRM_Core_Session::setStatus($status, ts('Notice'), 'alert');
168 }
169 }
170 }
171
172 }