[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm londocs.pm

raeburn raeburn at source.lon-capa.org
Thu Jul 5 17:33:39 EDT 2012


raeburn		Thu Jul  5 21:33:39 2012 EDT

  Modified files:              
    /loncom/interface	londocs.pm loncommon.pm 
  Log:
  - Pasting from paste buffer.
    - When pasting content in /uploaded from a different course to current course, require 'mdc' priv in original course.
    - Parse file for dependencies in HTML file being pasted from docs/supplemental to
      different type, or different course, and copy any dependencies which
      exist in the source.
  - Fix active tab when viewing History for Supplemental Content.   
  
  
-------------- next part --------------
Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.488 loncom/interface/londocs.pm:1.489
--- loncom/interface/londocs.pm:1.488	Sat Jun 30 23:11:11 2012
+++ loncom/interface/londocs.pm	Thu Jul  5 21:33:39 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.488 2012/06/30 23:11:11 raeburn Exp $
+# $Id: londocs.pm,v 1.489 2012/07/05 21:33:39 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -438,7 +438,7 @@
              '</script>'."\n";
     $r->print(&Apache::loncommon::start_page('Content Change Log',$js));
     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Content Change Log'));
-    $r->print(&startContentScreen('docs'));
+    $r->print(&startContentScreen(($supplementalflag?'suppdocs':'docs')));
     my %orderhash;
     my $container='sequence';
     my $pathitem;
@@ -457,6 +457,9 @@
     my $jumpto = $readfile;
     $jumpto =~ s{^/}{};
     my $tid = 1;
+    if ($supplementalflag) {
+        $tid = 2;
+    }
     my ($breadcrumbtrail) = &breadcrumbs($allowed,$crstype);
     $r->print($breadcrumbtrail.
               &generate_edit_table($tid,\%orderhash,undef,$iconpath,$jumpto,
@@ -729,7 +732,9 @@
     my $url=&LONCAPA::map::qtescape($env{'docs.markedcopy_url'});
     my $title=&LONCAPA::map::qtescape($env{'docs.markedcopy_title'});
 # Maps need to be copied first
-    my ($oldurl,%removefrommap,%addedmaps,%rewrites,%copies,%dbcopies,%zombies,%params);
+    my ($oldurl,%removefrommap,%addedmaps,%rewrites,%copies,%dbcopies,%zombies,%params,
+        %moves,$srcdom,$srcnum);
+    $oldurl = $url;
     if ($url=~/\.(page|sequence)$/) {
         # If pasting a map, check if map contains other maps
         &contained_map_check($url,$folder,\%removefrommap,\%addedmaps);
@@ -770,10 +775,12 @@
             }
             if ($is_unique) {
                 if ($path =~ m{^/uploaded/($match_domain)/($match_courseid)/$}) {
-                    my $srcdom = $1;
-                    my $srcnum = $2;
-                    if (($srcdom ne $coursedom) && ($srcnum ne $coursenum)) {
-                        if (&Apache::lonnet::allowed('mdc',$srcdom.'_'.$srcnum)) {
+                    $srcdom = $1;
+                    $srcnum = $2;
+                    if (($1 ne $coursedom) && ($2 ne $coursenum)) {
+                        my $srcdom = $1;
+                        my $srcnum = $2;
+                        if ($env{"user.priv.cm./$srcdom/$srcnum"} =~ /\Q:mdc&F\E/) {
                             &url_paste_fixups($oldid,$ext,$coursedom,$coursenum,
                                               \%allmaps, \%rewrites,\%copies,\%dbcopies,\%zombies,\%params);
                         } else {
@@ -808,6 +815,14 @@
                 return &mt('Paste failed: only one instance of a particular published sequence or page is allowed within each course.');
             }
         }
+    } elsif ($url =~ m{^/uploaded/($match_domain)/($match_courseid)/}) {
+        if (($1 ne $coursedom) || ($2 ne $coursenum)) {
+            $srcdom = $1;
+            $srcnum = $2;
+            unless ($env{"user.priv.cm./$srcdom/$srcnum"} =~ /\Q:mdc&F\E/) {
+                return &mt('Paste failed: Item is from a different course which you do not have rights to edit');
+            }
+        }
     }
     if ($url=~ m{/smppg$}) {
 	my $db_name = &Apache::lonsimplepage::get_db_name($url);
@@ -822,20 +837,22 @@
 	    $title=&mt('Copy of').' '.$title;
 	}
     }
+    my ($relpath,$oldprefix,$prefixchg);
+    if ($url =~ m{^/uploaded/$match_domain/$match_courseid/(docs|supplemental)/(.+)$}) {
+        $oldprefix = $1;
+        $relpath = $2;
+        if (($folder =~ /^supplemental/) && ($oldprefix eq 'docs')) { 
+            $prefixchg = 1;
+        } elsif (($folder =~ /^default/) && ($oldprefix eq 'supplemental')) {
+            $prefixchg = 1;
+        }
+    }
     $title = &LONCAPA::map::qtunescape($title);
     my $ext='false';
     if ($url=~m{^http(|s)://}) { $ext='true'; }
     $url       = &LONCAPA::map::qtunescape($url);
 # Now insert the URL at the bottom
     my $newidx = &LONCAPA::map::getresidx($url);
-    my $relpath;
-    if (($folder =~ /^supplemental/) && 
-        ($url =~ m{^/uploaded/$coursedom/$coursenum/docs/(.+)})) {
-        $relpath = $1;
-    } elsif (($folder =~ /^default/) &&
-             ($url =~ m{^/uploaded/$coursedom/$coursenum/supplemental/(.+)})) {
-        $relpath = $1;
-    }
     if ($relpath ne '') {
         my ($prefix,$subdir,$rem) = ($relpath =~ m{^(default|\d+)/(\d+)/(.+)$});
         my ($newloc,$newsubdir) = ($folder =~ /^(default|supplemental)_?(\d*)/);
@@ -846,19 +863,26 @@
         if ($newsubdir eq '') {
             $newsubdir = 'default';
         }
-        my $newpath = "$newprefix/$newsubdir/$newidx/$rem";
-        $url =
-            &Apache::lonclonecourse::writefile($env{'request.course.id'},$newpath,
-                                               &Apache::lonnet::getfile($url));
-        if ($url eq '/adm/notfound.html') {
-            return &mt('Paste failed: an error occurred saving the file.');
+        if (($prefixchg) || ($srcdom ne '') && ($srcnum ne '')) {
+            my $newpath = "$newprefix/$newsubdir/$newidx/$rem";
+            $url =
+                &Apache::lonclonecourse::writefile($env{'request.course.id'},$newpath,
+                                                   &Apache::lonnet::getfile($oldurl));
+            if ($url eq '/adm/notfound.html') {
+                return &mt('Paste failed: an error occurred saving the file.');
+            } else {
+                my ($newsubpath) = ($newpath =~ m{^(.*/)[^/]*$});
+                $newsubpath =~ s{/+$}{/};
+                $moves{$oldurl} = $newsubpath;
+            }
         }
     }
     my $noparams = 0;
     if ((ref($params{$oldurl}) eq 'HASH') && ($relpath ne '') && ($folder =~ /^supplemental/)) {
         $noparams = 1;
     }
-    &apply_fixups($coursedom,$coursenum,$oldurl,$url,$noparams,\%rewrites,\%copies,\%dbcopies,\%zombies,\%params);
+    &apply_fixups($coursedom,$coursenum,$oldurl,$url,$noparams,\%rewrites,\%copies,
+                  \%dbcopies,\%zombies,\%params,\%moves);
     if ($env{'docs.markedcopy_supplemental'}) {
         if ($folder =~ /^supplemental/) {
             $title = $env{'docs.markedcopy_supplemental'};
@@ -978,6 +1002,7 @@
                         $rewrites->{$oldurl}{$ressrc} = $id;
                         &url_paste_fixups($ressrc,$3,$cdom,$cnum,$allmaps,$rewrites,$copies,$dbcopies,$zombies,$params);
                     } else {
+                        $rewrites->{$oldurl}{$ressrc} = $id;
                         $copies->{$oldurl}{$ressrc} = $id;
                     }
                 }
@@ -1011,38 +1036,68 @@
 }
 
 sub apply_fixups {
-    my ($cdom,$cnum,$oldurl,$url,$noparams,$rewrites,$copies,$dbcopies,$zombies,$params) = @_;
+    my ($cdom,$cnum,$oldurl,$url,$noparams,$rewrites,$copies,$dbcopies,$zombies,$params,
+        $moves) = @_;
     my (%newdb,%newdoc);
     if (ref($dbcopies->{$oldurl}) eq 'HASH') {
         foreach my $item (keys(%{$dbcopies->{$oldurl}})) {
             $newdb{$item} = &dbcopy($item);
         }
     }
+    my @allcopies;
     if (ref($copies->{$oldurl}) eq 'HASH') {
-        foreach my $item (keys(%{$copies->{$oldurl}})) {
+        push(@allcopies,keys(%{$copies->{$oldurl}}));
+    }
+    if ((ref($moves) eq 'HASH') && (exists($moves->{$oldurl}))) {
+        push(@allcopies,$oldurl);
+    }
+    if (@allcopies > 0) {
+        foreach my $item (@allcopies) {
             my $content = &Apache::lonnet::getfile($item);
-            my $newcontent;
             unless ($content eq '-1') {
                 my $mm = new File::MMagic;
                 my $mimetype = $mm->checktype_contents($content);
                 if ($mimetype eq 'text/html') {
                     my (%allfiles,%codebase,$state);
-                    my ($embedded,$num,$delnum) =
-                        &Apache::loncommon::ask_for_embedded_content(
-                            '/adm/dependencies',$state,\%allfiles,\%codebase,
-                            {'error_on_invalid_names'   => 1,
-                             'ignore_remote_references' => 1,
-                             'docs_url'                 => $oldurl});
-                    if ($embedded) {
-                        #FIXME Need to check for dependencies and copy and update refs.
+                    if (&Apache::lonnet::extract_embedded_items(undef,\%allfiles,\%codebase,\$content) eq 'ok') {
+                        my ($numexisting,$numpathchanges,$existing);
+                        (undef,$numexisting,$numpathchanges,$existing) = 
+                            &Apache::loncommon::ask_for_embedded_content(
+                                '/adm/coursedocs',$state,\%allfiles,\%codebase,
+                                {'error_on_invalid_names'   => 1,
+                                 'ignore_remote_references' => 1,
+                                 'docs_url'                 => $oldurl,
+                                 'context'                  => 'paste'});
+                        if ($numexisting > 0) {
+                            if (ref($existing) eq 'HASH') {
+                                my ($relpath) = ($item =~ m{^(/uploaded/$match_domain/$match_courseid/(?:docs|supplemental)/(?:default|\d+)/.*/)[^/]+$});
+                                foreach my $dep (keys(%{$existing})) {
+                                    $dep =~ s{^\Q$relpath\E}{};
+                                    my $depfile = $relpath.$dep;
+                                    my $depstorefn;
+                                    if ((ref($copies->{$oldurl}) eq 'HASH') &&
+                                        ($copies->{$oldurl}{$item})) {
+                                        $depstorefn = $relpath;
+                                        $depstorefn =~s{^/\w+/$match_domain/$match_courseid/}{};
+                                    } elsif ((ref($moves) eq 'HASH') &&
+                                             (exists($moves->{$oldurl}))) {
+                                        $depstorefn = $moves->{$oldurl};
+                                    }
+                                    $depstorefn .= $dep;
+                                    my $depcontent = &Apache::lonnet::getfile($depfile);
+                                    unless ($depcontent eq '-1') {
+                                        &Apache::lonclonecourse::writefile($env{'request.course.id'},$depstorefn,$depcontent);
+                                    }
+                                }
+                            }
+                        }
                     }
-                    $newcontent = $content;
-                } else {
-                    $newcontent = $content;
                 }
                 my $storefn=$item;
-                $storefn=~s{^/\w+/$match_domain/$match_courseid/}{};
-                $newdoc{$item} = &Apache::lonclonecourse::writefile($env{'request.course.id'},$storefn,$newcontent); 
+                unless (exists($moves->{$oldurl})) {
+                    $storefn=~s{^/\w+/$match_domain/$match_courseid/}{};
+                    $newdoc{$item} = &Apache::lonclonecourse::writefile($env{'request.course.id'},$storefn,$content);
+                }
             }
         }
     }
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1083 loncom/interface/loncommon.pm:1.1084
--- loncom/interface/loncommon.pm:1.1083	Sat Jun 30 23:02:20 2012
+++ loncom/interface/loncommon.pm	Thu Jul  5 21:33:39 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1083 2012/06/30 23:02:20 raeburn Exp $
+# $Id: loncommon.pm,v 1.1084 2012/07/05 21:33:39 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -9358,8 +9358,15 @@
         if (ref($args) eq 'HASH') {
             $url = $args->{'docs_url'};
             $toplevel = $url;
+            if ($args->{'context'} eq 'paste') {
+                ($cdom,$cnum) = ($url =~ m{^\Q/uploaded/\E($match_domain)/($match_courseid)/});
+                ($path) = 
+                    ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
+                $fileloc = &Apache::lonnet::filelocation('',$toplevel);
+                $fileloc =~ s{^/}{};
+            }
         }
-    } elsif ($actionurl eq '/adm/dependencies') {
+    } elsif ($actionurl eq '/adm/dependencies')  {
         if ($env{'request.course.id'} ne '') {
             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
             $cnum =  $env{'course.'.$env{'request.course.id'}.'.num'};
@@ -9429,7 +9436,9 @@
                 my @subdir_list = grep(!/^\./,readdir($dir));
                 map {$currsubfile{$path}{$_} = 1;} @subdir_list;
             }
-        } elsif ($actionurl eq '/adm/dependencies') {
+        } elsif (($actionurl eq '/adm/dependencies') ||
+                 (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
+                  ($args->{'context'} eq 'paste'))) {
             if ($env{'request.course.id'} ne '') {
                 my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
                 if ($dir ne '') {
@@ -9487,7 +9496,9 @@
             my @dir_list = grep(!/^\./,readdir($dir));
             map {$currfile{$_} = 1;} @dir_list;
         }
-    } elsif ($actionurl eq '/adm/dependencies') {
+    } elsif (($actionurl eq '/adm/dependencies') ||
+             (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
+              ($args->{'context'} eq 'paste'))) {
         if ($env{'request.course.id'} ne '') {
             my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
             if ($dir ne '') {
@@ -9524,6 +9535,12 @@
             $unused{$file} = 1;
         }
     }
+    if (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
+        ($args->{'context'} eq 'paste')) {
+        $counter = scalar(keys(%existing));
+        $numpathchg = scalar(keys(%pathchanges));
+        return ($output,$counter,$numpathchg,\%existing); 
+    }
     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
         if ($actionurl eq '/adm/dependencies') {
             next if ($embed_file =~ m{^\w+://});
@@ -9540,7 +9557,6 @@
             $numremref++;
         } elsif ($args->{'error_on_invalid_names'}
             && $embed_file ne &Apache::lonnet::clean_filename($embed_file,{'keep_path' => 1,})) {
-
             $upload_output.='<span class="LC_warning">'.&mt('Invalid characters').'</span>';
             $numinvalid++;
         } else {


More information about the LON-CAPA-cvs mailing list