3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class provides the functionality to add contacts for
40 class CRM_Campaign_Form_Task_Reserve
extends CRM_Campaign_Form_Task
{
54 protected $_interviewerId;
61 protected $_surveyDetails;
68 protected $_numVoters;
71 * Build all the data structures needed to build the form
75 public function preProcess() {
78 //get the survey id from user submitted values.
79 $this->_surveyId
= $this->get('surveyId');
80 $this->_interviewerId
= $this->get('interviewerId');
81 if (!$this->_surveyId
) {
82 CRM_Core_Error
::statusBounce(ts("Could not find Survey Id."));
84 if (!$this->_interviewerId
) {
85 CRM_Core_Error
::statusBounce(ts("Missing Interviewer contact."));
87 if (!is_array($this->_contactIds
) ||
empty($this->_contactIds
)) {
88 CRM_Core_Error
::statusBounce(ts("Could not find contacts for reservation."));
91 $params = array('id' => $this->_surveyId
);
92 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_surveyDetails
);
94 //get the survey activities.
95 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
97 foreach (array('Scheduled') as $name) {
98 if ($statusId = array_search($name, $activityStatus)) {
99 $statusIds[] = $statusId;
103 // these are the activities count that are linked to the current
104 // interviewer and current survey and not the list of ALL survey activities
105 $this->_numVoters
= CRM_Campaign_BAO_Survey
::getSurveyActivities($this->_surveyId
,
106 $this->_interviewerId
,
111 //validate the selected survey.
112 $this->validateSurvey();
113 $this->assign('surveyTitle', $this->_surveyDetails
['title']);
114 $this->assign('activityType', $this->_surveyDetails
['activity_type_id']);
115 $this->assign('surveyId', $this->_surveyId
);
117 //append breadcrumb to survey dashboard.
118 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
119 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
120 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
124 CRM_Utils_System
::setTitle(ts('Reserve Respondents'));
127 public function validateSurvey() {
129 $maxVoters = CRM_Utils_Array
::value('max_number_of_contacts', $this->_surveyDetails
);
131 if ($maxVoters <= $this->_numVoters
) {
132 $errorMsg = ts('The maximum number of contacts is already reserved for this interviewer.');
134 elseif (count($this->_contactIds
) > ($maxVoters - $this->_numVoters
)) {
135 $errorMsg = ts('You can reserve a maximum of %1 contact(s) at a time for this survey.',
136 array(1 => $maxVoters - $this->_numVoters
)
141 $defaultNum = CRM_Utils_Array
::value('default_number_of_contacts', $this->_surveyDetails
);
142 if (!$errorMsg && $defaultNum && (count($this->_contactIds
) > $defaultNum)) {
143 $errorMsg = ts('You can reserve a maximum of %1 contact(s) at a time for this survey.',
144 array(1 => $defaultNum)
149 CRM_Core_Error
::statusBounce($errorMsg);
154 * Build the form object
159 public function buildQuickForm() {
160 // allow to add contact to either new or existing group.
161 $this->addElement('text', 'ActivityType', ts('Activity Type'));
162 $this->addElement('text', 'newGroupName', ts('Name for new group'));
163 $this->addElement('text', 'newGroupDesc', ts('Description of new group'));
164 $groups = CRM_Core_PseudoConstant
::nestedGroup();
165 $hasExistingGroups = FALSE;
166 if (is_array($groups) && !empty($groups)) {
167 $hasExistingGroups = TRUE;
168 $this->addElement('select', 'groups', ts('Add respondent(s) to existing group(s)'),
169 $groups, array('multiple' => "multiple", 'class' => 'crm-select2')
172 $this->assign('hasExistingGroups', $hasExistingGroups);
177 'name' => ts('Reserve'),
178 'subName' => 'reserve',
183 if (CRM_Core_Permission
::check('manage campaign') ||
184 CRM_Core_Permission
::check('administer CiviCampaign') ||
185 CRM_Core_Permission
::check('interview campaign contacts')
189 'name' => ts('Reserve and Interview'),
190 'subName' => 'reserveToInterview',
195 'name' => ts('Cancel'),
198 $this->addButtons($buttons);
199 $this->addFormRule(array('CRM_Campaign_Form_Task_Reserve', 'formRule'), $this);
203 * Global validation rules for the form
205 * @param array $fields
206 * Posted values of the form.
212 * list of errors to be posted back to the form
214 public static function formRule($fields, $files, $self) {
216 $invalidGroupName = FALSE;
217 if (!empty($fields['newGroupName'])) {
218 $title = trim($fields['newGroupName']);
219 $name = CRM_Utils_String
::titleToVar($title);
220 $query = 'select count(*) from civicrm_group where name like %1 OR title like %2';
221 $grpCnt = CRM_Core_DAO
::singleValueQuery($query, array(
222 1 => array($name, 'String'),
223 2 => array($title, 'String'),
226 $invalidGroupName = TRUE;
227 $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName']));
230 $self->assign('invalidGroupName', $invalidGroupName);
232 return empty($errors) ?
TRUE : $errors;
236 * Process the form after the input has been submitted and validated
241 public function postProcess() {
244 $maxVoters = CRM_Utils_Array
::value('max_number_of_contacts', $this->_surveyDetails
);
245 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
246 $statusHeld = array_search('Scheduled', $activityStatus);
248 $reservedVoterIds = array();
249 foreach ($this->_contactIds
as $cid) {
250 $subject = ts('%1', array(1 => $this->_surveyDetails
['title'])) . ' - ' . ts('Respondent Reservation');
251 $session = CRM_Core_Session
::singleton();
252 $activityParams = array(
253 'source_contact_id' => $session->get('userID'),
254 'assignee_contact_id' => array($this->_interviewerId
),
255 'target_contact_id' => array($cid),
256 'source_record_id' => $this->_surveyId
,
257 'activity_type_id' => $this->_surveyDetails
['activity_type_id'],
258 'subject' => $subject,
259 'activity_date_time' => date('YmdHis'),
260 'status_id' => $statusHeld,
261 'skipRecentView' => 1,
262 'campaign_id' => CRM_Utils_Array
::value('campaign_id', $this->_surveyDetails
),
264 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
267 $reservedVoterIds[$cid] = $cid;
269 if ($maxVoters && ($maxVoters <= ($this->_numVoters +
$countVoters))) {
274 //add reserved voters to groups.
275 $groupAdditions = $this->_addRespondentToGroup($reservedVoterIds);
278 if ($countVoters > 0) {
279 $status = '<p>' . ts("%1 Contact(s) have been reserved.", array(1 => $countVoters)) . '</p>';
280 if ($groupAdditions) {
281 $status .= '<p>' . ts('Respondent(s) has been added to %1 group(s).',
282 array(1 => implode(', ', $groupAdditions))
285 CRM_Core_Session
::setStatus($status, ts('Reservation Added'), 'success');
288 if (count($this->_contactIds
) > $countVoters) {
289 CRM_Core_Session
::setStatus(ts('Reservation did not add for %1 contact(s).',
290 array(1 => (count($this->_contactIds
) - $countVoters))
294 //get ready to jump to voter interview form.
295 $buttonName = $this->controller
->getButtonName();
296 if (!empty($reservedVoterIds) &&
297 $buttonName == '_qf_Reserve_next_reserveToInterview'
299 $this->controller
->set('surveyId', $this->_surveyId
);
300 $this->controller
->set('contactIds', $reservedVoterIds);
301 $this->controller
->set('interviewerId', $this->_interviewerId
);
302 $this->controller
->set('reserveToInterview', TRUE);
311 private function _addRespondentToGroup($contactIds) {
312 $groupAdditions = array();
313 if (empty($contactIds)) {
314 return $groupAdditions;
317 $params = $this->controller
->exportValues($this->_name
);
318 $groups = CRM_Utils_Array
::value('groups', $params, array());
319 $newGroupName = CRM_Utils_Array
::value('newGroupName', $params);
320 $newGroupDesc = CRM_Utils_Array
::value('newGroupDesc', $params);
326 'title' => $newGroupName,
327 'description' => $newGroupDesc,
330 $group = CRM_Contact_BAO_Group
::create($grpParams);
331 $groups[] = $newGroupId = $group->id
;
334 //add the respondents to groups.
335 if (is_array($groups)) {
336 $existingGroups = CRM_Core_PseudoConstant
::group();
337 foreach ($groups as $groupId) {
338 $addCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds, $groupId);
339 $totalCount = CRM_Utils_Array
::value(1, $addCount);
340 if ($groupId == $newGroupId) {
341 $name = $newGroupName;
345 $name = $existingGroups[$groupId];
349 $url = CRM_Utils_System
::url('civicrm/group/search',
350 'reset=1&force=1&context=smog&gid=' . $groupId
352 $groupAdditions[] = '<a href="' . $url . '">' . $name . '</a>';
357 return $groupAdditions;