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(
102 'Activity Type', 'Status'))) {
103 $caseActivities[$activityindex][$fieldindex] = array();
104 $caseActivities[$activityindex][$fieldindex]['label'] = $label;
105 $caseActivities[$activityindex][$fieldindex]['datatype'] = $datatype;
106 $caseActivities[$activityindex][$fieldindex]['value'] = $value;
109 $activityStatusType[$activityindex][$fieldindex] = array();
110 $activityStatusType[$activityindex][$fieldindex]['label'] = $label;
111 $activityStatusType[$activityindex][$fieldindex]['datatype'] = $datatype;
112 $activityStatusType[$activityindex][$fieldindex]['value'] = $value;
122 $caseActivities[$activityindex] = CRM_Utils_Array
::crmArrayMerge($activityStatusType[$activityindex], $caseActivities[$activityindex]);
123 $caseActivities[$activityindex]['sortValues'] = $sortValues;
126 if ($includeAll ||
!$completed) {
127 $retval[$activityindex]['completed'] = $completed;
128 $retval[$activityindex]['category'] = $category;
129 $retval[$activityindex]['sortValues'] = $sortValues;
131 // Now sort the fields based on the order in the config file.
132 foreach ($regionList as $region) {
133 $this->auditConfig
->sort($retval[$activityindex][$region], $region);
136 $retval[$activityindex]['editurl'] = $activity->getElementsByTagName("EditURL")->item(0)->nodeValue
;
138 // If there are any fields with ifBlank specified, replace their values.
139 // 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.
140 foreach ($regionList as $region) {
141 foreach ($retval[$activityindex][$region] as & $v) {
142 $vlabel = $v['label'];
143 if (trim($v['value']) == '' && !empty($ifBlanks[$region][$vlabel])) {
144 if (!empty($ifBlankReplacements[$ifBlanks[$region][$vlabel]])) {
145 $v['value'] = $ifBlankReplacements[$ifBlanks[$region][$vlabel]];
155 /* This is a little bit inefficient, but the alternative is to do two passes
156 because we don't know until we've examined all the field values whether the activity
157 is completed, since the field that determines it and its value is configurable,
158 so either way isn't ideal. */
160 unset($retval[$activityindex]);
161 unset($caseActivities[$activityindex]);
166 @uasort
($caseActivities, array($this, "compareActivities"));
169 @uasort
($retval, array($this, "compareActivities"));
174 return $caseActivities;
183 * This is intended to be called as a sort callback function, returning whether an activity's date is earlier or later than another's.
184 * The type of date to use is specified in the config.
194 public function compareActivities($a, $b) {
196 foreach ($this->auditConfig
->getSortByLabels() as $label) {
197 $aval .= empty($a['sortValues']) ?
"" : (empty($a['sortValues'][$label]) ?
"" : $a['sortValues'][$label]);
198 $bval .= empty($b['sortValues']) ?
"" : (empty($b['sortValues'][$label]) ?
"" : $b['sortValues'][$label]);
204 elseif ($aval > $bval) {
214 * @param int $clientID
216 * @param bool $printReport
221 public function run($xmlString, $clientID, $caseID, $printReport = FALSE) {
223 $fh = fopen('C:/temp/audit2.xml', 'w');
224 fwrite($fh, $xmlString);
228 $audit = new CRM_Case_Audit_Audit($xmlString, 'audit.conf.xml');
229 $activities = $audit->getActivities($printReport);
231 $template = CRM_Core_Smarty
::singleton();
232 $template->assign_by_ref('activities', $activities);
235 $reportDate = CRM_Utils_Date
::customFormat(date('Y-m-d H:i'));
236 $template->assign('reportDate', $reportDate);
237 $contents = $template->fetch('CRM/Case/Audit/Report.tpl');
240 $contents = $template->fetch('CRM/Case/Audit/Audit.tpl');