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