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