phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Activity / BAO / ActivityAssignment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is for activity assignment functions
38 *
39 */
91da6cd5 40class CRM_Activity_BAO_ActivityAssignment extends CRM_Activity_DAO_ActivityContact {
6a488035
TO
41
42 /**
100fef9d 43 * Class constructor
6a488035
TO
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Add activity assignment.
51 *
fd31fa4c
EM
52 * @param array $params (reference ) an assoc array of name/value pairs
53 *
6a488035
TO
54 * @return object activity type of object that is added
55 * @access public
6a488035
TO
56 */
57 public static function create(&$params) {
cc1c86e9 58 $assignment = new CRM_Activity_BAO_ActivityContact();
e7e657f0 59 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 60 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035
TO
61
62 $assignment->copyValues($params);
a24b3694 63 $assignment->record_type_id = $assigneeID;
cc1c86e9 64
6a488035
TO
65 return $assignment->save();
66 }
67
68 /**
69 * Retrieve assignee_id by activity_id
70 *
c490a46a 71 * @param int $activity_id
6a488035 72 *
91d49cae 73 * @return array
6a488035
TO
74 *
75 * @access public
6a488035
TO
76 */
77 static function retrieveAssigneeIdsByActivityId($activity_id) {
78 $assigneeArray = array();
79 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
80 return $assigneeArray;
81 }
82
e7e657f0 83 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 84 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
85
91da6cd5 86 $sql = "
1d85d241
DL
87SELECT contact_id
88FROM civicrm_activity_contact
91da6cd5
DL
89INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
90WHERE activity_id = %1
a24b3694 91AND record_type_id = $assigneeID
91da6cd5
DL
92AND civicrm_contact.is_deleted = 0
93";
6a488035
TO
94 $assignment = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
95 while ($assignment->fetch()) {
91da6cd5 96 $assigneeArray[] = $assignment->contact_id;
6a488035
TO
97 }
98
99 return $assigneeArray;
100 }
101
102 /**
103 * Retrieve assignee names by activity_id
104 *
90b05581 105 * @param array $activityIDs IDs of the activities
6a488035
TO
106 * @param boolean $isDisplayName if set returns display names of assignees
107 * @param boolean $skipDetails if false returns all details of assignee contact.
108 *
109 * @return array
110 *
111 * @access public
112 *
113 */
90b05581 114 static function getAssigneeNames($activityIDs, $isDisplayName = FALSE, $skipDetails = TRUE) {
6a488035 115 $assigneeNames = array();
90b05581 116 if (empty($activityIDs)) {
6a488035
TO
117 return $assigneeNames;
118 }
e7e657f0 119 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 120 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035
TO
121
122 $whereClause = "";
123 if (!$skipDetails) {
124 $whereClause = " AND ce.is_primary= 1";
125 }
90b05581 126 $inClause = implode(",", $activityIDs);
6a488035 127
91da6cd5 128 $query = "
90b05581
DG
129SELECT contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email,
130 civicrm_activity_contact.activity_id
91da6cd5 131FROM civicrm_contact contact_a
1d85d241 132INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
91da6cd5 133LEFT JOIN civicrm_email ce ON ce.contact_id = contact_a.id
90b05581 134WHERE civicrm_activity_contact.activity_id IN ( $inClause )
91da6cd5 135AND contact_a.is_deleted = 0
f813f78e 136AND civicrm_activity_contact.record_type_id = $assigneeID
91da6cd5
DL
137 {$whereClause}
138";
6a488035 139
90b05581 140 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
141 while ($dao->fetch()) {
142 if (!$isDisplayName) {
143 $assigneeNames[$dao->id] = $dao->sort_name;
144 }
145 else {
146 if ($skipDetails) {
147 $assigneeNames[$dao->id] = $dao->display_name;
148 }
149 else {
150 $assigneeNames[$dao->id]['contact_id'] = $dao->id;
151 $assigneeNames[$dao->id]['display_name'] = $dao->display_name;
152 $assigneeNames[$dao->id]['sort_name'] = $dao->sort_name;
153 $assigneeNames[$dao->id]['email'] = $dao->email;
154 $assigneeNames[$dao->id]['role'] = ts('Activity Assignee');
90b05581 155 $assigneeNames[$dao->id]['activity_id'] = $dao->activity_id;
6a488035
TO
156 }
157 }
158 }
159 return $assigneeNames;
160 }
161}
162