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

1.13      albertel    1: # The LearningOnline Network with CAPA 
                      2: # tags that create controlled output
                      3: #
1.53    ! jms         4: # $Id: outputtags.pm,v 1.52 2008/11/14 21:27:17 jms 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: =head1 NAME
                     30: 
                     31: Apache::outputtags;
                     32: 
                     33: =head1 SYNOPSIS
                     34: 
                     35: Handles tags associated with output. Seems to
                     36: relate to due dates of the assignment.
                     37: 
                     38: This is part of the LearningOnline Network with CAPA project
                     39: described at http://www.lon-capa.org.
                     40: 
1.53    ! jms        41: =head1 SUBROUTINES
1.52      jms        42: 
                     43: =over
                     44: 
1.53    ! jms        45: =item start_displayduedate()
1.52      jms        46: 
1.53    ! jms        47: =item initialize_outputtags()
1.52      jms        48: 
1.53    ! jms        49: Empties the hash of tags that have already been displayed that should only be displayed once.
1.52      jms        50: 
1.53    ! jms        51: =item end_displayduedate()
1.52      jms        52: 
1.53    ! jms        53: =item start_displaytitle()
1.52      jms        54: 
1.53    ! jms        55: =item end_displaytitle()
1.52      jms        56: 
1.53    ! jms        57: =item multipart()
1.52      jms        58: 
1.53    ! jms        59: =item start_displayweight()
1.52      jms        60: 
1.53    ! jms        61: =item end_displayweight()
1.52      jms        62: 
1.53    ! jms        63: =item start_displaystudentphoto()
1.52      jms        64: 
1.53    ! jms        65: =item end_displaystudentphoto()
1.52      jms        66: 
                     67: 
                     68: =back
                     69: 
                     70: =cut
                     71: 
1.1       albertel   72: package Apache::outputtags; 
                     73: 
                     74: use strict;
1.40      albertel   75: use Apache::lonlocal;
1.1       albertel   76: use Apache::lonnet;
                     77: use POSIX qw(strftime);
                     78: 
1.15      harris41   79: BEGIN {
1.35      albertel   80:     &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
1.20      albertel   81: }
1.52      jms        82: 
1.20      albertel   83: sub initialize_outputtags {
                     84:     %Apache::outputtags::showonce=();
1.1       albertel   85: }
                     86: 
