Merge pull request #14878 from civicrm/5.16
[civicrm-core.git] / CRM / Campaign / Form / Task / Release.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
3819f101 35 * This class provides the functionality to add contacts for voter reservation.
6a488035
TO
36 */
37class CRM_Campaign_Form_Task_Release extends CRM_Campaign_Form_Task {
38
39 /**
100fef9d 40 * Survet id
6a488035
TO
41 *
42 * @var int
43 */
44 protected $_surveyId;
45
46 /**
100fef9d 47 * Number of voters
6a488035
TO
48 *
49 * @var int
50 */
51 protected $_interviewerId;
52
53 /**
100fef9d 54 * Survey details
6a488035
TO
55 *
56 * @var object
57 */
58 protected $_surveyDetails;
59
60 protected $_surveyActivities;
61
62 /**
fe482240 63 * Build all the data structures needed to build the form.
95ea96be 64 */
28a04ea9 65 public function preProcess() {
6a488035
TO
66 $this->_interviewToRelease = $this->get('interviewToRelease');
67 if ($this->_interviewToRelease) {
68 //user came from interview form.
be2fb01f 69 foreach ([
5d4fcf54
TO
70 'surveyId',
71 'contactIds',
72 'interviewerId',
73 ] as $fld) {
6a488035
TO
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
be2fb01f
CW
98 $surveyDetails = [];
99 $params = ['id' => $this->_surveyId];
6a488035
TO
100 $this->_surveyDetails = CRM_Campaign_BAO_Survey::retrieve($params, $surveyDetails);
101
102 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
be2fb01f 103 $statusIds = [];
5d4fcf54 104 foreach (['Scheduled'] as $name) {
6a488035
TO
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');
be2fb01f 124 CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey(s)'), 'url' => $url]]);
6a488035
TO
125 }
126
127 //set the title.
128 CRM_Utils_System::setTitle(ts('Release Respondents'));
129 }
130
131 /**
fe482240 132 * Build the form object.
6a488035 133 */
00be9182 134 public function buildQuickForm() {
6a488035
TO
135
136 $this->addDefaultButtons(ts('Release Respondents'), 'done');
137 }
138
00be9182 139 public function postProcess() {
be2fb01f 140 $deleteActivityIds = [];
6a488035
TO
141 foreach ($this->_contactIds as $cid) {
142 if (array_key_exists($cid, $this->_surveyActivities)) {
143 $deleteActivityIds[] = $this->_surveyActivities[$cid]['activity_id'];
144 }
145 }
146
b44e3f84 147 //set survey activities as deleted = true.
6a488035
TO
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
99483ce8 152 if ($deleteActivityIds) {
be2fb01f 153 $status = ts("Respondent has been released.", [
99483ce8
CW
154 'count' => count($deleteActivityIds),
155 'plural' => '%count respondents have been released.',
be2fb01f 156 ]);
99483ce8
CW
157 CRM_Core_Session::setStatus($status, ts('Released'), 'success');
158 }
159
6a488035 160 if (count($this->_contactIds) > count($deleteActivityIds)) {
99483ce8 161 $status = ts('1 respondent did not release.',
be2fb01f 162 [
99483ce8
CW
163 'count' => (count($this->_contactIds) - count($deleteActivityIds)),
164 'plural' => '%count respondents did not release.',
be2fb01f 165 ]
6a488035 166 );
99483ce8 167 CRM_Core_Session::setStatus($status, ts('Notice'), 'alert');
6a488035 168 }
6a488035
TO
169 }
170 }
96025800 171
6a488035 172}