Commit | Line | Data |
---|---|---|
708d8fa2 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
708d8fa2 | 5 | | | |
41498ac5 TO |
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 | | |
708d8fa2 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
708d8fa2 TO |
11 | namespace Civi\CCase; |
12 | ||
7fe37828 EM |
13 | /** |
14 | * Class Analyzer | |
15 | * | |
16 | * @package Civi\CCase | |
17 | */ | |
708d8fa2 TO |
18 | class Analyzer { |
19 | /** | |
20 | * @var int | |
21 | */ | |
22 | private $caseId; | |
23 | ||
24 | /** | |
99cd5892 TO |
25 | * The "Case" data, formatted per APIv3. |
26 | * | |
27 | * @var array | |
708d8fa2 TO |
28 | */ |
29 | private $case; | |
30 | ||
31 | /** | |
32 | * @var string | |
33 | */ | |
34 | private $caseType; | |
35 | ||
36 | /** | |
99cd5892 TO |
37 | * List of activities, formatted per APIv3. |
38 | * | |
39 | * @var array | |
708d8fa2 TO |
40 | */ |
41 | private $activities; | |
42 | ||
43 | /** | |
44 | * @var \SimpleXMLElement | |
45 | */ | |
46 | private $xml; | |
47 | ||
48 | /** | |
99cd5892 TO |
49 | * A list of activity indices, which sort the various activities by some set of keys. |
50 | * | |
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. | |
53 | * | |
54 | * @var array | |
708d8fa2 TO |
55 | */ |
56 | private $indices; | |
57 | ||
7fe37828 EM |
58 | /** |
59 | * @param $caseId | |
60 | */ | |
708d8fa2 TO |
61 | public function __construct($caseId) { |
62 | $this->caseId = $caseId; | |
63 | $this->flush(); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Determine if case includes an activity of given type/status | |
68 | * | |
04855556 TO |
69 | * @param string $type |
70 | * Eg "Phone Call", "Interview Prospect", "Background Check". | |
71 | * @param string $status | |
72 | * Eg "Scheduled", "Completed". | |
708d8fa2 TO |
73 | * @return bool |
74 | */ | |
75 | public function hasActivity($type, $status = NULL) { | |
c64f69d9 CW |
76 | $idx = $this->getActivityIndex(['activity_type_id', 'status_id']); |
77 | $activityTypeGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_type']); | |
78 | $activityType = [ | |
708d8fa2 TO |
79 | 'name' => $type, |
80 | 'option_group_id' => $activityTypeGroup['id'], | |
c64f69d9 | 81 | ]; |
708d8fa2 TO |
82 | $activityTypeID = civicrm_api3('option_value', 'get', $activityType); |
83 | $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value']; | |
84 | if ($status) { | |
c64f69d9 CW |
85 | $activityStatusGroup = civicrm_api3('option_group', 'get', ['name' => 'activity_status']); |
86 | $activityStatus = [ | |
708d8fa2 | 87 | 'name' => $status, |
608e6658 | 88 | 'option_group_id' => $activityStatusGroup['id'], |
c64f69d9 | 89 | ]; |
708d8fa2 TO |
90 | $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus); |
91 | $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value']; | |
92 | } | |
93 | if ($status === NULL) { | |
94 | return !empty($idx[$activityTypeID]); | |
95 | } | |
96 | else { | |
97 | return !empty($idx[$activityTypeID][$activityStatusID]); | |
98 | } | |
99 | } | |
100 | ||
101 | /** | |
fe482240 | 102 | * Get a list of all activities in the case. |
708d8fa2 | 103 | * |
a6c01b45 CW |
104 | * @return array |
105 | * list of activity records (api/v3 format) | |
708d8fa2 TO |
106 | */ |
107 | public function getActivities() { | |
108 | if ($this->activities === NULL) { | |
f99a2e5d TO |
109 | // TODO find batch-oriented API for getting all activities in a case |
110 | $case = $this->getCase(); | |
c64f69d9 | 111 | $activities = []; |
f99a2e5d TO |
112 | if (isset($case['activities'])) { |
113 | foreach ($case['activities'] as $actId) { | |
c64f69d9 | 114 | $result = civicrm_api3('Activity', 'get', [ |
f99a2e5d TO |
115 | 'id' => $actId, |
116 | 'is_current_revision' => 1, | |
c64f69d9 | 117 | ]); |
f99a2e5d TO |
118 | $activities = array_merge($activities, $result['values']); |
119 | } | |
120 | } | |
121 | $this->activities = $activities; | |
708d8fa2 TO |
122 | } |
123 | return $this->activities; | |
124 | } | |
125 | ||
126 | /** | |
fe482240 | 127 | * Get a single activity record by type. |
b864360d | 128 | * This function is only used by SequenceListenerTest |
708d8fa2 TO |
129 | * |
130 | * @param string $type | |
131 | * @throws \Civi\CCase\Exception\MultipleActivityException | |
132 | * @return array|NULL, activity record (api/v3) | |
133 | */ | |
134 | public function getSingleActivity($type) { | |
c64f69d9 | 135 | $idx = $this->getActivityIndex(['activity_type_id', 'id']); |
b864360d | 136 | $actTypes = array_flip(\CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate')); |
708d8fa2 TO |
137 | $typeId = $actTypes[$type]; |
138 | $count = isset($idx[$typeId]) ? count($idx[$typeId]) : 0; | |
139 | ||
140 | if ($count === 0) { | |
141 | return NULL; | |
142 | } | |
143 | elseif ($count === 1) { | |
144 | foreach ($idx[$typeId] as $item) { | |
145 | return $item; | |
146 | } | |
147 | } | |
148 | else { | |
149 | throw new \Civi\CCase\Exception\MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count); | |
150 | } | |
151 | } | |
152 | ||
153 | /** | |
154 | * @return int | |
155 | */ | |
156 | public function getCaseId() { | |
157 | return $this->caseId; | |
158 | } | |
159 | ||
160 | /** | |
161 | * @return array, Case record (api/v3 format) | |
162 | */ | |
163 | public function getCase() { | |
164 | if ($this->case === NULL) { | |
c64f69d9 | 165 | $this->case = civicrm_api3('case', 'getsingle', ['id' => $this->caseId]); |
708d8fa2 TO |
166 | } |
167 | return $this->case; | |
168 | } | |
169 | ||
170 | /** | |
171 | * @return string | |
45322593 | 172 | * @throws \CRM_Core_Exception |
708d8fa2 TO |
173 | */ |
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!"); | |
180 | } | |
181 | $this->caseType = $caseTypes[$case['case_type_id']]; | |
182 | } | |
183 | return $this->caseType; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Get a list of all activities in the case (indexed by some property/properties) | |
188 | * | |
04855556 TO |
189 | * @param array $keys |
190 | * List of properties by which to index activities. | |
a6c01b45 CW |
191 | * @return array |
192 | * list of activity records (api/v3 format), indexed by $keys | |
708d8fa2 TO |
193 | */ |
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()); | |
198 | } | |
199 | return $this->indices[$key]; | |
200 | } | |
201 | ||
202 | /** | |
45322593 | 203 | * @return \SimpleXMLElement|NULL |
708d8fa2 TO |
204 | */ |
205 | public function getXml() { | |
206 | if ($this->xml === NULL) { | |
207 | $this->xml = \CRM_Case_XMLRepository::singleton()->retrieve($this->getCaseType()); | |
208 | } | |
209 | return $this->xml; | |
210 | } | |
211 | ||
212 | /** | |
fe482240 | 213 | * Flush any cached information. |
708d8fa2 TO |
214 | */ |
215 | public function flush() { | |
216 | $this->case = NULL; | |
217 | $this->caseType = NULL; | |
218 | $this->activities = NULL; | |
c64f69d9 | 219 | $this->indices = []; |
708d8fa2 | 220 | } |
96025800 | 221 | |
c206647d | 222 | } |