[LON-CAPA-cvs] cvs: loncom(version_2_8_X) /interface londocs.pm

raeburn raeburn@source.lon-capa.org
Sun, 21 Dec 2008 15:47:51 -0000


raeburn		Sun Dec 21 15:47:51 2008 EDT

  Modified files:              (Branch: version_2_8_X)
    /loncom/interface	londocs.pm 
  Log:
  - Backport 1.322.
  
  
Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.314.2.1 loncom/interface/londocs.pm:1.314.2.2
--- loncom/interface/londocs.pm:1.314.2.1	Sat Dec 13 23:32:01 2008
+++ loncom/interface/londocs.pm	Sun Dec 21 15:47:50 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.314.2.1 2008/12/13 23:32:01 raeburn Exp $
+# $Id: londocs.pm,v 1.314.2.2 2008/12/21 15:47:50 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1280,8 +1280,32 @@
     if (($url=~/\.(page|sequence)$/) && ($url=~/^\/uploaded\//)) {
 	$title=&mt('Copy of').' '.$title;
 	my $newid=$$.time;
-	$url=~/^(.+)\.(\w+)$/;
-	my $newurl=$1.$newid.'.'.$2;
+	my ($oldid,$ext) = ($url=~/^(.+)\.(\w+)$/);
+        if ($oldid =~ m{^(/uploaded/\Q$coursedom\E/\Q$coursenum\E/)(\D+)(\d+)$}) {
+            my $path = $1;
+            my $prefix = $2;
+            my $ancestor = $3;
+            if (length($ancestor) > 10) {
+                $ancestor = substr($ancestor,-10,10);
+            }
+            $oldid = $path.$prefix.$ancestor;
+        }
+        my $counter = 0;
+        my $newurl=$oldid.$newid.'.'.$ext;
+        my $is_unique = &uniqueness_check($newurl);
+        while (!$is_unique && $counter < 100) {
+            $counter ++;
+            $newid ++;
+            $newurl = $oldid.$newid;
+            $is_unique = &uniqueness_check($newurl);
+        }
+        if (!$is_unique) {
+            if ($url=~/\.page$/) {
+                return &mt('Paste failed: an error occurred creating a unique URL for the composite page');
+            } else {
+                return &mt('Paste failed: an error occurred creating a unique URL for the folder');
+            }
+        }
 	my $storefn=$newurl;
 	$storefn=~s{^/\w+/$match_domain/$match_username/}{};
 	&Apache::lonclonecourse::writefile($env{'request.course.id'},$storefn,
@@ -1330,6 +1354,20 @@
 # Store the result
 }
 
+sub uniqueness_check {
+    my ($newurl) = @_;
+    my $unique = 1;
+    foreach my $res (@LONCAPA::map::order) {
+        my ($name,$url)=split(/\:/,$LONCAPA::map::resources[$res]);
+        $url=&LONCAPA::map::qtescape($url);
+        if ($newurl eq $url) {
+            $unique = 0;
+            last;    
+        }
+    }
+    return $unique;
+}
+
 my %parameter_type = ( 'randompick'     => 'int_pos',
 		       'hiddenresource' => 'string_yesno',
 		       'encrypturl'     => 'string_yesno',