Merge pull request #18302 from civicrm/5.29
[civicrm-core.git] / CRM / Campaign / Form / Task / Release.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
3819f101 19 * This class provides the functionality to add contacts for voter reservation.
6a488035
TO
20 */
21class CRM_Campaign_Form_Task_Release extends CRM_Campaign_Form_Task {
22
23 /**
100fef9d 24 * Survet id
6a488035
TO
25 *
26 * @var int
27 */
28 protected $_surveyId;
29
30 /**
100fef9d 31 * Number of voters
6a488035
TO
32 *
33 * @var int
34 */
35 protected $_interviewerId;
36
37 /**
100fef9d 38 * Survey details
6a488035
TO
39 *
40 * @var object
41 */
42 protected $_surveyDetails;
43
44 protected $_surveyActivities;
45
46 /**
fe482240 47 * Build all the data structures needed to build the form.
95ea96be 48 */
28a04ea9 49 public function preProcess() {
6a488035
TO
50 $this->_interviewToRelease = $this->get('interviewToRelease');
51 if ($this->_interviewToRelease) {
52 //user came from interview form.
be2fb01f 53 foreach ([
5d4fcf54
TO
54 'surveyId',
55 'contactIds',
56 'interviewerId',
57 ] as $fld) {
6a488035
TO
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
be2fb01f
CW
82 $surveyDetails = [];
83 $params = ['id' => $this->_surveyId];
6a488035
TO
84 $this->_surveyDetails = CRM_Campaign_BAO_Survey::retrieve($params, $surveyDetails);
85
86 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
be2fb01f 87 $statusIds = [];
5d4fcf54 88 foreach (['Scheduled'] as $name) {
6a488035
TO
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');
be2fb01f 108 CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey(s)'), 'url' => $url]]);
6a488035
TO
109 }
110
111 //set the title.
112 CRM_Utils_System::setTitle(ts('Release Respondents'));
113 }
114
115 /**
fe482240 116 * Build the form object.
6a488035 117 */
00be9182 118 public function buildQuickForm() {
6a488035
TO
119
120 $this->addDefaultButtons(ts('Release Respondents'), 'done');
121 }
122
00be9182 123 public function postProcess() {
be2fb01f 124 $deleteActivityIds = [];
6a488035
TO
125 foreach ($this->_contactIds as $cid) {
126 if (array_key_exists($cid, $this->_surveyActivities)) {
127 $deleteActivityIds[] = $this->_surveyActivities[$cid]['activity_id'];
128 }
129 }
130
b44e3f84 131 //set survey activities as deleted = true.
6a488035
TO
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
99483ce8 136 if ($deleteActivityIds) {
be2fb01f 137 $status = ts("Respondent has been released.", [
99483ce8
CW
138 'count' => count($deleteActivityIds),
139 'plural' => '%count respondents have been released.',
be2fb01f 140 ]);
99483ce8
CW
141 CRM_Core_Session::setStatus($status, ts('Released'), 'success');
142 }
143
6a488035 144 if (count($this->_contactIds) > count($deleteActivityIds)) {
99483ce8 145 $status = ts('1 respondent did not release.',
be2fb01f 146 [
99483ce8
CW
147 'count' => (count($this->_contactIds) - count($deleteActivityIds)),
148 'plural' => '%count respondents did not release.',
be2fb01f 149 ]
6a488035 150 );
99483ce8 151 CRM_Core_Session::setStatus($status, ts('Notice'), 'alert');
6a488035 152 }
6a488035
TO
153 }
154 }
96025800 155
6a488035 156}