Example #1
0
function getSubjectDataAsXml($subjectLabel, Database $db)
{
    if (empty($subjectLabel)) {
        throw new Exception("Empty subject label.");
    }
    //Retrieve the form data
    $result = $db->getSubjectData($subjectLabel);
    //Retrieve and decode the final data
    $finaldata = $db->getSubjectFinalData($subjectLabel);
    $skipped = 0;
    $xmlerrors = 0;
    $count = 0;
    //Register a new error handler to handle libxml specific errors
    $originalHandler = set_error_handler("libxml_error_handler");
    //Create an XML document to store all the data (properly)
    $xmldoc = new DomDocument();
    $xmldoc->loadXML('<result subject="' . $subjectLabel . '"/>');
    //Create an XML document to store the final form data. This will be appended
    //to $xmldoc.
    $xmlFinal = $xmldoc->createDocumentFragment();
    //Strip the <?xml .. tag
    $finalxmldata = str_replace('<?xml version="1.0"?>', '', $finaldata['data']);
    $xmlFinal->appendXML($finalxmldata);
    $nodeFinal = $xmldoc->createElement('final');
    if ($xmlFinal->hasChildNodes()) {
        $nodeFinal->appendChild($xmlFinal);
    }
    $locked = 0;
    if ($finaldata['locked'] > 0) {
        $locked = $finaldata['locked'];
    }
    $nodeFinal->setAttribute('locked', $locked);
    //Insert the <final> element into the main XML document.
    $xmldoc->documentElement->appendChild($nodeFinal);
    /* Loop through all the sessions and append the data */
    $lastSessionId = null;
    $xmlSession = null;
    //Get all the elements to construct a valid XML file
    foreach ($result as $row) {
        //Element doesn't conform to our schema arch? skip it.
        if (empty($row['schemaName'])) {
            $skipped++;
            continue;
        }
        $sessionId = $row['fkSessionID'];
        $schema = $row['schemaName'];
        $sessionLabel = $row['label'];
        $dateSessionCreated = $row['dateSessionCreated'];
        $subjectLabel = $row['subjectLabel'];
        $owner = $row['username'];
        $formId = $row['formID'];
        //Test data integrity
        try {
            $xmlForm = new DomDocument();
            if (!$xmlForm->loadXML($row['data'])) {
                throw new Exception("Invalid XML data: Session " . $sessionLabel);
            }
            //Insert metadata as attributes of the <form> element
            $xmlForm->documentElement->setAttribute('id', $formId);
            $xmlForm->documentElement->setAttribute('schema', $schema);
        } catch (Exception $e) {
            //Keep track of errors
            $xmlerrors++;
            $skipped++;
            continue;
        }
        //Should we start a new session?
        if ($lastSessionId != $sessionId) {
            $count++;
            //Create a <session id='foo'/> element and append it to the document
            $nodeSession = $xmldoc->createElement('session');
            $nodeSession->setAttribute('id', $sessionLabel);
            $nodeSession->setAttribute('owner', $owner);
            $nodeSession->setAttribute('date', $dateSessionCreated);
            //Insert the <session> element into the xml document
            $xmlSession = $xmldoc->documentElement->appendChild($nodeSession);
        }
        //Copy the data document as a child element of the sesssion
        $datanode = $xmldoc->importNode($xmlForm->documentElement, true);
        $nodeSession->appendChild($datanode);
        $lastSessionId = $sessionId;
    }
    //xmldoc contains the result organized as a list of sessions with form data
    $xmldoc->documentElement->setAttribute('total', $count);
    //Restore the original error handler
    if (!empty($originalHandler)) {
        set_error_handler($originalHandler);
    }
    //print $xmldoc->saveXML();
    return $xmldoc;
}