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