Merge pull request #16736 from ixiam/dev_report_issue#27
[civicrm-core.git] / CRM / Campaign / Form / Task / Release.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to add contacts for voter reservation.
20 */
21 class CRM_Campaign_Form_Task_Release extends CRM_Campaign_Form_Task {
22
23 /**
24 * Survet id
25 *
26 * @var int
27 */
28 protected $_surveyId;
29
30 /**
31 * Number of voters
32 *
33 * @var int
34 */
35 protected $_interviewerId;
36
37 /**
38 * Survey details
39 *
40 * @var object
41 */
42 protected $_surveyDetails;
43
44 protected $_surveyActivities;
45
46 /**
47 * Build all the data structures needed to build the form.
48 */
49 public function preProcess() {
50 $this->_interviewToRelease = $this->get('interviewToRelease');
51 if ($this->_interviewToRelease) {
52 //user came from interview form.
53 foreach ([
54 'surveyId',
55 'contactIds',
56 'interviewerId',
57 ] as $fld) {
58 $this->{"_$fld"} = $this->get($fld);
59 }
60
61 if (!empty($this->_contactIds)) {
62 $this->assign('totalSelectedContacts', count($this->_contactIds));
63 }
64 }
65 else {
66 parent::preProcess();
67 //get the survey id from user submitted values.
68 $this->_surveyId = CRM_Utils_Array::value('campaign_survey_id', $this->get('formValues'));
69 $this->_interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->get('formValues'));
70 }
71
72 if (!$this->_surveyId) {
73 CRM_Core_Error::statusBounce(ts("Please search with 'Survey', to apply this action."));
74 }
75 if (!$this->_interviewerId) {
76 CRM_Core_Error::statusBounce(ts('Missing Interviewer contact.'));
77 }
78 if (!is_array($this->_contactIds) || empty($this->_contactIds)) {
79 CRM_Core_Error::statusBounce(ts('Could not find respondents to release.'));
80 }
81
82 $surveyDetails = [];
83 $params = ['id' => $this->_surveyId];
84 $this->_surveyDetails = CRM_Campaign_BAO_Survey::retrieve($params, $surveyDetails);
85
86 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
87 $statusIds = [];
88 foreach (['Scheduled'] as $name) {
89 if ($statusId = array_search($name, $activityStatus)) {
90 $statusIds[] = $statusId;
91 }
92 }
93 //fetch the target survey activities.
94 $this->_surveyActivities = CRM_Campaign_BAO_Survey::voterActivityDetails($this->_surveyId,
95 $this->_contactIds,
96 $this->_interviewerId,
97 $statusIds
98 );
99 if (count($this->_surveyActivities) < 1) {
100 CRM_Core_Error::statusBounce(ts('We could not found respondent for this survey to release.'));
101 }
102
103 $this->assign('surveyTitle', $surveyDetails['title']);
104
105 //append breadcrumb to survey dashboard.
106 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
107 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
108 CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey(s)'), 'url' => $url]]);
109 }
110
111 //set the title.
112 CRM_Utils_System::setTitle(ts('Release Respondents'));
113 }
114
115 /**
116 * Build the form object.
117 */
118 public function buildQuickForm() {
119
120 $this->addDefaultButtons(ts('Release Respondents'), 'done');
121 }
122
123 public function postProcess() {
124 $deleteActivityIds = [];
125 foreach ($this->_contactIds as $cid) {
126 if (array_key_exists($cid, $this->_surveyActivities)) {
127 $deleteActivityIds[] = $this->_surveyActivities[$cid]['activity_id'];
128 }
129 }
130
131 //set survey activities as deleted = true.
132 if (!empty($deleteActivityIds)) {
133 $query = 'UPDATE civicrm_activity SET is_deleted = 1 WHERE id IN ( ' . implode(', ', $deleteActivityIds) . ' )';
134 CRM_Core_DAO::executeQuery($query);
135
136 if ($deleteActivityIds) {
137 $status = ts("Respondent has been released.", [
138 'count' => count($deleteActivityIds),
139 'plural' => '%count respondents have been released.',
140 ]);
141 CRM_Core_Session::setStatus($status, ts('Released'), 'success');
142 }
143
144 if (count($this->_contactIds) > count($deleteActivityIds)) {
145 $status = ts('1 respondent did not release.',
146 [
147 'count' => (count($this->_contactIds) - count($deleteActivityIds)),
148 'plural' => '%count respondents did not release.',
149 ]
150 );
151 CRM_Core_Session::setStatus($status, ts('Notice'), 'alert');
152 }
153 }
154 }
155
156 }