1: # The LearningOnline Network with CAPA
2: # tags that create controlled output
3: #
4: # $Id: outputtags.pm,v 1.55 2009/05/14 10:57:40 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:
30:
31: package Apache::outputtags;
32:
33: use strict;
34: use Apache::lonlocal;
35: use Apache::lonnet;
36: use POSIX qw(strftime);
37:
38: BEGIN {
39: &Apache::lonxml::register('Apache::outputtags',('displayduedate','displaytitle','displayweight','displaystudentphoto'));
40: }
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 = &Apache::lonhomework::due_date($id);
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: my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
95: if ($target eq 'web') {
96: if (lc($style) !~ 'plain') {
97: $result ='<table border="on"><tr><td>Due '.$duetext.'</td></tr></table>';
98: } else {
99: $result=&mt('Due').' '.$duetext;
100: }
101: } elsif ($target eq 'tex') {
102: # For TeX we'll make the duedate tag work exactly like the
103: # duedate tag for web.
104:
105: my $duetext = &Apache::lonnavmaps::timeToHumanString($date, '', $format);
106: if (lc($style) !~ 'plain') {
107: # The due date will be put in a box.
108: # at the start of the line to ensure it won't overlap
109: # the 1 col boundary.
110:
111: $result = '\\\\ \framebox{'
112: .&mt('Due').' '.$duetext.'}';
113: } else {
114: $result = &mt('Due') . ' '.$duetext;
115: }
116: }
117: }
118:
119: }
120: if ( $target eq 'edit' ) {
121: $result=&Apache::edit::tag_start($target,$token);
122: $result.='</td></tr>';
123: $result.=&Apache::edit::end_table();
124:
125: }
126: return $result;
127: }
128:
129: sub end_displayduedate {
130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
131: my @result;
132: if ($target eq 'edit') { $result[1]='no'; }
133: return @result;
134: }
135:
136: sub start_displaytitle {
137: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
138: my $result='';
139: if (exists($Apache::outputtags::showonce{'displayduetitle'})) {
140: return '';
141: } else {
142: $Apache::outputtags::showonce{'displayduetitle'}=1;
143: }
144: my $name=&Apache::structuretags::get_resource_name();
145: my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval);
146: if ($target eq 'web') {
147: $result=$name;
148: if (lc($style) !~ 'plain') { $result="<h1>$name</h1>"; }
149: } elsif ($target eq 'edit') {
150: $result=&Apache::edit::tag_start($target,$token);
151: $result.='</td></tr>';
152: $result.=&Apache::edit::end_table();
153: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
154: $name=&Apache::lonxml::latex_special_symbols($name);
155: if (lc($style) !~ 'plain') {
156: $result='\vskip 0 mm\noindent\textbf{'.$name.'}\vskip 0 mm';
157: } else {
158: $result=$name;
159: }
160: }
161: return $result;
162: }
163:
164: sub end_displaytitle {
165: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
166: my @result;
167: if ($target eq 'edit') { $result[1]='no'; }
168: return @result;
169: }
170:
171: sub multipart {
172: my ($uri)=@_;
173: if (!defined($uri)) { $uri=$env{'request.uri'}; }
174: my ($symb)=&Apache::lonnet::whichuser();
175:
176: my @parts;
177: my $metadata = &Apache::lonnet::metadata($uri,'packages');
178: foreach (split(/\,/,$metadata)) {
179: if ($_ =~ /^part_(.*)$/) {
180: my $part = $1;
181: if ($part ne '0'
182: && !&Apache::loncommon::check_if_partid_hidden($part,
183: $symb)) {
184: push(@parts,$part);
185: }
186: }
187: }
188: return @parts;
189: }
190:
191: sub start_displayweight {
192: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
193: my $result;
194: if (exists($Apache::outputtags::showonce{'displayweight'})) {
195: if(grep(/^\Q$Apache::inputtags::part\E$/,
196: @{$Apache::outputtags::showonce{'displayweight'}})) {
197: return '';
198: }
199: }
200: push(@{$Apache::outputtags::showonce{'displayweight'}},
201: $Apache::inputtags::part);
202: if ($target eq 'web' || $target eq 'tex') {
203: my $id = $Apache::inputtags::part;
204: if ($id ne '0') {
205: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
206: if (!defined($weight) || ($weight eq '')) { $weight=1; }
207: $result.=$weight;
208: } else {
209: my @parts=&multipart($env{'request.uri'});
210: my $weight;
211: if (@parts) {
212: foreach my $part (@parts) {
213: my $pweight=&Apache::lonnet::EXT("resource.$part.weight");
214: if (!defined($pweight) || ($pweight eq '')) { $pweight=1; }
215: $weight+=$pweight;
216: }
217: } else {
218: $weight = &Apache::lonnet::EXT("resource.$id.weight");
219: if (!defined($weight) || ($weight eq '')) { $weight=1; }
220: }
221: $result=$weight;
222: }
223: } elsif ( $target eq 'edit' ) {
224: $result=&Apache::edit::tag_start($target,$token);
225: $result.='</td></tr>';
226: $result.=&Apache::edit::end_table();
227: }
228: return $result;
229: }
230:
231: sub end_displayweight {
232: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
233: my @result;
234: if ($target eq 'edit') { $result[1]='no'; }
235: return @result;
236: }
237:
238: sub start_displaystudentphoto {
239: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
240: my $result;
241: my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
242: if ($target eq 'web' && $user eq $env{'user.name'}) {
243: my $url=&Apache::lonnet::studentphoto($domain,$user,"gif");
244: my $args;
245: my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
246: if ($width) { $args.=" width=\"$width\" "; }
247: my $height=&Apache::lonxml::get_param('heigth',$parstack,$safeeval);
248: if ($height) { $args.=" height=\"$height\" "; }
249: my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval);
250: if ($align) { $args.=" align=\"$align\" "; }
251: $result.=" <img $args src=\"$url\" alt=\"$user\@$domain\" />";
252: }
253: if ($target eq 'tex' && $env{'request.role'} =~ /^cc/) {
254: my $url=&Apache::lonnet::studentphoto($domain,$user,"eps");
255: my $ua=new LWP::UserAgent;
256: my $request=new HTTP::Request('GET',$url);
257: my $response=$ua->request($request);
258: if ($response->is_success) {
259: my $file=$user."_".$domain."_studentphoto.eps";
260: open(FILE,">".$Apache::lonnet::perlvar{'lonPrtDir'}."/$file");
261: print FILE $response->content;
262: close(FILE);
263: my $width_param=&Apache::londefdef::image_size($Apache::lonnet::perlvar{'lonPrtDir'}."/$file",'0.3',$parstack,$safeeval);
264: $result.=' \graphicspath{{'.$Apache::lonnet::perlvar{'lonPrtDir'}.
265: '}}\includegraphics[width='.$width_param.' mm]{'.$file.'} ';
266: } else {
267: $result="$user\@$domain";
268: }
269: }
270: return $result;
271: }
272:
273: sub end_displaystudentphoto {
274: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
275: my @result;
276: if ($target eq 'edit') { $result[1]='no'; }
277: return @result;
278: }
279:
280: 1;
281: __END__
282:
283:
284: =head1 NAME
285:
286: Apache::outputtags;
287:
288: =head1 SYNOPSIS
289:
290: Handles tags associated with output. Seems to
291: relate to due dates of the assignment.
292:
293: This is part of the LearningOnline Network with CAPA project
294: described at http://www.lon-capa.org.
295:
296: =head1 SUBROUTINES
297:
298: =over
299:
300: =item start_displayduedate()
301:
302: =item initialize_outputtags()
303:
304: Empties the hash of tags that have already been displayed that should only be displayed once.
305:
306: =item end_displayduedate()
307:
308: =item start_displaytitle()
309:
310: =item end_displaytitle()
311:
312: =item multipart()
313:
314: =item start_displayweight()
315:
316: =item end_displayweight()
317:
318: =item start_displaystudentphoto()
319:
320: =item end_displaystudentphoto()
321:
322:
323: =back
324:
325: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>