File:  [LON-CAPA] / loncom / homework / outputtags.pm
Revision 1.43: download - view: text, annotated - select for diffs
Tue Nov 8 12:00:22 2005 UTC (18 years, 7 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Suport format in displayduedate (web target only for now).

    1: # The LearningOnline Network with CAPA 
    2: # tags that create controlled output
    3: #
    4: # $Id: outputtags.pm,v 1.43 2005/11/08 12:00:22 foxr 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 = '%c';
   73:     }
   74:     if (($status =~ /CAN.*_ANSWER/)) {
   75: 	my $id = $Apache::inputtags::part;
   76: 	my $date = &Apache::lonnet::EXT("resource.$id.duedate");
   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: 	    if ($target eq 'web') {
   95: 		if (lc($style) !~ 'plain') { 
   96: 		    $result ='<table border="on"><tr><td>Due '.
   97: 			&Apache::lonnavmaps::timeToHumanString($date,'', $format).
   98: 			'</td></tr></table>';
   99: 		} else {
  100: 		    $result=&mt('Due').' '.&Apache::lonnavmaps::timeToHumanString($date);
  101: 		}
  102: 	    } elsif ($target eq 'tex') {
  103: 	    }
  104: 	}
  105:      
  106:     } 
  107:     if ( $target eq 'edit' ) {
  108: 	$result=&Apache::edit::tag_start($target,$token);
  109: 	$result.='</td></tr>';
  110: 	$result.=&Apache::edit::end_table();
  111: 	
  112:     }
  113:     return $result;
  114: }
  115: 
  116: sub end_displayduedate {
  117:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  118:     my @result;
  119:     if ($target eq 'edit') { $result[1]='no'; }
  120:     return @result;
  121: }
  122: 
  123: sub start_displaytitle {
  124:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  125:     my $result='';
  126:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
  127: 	return '';
  128:     } else {
  129: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
  130:     }
  131:     my $name=&Apache::structuretags::get_resource_name();
  132:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  133:     if ($target eq 'web') {
  134: 	$result=$name;
  135: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
  136:     } elsif ($target eq 'edit') {
  137: 	$result=&Apache::edit::tag_start($target,$token);
  138: 	$result.='</td></tr>';
  139: 	$result.=&Apache::edit::end_table();
  140:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  141: 	$name=&Apache::lonxml::latex_special_symbols($name);
  142: 	if (lc($style) !~ 'plain') { 
  143: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
  144: 	} else {
  145: 	    $result=$name;
  146: 	}
  147:     }
  148:     return $result;
  149: }
  150: 
  151: sub end_displaytitle {
  152:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  153:     my @result;
  154:     if ($target eq 'edit') { $result[1]='no'; }
  155:     return @result;
  156: }
  157: 
  158: sub multipart {
  159:     my ($uri)=@_;
  160:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
  161:     my @parts;
  162:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
  163:     foreach (split(/\,/,$metadata)) {
  164: 	if ($_ =~ /^part_(.*)$/) {
  165: 	    my $part = $1;
  166: 	    if ($part ne '0') { push(@parts,$part); }
  167: 	}
  168:     }
  169:     return @parts;
  170: }
  171: 
  172: sub start_displayweight {
  173:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  174:     my $result;
  175:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
  176: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
  177: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
  178: 	    return '';
  179: 	}
  180:     }
  181:     push(@{$Apache::outputtags::showonce{'displayweight'}},
  182: 	 $Apache::inputtags::part);
  183:     if ($target eq 'web' || $target eq 'tex') {
  184: 	my $id = $Apache::inputtags::part;
  185: 	if ($id ne '0') {
  186: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  187: 	    if (!defined($weight) || ($weight eq '')) { $weight=1; }
  188: 	    $result.=$weight;
  189: 	} else {
  190: 	    my @parts=&multipart($env{'request.uri'});
  191: 	    my $weight;
  192: 	    if (@parts) {
  193: 	        foreach my $part (@parts) {
  194: 		    my $pweight=&Apache::lonnet::EXT("resource.$part.weight");
  195: 		    if (!defined($pweight) || ($pweight eq '')) { $pweight=1; }
  196: 		    $weight+=$pweight;
  197: 	        }
  198: 	    } else {
  199: 		$weight = &Apache::lonnet::EXT("resource.$id.weight");
  200:                 if (!defined($weight) || ($weight eq '')) { $weight=1; }
  201: 	    }
  202: 	    $result=$weight;
  203: 	}
  204:     } elsif ( $target eq 'edit' ) {
  205: 	$result=&Apache::edit::tag_start($target,$token);
  206: 	$result.='</td></tr>';
  207: 	$result.=&Apache::edit::end_table();
  208:     }
  209:     return $result;
  210: }
  211: 
  212: sub end_displayweight {
  213:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  214:     my @result;
  215:     if ($target eq 'edit') { $result[1]='no'; }
  216:     return @result;
  217: }
  218: 
  219: sub start_displaystudentphoto {
  220:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  221:     my $result;
  222:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
  223:     if ($target eq 'web' && $user eq $env{'user.name'}) {
  224: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
  225: 	my $args;
  226: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
  227: 	if ($width) { $args.=" width=\"$width\" "; }
  228: 	my $height=&Apache::lonxml::get_param('heigth',$parstack,$safeeval);
  229: 	if ($height) { $args.=" height=\"$height\" "; }
  230: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
  231: 	if ($align) { $args.=" align=\"$align\" "; }
  232: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
  233:     }
  234:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
  235: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
  236: 	my $ua=new LWP::UserAgent;
  237: 	my $request=new HTTP::Request('GET',$url);
  238: 	my $response=$ua->request($request);
  239: 	if ($response->is_success) {
  240: 	    my $file=$user."_".$domain."_studentphoto.eps";
  241: 	    open(FILE,">".$Apache::lonnet::perlvar{'lonPrtDir'}."/$file");
  242: 	    print FILE $response->content;
  243: 	    close(FILE);
  244: 	    my $width_param=&Apache::londefdef::image_size($Apache::lonnet::perlvar{'lonPrtDir'}."/$file",'0.3',$parstack,$safeeval);
  245: 	    $result.=' \graphicspath{{'.$Apache::lonnet::perlvar{'lonPrtDir'}.
  246: 		'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
  247: 	} else {
  248: 	    $result="$user\@$domain";
  249: 	}
  250:     }
  251:     return $result;
  252: }
  253: 
  254: sub end_displaystudentphoto {
  255:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  256:     my @result;
  257:     if ($target eq 'edit') { $result[1]='no'; }
  258:     return @result;
  259: }
  260: 
  261: 1;
  262: __END__

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