3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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(['activity_type_id', 'status_id']);
84 $activityTypeGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_type']);
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', ['name' => 'activity_status']);
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();
119 if (isset($case['activities'])) {
120 foreach ($case['activities'] as $actId) {
121 $result = civicrm_api3('Activity', 'get', [
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.
135 * This function is only used by SequenceListenerTest
137 * @param string $type
138 * @throws \Civi\CCase\Exception\MultipleActivityException
139 * @return array|NULL, activity record (api/v3)
141 public function getSingleActivity($type) {
142 $idx = $this->getActivityIndex(['activity_type_id', 'id']);
143 $actTypes = array_flip(\CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate'));
144 $typeId = $actTypes[$type];
145 $count = isset($idx[$typeId]) ?
count($idx[$typeId]) : 0;
150 elseif ($count === 1) {
151 foreach ($idx[$typeId] as $item) {
156 throw new \Civi\CCase\Exception\
MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
163 public function getCaseId() {
164 return $this->caseId
;
168 * @return array, Case record (api/v3 format)
170 public function getCase() {
171 if ($this->case === NULL) {
172 $this->case = civicrm_api3('case', 'getsingle', ['id' => $this->caseId
]);
179 * @throws \CRM_Core_Exception
181 public function getCaseType() {
182 if ($this->caseType
=== NULL) {
183 $case = $this->getCase();
184 $caseTypes = \CRM_Case_XMLRepository
::singleton()->getAllCaseTypes();
185 if (!isset($caseTypes[$case['case_type_id']])) {
186 throw new \
CRM_Core_Exception("Case does not have a recognized case-type!");
188 $this->caseType
= $caseTypes[$case['case_type_id']];
190 return $this->caseType
;
194 * Get a list of all activities in the case (indexed by some property/properties)
197 * List of properties by which to index activities.
199 * list of activity records (api/v3 format), indexed by $keys
201 public function getActivityIndex($keys) {
202 $key = implode(";", $keys);
203 if (!isset($this->indices
[$key])) {
204 $this->indices
[$key] = \CRM_Utils_Array
::index($keys, $this->getActivities());
206 return $this->indices
[$key];
210 * @return \SimpleXMLElement|NULL
212 public function getXml() {
213 if ($this->xml
=== NULL) {
214 $this->xml
= \CRM_Case_XMLRepository
::singleton()->retrieve($this->getCaseType());
220 * Flush any cached information.
222 public function flush() {
224 $this->caseType
= NULL;
225 $this->activities
= NULL;