3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
36 * @var array per APIv3
46 * @var array per APIv3
51 * @var \SimpleXMLElement
56 * @var array<string,array>
60 public function __construct($caseId) {
61 $this->caseId
= $caseId;
66 * Determine if case includes an activity of given type/status
68 * @param string $type eg "Phone Call", "Interview Prospect", "Background Check"
69 * @param string $status eg "Scheduled", "Completed"
72 public function hasActivity($type, $status = NULL) {
73 $idx = $this->getActivityIndex(array('activity_type_id', 'status_id'));
74 $activityTypeGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_type'));
75 $activityType = array(
77 'option_group_id' => $activityTypeGroup['id'],
79 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
80 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
82 $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status'));
83 $activityStatus = array(
85 'option_group_id' => $activityStatusGroup['id']
87 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
88 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
90 if ($status === NULL) {
91 return !empty($idx[$activityTypeID]);
94 return !empty($idx[$activityTypeID][$activityStatusID]);
99 * Get a list of all activities in the case
101 * @return array list of activity records (api/v3 format)
103 public function getActivities() {
104 if ($this->activities
=== NULL) {
105 // TODO find batch-oriented API for getting all activities in a case
106 $case = $this->getCase();
107 $activities = array();
108 if (isset($case['activities'])) {
109 foreach ($case['activities'] as $actId) {
110 $result = civicrm_api3('Activity', 'get', array(
112 'is_current_revision' => 1,
114 $activities = array_merge($activities, $result['values']);
117 $this->activities
= $activities;
119 return $this->activities
;
123 * Get a single activity record by type
125 * @param string $type
126 * @throws \Civi\CCase\Exception\MultipleActivityException
127 * @return array|NULL, activity record (api/v3)
129 public function getSingleActivity($type) {
130 $idx = $this->getActivityIndex(array('activity_type_id', 'id'));
131 $actTypes = array_flip(\CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name'));
132 $typeId = $actTypes[$type];
133 $count = isset($idx[$typeId]) ?
count($idx[$typeId]) : 0;
138 elseif ($count === 1) {
139 foreach ($idx[$typeId] as $item) {
144 throw new \Civi\CCase\Exception\
MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
151 public function getCaseId() {
152 return $this->caseId
;
156 * @return array, Case record (api/v3 format)
158 public function getCase() {
159 if ($this->case === NULL) {
160 $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId
));
168 public function getCaseType() {
169 if ($this->caseType
=== NULL) {
170 $case = $this->getCase();
171 $caseTypes = \CRM_Case_XMLRepository
::singleton()->getAllCaseTypes();
172 if (!isset($caseTypes[$case['case_type_id']])) {
173 throw new \
CRM_Core_Exception("Case does not have a recognized case-type!");
175 $this->caseType
= $caseTypes[$case['case_type_id']];
177 return $this->caseType
;
181 * Get a list of all activities in the case (indexed by some property/properties)
183 * @param array $keys list of properties by which to index activities
184 * @return array list of activity records (api/v3 format), indexed by $keys
186 public function getActivityIndex($keys) {
187 $key = implode(";", $keys);
188 if (!isset($this->indices
[$key])) {
189 $this->indices
[$key] = \CRM_Utils_Array
::index($keys, $this->getActivities());
191 return $this->indices
[$key];
195 * @return SimpleXMLElement|NULL
197 public function getXml() {
198 if ($this->xml
=== NULL) {
199 $this->xml
= \CRM_Case_XMLRepository
::singleton()->retrieve($this->getCaseType());
205 * Flush any cached information
209 public function flush() {
211 $this->caseType
= NULL;
212 $this->activities
= NULL;
213 $this->indices
= array();