3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to add contacts for voter reservation.
21 class CRM_Campaign_Form_Task_Reserve
extends CRM_Campaign_Form_Task
{
34 protected $_interviewerId;
41 protected $_surveyDetails;
48 protected $_numVoters;
51 * Build all the data structures needed to build the form.
53 public function preProcess() {
56 //get the survey id from user submitted values.
57 $this->_surveyId
= $this->get('surveyId');
58 $this->_interviewerId
= $this->get('interviewerId');
59 if (!$this->_surveyId
) {
60 CRM_Core_Error
::statusBounce(ts("Could not find Survey Id."));
62 if (!$this->_interviewerId
) {
63 CRM_Core_Error
::statusBounce(ts("Missing Interviewer contact."));
65 if (!is_array($this->_contactIds
) ||
empty($this->_contactIds
)) {
66 CRM_Core_Error
::statusBounce(ts("Could not find contacts for reservation."));
69 $params = ['id' => $this->_surveyId
];
70 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_surveyDetails
);
72 //get the survey activities.
73 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
75 foreach (['Scheduled'] as $name) {
76 if ($statusId = array_search($name, $activityStatus)) {
77 $statusIds[] = $statusId;
81 // these are the activities count that are linked to the current
82 // interviewer and current survey and not the list of ALL survey activities
83 $this->_numVoters
= CRM_Campaign_BAO_Survey
::getSurveyActivities($this->_surveyId
,
84 $this->_interviewerId
,
89 //validate the selected survey.
90 $this->validateSurvey();
91 $this->assign('surveyTitle', $this->_surveyDetails
['title']);
92 $this->assign('activityType', $this->_surveyDetails
['activity_type_id']);
93 $this->assign('surveyId', $this->_surveyId
);
95 //append breadcrumb to survey dashboard.
96 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
97 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
98 CRM_Utils_System
::appendBreadCrumb([['title' => ts('Survey(s)'), 'url' => $url]]);
102 $this->setTitle(ts('Reserve Respondents'));
105 public function validateSurvey() {
107 $maxVoters = $this->_surveyDetails
['max_number_of_contacts'] ??
NULL;
109 if ($maxVoters <= $this->_numVoters
) {
110 $errorMsg = ts('The maximum number of contacts is already reserved for this interviewer.');
112 elseif (count($this->_contactIds
) > ($maxVoters - $this->_numVoters
)) {
113 $errorMsg = ts('You can reserve a maximum of %count contact at a time for this survey.',
115 'plural' => 'You can reserve a maximum of %count contacts at a time for this survey.',
116 'count' => $maxVoters - $this->_numVoters
,
122 $defaultNum = $this->_surveyDetails
['default_number_of_contacts'] ??
NULL;
123 if (!$errorMsg && $defaultNum && (count($this->_contactIds
) > $defaultNum)) {
124 $errorMsg = ts('You can reserve a maximum of %count contact at a time for this survey.',
126 'plural' => 'You can reserve a maximum of %count contacts at a time for this survey.',
127 'count' => $defaultNum,
133 CRM_Core_Error
::statusBounce($errorMsg);
138 * Build the form object.
140 public function buildQuickForm() {
141 // allow to add contact to either new or existing group.
142 $this->addElement('text', 'ActivityType', ts('Activity Type'));
143 $this->addElement('text', 'newGroupName', ts('Name for new group'));
144 $this->addElement('text', 'newGroupDesc', ts('Description of new group'));
145 $groups = CRM_Core_PseudoConstant
::nestedGroup();
146 $hasExistingGroups = FALSE;
147 if (is_array($groups) && !empty($groups)) {
148 $hasExistingGroups = TRUE;
149 $this->addElement('select', 'groups', ts('Add respondent(s) to existing group(s)'),
150 $groups, ['multiple' => "multiple", 'class' => 'crm-select2']
153 $this->assign('hasExistingGroups', $hasExistingGroups);
158 'name' => ts('Reserve'),
159 'subName' => 'reserve',
164 if (CRM_Core_Permission
::check('manage campaign') ||
165 CRM_Core_Permission
::check('administer CiviCampaign') ||
166 CRM_Core_Permission
::check('interview campaign contacts')
170 'name' => ts('Reserve and Interview'),
171 'subName' => 'reserveToInterview',
176 'name' => ts('Cancel'),
179 $this->addButtons($buttons);
180 $this->addFormRule(['CRM_Campaign_Form_Task_Reserve', 'formRule'], $this);
184 * Global validation rules for the form.
186 * @param array $fields
187 * Posted values of the form.
193 * list of errors to be posted back to the form
195 public static function formRule($fields, $files, $self) {
197 $invalidGroupName = FALSE;
198 if (!empty($fields['newGroupName'])) {
199 $title = trim($fields['newGroupName']);
200 $name = CRM_Utils_String
::titleToVar($title);
201 $query = 'select count(*) from civicrm_group where name like %1 OR title like %2';
202 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, [
203 1 => [$name, 'String'],
204 2 => [$title, 'String'],
207 $invalidGroupName = TRUE;
208 $errors['newGroupName'] = ts('Group \'%1\' already exists.', [1 => $fields['newGroupName']]);
211 $self->assign('invalidGroupName', $invalidGroupName);
213 return empty($errors) ?
TRUE : $errors;
217 * Process the form after the input has been submitted and validated.
219 public function postProcess() {
222 $maxVoters = $this->_surveyDetails
['max_number_of_contacts'] ??
NULL;
223 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
224 $statusHeld = array_search('Scheduled', $activityStatus);
226 $reservedVoterIds = [];
227 foreach ($this->_contactIds
as $cid) {
228 $subject = $this->_surveyDetails
['title'] . ' - ' . ts('Respondent Reservation');
229 $session = CRM_Core_Session
::singleton();
231 'source_contact_id' => $session->get('userID'),
232 'assignee_contact_id' => [$this->_interviewerId
],
233 'target_contact_id' => [$cid],
234 'source_record_id' => $this->_surveyId
,
235 'activity_type_id' => $this->_surveyDetails
['activity_type_id'],
236 'subject' => $subject,
237 'activity_date_time' => date('YmdHis'),
238 'status_id' => $statusHeld,
239 'skipRecentView' => 1,
240 'campaign_id' => $this->_surveyDetails
['campaign_id'] ??
NULL,
242 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
245 $reservedVoterIds[$cid] = $cid;
247 if ($maxVoters && ($maxVoters <= ($this->_numVoters +
$countVoters))) {
252 //add reserved voters to groups.
253 $groupAdditions = $this->_addRespondentToGroup($reservedVoterIds);
256 if ($countVoters > 0) {
257 $status = '<p>' . ts("%count contact has been reserved.", ['plural' => '%count contacts have been reserved.', 'count' => $countVoters]) . '</p>';
258 if ($groupAdditions) {
259 $status .= '<p>' . ts('They have been added to %1.',
260 [1 => implode(' ' . ts('and') . ' ', $groupAdditions)]
263 CRM_Core_Session
::setStatus($status, ts('Reservation Added'), 'success');
266 if (count($this->_contactIds
) > $countVoters) {
267 CRM_Core_Session
::setStatus(ts('Reservation did not add for %count contact.',
269 'plural' => 'Reservation did not add for %count contacts.',
270 'count' => (count($this->_contactIds
) - $countVoters),
275 //get ready to jump to voter interview form.
276 $buttonName = $this->controller
->getButtonName();
277 if (!empty($reservedVoterIds) &&
278 $buttonName == '_qf_Reserve_next_reserveToInterview'
280 $this->controller
->set('surveyId', $this->_surveyId
);
281 $this->controller
->set('contactIds', $reservedVoterIds);
282 $this->controller
->set('interviewerId', $this->_interviewerId
);
283 $this->controller
->set('reserveToInterview', TRUE);
292 private function _addRespondentToGroup($contactIds) {
293 $groupAdditions = [];
294 if (empty($contactIds)) {
295 return $groupAdditions;
298 $params = $this->controller
->exportValues($this->_name
);
299 $groups = CRM_Utils_Array
::value('groups', $params, []);
300 $newGroupName = $params['newGroupName'] ??
NULL;
301 $newGroupDesc = $params['newGroupDesc'] ??
NULL;
307 'title' => $newGroupName,
308 'description' => $newGroupDesc,
311 $group = CRM_Contact_BAO_Group
::create($grpParams);
312 $groups[] = $newGroupId = $group->id
;
315 //add the respondents to groups.
316 if (is_array($groups)) {
317 $existingGroups = CRM_Core_PseudoConstant
::group();
318 foreach ($groups as $groupId) {
319 $addCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds, $groupId);
320 $totalCount = $addCount[1] ??
NULL;
321 if ($groupId == $newGroupId) {
322 $name = $newGroupName;
326 $name = $existingGroups[$groupId];
330 $url = CRM_Utils_System
::url('civicrm/group/search',
331 'reset=1&force=1&context=smog&gid=' . $groupId
333 $groupAdditions[] = '<a href="' . $url . '">' . $name . '</a>';
338 return $groupAdditions;