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