[LON-CAPA-cvs] cvs: loncom /homework randomlabel.pm

raeburn raeburn at source.lon-capa.org
Sun Jan 26 11:29:03 EST 2025


raeburn		Sun Jan 26 16:29:03 2025 EDT

  Modified files:              
    /loncom/homework	randomlabel.pm 
  Log:
  - WCAG 2 compliance
    Add alt attribute to <img> tag for background image in randomlabel.
  
  
Index: loncom/homework/randomlabel.pm
diff -u loncom/homework/randomlabel.pm:1.97 loncom/homework/randomlabel.pm:1.98
--- loncom/homework/randomlabel.pm:1.97	Fri Jul  1 19:59:15 2016
+++ loncom/homework/randomlabel.pm	Sun Jan 26 16:29:03 2025
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # random labelling tool
 #
-# $Id: randomlabel.pm,v 1.97 2016/07/01 19:59:15 raeburn Exp $
+# $Id: randomlabel.pm,v 1.98 2025/01/26 16:29:03 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -110,6 +110,8 @@
 use Apache::Constants qw(:common :http);
 use Image::Magick;
 use Apache::lonplot;
+use Apache::lonlocal;
+use Apache::lonmeta;
 use LONCAPA;
  
 
@@ -162,13 +164,14 @@
 
 }
 
-my ($height_param,$width_param);
+my ($height_param,$width_param,$alt_param);
 sub start_randomlabel {
 
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result='';
     push (@Apache::lonxml::namespace,'randomlabel');
     ($height_param,$width_param)=(0,0);
+    $alt_param = '';
     $label_xscale = 1.0;		# Assume image size not overridden.
     $label_yscale = 1.0;
     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
@@ -185,6 +188,13 @@
 	$args{"cgi.$cgi_id.BGIMG"}=&escape($bgimg);
 	$height_param = &Apache::lonxml::get_param('height',$parstack, $safeeval);
 	$width_param  = &Apache::lonxml::get_param('width', $parstack, $safeeval);
+        $alt_param    = &Apache::lonxml::get_param('alt',   $parstack, $safeeval);
+        if ($alt_param eq '') {
+            unless (($bgimg =~ m{^data\:image/gif;base64,}) || ($bgimg =~ /^https?\:/) ||
+                    ($bgimg eq $Apache::lonnet::perlvar{'lonDocRoot'}.'/adm/lonKaputt/lonlogo_broken.gif')) {
+                $alt_param = &Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$bgimg);
+            }
+        }
     } elsif ($target eq 'tex' && defined($bgimg)) {
 	$result.=&make_eps_image($bgimg,$parstack,$safeeval);
     } elsif ($target eq 'edit') {
@@ -196,6 +206,7 @@
 	$result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' '.
 	         &Apache::edit::browse_or_search('bgimg',undef,undef,$only,undef,1).
 	         '<br />'.
+            &Apache::edit::text_arg('Description:'  ,'alt'     ,$token,40).'<br />'.
 	    &Apache::edit::text_arg('Width(pixel):' ,'width'   ,$token,6).
 	    &Apache::edit::text_arg('Height(pixel):','height'  ,$token,6).
 	    &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
@@ -203,7 +214,7 @@
     } elsif ($target eq 'modified') {
 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						     $safeeval,'bgimg','width',
-						     'height','texwidth');
+						     'height','texwidth','alt');
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
 	}
@@ -217,9 +228,16 @@
     my $count;
     pop @Apache::lonxml::namespace;
     if ($target eq 'web') {
+        my $alttext;
+        if ($alt_param eq '') {
+            $alttext = &mt('labeled image');
+        } else {
+            $alttext = $alt_param;
+        }
+        $alttext = &HTML::Entities::encode($alttext,'<>&"');
 	$count = $Apache::randomlabel::obj_cnt;
 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
-	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
+	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" alt="'.$alttext.'" /><br />'."\n";
 	&Apache::lonnet::appenv(\%args);
     } elsif ($target eq 'tex') {
 	$result='\end{picture}\\\\';




More information about the LON-CAPA-cvs mailing list