File:  [LON-CAPA] / loncom / homework / outputtags.pm
Revision 1.52: download - view: text, annotated - select for diffs
Fri Nov 14 21:27:17 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added/converted comments to POD style

    1: # The LearningOnline Network with CAPA 
    2: # tags that create controlled output
    3: #
    4: # $Id: outputtags.pm,v 1.52 2008/11/14 21:27:17 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 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: 
   90: package Apache::outputtags; 
   91: 
   92: use strict;
   93: use Apache::lonlocal;
   94: use Apache::lonnet;
   95: use POSIX qw(strftime);
   96: 
   97: BEGIN {
   98:     &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
   99: }
  100: 
  101: sub initialize_outputtags {
  102:     %Apache::outputtags::showonce=();
  103: }
  104: 
  105: 
  106: sub start_displayduedate {
  107:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  108:     my $result;
  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: 
  113:     if (exists($Apache::outputtags::showonce{'displayduedate'})) {
  114: 	if (grep(/^\Q$Apache::inputtags::part\E$/,
  115: 		 @{$Apache::outputtags::showonce{'displayduedate'}})) {
  116: 	    return '';		# Already shown the duedate for this part.
  117: 	}
  118:     }
  119:     # since we will show the duedate for this part, remember it.
  120: 
  121:     push (@{$Apache::outputtags::showonce{'displayduedate'}},
  122: 	  $Apache::inputtags::part);
  123: 
  124:     #  Determine the due date format:
  125:     #
  126:     my $status=$Apache::inputtags::status['-1'];
  127:     &Apache::lonxml::debug("got a $status in duedatebox");
  128:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  129:     my $format = &Apache::lonxml::get_param('format', $parstack, $safeeval);
  130:     if (!$format) {
  131: 	$format = undef;
  132:     }
  133:     if (($status =~ /CAN.*_ANSWER/)) {
  134: 	my $id = $Apache::inputtags::part;
  135: 	my $date = &Apache::lonhomework::due_date($id);
  136: 	&Apache::lonxml::debug("duedatebox found $date for $id");
  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) {
  153: 	    my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
  154: 	    if ($target eq 'web') {
  155: 		if (lc($style) !~ 'plain') { 
  156: 		    $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
  157: 		} else {
  158: 		    $result=&mt('Due').' '.$duetext;
  159: 		}
  160: 	    } elsif ($target eq 'tex') {
  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: 		}
  173: 	    }
  174: 	}
  175:      
  176:     } 
  177:     if ( $target eq 'edit' ) {
  178: 	$result=&Apache::edit::tag_start($target,$token);
  179: 	$result.='</td></tr>';
  180: 	$result.=&Apache::edit::end_table();
  181: 	
  182:     }
  183:     return $result;
  184: }
  185: 
  186: sub end_displayduedate {
  187:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  188:     my @result;
  189:     if ($target eq 'edit') { $result[1]='no'; }
  190:     return @result;
  191: }
  192: 
  193: sub start_displaytitle {
  194:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  195:     my $result='';
  196:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
  197: 	return '';
  198:     } else {
  199: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
  200:     }
  201:     my $name=&Apache::structuretags::get_resource_name();
  202:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  203:     if ($target eq 'web') {
  204: 	$result=$name;
  205: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
  206:     } elsif ($target eq 'edit') {
  207: 	$result=&Apache::edit::tag_start($target,$token);
  208: 	$result.='</td></tr>';
  209: 	$result.=&Apache::edit::end_table();
  210:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  211: 	$name=&Apache::lonxml::latex_special_symbols($name);
  212: 	if (lc($style) !~ 'plain') { 
  213: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
  214: 	} else {
  215: 	    $result=$name;
  216: 	}
  217:     }
  218:     return $result;
  219: }
  220: 
  221: sub end_displaytitle {
  222:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  223:     my @result;
  224:     if ($target eq 'edit') { $result[1]='no'; }
  225:     return @result;
  226: }
  227: 
  228: sub multipart {
  229:     my ($uri)=@_;
  230:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
  231:     my ($symb)=&Apache::lonnet::whichuser();
  232: 
  233:     my @parts;
  234:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
  235:     foreach (split(/\,/,$metadata)) {
  236: 	if ($_ =~ /^part_(.*)$/) {
  237: 	    my $part = $1;
  238: 	    if ($part ne '0' 
  239: 		&& !&Apache::loncommon::check_if_partid_hidden($part, 
  240: 							       $symb)) {
  241: 		push(@parts,$part);
  242: 	    }
  243: 	}
  244:     }
  245:     return @parts;
  246: }
  247: 
  248: sub start_displayweight {
  249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  250:     my $result;
  251:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
  252: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
  253: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
  254: 	    return '';
  255: 	}
  256:     }
  257:     push(@{$Apache::outputtags::showonce{'displayweight'}},
  258: 	 $Apache::inputtags::part);
  259:     if ($target eq 'web' || $target eq 'tex') {
  260: 	my $id = $Apache::inputtags::part;
  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 {
  266: 	    my @parts=&multipart($env{'request.uri'});
  267: 	    my $weight;
  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; }
  277: 	    }
  278: 	    $result=$weight;
  279: 	}
  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 {
  289:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  290:     my @result;
  291:     if ($target eq 'edit') { $result[1]='no'; }
  292:     return @result;
  293: }
  294: 
  295: sub start_displaystudentphoto {
  296:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  297:     my $result;
  298:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
  299:     if ($target eq 'web' && $user eq $env{'user.name'}) {
  300: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
  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\" "; }
  306: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
  307: 	if ($align) { $args.=" align=\"$align\" "; }
  308: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
  309:     }
  310:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
  311: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
  312: 	my $ua=new LWP::UserAgent;
  313: 	my $request=new HTTP::Request('GET',$url);
  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.'} ';
  323: 	} else {
  324: 	    $result="$user\@$domain";
  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: 
  337: 1;
  338: __END__

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