apply the changes in atutor to fix the issue that the export puts the items into...
[acontent.git] / docs / home / ims / ims_import.php
index 9a66f19..33d3d24 100644 (file)
@@ -372,6 +372,46 @@ function rehash($items){
        return $rehashed_items;
 }
 
+/**
+ * Take out the common path within all $items['new_path'].
+ * This allows import/export repeatedly without duplicating its path
+ * @param   array   contains the breakdown of all resources in the XML
+ */
+function removeCommonPath($items){
+    $common_path; 
+    $quit = false;  //a flag that is set if it's not the first time being run.
+    
+    foreach($items as $index=>$item){
+        if (isset($item['new_path']) && $item['new_path']!=''){
+            $path = $item['new_path'];
+        } else {
+            continue;
+        }
+
+        //hack
+        //check if this is a XML file; if so, skip through, 
+        //cause XML most likely isn't a content resource.
+        $ext = substr($item['href'], (strrpos($item['href'], '.')+1));
+        if($ext=='xml'){
+            continue;
+        }
+        
+        //if common path is empty, assign the first path to it.
+        if ($common_path=='' && $quit==false){
+            $common_path = $path;
+            $quit = true;   //the next time common_path is empty, quit;
+            continue;
+        }
+        //we use '/' here instead of DIRECTORY_SEPARATOR because php would
+        //actually use '\' and return the whole string. 
+        $common_array = explode('/', $common_path);
+        $path_array = explode('/', $path);
+        $intersect_array = array_intersect($common_array, $path_array);
+        $common_path = implode('/', $intersect_array);       
+    }
+    return $common_path;
+}
+
 
 /** 
  * This function will take the test accessment XML and add these to the database.
@@ -1111,6 +1151,7 @@ if ($xml_base_path) {
 $order_offset = $contentDAO->getMaxOrdering($_course_id, 0);
 $lti_offset = array(); //since we don't need lti tools, the ordering needs to be subtracted
 //reorder the items stack
+$common_path = removeCommonPath($items);
 $items = rehash($items);
 //debug($items);exit;
 foreach ($items as $item_id => $content_info) 
@@ -1343,8 +1384,8 @@ foreach ($items as $item_id => $content_info)
                $all_package_base_path = implode('/', $all_package_base_path);
        }
 
-       if ($all_package_base_path != '') {
-               $content_info['new_path'] = $package_base_name . substr($content_info['new_path'], strlen($all_package_base_path));
+       if ($common_path != '') {
+               $content_info['new_path'] = $package_base_name . substr($content_info['new_path'], strlen($common_path));
        } else {
                $content_info['new_path'] = $package_base_name . '/' . $content_info['new_path'];
        }
@@ -1527,6 +1568,7 @@ if (!is_dir($course_dir)) {
 }
 
 // loop through the files outside the package folder, and copy them to its relative path
+/**
 if (is_dir($import_path.'resources')) {
        $handler = opendir($import_path.'resources');
        while ($file = readdir($handler)){
@@ -1537,21 +1579,29 @@ if (is_dir($import_path.'resources')) {
        }
        closedir($handler);
 }
+**/
+//--- harris edit for path thing
+$file = TR_CONTENT_DIR . 'import/'.$_course_id.DIRECTORY_SEPARATOR.$common_path;
+if (is_dir($file)) {
+    rename($file, TR_CONTENT_DIR .$_course_id.'/'.$package_base_name);
+}
+//--- end
 //takes care of the condition where the whole package doesn't have any contents but question banks
 //also is the case of urls
 if(is_array($all_package_base_path)){
        $all_package_base_path = implode('/', $all_package_base_path);
-}
-if(strpos($all_package_base_path, 'http:/')===false){
-       if (@rename($import_path.$all_package_base_path, TR_CONTENT_DIR .$_course_id.'/'.$package_base_name) === false) {
-        if (!$msg->containsErrors()) {
-                       if ($oauth_import) {
-                               echo "error=".urlencode('Cannot move lesson directory into content directory');
-                       } else {
-                               $msg->addError('IMPORT_FAILED');
-                       }
-        }
-    }
+
+       if(strpos($all_package_base_path, 'http:/')===false){
+               if (@rename($import_path.$all_package_base_path, TR_CONTENT_DIR .$_course_id.'/'.$package_base_name) === false) {
+               if (!$msg->containsErrors()) {
+                               if ($oauth_import) {
+                                       echo "error=".urlencode('Cannot move lesson directory into content directory');
+                               } else {
+                                       $msg->addError('IMPORT_FAILED');
+                               }
+               }
+           }
+       }
 }
 //check if there are still resources missing
 foreach($items as $idetails){