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

raeburn raeburn@source.lon-capa.org
Sat, 25 Dec 2010 04:40:16 -0000


This is a MIME encoded message

--raeburn1293252016
Content-Type: text/plain

raeburn		Sat Dec 25 04:40:16 2010 EDT

  Modified files:              (Branch: version_2_10_X)
    /loncom/interface	londocs.pm 
  Log:
  - Backport 1.440.
  
  
--raeburn1293252016
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20101225044016.txt"

Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.434.2.2 loncom/interface/londocs.pm:1.434.2.3
--- loncom/interface/londocs.pm:1.434.2.2	Tue Nov  9 18:19:08 2010
+++ loncom/interface/londocs.pm	Sat Dec 25 04:40:15 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.434.2.2 2010/11/09 18:19:08 raeburn Exp $
+# $Id: londocs.pm,v 1.434.2.3 2010/12/25 04:40:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1682,11 +1682,10 @@
 sub process_file_upload {
     my ($upload_output,$coursenum,$coursedom,$allfiles,$codebase,$uploadcmd) = @_;
 # upload a file, if present
-    my $parseaction;
-   if ($env{'form.parserflag'}) {
+    my ($parseaction,$showupload,$nextphase,$mimetype);
+    if ($env{'form.parserflag'}) {
         $parseaction = 'parse';
     }
-    my $phase_status;
     my $folder=$env{'form.folder'};
     if ($folder eq '') {
         $folder='default';
@@ -1705,7 +1704,8 @@
             $LONCAPA::map::resources[1]='';
         }
         if ($fatal) {
-            return 'failed';
+            $$upload_output = '<p><span class="LC_error">'.&mt('The uploaded file has not been stored as an error occurred reading the contents of the current folder.').'</span></p>';
+            return;
         }
         my $destination = 'docs/';
         if ($folder =~ /^supplemental/) {
@@ -1716,12 +1716,23 @@
         } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
             $destination .=  $2.'/';
         }
-# this is for a course, not a user, so set coursedoc flag
+# this is for a course, not a user, so set context to coursedoc.
         my $newidx=&LONCAPA::map::getresidx();
         $destination .= $newidx;
         my $url=&Apache::lonnet::userfileupload('uploaddoc','coursedoc',$destination,
 						$parseaction,$allfiles,
-						$codebase);
+                                                $codebase,undef,undef,undef,undef,
+                                                undef,undef,\$mimetype);
+        if ($url =~ m{^/uploaded/\Q$coursedom\E/\Q$coursenum\E.*/([^/]+)$}) {
+            my $stored = $1;
+            $showupload = '<p>'.&mt('Uploaded [_1]','<span class="LC_filename">'.
+                          $stored.'</span>').'</p>';
+        } else {
+            my ($filename) = ($env{'form.uploaddoc.filename'} =~ m{([^/]+)$});
+
+            $$upload_output = '<p><span class="LC_error">'.&mt('Unable to save file [_1].','<span class="LC_filename">'.$filename.'</span>').'</span></p>';
+            return;
+        }
         my $ext='false';
         if ($url=~m{^http://}) { $ext='true'; }
 	$url     = &LONCAPA::map::qtunescape($url);
@@ -1738,51 +1749,37 @@
         ($errtext,$fatal)=&storemap($coursenum,$coursedom,
 				    $folder.'.'.$container);
         if ($fatal) {
-            $$upload_output .= '<p><span class="LC_error">'.$errtext.'</span></p>';
-            return 'failed';
+            $$upload_output = '<p><span class="LC_error">'.$errtext.'</span></p>';
+            return;
         } else {
-            if ($parseaction eq 'parse') {
+            if ($parseaction eq 'parse' && $mimetype eq 'text/html') {
+                $$upload_output = $showupload;
                 my $total_embedded = scalar(keys(%{$allfiles}));
                 if ($total_embedded > 0) {
-                    my $num = 0;
-		    my $state = '
-   <input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($env{'form.folderpath'},'<>&"').'" />
-   <input type="hidden" name="cmd" value="upload_embedded" />
-   <input type="hidden" name="newidx" value="'.$newidx.'" />
-   <input type="hidden" name="primaryurl" value="'.&escape($url).'" />
-   <input type="hidden" name="phasetwo" value="'.$total_embedded.'" />';
-		    $phase_status = 'phasetwo';
-
-                    $$upload_output .=
-			'This file contains embedded multimedia objects, which need to be uploaded to LON-CAPA.<br />'.
-			&Apache::loncommon::ask_for_embedded_content(
-                            '/adm/coursedocs',$state,$allfiles,$codebase);
+                    my $uploadphase = 'upload_embedded';
+                    my $primaryurl = &HTML::Entities::encode($url,'<>&"');
+                    my $state = &embedded_form_elems($uploadphase,$primaryurl,$newidx);
+                    my ($embedded,$num) =
+                        &Apache::loncommon::ask_for_embedded_content(
+                            '/adm/coursedocs',$state,$allfiles,$codebase,{'docs_url' => $url});
+                    if ($embedded) {
+                        if ($num) {
+                            $$upload_output .=
+                                 '<p>'.&mt('This file contains embedded multimedia objects, which need to be uploaded.').'</p>'.$embedded;
+                            $nextphase = $uploadphase;
+                        } else {
+                            $$upload_output .= $embedded;
+                        }
+                    } else {
+                        $$upload_output .= &mt('Embedded item(s) already present, so no additional upload(s) required').'<br />';
+                    }
                 } else {
-                    $$upload_output .= 'No embedded items identified<br />';
+                    $$upload_output .= &mt('No embedded items identified').'<br />';
                 }
             }
         }
     }
-    return $phase_status;
-}
-
-sub process_secondary_uploads {
-    my ($upload_output,$coursedom,$coursenum,$formname,$num,$newidx) = @_;
-    my $folder=$env{'form.folder'};
-    my $destination = 'docs/';
-    if ($folder =~ /^supplemental/) {
-        $destination = 'supplemental/';
-    }
-    if (($folder eq 'default') || ($folder eq 'supplemental')) {
-        $destination .= 'default/';
-    } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
-        $destination .=  $2.'/';
-    }
-    $destination .= $newidx;
-    my ($url,$filename);
-    $url=&Apache::lonnet::userfileupload($formname.$num,'coursedoc',$destination);
-    ($filename) = ($url =~ m{^/uploaded/\Q$coursedom\E/\Q$coursenum\E/\Q$destination\E/(.+)$});
-    return $filename;
+    return $nextphase;
 }
 
 sub is_supplemental_title {
@@ -1847,7 +1844,7 @@
     }
     if ($env{'form.pagepath'}) {
         $type = $container = 'page';
-        $esc_path=&escape($path = $env{'form.pagepath'});
+        $esc_path=&escape($env{'form.pagepath'});
 	$path = &HTML::Entities::encode($env{'form.pagepath'},'<>&"');
         $symb=&escape($env{'form.pagesymb'});
     }
