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