Example #1
0
function handleupload(&$errors, &$warnings, &$messages)
{
    if (empty($_FILES) || empty($_FILES["file"])) {
        $errors[] = "No file was uploaded";
        return false;
    }
    if ($_FILES["file"]["error"]) {
        $errors[] = "There was a problem uploading the file — try again in a short while";
        return false;
    }
    switch ($_FILES["file"]["type"]) {
        case "application/xml":
        case "text/xml":
        case "application/qti+xml":
            $metadata = array();
            $filename = $_FILES["file"]["name"];
            $xml = file_get_contents($_FILES["file"]["tmp_name"]);
            break;
        case "application/zip":
        case "application/x-zip-compressed":
        case "application/x-zip":
            // open zip file
            $zip = new ZipArchive();
            if ($zip->open($_FILES["file"]["tmp_name"]) !== true) {
                $errors[] = "Failed to open the zip file. Ensure it is not corrupt and try again.";
                return false;
            }
            // get manifest, make sure it's valid XML
            $mxml = $zip->getfromName("imsmanifest.xml");
            if ($mxml === false) {
                $errors[] = "Error getting manifest file -- are you sure this is a content package?";
                return false;
            }
            $mxml = simplexml_load_string($mxml);
            if ($mxml === false) {
                $errors[] = "The manifest file in the uploaded content package is not valid XML";
                return false;
            }
            $metadata = array();
            // get manifest identifier
            $metadata["midentifier"] = (string) $mxml["identifier"];
            // ensure there's only one resource
            $resource = $mxml->resources->resource;
            if (count($resource) > 1) {
                $errors[] = "More than one resource element found in the manifest -- this is not a single-item content package";
                return false;
            }
            if (count($resource) == 0) {
                $errors[] = "No resource elements found in the manifest -- this is not a single-item content package";
                return false;
            }
            // ensure it's an item rather than an assessment
            if (!preg_match('%^imsqti_item_%', (string) $resource["type"])) {
                $errors[] = "This content package contains an assessment test rather than an assessment item";
                return false;
            }
            // get the metadata
            $imsmd = $resource->metadata->children(NS_IMSMD);
            if (isset($imsmd->lom->general->description->langstring[0])) {
                $metadata["description"] = (string) $imsmd->lom->general->description->langstring[0];
            }
            $metadata["keywords"] = array();
            foreach ($imsmd->lom->general->keyword as $keyword) {
                $metadata["keywords"][] = (string) $keyword->langstring[0];
            }
            // get the file pointed to
            $filename = (string) $resource["href"];
            $xml = $zip->getfromName($filename);
            if ($xml === false) {
                $errors[] = "Error getting item file \"{$filename}\" from archive";
                return false;
            }
            break;
        default:
            $errors[] = "The uploaded file (of type " . $_FILES["file"]["type"] . ") was not recognized as a content package (zip) or QTI XML file.";
            return false;
    }
    return array($xml, $metadata);
}