[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface loncommon.pm

raeburn raeburn at source.lon-capa.org
Fri May 10 19:18:43 EDT 2013


raeburn		Fri May 10 23:18:43 2013 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	loncommon.pm 
  Log:
  - For 2.11
    - Backport 1.1123, 1.1125, 1.1126, 1.1127, 1.1128
  
  
-------------- next part --------------
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1075.2.34 loncom/interface/loncommon.pm:1.1075.2.35
--- loncom/interface/loncommon.pm:1.1075.2.34	Tue Apr 30 13:25:45 2013
+++ loncom/interface/loncommon.pm	Fri May 10 23:18:42 2013
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1075.2.34 2013/04/30 13:25:45 raeburn Exp $
+# $Id: loncommon.pm,v 1.1075.2.35 2013/05/10 23:18:42 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -9532,18 +9532,23 @@
     my $heading = &mt('Upload embedded files');
     my $buttontext = &mt('Upload');
 
-    my $navmap;
+    my ($navmap,$cdom,$cnum);
     if ($env{'request.course.id'}) {
-        $navmap = Apache::lonnavmaps::navmap->new();
+        if ($actionurl eq '/adm/dependencies') {
+            $navmap = Apache::lonnavmaps::navmap->new();
+        }
+        $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+        $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
     }
-    if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
+    if (($actionurl eq '/adm/portfolio') ||
+        ($actionurl eq '/adm/coursegrp_portfolio')) {
         my $current_path='/';
         if ($env{'form.currentpath'}) {
             $current_path = $env{'form.currentpath'};
         }
         if ($actionurl eq '/adm/coursegrp_portfolio') {
-            $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
-            $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
+            $udom = $cdom;
+            $uname = $cnum;
             $url = '/userfiles/groups/'.$env{'form.group'}.'/portfolio';
         } else {
             $udom = $env{'user.domain'};
@@ -9575,24 +9580,43 @@
         }
     } 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'};
             if (ref($args) eq 'HASH') {
                 $url = $args->{'docs_url'};
                 $title = $args->{'docs_title'};
-                $toplevel = "/$url";
+                $toplevel = $url;
+                unless ($toplevel =~ m{^/}) {
+                    $toplevel = "/$url";
+                }
                 ($rem) = ($toplevel =~ m{^(.+/)[^/]+$});
-                ($path) =  
-                    ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
+                if ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E)}) {
+                    $path = $1;
+                } else {
+                    ($path) =
+                        ($toplevel =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/(?:default|\d+)/\d+)/});
+                }
                 $fileloc = &Apache::lonnet::filelocation('',$toplevel);
                 $fileloc =~ s{^/}{};
                 ($filename) = ($fileloc =~ m{.+/([^/]+)$});
                 $heading = &mt('Status of dependencies in [_1]',"$title ($filename)");
             }
         }
+    } elsif ($actionurl eq "/public/$cdom/$cnum/syllabus") {
+        $udom = $cdom;
+        $uname = $cnum;
+        $url = "/uploaded/$cdom/$cnum/portfolio/syllabus";
+        $toplevel = $url;
+        $path = $url;
+        $fileloc = &Apache::lonnet::filelocation('',$toplevel).'/';
+        $fileloc =~ s{^/}{};
+    }
     }
