1: # The LearningOnline Network with CAPA
2: # tags that create controlled output
3: #
4: # $Id: outputtags.pm,v 1.41 2005/11/03 11:12:37 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, remeber it.
61:
62: push (@{$Apache::outputtags::showonce{'displayduedate'}},
63: $Apache::inputtags::part);
64:
65: my $status=$Apache::inputtags::status['-1'];
66: &Apache::lonxml::debug("got a $status in duedatebox");
67: my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
68: if (($status =~ /CAN.*_ANSWER/) && ($target eq 'web')) {
69: my $id = $Apache::inputtags::part;
70: my $date = &Apache::lonnet::EXT("resource.$id.duedate");
71: &Apache::lonxml::debug("duedatebox found $date for $id");
72:
73: # Only show the due date if the current date is
74: # different from due date of the previous part. I think
75: # this is probably the best way to avoid due date clutter.
76:
77: my $showduedate = 1;
78: my $part_count = scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
79: if ($part_count > 1) {
80: my $prev_part_id = $Apache::outputtags::showonce{'displayduedate'}->[$part_count-2];
81: my $prev_due_date = &Apache::lonnet::EXT("resource.$prev_part_id.duedate");
82: if ($prev_due_date == $date) {
83: $showduedate = 0;
84: }
85: }
86:
87: if ($showduedate) {
88: if (lc($style) !~ 'plain') {
89: $result ='<table border="on"><tr><td>Due '.
90: &Apache::lonnavmaps::timeToHumanString($date).
91: '</td></tr></table>';
92: } else {
93: $result=&mt('Due').' '.&Apache::lonnavmaps::timeToHumanString($date);
94: }
95: }
96: } elsif ( $target eq 'edit' ) {
97: $result=&Apache::edit::tag_start($target,$token);
98: $result.='</td></tr>';
99: $result.=&Apache::edit::end_table();
100: } elsif {$target eq 'tex'} {
101: # print target.
102: $result =''; # Stubbed off for now.
103: }
104: return $result;
105: }
106:
107: sub end_displayduedate {
108: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
109: my @result;
110: if ($target eq 'edit') { $result[1]='no'; }
111: return @result;
112: }
113:
114: sub start_displaytitle {
115: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
116: my $result='';
117: if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
118: return '';
119: } else {
120: $Apache::outputtags::showonce{'displayduetitle'}=1;
121: }
122: my $name=&Apache::structuretags::get_resource_name();
123: my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
124: if ($target eq 'web') {
125: $result=$name;
126: if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
127: } elsif ($target eq 'edit') {
128: $result=&Apache::edit::tag_start($target,$token);
129: $result.='</td></tr>';
130: $result.=&Apache::edit::end_table();
131: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
132: $name=&Apache::lonxml::latex_special_symbols($name);
133: if (lc($style) !~ 'plain') {
134: $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
135: } else {
136: $result=$name;
137: }
138: }
139: return $result;
140: }
141:
142: sub end_displaytitle {
143: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
144: my @result;
145: if ($target eq 'edit') { $result[1]='no'; }
146: return @result;
147: }
148:
149: sub multipart {
150: my ($uri)=@_;
151: if (!defined($uri)) { $uri=$env{'request.uri'}; }
152: my @parts;
153: my $metadata = &Apache::lonnet::metadata($uri,'packages');
154: foreach (split(/\,/,$metadata)) {
155: if ($_ =~ /^part_(.*)$/) {
156: my $part = $1;
157: if ($part ne '0') { push(@parts,$part); }
158: }
159: }
160: return @parts;
161: }
162:
163: sub start_displayweight {
164: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
165: my $result;
166: if (exists($Apache::outputtags::showonce{'displayweight'})) {
167: if(grep(/^\Q$Apache::inputtags::part\E$/,
168: @{$Apache::outputtags::showonce{'displayweight'}})) {
169: return '';
170: }
171: }
172: push(@{$Apache::outputtags::showonce{'displayweight'}},
173: $Apache::inputtags::part);
174: if ($target eq 'web' || $target eq 'tex') {
175: my $id = $Apache::inputtags::part;
176: if ($id ne '0') {
177: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
178: if (!defined($weight) || ($weight eq '')) { $weight=1; }
179: $result.=$weight;
180: } else {
181: my @parts=&multipart($env{'request.uri'});
182: my $weight;
183: if (@parts) {
184: foreach my $part (@parts) {
185: my $pweight=&Apache::lonnet::EXT("resource.$part.weight");
186: if (!defined($pweight) || ($pweight eq '')) { $pweight=1; }
187: $weight+=$pweight;
188: }
189: } else {
190: $weight = &Apache::lonnet::EXT("resource.$id.weight");
191: if (!defined($weight) || ($weight eq '')) { $weight=1; }
192: }
193: $result=$weight;
194: }
195: } elsif ( $target eq 'edit' ) {
196: $result=&Apache::edit::tag_start($target,$token);
197: $result.='</td></tr>';
198: $result.=&Apache::edit::end_table();
199: }
200: return $result;
201: }
202:
203: sub end_displayweight {
204: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
205: my @result;
206: if ($target eq 'edit') { $result[1]='no'; }
207: return @result;
208: }
209:
210: sub start_displaystudentphoto {
211: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
212: my $result;
213: my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
214: if ($target eq 'web' && $user eq $env{'user.name'}) {
215: my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
216: my $args;
217: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
218: if ($width) { $args.=" width=\"$width\" "; }
219: my $height=&Apache::lonxml::get_param('heigth',$parstack,$safeeval);
220: if ($height) { $args.=" height=\"$height\" "; }
221: my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
222: if ($align) { $args.=" align=\"$align\" "; }
223: $result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
224: }
225: if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
226: my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
227: my $ua=new LWP::UserAgent;
228: my $request=new HTTP::Request('GET',$url);
229: my $response=$ua->request($request);
230: if ($response->is_success) {
231: my $file=$user."_".$domain."_studentphoto.eps";
232: open(FILE,">".$Apache::lonnet::perlvar{'lonPrtDir'}."/$file");
233: print FILE $response->content;
234: close(FILE);
235: my $width_param=&Apache::londefdef::image_size($Apache::lonnet::perlvar{'lonPrtDir'}."/$file",'0.3',$parstack,$safeeval);
236: $result.=' \graphicspath{{'.$Apache::lonnet::perlvar{'lonPrtDir'}.
237: '}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
238: } else {
239: $result="$user\@$domain";
240: }
241: }
242: return $result;
243: }
244:
245: sub end_displaystudentphoto {
246: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
247: my @result;
248: if ($target eq 'edit') { $result[1]='no'; }
249: return @result;
250: }
251:
252: 1;
253: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>