Annotation of loncom/homework/outputtags.pm, revision 1.57

1.13      albertel    1: # The LearningOnline Network with CAPA 
                      2: # tags that create controlled output
                      3: #
1.57    ! foxr        4: # $Id: outputtags.pm,v 1.56 2009/11/01 16:10:35 foxr Exp $
1.13      albertel    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: 
1.52      jms        29: 
                     30: 
1.1       albertel   31: package Apache::outputtags; 
                     32: 
                     33: use strict;
1.40      albertel   34: use Apache::lonlocal;
1.1       albertel   35: use Apache::lonnet;
                     36: use POSIX qw(strftime);
                     37: 
1.15      harris41   38: BEGIN {
1.35      albertel   39:     &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
1.20      albertel   40: }
1.52      jms        41: 
1.56      foxr       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: 
1.20      albertel  104: sub initialize_outputtags {
                    105:     %Apache::outputtags::showonce=();
1.1       albertel  106: }
                    107: 
1.39      foxr      108: 
1.56      foxr      109: 
1.2       albertel  110: sub start_displayduedate {
1.20      albertel  111:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    112:     my $result;
1.39      foxr      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: 
1.20      albertel  117:     if (exists($Apache::outputtags::showonce{'displayduedate'})) {
1.39      foxr      118: 	if (grep(/^\Q$Apache::inputtags::part\E$/,
                    119: 		 @{$Apache::outputtags::showonce{'displayduedate'}})) {
1.41      foxr      120: 	    return '';		# Already shown the duedate for this part.
1.39      foxr      121: 	}
1.10      albertel  122:     }
1.43      foxr      123:     # since we will show the duedate for this part, remember it.
1.41      foxr      124: 
1.39      foxr      125:     push (@{$Apache::outputtags::showonce{'displayduedate'}},
                    126: 	  $Apache::inputtags::part);
                    127: 
1.43      foxr      128:     #  Determine the due date format:
                    129:     #
1.20      albertel  130:     my $status=$Apache::inputtags::status['-1'];
                    131:     &Apache::lonxml::debug("got a $status in duedatebox");
1.32      albertel  132:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
1.43      foxr      133:     my $format = &Apache::lonxml::get_param('format', $parstack, $safeeval);
                    134:     if (!$format) {
1.46      albertel  135: 	$format = undef;
1.43      foxr      136:     }
                    137:     if (($status =~ /CAN.*_ANSWER/)) {
1.20      albertel  138: 	my $id = $Apache::inputtags::part;
1.48      albertel  139: 	my $date = &Apache::lonhomework::due_date($id);
1.20      albertel  140: 	&Apache::lonxml::debug("duedatebox found $date for $id");
1.39      foxr      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) {
1.44      foxr      157: 	    my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
1.43      foxr      158: 	    if ($target eq 'web') {
                    159: 		if (lc($style) !~ 'plain') { 
1.44      foxr      160: 		    $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
1.43      foxr      161: 		} else {
1.44      foxr      162: 		    $result=&mt('Due').' '.$duetext;
1.43      foxr      163: 		}
                    164: 	    } elsif ($target eq 'tex') {
1.44      foxr      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.
1.55      foxr      171: 		    # at the start of the line to ensure it won't overlap
                    172: 		    # the 1 col boundary.
                    173: 
1.57    ! foxr      174: 		    $result = '\vspace{1.0 ex} \framebox{'
1.44      foxr      175:     			       .&mt('Due').' '.$duetext.'}';
                    176: 		} else {
                    177: 		    $result = &mt('Due') . ' '.$duetext;
                    178: 		}
1.39      foxr      179: 	    }
1.32      albertel  180: 	}
1.43      foxr      181:      
                    182:     } 
                    183:     if ( $target eq 'edit' ) {
1.20      albertel  184: 	$result=&Apache::edit::tag_start($target,$token);
                    185: 	$result.='</td></tr>';
                    186: 	$result.=&Apache::edit::end_table();
1.43      foxr      187: 	
1.5       www       188:     }
1.20      albertel  189:     return $result;
1.1       albertel  190: }
                    191: 
