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