4892: ims_import.php: fix several issues that cause import failures
authorTJ <acontent@iam.tj>
Thu, 20 Oct 2011 13:14:42 +0000 (14:14 +0100)
committerTJ <acontent@iam.tj>
Thu, 20 Oct 2011 13:42:01 +0000 (14:42 +0100)
  * Weblinks: correctly locate link resource file by replacing incorrect
     $content_info['href'] with $content_info['file'][0]
  * Weblinks: ensure weblink file has been parsed correctly before assigning variables
  * capture and use result from $contentDAO->Create()
  * undefined indexes: $items[$current_item_id], $items[$dependency_ref],
     $items[$content_info['parent_content_id']], $items[$item_id]['type']
     $content_info['type'], $content_info['dependency'], $content_info['href'], $content_info['new_path'],
  * undefined variables: $ext, $full_filename, $lti_offset[$content_info['parent_content_id']],

home/ims/ims_import.php

index 0595e9d..dbef6e9 100644 (file)
@@ -716,7 +716,7 @@ function removeCommonPath($items){
                global $current_identifier;
 
                $str_trimmed_data = trim($data);
-               
+
                if (!empty($str_trimmed_data)) {
                        $size = count($path);
                        if ($size > 0) {
@@ -727,8 +727,7 @@ function removeCommonPath($items){
                                        $parent_item_id = 0;
                                }
 
-                               if (isset($items[$current_item_id]['parent_content_id']) && is_array($items[$current_item_id])) {
-
+                               if (array_key_exists($current_item_id, $items) && is_array($items[$current_item_id]) && isset($items[$current_item_id]['parent_content_id'])) {
                                        /* this item already exists, append the title           */
                                        /* this fixes {\n, \t, `, &} characters in elements */
 
@@ -838,7 +837,7 @@ if (isset($_REQUEST['url']) && ($_REQUEST['url'] != 'http://') ) {
     $package_base_name_url = md5(time());
 }
 $ext = pathinfo($_FILES['file']['name']);
-$ext = $ext['extension'];
+$ext = array_key_exists('extension', $ext) ? $ext['extension'] : '';
 
 if ($ext != 'zip') {
        $msg->addError('IMPORTDIR_IMS_NOTVALID');
@@ -859,7 +858,7 @@ if ($msg->containsErrors()) {
        } else {
                header('Location: '.$_SERVER['HTTP_REFERER']);
        }
-       if (file_exists($full_filename)) @unlink($full_filename);
+       if (isset($full_filename) && file_exists($full_filename)) @unlink($full_filename);
        exit;
 }
 
@@ -1090,17 +1089,18 @@ $common_path = removeCommonPath($items);
 $items = rehash($items);
 //debug($items);exit;
 foreach ($items as $item_id => $content_info) 
-{      
+{
        //formatting field, default 1
        $content_formatting = 1;        //CONTENT_TYPE_CONTENT
+       $lti_offset[$content_info['parent_content_id']] = 0;
 
        //don't want to display glossary as a page
-       if ($content_info['href']== 'glossary.xml'){
+       if (isset($content_info['href']) && $content_info['href'] == 'glossary.xml'){
                continue;
        }
 
        //if discussion tools, add it to the list of unhandled dts
-       if ($content_info['type']=='imsdt_xmlv1p0'){
+       if (isset($content_info['type']) && $content_info['type'] == 'imsdt_xmlv1p0'){
                //if it will be taken care after (has dependency), then move along.
                if (in_array($item_id, $avail_dt)){
                        $lti_offset[$content_info['parent_content_id']]++;
@@ -1124,14 +1124,14 @@ foreach ($items as $item_id => $content_info)
        
        //check dependency immediately, then handles it
        $head = '';
-       if (is_array($content_info['dependency']) && !empty($content_info['dependency'])){
+       if (isset($content_info['dependency']) && is_array($content_info['dependency']) && !empty($content_info['dependency'])){
                foreach($content_info['dependency'] as $dependency_ref){
                        //check if this is a discussion tool dependency
-                       if ($items[$dependency_ref]['type']=='imsdt_xmlv1p0'){
+                       if (array_key_exists($dependency_ref, $items) && $items[$dependency_ref]['type']=='imsdt_xmlv1p0'){
                                $items[$item_id]['forum'][$dependency_ref] = $items[$dependency_ref]['href'];
                        }
                        //check if this is a QTI dependency
-                       if (strpos($items[$dependency_ref]['type'], 'imsqti_xmlv1p2/imscc_xmlv1p0') !== false){
+                       if (array_key_exists($dependency_ref, $items) && strpos($items[$dependency_ref]['type'], 'imsqti_xmlv1p2/imscc_xmlv1p0') !== false){
                                $items[$item_id]['tests'][$dependency_ref] = $items[$dependency_ref]['href'];
                        }
                }
@@ -1139,7 +1139,7 @@ foreach ($items as $item_id => $content_info)
 
 
        // remote href
-       if (preg_match('/^http.*:\/\//', trim($content_info['href'])) )
+       if (isset($content_info['href']) && preg_match('/^http.*:\/\//', trim($content_info['href'])) )
        {
                $content = '<a href="'.$content_info['href'].'" target="_blank">'.$content_info['title'].'</a>';
        }
@@ -1148,7 +1148,7 @@ foreach ($items as $item_id => $content_info)
                if ($content_type == 'IMS Common Cartridge'){
                        //to handle import with purely images but nothing else
                        //don't need a content base path for it.
-                       $content_new_path = $content_info['new_path'];
+                       $content_new_path = isset($content_info['new_path']) ? $content_info['new_path'] : '';
                        $content_info['new_path'] = '';
                }
                if (isset($content_info['href'], $xml_base_path)) {
@@ -1260,7 +1260,7 @@ foreach ($items as $item_id => $content_info)
        }
        $content_parent_id = $cid;
        if ($content_info['parent_content_id'] !== 0) {
-               $content_parent_id = $items[$content_info['parent_content_id']]['real_content_id'];
+               $content_parent_id = isset($items[$content_info['parent_content_id']]) ? $items[$content_info['parent_content_id']]['real_content_id'] : 0;
                //if it's not there, use $cid
                if (!$content_parent_id){
                        $content_parent_id = $cid;
@@ -1290,20 +1290,21 @@ foreach ($items as $item_id => $content_info)
        }
 
        //handles weblinks
-       if ($content_info['type']=='imswl_xmlv1p0'){
+       if (isset($content_info['type']) && $content_info['type']=='imswl_xmlv1p0'){
                $weblinks_parser = new WeblinksParser();
-               $xml_content = @file_get_contents($import_path . $content_info['href']);
-               $weblinks_parser->parse($xml_content);
-               $content_info['title'] = $weblinks_parser->getTitle();
-               $content = $weblinks_parser->getUrl();
-               $content_folder_type = CONTENT_TYPE_WEBLINK;
-               $content_formatting = 2;
+               $xml_content = @file_get_contents($import_path . $content_info['file'][0]);
+               if ($weblinks_parser->parse($xml_content)) {
+                       $content_info['title'] = $weblinks_parser->getTitle();
+                       $content = $weblinks_parser->getUrl();
+                       $content_folder_type = CONTENT_TYPE_WEBLINK;
+                       $content_formatting = 2;
+               }
        }
 
 
        //if this file is a test_xml, create a blank page instead, for imscc.
-       if (preg_match('/((.*)\/)*tests\_[0-9]+\.xml$/', $content_info['href']
-               || preg_match('/imsqti\_(.*)/', $content_info['type'])) {
+       if ((isset($content_info['href']) && preg_match('/((.*)\/)*tests\_[0-9]+\.xml$/', $content_info['href'])
+               || (isset($content_info['type']) && preg_match('/imsqti\_(.*)/', $content_info['type']))) {
                $content = ' ';
        } 
 
@@ -1312,15 +1313,17 @@ foreach ($items as $item_id => $content_info)
        if ($content_formatting!=CONTENT_TYPE_WEBLINK){
                $content_folder_type = (!isset($content_info['type'])?CONTENT_TYPE_FOLDER:CONTENT_TYPE_CONTENT);
        }
-       
-       $items[$item_id]['real_content_id'] = $contentDAO->Create($_course_id, intval($content_parent_id), 
-                           ($content_info['ordering'] + $my_offset - $lti_offset[$content_info['parent_content_id']] + 1),
-                           0, $content_formatting, "", $content_info['new_path'], $content_info['title'],
-                           $content, $head, 1, $content_info['test_message'], $content_folder_type);
-
-
+       $result = $contentDAO->Create($_course_id, intval($content_parent_id), 
+               ($content_info['ordering'] + $my_offset - (array_key_exists($content_info['parent_content_id'], $lti_offset) ? $lti_offset[$content_info['parent_content_id']] : 0) + 1),
+               0, $content_formatting, "", $content_info['new_path'], $content_info['title'],
+               $content, $head, 1, array_key_exists('test_message',$content_info) ? $content_info['test_message'] : '', $content_folder_type);
+       if ($result)
+                       $items[$item_id]['real_content_id'] = $result;
+       else {
+               $msg->addError(array('IMPORT_CARTRIDGE_FAILED', 'course_id='.$_course_id.', $item_id='.$item_id.', $content_parent_id='.$content_parent_id.', title="'.$content_info['title'].'"'."\n"));
+       }
        /* get the tests associated with this content */
-       if (!empty($items[$item_id]['tests']) || strpos($items[$item_id]['type'], 'imsqti_xmlv1p2/imscc_xmlv1p0') !== false){
+       if (!empty($items[$item_id]['tests']) || (isset($items[$item_id]['type']) && strpos($items[$item_id]['type'], 'imsqti_xmlv1p2/imscc_xmlv1p0') !== false)){
                $qti_import = new QTIImport($import_path);
                if (isset($items[$item_id]['tests'])){
                        $loop_var = $items[$item_id]['tests'];
@@ -1388,7 +1391,7 @@ foreach ($items as $item_id => $content_info)
                        }
 
                }
-       } elseif ($items[$item_id]['type']=='imsdt_xmlv1p0'){
+       } elseif (isset($items[$item_id]['type']) && $items[$item_id]['type']=='imsdt_xmlv1p0'){
                //optimize this, repeated codes as above
                $dt_parser = new DiscussionToolsParser();
                $dt_import = new DiscussionToolsImport();