File:  [LON-CAPA] / loncom / homework / outputtags.pm
Revision 1.59: download - view: text, annotated - select for diffs
Tue May 23 03:07:34 2017 UTC (6 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Replace use of LWP::UserAgent with LONCAPA::LWPReq so replication of
  content from /raw/ can include verification of the requesting server's
  client certificate (Apache/SSL used).

    1: # The LearningOnline Network with CAPA 
    2: # tags that create controlled output
    3: #
    4: # $Id: outputtags.pm,v 1.59 2017/05/23 03:07:34 raeburn 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: 
   30: 
   31: package Apache::outputtags; 
   32: 
   33: use strict;
   34: use Apache::lonlocal;
   35: use Apache::lonnet;
   36: use LONCAPA::LWPReq;
   37: use POSIX qw(strftime);
   38: 
   39: BEGIN {
   40:     &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
   41: }
   42: 
   43: ################################ utilities ###########################
   44: 
   45: # 
   46: #  Does a simple substitution of a tab when the opening tag can
   47: #  be replaced by a fixed string.. and same for the closing tag.
   48: #  Parameters:
   49: #    $input   - String in in which to do the substitutions.
   50: #    $tag     - name of tag without the <>'s e.g. sub for <sub></sub>
   51: #    $opening - What to replace <$tag> with
   52: #    $closing - What to replace </$tag> with.
   53: #  Returns:
   54: #    Input string appropriately substituted.
   55: #
   56: sub substitute_tag {
   57:     my ($input,
   58: 	$tag,
   59: 	$opening,
   60: 	$closing) = @_;
   61: 
   62:     $input =~ s/<$tag>/$opening/gi;
   63:     $input =~ s/<\/$tag>/$closing/gi;
   64: 
   65:     return $input;
   66: }  
   67: 
   68: #
   69: #  Substitutes the simple formatting tags in a string
   70: #  Parameters:
   71: #    $string - input string.
   72: #  Returns
   73: #    Result of string after simple substitutions
   74: #  Tags we handle are:
   75: #    <sub>, <sup> <em>
   76: #
   77: sub substitute_simple_tags_latex {
   78:     my ($string)  = @_;
   79: 
   80:     # restore the <>'s:
   81: 
   82:     $string =~ s/\\ensuremath\{<}/</g;
   83:     $string =~ s/\\ensuremath\{>}/>/g;
   84: 
   85: 
   86:     # Substitute the tags:
   87: 
   88:     $string = &substitute_tag($string, "sub", '\ensuremath{_', '}');
   89:     $string = &substitute_tag($string, 'sup', '\ensuremath{^', '}');
   90:     $string = &substitute_tag($string, 'em', '\em{', '}');
   91: 
   92: 
   93:     # Escape the remaining <>'s again:
   94: 
   95:     $string =~ s/</\\ensuremath{<}/g;
   96:     $string =~ s/>/\\ensuremath{>}/g;
   97: 
   98: 
   99: 
  100:     return $string;
  101: }
  102: 
  103: ################################ The parser ##########################
  104: 
  105: sub initialize_outputtags {
  106:     %Apache::outputtags::showonce=();
  107: }
  108: 
  109: 
  110: 
  111: sub start_displayduedate {
  112:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  113:     my $result;
  114: 
  115:     # Different parts can have different due dates... so we keep a list
  116:     # of the parts for which we've displayed the duedate:
  117: 
  118:     if (exists($Apache::outputtags::showonce{'displayduedate'})) {
  119: 	if (grep(/^\Q$Apache::inputtags::part\E$/,
  120: 		 @{$Apache::outputtags::showonce{'displayduedate'}})) {
  121: 	    return '';		# Already shown the duedate for this part.
  122: 	}
  123:     }
  124:     # since we will show the duedate for this part, remember it.
  125: 
  126:     push (@{$Apache::outputtags::showonce{'displayduedate'}},
  127: 	  $Apache::inputtags::part);
  128: 
  129:     #  Determine the due date format:
  130:     #
  131:     my $status=$Apache::inputtags::status['-1'];
  132:     &Apache::lonxml::debug("got a $status in duedatebox");
  133:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  134:     my $format = &Apache::lonxml::get_param('format', $parstack, $safeeval);
  135:     if (!$format) {
  136: 	$format = undef;
  137:     }
  138:     if (($status =~ /CAN.*_ANSWER/)) {
  139: 	my $id = $Apache::inputtags::part;
  140: 	my $date = &Apache::lonhomework::due_date($id);
  141: 	&Apache::lonxml::debug("duedatebox found $date for $id");
  142: 
  143: 	# Only show the due date if the current date is 
  144: 	# different from due date of the previous part.  I think
  145: 	# this is probably the best way to avoid due date clutter.
  146: 
  147:        	my $showduedate = 1;
  148: 	my $part_count  = scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
  149: 	if ($part_count > 1) {
  150: 	    my $prev_part_id = $Apache::outputtags::showonce{'displayduedate'}->[$part_count-2];
  151: 	    my $prev_due_date = &Apache::lonnet::EXT("resource.$prev_part_id.duedate");
  152: 	    if ($prev_due_date == $date) {
  153: 		$showduedate = 0;
  154: 	    }
  155: 	}
  156: 
  157: 	if ($showduedate) {
  158: 	    my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
  159: 	    if ($target eq 'web') {
  160: 		if (lc($style) !~ 'plain') { 
  161: 		    $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
  162: 		} else {
  163: 		    $result=&mt('Due').' '.$duetext;
  164: 		}
  165: 	    } elsif ($target eq 'tex') {
  166: 		# For TeX we'll make the duedate tag work exactly like the 
  167: 		# duedate tag for web.
  168: 
  169: 		my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
  170: 		if (lc($style) !~ 'plain') {
  171: 		    # The due date will be put in a box.
  172: 		    # at the start of the line to ensure it won't overlap
  173: 		    # the 1 col boundary.
  174: 
  175: 		    $result = '\vspace{1.0 ex} \framebox{'
  176:     			       .&mt('Due').' '.$duetext.'}';
  177: 		} else {
  178: 		    $result = &mt('Due') . ' '.$duetext;
  179: 		}
  180: 	    }
  181: 	}
  182:      
  183:     } 
  184:     if ( $target eq 'edit' ) {
  185: 	$result=&Apache::edit::tag_start($target,$token);
  186: 	$result.='</td></tr>';
  187: 	$result.=&Apache::edit::end_table();
  188: 	
  189:     }
  190:     return $result;
  191: }
  192: 
  193: sub end_displayduedate {
  194:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  195:     my @result;
  196:     if ($target eq 'edit') { $result[1]='no'; }
  197:     return @result;
  198: }
  199: 
  200: sub start_displaytitle {
  201:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  202:     my $result='';
  203:     if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
  204: 	return '';
  205:     } else {
  206: 	$Apache::outputtags::showonce{'displayduetitle'}=1;
  207:     }
  208:     my $name=&Apache::structuretags::get_resource_name();
  209:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
  210:     if ($target eq 'web') {
  211: 	$result=$name;
  212: 	if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
  213:     } elsif ($target eq 'edit') {
  214: 	$result=&Apache::edit::tag_start($target,$token);
  215: 	$result.='</td></tr>';
  216: 	$result.=&Apache::edit::end_table();
  217:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  218: 	$name=&Apache::lonxml::latex_special_symbols($name);
  219: 	$name = &substitute_simple_tags_latex($name);
  220: 	if (lc($style) !~ 'plain') { 
  221: 	    $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
  222: 	} else {
  223: 	    $result=$name;
  224: 	}
  225:     }
  226:     return $result;
  227: }
  228: 
  229: sub end_displaytitle {
  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 multipart {
  237:     my ($uri)=@_;
  238:     if (!defined($uri)) { $uri=$env{'request.uri'}; }
  239:     my ($symb)=&Apache::lonnet::whichuser();
  240: 
  241:     my @parts;
  242:     my $metadata = &Apache::lonnet::metadata($uri,'packages');
  243:     foreach (split(/\,/,$metadata)) {
  244: 	if ($_ =~ /^part_(.*)$/) {
  245: 	    my $part = $1;
  246: 	    if ($part ne '0' 
  247: 		&& !&Apache::loncommon::check_if_partid_hidden($part, 
  248: 							       $symb)) {
  249: 		push(@parts,$part);
  250: 	    }
  251: 	}
  252:     }
  253:     return @parts;
  254: }
  255: 
  256: sub start_displayweight {
  257:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  258:     my $result;
  259:     if (exists($Apache::outputtags::showonce{'displayweight'})) {
  260: 	if(grep(/^\Q$Apache::inputtags::part\E$/,
  261: 		@{$Apache::outputtags::showonce{'displayweight'}})) {
  262: 	    return '';
  263: 	}
  264:     }
  265:     push(@{$Apache::outputtags::showonce{'displayweight'}},
  266: 	 $Apache::inputtags::part);
  267:     if ($target eq 'web' || $target eq 'tex') {
  268: 	my $id = $Apache::inputtags::part;
  269: 	if ($id ne '0') {
  270: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  271: 	    if (!defined($weight) || ($weight eq '')) { $weight=1; }
  272: 	    $result.=$weight;
  273: 	} else {
  274: 	    my @parts=&multipart($env{'request.uri'});
  275: 	    my $weight;
  276: 	    if (@parts) {
  277: 	        foreach my $part (@parts) {
  278: 		    my $pweight=&Apache::lonnet::EXT("resource.$part.weight");
  279: 		    if (!defined($pweight) || ($pweight eq '')) { $pweight=1; }
  280: 		    $weight+=$pweight;
  281: 	        }
  282: 	    } else {
  283: 		$weight = &Apache::lonnet::EXT("resource.$id.weight");
  284:                 if (!defined($weight) || ($weight eq '')) { $weight=1; }
  285: 	    }
  286: 	    $result=$weight;
  287: 	}
  288:     } elsif ( $target eq 'edit' ) {
  289: 	$result=&Apache::edit::tag_start($target,$token);
  290: 	$result.='</td></tr>';
  291: 	$result.=&Apache::edit::end_table();
  292:     }
  293:     return $result;
  294: }
  295: 
  296: sub end_displayweight {
  297:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  298:     my @result;
  299:     if ($target eq 'edit') { $result[1]='no'; }
  300:     return @result;
  301: }
  302: 
  303: sub start_displaystudentphoto {
  304:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  305:     my $result;
  306:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
  307:     if ($target eq 'web' && $user eq $env{'user.name'}) {
  308: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
  309: 	my $args;
  310: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
  311: 	if ($width) { $args.=" width=\"$width\" "; }
  312: 	my $height=&Apache::lonxml::get_param('heigth',$parstack,$safeeval);
  313: 	if ($height) { $args.=" height=\"$height\" "; }
  314: 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
  315: 	if ($align) { $args.=" align=\"$align\" "; }
  316: 	$result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
  317:     }
  318:     if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
  319: 	my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
  320:         my $uhome=&Apache::lonnet::homeserver($user,$domain);
  321: 	my $request=new HTTP::Request('GET',$url);
  322:         my $response=&LONCAPA::LWPReq::makerequest($uhome,$request,'','','',1);
  323: 	my $response=$ua->request($request);
  324: 	if ($response->is_success) {
  325: 	    my $file=$user."_".$domain."_studentphoto.eps";
  326: 	    open(FILE,">".$Apache::lonnet::perlvar{'lonPrtDir'}."/$file");
  327: 	    print FILE $response->content;
  328: 	    close(FILE);
  329: 	    my $width_param=&Apache::londefdef::image_size($Apache::lonnet::perlvar{'lonPrtDir'}."/$file",'0.3',$parstack,$safeeval);
  330: 	    $result.=' \graphicspath{{'.$Apache::lonnet::perlvar{'lonPrtDir'}.
  331: 		'}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
  332: 	} else {
  333: 	    $result="$user\@$domain";
  334: 	}
  335:     }
  336:     return $result;
  337: }
  338: 
  339: sub end_displaystudentphoto {
  340:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  341:     my @result;
  342:     if ($target eq 'edit') { $result[1]='no'; }
  343:     return @result;
  344: }
  345: 
  346: 1;
  347: __END__
  348: 
  349: 
  350: =head1 NAME
  351: 
  352: Apache::outputtags;
  353: 
  354: =head1 SYNOPSIS
  355: 
  356: Handles tags associated with output. Seems to
  357: relate to due dates of the assignment.
  358: 
  359: This is part of the LearningOnline Network with CAPA project
  360: described at http://www.lon-capa.org.
  361: 
  362: =head1 SUBROUTINES
  363: 
  364: =over
  365: 
  366: =item start_displayduedate()
  367: 
  368: =item initialize_outputtags()
  369: 
  370: Empties the hash of tags that have already been displayed that should only be displayed once.
  371: 
  372: =item end_displayduedate()
  373: 
  374: =item start_displaytitle()
  375: 
  376: =item end_displaytitle()
  377: 
  378: =item multipart()
  379: 
  380: =item start_displayweight()
  381: 
  382: =item end_displayweight()
  383: 
  384: =item start_displaystudentphoto()
  385: 
  386: =item end_displaystudentphoto()
  387: 
  388: 
  389: =back
  390: 
  391: =cut

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