[LON-CAPA-cvs] cvs: loncom /lonnet/perl lonnet.pm

www www@source.lon-capa.org
Fri, 04 Jun 2010 00:14:15 -0000


www		Fri Jun  4 00:14:15 2010 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  Rebuilding the partorder metadata-field when importing parts.
  
  
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1069 loncom/lonnet/perl/lonnet.pm:1.1070
--- loncom/lonnet/perl/lonnet.pm:1.1069	Thu Jun  3 17:04:41 2010
+++ loncom/lonnet/perl/lonnet.pm	Fri Jun  4 00:14:15 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1069 2010/06/03 17:04:41 www Exp $
+# $Id: lonnet.pm,v 1.1070 2010/06/04 00:14:15 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -8131,6 +8131,7 @@
 # ---------------------------------------------------------------- Get metadata
 
 my %metaentry;
+my %importedpartids;
 sub metadata {
     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
     $uri=&declutter($uri);
@@ -8158,7 +8159,8 @@
     }
     {
 # Imported parts would go here
-        my @newpartorder=();
+        my %importedids=();
+        my @origfileimportpartids=();
         my $importedparts=0;
 #
 # Is this a recursive call for a library?
@@ -8264,24 +8266,24 @@
 # We need to get the original file and the imported file to get the part order correct
 # Good news: we do not need to worry about nested libraries, since parts cannot be nested
 # Load and inspect original file
-                           my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
-                           my $origfile=&getfile($origfilelocation);
-                           my @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
+                           if ($#origfileimportpartids<0) {
+                              undef(%importedpartids);
+                              my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
+                              my $origfile=&getfile($origfilelocation);
+                              @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
+                           }
+
 # Load and inspect imported file
                            my $impfile=&getfile($location);
                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
-
-#&logthis("Found imported parts".join(',',@impfilepartids));
-#&logthis("Found original parts and imports".join(',',@origfileimportpartids));
                            if ($#impfilepartids>=0) {
 # This problem had parts
-#&logthis("Importing parted problem");
+                               $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
                            } else {
 # Importing by turning a single problem into a problem part
 # It gets the import-tags ID as part-ID
-#&logthis("Importing unparted problem");
                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
-                               push(@newpartorder,$token->[2]->{'id'});
+                               $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
                            }
                         } else {
 # Normal import
@@ -8291,8 +8293,6 @@
                            }
                         }
 
-#&logthis("About to use unikey $unikey");
-
 			if ($depthcount<20) {
 			    my $metadata = 
 				&metadata($uri,'keys', $location,$unikey,
@@ -8302,7 +8302,6 @@
 				$metathesekeys{$meta}=1;
 			    }
 			
-#&logthis("Metadata $metadata");
                         }
 		    } else {
 #
@@ -8385,6 +8384,22 @@
 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
 	$metaentry{':packages'} = join(',',@uniq_packages);
 
+        if ($importedparts) {
+# We had imported parts and need to rebuild partorder
+           $metaentry{':partorder'}='';
+           $metathesekeys{'partorder'}=1;
+           for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
+               if ($origfileimportpartids[$index] eq 'part') {
+# original part, part of the problem
+                  $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
+               } else {
+# we have imported parts at this position
+                  $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
+               }
+           }
+           $metaentry{':partorder'}=~s/^\,//;
+        }
+
 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);