4 * Class CRM_Case_Audit_Audit
6 class CRM_Case_Audit_Audit
{
12 * @param string $confFilename
14 public function __construct($xmlString, $confFilename) {
15 $this->xmlString
= $xmlString;
16 $this->auditConfig
= new CRM_Case_Audit_AuditConfig($confFilename);
20 * @param bool $printReport
24 public function getActivities($printReport = FALSE) {
28 * Loop through the activities in the file and add them to the appropriate region array.
31 $doc = new DOMDocument();
33 if ($doc->loadXML($this->xmlString
)) {
34 $regionList = $this->auditConfig
->getRegions();
36 $ifBlanks = $this->auditConfig
->getIfBlanks();
38 $includeAll = $doc->getElementsByTagName("IncludeActivities")->item(0)->nodeValue
;
39 $includeAll = ($includeAll == 'All');
42 $activityList = $doc->getElementsByTagName("Activity");
45 $activityStatusType = [];
47 foreach ($activityList as $activity) {
48 $retval[$activityindex] = [];
50 $ifBlankReplacements = [];
53 $sortValues = ['1970-01-01'];
56 $fields = $activity->getElementsByTagName("Field");
57 foreach ($fields as $field) {
58 $datatype_elements = $field->getElementsByTagName("Type");
59 $datatype = $datatype_elements->item(0)->nodeValue
;
61 $name_elements = $field->getElementsByTagName("Name");
62 $name = $name_elements->item(0)->nodeValue
;
64 $label_elements = $field->getElementsByTagName("Label");
65 $label = $label_elements->item(0)->nodeValue
;
67 $value_elements = $field->getElementsByTagName("Value");
68 $value = $value_elements->item(0)->nodeValue
;
70 $category_elements = $field->getElementsByTagName("Category");
71 if (!empty($category_elements->length
)) {
72 $category = $category_elements->item(0)->nodeValue
;
75 // Based on the config file, does this field's label and value indicate a completed activity?
76 if ($label == $this->auditConfig
->getCompletionLabel() && $value == $this->auditConfig
->getCompletionValue()) {
80 // Based on the config file, does this field's label match the one to use for sorting activities?
81 if (in_array($label, $this->auditConfig
->getSortByLabels())) {
82 $sortValues[$label] = $value;
85 foreach ($regionList as $region) {
86 // Based on the config file, is this field a potential replacement for another?
87 if (!empty($ifBlanks[$region])) {
88 if (in_array($label, $ifBlanks[$region])) {
89 $ifBlankReplacements[$label] = $value;
93 if ($this->auditConfig
->includeInRegion($label, $region)) {
94 $retval[$activityindex][$region][$fieldindex] = [];
95 $retval[$activityindex][$region][$fieldindex]['name'] = $name;
96 $retval[$activityindex][$region][$fieldindex]['label'] = $label;
97 $retval[$activityindex][$region][$fieldindex]['datatype'] = $datatype;
98 $retval[$activityindex][$region][$fieldindex]['value'] = $value;
99 if ($datatype == 'Date') {
100 $retval[$activityindex][$region][$fieldindex]['includeTime'] = $this->auditConfig
->includeTime($label, $region);
105 if (!in_array($label, [
110 $caseActivities[$activityindex][$fieldindex] = [];
111 $caseActivities[$activityindex][$fieldindex]['name'] = $name;
112 $caseActivities[$activityindex][$fieldindex]['label'] = $label;
113 $caseActivities[$activityindex][$fieldindex]['datatype'] = $datatype;
114 $caseActivities[$activityindex][$fieldindex]['value'] = $value;
117 $activityStatusType[$activityindex][$fieldindex] = [];
118 $activityStatusType[$activityindex][$fieldindex]['name'] = $name;
119 $activityStatusType[$activityindex][$fieldindex]['label'] = $label;
120 $activityStatusType[$activityindex][$fieldindex]['datatype'] = $datatype;
121 $activityStatusType[$activityindex][$fieldindex]['value'] = $value;
131 $caseActivities[$activityindex] = CRM_Utils_Array
::crmArrayMerge($activityStatusType[$activityindex], $caseActivities[$activityindex]);
132 $caseActivities[$activityindex]['sortValues'] = $sortValues;
135 if ($includeAll ||
!$completed) {
136 $retval[$activityindex]['completed'] = $completed;
137 $retval[$activityindex]['category'] = $category;
138 $retval[$activityindex]['sortValues'] = $sortValues;
140 // Now sort the fields based on the order in the config file.
141 foreach ($regionList as $region) {
142 $this->auditConfig
->sort($retval[$activityindex][$region], $region);
145 $retval[$activityindex]['editurl'] = $activity->getElementsByTagName("EditURL")->item(0)->nodeValue
;
147 // If there are any fields with ifBlank specified, replace their values.
148 // We need to do this as a second pass because if we do it while looping through fields we might not have come across the field we need yet.
149 foreach ($regionList as $region) {
150 foreach ($retval[$activityindex][$region] as & $v) {
151 $vlabel = $v['label'];
152 if (trim($v['value']) == '' && !empty($ifBlanks[$region][$vlabel])) {
153 if (!empty($ifBlankReplacements[$ifBlanks[$region][$vlabel]])) {
154 $v['value'] = $ifBlankReplacements[$ifBlanks[$region][$vlabel]];
164 /* This is a little bit inefficient, but the alternative is to do two passes
165 because we don't know until we've examined all the field values whether the activity
166 is completed, since the field that determines it and its value is configurable,
167 so either way isn't ideal. */
169 unset($retval[$activityindex]);
170 unset($caseActivities[$activityindex]);
175 @uasort
($caseActivities, [$this, "compareActivities"]);
178 @uasort
($retval, [$this, "compareActivities"]);
183 return $caseActivities;
192 * This is intended to be called as a sort callback function, returning whether an activity's date is earlier or later than another's.
193 * The type of date to use is specified in the config.
202 public function compareActivities($a, $b) {
204 foreach ($this->auditConfig
->getSortByLabels() as $label) {
205 $aval .= empty($a['sortValues']) ?
"" : (empty($a['sortValues'][$label]) ?
"" : $a['sortValues'][$label]);
206 $bval .= empty($b['sortValues']) ?
"" : (empty($b['sortValues'][$label]) ?
"" : $b['sortValues'][$label]);
212 elseif ($aval > $bval) {
221 * @param string $xmlString
222 * @param int $clientID
227 public static function run($xmlString, $clientID, $caseID) {
228 $audit = new CRM_Case_Audit_Audit($xmlString, 'audit.conf.xml');
229 $activities = $audit->getActivities(TRUE);
231 $template = CRM_Core_Smarty
::singleton();
232 $template->assign_by_ref('activities', $activities);
234 $reportDate = CRM_Utils_Date
::customFormat(date('Y-m-d H:i'));
235 $template->assign('reportDate', $reportDate);
236 $contents = $template->fetch('CRM/Case/Audit/Report.tpl');