1.2       albertel  192: sub end_displayduedate {
1.20      albertel  193:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    194:     my @result;
                    195:     if ($target eq 'edit') { $result[1]='no'; }
                    196:     return @result;
1.2       albertel  197: }
                    198: 
                    199: sub start_displaytitle {
1.20      albertel  200:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    201:     my $result='';
1.21      albertel  202:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
1.20      albertel  203: 	return '';
                    204:     } else {
1.21      albertel  205: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
1.20      albertel  206:     }
1.31      albertel  207:     my $name=&Apache::structuretags::get_resource_name();
1.32      albertel  208:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
1.20      albertel  209:     if ($target eq 'web') {
1.32      albertel  210: 	$result=$name;
                    211: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
1.20      albertel  212:     } elsif ($target eq 'edit') {
                    213: 	$result=&Apache::edit::tag_start($target,$token);
                    214: 	$result.='</td></tr>';
                    215: 	$result.=&Apache::edit::end_table();
1.25      sakharuk  216:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.31      albertel  217: 	$name=&Apache::lonxml::latex_special_symbols($name);
1.56      foxr      218: 	$name = &substitute_simple_tags_latex($name);
1.32      albertel  219: 	if (lc($style) !~ 'plain') { 
                    220: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
                    221: 	} else {
                    222: 	    $result=$name;
                    223: 	}
1.20      albertel  224:     }
                    225:     return $result;
1.2       albertel  226: }
                    227: 
                    228: sub end_displaytitle {
1.22      albertel  229:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    230:     my @result;
                    231:     if ($target eq 'edit') { $result[1]='no'; }
                    232:     return @result;
                    233: }
                    234: 
1.29      albertel  235: sub multipart {
                    236:     my ($uri)=@_;
1.38      albertel  237:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
1.49      albertel  238:     my ($symb)=&Apache::lonnet::whichuser();
1.45      albertel  239: 
1.29      albertel  240:     my @parts;
                    241:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
                    242:     foreach (split(/\,/,$metadata)) {
                    243: 	if ($_ =~ /^part_(.*)$/) {
                    244: 	    my $part = $1;
1.45      albertel  245: 	    if ($part ne '0' 
                    246: 		&& !&Apache::loncommon::check_if_partid_hidden($part, 
                    247: 							       $symb)) {
                    248: 		push(@parts,$part);
                    249: 	    }
1.29      albertel  250: 	}
                    251:     }
                    252:     return @parts;
                    253: }
                    254: 
1.22      albertel  255: sub start_displayweight {
                    256:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    257:     my $result;
                    258:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
1.26      albertel  259: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
                    260: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
                    261: 	    return '';
                    262: 	}
1.22      albertel  263:     }
1.26      albertel  264:     push(@{$Apache::outputtags::showonce{'displayweight'}},
                    265: 	 $Apache::inputtags::part);
1.24      albertel  266:     if ($target eq 'web' || $target eq 'tex') {
1.22      albertel  267: 	my $id = $Apache::inputtags::part;
1.27      albertel  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 {
1.38      albertel  273: 	    my @parts=&multipart($env{'request.uri'});
1.27      albertel  274: 	    my $weight;
1.30      albertel  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; }
1.27      albertel  284: 	    }
                    285: 	    $result=$weight;
                    286: 	}
1.22      albertel  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 {
1.20      albertel  296:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    297:     my @result;
                    298:     if ($target eq 'edit') { $result[1]='no'; }
                    299:     return @result;
1.1       albertel  300: }
                    301: 
1.35      albertel  302: sub start_displaystudentphoto {
                    303:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    304:     my $result;
1.49      albertel  305:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
1.38      albertel  306:     if ($target eq 'web' && $user eq $env{'user.name'}) {
1.37      albertel  307: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
1.35      albertel  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\" "; }
1.36      albertel  313: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
                    314: 	if ($align) { $args.=" align=\"$align\" "; }
1.37      albertel  315: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
1.35      albertel  316:     }
1.38      albertel  317:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
1.37      albertel  318: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
1.35      albertel  319: 	my $ua=new LWP::UserAgent;
1.37      albertel  320: 	my $request=new HTTP::Request('GET',$url);
1.35      albertel  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.'} ';
1.36      albertel  330: 	} else {
                    331: 	    $result="$user\@$domain";
1.35      albertel  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: 
1.1       albertel  344: 1;
                    345: __END__
1.54      jms       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: 
1.55      foxr      389: =cut

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