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 * The "Case" data, formatted per APIv3.
37 * List of activities, formatted per APIv3.
44 * @var \SimpleXMLElement
49 * A list of activity indices, which sort the various activities by some set of keys.
51 * Each index is identified by its key-set - e.g. "activity_type_id;source_contact_id" would be a
52 * two-dimensional index listing activities by their type ID and their source.
61 public function __construct($caseId) {
62 $this->caseId
= $caseId;
67 * Determine if case includes an activity of given type/status
70 * Eg "Phone Call", "Interview Prospect", "Background Check".
71 * @param string $status
72 * Eg "Scheduled", "Completed".
75 public function hasActivity($type, $status = NULL) {
76 $idx = $this->getActivityIndex(['activity_type_id', 'status_id']);
77 $activityTypeGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_type']);
80 'option_group_id' => $activityTypeGroup['id'],
82 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
83 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
85 $activityStatusGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_status']);
88 'option_group_id' => $activityStatusGroup['id'],
90 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
91 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
93 if ($status === NULL) {
94 return !empty($idx[$activityTypeID]);
97 return !empty($idx[$activityTypeID][$activityStatusID]);
102 * Get a list of all activities in the case.
105 * list of activity records (api/v3 format)
107 public function getActivities() {
108 if ($this->activities
=== NULL) {
109 // TODO find batch-oriented API for getting all activities in a case
110 $case = $this->getCase();
112 if (isset($case['activities'])) {
113 foreach ($case['activities'] as $actId) {
114 $result = civicrm_api3('Activity', 'get', [
116 'is_current_revision' => 1,
118 $activities = array_merge($activities, $result['values']);
121 $this->activities
= $activities;
123 return $this->activities
;
127 * Get a single activity record by type.
128 * This function is only used by SequenceListenerTest
130 * @param string $type
131 * @throws \Civi\CCase\Exception\MultipleActivityException
132 * @return array|NULL, activity record (api/v3)
134 public function getSingleActivity($type) {
135 $idx = $this->getActivityIndex(['activity_type_id', 'id']);
136 $actTypes = array_flip(\CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate'));
137 $typeId = $actTypes[$type];
138 $count = isset($idx[$typeId]) ?
count($idx[$typeId]) : 0;
143 elseif ($count === 1) {
144 foreach ($idx[$typeId] as $item) {
149 throw new \Civi\CCase\Exception\
MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
156 public function getCaseId() {
157 return $this->caseId
;
161 * @return array, Case record (api/v3 format)
163 public function getCase() {
164 if ($this->case === NULL) {
165 $this->case = civicrm_api3('case', 'getsingle', ['id' => $this->caseId
]);
172 * @throws \CRM_Core_Exception
174 public function getCaseType() {
175 if ($this->caseType
=== NULL) {
176 $case = $this->getCase();
177 $caseTypes = \CRM_Case_XMLRepository
::singleton()->getAllCaseTypes();
178 if (!isset($caseTypes[$case['case_type_id']])) {
179 throw new \
CRM_Core_Exception("Case does not have a recognized case-type!");
181 $this->caseType
= $caseTypes[$case['case_type_id']];
183 return $this->caseType
;
187 * Get a list of all activities in the case (indexed by some property/properties)
190 * List of properties by which to index activities.
192 * list of activity records (api/v3 format), indexed by $keys
194 public function getActivityIndex($keys) {
195 $key = implode(";", $keys);
196 if (!isset($this->indices
[$key])) {
197 $this->indices
[$key] = \CRM_Utils_Array
::index($keys, $this->getActivities());
199 return $this->indices
[$key];
203 * @return \SimpleXMLElement|NULL
205 public function getXml() {
206 if ($this->xml
=== NULL) {
207 $this->xml
= \CRM_Case_XMLRepository
::singleton()->retrieve($this->getCaseType());
213 * Flush any cached information.
215 public function flush() {
217 $this->caseType
= NULL;
218 $this->activities
= NULL;