1.39      foxr       87: 
1.2       albertel   88: sub start_displayduedate {
1.20      albertel   89:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     90:     my $result;
1.39      foxr       91: 
                     92:     # Different parts can have different due dates... so we keep a list
                     93:     # of the parts for which we've displayed the duedate:
                     94: 
1.20      albertel   95:     if (exists($Apache::outputtags::showonce{'displayduedate'})) {
1.39      foxr       96: 	if (grep(/^\Q$Apache::inputtags::part\E$/,
                     97: 		 @{$Apache::outputtags::showonce{'displayduedate'}})) {
1.41      foxr       98: 	    return '';		# Already shown the duedate for this part.
1.39      foxr       99: 	}
1.10      albertel  100:     }
1.43      foxr      101:     # since we will show the duedate for this part, remember it.
1.41      foxr      102: 
1.39      foxr      103:     push (@{$Apache::outputtags::showonce{'displayduedate'}},
                    104: 	  $Apache::inputtags::part);
                    105: 
1.43      foxr      106:     #  Determine the due date format:
                    107:     #
1.20      albertel  108:     my $status=$Apache::inputtags::status['-1'];
                    109:     &Apache::lonxml::debug("got a $status in duedatebox");
1.32      albertel  110:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
1.43      foxr      111:     my $format = &Apache::lonxml::get_param('format', $parstack, $safeeval);
                    112:     if (!$format) {
1.46      albertel  113: 	$format = undef;
1.43      foxr      114:     }
                    115:     if (($status =~ /CAN.*_ANSWER/)) {
1.20      albertel  116: 	my $id = $Apache::inputtags::part;
1.48      albertel  117: 	my $date = &Apache::lonhomework::due_date($id);
1.20      albertel  118: 	&Apache::lonxml::debug("duedatebox found $date for $id");
1.39      foxr      119: 
                    120: 	# Only show the due date if the current date is 
                    121: 	# different from due date of the previous part.  I think
                    122: 	# this is probably the best way to avoid due date clutter.
                    123: 
                    124:        	my $showduedate = 1;
                    125: 	my $part_count  = scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
                    126: 	if ($part_count > 1) {
                    127: 	    my $prev_part_id = $Apache::outputtags::showonce{'displayduedate'}->[$part_count-2];
                    128: 	    my $prev_due_date = &Apache::lonnet::EXT("resource.$prev_part_id.duedate");
                    129: 	    if ($prev_due_date == $date) {
                    130: 		$showduedate = 0;
                    131: 	    }
                    132: 	}
                    133: 
                    134: 	if ($showduedate) {
1.44      foxr      135: 	    my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
1.43      foxr      136: 	    if ($target eq 'web') {
                    137: 		if (lc($style) !~ 'plain') { 
1.44      foxr      138: 		    $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
1.43      foxr      139: 		} else {
1.44      foxr      140: 		    $result=&mt('Due').' '.$duetext;
1.43      foxr      141: 		}
                    142: 	    } elsif ($target eq 'tex') {
1.44      foxr      143: 		# For TeX we'll make the duedate tag work exactly like the 
                    144: 		# duedate tag for web.
                    145: 
                    146: 		my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
                    147: 		if (lc($style) !~ 'plain') {
                    148: 		    # The due date will be put in a box.
                    149: 		    
                    150: 		    $result = '\framebox{'
                    151:     			       .&mt('Due').' '.$duetext.'}';
                    152: 		} else {
                    153: 		    $result = &mt('Due') . ' '.$duetext;
                    154: 		}
1.39      foxr      155: 	    }
1.32      albertel  156: 	}
1.43      foxr      157:      
                    158:     } 
                    159:     if ( $target eq 'edit' ) {
1.20      albertel  160: 	$result=&Apache::edit::tag_start($target,$token);
                    161: 	$result.='</td></tr>';
                    162: 	$result.=&Apache::edit::end_table();
1.43      foxr      163: 	
1.5       www       164:     }
1.20      albertel  165:     return $result;
1.1       albertel  166: }
                    167: 
1.2       albertel  168: sub end_displayduedate {
1.20      albertel  169:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    170:     my @result;
                    171:     if ($target eq 'edit') { $result[1]='no'; }
                    172:     return @result;
1.2       albertel  173: }
                    174: 
                    175: sub start_displaytitle {
1.20      albertel  176:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    177:     my $result='';
1.21      albertel  178:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
1.20      albertel  179: 	return '';
                    180:     } else {
1.21      albertel  181: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
1.20      albertel  182:     }
1.31      albertel  183:     my $name=&Apache::structuretags::get_resource_name();
1.32      albertel  184:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
1.20      albertel  185:     if ($target eq 'web') {
1.32      albertel  186: 	$result=$name;
                    187: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
1.20      albertel  188:     } elsif ($target eq 'edit') {
                    189: 	$result=&Apache::edit::tag_start($target,$token);
                    190: 	$result.='</td></tr>';
                    191: 	$result.=&Apache::edit::end_table();
1.25      sakharuk  192:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.31      albertel  193: 	$name=&Apache::lonxml::latex_special_symbols($name);
1.32      albertel  194: 	if (lc($style) !~ 'plain') { 
                    195: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
                    196: 	} else {
                    197: 	    $result=$name;
                    198: 	}
1.20      albertel  199:     }
                    200:     return $result;
1.2       albertel  201: }
                    202: 
                    203: sub end_displaytitle {
1.22      albertel  204:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    205:     my @result;
                    206:     if ($target eq 'edit') { $result[1]='no'; }
                    207:     return @result;
                    208: }
                    209: 
1.29      albertel  210: sub multipart {
                    211:     my ($uri)=@_;
1.38      albertel  212:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
1.49      albertel  213:     my ($symb)=&Apache::lonnet::whichuser();
1.45      albertel  214: 
1.29      albertel  215:     my @parts;
                    216:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
                    217:     foreach (split(/\,/,$metadata)) {
                    218: 	if ($_ =~ /^part_(.*)$/) {
                    219: 	    my $part = $1;
1.45      albertel  220: 	    if ($part ne '0' 
                    221: 		&& !&Apache::loncommon::check_if_partid_hidden($part, 
                    222: 							       $symb)) {
                    223: 		push(@parts,$part);
                    224: 	    }
1.29      albertel  225: 	}
                    226:     }
                    227:     return @parts;
                    228: }
                    229: 
