3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
41 * @var array per APIv3
51 * @var array per APIv3
56 * @var \SimpleXMLElement
61 * @var array<string,array>
68 public function __construct($caseId) {
69 $this->caseId
= $caseId;
74 * Determine if case includes an activity of given type/status
77 * Eg "Phone Call", "Interview Prospect", "Background Check".
78 * @param string $status
79 * Eg "Scheduled", "Completed".
82 public function hasActivity($type, $status = NULL) {
83 $idx = $this->getActivityIndex(array('activity_type_id', 'status_id'));
84 $activityTypeGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_type'));
85 $activityType = array(
87 'option_group_id' => $activityTypeGroup['id'],
89 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
90 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
92 $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status'));
93 $activityStatus = array(
95 'option_group_id' => $activityStatusGroup['id'],
97 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
98 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
100 if ($status === NULL) {
101 return !empty($idx[$activityTypeID]);
104 return !empty($idx[$activityTypeID][$activityStatusID]);
109 * Get a list of all activities in the case.
112 * list of activity records (api/v3 format)
114 public function getActivities() {
115 if ($this->activities
=== NULL) {
116 // TODO find batch-oriented API for getting all activities in a case
117 $case = $this->getCase();
118 $activities = array();
119 if (isset($case['activities'])) {
120 foreach ($case['activities'] as $actId) {
121 $result = civicrm_api3('Activity', 'get', array(
123 'is_current_revision' => 1,
125 $activities = array_merge($activities, $result['values']);
128 $this->activities
= $activities;
130 return $this->activities
;
134 * Get a single activity record by type.
136 * @param string $type
137 * @throws \Civi\CCase\Exception\MultipleActivityException
138 * @return array|NULL, activity record (api/v3)
140 public function getSingleActivity($type) {
141 $idx = $this->getActivityIndex(array('activity_type_id', 'id'));
142 $actTypes = array_flip(\CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name'));
143 $typeId = $actTypes[$type];
144 $count = isset($idx[$typeId]) ?
count($idx[$typeId]) : 0;
149 elseif ($count === 1) {
150 foreach ($idx[$typeId] as $item) {
155 throw new \Civi\CCase\Exception\
MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
162 public function getCaseId() {
163 return $this->caseId
;
167 * @return array, Case record (api/v3 format)
169 public function getCase() {
170 if ($this->case === NULL) {
171 $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId
));
178 * @throws \CRM_Core_Exception
180 public function getCaseType() {
181 if ($this->caseType
=== NULL) {
182 $case = $this->getCase();
183 $caseTypes = \CRM_Case_XMLRepository
::singleton()->getAllCaseTypes();
184 if (!isset($caseTypes[$case['case_type_id']])) {
185 throw new \
CRM_Core_Exception("Case does not have a recognized case-type!");
187 $this->caseType
= $caseTypes[$case['case_type_id']];
189 return $this->caseType
;
193 * Get a list of all activities in the case (indexed by some property/properties)
196 * List of properties by which to index activities.
198 * list of activity records (api/v3 format), indexed by $keys
200 public function getActivityIndex($keys) {
201 $key = implode(";", $keys);
202 if (!isset($this->indices
[$key])) {
203 $this->indices
[$key] = \CRM_Utils_Array
::index($keys, $this->getActivities());
205 return $this->indices
[$key];
209 * @return \SimpleXMLElement|NULL
211 public function getXml() {
212 if ($this->xml
=== NULL) {
213 $this->xml
= \CRM_Case_XMLRepository
::singleton()->retrieve($this->getCaseType());
219 * Flush any cached information.
221 public function flush() {
223 $this->caseType
= NULL;
224 $this->activities
= NULL;
225 $this->indices
= array();