fix version in header
[civicrm-core.git] / CRM / Activity / BAO / ActivityAssignment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is for activity assignment functions
38 *
39 */
40 class CRM_Activity_BAO_ActivityAssignment extends CRM_Activity_DAO_ActivityContact {
41
42 /**
43 * Class constructor
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Add activity assignment.
51 *
52 * @param array $params (reference ) an assoc array of name/value pairs
53 *
54 * @return object activity type of object that is added
55 * @access public
56 */
57 public static function create(&$params) {
58 $assignment = new CRM_Activity_BAO_ActivityContact();
59 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
60 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
61
62 $assignment->copyValues($params);
63 $assignment->record_type_id = $assigneeID;
64
65 return $assignment->save();
66 }
67
68 /**
69 * Retrieve assignee_id by activity_id
70 *
71 * @param int $activity_id
72 *
73 * @return array
74 *
75 * @access public
76 */
77 static function retrieveAssigneeIdsByActivityId($activity_id) {
78 $assigneeArray = array();
79 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
80 return $assigneeArray;
81 }
82
83 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
84 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
85
86 $sql = "
87 SELECT contact_id
88 FROM civicrm_activity_contact
89 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
90 WHERE activity_id = %1
91 AND record_type_id = $assigneeID
92 AND civicrm_contact.is_deleted = 0
93 ";
94 $assignment = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
95 while ($assignment->fetch()) {
96 $assigneeArray[] = $assignment->contact_id;
97 }
98
99 return $assigneeArray;
100 }
101
102 /**
103 * Retrieve assignee names by activity_id
104 *
105 * @param array $activityIDs IDs of the activities
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 */
114 static function getAssigneeNames($activityIDs, $isDisplayName = FALSE, $skipDetails = TRUE) {
115 $assigneeNames = array();
116 if (empty($activityIDs)) {
117 return $assigneeNames;
118 }
119 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
120 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
121
122 $whereClause = "";
123 if (!$skipDetails) {
124 $whereClause = " AND ce.is_primary= 1";
125 }
126 $inClause = implode(",", $activityIDs);
127
128 $query = "
129 SELECT contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email,
130 civicrm_activity_contact.activity_id
131 FROM civicrm_contact contact_a
132 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
133 LEFT JOIN civicrm_email ce ON ce.contact_id = contact_a.id
134 WHERE civicrm_activity_contact.activity_id IN ( $inClause )
135 AND contact_a.is_deleted = 0
136 AND civicrm_activity_contact.record_type_id = $assigneeID
137 {$whereClause}
138 ";
139
140 $dao = CRM_Core_DAO::executeQuery($query);
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');
155 $assigneeNames[$dao->id]['activity_id'] = $dao->activity_id;
156 }
157 }
158 }
159 return $assigneeNames;
160 }
161 }
162