code cleanup
[civicrm-core.git] / Civi / CCase / Analyzer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
69 * Eg "Phone Call", "Interview Prospect", "Background Check".
70 * @param string $status
71 * Eg "Scheduled", "Completed".
72 * @return bool
73 */
74 public function hasActivity($type, $status = NULL) {
75 $idx = $this->getActivityIndex(array('activity_type_id', 'status_id'));
76 $activityTypeGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_type'));
77 $activityType = array(
78 'name' => $type,
79 'option_group_id' => $activityTypeGroup['id'],
80 );
81 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
82 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
83 if ($status) {
84 $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status'));
85 $activityStatus = array(
86 'name' => $status,
87 'option_group_id' => $activityStatusGroup['id'],
88 );
89 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
90 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
91 }
92 if ($status === NULL) {
93 return !empty($idx[$activityTypeID]);
94 }
95 else {
96 return !empty($idx[$activityTypeID][$activityStatusID]);
97 }
98 }
99
100 /**
101 * Get a list of all activities in the case
102 *
103 * @return array
104 * list of activity records (api/v3 format)
105 */
106 public function getActivities() {
107 if ($this->activities === NULL) {
108 // TODO find batch-oriented API for getting all activities in a case
109 $case = $this->getCase();
110 $activities = array();
111 if (isset($case['activities'])) {
112 foreach ($case['activities'] as $actId) {
113 $result = civicrm_api3('Activity', 'get', array(
114 'id' => $actId,
115 'is_current_revision' => 1,
116 ));
117 $activities = array_merge($activities, $result['values']);
118 }
119 }
120 $this->activities = $activities;
121 }
122 return $this->activities;
123 }
124
125 /**
126 * Get a single activity record by type
127 *
128 * @param string $type
129 * @throws \Civi\CCase\Exception\MultipleActivityException
130 * @return array|NULL, activity record (api/v3)
131 */
132 public function getSingleActivity($type) {
133 $idx = $this->getActivityIndex(array('activity_type_id', 'id'));
134 $actTypes = array_flip(\CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'));
135 $typeId = $actTypes[$type];
136 $count = isset($idx[$typeId]) ? count($idx[$typeId]) : 0;
137
138 if ($count === 0) {
139 return NULL;
140 }
141 elseif ($count === 1) {
142 foreach ($idx[$typeId] as $item) {
143 return $item;
144 }
145 }
146 else {
147 throw new \Civi\CCase\Exception\MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
148 }
149 }
150
151 /**
152 * @return int
153 */
154 public function getCaseId() {
155 return $this->caseId;
156 }
157
158 /**
159 * @return array, Case record (api/v3 format)
160 */
161 public function getCase() {
162 if ($this->case === NULL) {
163 $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId));
164 }
165 return $this->case;
166 }
167
168 /**
169 * @return string
170 */
171 public function getCaseType() {
172 if ($this->caseType === NULL) {
173 $case = $this->getCase();
174 $caseTypes = \CRM_Case_XMLRepository::singleton()->getAllCaseTypes();
175 if (!isset($caseTypes[$case['case_type_id']])) {
176 throw new \CRM_Core_Exception("Case does not have a recognized case-type!");
177 }
178 $this->caseType = $caseTypes[$case['case_type_id']];
179 }
180 return $this->caseType;
181 }
182
183 /**
184 * Get a list of all activities in the case (indexed by some property/properties)
185 *
186 * @param array $keys
187 * List of properties by which to index activities.
188 * @return array
189 * list of activity records (api/v3 format), indexed by $keys
190 */
191 public function getActivityIndex($keys) {
192 $key = implode(";", $keys);
193 if (!isset($this->indices[$key])) {
194 $this->indices[$key] = \CRM_Utils_Array::index($keys, $this->getActivities());
195 }
196 return $this->indices[$key];
197 }
198
199 /**
200 * @return SimpleXMLElement|NULL
201 */
202 public function getXml() {
203 if ($this->xml === NULL) {
204 $this->xml = \CRM_Case_XMLRepository::singleton()->retrieve($this->getCaseType());
205 }
206 return $this->xml;
207 }
208
209 /**
210 * Flush any cached information
211 *
212 * @return void
213 */
214 public function flush() {
215 $this->case = NULL;
216 $this->caseType = NULL;
217 $this->activities = NULL;
218 $this->indices = array();
219 }
220 }