-    my $now = time();
-    foreach my $embed_file (keys(%{$allfiles})) {
+    foreach my $file (keys(%{$allfiles})) {
+        my $embed_file;
+        if (($path eq "/uploaded/$cdom/$cnum/portfolio/syllabus") && ($file =~ m{^\Q$path/\E(.+)$})) {
+            $embed_file = $1;
+        } else {
+            $embed_file = $file;
+        }
         my $absolutepath;
         if ($embed_file =~ m{^\w+://}) {
             $newfiles{$embed_file} = 1;
@@ -9630,7 +9654,8 @@
     my $dirptr = 16384;
     foreach my $path (keys(%subdependencies)) {
         $currsubfile{$path} = {};
-        if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) { 
+        if (($actionurl eq '/adm/portfolio') ||
+            ($actionurl eq '/adm/coursegrp_portfolio')) { 
             my ($sublistref,$listerror) =
                 &Apache::lonnet::dirlist($url.$path,$udom,$uname,$getpropath);
             if (ref($sublistref) eq 'ARRAY') {
@@ -9646,9 +9671,15 @@
             }
         } elsif (($actionurl eq '/adm/dependencies') ||
                  (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
-                  ($args->{'context'} eq 'paste'))) {
+                  ($args->{'context'} eq 'paste')) ||
+                 ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
             if ($env{'request.course.id'} ne '') {
-                my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
+                my $dir;
+                if ($actionurl eq "/public/$cdom/$cnum/syllabus") {
+                    $dir = $fileloc;
+                } else {
+                    ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
+                }
                 if ($dir ne '') {
                     my ($sublistref,$listerror) =
                         &Apache::lonnet::dirlist($dir.$path,$cdom,$cnum,$getpropath,undef,'/');
@@ -9696,7 +9727,8 @@
         }
     }
     my %currfile;
-    if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
+    if (($actionurl eq '/adm/portfolio') ||
+        ($actionurl eq '/adm/coursegrp_portfolio')) {
         my ($dirlistref,$listerror) =
             &Apache::lonnet::dirlist($url,$udom,$uname,$getpropath);
         if (ref($dirlistref) eq 'ARRAY') {
@@ -9712,7 +9744,8 @@
         }
     } elsif (($actionurl eq '/adm/dependencies') ||
              (($actionurl eq '/adm/coursedocs') && (ref($args) eq 'HASH') &&
-              ($args->{'context'} eq 'paste'))) {
+              ($args->{'context'} eq 'paste')) ||
+             ($actionurl eq "/public/$cdom/$cnum/syllabus")) {
         if ($env{'request.course.id'} ne '') {
             my ($dir) = ($fileloc =~ m{^(.+/)[^/]+$});
             if ($dir ne '') {
@@ -9747,12 +9780,14 @@
                 ($file eq $filename.'.bak') ||
                 ($dependencies{$file})) {
             if ($actionurl eq '/adm/dependencies') {
-                next if (($rem ne '') &&
-                         (($env{"httpref.$rem".$file} ne '') ||
-                          (ref($navmap) &&
-                          (($navmap->getResourceByUrl($rem.$file) ne '') ||
-                           (($file =~ /^(.*\.s?html?)\.bak$/i) &&
-                            ($navmap->getResourceByUrl($rem.$1)))))));
+                unless ($toplevel =~ m{^\Q/uploaded/$cdom/$cnum/portfolio/syllabus\E}) {
+                    next if (($rem ne '') &&
+                             (($env{"httpref.$rem".$file} ne '') ||
+                              (ref($navmap) &&
+                              (($navmap->getResourceByUrl($rem.$file) ne '') ||
+                               (($file =~ /^(.*\.s?html?)\.bak$/i) &&
+                                ($navmap->getResourceByUrl($rem.$1)))))));
+                }
             }
             $unused{$file} = 1;
         }
@@ -9762,27 +9797,37 @@
         $counter = scalar(keys(%existing));
         $numpathchg = scalar(keys(%pathchanges));
         return ($output,$counter,$numpathchg,\%existing);
+    } elsif (($actionurl eq "/public/$cdom/$cnum/syllabus") &&
+             (ref($args) eq 'HASH') && ($args->{'context'} eq 'rewrites')) {
+        $counter = scalar(keys(%existing));
+        $numpathchg = scalar(keys(%pathchanges));
+        return ($output,$counter,$numpathchg,\%existing,\%mapping);
     }
     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%newfiles)) {
         if ($actionurl eq '/adm/dependencies') {
             next if ($embed_file =~ m{^\w+://});
         }
         $upload_output .= &start_data_table_row().
-                          '<td><img src="'.&icon($embed_file).'" /> '.
+                          '<td valign="top"><img src="'.&icon($embed_file).'" /> '.
                           '<span class="LC_filename">'.$embed_file.'</span>';
         unless ($mapping{$embed_file} eq $embed_file) {
-            $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.&mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
+            $upload_output .= '<br /><span class="LC_info" style="font-size:smaller;">'.
+                              &mt('changed from: [_1]',$mapping{$embed_file}).'</span>';
         }
-        $upload_output .= '</td><td>';
+        $upload_output .= '</td>';
         if ($args->{'ignore_remote_references'} && $embed_file =~ m{^\w+://}) { 
-            $upload_output.='<span class="LC_warning">'.&mt("URL points to other server.").'</span>';
+            $upload_output.='<td align="right">'.
+                            '<span class="LC_info LC_fontsize_medium">'.
+                            &mt("URL points to web address").'</span>';
             $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>';
+            $upload_output.='<td align="right"><span class="LC_warning">'.
+                            &mt('Invalid characters').'</span>';
             $numinvalid++;
         } else {
-            $upload_output .= &embedded_file_element('upload_embedded',$counter,
+            $upload_output .= '<td>'.
+                              &embedded_file_element('upload_embedded',$counter,
                                                      $embed_file,\%mapping,
                                                      $allfiles,$codebase,'upload');
             $counter ++;
@@ -9811,8 +9856,9 @@
             $counter ++;
         } else {
             $upload_output .= &start_data_table_row().
-                              '<td><span class="LC_filename">'.$embed_file.'</span></td>';
-                              '<td><span class="LC_warning">'.&mt('Already exists').'</span></td>'.
+                              '<td valign="top"><img src="'.&icon($embed_file).'" /> '.
+                              '<span class="LC_filename">'.$embed_file.'</span></td>'.
+                              '<td align="right"><span class="LC_info LC_fontsize_medium">'.&mt('Already exists').'</span></td>'.
                               &Apache::loncommon::end_data_table_row()."\n";
         }
     }
@@ -9907,7 +9953,7 @@
         $output = '<b>'.&mt('Referenced files').'</b>:<br />';
         if ($applies > 1) {
             $output .=  
-                &mt('No files need to be uploaded, as one of the following applies to each reference:').'<ul>';
+                &mt('No dependencies need to be uploaded, as one of the following applies to each reference:').'<ul>';
             if ($numremref) {
                 $output .= '<li>'.&mt('reference is to a URL which points to another server').'</li>'."\n";
             }
@@ -9950,7 +9996,7 @@
             $chgcount ++;
         }
     }
-    if ($counter) {
+    if (($counter) || ($numunused)) {
         if ($numpathchg) {
             $output .= '<input type ="hidden" name="number_pathchange_items" value="'.
                        $numpathchg.'" />'."\n";
@@ -9963,13 +10009,13 @@
         } elsif ($actionurl eq '/adm/dependencies') {
             $output .= '<input type="hidden" name="action" value="process_changes" />';
         }
-        $output .=  '<input type ="submit" value="'.$buttontext.'" />'."\n".'</form>'."\n";
+        $output .= '<input type ="submit" value="'.$buttontext.'" />'."\n".'</form>'."\n";
     } elsif ($numpathchg) {
         my %pathchange = ();
         $output .= &modify_html_form('pathchange',$actionurl,$state,\%pathchange,$pathchange_output);
         if (($actionurl eq '/adm/portfolio') || ($actionurl eq '/adm/coursegrp_portfolio')) {
             $output .= '<p>'.&mt('or').'</p>'; 
-        } 
+        }
     }
     return ($output,$counter,$numpathchg);
 }
@@ -10109,17 +10155,19 @@
             next;
         }
         $env{'form.embedded_item_'.$i.'.filename'}=$fname;
+        my $subdir = $path;
+        $subdir =~ s{/+$}{};
         if ($context eq 'portfolio') {
             my $result;
             if ($state eq 'existingfile') {
                 $result=
                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'existingfile',
-                                                    $dirpath.$env{'form.currentpath'}.$path);
+                                                    $dirpath.$env{'form.currentpath'}.$subdir);
             } else {
                 $result=
                     &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
                                                     $dirpath.
-                                                    $env{'form.currentpath'}.$path);
+                                                    $env{'form.currentpath'}.$subdir);
                 if ($result !~ m|^/uploaded/|) {
                     $output .= '<span class="LC_error">'
                                .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
@@ -10131,10 +10179,11 @@
                                $path.$fname.'</span>').'<br />';     
                 }
             }
