File:  [LON-CAPA] / loncom / homework / outputtags.pm
Revision 1.49: download - view: text, annotated - select for diffs
Tue Oct 17 15:11:12 2006 UTC (17 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
- complte the move of whichuser() from lonnxml -> lonnet

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

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