@@ -2824,92 +2821,49 @@
 
   my %allfiles = ();
   my %codebase = ();
-  my ($upload_result,$upload_output);
+  my ($upload_result,$upload_output,$uploadphase);
   if ($allowed) {
       if (($env{'form.uploaddoc.filename'}) &&
 	  ($env{'form.cmd'}=~/^upload_(\w+)/)) {
-# Process file upload - phase one - upload and parse primary file.
-	  undef($hadchanges);
-          $upload_result = &process_file_upload(\$upload_output,$coursenum,
-						$coursedom,\%allfiles,
-						\%codebase,$1);
+          my $context = $1;
+          # Process file upload - phase one - upload and parse primary file.
+          undef($hadchanges);
+          $uploadphase = &process_file_upload(\$upload_output,$coursenum,$coursedom,
+                                              \%allfiles,\%codebase,$context);
 	  if ($hadchanges) {
 	      &mark_hash_old();
 	  }
-          if ($upload_result eq 'phasetwo') {
-              $r->print($upload_output);
-          }
-      } elsif ($env{'form.phasetwo'}) {
-          my %newname = ();
-          my %origname = ();
-          my %attribs = ();
-          my $updateflag = 0;
-          my $residx = $env{'form.newidx'};
-          my $primary_url = &unescape($env{'form.primaryurl'});
-# Process file upload - phase two - gather secondary files.
-          for (my $i=0; $i<$env{'form.phasetwo'}; $i++) {
-              if ($env{'form.embedded_item_'.$i.'.filename'}) {
-                  my $javacodebase;
-                  $newname{$i} = &process_secondary_uploads(\$upload_output,$coursedom,$coursenum,'embedded_item_',$i,$residx);
-                  $origname{$i} = &unescape($env{'form.embedded_orig_'.$i});
-                  if (exists($env{'form.embedded_codebase_'.$i})) {
-                      $javacodebase =  &unescape($env{'form.embedded_codebase_'.$i});
-                      $origname{$i} =~ s#^\Q$javacodebase\E/##;
-                  }
-                  my @attributes = ();
-                  if ($env{'form.embedded_attrib_'.$i} =~ /:/) {
-                      @attributes = split(/:/,$env{'form.embedded_attrib_'.$i});
-                  } else {
-                      @attributes = ($env{'form.embedded_attrib_'.$i});
-                  }
-                  foreach my $attr (@attributes) {
-                      push(@{$attribs{$i}},&unescape($attr));
-                  }
-                  if ($javacodebase) {
-                      $codebase{$i} = $javacodebase;
-                      $codebase{$i} =~ s#/$##;
-                      $updateflag = 1;
-                  }
-              }
-              unless ($newname{$i} eq $origname{$i}) {
-                  $updateflag = 1;
-              }
-          }
-# Process file upload - phase three - modify primary file
-          if ($updateflag) {
-              my ($content,$rtncode);
-              my $updateflag = 0;
-              my $getstatus = &Apache::lonnet::getuploaded('GET',$primary_url,$coursedom,$coursenum,\$content,\$rtncode);
-              if ($getstatus eq 'ok') {
-                  foreach my $item (keys(%newname)) {
-                      if ($newname{$item} ne $origname{$item}) {
-                          my $attrib_regexp = '';
-                          if (@{$attribs{$item}} > 1) {
-                              $attrib_regexp = join('|',@{$attribs{$item}});
-                          } else {
-                              $attrib_regexp = $attribs{$item}[0];
-                          }
-                          if ($content =~ m#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#) {
-                          }
-                          $content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#$1$newname{$item}$2#gi;
-                      }
-                      if (exists($codebase{$item})) {
-                          $content =~ s/(codebase\s*=\s*["']?)\Q$codebase{$item}\E(["']?)/$1.$2/i; #' stupid emacs
-                      }
-                  }
-# Save edited file.
-                  my $saveresult;
-                  my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
-                  my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
-                  my $url = &Apache::lonnet::store_edited_file($primary_url,$content,$docudom,$docuname,\$saveresult);
-              } else {
-                  &Apache::lonnet::logthis('retrieval of uploaded file - '.$primary_url.' - for editing, failed: '.$getstatus);
-              }
-          }
+          $r->print($upload_output);
+      } elsif ($env{'form.phase'} eq 'upload_embedded') {
+          # Process file upload - phase two - upload embedded objects
+          $uploadphase = 'check_embedded';
+          my $primaryurl = &HTML::Entities::encode($env{'form.primaryurl'},'<>&"');
+          my $state = &embedded_form_elems($uploadphase,$primaryurl,
+                                           $env{'form.newidx'});
+          my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
+          my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
+          my ($destination,$dir_root) = &embedded_destination();
+          my $url_root = '/uploaded/'.$docudom.'/'.$docuname;
+          my $actionurl = '/adm/coursedocs';
+          my ($result,$flag) =
+              &Apache::loncommon::upload_embedded('coursedoc',$destination,
+                  $docuname,$docudom,$dir_root,$url_root,undef,undef,undef,$state,
+                  $actionurl);
+          $r->print($result.&return_to_editor());
+      } elsif ($env{'form.phase'} eq 'check_embedded') {
+          # Process file upload - phase three - modify references in HTML file
+          $uploadphase = 'modified_orightml';
+          my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
+          my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
+          my ($destination,$dir_root) = &embedded_destination();
+          $r->print(&Apache::loncommon::modify_html_refs('coursedoc',$destination,
+                                                         $docuname,$docudom,undef,
+                                                         $dir_root).
+                   &return_to_editor());
       }
   }
 
-  unless ($showdoc ||  $upload_result eq 'phasetwo') {
+  unless ($showdoc || $uploadphase) {
 # -----------------------------------------------------------------------------
        my %lt=&Apache::lonlocal::texthash(
                 'uplm' => 'Upload a new main '.lc($crstype).' document',
@@ -3467,7 +3421,7 @@
 </form>');
     }
   } else {
-      unless ($upload_result eq 'phasetwo') {
+      unless ($uploadphase) {
 # -------------------------------------------------------- This is showdoc mode
           $r->print("<h1>".&mt('Uploaded Document').' - '.
 		&Apache::lonnet::gettitle($r->uri).'</h1><p>'.
@@ -3480,6 +3434,42 @@
  return OK;
 }
 
+sub embedded_form_elems {
+    my ($phase,$primaryurl,$newidx) = @_;
+    my $folderpath = &HTML::Entities::encode($env{'form.folderpath'},'<>&"');
+    return <<STATE;
+    <input type="hidden" name="folderpath" value="$folderpath" />
+    <input type="hidden" name="cmd" value="upload_embedded" />
+    <input type="hidden" name="newidx" value="$newidx" />
+    <input type="hidden" name="phase" value="$phase" />
+    <input type="hidden" name="primaryurl" value="$primaryurl" />
+STATE
+}
+
+sub embedded_destination {
+    my $folder=$env{'form.folder'};
+    my $destination = 'docs/';
+    if ($folder =~ /^supplemental/) {
+        $destination = 'supplemental/';
+    }
+    if (($folder eq 'default') || ($folder eq 'supplemental')) {
+        $destination .= 'default/';
+    } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
+        $destination .=  $2.'/';
+    }
+    $destination .= $env{'form.newidx'};
+    my $dir_root = '/userfiles';
+    return ($destination,$dir_root);
+}
+
+sub return_to_editor {
+    my $actionurl = '/adm/coursedocs';
+    return '<p><form name="backtoeditor" method="post" action="'.$actionurl.'" />'."\n".
+           '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($env{'form.folderpath'},'<>&"').'" /></form>'."\n".
+           '<a href="javascript:document.backtoeditor.submit();">'.&mt('Return to Editor').
+           '</a></p>';
+}
+
 sub generate_admin_options {
   my ($help_ref,$env_ref) = @_;
   my %lt=&Apache::lonlocal::texthash(

--raeburn1293252016--