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

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

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