-        } elsif ($context eq 'coursedoc') {
+        } elsif (($context eq 'coursedoc') || ($context eq 'syllabus')) {
+            my $extendedsubdir = $dirpath.'/'.$subdir;
+            $extendedsubdir =~ s{/+$}{};
             my $result =
-                &Apache::lonnet::userfileupload('embedded_item_'.$i,'coursedoc',
-                                                $dirpath.'/'.$path);
+                &Apache::lonnet::userfileupload('embedded_item_'.$i,$context,$extendedsubdir);
             if ($result !~ m|^/uploaded/|) {
                 $output .= '<span class="LC_error">'
                            .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
@@ -10144,6 +10193,9 @@
             } else {
                 $output .= &mt('Uploaded [_1]','<span class="LC_filename">'.
                            $path.$fname.'</span>').'<br />';
+                if ($context eq 'syllabus') {
+                    &Apache::lonnet::make_public_indefinitely($result);
+                }
             }
         } else {
 # Save the file
@@ -10275,7 +10327,7 @@
 }
 
 sub modify_html_refs {
-    my ($context,$dirpath,$uname,$udom,$dir_root) = @_;
+    my ($context,$dirpath,$uname,$udom,$dir_root,$url) = @_;
     my $container;
     if ($context eq 'portfolio') {
         $container = $env{'form.container'};
@@ -10284,12 +10336,14 @@
     } elsif ($context eq 'manage_dependencies') {
         (undef,undef,$container) = &Apache::lonnet::decode_symb($env{'form.symb'});
         $container = "/$container";
+    } elsif ($context eq 'syllabus') {
+        $container = $url;
     } else {
         $container = $Apache::lonnet::perlvar{'lonDocRoot'}.$env{'form.filename'};
     }
     my (%allfiles,%codebase,$output,$content);
     my @changes = &get_env_multiple('form.namechange');
-    unless (@changes > 0) {
+    unless ((@changes > 0)  || ($context eq 'syllabus')) {
         if (wantarray) {
             return ('',0,0); 
         } else {
@@ -10297,7 +10351,7 @@
         }
     }
     if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
-        ($context eq 'manage_dependencies')) {
+        ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
         unless ($container =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/}) {
             if (wantarray) {
                 return ('',0,0);
@@ -10353,6 +10407,7 @@
                     if ($content =~ m{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
                         my $numchg = ($content =~ s{($attrib_regexp\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
                         $count += $numchg;
+                        $allfiles{$newname} = $allfiles{$ref};
                     }
                     if ($env{'form.embedded_codebase_'.$i} ne '') {
                         $codebase = &unescape($env{'form.embedded_codebase_'.$i});
@@ -10361,10 +10416,11 @@
                     }
                 }
             }
+            my $skiprewrites;
             if ($count || $codebasecount) {
                 my $saveresult;
                 if (($context eq 'portfolio') || ($context eq 'coursedoc') || 
-                    ($context eq 'manage_dependencies')) {
+                    ($context eq 'manage_dependencies') || ($context eq 'syllabus')) {
                     my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
                     if ($url eq $container) {
                         my ($fname) = ($container =~ m{/([^/]+)$});
@@ -10377,6 +10433,11 @@
                                    '<span class="LC_filename">'.
                                    $container.'</span>').'</p>';
                     }
+                    if ($context eq 'syllabus') {
+                        unless ($saveresult eq 'ok') {
+                            $skiprewrites = 1;
+                        }
+                    }
                 } else {
                     if (open(my $fh,">$container")) {
                         print $fh $content;
@@ -10392,6 +10453,47 @@
                     }
                 }
             }
+            if (($context eq 'syllabus') && (!$skiprewrites)) {
+                my ($actionurl,$state);
+                $actionurl = "/public/$udom/$uname/syllabus";
+                my ($ignore,$num,$numpathchanges,$existing,$mapping) =
+                    &ask_for_embedded_content($actionurl,$state,\%allfiles,
+                                              \%codebase,
+                                              {'context' => 'rewrites',
+                                               'ignore_remote_references' => 1,});
+                if (ref($mapping) eq 'HASH') {
+                    my $rewrites = 0;
+                    foreach my $key (keys(%{$mapping})) {
+                        next if ($key =~ m{^https?://});
+                        my $ref = $mapping->{$key};
+                        my $newname = "/uploaded/$udom/$uname/portfolio/syllabus/$key";
+                        my $attrib;
+                        if (ref($allfiles{$mapping->{$key}}) eq 'ARRAY') {
+                            $attrib = join('|',@{$allfiles{$mapping->{$key}}});
+                        }
+                        if ($content =~ m{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}) {
+                            my $numchg = ($content =~ s{($attrib\s*=\s*['"]?)\Q$ref\E(['"]?)}{$1$newname$2}gi);
+                            $rewrites += $numchg;
+                        }
+                    }
+                    if ($rewrites) {
+                        my $saveresult;
+                        my $url = &Apache::lonnet::store_edited_file($container,$content,$udom,$uname,\$saveresult);
+                        if ($url eq $container) {
+                            my ($fname) = ($container =~ m{/([^/]+)$});
+                            $output .= '<p>'.&mt('Rewrote [quant,_1,link] as [quant,_1,absolute link] in [_2].',
+                                            $count,'<span class="LC_filename">'.
+                                            $fname.'</span>').'</p>';
+                        } else {
+                            $output .= '<p class="LC_error">'.
+                                       &mt('Error: could not update links in [_1].',
+                                       '<span class="LC_filename">'.
+                                       $container.'</span>').'</p>';
+
+                        }
+                    }
+                }
+            }
         } else {
             &logthis('Failed to parse '.$container.
                      ' to modify references: '.$parse_result);


More information about the LON-CAPA-cvs mailing list