File:  [LON-CAPA] / loncom / homework / outputtags.pm
Revision 1.58: download - view: text, annotated - select for diffs
Tue Aug 9 23:43:42 2016 UTC (7 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_X, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, HEAD
- Eliminate "Unescaped left brace in regex is deprecated," warnings
  in error_log with perl 5.22 (Ubuntu 16 LTS).

    1: # The LearningOnline Network with CAPA 
    2: # tags that create controlled output
    3: #
    4: # $Id: outputtags.pm,v 1.58 2016/08/09 23:43:42 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: 
   31: package Apache::outputtags; 
   32: 
   33: use strict;
   34: use Apache::lonlocal;
   35: use Apache::lonnet;
   36: use POSIX qw(strftime);
   37: 
   38: BEGIN {
   39:     &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
   40: }
   41: 
   42: ################################ utilities ###########################
   43: 
   44: # 
   45: #  Does a simple substitution of a tab when the opening tag can
   46: #  be replaced by a fixed string.. and same for the closing tag.
   47: #  Parameters:
   48: #    $input   - String in in which to do the substitutions.
   49: #    $tag     - name of tag without the <>'s e.g. sub for <sub></sub>
   50: #    $opening - What to replace <$tag> with
   51: #    $closing - What to replace </$tag> with.
   52: #  Returns:
   53: #    Input string appropriately substituted.
   54: #
   55: sub substitute_tag {
   56:     my ($input,
   57: 	$tag,
   58: 	$opening,
   59: 	$closing) = @_;
   60: 
   61:     $input =~ s/<$tag>/$opening/gi;
   62:     $input =~ s/<\/$tag>/$closing/gi;
   63: 
   64:     return $input;
   65: }  
   66: 
   67: #
   68: #  Substitutes the simple formatting tags in a string
   69: #  Parameters:
   70: #    $string - input string.
   71: #  Returns
   72: #    Result of string after simple substitutions
   73: #  Tags we handle are:
   74: #    <sub>, <sup> <em>
   75: #
   76: sub substitute_simple_tags_latex {
   77:     my ($string)  = @_;
   78: 
   79:     # restore the <>'s:
   80: 
   81:     $string =~ s/\\ensuremath\{<}/</g;
   82:     $string =~ s/\\ensuremath\{>}/>/g;
   83: 
   84: 
   85:     # Substitute the tags:
   86: 
   87:     $string = &substitute_tag($string, "sub", '\ensuremath{_', '}');
   88:     $string = &substitute_tag($string, 'sup', '\ensuremath{^', '}');
   89:     $string = &substitute_tag($string, 'em', '\em{', '}');
   90: 
   91: 
   92:     # Escape the remaining <>'s again:
   93: 
   94:     $string =~ s/</\\ensuremath{<}/g;
   95:     $string =~ s/>/\\ensuremath{>}/g;
   96: 
   97: 
   98: 
   99:     return $string;
  100: }
  101: 
  102: ################################ The parser ##########################
  103: 
  104: sub initialize_outputtags {
  105:     %Apache::outputtags::showonce=();
  106: }
  107: 
  108: 
  109: 
  110: sub start_displayduedate {
  111:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  112:     my $result;
  113: 
  114:     # Different parts can have different due dates... so we keep a list
  115:     # of the parts for which we've displayed the duedate:
  116: 
  117:     if (exists($Apache::outputtags::showonce{'displayduedate'})) {
  118: 	if (grep(/^\Q$Apache::inputtags::part\E$/,
  119: 		 @{$Apache::outputtags::showonce{'displayduedate'}})) {
  120: 	    return '';		# Already shown the duedate for this part.
  121: 	}
  122:     }
  123:     # since we will show the duedate for this part, remember it.
  124: 
  125:     push (@{$Apache::outputtags::showonce{'displayduedate'}},
  126: 	  $Apache::inputtags::part);
  127: 
  128:     #  Determine the due date format:
  129:     #
  130:     my $status=$Apache::inputtags::status['-1'];
  131:     &Apache::lonxml::debug("got a $status in duedatebox");
  132:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  133:     my $format = &Apache::lonxml::get_param('format', $parstack, $safeeval);
  134:     if (!$format) {
  135: 	$format = undef;
  136:     }
  137:     if (($status =~ /CAN.*_ANSWER/)) {
  138: 	my $id = $Apache::inputtags::part;
  139: 	my $date = &Apache::lonhomework::due_date($id);
  140: 	&Apache::lonxml::debug("duedatebox found $date for $id");
  141: 
  142: 	# Only show the due date if the current date is 
  143: 	# different from due date of the previous part.  I think
  144: 	# this is probably the best way to avoid due date clutter.
  145: 
  146:        	my $showduedate = 1;
  147: 	my $part_count  = scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
  148: 	if ($part_count > 1) {
  149: 	    my $prev_part_id = $Apache::outputtags::showonce{'displayduedate'}->[$part_count-2];
  150: 	    my $prev_due_date = &Apache::lonnet::EXT("resource.$prev_part_id.duedate");
  151: 	    if ($prev_due_date == $date) {
  152: 		$showduedate = 0;
  153: 	    }
  154: 	}
  155: 
  156: 	if ($showduedate) {
  157: 	    my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
  158: 	    if ($target eq 'web') {
  159: 		if (lc($style) !~ 'plain') { 
  160: 		    $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
  161: 		} else {
  162: 		    $result=&mt('Due').' '.$duetext;
  163: 		}
  164: 	    } elsif ($target eq 'tex') {
  165: 		# For TeX we'll make the duedate tag work exactly like the 
  166: 		# duedate tag for web.
  167: 
  168: 		my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
  169: 		if (lc($style) !~ 'plain') {
  170: 		    # The due date will be put in a box.
  171: 		    # at the start of the line to ensure it won't overlap
  172: 		    # the 1 col boundary.
  173: 
  174: 		    $result = '\vspace{1.0 ex} \framebox{'
  175:     			       .&mt('Due').' '.$duetext.'}';
  176: 		} else {
  177: 		    $result = &mt('Due') . ' '.$duetext;
  178: 		}
  179: 	    }
  180: 	}
  181:      
  182:     } 
  183:     if ( $target eq 'edit' ) {
  184: 	$result=&Apache::edit::tag_start($target,$token);
  185: 	$result.='</td></tr>';
  186: 	$result.=&Apache::edit::end_table();
  187: 	
  188:     }
  189:     return $result;
  190: }
  191: 
  192: sub end_displayduedate {
  193:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  194:     my @result;
  195:     if ($target eq 'edit') { $result[1]='no'; }
  196:     return @result;
  197: }
  198: 
  199: sub start_displaytitle {
  200:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  201:     my $result='';
  202:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
  203: 	return '';
  204:     } else {
  205: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
  206:     }
  207:     my $name=&Apache::structuretags::get_resource_name();
  208:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  209:     if ($target eq 'web') {
  210: 	$result=$name;
  211: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
  212:     } elsif ($target eq 'edit') {
  213: 	$result=&Apache::edit::tag_start($target,$token);
  214: 	$result.='</td></tr>';
  215: 	$result.=&Apache::edit::end_table();
  216:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  217: 	$name=&Apache::lonxml::latex_special_symbols($name);
  218: 	$name = &substitute_simple_tags_latex($name);
  219: 	if (lc($style) !~ 'plain') { 
  220: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
  221: 	} else {
  222: 	    $result=$name;
  223: 	}
  224:     }
  225:     return $result;
  226: }
  227: 
  228: sub end_displaytitle {
  229:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  230:     my @result;
  231:     if ($target eq 'edit') { $result[1]='no'; }
  232:     return @result;
  233: }
  234: 
  235: sub multipart {
  236:     my ($uri)=@_;
  237:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
  238:     my ($symb)=&Apache::lonnet::whichuser();
  239: 
  240:     my @parts;
  241:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
  242:     foreach (split(/\,/,$metadata)) {
  243: 	if ($_ =~ /^part_(.*)$/) {
  244: 	    my $part = $1;
  245: 	    if ($part ne '0' 
  246: 		&& !&Apache::loncommon::check_if_partid_hidden($part, 
  247: 							       $symb)) {
  248: 		push(@parts,$part);
  249: 	    }
  250: 	}
  251:     }
  252:     return @parts;
  253: }
  254: 
  255: sub start_displayweight {
  256:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  257:     my $result;
  258:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
  259: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
  260: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
  261: 	    return '';
  262: 	}
  263:     }
  264:     push(@{$Apache::outputtags::showonce{'displayweight'}},
  265: 	 $Apache::inputtags::part);
  266:     if ($target eq 'web' || $target eq 'tex') {
  267: 	my $id = $Apache::inputtags::part;
  268: 	if ($id ne '0') {
  269: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  270: 	    if (!defined($weight) || ($weight eq '')) { $weight=1; }
  271: 	    $result.=$weight;
  272: 	} else {
  273: 	    my @parts=&multipart($env{'request.uri'});
  274: 	    my $weight;
  275: 	    if (@parts) {
  276: 	        foreach my $part (@parts) {
  277: 		    my $pweight=&Apache::lonnet::EXT("resource.$part.weight");
  278: 		    if (!defined($pweight) || ($pweight eq '')) { $pweight=1; }
  279: 		    $weight+=$pweight;
  280: 	        }
  281: 	    } else {
  282: 		$weight = &Apache::lonnet::EXT("resource.$id.weight");
  283:                 if (!defined($weight) || ($weight eq '')) { $weight=1; }
  284: 	    }
  285: 	    $result=$weight;
  286: 	}
  287:     } elsif ( $target eq 'edit' ) {
  288: 	$result=&Apache::edit::tag_start($target,$token);
  289: 	$result.='</td></tr>';
  290: 	$result.=&Apache::edit::end_table();
  291:     }
  292:     return $result;
  293: }
  294: 
  295: sub end_displayweight {
  296:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  297:     my @result;
  298:     if ($target eq 'edit') { $result[1]='no'; }
  299:     return @result;
  300: }
  301: 
  302: sub start_displaystudentphoto {
  303:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  304:     my $result;
  305:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
  306:     if ($target eq 'web' && $user eq $env{'user.name'}) {
  307: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
  308: 	my $args;
  309: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
  310: 	if ($width) { $args.=" width=\"$width\" "; }
  311: 	my $height=&Apache::lonxml::get_param('heigth',$parstack,$safeeval);
  312: 	if ($height) { $args.=" height=\"$height\" "; }
  313: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
  314: 	if ($align) { $args.=" align=\"$align\" "; }
  315: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
  316:     }
  317:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
  318: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
  319: 	my $ua=new LWP::UserAgent;
  320: 	my $request=new HTTP::Request('GET',$url);
  321: 	my $response=$ua->request($request);
  322: 	if ($response->is_success) {
  323: 	    my $file=$user."_".$domain."_studentphoto.eps";
  324: 	    open(FILE,">".$Apache::lonnet::perlvar{'lonPrtDir'}."/$file");
  325: 	    print FILE $response->content;
  326: 	    close(FILE);
  327: 	    my $width_param=&Apache::londefdef::image_size($Apache::lonnet::perlvar{'lonPrtDir'}."/$file",'0.3',$parstack,$safeeval);
  328: 	    $result.=' \graphicspath{{'.$Apache::lonnet::perlvar{'lonPrtDir'}.
  329: 		'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
  330: 	} else {
  331: 	    $result="$user\@$domain";
  332: 	}
  333:     }
  334:     return $result;
  335: }
  336: 
  337: sub end_displaystudentphoto {
  338:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  339:     my @result;
  340:     if ($target eq 'edit') { $result[1]='no'; }
  341:     return @result;
  342: }
  343: 
  344: 1;
  345: __END__
  346: 
  347: 
  348: =head1 NAME
  349: 
  350: Apache::outputtags;
  351: 
  352: =head1 SYNOPSIS
  353: 
  354: Handles tags associated with output. Seems to
  355: relate to due dates of the assignment.
  356: 
  357: This is part of the LearningOnline Network with CAPA project
  358: described at http://www.lon-capa.org.
  359: 
  360: =head1 SUBROUTINES
  361: 
  362: =over
  363: 
  364: =item start_displayduedate()
  365: 
  366: =item initialize_outputtags()
  367: 
  368: Empties the hash of tags that have already been displayed that should only be displayed once.
  369: 
  370: =item end_displayduedate()
  371: 
  372: =item start_displaytitle()
  373: 
  374: =item end_displaytitle()
  375: 
  376: =item multipart()
  377: 
  378: =item start_displayweight()
  379: 
  380: =item end_displayweight()
  381: 
  382: =item start_displaystudentphoto()
  383: 
  384: =item end_displaystudentphoto()
  385: 
  386: 
  387: =back
  388: 
  389: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>