ActivityAssignment.php 5.57 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
32 33 34 35 36 37 38 39
 * $Id$
 *
 */

/**
 * This class is for activity assignment functions
 *
 */
40
class CRM_Activity_BAO_ActivityAssignment extends CRM_Activity_DAO_ActivityContact {
totten's avatar
totten committed
41 42

  /**
colemanw's avatar
colemanw committed
43
   * Class constructor
totten's avatar
totten committed
44
   */
45
  public function __construct() {
totten's avatar
totten committed
46 47 48 49 50 51
    parent::__construct();
  }

  /**
   * Add activity assignment.
   *
52 53
   * @param array $params
   *   (reference ) an assoc array of name/value pairs.
Eileen McNaughton's avatar
Eileen McNaughton committed
54
   *
55 56
   * @return object
   *   activity type of object that is added
totten's avatar
totten committed
57 58
   */
  public static function create(&$params) {
yashodha's avatar
yashodha committed
59
    $assignment = new CRM_Activity_BAO_ActivityContact();
60
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
61
    $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
totten's avatar
totten committed
62 63

    $assignment->copyValues($params);
yashodha's avatar
yashodha committed
64
    $assignment->record_type_id = $assigneeID;
yashodha's avatar
yashodha committed
65

totten's avatar
totten committed
66 67 68 69 70 71
    return $assignment->save();
  }

  /**
   * Retrieve assignee_id by activity_id
   *
72
   * @param int $activity_id
totten's avatar
totten committed
73
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
74
   * @return array
totten's avatar
totten committed
75
   */
76
  public static function retrieveAssigneeIdsByActivityId($activity_id) {
totten's avatar
totten committed
77 78 79 80 81
    $assigneeArray = array();
    if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
      return $assigneeArray;
    }

82
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
83 84
    $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);

85
    $sql = "
lobo's avatar
lobo committed
86 87
SELECT     contact_id
FROM       civicrm_activity_contact
88 89
INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
WHERE      activity_id = %1
yashodha's avatar
yashodha committed
90
AND        record_type_id = $assigneeID
91 92
AND        civicrm_contact.is_deleted = 0
";
totten's avatar
totten committed
93 94
    $assignment = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
    while ($assignment->fetch()) {
95
      $assigneeArray[] = $assignment->contact_id;
totten's avatar
totten committed
96 97 98 99 100 101 102 103
    }

    return $assigneeArray;
  }

  /**
   * Retrieve assignee names by activity_id
   *
104 105 106 107 108 109
   * @param array $activityIDs
   *   IDs of the activities.
   * @param bool $isDisplayName
   *   If set returns display names of assignees.
   * @param bool $skipDetails
   *   If false returns all details of assignee contact.
totten's avatar
totten committed
110 111 112
   *
   * @return array
   */
113
  public static function getAssigneeNames($activityIDs, $isDisplayName = FALSE, $skipDetails = TRUE) {
totten's avatar
totten committed
114
    $assigneeNames = array();
115
    if (empty($activityIDs)) {
totten's avatar
totten committed
116 117
      return $assigneeNames;
    }
118
    $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
119
    $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
totten's avatar
totten committed
120 121 122 123 124

    $whereClause = "";
    if (!$skipDetails) {
      $whereClause = "  AND ce.is_primary= 1";
    }
125
    $inClause = implode(",", $activityIDs);
totten's avatar
totten committed
126

127
    $query = "
128 129
SELECT     contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email,
           civicrm_activity_contact.activity_id
130
FROM       civicrm_contact contact_a
lobo's avatar
lobo committed
131
INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
132
LEFT JOIN  civicrm_email ce ON ce.contact_id = contact_a.id
133
WHERE      civicrm_activity_contact.activity_id IN ( $inClause )
134
AND        contact_a.is_deleted = 0
135
AND        civicrm_activity_contact.record_type_id = $assigneeID
136 137
           {$whereClause}
";
totten's avatar
totten committed
138

139
    $dao = CRM_Core_DAO::executeQuery($query);
totten's avatar
totten committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153
    while ($dao->fetch()) {
      if (!$isDisplayName) {
        $assigneeNames[$dao->id] = $dao->sort_name;
      }
      else {
        if ($skipDetails) {
          $assigneeNames[$dao->id] = $dao->display_name;
        }
        else {
          $assigneeNames[$dao->id]['contact_id'] = $dao->id;
          $assigneeNames[$dao->id]['display_name'] = $dao->display_name;
          $assigneeNames[$dao->id]['sort_name'] = $dao->sort_name;
          $assigneeNames[$dao->id]['email'] = $dao->email;
          $assigneeNames[$dao->id]['role'] = ts('Activity Assignee');
154
          $assigneeNames[$dao->id]['activity_id'] = $dao->activity_id;
totten's avatar
totten committed
155 156 157 158 159
        }
      }
    }
    return $assigneeNames;
  }
160

totten's avatar
totten committed
161
}