Merge pull request #4622 from civicrm/4.5
[civicrm-core.git] / CRM / Activity / BAO / ActivityAssignment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 /**
43 * class constructor
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 *
54 * @internal param array $ids (reference ) the array that holds all the db ids
6a488035
TO
55 *
56 * @return object activity type of object that is added
57 * @access public
6a488035
TO
58 */
59 public static function create(&$params) {
cc1c86e9 60 $assignment = new CRM_Activity_BAO_ActivityContact();
e7e657f0 61 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 62 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035
TO
63
64 $assignment->copyValues($params);
a24b3694 65 $assignment->record_type_id = $assigneeID;
cc1c86e9 66
6a488035
TO
67 return $assignment->save();
68 }
69
70 /**
71 * Retrieve assignee_id by activity_id
72 *
6c8f6e67
EM
73 * @param $activity_id
74 *
75 * @internal param int $id ID of the activity
6a488035 76 *
91d49cae 77 * @return array
6a488035
TO
78 *
79 * @access public
6a488035
TO
80 */
81 static function retrieveAssigneeIdsByActivityId($activity_id) {
82 $assigneeArray = array();
83 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
84 return $assigneeArray;
85 }
86
e7e657f0 87 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 88 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
89
91da6cd5 90 $sql = "
1d85d241
DL
91SELECT contact_id
92FROM civicrm_activity_contact
91da6cd5
DL
93INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
94WHERE activity_id = %1
a24b3694 95AND record_type_id = $assigneeID
91da6cd5
DL
96AND civicrm_contact.is_deleted = 0
97";
6a488035
TO
98 $assignment = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
99 while ($assignment->fetch()) {
91da6cd5 100 $assigneeArray[] = $assignment->contact_id;
6a488035
TO
101 }
102
103 return $assigneeArray;
104 }
105
106 /**
107 * Retrieve assignee names by activity_id
108 *
90b05581 109 * @param array $activityIDs IDs of the activities
6a488035
TO
110 * @param boolean $isDisplayName if set returns display names of assignees
111 * @param boolean $skipDetails if false returns all details of assignee contact.
112 *
113 * @return array
114 *
115 * @access public
116 *
117 */
90b05581 118 static function getAssigneeNames($activityIDs, $isDisplayName = FALSE, $skipDetails = TRUE) {
6a488035 119 $assigneeNames = array();
90b05581 120 if (empty($activityIDs)) {
6a488035
TO
121 return $assigneeNames;
122 }
e7e657f0 123 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 124 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035
TO
125
126 $whereClause = "";
127 if (!$skipDetails) {
128 $whereClause = " AND ce.is_primary= 1";
129 }
90b05581 130 $inClause = implode(",", $activityIDs);
6a488035 131
91da6cd5 132 $query = "
90b05581
DG
133SELECT contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email,
134 civicrm_activity_contact.activity_id
91da6cd5 135FROM civicrm_contact contact_a
1d85d241 136INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
91da6cd5 137LEFT JOIN civicrm_email ce ON ce.contact_id = contact_a.id
90b05581 138WHERE civicrm_activity_contact.activity_id IN ( $inClause )
91da6cd5 139AND contact_a.is_deleted = 0
f813f78e 140AND civicrm_activity_contact.record_type_id = $assigneeID
91da6cd5
DL
141 {$whereClause}
142";
6a488035 143
90b05581 144 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
145 while ($dao->fetch()) {
146 if (!$isDisplayName) {
147 $assigneeNames[$dao->id] = $dao->sort_name;
148 }
149 else {
150 if ($skipDetails) {
151 $assigneeNames[$dao->id] = $dao->display_name;
152 }
153 else {
154 $assigneeNames[$dao->id]['contact_id'] = $dao->id;
155 $assigneeNames[$dao->id]['display_name'] = $dao->display_name;
156 $assigneeNames[$dao->id]['sort_name'] = $dao->sort_name;
157 $assigneeNames[$dao->id]['email'] = $dao->email;
158 $assigneeNames[$dao->id]['role'] = ts('Activity Assignee');
90b05581 159 $assigneeNames[$dao->id]['activity_id'] = $dao->activity_id;
6a488035
TO
160 }
161 }
162 }
163 return $assigneeNames;
164 }
165}
166