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