File:  [LON-CAPA] / loncom / homework / outputtags.pm
Revision 1.53: download - view: text, annotated - select for diffs
Tue Nov 18 19:14:28 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added/updated/converted POD. Many files had their POD redone to new standards.

    1: # The LearningOnline Network with CAPA 
    2: # tags that create controlled output
    3: #
    4: # $Id: outputtags.pm,v 1.53 2008/11/18 19:14:28 jms 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: =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 SUBROUTINES
   42: 
   43: =over
   44: 
   45: =item start_displayduedate()
   46: 
   47: =item initialize_outputtags()
   48: 
   49: Empties the hash of tags that have already been displayed that should only be displayed once.
   50: 
   51: =item end_displayduedate()
   52: 
   53: =item start_displaytitle()
   54: 
   55: =item end_displaytitle()
   56: 
   57: =item multipart()
   58: 
   59: =item start_displayweight()
   60: 
   61: =item end_displayweight()
   62: 
   63: =item start_displaystudentphoto()
   64: 
   65: =item end_displaystudentphoto()
   66: 
   67: 
   68: =back
   69: 
   70: =cut
   71: 
   72: package Apache::outputtags; 
   73: 
   74: use strict;
   75: use Apache::lonlocal;
   76: use Apache::lonnet;
   77: use POSIX qw(strftime);
   78: 
   79: BEGIN {
   80:     &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
   81: }
   82: 
   83: sub initialize_outputtags {
   84:     %Apache::outputtags::showonce=();
   85: }
   86: 
   87: 
   88: sub start_displayduedate {
   89:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   90:     my $result;
   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: 
   95:     if (exists($Apache::outputtags::showonce{'displayduedate'})) {
   96: 	if (grep(/^\Q$Apache::inputtags::part\E$/,
   97: 		 @{$Apache::outputtags::showonce{'displayduedate'}})) {
   98: 	    return '';		# Already shown the duedate for this part.
   99: 	}
  100:     }
  101:     # since we will show the duedate for this part, remember it.
  102: 
  103:     push (@{$Apache::outputtags::showonce{'displayduedate'}},
  104: 	  $Apache::inputtags::part);
  105: 
  106:     #  Determine the due date format:
  107:     #
  108:     my $status=$Apache::inputtags::status['-1'];
  109:     &Apache::lonxml::debug("got a $status in duedatebox");
  110:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  111:     my $format = &Apache::lonxml::get_param('format', $parstack, $safeeval);
  112:     if (!$format) {
  113: 	$format = undef;
  114:     }
  115:     if (($status =~ /CAN.*_ANSWER/)) {
  116: 	my $id = $Apache::inputtags::part;
  117: 	my $date = &Apache::lonhomework::due_date($id);
  118: 	&Apache::lonxml::debug("duedatebox found $date for $id");
  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) {
  135: 	    my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
  136: 	    if ($target eq 'web') {
  137: 		if (lc($style) !~ 'plain') { 
  138: 		    $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
  139: 		} else {
  140: 		    $result=&mt('Due').' '.$duetext;
  141: 		}
  142: 	    } elsif ($target eq 'tex') {
  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: 		}
  155: 	    }
  156: 	}
  157:      
  158:     } 
  159:     if ( $target eq 'edit' ) {
  160: 	$result=&Apache::edit::tag_start($target,$token);
  161: 	$result.='</td></tr>';
  162: 	$result.=&Apache::edit::end_table();
  163: 	
  164:     }
  165:     return $result;
  166: }
  167: 
  168: sub end_displayduedate {
  169:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  170:     my @result;
  171:     if ($target eq 'edit') { $result[1]='no'; }
  172:     return @result;
  173: }
  174: 
  175: sub start_displaytitle {
  176:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  177:     my $result='';
  178:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
  179: 	return '';
  180:     } else {
  181: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
  182:     }
  183:     my $name=&Apache::structuretags::get_resource_name();
  184:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  185:     if ($target eq 'web') {
  186: 	$result=$name;
  187: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
  188:     } elsif ($target eq 'edit') {
  189: 	$result=&Apache::edit::tag_start($target,$token);
  190: 	$result.='</td></tr>';
  191: 	$result.=&Apache::edit::end_table();
  192:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  193: 	$name=&Apache::lonxml::latex_special_symbols($name);
  194: 	if (lc($style) !~ 'plain') { 
  195: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
  196: 	} else {
  197: 	    $result=$name;
  198: 	}
  199:     }
  200:     return $result;
  201: }
  202: 
  203: sub end_displaytitle {
  204:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  205:     my @result;
  206:     if ($target eq 'edit') { $result[1]='no'; }
  207:     return @result;
  208: }
  209: 
  210: sub multipart {
  211:     my ($uri)=@_;
  212:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
  213:     my ($symb)=&Apache::lonnet::whichuser();
  214: 
  215:     my @parts;
  216:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
  217:     foreach (split(/\,/,$metadata)) {
  218: 	if ($_ =~ /^part_(.*)$/) {
  219: 	    my $part = $1;
  220: 	    if ($part ne '0' 
  221: 		&& !&Apache::loncommon::check_if_partid_hidden($part, 
  222: 							       $symb)) {
  223: 		push(@parts,$part);
  224: 	    }
  225: 	}
  226:     }
  227:     return @parts;
  228: }
  229: 
  230: sub start_displayweight {
  231:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  232:     my $result;
  233:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
  234: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
  235: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
  236: 	    return '';
  237: 	}
  238:     }
  239:     push(@{$Apache::outputtags::showonce{'displayweight'}},
  240: 	 $Apache::inputtags::part);
  241:     if ($target eq 'web' || $target eq 'tex') {
  242: 	my $id = $Apache::inputtags::part;
  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 {
  248: 	    my @parts=&multipart($env{'request.uri'});
  249: 	    my $weight;
  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; }
  259: 	    }
  260: 	    $result=$weight;
  261: 	}
  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 {
  271:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  272:     my @result;
  273:     if ($target eq 'edit') { $result[1]='no'; }
  274:     return @result;
  275: }
  276: 
  277: sub start_displaystudentphoto {
  278:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  279:     my $result;
  280:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
  281:     if ($target eq 'web' && $user eq $env{'user.name'}) {
  282: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
  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\" "; }
  288: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
  289: 	if ($align) { $args.=" align=\"$align\" "; }
  290: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
  291:     }
  292:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
  293: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
  294: 	my $ua=new LWP::UserAgent;
  295: 	my $request=new HTTP::Request('GET',$url);
  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.'} ';
  305: 	} else {
  306: 	    $result="$user\@$domain";
  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: 
  319: 1;
  320: __END__

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