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

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

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