[LON-CAPA-cvs] cvs: loncom /publisher testbankimport.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Wed, 24 Sep 2008 19:48:30 -0000


raeburn		Wed Sep 24 15:48:30 2008 EDT

  Modified files:              
    /loncom/publisher	testbankimport.pm 
  Log:
  - Store embedded images in a directory named with current timestamp to avoid overwriting existing files with the same name when importing a testbank RTF file.
  - Set uri used in <img> tag for embedded images in testbank RTF translated to HTML to a relative URL instead of an absolute URL.
  
  
Index: loncom/publisher/testbankimport.pm
diff -u loncom/publisher/testbankimport.pm:1.17 loncom/publisher/testbankimport.pm:1.18
--- loncom/publisher/testbankimport.pm:1.17	Sun Sep 21 21:53:33 2008
+++ loncom/publisher/testbankimport.pm	Wed Sep 24 15:48:29 2008
@@ -1,5 +1,5 @@
 # Handler for parsing text upload problem descriptions into .problems
-# $Id: testbankimport.pm,v 1.17 2008/09/22 01:53:33 raeburn Exp $
+# $Id: testbankimport.pm,v 1.18 2008/09/24 19:48:29 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1187,7 +1187,8 @@
        <input type="hidden" name="filename" value="'.$fn.'" />
        <input type="hidden" name="page" value="'.$page.'" />
        <input type="hidden" name="phase" value="three" />
-       <input type="hidden" name="go" value="'.$go.'" />';
+       <input type="hidden" name="go" value="'.$go.'" />
+       <input type="hidden" name="timestamp" value="'.$env{'form.timestamp'}.'" />';
     if ($page ne '') {
         $output .= '
        <table border="0">
@@ -1327,7 +1328,6 @@
     my $qstn = ${$qstnref}[0];
     my $numfoils = scalar(@{$qstnref}) - 1; 
     my $datestamp = localtime;
-    my $timestamp = time;
     my $numansrs = scalar(@{$answerref});
     my $output = '<problem>
  <startouttext />';
@@ -1610,7 +1610,7 @@
 }
 
 sub parse_datafile {
-    my ($r,$uname,$filename,$pathname,$dirpath,$urlpath,$page_name,$subdir) = @_;
+    my ($r,$uname,$filename,$pathname,$dirpath,$urlpath,$page_name,$subdir,$timestamp) = @_;
     my ($badfile,$res,%allfiles,%codebase);
     my $mm = new File::MMagic;
     my ($text,$header,$css,$js);
@@ -1623,16 +1623,18 @@
                 close(TESTBANK);
             } elsif ($res eq 'application/rtf') {
                 my $html = '';
-#                my $image_uri = '..'.$pathname;
-                my $image_uri = $pathname;
+                my $image_uri = $timestamp;
                 if ($page_name eq 'Target') {
-                    $image_uri = $urlpath;
+                    $image_uri = $urlpath.'/'.$timestamp;
                 }
-                $image_uri =~ s/\/$//;
                 my $image_dir;
                 if ($page_name eq 'Blocks') {
                     $image_dir = $dirpath;
                     $image_dir =~ s/\/$//;
+                    $image_dir .= '/'.$timestamp;
+                    if (!-e $image_dir) {
+                        mkdir($image_dir,0755);
+                    }
                 } else {
                     $image_dir = $r->dir_config('lonDaemons').'/tmp/'.
                                  $env{'user.name'}.'_'.$env{'user.domain'}.
@@ -1651,10 +1653,10 @@
                 $parser->parse();
                 utf8::decode($html);
                 ($text,$header,$css,$js) = 
-                    &parse_htmlcontent($res,$subdir,$html);
+                    &parse_htmlcontent($res,$subdir,$html,undef,$page_name);
             } elsif ($res eq 'text/html') {
                 ($text,$header,$css,$js) = 
-                    &parse_htmlcontent($res,$subdir,undef,$dirpath.$filename);
+                    &parse_htmlcontent($res,$subdir,undef,$dirpath.$filename,$page_name);
             } else {
                 $badfile = 1;
             }
@@ -1664,7 +1666,7 @@
 }
 
 sub parse_htmlcontent {
-    my ($res,$subdir,$html,$fullpath) = @_;
+    my ($res,$subdir,$html,$fullpath,$page_name) = @_;
     my ($p,$fh);
     if ($res eq 'application/rtf') {
         $p = HTML::TokeParser->new( \$html );
@@ -1702,7 +1704,10 @@
                     my $contents = $token->[4];
                     if ($subdir ne '') {
                         if (($token->[1] eq 'img') && ($token->[2]->{'src'} ne '')) {
-                            $contents =~ s/(src=\s*["']?)/$1..\//i;
+                            if (($res eq 'text/html') || 
+                                ($res eq 'application/rtf') && ($page_name ne 'Target')) {
+                                $contents =~ s/(src=\s*["']?)/$1..\//i;
+                            }
                         }
                     }
                     if (($line eq '') && ($current_tag eq 'font')) {
@@ -1836,6 +1841,9 @@
         $current_page = &display_control();
         my @pages = ('Welcome','Blocks','Format','Target','Confirmation');
         $page_name = $pages[$current_page];
+        if ($env{'form.timestamp'} eq '') {
+            $env{'form.timestamp'} = time; 
+        }
         if ($env{'form.newdir'} ne '') {
             if ($env{'form.newdir'} =~ /^\Q$dirpath\E(.+)$/) {
                 $subdir = $1;
@@ -1843,7 +1851,7 @@
         }
         ($res,$badfile,$textref,$header,$css,$js) = 
             &parse_datafile($r,$uname,$filename,$pathname,$dirpath,$urlpath,
-                            $page_name,$subdir);
+                            $page_name,$subdir,$env{'form.timestamp'});
         if ($page_name eq 'Welcome') {
              &jscript_zero($webpath,\$javascript);
         } elsif ($page_name eq 'Blocks') {