Commit | Line | Data |
---|---|---|
708d8fa2 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
708d8fa2 | 5 | +--------------------------------------------------------------------+ |
3b8eef99 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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) { | |
83 | $idx = $this->getActivityIndex(array('activity_type_id', 'status_id')); | |
84 | $activityTypeGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_type')); | |
85 | $activityType = array( | |
86 | 'name' => $type, | |
87 | 'option_group_id' => $activityTypeGroup['id'], | |
88 | ); | |
89 | $activityTypeID = civicrm_api3('option_value', 'get', $activityType); | |
90 | $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value']; | |
91 | if ($status) { | |
92 | $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status')); | |
93 | $activityStatus = array( | |
94 | 'name' => $status, | |
608e6658 | 95 | 'option_group_id' => $activityStatusGroup['id'], |
708d8fa2 TO |
96 | ); |
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(); | |
118 | $activities = array(); | |
119 | if (isset($case['activities'])) { | |
120 | foreach ($case['activities'] as $actId) { | |
121 | $result = civicrm_api3('Activity', 'get', array( | |
122 | 'id' => $actId, | |
123 | 'is_current_revision' => 1, | |
124 | )); | |
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. |
708d8fa2 TO |
135 | * |
136 | * @param string $type | |
137 | * @throws \Civi\CCase\Exception\MultipleActivityException | |
138 | * @return array|NULL, activity record (api/v3) | |
139 | */ | |
140 | public function getSingleActivity($type) { | |
141 | $idx = $this->getActivityIndex(array('activity_type_id', 'id')); | |
142 | $actTypes = array_flip(\CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name')); | |
143 | $typeId = $actTypes[$type]; | |
144 | $count = isset($idx[$typeId]) ? count($idx[$typeId]) : 0; | |
145 | ||
146 | if ($count === 0) { | |
147 | return NULL; | |
148 | } | |
149 | elseif ($count === 1) { | |
150 | foreach ($idx[$typeId] as $item) { | |
151 | return $item; | |
152 | } | |
153 | } | |
154 | else { | |
155 | throw new \Civi\CCase\Exception\MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count); | |
156 | } | |
157 | } | |
158 | ||
159 | /** | |
160 | * @return int | |
161 | */ | |
162 | public function getCaseId() { | |
163 | return $this->caseId; | |
164 | } | |
165 | ||
166 | /** | |
167 | * @return array, Case record (api/v3 format) | |
168 | */ | |
169 | public function getCase() { | |
170 | if ($this->case === NULL) { | |
171 | $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId)); | |
172 | } | |
173 | return $this->case; | |
174 | } | |
175 | ||
176 | /** | |
177 | * @return string | |
45322593 | 178 | * @throws \CRM_Core_Exception |
708d8fa2 TO |
179 | */ |
180 | public function getCaseType() { | |
181 | if ($this->caseType === NULL) { | |
182 | $case = $this->getCase(); | |
183 | $caseTypes = \CRM_Case_XMLRepository::singleton()->getAllCaseTypes(); | |
184 | if (!isset($caseTypes[$case['case_type_id']])) { | |
185 | throw new \CRM_Core_Exception("Case does not have a recognized case-type!"); | |
186 | } | |
187 | $this->caseType = $caseTypes[$case['case_type_id']]; | |
188 | } | |
189 | return $this->caseType; | |
190 | } | |
191 | ||
192 | /** | |
193 | * Get a list of all activities in the case (indexed by some property/properties) | |
194 | * | |
04855556 TO |
195 | * @param array $keys |
196 | * List of properties by which to index activities. | |
a6c01b45 CW |
197 | * @return array |
198 | * list of activity records (api/v3 format), indexed by $keys | |
708d8fa2 TO |
199 | */ |
200 | public function getActivityIndex($keys) { | |
201 | $key = implode(";", $keys); | |
202 | if (!isset($this->indices[$key])) { | |
203 | $this->indices[$key] = \CRM_Utils_Array::index($keys, $this->getActivities()); | |
204 | } | |
205 | return $this->indices[$key]; | |
206 | } | |
207 | ||
208 | /** | |
45322593 | 209 | * @return \SimpleXMLElement|NULL |
708d8fa2 TO |
210 | */ |
211 | public function getXml() { | |
212 | if ($this->xml === NULL) { | |
213 | $this->xml = \CRM_Case_XMLRepository::singleton()->retrieve($this->getCaseType()); | |
214 | } | |
215 | return $this->xml; | |
216 | } | |
217 | ||
218 | /** | |
fe482240 | 219 | * Flush any cached information. |
708d8fa2 TO |
220 | */ |
221 | public function flush() { | |
222 | $this->case = NULL; | |
223 | $this->caseType = NULL; | |
224 | $this->activities = NULL; | |
225 | $this->indices = array(); | |
226 | } | |
96025800 | 227 | |
c206647d | 228 | } |