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");
44 $caseActivities = array();
45 $activityStatusType = array();
47 foreach ($activityList as $activity) {
48 $retval[$activityindex] = array();
50 $ifBlankReplacements = array();
53 $sortValues = array('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 $label_elements = $field->getElementsByTagName("Label");
62 $label = $label_elements->item(0)->nodeValue
;
64 $value_elements = $field->getElementsByTagName("Value");
65 $value = $value_elements->item(0)->nodeValue
;
67 $category_elements = $field->getElementsByTagName("Category");
68 if (!empty($category_elements->length
)) {
69 $category = $category_elements->item(0)->nodeValue
;
72 // Based on the config file, does this field's label and value indicate a completed activity?
73 if ($label == $this->auditConfig
->getCompletionLabel() && $value == $this->auditConfig
->getCompletionValue()) {
77 // Based on the config file, does this field's label match the one to use for sorting activities?
78 if (in_array($label, $this->auditConfig
->getSortByLabels())) {
79 $sortValues[$label] = $value;
82 foreach ($regionList as $region) {
83 // Based on the config file, is this field a potential replacement for another?
84 if (!empty($ifBlanks[$region])) {
85 if (in_array($label, $ifBlanks[$region])) {
86 $ifBlankReplacements[$label] = $value;
90 if ($this->auditConfig
->includeInRegion($label, $region)) {
91 $retval[$activityindex][$region][$fieldindex] = array();
92 $retval[$activityindex][$region][$fieldindex]['label'] = $label;
93 $retval[$activityindex][$region][$fieldindex]['datatype'] = $datatype;
94 $retval[$activityindex][$region][$fieldindex]['value'] = $value;
95 if ($datatype == 'Date') {
96 $retval[$activityindex][$region][$fieldindex]['includeTime'] = $this->auditConfig
->includeTime($label, $region);
101 if (!in_array($label, array(
106 $caseActivities[$activityindex][$fieldindex] = array();
107 $caseActivities[$activityindex][$fieldindex]['label'] = $label;
108 $caseActivities[$activityindex][$fieldindex]['datatype'] = $datatype;
109 $caseActivities[$activityindex][$fieldindex]['value'] = $value;
112 $activityStatusType[$activityindex][$fieldindex] = array();
113 $activityStatusType[$activityindex][$fieldindex]['label'] = $label;
114 $activityStatusType[$activityindex][$fieldindex]['datatype'] = $datatype;
115 $activityStatusType[$activityindex][$fieldindex]['value'] = $value;
125 $caseActivities[$activityindex] = CRM_Utils_Array
::crmArrayMerge($activityStatusType[$activityindex], $caseActivities[$activityindex]);
126 $caseActivities[$activityindex]['sortValues'] = $sortValues;
129 if ($includeAll ||
!$completed) {
130 $retval[$activityindex]['completed'] = $completed;
131 $retval[$activityindex]['category'] = $category;
132 $retval[$activityindex]['sortValues'] = $sortValues;
134 // Now sort the fields based on the order in the config file.
135 foreach ($regionList as $region) {
136 $this->auditConfig
->sort($retval[$activityindex][$region], $region);
139 $retval[$activityindex]['editurl'] = $activity->getElementsByTagName("EditURL")->item(0)->nodeValue
;
141 // If there are any fields with ifBlank specified, replace their values.
142 // 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.
143 foreach ($regionList as $region) {
144 foreach ($retval[$activityindex][$region] as & $v) {
145 $vlabel = $v['label'];
146 if (trim($v['value']) == '' && !empty($ifBlanks[$region][$vlabel])) {
147 if (!empty($ifBlankReplacements[$ifBlanks[$region][$vlabel]])) {
148 $v['value'] = $ifBlankReplacements[$ifBlanks[$region][$vlabel]];
158 /* This is a little bit inefficient, but the alternative is to do two passes
159 because we don't know until we've examined all the field values whether the activity
160 is completed, since the field that determines it and its value is configurable,
161 so either way isn't ideal. */
163 unset($retval[$activityindex]);
164 unset($caseActivities[$activityindex]);
169 @uasort
($caseActivities, array($this, "compareActivities"));
172 @uasort
($retval, array($this, "compareActivities"));
177 return $caseActivities;
186 * This is intended to be called as a sort callback function, returning whether an activity's date is earlier or later than another's.
187 * The type of date to use is specified in the config.
196 public function compareActivities($a, $b) {
198 foreach ($this->auditConfig
->getSortByLabels() as $label) {
199 $aval .= empty($a['sortValues']) ?
"" : (empty($a['sortValues'][$label]) ?
"" : $a['sortValues'][$label]);
200 $bval .= empty($b['sortValues']) ?
"" : (empty($b['sortValues'][$label]) ?
"" : $b['sortValues'][$label]);
206 elseif ($aval > $bval) {
215 * @param string $xmlString
216 * @param int $clientID
218 * @param bool $printReport
222 public static function run($xmlString, $clientID, $caseID, $printReport = FALSE) {
224 $fh = fopen('C:/temp/audit2.xml', 'w');
225 fwrite($fh, $xmlString);
229 $audit = new CRM_Case_Audit_Audit($xmlString, 'audit.conf.xml');
230 $activities = $audit->getActivities($printReport);
232 $template = CRM_Core_Smarty
::singleton();
233 $template->assign_by_ref('activities', $activities);
236 $reportDate = CRM_Utils_Date
::customFormat(date('Y-m-d H:i'));
237 $template->assign('reportDate', $reportDate);
238 $contents = $template->fetch('CRM/Case/Audit/Report.tpl');
241 $contents = $template->fetch('CRM/Case/Audit/Audit.tpl');