[system] / branches / rel-2-1-patches / webwork-modperl / lib / WeBWorK / ContentGenerator.pm Repository:
ViewVC logotype

Annotation of /branches/rel-2-1-patches/webwork-modperl/lib/WeBWorK/ContentGenerator.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2849 - (view) (download) (as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator.pm

1 : sh002i 455 ################################################################################
2 : sh002i 1663 # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : sh002i 2849 # $CVSHeader: webwork2/lib/WeBWorK/ContentGenerator.pm,v 1.116 2004/09/29 16:49:30 sh002i Exp $
5 : sh002i 1663 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 : sh002i 455 ################################################################################
16 :    
17 : malsyned 313 package WeBWorK::ContentGenerator;
18 : malsyned 305
19 : sh002i 455 =head1 NAME
20 :    
21 :     WeBWorK::ContentGenerator - base class for modules that generate page content.
22 :    
23 : sh002i 1861 =head1 SYNOPSIS
24 :    
25 :     # start with a WeBWorK::Request object: $r
26 :    
27 :     use WeBWorK::ContentGenerator::SomeSubclass;
28 :    
29 :     my $cg = WeBWorK::ContentGenerator::SomeSubclass->new($r);
30 :     my $result = $cg->go();
31 :    
32 :     =head1 DESCRIPTION
33 :    
34 : sh002i 1873 WeBWorK::ContentGenerator provides the framework for generating page content.
35 :     "Content generators" are subclasses of this class which provide content for
36 :     particular parts of the system.
37 : sh002i 1861
38 : sh002i 1873 Default versions of methods used by the templating system are provided. Several
39 :     useful methods are provided for rendering common output idioms and some
40 :     miscellaneous utilities are provided.
41 :    
42 : sh002i 455 =cut
43 :    
44 : malsyned 441 use strict;
45 :     use warnings;
46 : sh002i 1911 use Apache::Constants qw(:response);
47 : sh002i 1882 use Carp;
48 : sh002i 1880 use CGI::Pretty qw(*ul *li);
49 : sh002i 469 use URI::Escape;
50 : sh002i 1869 use WeBWorK::Template qw(template);
51 : malsyned 390
52 : gage 2323 ###############################################################################
53 : sh002i 2390
54 : sh002i 1861 =head1 CONSTRUCTOR
55 : sh002i 1838
56 : sh002i 1861 =over
57 :    
58 :     =item new($r)
59 :    
60 : sh002i 1873 Creates a new instance of a content generator. Supply a WeBWorK::Request object
61 : sh002i 1861 $r.
62 :    
63 :     =cut
64 :    
65 : sh002i 1613 sub new {
66 : sh002i 1838 my ($invocant, $r) = @_;
67 : malsyned 323 my $class = ref($invocant) || $invocant;
68 : sh002i 809 my $self = {
69 : sh002i 1838 r => $r, # this is now a WeBWorK::Request
70 :     ce => $r->ce(), # these three are here for
71 :     db => $r->db(), # backward-compatability
72 :     authz => $r->authz(), # with unconverted CGs
73 : sh002i 1869 noContent => undef, # this should get clobbered at some point
74 : sh002i 809 };
75 : malsyned 305 bless $self, $class;
76 :     return $self;
77 :     }
78 :    
79 : sh002i 1861 =back
80 :    
81 :     =cut
82 :    
83 : sh002i 469 ################################################################################
84 : malsyned 323
85 : sh002i 1861 =head1 INVOCATION
86 :    
87 :     =over
88 :    
89 :     =item go()
90 :    
91 : sh002i 1873 Generates a page, using methods from the particular subclass of ContentGenerator
92 :     that is instantiated. Generatoion is broken up into several steps, to give
93 :     subclasses ample control over the process.
94 : sh002i 1861
95 :     =over
96 :    
97 : sh002i 1873 =item 1
98 : sh002i 1861
99 : sh002i 1873 go() will attempt to call the method pre_header_initialize(). This method may be
100 :     implemented in subclasses which must do processing before the HTTP header is
101 :     emitted.
102 : sh002i 1861
103 : sh002i 1873 =item 2
104 : sh002i 1861
105 : sh002i 1873 go() will attempt to call the method header(). This method emits the HTTP
106 :     header. It is defined in this class (see below), but may be overridden in
107 :     subclasses which need to send different header information. For some reason, the
108 :     return value of header() will be used as the result of this function, if it is
109 :     defined.
110 : sh002i 1861
111 : sh002i 1911 FIXME: figure out what the deal is with the return value of header(). If we sent
112 :     a header, it's too late to set the status by returning. If we didn't, header()
113 :     didn't perform its function!
114 :    
115 : sh002i 1873 =item 3
116 : sh002i 1861
117 : sh002i 1873 At this point, go() will terminate if the request is a HEAD request or if the
118 :     field $self->{noContent} contains a true value.
119 : sh002i 1861
120 : sh002i 1911 FIXME: I don't think we'll need noContent after reply_with_redirect() is
121 :     adopted by all modules.
122 :    
123 : sh002i 1873 =item 4
124 : sh002i 1861
125 : sh002i 1873 go() then attempts to call the method initialize(). This method may be
126 :     implemented in subclasses which must do processing after the HTTP header is sent
127 :     but before any content is sent.
128 : sh002i 1861
129 : sh002i 1873 =item 6
130 :    
131 :     The method content() is called to send the page content to client.
132 :    
133 : sh002i 1861 =back
134 :    
135 :     =cut
136 :    
137 : sh002i 469 sub go {
138 : sh002i 1873 my ($self) = @_;
139 :     my $r = $self->r;
140 : sh002i 1869 my $ce = $r->ce;
141 :    
142 : malsyned 1408 my $returnValue = OK;
143 : sh002i 1131
144 : sh002i 469 $self->pre_header_initialize(@_) if $self->can("pre_header_initialize");
145 : sh002i 1873
146 : sh002i 1911 # send a file instead of a normal reply (reply_with_file() sets this field)
147 :     defined $self->{reply_with_file} and do {
148 :     return $self->do_reply_with_file($self->{reply_with_file});
149 :     };
150 :    
151 :     # send a Location: header instead of a normal reply (reply_with_redirect() sets this field)
152 :     defined $self->{reply_with_redirect} and do {
153 :     return $self->do_reply_with_redirect($self->{reply_with_redirect});
154 :     };
155 :    
156 : malsyned 1408 my $headerReturn = $self->header(@_);
157 :     $returnValue = $headerReturn if defined $headerReturn;
158 : sh002i 1911 # FIXME: we won't need noContent after reply_with_redirect() is adopted
159 : malsyned 1408 return $returnValue if $r->header_only or $self->{noContent};
160 : malsyned 313
161 : sh002i 1873 $self->initialize() if $self->can("initialize");
162 : sh002i 1225
163 : sh002i 1873 $self->content();
164 : malsyned 1408
165 :     return $returnValue;
166 : sh002i 469 }
167 :    
168 : sh002i 1873 =item r()
169 : sh002i 1861
170 : sh002i 1873 Returns a reference to the WeBWorK::Request object associated with this
171 :     instance.
172 : sh002i 1861
173 :     =cut
174 :    
175 : sh002i 1873 sub r {
176 :     my ($self) = @_;
177 : sh002i 1869
178 : sh002i 1873 return $self->{r};
179 : sh002i 469 }
180 :    
181 : sh002i 1911 =item do_reply_with_file($fileHash)
182 :    
183 :     Handler for reply_with_file(), used by go(). DO NOT CALL THIS METHOD DIRECTLY.
184 :    
185 :     =cut
186 :    
187 :     sub do_reply_with_file {
188 :     my ($self, $fileHash) = @_;
189 :     my $r = $self->r;
190 :    
191 : sh002i 1984 my $type = $fileHash->{type};
192 : sh002i 1911 my $source = $fileHash->{source};
193 : sh002i 1984 my $name = $fileHash->{name};
194 :     my $delete_after = $fileHash->{delete_after};
195 : sh002i 1911
196 :     # if there was a problem, we return here and let go() worry about sending the reply
197 :     return NOT_FOUND unless -e $source;
198 :     return FORBIDDEN unless -r $source;
199 :    
200 :     # open the file now, so we can send the proper error status is we fail
201 :     open my $fh, "<", $source or return SERVER_ERROR;
202 :    
203 :     # send our custom HTTP header
204 :     $r->content_type($type);
205 :     $r->header_out("Content-Disposition" => "attachment; filename=\"$name\"");
206 :     $r->send_http_header;
207 :    
208 :     # send the file
209 :     $r->send_fd($fh);
210 :    
211 :     # close the file and go home
212 :     close $fh;
213 : sh002i 1984
214 :     if ($delete_after) {
215 :     unlink $source or warn "failed to unlink $source after sending: $!";
216 :     }
217 : sh002i 1911 }
218 :    
219 : sh002i 2030 =item do_reply_with_redirect($url)
220 :    
221 :     Handler for reply_with_redirect(), used by go(). DO NOT CALL THIS METHOD DIRECTLY.
222 :    
223 :     =cut
224 :    
225 :     sub do_reply_with_redirect {
226 :     my ($self, $url) = @_;
227 :     my $r = $self->r;
228 :    
229 :     $r->status(REDIRECT);
230 :     $r->header_out(Location => $url);
231 :     $r->send_http_header();
232 :     }
233 :    
234 :     =back
235 :    
236 :     =cut
237 :    
238 :     ################################################################################
239 :    
240 :     =head1 DATA MODIFIERS
241 :    
242 :     Modifiers allow the caller to register a piece of data for later retrieval in a
243 :     standard way.
244 :    
245 :     =over
246 :    
247 :     =item reply_with_file($type, $source, $name, $delete_after)
248 :    
249 :     Enables file sending mode, causing go() to send the file specified by $source to
250 :     the client after calling pre_header_initialize(). The content type sent is
251 :     $type, and the suggested client-side file name is $name. If $delete_after is
252 :     true, $source is deleted after it is sent.
253 :    
254 :     Must be called before the HTTP header is sent. Usually called from
255 :     pre_header_initialize().
256 :    
257 :     =cut
258 :    
259 :     sub reply_with_file {
260 :     my ($self, $type, $source, $name, $delete_after) = @_;
261 :     $delete_after ||= "";
262 :    
263 :     $self->{reply_with_file} = {
264 :     type => $type,
265 :     source => $source,
266 :     name => $name,
267 :     delete_after => $delete_after,
268 :     };
269 :     }
270 :    
271 : sh002i 1911 =item reply_with_redirect($url)
272 :    
273 :     Enables redirect mode, causing go() to redirect to the given URL after calling
274 :     pre_header_initialize().
275 :    
276 : sh002i 2030 Must be called before the HTTP header is sent. Usually called from
277 :     pre_header_initialize().
278 :    
279 : sh002i 1911 =cut
280 :    
281 :     sub reply_with_redirect {
282 :     my ($self, $url) = @_;
283 :    
284 :     $self->{reply_with_redirect} = $url;
285 :     }
286 :    
287 : sh002i 2030 =item addmessage($message)
288 : sh002i 1911
289 : sh002i 2030 Adds a message to the list of messages to be printed by the message() template
290 :     escape handler.
291 : sh002i 1911
292 : sh002i 2030 Must be called before the message() template escape is invoked.
293 :    
294 : sh002i 1911 =cut
295 :    
296 : sh002i 2030 # FIXME: we should probably
297 :    
298 :     sub addmessage {
299 :     my ($self, $message) = @_;
300 : sh002i 2034 $self->{status_message} .= $message;
301 : sh002i 1911 }
302 :    
303 : jj 2140 =item addgoodmessage($message)
304 :    
305 :     Adds a success message to the list of messages to be printed by the
306 :     message() template escape handler.
307 :    
308 :     =cut
309 :    
310 :    
311 :     sub addgoodmessage {
312 :     my ($self, $message) = @_;
313 :     $self->addmessage(CGI::div({class=>"ResultsWithoutError"}, $message));
314 :     }
315 :    
316 : jj 2152 =item addbadmessage($message)
317 :    
318 :     Adds a failure message to the list of messages to be printed by the
319 :     message() template escape handler.
320 :    
321 :     =cut
322 :    
323 :    
324 :     sub addbadmessage {
325 :     my ($self, $message) = @_;
326 :     $self->addmessage(CGI::div({class=>"ResultsWithError"}, $message));
327 :     }
328 :    
329 : sh002i 1873 =back
330 : sh002i 1861
331 :     =cut
332 :    
333 : sh002i 1873 ################################################################################
334 : sh002i 469
335 : sh002i 1873 =head1 STANDARD METHODS
336 : sh002i 1861
337 : sh002i 1873 The following are the standard content generator methods. Some are defined here,
338 :     but may be overridden in a subclass. Others are not defined unless they are
339 :     defined in a subclass.
340 : sh002i 1861
341 : sh002i 1873 =over
342 : sh002i 469
343 : sh002i 1873 =item pre_header_initialize()
344 : sh002i 1861
345 : sh002i 1873 Not defined in this package.
346 : sh002i 1861
347 : sh002i 1873 May be defined by a subclass to perform any processing that must occur before
348 :     the HTTP header is sent.
349 :    
350 : sh002i 1861 =cut
351 :    
352 : sh002i 1873 #sub pre_header_initialize { }
353 : malsyned 323
354 : sh002i 1873 =item header()
355 : sh002i 1861
356 : sh002i 1873 Defined in this package.
357 : sh002i 1861
358 : sh002i 2030 Generates and sends a default HTTP header, specifying the "text/html" content
359 :     type.
360 : sh002i 1861
361 :     =cut
362 :    
363 : sh002i 1873 sub header {
364 : sh002i 425 my $self = shift;
365 : sh002i 1873 my $r = $self->r;
366 : sh002i 425
367 : sh002i 2030 $r->content_type("text/html");
368 : sh002i 1873 $r->send_http_header();
369 :     return OK;
370 : sh002i 425 }
371 :    
372 : sh002i 1873 =item initialize()
373 : sh002i 1861
374 : sh002i 1873 Not defined in this package.
375 : sh002i 1861
376 : sh002i 1873 May be defined by a subclass to perform any processing that must occur after the
377 :     HTTP header is sent but before any content is sent.
378 : sh002i 1861
379 :     =cut
380 :    
381 : sh002i 1873 #sub initialize { }
382 : sh002i 1861
383 : sh002i 1873 =item content()
384 : sh002i 1861
385 : sh002i 1873 Defined in this package.
386 : sh002i 1861
387 : sh002i 1873 Print the content of the generated page.
388 : sh002i 1861
389 : sh002i 1873 The implementation in this package uses WeBWorK::Template to define the content
390 :     of the page. See WeBWorK::Template for details.
391 : malsyned 390
392 : sh002i 1873 If a method named templateName() exists, it it called to determine the name of
393 :     the template to use. If not, the default template, "system", is used. The
394 :     location of the template is looked up in the course environment.
395 : sh002i 1861
396 :     =cut
397 :    
398 : sh002i 1873 sub content {
399 :     my ($self) = @_;
400 :     my $ce = $self->r->ce;
401 : sh002i 737
402 : sh002i 1873 # if the content generator specifies a custom template name, use that
403 :     # field in the $ce->{templates} hash instead of "system" if it exists.
404 :     my $templateName;
405 :     if ($self->can("templateName")) {
406 :     $templateName = $self->templateName;
407 :     } else {
408 :     $templateName = "system";
409 :     }
410 :     $templateName = "system" unless exists $ce->{templates}->{$templateName};
411 :     template($ce->{templates}->{$templateName}, $self);
412 : sh002i 737 }
413 :    
414 : sh002i 1861 =back
415 :    
416 :     =cut
417 :    
418 : sh002i 1873 # ------------------------------------------------------------------------------
419 : sh002i 1866
420 : sh002i 1873 =head2 Template escape handlers
421 : sh002i 1866
422 : sh002i 1873 Template escape handlers are invoked when the template processor encounters a
423 :     matching escape sequence in the template. The escapse sequence's arguments are
424 :     passed to the methods as a reference to a hash.
425 : sh002i 1866
426 : sh002i 1873 For more information, refer to WeBWorK::Template.
427 : malsyned 390
428 : sh002i 1873 The following template escapes handlers are defined here or may be defined in
429 :     subclasses. For methods that are not defined in this package, the documentation
430 :     defines the interface and behavior that any subclass implementation must follow.
431 : malsyned 313
432 : sh002i 1861 =over
433 :    
434 : sh002i 1873 =item head()
435 : sh002i 1861
436 : sh002i 1873 Not defined in this package.
437 : sh002i 1861
438 : sh002i 1873 Any tags that should appear in the HEAD of the document.
439 : sh002i 1861
440 :     =cut
441 :    
442 : sh002i 1873 #sub head { }
443 : malsyned 349
444 : sh002i 1873 =item info()
445 : sh002i 682
446 : sh002i 1873 Not defined in this package.
447 : sh002i 1861
448 : sh002i 1873 Auxiliary information related to the content displayed in the C<body>.
449 : sh002i 1861
450 : sh002i 1873 =cut
451 : sh002i 1861
452 : sh002i 1873 #sub info { }
453 : sh002i 1861
454 : sh002i 1873 =item links()
455 : sh002i 1861
456 : sh002i 1873 Defined in this package.
457 : sh002i 1861
458 : sh002i 1873 Links that should appear on every page.
459 : sh002i 1861
460 :     =cut
461 :    
462 : sh002i 1866 sub links {
463 :     my ($self) = @_;
464 : sh002i 1873 my $r = $self->r;
465 : sh002i 1866 my $db = $r->db;
466 : toenail 2356 my $authz = $r->authz;
467 : gage 2323 my $ce = $r->ce;
468 : sh002i 1866 my $urlpath = $r->urlpath;
469 : toenail 2356 my $user = $r->param('user');
470 : sh002i 1866
471 :     # we're linking to other places in the same course, so grab the courseID from the current path
472 :     my $courseID = $urlpath->arg("courseID");
473 :    
474 :     # to make things more concise
475 :     my %args = ( courseID => $courseID );
476 :     my $pfx = "WeBWorK::ContentGenerator::";
477 :    
478 : sh002i 1880 my $sets = $urlpath->newFromModule("${pfx}ProblemSets", %args);
479 :     my $options = $urlpath->newFromModule("${pfx}Options", %args);
480 :     my $grades = $urlpath->newFromModule("${pfx}Grades", %args);
481 :     my $logout = $urlpath->newFromModule("${pfx}Logout", %args);
482 :    
483 :     print "\n<!-- BEGIN " . __PACKAGE__ . "::links -->\n";
484 : sh002i 2849
485 : gage 2323 # only users with appropriate permissions can report bugs
486 : sh002i 2849 if ($authz->hasPermissions($user, "report_bugs")) {
487 :     print CGI::p(CGI::a({style=>"font-size:larger", href=>$ce->{webworkURLs}{bugReporter}}, "Report bugs")),CGI::hr();
488 :     }
489 : gage 2323
490 : sh002i 1880 print CGI::start_ul({class=>"LinksMenu"});
491 :     print CGI::li(CGI::span({style=>"font-size:larger"},
492 : sh002i 2847 CGI::a({href=>$self->systemLink($sets)}, sp2nbsp($sets->name))));
493 : sh002i 2849
494 :     if ($authz->hasPermissions($user, "change_password") or $authz->hasPermissions($user, "change_email_address")) {
495 :     print CGI::li(CGI::a({href=>$self->systemLink($options)}, sp2nbsp($options->name)));
496 :     }
497 :    
498 : sh002i 2847 print CGI::li(CGI::a({href=>$self->systemLink($grades)}, sp2nbsp($grades->name)));
499 :     print CGI::li(CGI::a({href=>$self->systemLink($logout)}, sp2nbsp($logout->name)));
500 : sh002i 1880
501 : toenail 2397 if ($authz->hasPermissions($user, "access_instructor_tools")) {
502 : sh002i 1866 my $ipfx = "${pfx}Instructor::";
503 :    
504 :     my $userID = $r->param("effectiveUser");
505 :     my $setID = $urlpath->arg("setID");
506 : toenail 2014 $setID = "" if (defined $setID && !(grep /$setID/, $db->listUserSets($userID)));
507 : sh002i 1866 my $problemID = $urlpath->arg("problemID");
508 : toenail 2014 $problemID = "" if (defined $problemID && !(grep /$problemID/, $db->listUserProblems($userID, $setID)));
509 : sh002i 1866
510 :     my $instr = $urlpath->newFromModule("${ipfx}Index", %args);
511 : sh002i 1918 my $addUsers = $urlpath->newFromModule("${ipfx}AddUsers", %args);
512 : sh002i 1866 my $userList = $urlpath->newFromModule("${ipfx}UserList", %args);
513 :    
514 :     # set list links
515 :     my $setList = $urlpath->newFromModule("${ipfx}ProblemSetList", %args);
516 : toenail 2804 my $setDetail = $urlpath->newFromModule("${ipfx}ProblemSetDetail", %args, setID => $setID);
517 : sh002i 1866 my $problemEditor = $urlpath->newFromModule("${ipfx}PGProblemEditor", %args, setID => $setID, problemID => $problemID);
518 :    
519 : jj 1998 my $maker = $urlpath->newFromModule("${ipfx}SetMaker", %args);
520 : sh002i 1918 my $assigner = $urlpath->newFromModule("${ipfx}Assigner", %args);
521 : sh002i 1866 my $mail = $urlpath->newFromModule("${ipfx}SendMail", %args);
522 :     my $scoring = $urlpath->newFromModule("${ipfx}Scoring", %args);
523 :    
524 :     # statistics links
525 :     my $stats = $urlpath->newFromModule("${ipfx}Stats", %args);
526 :     my $userStats = $urlpath->newFromModule("${ipfx}Stats", %args, statType => "student", userID => $userID);
527 :     my $setStats = $urlpath->newFromModule("${ipfx}Stats", %args, statType => "set", setID => $setID);
528 : apizer 2161
529 :     # progress links
530 :     my $progress = $urlpath->newFromModule("${ipfx}StudentProgress", %args);
531 :     my $userProgress = $urlpath->newFromModule("${ipfx}StudentProgress", %args, statType => "student", userID => $userID);
532 :     my $setProgress = $urlpath->newFromModule("${ipfx}StudentProgress", %args, statType => "set", setID => $setID);
533 : sh002i 1866
534 : apizer 2161
535 : sh002i 1866 my $files = $urlpath->newFromModule("${ipfx}FileXfer", %args);
536 :    
537 : jj 1998 print CGI::hr();
538 : sh002i 1880 print CGI::start_li();
539 : gage 2485 print CGI::span({style=>"font-size:larger"},
540 :     CGI::a({href=>$self->systemLink($instr)}, space2nbsp($instr->name))
541 :     );
542 : sh002i 1880 print CGI::start_ul();
543 : sh002i 2847 #print CGI::li(CGI::a({href=>$self->systemLink($addUsers)}, sp2nbsp($addUsers->name))) if $authz->hasPermissions($user, "modify_student_data");
544 :     print CGI::li(CGI::a({href=>$self->systemLink($userList)}, sp2nbsp($userList->name)));
545 : sh002i 1880 print CGI::start_li();
546 : sh002i 2847 print CGI::a({href=>$self->systemLink($setList)}, sp2nbsp($setList->name));
547 : sh002i 1869 if (defined $setID and $setID ne "") {
548 : sh002i 1880 print CGI::start_ul();
549 :     print CGI::start_li();
550 :     print CGI::a({href=>$self->systemLink($setDetail)}, $setID);
551 : sh002i 1869 if (defined $problemID and $problemID ne "") {
552 : sh002i 1880 print CGI::ul(
553 : sh002i 1866 CGI::li(CGI::a({href=>$self->systemLink($problemEditor)}, $problemID))
554 :     );
555 :     }
556 : sh002i 1880 print CGI::end_li();
557 :     print CGI::end_ul();
558 : sh002i 1866 }
559 : sh002i 1880 print CGI::end_li();
560 : sh002i 2847 print CGI::li(CGI::a({href=>$self->systemLink($maker)}, sp2nbsp($maker->name))) if $authz->hasPermissions($user, "modify_problem_sets");
561 :     print CGI::li(CGI::a({href=>$self->systemLink($assigner)}, sp2nbsp($assigner->name))) if $authz->hasPermissions($user, "assign_problem_sets");
562 : gage 2323
563 : sh002i 2847 print CGI::li(CGI::a({href=>$self->systemLink($stats)}, sp2nbsp($stats->name)));
564 : gage 2323
565 : gage 2485 ## Added Link for Student Progress
566 : sh002i 2847 print CGI::li(CGI::a({href=>$self->systemLink($progress)}, sp2nbsp($progress->name)));
567 : sh002i 1880 print CGI::start_li();
568 : gage 2486 if (defined $userID and $userID ne "") {
569 :     print CGI::ul(
570 :     CGI::li(CGI::a({href=>$self->systemLink($userProgress)}, $userID))
571 :     );
572 :     }
573 :     if (defined $setID and $setID ne "") {
574 :     print CGI::ul(
575 :     CGI::li(CGI::a({href=>$self->systemLink($setProgress)}, space2nbsp($setID)))
576 :     );
577 :     }
578 : apizer 2161 print CGI::end_li();
579 :    
580 : sh002i 2847 print CGI::li(CGI::a({href=>$self->systemLink($scoring)}, sp2nbsp($scoring->name))) if $authz->hasPermissions($user, "score_sets");
581 :     print CGI::li(CGI::a({href=>$self->systemLink($mail)}, sp2nbsp($mail->name))) if $authz->hasPermissions($user, "send_mail");
582 :     print CGI::li(CGI::a({href=>$self->systemLink($files)}, sp2nbsp($files->name)));
583 : gage 2486 print CGI::li( $self->helpMacro('instructor_links'));
584 : sh002i 1880 print CGI::end_ul();
585 : gage 2486
586 : sh002i 1866 }
587 :    
588 : sh002i 1880 print CGI::end_ul();
589 :     print "<!-- end " . __PACKAGE__ . "::links -->\n";
590 : sh002i 1866
591 : sh002i 1880 return "";
592 : sh002i 1866 }
593 :    
594 : sh002i 1873 =item loginstatus()
595 : sh002i 1861
596 : sh002i 1873 Defined in this package.
597 : sh002i 1861
598 : sh002i 1873 Print a notification message announcing the current real user and effective
599 :     user, a link to stop acting as the effective user, and a link to logout.
600 : sh002i 1861
601 :     =cut
602 :    
603 :     sub loginstatus {
604 : sh002i 1869 my ($self) = @_;
605 : sh002i 1873 my $r = $self->r;
606 : sh002i 1869 my $urlpath = $r->urlpath;
607 : sh002i 1861
608 :     my $key = $r->param("key");
609 :    
610 : sh002i 1869 if ($key) {
611 :     my $courseID = $urlpath->arg("courseID");
612 :     my $userID = $r->param("user");
613 :     my $eUserID = $r->param("effectiveUser");
614 :    
615 : sh002i 1882 my $stopActingURL = $self->systemLink($urlpath, # current path
616 : sh002i 1911 params => { effectiveUser => $userID },
617 : sh002i 1882 );
618 : sh002i 1869 my $logoutURL = $self->systemLink($urlpath->newFromModule(__PACKAGE__ . "::Logout", courseID => $courseID));
619 :    
620 : sh002i 1880 print "\n<!-- BEGIN " . __PACKAGE__ . "::loginstatus -->\n";
621 :    
622 : gage 2257 print "Logged in as $userID. ", CGI::br();
623 : sh002i 1869 print CGI::a({href=>$logoutURL}, "Log Out");
624 :    
625 :     if ($eUserID ne $userID) {
626 :     print " | Acting as $eUserID. ";
627 :     print CGI::a({href=>$stopActingURL}, "Stop Acting");
628 :     }
629 : sh002i 1880
630 :     print "<!-- END " . __PACKAGE__ . "::loginstatus -->\n";
631 : sh002i 1861 }
632 :    
633 :     return "";
634 :     }
635 :    
636 : sh002i 1873 =item nav($args)
637 : sh002i 1869
638 : sh002i 1873 Not defined in this package.
639 : sh002i 1861
640 : sh002i 1873 Links to the previous, next, and parent objects.
641 : sh002i 1861
642 : sh002i 1873 $args is a reference to a hash containing the following fields:
643 :    
644 : sh002i 1861 style => text|image
645 :     imageprefix => prefix to prepend to base image URL
646 :     imagesuffix => suffix to append to base image URL
647 :     separator => HTML to place in between links
648 :    
649 : sh002i 1873 If C<style> is "image", image URLs are constructed by prepending C<imageprefix>
650 :     and postpending C<imagesuffix> to the image base names defined by the
651 :     implementor. (Examples of base names include "Prev", "Next", "ProbSet", and
652 :     "Up"). Each concatenated string should form an absolute URL to an image file.
653 :     For example:
654 : sh002i 1861
655 : sh002i 1873 <!--#nav style="images" imageprefix="/webwork2_files/images/nav"
656 :     imagesuffix=".gif" separator=" "-->
657 : sh002i 1861
658 : sh002i 1873 =cut
659 : sh002i 1861
660 : sh002i 1873 #sub nav { }
661 : sh002i 1861
662 : sh002i 1873 =item options()
663 :    
664 :     Not defined in this package.
665 :    
666 :     Print an auxiliary options form, related to the content displayed in the
667 :     C<body>.
668 :    
669 :     =item path($args)
670 :    
671 :     Defined in this package.
672 :    
673 :     Print "breadcrubs" from the root of the virtual hierarchy to the current page.
674 :     $args is a reference to a hash containing the following fields:
675 :    
676 : sh002i 1861 style => type of separator: text|image
677 : sh002i 1873 image => if style=image, URL of image to use as path separator
678 :     text => if style=text, text to use as path separator
679 :     if style=image, the ALT text of each separator image
680 :     textonly => suppress all HTML, return only plain text
681 : sh002i 1861
682 : sh002i 1873 The implementation in this package takes information from the WeBWorK::URLPath
683 :     associated with the current request.
684 :    
685 : sh002i 1861 =cut
686 :    
687 :     sub path {
688 :     my ($self, $args) = @_;
689 : sh002i 1873 my $r = $self->r;
690 : sh002i 1861
691 :     my @path;
692 :    
693 :     my $urlpath = $r->urlpath;
694 :     do {
695 :     unshift @path, $urlpath->name, $r->location . $urlpath->path;
696 :     } while ($urlpath = $urlpath->parent);
697 :    
698 :     $path[$#path] = ""; # we don't want the last path element to be a link
699 :    
700 : sh002i 1882 #print "\n<!-- BEGIN " . __PACKAGE__ . "::path -->\n";
701 : sh002i 1880 print $self->pathMacro($args, @path);
702 : sh002i 1882 #print "<!-- END " . __PACKAGE__ . "::path -->\n";
703 : sh002i 1880
704 :     return "";
705 : sh002i 1861 }
706 :    
707 : sh002i 1873 =item siblings()
708 : sh002i 1861
709 : sh002i 1873 Not defined in this package.
710 : sh002i 1861
711 : sh002i 1873 Print links to siblings of the current object.
712 : sh002i 1861
713 : sh002i 1873 =cut
714 : sh002i 1861
715 : sh002i 1873 #sub siblings { }
716 :    
717 : jj 1942 =item timestamp()
718 :    
719 :     Defined in this package.
720 :    
721 :     Display the current time and date using default format "3:37pm on Jan 7, 2004".
722 :     The display format can be adjusted by giving a style in the template.
723 :     For example,
724 :    
725 :     <!--#timestamp style="%m/%d/%y at %I:%M%P"-->
726 :    
727 :     will give standard WeBWorK time format. Wording and other formatting
728 :     can be done in the template itself.
729 :     =cut
730 :    
731 :     sub timestamp {
732 :     my ($self, $args) = @_;
733 :     my $formatstring = "%l:%M%P on %b %e, %Y";
734 :     $formatstring = $args->{style} if(defined($args->{style}));
735 :     return(Date::Format::time2str($formatstring, time()));
736 :     }
737 :    
738 : sh002i 1873 =item submiterror()
739 :    
740 :     Defined in this package.
741 :    
742 :     Print any error messages resulting from the last form submission.
743 :    
744 : toenail 2013 This method is deprecated -- use message() instead
745 :    
746 : sh002i 1873 The implementation in this package prints the value of the field
747 :     $self->{submitError}, if it is present.
748 :    
749 : sh002i 1861 =cut
750 :    
751 :     sub submiterror {
752 :     my ($self) = @_;
753 : sh002i 1880
754 :     print "\n<!-- BEGIN " . __PACKAGE__ . "::submiterror -->\n";
755 :     print $self->{submitError} if exists $self->{submitError};
756 :     print "<!-- END " . __PACKAGE__ . "::submiterror -->\n";
757 :    
758 :     return "";
759 : sh002i 1861 }
760 :    
761 : toenail 2013 =item message()
762 :    
763 :     Defined in this package.
764 :    
765 :     Print any messages (error or non-error) resulting from the last form submission.
766 :     This could be used to give Sucess and Failure messages after an action is performed by a module.
767 :    
768 :     The implementation in this package prints the value of the field
769 : sh002i 2034 $self->{status_message}, if it is present.
770 : toenail 2013
771 :     =cut
772 :    
773 :     sub message {
774 :     my ($self) = @_;
775 :    
776 :     print "\n<!-- BEGIN " . __PACKAGE__ . "::message -->\n";
777 : sh002i 2034 print $self->{status_message} if exists $self->{status_message};
778 : toenail 2013 print "<!-- END " . __PACKAGE__ . "::message -->\n";
779 :    
780 :     return "";
781 :     }
782 :    
783 : sh002i 1873 =item title()
784 : sh002i 1861
785 : sh002i 1873 Defined in this package.
786 : sh002i 1861
787 : sh002i 1873 Print the title of the current page.
788 :    
789 :     The implementation in this package takes information from the WeBWorK::URLPath
790 :     associated with the current request.
791 :    
792 : sh002i 1861 =cut
793 :    
794 :     sub title {
795 :     my ($self, $args) = @_;
796 : sh002i 1873 my $r = $self->r;
797 : sh002i 1861
798 : sh002i 1882 #print "\n<!-- BEGIN " . __PACKAGE__ . "::title -->\n";
799 : sh002i 1880 print $r->urlpath->name;
800 : sh002i 1882 #print "<!-- END " . __PACKAGE__ . "::title -->\n";
801 : sh002i 1880
802 :     return "";
803 : sh002i 1861 }
804 :    
805 : sh002i 1873 =item warnings()
806 : sh002i 1861
807 : sh002i 1873 Defined in this package.
808 : sh002i 1861
809 : sh002i 1873 Print accumulated warnings.
810 :    
811 :     The implementation in this package checks for a note in the request named
812 :     "warnings". If present, its contents are formatted and returned.
813 :    
814 : sh002i 1861 =cut
815 :    
816 : sh002i 2383 sub warnings {
817 :     my ($self) = @_;
818 :     my $r = $self->r;
819 :    
820 :     print "\n<!-- BEGIN " . __PACKAGE__ . "::warnings -->\n";
821 :     print $self->warningOutput($r->notes("warnings")) if $r->notes("warnings");
822 :     print "<!-- END " . __PACKAGE__ . "::warnings -->\n";
823 :    
824 :     return "";
825 :     }
826 : gage 2257
827 : sh002i 2383 =item help()
828 : gage 2257
829 : sh002i 2383 Display a link to context-sensitive help. If the argument C<name> is defined,
830 :     the link will be to the help document for that name. Otherwise the name of the
831 :     WeBWorK::URLPath node for the current system location will be used.
832 :    
833 : gage 2257 =cut
834 :    
835 :     sub help {
836 :     my $self = shift;
837 :     my $args = shift;
838 :     my $name = $args->{name};
839 : gage 2260 $name = lc($self->r->urlpath->name) unless defined($name);
840 :     $name =~ s/\s/_/g;
841 : gage 2257 $self->helpMacro($name);
842 :     }
843 :    
844 : sh002i 1861 =back
845 :    
846 : sh002i 1873 =cut
847 : sh002i 1861
848 : sh002i 1873 # ------------------------------------------------------------------------------
849 : sh002i 1861
850 : sh002i 1873 =head2 Conditional predicates
851 :    
852 :     Conditional predicate methods are invoked when the C<#if> escape sequence is
853 :     encountered in the template. If a method named C<if_predicate> is defined in
854 :     here or in the instantiated subclass, it is invoked.
855 :    
856 : sh002i 1861 The following predicates are currently defined:
857 :    
858 :     =over
859 :    
860 : sh002i 1873 =item if_can($function)
861 : sh002i 1861
862 : sh002i 1873 If a function named $function is present in the current content generator (or
863 :     any superclass), a true value is returned. Otherwise, a false value is returned.
864 : sh002i 1861
865 : sh002i 1873 The implementation in this package uses the method UNIVERSAL->can(function) to
866 :     arrive at the result.
867 :    
868 :     A subclass could redefine this method to, for example, "hide" a method from the
869 :     template:
870 :    
871 :     sub if_can {
872 :     my ($self, $arg) = @_;
873 :    
874 :     if ($arg eq "floobar") {
875 :     return 0;
876 :     } else {
877 :     return $self->SUPER::if_can($arg);
878 :     }
879 :     }
880 :    
881 : sh002i 1861 =cut
882 :    
883 : sh002i 1873 sub if_can {
884 :     my ($self, $arg) = @_;
885 : malsyned 525
886 : sh002i 1873 return $self->can($arg) ? 1 : 0;
887 : malsyned 525 }
888 :    
889 : sh002i 1873 =item if_loggedin($arg)
890 : sh002i 1861
891 : sh002i 1873 If the user is currently logged in, $arg is returned. Otherwise, the inverse of
892 :     $arg is returned.
893 : sh002i 1861
894 : sh002i 1873 The implementation in this package always returns $arg, since most content
895 :     generators are only reachable when the user is authenticated. It is up to
896 :     classes that can be reached without logging in to override this method and
897 :     provide the correct behavior.
898 :    
899 :     This is suboptimal, and may change in the future.
900 :    
901 : sh002i 1861 =cut
902 :    
903 : sh002i 1873 sub if_loggedin {
904 :     my ($self, $arg) = @_;
905 : malsyned 748
906 :     return $arg;
907 :     }
908 :    
909 : sh002i 1873 =item if_submiterror($arg)
910 : sh002i 1131
911 : sh002i 1873 If the last form submission generated an error, $arg is returned. Otherwise, the
912 :     inverse of $arg is returned.
913 :    
914 :     The implementation in this package checks for the field $self->{submitError} to
915 :     determine if an error condition is present.
916 :    
917 :     If a subclass uses some other method to classify submission results, this method could be
918 :     redefined to handle that variance:
919 :    
920 :     sub if_submiterror {
921 :     my ($self, $arg) = @_;
922 :    
923 :     my $status = $self->{processReturnValue};
924 :     if ($status != 0) {
925 :     return $arg;
926 :     } else {
927 :     return !$arg;
928 :     }
929 :     }
930 :    
931 : sh002i 1861 =cut
932 :    
933 : sh002i 1873 sub if_submiterror {
934 : malsyned 1017 my ($self, $arg) = @_;
935 : sh002i 1873
936 : malsyned 1017 if (exists $self->{submitError}) {
937 :     return $arg;
938 :     } else {
939 :     return !$arg;
940 :     }
941 :     }
942 :    
943 : toenail 2013 =item if_message($arg)
944 :    
945 :     If the last form submission generated a message, $arg is returned. Otherwise, the
946 :     inverse of $arg is returned.
947 :    
948 : sh002i 2034 The implementation in this package checks for the field $self->{status_message} to
949 : toenail 2013 determine if a message is present.
950 :    
951 :     If a subclass uses some other method to classify submission results, this method could be
952 :     redefined to handle that variance:
953 :    
954 :     sub if_message {
955 :     my ($self, $arg) = @_;
956 :    
957 :     my $status = $self->{processReturnValue};
958 :     if ($status != 0) {
959 :     return $arg;
960 :     } else {
961 :     return !$arg;
962 :     }
963 :     }
964 :    
965 :     =cut
966 :    
967 :     sub if_message {
968 :     my ($self, $arg) = @_;
969 :    
970 : sh002i 2034 if (exists $self->{status_message}) {
971 : toenail 2013 return $arg;
972 :     } else {
973 :     return !$arg;
974 :     }
975 :     }
976 :    
977 : sh002i 1861 =item if_warnings
978 : sh002i 1131
979 : sh002i 1873 If warnings have been emitted while handling this request, $arg is returned.
980 :     Otherwise, the inverse of $arg is returned.
981 :    
982 :     The implementation in this package checks for a note in the request named
983 :     "warnings". This is set by the WARN handler in Apache::WeBWorK when a warning is
984 :     handled.
985 :    
986 : sh002i 1869 =cut
987 :    
988 : sh002i 1873 sub if_warnings {
989 : sh002i 1131 my ($self, $arg) = @_;
990 : sh002i 1873 my $r = $self->r;
991 :    
992 :     if ($r->notes("warnings")) {
993 :     return $arg;
994 :     } else {
995 :     !$arg;
996 :     }
997 : sh002i 1131 }
998 :    
999 : sh002i 1861 =back
1000 : sh002i 1131
1001 : sh002i 1861 =cut
1002 :    
1003 : sh002i 1873 ################################################################################
1004 : malsyned 508
1005 : sh002i 1873 =head1 HTML MACROS
1006 : malsyned 508
1007 : sh002i 1873 Various routines are defined in this package for rendering common WeBWorK
1008 :     idioms.
1009 :    
1010 :     FIXME: some of these should be moved to WeBWorK::HTML:: modules!
1011 :    
1012 :     # ------------------------------------------------------------------------------
1013 :    
1014 :     =head2 Template escape handler macros
1015 :    
1016 :     These methods are used by implementations of the escape sequence handlers to
1017 :     maintain a consistent style.
1018 :    
1019 :     =over
1020 :    
1021 :     =item pathMacro($args, @path)
1022 :    
1023 :     Helper macro for the C<#path> escape sequence: $args is a hash reference
1024 :     containing the "style", "image", "text", and "textonly" arguments to the escape.
1025 :     @path consists of ordered key-value pairs of the form:
1026 :    
1027 :     "Page Name" => URL
1028 :    
1029 :     If the page should not have a link associated with it, the URL should be left
1030 :     empty. Authentication data is added to each URL so you don't have to. A fully-
1031 :     formed path line is returned, suitable for returning by a function implementing
1032 :     the C<#path> escape.
1033 :    
1034 :     FIXME: authentication data probably shouldn't be added here any more, now that
1035 :     we have systemLink().
1036 :    
1037 :     =cut
1038 :    
1039 :     sub pathMacro {
1040 :     my ($self, $args, @path) = @_;
1041 :     my %args = %$args;
1042 :     $args{style} = "text" if $args{textonly};
1043 :    
1044 :     my $auth = $self->url_authen_args;
1045 :     my $sep;
1046 :     if ($args{style} eq "image") {
1047 :     $sep = CGI::img({-src=>$args{image}, -alt=>$args{text}});
1048 :     } else {
1049 :     $sep = $args{text};
1050 :     }
1051 :    
1052 :     my @result;
1053 :     while (@path) {
1054 :     my $name = shift @path;
1055 :     my $url = shift @path;
1056 :     if ($url and not $args{textonly}) {
1057 :     push @result, CGI::a({-href=>"$url?$auth"}, $name);
1058 :     } else {
1059 :     push @result, $name;
1060 :     }
1061 :     }
1062 :    
1063 :     return join($sep, @result), "\n";
1064 :     }
1065 :    
1066 :     =item siblingsMacro(@siblings)
1067 :    
1068 :     Helper macro for the C<#siblings> escape sequence. @siblings consists of ordered
1069 :     key-value pairs of the form:
1070 :    
1071 :     "Sibling Name" => URL
1072 :    
1073 :     If the sibling should not have a link associated with it, the URL should be left
1074 :     empty. Authentication data is added to each URL so you don't have to. A fully-
1075 :     formed siblings block is returned, suitable for returning by a function
1076 :     implementing the C<#siblings> escape.
1077 :    
1078 :     FIXME: authentication data probably shouldn't be added here any more, now that
1079 :     we have systemLink().
1080 :    
1081 :     =cut
1082 :    
1083 :     sub siblingsMacro {
1084 :     my ($self, @siblings) = @_;
1085 :    
1086 :     my $auth = $self->url_authen_args;
1087 :     my $sep = CGI::br();
1088 :    
1089 :     my @result;
1090 :     while (@siblings) {
1091 :     my $name = shift @siblings;
1092 :     my $url = shift @siblings;
1093 :     push @result, $url
1094 :     ? CGI::a({-href=>"$url?$auth"}, $name)
1095 :     : $name;
1096 :     }
1097 :    
1098 :     return join($sep, @result) . "\n";
1099 :     }
1100 :    
1101 : gage 2257
1102 :    
1103 : sh002i 1873 =item navMacro($args, $tail, @links)
1104 :    
1105 :     Helper macro for the C<#nav> escape sequence: $args is a hash reference
1106 :     containing the "style", "imageprefix", "imagesuffix", and "separator" arguments
1107 :     to the escape. @siblings consists of ordered tuples of the form:
1108 :    
1109 :     "Link Name", URL, ImageBaseName
1110 :    
1111 :     If the sibling should not have a link associated with it, the URL should be left
1112 :     empty. ImageBaseName is placed between the C<imageprefix> and C<imagesuffix>.
1113 :     Authentication data is added to each URL so you don't have to. $tail is appended
1114 :     to each URL, after the authentication information. A fully-formed nav line is
1115 :     returned, suitable for returning by a function implementing the C<#nav> escape.
1116 :    
1117 :     =cut
1118 :    
1119 :     sub navMacro {
1120 :     my ($self, $args, $tail, @links) = @_;
1121 :     my $r = $self->r;
1122 :     my $ce = $r->ce;
1123 :     my %args = %$args;
1124 :    
1125 :     my $auth = $self->url_authen_args;
1126 :     my $prefix = $ce->{webworkURLs}->{htdocs}."/images";
1127 :    
1128 :     my @result;
1129 :     while (@links) {
1130 :     my $name = shift @links;
1131 :     my $url = shift @links;
1132 :     my $img = shift @links;
1133 :     my $html =
1134 :     ($img && $args{style} eq "images")
1135 :     ? CGI::img(
1136 :     {src=>($prefix."/".$img.$args{imagesuffix}),
1137 :     border=>"",
1138 :     alt=>"$name"})
1139 :     : $name;
1140 :     unless($img && !$url) {
1141 :     push @result, $url
1142 :     ? CGI::a({-href=>"$url?$auth$tail"}, $html)
1143 :     : $html;
1144 :     }
1145 :     }
1146 :    
1147 :     return join($args{separator}, @result) . "\n";
1148 :     }
1149 :    
1150 : sh002i 2383 =item helpMacro($name)
1151 :    
1152 :     This escape is represented by a question mark which links to an html page in the
1153 :     helpFiles directory. Currently the link is made to the file $name.html
1154 :    
1155 :     =cut
1156 :    
1157 :     sub helpMacro {
1158 :     my $self = shift;
1159 :     my $name = shift;
1160 :     my $ce = $self->r->ce;
1161 :     my $basePath = $ce->{webworkDirs}->{local_help};
1162 :     $name = 'no_help' unless -e "$basePath/$name.html";
1163 :     my $path = "$basePath/$name.html";
1164 :     my $url = $ce->{webworkURLs}->{local_help}."/$name.html";
1165 :     my $imageURL = $ce->{webworkURLs}->{htdocs}."/images/question_mark.png";
1166 :     return CGI::a({href => $url,
1167 :     target => 'ww_help',
1168 :     onclick => "window.open(this.href,this.target,'width=550,height=350,scrollbars=yes,resizable=on')"},
1169 :     CGI::img({src=>$imageURL}));
1170 :     }
1171 :    
1172 : sh002i 1873 =back
1173 :    
1174 :     =cut
1175 :    
1176 :     # ------------------------------------------------------------------------------
1177 :    
1178 :     =head2 Parameter management
1179 :    
1180 :     Methods for formatting request parameters as hidden form fields or query string
1181 :     fragments.
1182 :    
1183 :     =over
1184 :    
1185 :     =item hidden_fields(@fields)
1186 :    
1187 :     Return hidden <INPUT> tags for each field mentioned in @fields (or all fields if
1188 :     list is empty), taking data from the current request.
1189 :    
1190 :     =cut
1191 :    
1192 :     sub hidden_fields {
1193 :     my ($self, @fields) = @_;
1194 :     my $r = $self->r;
1195 :    
1196 :     @fields = $r->param unless @fields;
1197 :    
1198 :     my $html = "";
1199 :     foreach my $param (@fields) {
1200 :     my @values = $r->param($param);
1201 :     $html .= CGI::hidden($param, @values);
1202 :     }
1203 :     return $html;
1204 :     }
1205 :    
1206 :     =item hidden_authen_fields()
1207 :    
1208 :     Use hidden_fields to return hidden <INPUT> tags for request fields used in
1209 :     authentication.
1210 :    
1211 :     =cut
1212 :    
1213 :     sub hidden_authen_fields {
1214 :     my ($self) = @_;
1215 :    
1216 :     return $self->hidden_fields("user", "effectiveUser", "key");
1217 :     }
1218 :    
1219 :     =item url_args(@fields)
1220 :    
1221 :     Return a URL query string (without the leading `?') containing values for each
1222 :     field mentioned in @fields, or all fields if list is empty. Data is taken from
1223 :     the current request.
1224 :    
1225 :     =cut
1226 :    
1227 :     sub url_args {
1228 :     my ($self, @fields) = @_;
1229 :     my $r = $self->r;
1230 :    
1231 :     @fields = $r->param unless @fields;
1232 :    
1233 :     my @pairs;
1234 :     foreach my $param (@fields) {
1235 :     my @values = $r->param($param);
1236 :     foreach my $value (@values) {
1237 :     push @pairs, uri_escape($param) . "=" . uri_escape($value);
1238 :     }
1239 :     }
1240 :    
1241 :     return join("&", @pairs);
1242 :     }
1243 :    
1244 :     =item url_authen_args()
1245 :    
1246 :     Use url_args to return a URL query string for request fields used in
1247 :     authentication.
1248 :    
1249 :     =cut
1250 :    
1251 :     sub url_authen_args {
1252 :     my ($self) = @_;
1253 :    
1254 :     return $self->url_args("user", "effectiveUser", "key");
1255 :     }
1256 :    
1257 :     =item print_form_data($begin, $middle, $end, $omit)
1258 :    
1259 :     Return a string containing every request field not matched by the quoted reguar
1260 :     expression $omit, placing $begin before each field name, $middle between each
1261 :     field name and its value, and $end after each value. Values are taken from the
1262 :     current request.
1263 :    
1264 :     =cut
1265 :    
1266 :     sub print_form_data {
1267 :     my ($self, $begin, $middle, $end, $qr_omit) = @_;
1268 :     my $r=$self->r;
1269 :     my @form_data = $r->param;
1270 :    
1271 :     my $return_string = "";
1272 :     foreach my $name (@form_data) {
1273 :     next if ($qr_omit and $name =~ /$qr_omit/);
1274 :     my @values = $r->param($name);
1275 :     foreach my $variable (qw(begin name middle value end)) {
1276 :     # FIXME: can this loop be moved out of the enclosing loop?
1277 :     no strict 'refs';
1278 :     ${$variable} = "" unless defined ${$variable};
1279 :     }
1280 :     foreach my $value (@values) {
1281 :     $return_string .= "$begin$name$middle$value$end";
1282 :     }
1283 :     }
1284 :    
1285 :     return $return_string;
1286 :     }
1287 :    
1288 :     =back
1289 :    
1290 :     =cut
1291 :    
1292 :     # ------------------------------------------------------------------------------
1293 :    
1294 :     =head2 Utilities
1295 :    
1296 :     =over
1297 :    
1298 :     =item systemLink($urlpath, %options)
1299 :    
1300 :     Generate a link to another part of the system. $urlpath is WeBWorK::URLPath
1301 :     object from which the base path will be taken. %options can consist of:
1302 :    
1303 :     =over
1304 :    
1305 : sh002i 1882 =item params
1306 : sh002i 1873
1307 : sh002i 1911 Can be either a reference to an array or a reference to a hash.
1308 : sh002i 1873
1309 : sh002i 1911 If it is a reference to a hash, it maps parmaeter names to values. These
1310 :     parameters will be included in the generated link. If a value is an arrayref,
1311 :     the values of the array referenced will be used. If a value is undefined, the
1312 :     value from the current request will be used.
1313 : sh002i 1873
1314 : sh002i 1911 If C<params> is an arrayref, it is interpreted as a list of parameter names.
1315 :     These parameters will be included in the generated link, using the values from
1316 :     the current request.
1317 : sh002i 1873
1318 : sh002i 1911 Unless C<authen> is false (see below), the authentication parameters (C<user>,
1319 :     C<effectiveUser>, and C<key>) are included with their default values.
1320 : sh002i 1898
1321 : sh002i 1882 =item authen
1322 : sh002i 1873
1323 : sh002i 1911 If set to a false value, the authentication parameters (C<user>,
1324 :     C<effectiveUser>, and C<key>) are included in the the generated link unless
1325 :     explicitly listed in C<params>.
1326 : sh002i 1873
1327 :     =back
1328 :    
1329 :     =cut
1330 :    
1331 : sh002i 1882 # FIXME: there should probably be an option for prepending "http://hostname:port"
1332 : sh002i 1873 sub systemLink {
1333 :     my ($self, $urlpath, %options) = @_;
1334 :     my $r = $self->r;
1335 :    
1336 : sh002i 1911 my %params = ();
1337 : sh002i 1882 if (exists $options{params}) {
1338 : sh002i 1911 if (ref $options{params} eq "HASH") {
1339 :     %params = %{ $options{params} };
1340 :     } elsif (ref $options{params} eq "ARRAY") {
1341 :     my @names = @{ $options{params} };
1342 :     @params{@names} = ();
1343 :     } else {
1344 :     croak "option 'params' is not a hashref or an arrayref";
1345 :     }
1346 : sh002i 1882 }
1347 : sh002i 1873
1348 : sh002i 1893 my $authen = exists $options{authen} ? $options{authen} : 1;
1349 : sh002i 1911 if ($authen) {
1350 :     $params{user} = undef unless exists $params{user};
1351 :     $params{effectiveUser} = undef unless exists $params{effectiveUser};
1352 :     $params{key} = undef unless exists $params{key};
1353 : sh002i 1882 }
1354 :    
1355 : sh002i 1873 my $url = $r->location . $urlpath->path;
1356 : sh002i 1911 my $first = 1;
1357 : sh002i 1873
1358 : sh002i 1911 foreach my $name (keys %params) {
1359 :     my $value = $params{$name};
1360 :    
1361 :     my @values;
1362 :     if (defined $value) {
1363 :     if (ref $value eq "ARRAY") {
1364 :     @values = @$value;
1365 :     } else {
1366 :     @values = $value;
1367 :     }
1368 :     } elsif (defined $r->param($name)) {
1369 :     @values = $r->param($name);
1370 :     }
1371 :    
1372 :     if (@values) {
1373 :     if ($first) {
1374 :     $url .= "?";
1375 :     $first = 0;
1376 :     } else {
1377 :     $url .= "&";
1378 :     }
1379 :     $url .= join "&", map { "$name=$_" } @values;
1380 :     }
1381 : sh002i 1873 }
1382 :    
1383 :     return $url;
1384 :     }
1385 :    
1386 :     =item nbsp($string)
1387 :    
1388 :     If string consists of only whitespace, the HTML entity C<&nbsp;> is returned.
1389 :     Otherwise $string is returned.
1390 :    
1391 :     =cut
1392 :    
1393 :     sub nbsp {
1394 : sh002i 2847 my ($self, $str) = @_;
1395 :     return (defined $str && $str =~/\S/) ? $str : "&nbsp;";
1396 : sh002i 1873 }
1397 :    
1398 : sh002i 2847 =item sp2nbsp($string)
1399 :    
1400 :     A copy of $string is returned with each space character replaced by the
1401 :     C<&nbsp;> entity.
1402 :    
1403 :     =cut
1404 :    
1405 :     sub sp2nbsp {
1406 :     my ($str) = @_;
1407 :     return unless defined $str;
1408 :     $str =~ s/ /&nbsp;/g;
1409 :     return $str;
1410 :     }
1411 :    
1412 : jj 2222 =item space2nbsp($string)
1413 :    
1414 :     Replace spaces in the string with html non-breaking spaces.
1415 :    
1416 :     =cut
1417 :    
1418 :     sub space2nbsp {
1419 :     my $str = shift;
1420 :     $str =~ s/\s/&nbsp;/g;
1421 :     return($str);
1422 :     }
1423 :    
1424 : sh002i 1873 =item errorOutput($error, $details)
1425 :    
1426 :     =cut
1427 :    
1428 :     sub errorOutput($$$) {
1429 :     my ($self, $error, $details) = @_;
1430 :     return
1431 :     CGI::h3("Software Error"),
1432 :     CGI::p(<<EOF),
1433 :     WeBWorK has encountered a software error while attempting to process this
1434 :     problem. It is likely that there is an error in the problem itself. If you are
1435 :     a student, contact your professor to have the error corrected. If you are a
1436 : dpvc 2623 professor, please consult the error output below for more information.
1437 : sh002i 1873 EOF
1438 :     # FIXME: this message shouldn't refer the the "problem" since it is for general error reporting
1439 :     CGI::h3("Error messages"), CGI::p(CGI::tt($error)),
1440 :     CGI::h3("Error context"), CGI::p(CGI::tt($details));
1441 :     }
1442 :    
1443 :     =item warningOutput($warnings)
1444 :    
1445 :     =cut
1446 :    
1447 :     sub warningOutput($$) {
1448 :     my ($self, $warnings) = @_;
1449 :    
1450 :     my @warnings = split m/\n+/, $warnings;
1451 :    
1452 :     return
1453 :     CGI::h3("Software Warnings"),
1454 :     CGI::p(<<EOF),
1455 :     WeBWorK has encountered warnings while attempting to process this problem. It
1456 :     is likely that this indicates an error or ambiguity in the problem itself. If
1457 :     you are a student, contact your professor to have the problem corrected. If you
1458 : dpvc 2623 are a professor, please consult the warning output below for more information.
1459 : sh002i 1873 EOF
1460 :     # FIXME: this message shouldn't refer the the "problem" since it is for general warning reporting
1461 :     CGI::h3("Warning messages"),
1462 :     CGI::ul(CGI::li(\@warnings));
1463 :     }
1464 :    
1465 : sh002i 2775 =item $dateTime = parseDateTime($string, $display_tz)
1466 :    
1467 :     Parses $string as a datetime. If $display_tz is given, $string is assumed to be
1468 :     in that timezone. Otherwise, the timezone defined in the course environment
1469 :     variable $siteDefaults{timezone} is used. The result, $dateTime, is an integer
1470 :     UNIX datetime (epoch) in the server's timezone.
1471 :    
1472 :     =cut
1473 :    
1474 :     sub parseDateTime {
1475 :     my ($self, $string, $display_tz) = @_;
1476 :     my $ce = $self->r->ce;
1477 :     $display_tz ||= $ce->{siteDefaults}{timezone};
1478 :     return WeBWorK::Utils::parseDateTime($string, $display_tz);
1479 :     };
1480 :    
1481 :     =item $string = formatDateTime($dateTime, $display_tz)
1482 :    
1483 :     Formats the UNIX datetime $dateTime in the standard WeBWorK datetime format.
1484 :     $dateTime is assumed to be in the server's time zone. If $display_tz is given,
1485 :     the datetime is converted from the server's timezone to the timezone specified.
1486 :     Otherwise, the timezone defined in the course environment variable
1487 :     $siteDefaults{timezone} is used.
1488 :    
1489 :     =cut
1490 :    
1491 :     sub formatDateTime {
1492 :     my ($self, $dateTime, $display_tz) = @_;
1493 :     my $ce = $self->r->ce;
1494 :     $display_tz ||= $ce->{siteDefaults}{timezone};
1495 :     return WeBWorK::Utils::formatDateTime($dateTime, $display_tz);
1496 :     }
1497 :    
1498 : sh002i 1873 =back
1499 :    
1500 : malsyned 508 =head1 AUTHOR
1501 :    
1502 : sh002i 1873 Written by Dennis Lambe Jr., malsyned (at) math.rochester.edu and Sam Hathaway,
1503 :     sh002i (at) math.rochester.edu.
1504 : malsyned 508
1505 :     =cut
1506 : sh002i 1873
1507 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9