3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
25 * @var array per APIv3
35 * @var array per APIv3
40 * @var \SimpleXMLElement
45 * @var array<string,array>
52 public function __construct($caseId) {
53 $this->caseId
= $caseId;
58 * Determine if case includes an activity of given type/status
61 * Eg "Phone Call", "Interview Prospect", "Background Check".
62 * @param string $status
63 * Eg "Scheduled", "Completed".
66 public function hasActivity($type, $status = NULL) {
67 $idx = $this->getActivityIndex(['activity_type_id', 'status_id']);
68 $activityTypeGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_type']);
71 'option_group_id' => $activityTypeGroup['id'],
73 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
74 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
76 $activityStatusGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_status']);
79 'option_group_id' => $activityStatusGroup['id'],
81 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
82 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
84 if ($status === NULL) {
85 return !empty($idx[$activityTypeID]);
88 return !empty($idx[$activityTypeID][$activityStatusID]);
93 * Get a list of all activities in the case.
96 * list of activity records (api/v3 format)
98 public function getActivities() {
99 if ($this->activities
=== NULL) {
100 // TODO find batch-oriented API for getting all activities in a case
101 $case = $this->getCase();
103 if (isset($case['activities'])) {
104 foreach ($case['activities'] as $actId) {
105 $result = civicrm_api3('Activity', 'get', [
107 'is_current_revision' => 1,
109 $activities = array_merge($activities, $result['values']);
112 $this->activities
= $activities;
114 return $this->activities
;
118 * Get a single activity record by type.
119 * This function is only used by SequenceListenerTest
121 * @param string $type
122 * @throws \Civi\CCase\Exception\MultipleActivityException
123 * @return array|NULL, activity record (api/v3)
125 public function getSingleActivity($type) {
126 $idx = $this->getActivityIndex(['activity_type_id', 'id']);
127 $actTypes = array_flip(\CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate'));
128 $typeId = $actTypes[$type];
129 $count = isset($idx[$typeId]) ?
count($idx[$typeId]) : 0;
134 elseif ($count === 1) {
135 foreach ($idx[$typeId] as $item) {
140 throw new \Civi\CCase\Exception\
MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
147 public function getCaseId() {
148 return $this->caseId
;
152 * @return array, Case record (api/v3 format)
154 public function getCase() {
155 if ($this->case === NULL) {
156 $this->case = civicrm_api3('case', 'getsingle', ['id' => $this->caseId
]);
163 * @throws \CRM_Core_Exception
165 public function getCaseType() {
166 if ($this->caseType
=== NULL) {
167 $case = $this->getCase();
168 $caseTypes = \CRM_Case_XMLRepository
::singleton()->getAllCaseTypes();
169 if (!isset($caseTypes[$case['case_type_id']])) {
170 throw new \
CRM_Core_Exception("Case does not have a recognized case-type!");
172 $this->caseType
= $caseTypes[$case['case_type_id']];
174 return $this->caseType
;
178 * Get a list of all activities in the case (indexed by some property/properties)
181 * List of properties by which to index activities.
183 * list of activity records (api/v3 format), indexed by $keys
185 public function getActivityIndex($keys) {
186 $key = implode(";", $keys);
187 if (!isset($this->indices
[$key])) {
188 $this->indices
[$key] = \CRM_Utils_Array
::index($keys, $this->getActivities());
190 return $this->indices
[$key];
194 * @return \SimpleXMLElement|NULL
196 public function getXml() {
197 if ($this->xml
=== NULL) {
198 $this->xml
= \CRM_Case_XMLRepository
::singleton()->retrieve($this->getCaseType());
204 * Flush any cached information.
206 public function flush() {
208 $this->caseType
= NULL;
209 $this->activities
= NULL;