1.22      albertel  230: sub start_displayweight {
                    231:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    232:     my $result;
                    233:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
1.26      albertel  234: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
                    235: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
                    236: 	    return '';
                    237: 	}
1.22      albertel  238:     }
1.26      albertel  239:     push(@{$Apache::outputtags::showonce{'displayweight'}},
                    240: 	 $Apache::inputtags::part);
1.24      albertel  241:     if ($target eq 'web' || $target eq 'tex') {
1.22      albertel  242: 	my $id = $Apache::inputtags::part;
1.27      albertel  243: 	if ($id ne '0') {
                    244: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    245: 	    if (!defined($weight) || ($weight eq '')) { $weight=1; }
                    246: 	    $result.=$weight;
                    247: 	} else {
1.38      albertel  248: 	    my @parts=&multipart($env{'request.uri'});
1.27      albertel  249: 	    my $weight;
1.30      albertel  250: 	    if (@parts) {
                    251: 	        foreach my $part (@parts) {
                    252: 		    my $pweight=&Apache::lonnet::EXT("resource.$part.weight");
                    253: 		    if (!defined($pweight) || ($pweight eq '')) { $pweight=1; }
                    254: 		    $weight+=$pweight;
                    255: 	        }
                    256: 	    } else {
                    257: 		$weight = &Apache::lonnet::EXT("resource.$id.weight");
                    258:                 if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.27      albertel  259: 	    }
                    260: 	    $result=$weight;
                    261: 	}
1.22      albertel  262:     } elsif ( $target eq 'edit' ) {
                    263: 	$result=&Apache::edit::tag_start($target,$token);
                    264: 	$result.='</td></tr>';
                    265: 	$result.=&Apache::edit::end_table();
                    266:     }
                    267:     return $result;
                    268: }
                    269: 
                    270: sub end_displayweight {
1.20      albertel  271:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    272:     my @result;
                    273:     if ($target eq 'edit') { $result[1]='no'; }
                    274:     return @result;
1.1       albertel  275: }
                    276: 
1.35      albertel  277: sub start_displaystudentphoto {
                    278:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    279:     my $result;
1.49      albertel  280:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
1.38      albertel  281:     if ($target eq 'web' && $user eq $env{'user.name'}) {
1.37      albertel  282: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
1.35      albertel  283: 	my $args;
                    284: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
                    285: 	if ($width) { $args.=" width=\"$width\" "; }
                    286: 	my $height=&Apache::lonxml::get_param('heigth',$parstack,$safeeval);
                    287: 	if ($height) { $args.=" height=\"$height\" "; }
1.36      albertel  288: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
                    289: 	if ($align) { $args.=" align=\"$align\" "; }
1.37      albertel  290: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
1.35      albertel  291:     }
1.38      albertel  292:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
1.37      albertel  293: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
1.35      albertel  294: 	my $ua=new LWP::UserAgent;
1.37      albertel  295: 	my $request=new HTTP::Request('GET',$url);
1.35      albertel  296: 	my $response=$ua->request($request);
                    297: 	if ($response->is_success) {
                    298: 	    my $file=$user."_".$domain."_studentphoto.eps";
                    299: 	    open(FILE,">".$Apache::lonnet::perlvar{'lonPrtDir'}."/$file");
                    300: 	    print FILE $response->content;
                    301: 	    close(FILE);
                    302: 	    my $width_param=&Apache::londefdef::image_size($Apache::lonnet::perlvar{'lonPrtDir'}."/$file",'0.3',$parstack,$safeeval);
                    303: 	    $result.=' \graphicspath{{'.$Apache::lonnet::perlvar{'lonPrtDir'}.
                    304: 		'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
1.36      albertel  305: 	} else {
                    306: 	    $result="$user\@$domain";
1.35      albertel  307: 	}
                    308:     }
                    309:     return $result;
                    310: }
                    311: 
                    312: sub end_displaystudentphoto {
                    313:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    314:     my @result;
                    315:     if ($target eq 'edit') { $result[1]='no'; }
                    316:     return @result;
                    317: }
                    318: 
1.1       albertel  319: 1;
                    320: __END__

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