[LON-CAPA-cvs] cvs: loncom /imspackages imsprocessor.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Fri, 24 Mar 2006 17:16:07 -0000


This is a MIME encoded message

--raeburn1143220567
Content-Type: text/plain

raeburn		Fri Mar 24 12:16:07 2006 EDT

  Modified files:              
    /loncom/imspackages	imsprocessor.pm 
  Log:
  Use relative paths in src, when constructing <img src="" /> tags in items imported into CTSR so images will load when viewing resources prior to publication of imported items.
  
  
--raeburn1143220567
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20060324121607.txt"

Index: loncom/imspackages/imsprocessor.pm
diff -u loncom/imspackages/imsprocessor.pm:1.32 loncom/imspackages/imsprocessor.pm:1.33
--- loncom/imspackages/imsprocessor.pm:1.32	Thu Oct 27 14:19:36 2005
+++ loncom/imspackages/imsprocessor.pm	Fri Mar 24 12:16:07 2006
@@ -511,10 +511,10 @@
                 }
             } elsif ($$resources{$key}{type} eq "resource/x-bb-staffinfo") {
                 %{$$resinfo{$key}} = ();
-                &process_staff($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
+                &process_staff($key,$docroot,$destdir,\%{$$resinfo{$key}},$resrcfiles);
             } elsif ($$resources{$key}{type} eq "resource/x-bb-externallink") {
                 %{$$resinfo{$key}} = ();
-                &process_link($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
+                &process_link($key,$docroot,$destdir,\%{$$resinfo{$key}},$resrcfiles);
             } elsif ($$resources{$key}{type} eq "resource/x-bb-discussionboard") {
                 %{$$resinfo{$key}} = ();
                 unless ($db_handling eq 'ignore') {
@@ -1205,12 +1205,12 @@
 
 # ---------------------------------------------------------------- Process Blackboard Staff
 sub process_staff {
-  my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
+  my ($res,$docroot,$destdir,$settings,$resrcfiles) = @_;
   my $xmlfile = $docroot.'/'.$res.".dat";
   my $filecount = 0;
   my @state;
   %{$$settings{name}} = ();
-  %{$$settings{office}} = ();  
+  %{$$settings{office}} = ();
 
   my $p = HTML::Parser->new
     (
@@ -1363,7 +1363,7 @@
      |;
      if ( defined($$settings{image}) ) {
          $staffentry .= qq|
-      <img src="$dirname/resfiles/$res/$$settings{image}">
+      <img src="$res/$$settings{image}">
          |;
      }
      $staffentry .= qq|
@@ -1386,7 +1386,7 @@
 
 # ---------------------------------------------------------------- Process Blackboard Links
 sub process_link {
-    my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
+    my ($res,$docroot,$destdir,$settings,$resrcfiles) = @_;
     my $xmlfile = $docroot.'/'.$res.".dat";
     my @state = ();
     my $p = HTML::Parser->new
@@ -2718,13 +2718,19 @@
 sub write_bb5_questions {
     my ($allids,$containerdir,$context,$settings,$dirname,$destdir,$res,$allanswers,$allchoices,$total,$newdir,$cid,$cdom,$cnum,$docroot) = @_;
     my $qnum = 0;
+    my $pathstart;
+    if ($context eq 'CSTR') {
+        $pathstart = '../..';
+    } else {
+        $pathstart = $dirname;
+    }
     foreach my $id (@{$allids}) {
         if ($$settings{$id}{ishtml} eq 'true') {
             $$settings{$id}{text} = &HTML::Entities::decode($$settings{$id}{text});
         }
         if ($$settings{$id}{text} =~ m#<img src=['"]?(https?://[^\s]+/)([^/\s\'"]+)['"]?[^>]*>#) {
             if (&retrieve_image($context,$res,$dirname,$cdom,$cnum,$docroot,$destdir,$1,$2) eq 'ok') {
-                $$settings{$id}{text} =~ s#(<img src=['"]?)(https?://[^\s]+/)([^/\s'"]+)(['"]?[^>]*>)#$1../../resfiles/$res/webimages/$3$4#g;
+                $$settings{$id}{text} =~ s#(<img src=['"]?)(https?://[^\s]+/)([^/\s'"]+)(['"]?[^>]*>)#$1$pathstart/resfiles/$res/webimages/$3$4#g;
             }
         }
         $$settings{$id}{text} =~ s#(<img src=[^>]+)/*>#$1 />#gi;
@@ -2772,9 +2778,9 @@
             my ($image,$imglink,$url);
             if ( defined($$settings{$id}{image}) ) {
                 if ( $$settings{$id}{style} eq 'embed' ) {
-                    $image = qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{image}" /><br />|;
+                    $image = qq|<br /><img src="$pathstart/resfiles/$res/$$settings{$id}{image}" /><br />|;
                 } else {
-                    $imglink = qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{image}">Link to file</a><br />|;
+                    $imglink = qq|<br /><a href="$pathstart/resfiles/$res/$$settings{$id}{image}">Link to file</a><br />|;
                 }
             }
             if ( defined($$settings{$id}{url}) ) {
@@ -2819,9 +2825,9 @@
                     my ($ans_image,$ans_link);
                     if ( defined($$settings{$id}{$$allanswers{$id}[$k]}{image}) ) {
                         if ( $$settings{$id}{$$allanswers{$id}[$k]}{style} eq 'embed' ) {
-                            $ans_image .= qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" /><br />|;
+                            $ans_image .= qq|<br /><img src="$pathstart/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" /><br />|;
                         } else {
-                            $ans_link .= qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" />Link to file</a><br/>|;
+                            $ans_link .= qq|<br /><a href="$pathstart/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" />Link to file</a><br/>|;
                         }
                     }
                     $output .= $ans_image.$ans_link.'<endouttext /></foil>'."\n";
@@ -3642,7 +3648,7 @@
 |;
         }
         $$total{prob} ++;
-        $questiontext .= &add_images_links('question',$settings,$id,$dirname,$res);
+        $questiontext .= &add_images_links('question',$context,$settings,$id,$dirname,$res);
         if ($$settings{$id}{class} eq "Essay") {
             if ($context eq 'CSTR') {
                 $output .= qq|<startouttext />$questiontext<endouttext />
@@ -3680,7 +3686,7 @@
                     my $answer_text = $$settings{$id}{$answer_id}{text};
                     my $texttype = $$settings{$id}{$answer_id}{texttype};
                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
-                    $answer_text .= &add_images_links('response',$settings,$id,$dirname,$res); 
+                    $answer_text .= &add_images_links('response',$context,$settings,$id,$dirname,$res); 
                     $output .= "   <foil name=\"foil".$k."\" value=\"";
                     if (grep/^$answer_id$/,@{$$settings{$id}{correctanswer}}) {
                         $output .= "true\" location=\"";
@@ -3733,7 +3739,7 @@
                     my $answer_text = $$settings{$id}{$answer_id}{text};
                     my $texttype = $$settings{$id}{$answer_id}{texttype};
                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
-                    $answer_text .= &add_images_links('response',$settings,$id,$dirname,$res);
+                    $answer_text .= &add_images_links('response',$context,$settings,$id,$dirname,$res);
 
                     $output .= "   <foil name=\"foil".$k."\" value=\"";
                     if (grep/^$answer_id$/,@{$$settings{$id}{correctanswer}}) {
@@ -3779,7 +3785,7 @@
                     my $answer_text = $$settings{$id}{$answer_id}{text};
                     my $texttype = $$settings{$id}{$answer_id}{texttype};
                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
-                    $answer_text .= &add_images_links('response',$settings,$id,$dirname,$res);
+                    $answer_text .= &add_images_links('response',$context,$settings,$id,$dirname,$res);
                     my $iter = $k+1;
                     if ($context eq 'CSTR') {
                         $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$settings{$id}{$answer_id}{order}."\"><startouttext />".$answer_text."<endouttext /></foil>\n";
@@ -3920,7 +3926,7 @@
                     my $texttype = $$settings{$id}{$choice_id}{texttype};
                     my $choice_plaintext = &remove_html($choice_text);
                     &process_html(\$choice_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
-                    $choice_text .= &add_images_links($choice_id,$settings,$id,$dirname,$res);
+                    $choice_text .= &add_images_links($choice_id,$context,$settings,$id,$dirname,$res);
                     push(@allmatchers,$choice_plaintext);
                     if ($context eq 'CSTR') {
                         $output .= qq|
@@ -3940,7 +3946,7 @@
                     my $answer_text = $$settings{$id}{$answer_id}{text};
                     my $texttype = $$settings{$id}{$answer_id}{texttype};
                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
-                    $answer_text .= &add_images_links($answer_id,$settings,$id,$dirname,$res);
+                    $answer_text .= &add_images_links($answer_id,$context,$settings,$id,$dirname,$res);
                     if ($context eq 'CSTR') {
                         $output .= '
         <foil location="random" value="rightmatch'.$$settings{$id}{$$settings{$id}{$answer_id}{correctanswer}}{order}.'" name="'.$answer_id.'">
@@ -4571,7 +4577,12 @@
 
 sub process_html {
     my ($text,$caller,$html_cond,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir) = @_;
-    
+    my $pathstart;
+    if ($context eq 'CSTR') {
+        $pathstart = '../..';
+    } else {
+        $pathstart = $dirname;
+    }
     if ($caller eq 'bb5') {
         if ($html_cond eq 'true') {
             $$text = &HTML::Entities::decode($$text);
@@ -4583,7 +4594,7 @@
     }
     if ($$text =~ m#<img src=['"]?(https?://[^\s]+/)([^/\s\'"]+)['"]?[^>]*>#) {
         if (&retrieve_image($context,$res,$dirname,$cdom,$cnum,$docroot,$destdir,$1,$2) eq 'ok') {
-            $$text =~ s#(<img src=['"]?)(https?://[^\s]+/)([^/\s'"]+)(['"]?[^>]*>)#$1../../resfiles/$res/webimages/$3$4#g;
+            $$text =~ s#(<img src=['"]?)(https?://[^\s]+/)([^/\s'"]+)(['"]?[^>]*>)#$1$pathstart/resfiles/$res/webimages/$3$4#g;
         }
     }
     $$text =~ s#(<img src=[^>]+)/*>#$1 />#gi;
@@ -4592,13 +4603,18 @@
 }
 
 sub add_images_links {
-    my ($type,$settings,$id,$dirname,$res) = @_;
-    my ($image,$imglink,$url);
+    my ($type,$context,$settings,$id,$dirname,$res) = @_;
+    my ($image,$imglink,$url,$pathstart);
+    if ($context eq 'CSTR') {
+        $pathstart = '../..';
+    } else {
+        $pathstart = $dirname;
+    }
     if ((defined($$settings{$id}{$type}{image})) && ($$settings{$id}{$type}{image} ne '')) {
         if ( $$settings{$id}{$type}{style} eq 'Inline' ) {
-            $image = qq|<br /><img src="../../resfiles/$res/$$settings{$id}{$type}{image}" alt="$$settings{$id}{$type}{label}"/><br />|;
+            $image = qq|<br /><img src="$pathstart/resfiles/$res/$$settings{$id}{$type}{image}" alt="$$settings{$id}{$type}{label}"/><br />|;
         } else {
-            $imglink = qq|<br /><a href="../../resfiles/$res/$$settings{$id}{$type}{image}">$$settings{$id}{$type}{label}</a><br />|;
+            $imglink = qq|<br /><a href="$pathstart/resfiles/$res/$$settings{$id}{$type}{image}">$$settings{$id}{$type}{label}</a><br />|;
         }
     }
     if ((defined($$settings{$id}{$type}{link})) && ($$settings{$id}{$type}{link} ne '' )) {

--raeburn1143220567--