Parent Directory
|
Revision Log
Revision 1974 -
(view)
(download)
(as text)
Original Path: trunk/webwork2/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 | 1974 | # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator.pm,v 1.91 2004/04/04 17:08:45 jj 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 : | sh002i | 1873 | ################################################################################ |
| 53 : | malsyned | 390 | |
| 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 | If the field $self->{sendFile} is defined, the method sendFile() is called to |
| 126 : | send the specified file to the client, and go() terminates. See below. | ||
| 127 : | sh002i | 1861 | |
| 128 : | sh002i | 1873 | =item 5 |
| 129 : | sh002i | 1861 | |
| 130 : | sh002i | 1873 | go() then attempts to call the method initialize(). This method may be |
| 131 : | implemented in subclasses which must do processing after the HTTP header is sent | ||
| 132 : | but before any content is sent. | ||
| 133 : | sh002i | 1861 | |
| 134 : | sh002i | 1873 | =item 6 |
| 135 : | |||
| 136 : | The method content() is called to send the page content to client. | ||
| 137 : | |||
| 138 : | sh002i | 1861 | =back |
| 139 : | |||
| 140 : | =cut | ||
| 141 : | |||
| 142 : | sh002i | 469 | sub go { |
| 143 : | sh002i | 1873 | my ($self) = @_; |
| 144 : | my $r = $self->r; | ||
| 145 : | sh002i | 1869 | my $ce = $r->ce; |
| 146 : | |||
| 147 : | malsyned | 1408 | my $returnValue = OK; |
| 148 : | sh002i | 1131 | |
| 149 : | sh002i | 469 | $self->pre_header_initialize(@_) if $self->can("pre_header_initialize"); |
| 150 : | sh002i | 1873 | |
| 151 : | sh002i | 1911 | # send a file instead of a normal reply (reply_with_file() sets this field) |
| 152 : | defined $self->{reply_with_file} and do { | ||
| 153 : | return $self->do_reply_with_file($self->{reply_with_file}); | ||
| 154 : | }; | ||
| 155 : | |||
| 156 : | # send a Location: header instead of a normal reply (reply_with_redirect() sets this field) | ||
| 157 : | defined $self->{reply_with_redirect} and do { | ||
| 158 : | return $self->do_reply_with_redirect($self->{reply_with_redirect}); | ||
| 159 : | }; | ||
| 160 : | |||
| 161 : | malsyned | 1408 | my $headerReturn = $self->header(@_); |
| 162 : | $returnValue = $headerReturn if defined $headerReturn; | ||
| 163 : | sh002i | 1911 | # FIXME: we won't need noContent after reply_with_redirect() is adopted |
| 164 : | malsyned | 1408 | return $returnValue if $r->header_only or $self->{noContent}; |
| 165 : | malsyned | 313 | |
| 166 : | sh002i | 1911 | # FIXME: when $self->{sendFile} is no longer being set, remove this: |
| 167 : | sh002i | 1613 | # if the sendFile flag is set, send the file and exit; |
| 168 : | if ($self->{sendFile}) { | ||
| 169 : | return $self->sendFile; | ||
| 170 : | } | ||
| 171 : | |||
| 172 : | sh002i | 1873 | $self->initialize() if $self->can("initialize"); |
| 173 : | sh002i | 1225 | |
| 174 : | sh002i | 1873 | $self->content(); |
| 175 : | malsyned | 1408 | |
| 176 : | return $returnValue; | ||
| 177 : | sh002i | 469 | } |
| 178 : | |||
| 179 : | sh002i | 1861 | =item sendFile() |
| 180 : | |||
| 181 : | sh002i | 1873 | Sends the file specified in $self->{sendFile} to the client. $self->{sendFile} |
| 182 : | should be a reference to a hash containing the following fields: | ||
| 183 : | |||
| 184 : | source => full path to the file to send | ||
| 185 : | type => the content type of the file | ||
| 186 : | name => the name that the client should give to the file upon download | ||
| 187 : | |||
| 188 : | This method is called internally by go() if the field $self->{sendFile} is | ||
| 189 : | present. | ||
| 190 : | |||
| 191 : | This mechanism relies on the header() method to send appropriate C<Content-Type> | ||
| 192 : | and C<Content-Disposition> headers. | ||
| 193 : | |||
| 194 : | This mechanism is fragile and will probably be replaced by something else in the | ||
| 195 : | future. | ||
| 196 : | |||
| 197 : | sh002i | 1861 | =cut |
| 198 : | |||
| 199 : | sh002i | 1911 | # FIXME: when $self->{sendFile} is no longer being set, remove this method |
| 200 : | sh002i | 1613 | sub sendFile { |
| 201 : | my ($self) = @_; | ||
| 202 : | |||
| 203 : | my $file = $self->{sendFile}->{source}; | ||
| 204 : | |||
| 205 : | return NOT_FOUND unless -e $file; | ||
| 206 : | return FORBIDDEN unless -r $file; | ||
| 207 : | |||
| 208 : | open my $fh, "<", $file | ||
| 209 : | or return SERVER_ERROR; | ||
| 210 : | while (<$fh>) { | ||
| 211 : | print $_; | ||
| 212 : | } | ||
| 213 : | close $fh; | ||
| 214 : | |||
| 215 : | return OK; | ||
| 216 : | } | ||
| 217 : | |||
| 218 : | sh002i | 1873 | =item r() |
| 219 : | sh002i | 1861 | |
| 220 : | sh002i | 1873 | Returns a reference to the WeBWorK::Request object associated with this |
| 221 : | instance. | ||
| 222 : | sh002i | 1861 | |
| 223 : | =cut | ||
| 224 : | |||
| 225 : | sh002i | 1873 | sub r { |
| 226 : | my ($self) = @_; | ||
| 227 : | sh002i | 1869 | |
| 228 : | sh002i | 1873 | return $self->{r}; |
| 229 : | sh002i | 469 | } |
| 230 : | |||
| 231 : | sh002i | 1911 | =item reply_with_file($type, $source, $name) |
| 232 : | |||
| 233 : | Enables file sending mode, causing go() to send the file specified by $source to | ||
| 234 : | the client after calling pre_header_initialize(). The content type sent is | ||
| 235 : | $type, and the suggested client-side file name is $name. | ||
| 236 : | |||
| 237 : | =cut | ||
| 238 : | |||
| 239 : | sub reply_with_file { | ||
| 240 : | my ($self, $type, $source, $name) = @_; | ||
| 241 : | |||
| 242 : | $self->{reply_with_file} = { | ||
| 243 : | type => $type, | ||
| 244 : | source => $source, | ||
| 245 : | name => $name, | ||
| 246 : | }; | ||
| 247 : | } | ||
| 248 : | |||
| 249 : | =item do_reply_with_file($fileHash) | ||
| 250 : | |||
| 251 : | Handler for reply_with_file(), used by go(). DO NOT CALL THIS METHOD DIRECTLY. | ||
| 252 : | |||
| 253 : | =cut | ||
| 254 : | |||
| 255 : | sub do_reply_with_file { | ||
| 256 : | my ($self, $fileHash) = @_; | ||
| 257 : | my $r = $self->r; | ||
| 258 : | |||
| 259 : | my $type = $self->{sendFile}->{type}; | ||
| 260 : | my $source = $fileHash->{source}; | ||
| 261 : | my $name = $self->{sendFile}->{name}; | ||
| 262 : | |||
| 263 : | # if there was a problem, we return here and let go() worry about sending the reply | ||
| 264 : | return NOT_FOUND unless -e $source; | ||
| 265 : | return FORBIDDEN unless -r $source; | ||
| 266 : | |||
| 267 : | # open the file now, so we can send the proper error status is we fail | ||
| 268 : | open my $fh, "<", $source or return SERVER_ERROR; | ||
| 269 : | |||
| 270 : | # send our custom HTTP header | ||
| 271 : | $r->status(OK); | ||
| 272 : | $r->content_type($type); | ||
| 273 : | $r->header_out("Content-Disposition" => "attachment; filename=\"$name\""); | ||
| 274 : | $r->send_http_header; | ||
| 275 : | |||
| 276 : | # send the file | ||
| 277 : | $r->send_fd($fh); | ||
| 278 : | |||
| 279 : | # close the file and go home | ||
| 280 : | close $fh; | ||
| 281 : | } | ||
| 282 : | |||
| 283 : | =item reply_with_redirect($url) | ||
| 284 : | |||
| 285 : | Enables redirect mode, causing go() to redirect to the given URL after calling | ||
| 286 : | pre_header_initialize(). | ||
| 287 : | |||
| 288 : | =cut | ||
| 289 : | |||
| 290 : | sub reply_with_redirect { | ||
| 291 : | my ($self, $url) = @_; | ||
| 292 : | |||
| 293 : | $self->{reply_with_redirect} = $url; | ||
| 294 : | } | ||
| 295 : | |||
| 296 : | =item do_reply_with_redirect($url) | ||
| 297 : | |||
| 298 : | Handler for reply_with_redirect(), used by go(). DO NOT CALL THIS METHOD DIRECTLY. | ||
| 299 : | |||
| 300 : | =cut | ||
| 301 : | |||
| 302 : | sub do_reply_with_redirect { | ||
| 303 : | my ($self, $url) = @_; | ||
| 304 : | my $r = $self->r; | ||
| 305 : | |||
| 306 : | $r->status(REDIRECT); | ||
| 307 : | $r->header_out(Location => $url); | ||
| 308 : | $r->send_http_header(); | ||
| 309 : | } | ||
| 310 : | |||
| 311 : | sh002i | 1873 | =back |
| 312 : | sh002i | 1861 | |
| 313 : | =cut | ||
| 314 : | |||
| 315 : | sh002i | 1873 | ################################################################################ |
| 316 : | sh002i | 469 | |
| 317 : | sh002i | 1873 | =head1 STANDARD METHODS |
| 318 : | sh002i | 1861 | |
| 319 : | sh002i | 1873 | The following are the standard content generator methods. Some are defined here, |
| 320 : | but may be overridden in a subclass. Others are not defined unless they are | ||
| 321 : | defined in a subclass. | ||
| 322 : | sh002i | 1861 | |
| 323 : | sh002i | 1873 | =over |
| 324 : | sh002i | 469 | |
| 325 : | sh002i | 1873 | =item pre_header_initialize() |
| 326 : | sh002i | 1861 | |
| 327 : | sh002i | 1873 | Not defined in this package. |
| 328 : | sh002i | 1861 | |
| 329 : | sh002i | 1873 | May be defined by a subclass to perform any processing that must occur before |
| 330 : | the HTTP header is sent. | ||
| 331 : | |||
| 332 : | sh002i | 1861 | =cut |
| 333 : | |||
| 334 : | sh002i | 1873 | #sub pre_header_initialize { } |
| 335 : | malsyned | 323 | |
| 336 : | sh002i | 1873 | =item header() |
| 337 : | sh002i | 1861 | |
| 338 : | sh002i | 1873 | Defined in this package. |
| 339 : | sh002i | 1861 | |
| 340 : | sh002i | 1873 | Generates and sends a default HTTP header. If the field $self->{sendFile} is |
| 341 : | present, sends the following headers (where TYPE is $self->{sendFile}->{type} | ||
| 342 : | and NAME is $self->{sendFile}->{name}): | ||
| 343 : | sh002i | 1861 | |
| 344 : | sh002i | 1873 | Content-Type: TYPE |
| 345 : | Content-Disposition: attachment; filename=NAME | ||
| 346 : | sh002i | 425 | |
| 347 : | sh002i | 1873 | If $self->{sendFile} is not present, sends the following headers: |
| 348 : | sh002i | 1861 | |
| 349 : | sh002i | 1873 | Content-Type: text/html |
| 350 : | sh002i | 1861 | |
| 351 : | sh002i | 1873 | See sendFile() above for more information on the sendFile mechanism. |
| 352 : | |||
| 353 : | sh002i | 1861 | =cut |
| 354 : | |||
| 355 : | sh002i | 1873 | sub header { |
| 356 : | sh002i | 425 | my $self = shift; |
| 357 : | sh002i | 1873 | my $r = $self->r; |
| 358 : | sh002i | 425 | |
| 359 : | sh002i | 1911 | # FIXME: when $self->{sendFile} is no longer being set, remove sendFile handler |
| 360 : | |||
| 361 : | sh002i | 1873 | if ($self->{sendFile}) { |
| 362 : | my $contentType = $self->{sendFile}->{type}; | ||
| 363 : | my $fileName = $self->{sendFile}->{name}; | ||
| 364 : | $r->content_type($contentType); | ||
| 365 : | $r->header_out("Content-Disposition" => "attachment; filename=\"$fileName\""); | ||
| 366 : | } else { | ||
| 367 : | $r->content_type("text/html"); | ||
| 368 : | |||
| 369 : | sh002i | 425 | } |
| 370 : | sh002i | 469 | |
| 371 : | sh002i | 1873 | $r->send_http_header(); |
| 372 : | return OK; | ||
| 373 : | sh002i | 425 | } |
| 374 : | |||
| 375 : | sh002i | 1873 | =item initialize() |
| 376 : | sh002i | 1861 | |
| 377 : | sh002i | 1873 | Not defined in this package. |
| 378 : | sh002i | 1861 | |
| 379 : | sh002i | 1873 | May be defined by a subclass to perform any processing that must occur after the |
| 380 : | HTTP header is sent but before any content is sent. | ||
| 381 : | sh002i | 1861 | |
| 382 : | =cut | ||
| 383 : | |||
| 384 : | sh002i | 1873 | #sub initialize { } |
| 385 : | sh002i | 1861 | |
| 386 : | sh002i | 1873 | =item content() |
| 387 : | sh002i | 1861 | |
| 388 : | sh002i | 1873 | Defined in this package. |
| 389 : | sh002i | 1861 | |
| 390 : | sh002i | 1873 | Print the content of the generated page. |
| 391 : | sh002i | 1861 | |
| 392 : | sh002i | 1873 | The implementation in this package uses WeBWorK::Template to define the content |
| 393 : | of the page. See WeBWorK::Template for details. | ||
| 394 : | malsyned | 390 | |
| 395 : | sh002i | 1873 | If a method named templateName() exists, it it called to determine the name of |
| 396 : | the template to use. If not, the default template, "system", is used. The | ||
| 397 : | location of the template is looked up in the course environment. | ||
| 398 : | sh002i | 1861 | |
| 399 : | =cut | ||
| 400 : | |||
| 401 : | sh002i | 1873 | sub content { |
| 402 : | my ($self) = @_; | ||
| 403 : | my $ce = $self->r->ce; | ||
| 404 : | sh002i | 737 | |
| 405 : | sh002i | 1873 | # if the content generator specifies a custom template name, use that |
| 406 : | # field in the $ce->{templates} hash instead of "system" if it exists. | ||
| 407 : | my $templateName; | ||
| 408 : | if ($self->can("templateName")) { | ||
| 409 : | $templateName = $self->templateName; | ||
| 410 : | } else { | ||
| 411 : | $templateName = "system"; | ||
| 412 : | } | ||
| 413 : | $templateName = "system" unless exists $ce->{templates}->{$templateName}; | ||
| 414 : | template($ce->{templates}->{$templateName}, $self); | ||
| 415 : | sh002i | 737 | } |
| 416 : | |||
| 417 : | sh002i | 1861 | =back |
| 418 : | |||
| 419 : | =cut | ||
| 420 : | |||
| 421 : | sh002i | 1873 | # ------------------------------------------------------------------------------ |
| 422 : | sh002i | 1866 | |
| 423 : | sh002i | 1873 | =head2 Template escape handlers |
| 424 : | sh002i | 1866 | |
| 425 : | sh002i | 1873 | Template escape handlers are invoked when the template processor encounters a |
| 426 : | matching escape sequence in the template. The escapse sequence's arguments are | ||
| 427 : | passed to the methods as a reference to a hash. | ||
| 428 : | sh002i | 1866 | |
| 429 : | sh002i | 1873 | For more information, refer to WeBWorK::Template. |
| 430 : | malsyned | 390 | |
| 431 : | sh002i | 1873 | The following template escapes handlers are defined here or may be defined in |
| 432 : | subclasses. For methods that are not defined in this package, the documentation | ||
| 433 : | defines the interface and behavior that any subclass implementation must follow. | ||
| 434 : | malsyned | 313 | |
| 435 : | sh002i | 1861 | =over |
| 436 : | |||
| 437 : | sh002i | 1873 | =item head() |
| 438 : | sh002i | 1861 | |
| 439 : | sh002i | 1873 | Not defined in this package. |
| 440 : | sh002i | 1861 | |
| 441 : | sh002i | 1873 | Any tags that should appear in the HEAD of the document. |
| 442 : | sh002i | 1861 | |
| 443 : | =cut | ||
| 444 : | |||
| 445 : | sh002i | 1873 | #sub head { } |
| 446 : | malsyned | 349 | |
| 447 : | sh002i | 1873 | =item info() |
| 448 : | sh002i | 682 | |
| 449 : | sh002i | 1873 | Not defined in this package. |
| 450 : | sh002i | 1861 | |
| 451 : | sh002i | 1873 | Auxiliary information related to the content displayed in the C<body>. |
| 452 : | sh002i | 1861 | |
| 453 : | sh002i | 1873 | =cut |
| 454 : | sh002i | 1861 | |
| 455 : | sh002i | 1873 | #sub info { } |
| 456 : | sh002i | 1861 | |
| 457 : | sh002i | 1873 | =item links() |
| 458 : | sh002i | 1861 | |
| 459 : | sh002i | 1873 | Defined in this package. |
| 460 : | sh002i | 1861 | |
| 461 : | sh002i | 1873 | Links that should appear on every page. |
| 462 : | sh002i | 1861 | |
| 463 : | =cut | ||
| 464 : | |||
| 465 : | sh002i | 1866 | sub links { |
| 466 : | my ($self) = @_; | ||
| 467 : | sh002i | 1873 | my $r = $self->r; |
| 468 : | sh002i | 1866 | my $db = $r->db; |
| 469 : | my $urlpath = $r->urlpath; | ||
| 470 : | |||
| 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 : | print CGI::start_ul({class=>"LinksMenu"}); | ||
| 485 : | print CGI::li(CGI::span({style=>"font-size:larger"}, | ||
| 486 : | CGI::a({href=>$self->systemLink($sets)}, "Problem Sets"))); | ||
| 487 : | print CGI::li(CGI::a({href=>$self->systemLink($options)}, $options->name)); | ||
| 488 : | print CGI::li(CGI::a({href=>$self->systemLink($grades)}, $grades->name)); | ||
| 489 : | print CGI::li(CGI::a({href=>$self->systemLink($logout)}, $logout->name)); | ||
| 490 : | |||
| 491 : | sh002i | 1866 | my $PermissionLevel = $db->getPermissionLevel($r->param("user")); # checked |
| 492 : | my $permLevel = $PermissionLevel ? $PermissionLevel->permission : 0; | ||
| 493 : | |||
| 494 : | if ($permLevel > 0) { | ||
| 495 : | my $ipfx = "${pfx}Instructor::"; | ||
| 496 : | |||
| 497 : | my $userID = $r->param("effectiveUser"); | ||
| 498 : | my $setID = $urlpath->arg("setID"); | ||
| 499 : | my $problemID = $urlpath->arg("problemID"); | ||
| 500 : | |||
| 501 : | my $instr = $urlpath->newFromModule("${ipfx}Index", %args); | ||
| 502 : | sh002i | 1918 | my $addUsers = $urlpath->newFromModule("${ipfx}AddUsers", %args); |
| 503 : | sh002i | 1866 | my $userList = $urlpath->newFromModule("${ipfx}UserList", %args); |
| 504 : | |||
| 505 : | # set list links | ||
| 506 : | my $setList = $urlpath->newFromModule("${ipfx}ProblemSetList", %args); | ||
| 507 : | my $setDetail = $urlpath->newFromModule("${ipfx}ProblemSetEditor", %args, setID => $setID); | ||
| 508 : | my $problemEditor = $urlpath->newFromModule("${ipfx}PGProblemEditor", %args, setID => $setID, problemID => $problemID); | ||
| 509 : | |||
| 510 : | sh002i | 1918 | my $assigner = $urlpath->newFromModule("${ipfx}Assigner", %args); |
| 511 : | sh002i | 1866 | my $mail = $urlpath->newFromModule("${ipfx}SendMail", %args); |
| 512 : | my $scoring = $urlpath->newFromModule("${ipfx}Scoring", %args); | ||
| 513 : | |||
| 514 : | # statistics links | ||
| 515 : | my $stats = $urlpath->newFromModule("${ipfx}Stats", %args); | ||
| 516 : | my $userStats = $urlpath->newFromModule("${ipfx}Stats", %args, statType => "student", userID => $userID); | ||
| 517 : | my $setStats = $urlpath->newFromModule("${ipfx}Stats", %args, statType => "set", setID => $setID); | ||
| 518 : | |||
| 519 : | my $files = $urlpath->newFromModule("${ipfx}FileXfer", %args); | ||
| 520 : | |||
| 521 : | sh002i | 1880 | print CGI::start_li(); |
| 522 : | print CGI::span({style=>"font-size:larger"}, CGI::a({href=>$self->systemLink($instr)}, $instr->name)); | ||
| 523 : | print CGI::start_ul(); | ||
| 524 : | sh002i | 1918 | print CGI::li(CGI::a({href=>$self->systemLink($addUsers)}, $addUsers->name)); |
| 525 : | sh002i | 1880 | print CGI::li(CGI::a({href=>$self->systemLink($userList)}, $userList->name)); |
| 526 : | print CGI::start_li(); | ||
| 527 : | print CGI::a({href=>$self->systemLink($setList)}, $setList->name); | ||
| 528 : | sh002i | 1869 | if (defined $setID and $setID ne "") { |
| 529 : | sh002i | 1880 | print CGI::start_ul(); |
| 530 : | print CGI::start_li(); | ||
| 531 : | print CGI::a({href=>$self->systemLink($setDetail)}, $setID); | ||
| 532 : | sh002i | 1869 | if (defined $problemID and $problemID ne "") { |
| 533 : | sh002i | 1880 | print CGI::ul( |
| 534 : | sh002i | 1866 | CGI::li(CGI::a({href=>$self->systemLink($problemEditor)}, $problemID)) |
| 535 : | ); | ||
| 536 : | } | ||
| 537 : | sh002i | 1880 | print CGI::end_li(); |
| 538 : | print CGI::end_ul(); | ||
| 539 : | sh002i | 1866 | } |
| 540 : | sh002i | 1880 | print CGI::end_li(); |
| 541 : | sh002i | 1918 | print CGI::li(CGI::a({href=>$self->systemLink($assigner)}, $assigner->name)); |
| 542 : | sh002i | 1880 | print CGI::li(CGI::a({href=>$self->systemLink($mail)}, $mail->name)); |
| 543 : | print CGI::li(CGI::a({href=>$self->systemLink($scoring)}, $scoring->name)); | ||
| 544 : | print CGI::start_li(); | ||
| 545 : | print CGI::a({href=>$self->systemLink($stats)}, $stats->name); | ||
| 546 : | sh002i | 1869 | if (defined $userID and $userID ne "") { |
| 547 : | sh002i | 1880 | print CGI::ul( |
| 548 : | sh002i | 1866 | CGI::li(CGI::a({href=>$self->systemLink($userStats)}, $userID)) |
| 549 : | ); | ||
| 550 : | } | ||
| 551 : | sh002i | 1869 | if (defined $setID and $setID ne "") { |
| 552 : | sh002i | 1880 | print CGI::ul( |
| 553 : | sh002i | 1866 | CGI::li(CGI::a({href=>$self->systemLink($setStats)}, $setID)) |
| 554 : | ); | ||
| 555 : | } | ||
| 556 : | sh002i | 1880 | print CGI::end_li(); |
| 557 : | print CGI::li(CGI::a({href=>$self->systemLink($files)}, $files->name)); | ||
| 558 : | print CGI::end_ul(); | ||
| 559 : | print CGI::end_li(); | ||
| 560 : | sh002i | 1866 | } |
| 561 : | |||
| 562 : | sh002i | 1880 | print CGI::end_ul(); |
| 563 : | print "<!-- end " . __PACKAGE__ . "::links -->\n"; | ||
| 564 : | sh002i | 1866 | |
| 565 : | sh002i | 1880 | return ""; |
| 566 : | sh002i | 1866 | } |
| 567 : | |||
| 568 : | sh002i | 1873 | =item loginstatus() |
| 569 : | sh002i | 1861 | |
| 570 : | sh002i | 1873 | Defined in this package. |
| 571 : | sh002i | 1861 | |
| 572 : | sh002i | 1873 | Print a notification message announcing the current real user and effective |
| 573 : | user, a link to stop acting as the effective user, and a link to logout. | ||
| 574 : | sh002i | 1861 | |
| 575 : | =cut | ||
| 576 : | |||
| 577 : | sub loginstatus { | ||
| 578 : | sh002i | 1869 | my ($self) = @_; |
| 579 : | sh002i | 1873 | my $r = $self->r; |
| 580 : | sh002i | 1869 | my $urlpath = $r->urlpath; |
| 581 : | sh002i | 1861 | |
| 582 : | my $key = $r->param("key"); | ||
| 583 : | |||
| 584 : | sh002i | 1869 | if ($key) { |
| 585 : | my $courseID = $urlpath->arg("courseID"); | ||
| 586 : | my $userID = $r->param("user"); | ||
| 587 : | my $eUserID = $r->param("effectiveUser"); | ||
| 588 : | |||
| 589 : | sh002i | 1882 | my $stopActingURL = $self->systemLink($urlpath, # current path |
| 590 : | sh002i | 1911 | params => { effectiveUser => $userID }, |
| 591 : | sh002i | 1882 | ); |
| 592 : | sh002i | 1869 | my $logoutURL = $self->systemLink($urlpath->newFromModule(__PACKAGE__ . "::Logout", courseID => $courseID)); |
| 593 : | |||
| 594 : | sh002i | 1880 | print "\n<!-- BEGIN " . __PACKAGE__ . "::loginstatus -->\n"; |
| 595 : | |||
| 596 : | sh002i | 1869 | print "Logged in as $userID. "; |
| 597 : | print CGI::a({href=>$logoutURL}, "Log Out"); | ||
| 598 : | |||
| 599 : | if ($eUserID ne $userID) { | ||
| 600 : | print " | Acting as $eUserID. "; | ||
| 601 : | print CGI::a({href=>$stopActingURL}, "Stop Acting"); | ||
| 602 : | } | ||
| 603 : | sh002i | 1880 | |
| 604 : | print "<!-- END " . __PACKAGE__ . "::loginstatus -->\n"; | ||
| 605 : | sh002i | 1861 | } |
| 606 : | |||
| 607 : | return ""; | ||
| 608 : | } | ||
| 609 : | |||
| 610 : | sh002i | 1873 | =item nav($args) |
| 611 : | sh002i | 1869 | |
| 612 : | sh002i | 1873 | Not defined in this package. |
| 613 : | sh002i | 1861 | |
| 614 : | sh002i | 1873 | Links to the previous, next, and parent objects. |
| 615 : | sh002i | 1861 | |
| 616 : | sh002i | 1873 | $args is a reference to a hash containing the following fields: |
| 617 : | |||
| 618 : | sh002i | 1861 | style => text|image |
| 619 : | imageprefix => prefix to prepend to base image URL | ||
| 620 : | imagesuffix => suffix to append to base image URL | ||
| 621 : | separator => HTML to place in between links | ||
| 622 : | |||
| 623 : | sh002i | 1873 | If C<style> is "image", image URLs are constructed by prepending C<imageprefix> |
| 624 : | and postpending C<imagesuffix> to the image base names defined by the | ||
| 625 : | implementor. (Examples of base names include "Prev", "Next", "ProbSet", and | ||
| 626 : | "Up"). Each concatenated string should form an absolute URL to an image file. | ||
| 627 : | For example: | ||
| 628 : | sh002i | 1861 | |
| 629 : | sh002i | 1873 | <!--#nav style="images" imageprefix="/webwork2_files/images/nav" |
| 630 : | imagesuffix=".gif" separator=" "--> | ||
| 631 : | sh002i | 1861 | |
| 632 : | sh002i | 1873 | =cut |
| 633 : | sh002i | 1861 | |
| 634 : | sh002i | 1873 | #sub nav { } |
| 635 : | sh002i | 1861 | |
| 636 : | sh002i | 1873 | =item options() |
| 637 : | |||
| 638 : | Not defined in this package. | ||
| 639 : | |||
| 640 : | Print an auxiliary options form, related to the content displayed in the | ||
| 641 : | C<body>. | ||
| 642 : | |||
| 643 : | =item path($args) | ||
| 644 : | |||
| 645 : | Defined in this package. | ||
| 646 : | |||
| 647 : | Print "breadcrubs" from the root of the virtual hierarchy to the current page. | ||
| 648 : | $args is a reference to a hash containing the following fields: | ||
| 649 : | |||
| 650 : | sh002i | 1861 | style => type of separator: text|image |
| 651 : | sh002i | 1873 | image => if style=image, URL of image to use as path separator |
| 652 : | text => if style=text, text to use as path separator | ||
| 653 : | if style=image, the ALT text of each separator image | ||
| 654 : | textonly => suppress all HTML, return only plain text | ||
| 655 : | sh002i | 1861 | |
| 656 : | sh002i | 1873 | The implementation in this package takes information from the WeBWorK::URLPath |
| 657 : | associated with the current request. | ||
| 658 : | |||
| 659 : | sh002i | 1861 | =cut |
| 660 : | |||
| 661 : | sub path { | ||
| 662 : | my ($self, $args) = @_; | ||
| 663 : | sh002i | 1873 | my $r = $self->r; |
| 664 : | sh002i | 1861 | |
| 665 : | my @path; | ||
| 666 : | |||
| 667 : | my $urlpath = $r->urlpath; | ||
| 668 : | do { | ||
| 669 : | unshift @path, $urlpath->name, $r->location . $urlpath->path; | ||
| 670 : | } while ($urlpath = $urlpath->parent); | ||
| 671 : | |||
| 672 : | $path[$#path] = ""; # we don't want the last path element to be a link | ||
| 673 : | |||
| 674 : | sh002i | 1882 | #print "\n<!-- BEGIN " . __PACKAGE__ . "::path -->\n"; |
| 675 : | sh002i | 1880 | print $self->pathMacro($args, @path); |
| 676 : | sh002i | 1882 | #print "<!-- END " . __PACKAGE__ . "::path -->\n"; |
| 677 : | sh002i | 1880 | |
| 678 : | return ""; | ||
| 679 : | sh002i | 1861 | } |
| 680 : | |||
| 681 : | sh002i | 1873 | =item siblings() |
| 682 : | sh002i | 1861 | |
| 683 : | sh002i | 1873 | Not defined in this package. |
| 684 : | sh002i | 1861 | |
| 685 : | sh002i | 1873 | Print links to siblings of the current object. |
| 686 : | sh002i | 1861 | |
| 687 : | sh002i | 1873 | =cut |
| 688 : | sh002i | 1861 | |
| 689 : | sh002i | 1873 | #sub siblings { } |
| 690 : | |||
| 691 : | jj | 1942 | =item timestamp() |
| 692 : | |||
| 693 : | Defined in this package. | ||
| 694 : | |||
| 695 : | Display the current time and date using default format "3:37pm on Jan 7, 2004". | ||
| 696 : | The display format can be adjusted by giving a style in the template. | ||
| 697 : | For example, | ||
| 698 : | |||
| 699 : | <!--#timestamp style="%m/%d/%y at %I:%M%P"--> | ||
| 700 : | |||
| 701 : | will give standard WeBWorK time format. Wording and other formatting | ||
| 702 : | can be done in the template itself. | ||
| 703 : | =cut | ||
| 704 : | |||
| 705 : | sub timestamp { | ||
| 706 : | my ($self, $args) = @_; | ||
| 707 : | my $formatstring = "%l:%M%P on %b %e, %Y"; | ||
| 708 : | $formatstring = $args->{style} if(defined($args->{style})); | ||
| 709 : | return(Date::Format::time2str($formatstring, time())); | ||
| 710 : | } | ||
| 711 : | |||
| 712 : | sh002i | 1873 | =item submiterror() |
| 713 : | |||
| 714 : | Defined in this package. | ||
| 715 : | |||
| 716 : | Print any error messages resulting from the last form submission. | ||
| 717 : | |||
| 718 : | The implementation in this package prints the value of the field | ||
| 719 : | $self->{submitError}, if it is present. | ||
| 720 : | |||
| 721 : | sh002i | 1861 | =cut |
| 722 : | |||
| 723 : | sub submiterror { | ||
| 724 : | my ($self) = @_; | ||
| 725 : | sh002i | 1880 | |
| 726 : | print "\n<!-- BEGIN " . __PACKAGE__ . "::submiterror -->\n"; | ||
| 727 : | print $self->{submitError} if exists $self->{submitError}; | ||
| 728 : | print "<!-- END " . __PACKAGE__ . "::submiterror -->\n"; | ||
| 729 : | |||
| 730 : | return ""; | ||
| 731 : | sh002i | 1861 | } |
| 732 : | |||
| 733 : | sh002i | 1873 | =item title() |
| 734 : | sh002i | 1861 | |
| 735 : | sh002i | 1873 | Defined in this package. |
| 736 : | sh002i | 1861 | |
| 737 : | sh002i | 1873 | Print the title of the current page. |
| 738 : | |||
| 739 : | The implementation in this package takes information from the WeBWorK::URLPath | ||
| 740 : | associated with the current request. | ||
| 741 : | |||
| 742 : | sh002i | 1861 | =cut |
| 743 : | |||
| 744 : | sub title { | ||
| 745 : | my ($self, $args) = @_; | ||
| 746 : | sh002i | 1873 | my $r = $self->r; |
| 747 : | sh002i | 1861 | |
| 748 : | sh002i | 1880 | |
| 749 : | sh002i | 1882 | #print "\n<!-- BEGIN " . __PACKAGE__ . "::title -->\n"; |
| 750 : | sh002i | 1880 | print $r->urlpath->name; |
| 751 : | sh002i | 1882 | #print "<!-- END " . __PACKAGE__ . "::title -->\n"; |
| 752 : | sh002i | 1880 | |
| 753 : | return ""; | ||
| 754 : | sh002i | 1861 | } |
| 755 : | |||
| 756 : | sh002i | 1873 | =item warnings() |
| 757 : | sh002i | 1861 | |
| 758 : | sh002i | 1873 | Defined in this package. |
| 759 : | sh002i | 1861 | |
| 760 : | sh002i | 1873 | Print accumulated warnings. |
| 761 : | |||
| 762 : | The implementation in this package checks for a note in the request named | ||
| 763 : | "warnings". If present, its contents are formatted and returned. | ||
| 764 : | |||
| 765 : | sh002i | 1861 | =cut |
| 766 : | |||
| 767 : | sub warnings { | ||
| 768 : | my ($self) = @_; | ||
| 769 : | sh002i | 1873 | my $r = $self->r; |
| 770 : | sh002i | 1880 | |
| 771 : | print "\n<!-- BEGIN " . __PACKAGE__ . "::warnings -->\n"; | ||
| 772 : | print $self->warningOutput($r->notes("warnings")) if $r->notes("warnings"); | ||
| 773 : | print "<!-- END " . __PACKAGE__ . "::warnings -->\n"; | ||
| 774 : | |||
| 775 : | return ""; | ||
| 776 : | sh002i | 1861 | } |
| 777 : | |||
| 778 : | =back | ||
| 779 : | |||
| 780 : | sh002i | 1873 | =cut |
| 781 : | sh002i | 1861 | |
| 782 : | sh002i | 1873 | # ------------------------------------------------------------------------------ |
| 783 : | sh002i | 1861 | |
| 784 : | sh002i | 1873 | =head2 Conditional predicates |
| 785 : | |||
| 786 : | Conditional predicate methods are invoked when the C<#if> escape sequence is | ||
| 787 : | encountered in the template. If a method named C<if_predicate> is defined in | ||
| 788 : | here or in the instantiated subclass, it is invoked. | ||
| 789 : | |||
| 790 : | sh002i | 1861 | The following predicates are currently defined: |
| 791 : | |||
| 792 : | =over | ||
| 793 : | |||
| 794 : | sh002i | 1873 | =item if_can($function) |
| 795 : | sh002i | 1861 | |
| 796 : | sh002i | 1873 | If a function named $function is present in the current content generator (or |
| 797 : | any superclass), a true value is returned. Otherwise, a false value is returned. | ||
| 798 : | sh002i | 1861 | |
| 799 : | sh002i | 1873 | The implementation in this package uses the method UNIVERSAL->can(function) to |
| 800 : | arrive at the result. | ||
| 801 : | |||
| 802 : | A subclass could redefine this method to, for example, "hide" a method from the | ||
| 803 : | template: | ||
| 804 : | |||
| 805 : | sub if_can { | ||
| 806 : | my ($self, $arg) = @_; | ||
| 807 : | |||
| 808 : | if ($arg eq "floobar") { | ||
| 809 : | return 0; | ||
| 810 : | } else { | ||
| 811 : | return $self->SUPER::if_can($arg); | ||
| 812 : | } | ||
| 813 : | } | ||
| 814 : | |||
| 815 : | sh002i | 1861 | =cut |
| 816 : | |||
| 817 : | sh002i | 1873 | sub if_can { |
| 818 : | my ($self, $arg) = @_; | ||
| 819 : | malsyned | 525 | |
| 820 : | sh002i | 1873 | return $self->can($arg) ? 1 : 0; |
| 821 : | malsyned | 525 | } |
| 822 : | |||
| 823 : | sh002i | 1873 | =item if_loggedin($arg) |
| 824 : | sh002i | 1861 | |
| 825 : | sh002i | 1873 | If the user is currently logged in, $arg is returned. Otherwise, the inverse of |
| 826 : | $arg is returned. | ||
| 827 : | sh002i | 1861 | |
| 828 : | sh002i | 1873 | The implementation in this package always returns $arg, since most content |
| 829 : | generators are only reachable when the user is authenticated. It is up to | ||
| 830 : | classes that can be reached without logging in to override this method and | ||
| 831 : | provide the correct behavior. | ||
| 832 : | |||
| 833 : | This is suboptimal, and may change in the future. | ||
| 834 : | |||
| 835 : | sh002i | 1861 | =cut |
| 836 : | |||
| 837 : | sh002i | 1873 | sub if_loggedin { |
| 838 : | my ($self, $arg) = @_; | ||
| 839 : | malsyned | 748 | |
| 840 : | return $arg; | ||
| 841 : | } | ||
| 842 : | |||
| 843 : | sh002i | 1873 | =item if_submiterror($arg) |
| 844 : | sh002i | 1131 | |
| 845 : | sh002i | 1873 | If the last form submission generated an error, $arg is returned. Otherwise, the |
| 846 : | inverse of $arg is returned. | ||
| 847 : | |||
| 848 : | The implementation in this package checks for the field $self->{submitError} to | ||
| 849 : | determine if an error condition is present. | ||
| 850 : | |||
| 851 : | If a subclass uses some other method to classify submission results, this method could be | ||
| 852 : | redefined to handle that variance: | ||
| 853 : | |||
| 854 : | sub if_submiterror { | ||
| 855 : | my ($self, $arg) = @_; | ||
| 856 : | |||
| 857 : | my $status = $self->{processReturnValue}; | ||
| 858 : | if ($status != 0) { | ||
| 859 : | return $arg; | ||
| 860 : | } else { | ||
| 861 : | return !$arg; | ||
| 862 : | } | ||
| 863 : | } | ||
| 864 : | |||
| 865 : | sh002i | 1861 | =cut |
| 866 : | |||
| 867 : | sh002i | 1873 | sub if_submiterror { |
| 868 : | malsyned | 1017 | my ($self, $arg) = @_; |
| 869 : | sh002i | 1873 | |
| 870 : | malsyned | 1017 | if (exists $self->{submitError}) { |
| 871 : | return $arg; | ||
| 872 : | } else { | ||
| 873 : | return !$arg; | ||
| 874 : | } | ||
| 875 : | } | ||
| 876 : | |||
| 877 : | sh002i | 1861 | =item if_warnings |
| 878 : | sh002i | 1131 | |
| 879 : | sh002i | 1873 | If warnings have been emitted while handling this request, $arg is returned. |
| 880 : | Otherwise, the inverse of $arg is returned. | ||
| 881 : | |||
| 882 : | The implementation in this package checks for a note in the request named | ||
| 883 : | "warnings". This is set by the WARN handler in Apache::WeBWorK when a warning is | ||
| 884 : | handled. | ||
| 885 : | |||
| 886 : | sh002i | 1869 | =cut |
| 887 : | |||
| 888 : | sh002i | 1873 | sub if_warnings { |
| 889 : | sh002i | 1131 | my ($self, $arg) = @_; |
| 890 : | sh002i | 1873 | my $r = $self->r; |
| 891 : | |||
| 892 : | if ($r->notes("warnings")) { | ||
| 893 : | return $arg; | ||
| 894 : | } else { | ||
| 895 : | !$arg; | ||
| 896 : | } | ||
| 897 : | sh002i | 1131 | } |
| 898 : | |||
| 899 : | sh002i | 1861 | =back |
| 900 : | sh002i | 1131 | |
| 901 : | sh002i | 1861 | =cut |
| 902 : | |||
| 903 : | sh002i | 1873 | ################################################################################ |
| 904 : | malsyned | 508 | |
| 905 : | sh002i | 1873 | =head1 HTML MACROS |
| 906 : | malsyned | 508 | |
| 907 : | sh002i | 1873 | Various routines are defined in this package for rendering common WeBWorK |
| 908 : | idioms. | ||
| 909 : | |||
| 910 : | FIXME: some of these should be moved to WeBWorK::HTML:: modules! | ||
| 911 : | |||
| 912 : | # ------------------------------------------------------------------------------ | ||
| 913 : | |||
| 914 : | =head2 Template escape handler macros | ||
| 915 : | |||
| 916 : | These methods are used by implementations of the escape sequence handlers to | ||
| 917 : | maintain a consistent style. | ||
| 918 : | |||
| 919 : | =over | ||
| 920 : | |||
| 921 : | =item pathMacro($args, @path) | ||
| 922 : | |||
| 923 : | Helper macro for the C<#path> escape sequence: $args is a hash reference | ||
| 924 : | containing the "style", "image", "text", and "textonly" arguments to the escape. | ||
| 925 : | @path consists of ordered key-value pairs of the form: | ||
| 926 : | |||
| 927 : | "Page Name" => URL | ||
| 928 : | |||
| 929 : | If the page should not have a link associated with it, the URL should be left | ||
| 930 : | empty. Authentication data is added to each URL so you don't have to. A fully- | ||
| 931 : | formed path line is returned, suitable for returning by a function implementing | ||
| 932 : | the C<#path> escape. | ||
| 933 : | |||
| 934 : | FIXME: authentication data probably shouldn't be added here any more, now that | ||
| 935 : | we have systemLink(). | ||
| 936 : | |||
| 937 : | =cut | ||
| 938 : | |||
| 939 : | sub pathMacro { | ||
| 940 : | my ($self, $args, @path) = @_; | ||
| 941 : | my %args = %$args; | ||
| 942 : | $args{style} = "text" if $args{textonly}; | ||
| 943 : | |||
| 944 : | my $auth = $self->url_authen_args; | ||
| 945 : | my $sep; | ||
| 946 : | if ($args{style} eq "image") { | ||
| 947 : | $sep = CGI::img({-src=>$args{image}, -alt=>$args{text}}); | ||
| 948 : | } else { | ||
| 949 : | $sep = $args{text}; | ||
| 950 : | } | ||
| 951 : | |||
| 952 : | my @result; | ||
| 953 : | while (@path) { | ||
| 954 : | my $name = shift @path; | ||
| 955 : | my $url = shift @path; | ||
| 956 : | if ($url and not $args{textonly}) { | ||
| 957 : | push @result, CGI::a({-href=>"$url?$auth"}, $name); | ||
| 958 : | } else { | ||
| 959 : | push @result, $name; | ||
| 960 : | } | ||
| 961 : | } | ||
| 962 : | |||
| 963 : | return join($sep, @result), "\n"; | ||
| 964 : | } | ||
| 965 : | |||
| 966 : | =item siblingsMacro(@siblings) | ||
| 967 : | |||
| 968 : | Helper macro for the C<#siblings> escape sequence. @siblings consists of ordered | ||
| 969 : | key-value pairs of the form: | ||
| 970 : | |||
| 971 : | "Sibling Name" => URL | ||
| 972 : | |||
| 973 : | If the sibling should not have a link associated with it, the URL should be left | ||
| 974 : | empty. Authentication data is added to each URL so you don't have to. A fully- | ||
| 975 : | formed siblings block is returned, suitable for returning by a function | ||
| 976 : | implementing the C<#siblings> escape. | ||
| 977 : | |||
| 978 : | FIXME: authentication data probably shouldn't be added here any more, now that | ||
| 979 : | we have systemLink(). | ||
| 980 : | |||
| 981 : | =cut | ||
| 982 : | |||
| 983 : | sub siblingsMacro { | ||
| 984 : | my ($self, @siblings) = @_; | ||
| 985 : | |||
| 986 : | my $auth = $self->url_authen_args; | ||
| 987 : | my $sep = CGI::br(); | ||
| 988 : | |||
| 989 : | my @result; | ||
| 990 : | while (@siblings) { | ||
| 991 : | my $name = shift @siblings; | ||
| 992 : | my $url = shift @siblings; | ||
| 993 : | push @result, $url | ||
| 994 : | ? CGI::a({-href=>"$url?$auth"}, $name) | ||
| 995 : | : $name; | ||
| 996 : | } | ||
| 997 : | |||
| 998 : | return join($sep, @result) . "\n"; | ||
| 999 : | } | ||
| 1000 : | |||
| 1001 : | =item navMacro($args, $tail, @links) | ||
| 1002 : | |||
| 1003 : | Helper macro for the C<#nav> escape sequence: $args is a hash reference | ||
| 1004 : | containing the "style", "imageprefix", "imagesuffix", and "separator" arguments | ||
| 1005 : | to the escape. @siblings consists of ordered tuples of the form: | ||
| 1006 : | |||
| 1007 : | "Link Name", URL, ImageBaseName | ||
| 1008 : | |||
| 1009 : | If the sibling should not have a link associated with it, the URL should be left | ||
| 1010 : | empty. ImageBaseName is placed between the C<imageprefix> and C<imagesuffix>. | ||
| 1011 : | Authentication data is added to each URL so you don't have to. $tail is appended | ||
| 1012 : | to each URL, after the authentication information. A fully-formed nav line is | ||
| 1013 : | returned, suitable for returning by a function implementing the C<#nav> escape. | ||
| 1014 : | |||
| 1015 : | =cut | ||
| 1016 : | |||
| 1017 : | sub navMacro { | ||
| 1018 : | my ($self, $args, $tail, @links) = @_; | ||
| 1019 : | my $r = $self->r; | ||
| 1020 : | my $ce = $r->ce; | ||
| 1021 : | my %args = %$args; | ||
| 1022 : | |||
| 1023 : | my $auth = $self->url_authen_args; | ||
| 1024 : | my $prefix = $ce->{webworkURLs}->{htdocs}."/images"; | ||
| 1025 : | |||
| 1026 : | my @result; | ||
| 1027 : | while (@links) { | ||
| 1028 : | my $name = shift @links; | ||
| 1029 : | my $url = shift @links; | ||
| 1030 : | my $img = shift @links; | ||
| 1031 : | my $html = | ||
| 1032 : | ($img && $args{style} eq "images") | ||
| 1033 : | ? CGI::img( | ||
| 1034 : | {src=>($prefix."/".$img.$args{imagesuffix}), | ||
| 1035 : | border=>"", | ||
| 1036 : | alt=>"$name"}) | ||
| 1037 : | : $name; | ||
| 1038 : | unless($img && !$url) { | ||
| 1039 : | push @result, $url | ||
| 1040 : | ? CGI::a({-href=>"$url?$auth$tail"}, $html) | ||
| 1041 : | : $html; | ||
| 1042 : | } | ||
| 1043 : | } | ||
| 1044 : | |||
| 1045 : | return join($args{separator}, @result) . "\n"; | ||
| 1046 : | } | ||
| 1047 : | |||
| 1048 : | =back | ||
| 1049 : | |||
| 1050 : | =cut | ||
| 1051 : | |||
| 1052 : | # ------------------------------------------------------------------------------ | ||
| 1053 : | |||
| 1054 : | =head2 Parameter management | ||
| 1055 : | |||
| 1056 : | Methods for formatting request parameters as hidden form fields or query string | ||
| 1057 : | fragments. | ||
| 1058 : | |||
| 1059 : | =over | ||
| 1060 : | |||
| 1061 : | =item hidden_fields(@fields) | ||
| 1062 : | |||
| 1063 : | Return hidden <INPUT> tags for each field mentioned in @fields (or all fields if | ||
| 1064 : | list is empty), taking data from the current request. | ||
| 1065 : | |||
| 1066 : | =cut | ||
| 1067 : | |||
| 1068 : | sub hidden_fields { | ||
| 1069 : | my ($self, @fields) = @_; | ||
| 1070 : | my $r = $self->r; | ||
| 1071 : | |||
| 1072 : | @fields = $r->param unless @fields; | ||
| 1073 : | |||
| 1074 : | my $html = ""; | ||
| 1075 : | foreach my $param (@fields) { | ||
| 1076 : | my @values = $r->param($param); | ||
| 1077 : | $html .= CGI::hidden($param, @values); | ||
| 1078 : | } | ||
| 1079 : | return $html; | ||
| 1080 : | } | ||
| 1081 : | |||
| 1082 : | =item hidden_authen_fields() | ||
| 1083 : | |||
| 1084 : | Use hidden_fields to return hidden <INPUT> tags for request fields used in | ||
| 1085 : | authentication. | ||
| 1086 : | |||
| 1087 : | =cut | ||
| 1088 : | |||
| 1089 : | sub hidden_authen_fields { | ||
| 1090 : | my ($self) = @_; | ||
| 1091 : | |||
| 1092 : | return $self->hidden_fields("user", "effectiveUser", "key"); | ||
| 1093 : | } | ||
| 1094 : | |||
| 1095 : | =item url_args(@fields) | ||
| 1096 : | |||
| 1097 : | Return a URL query string (without the leading `?') containing values for each | ||
| 1098 : | field mentioned in @fields, or all fields if list is empty. Data is taken from | ||
| 1099 : | the current request. | ||
| 1100 : | |||
| 1101 : | =cut | ||
| 1102 : | |||
| 1103 : | sub url_args { | ||
| 1104 : | my ($self, @fields) = @_; | ||
| 1105 : | my $r = $self->r; | ||
| 1106 : | |||
| 1107 : | @fields = $r->param unless @fields; | ||
| 1108 : | |||
| 1109 : | my @pairs; | ||
| 1110 : | foreach my $param (@fields) { | ||
| 1111 : | my @values = $r->param($param); | ||
| 1112 : | foreach my $value (@values) { | ||
| 1113 : | push @pairs, uri_escape($param) . "=" . uri_escape($value); | ||
| 1114 : | } | ||
| 1115 : | } | ||
| 1116 : | |||
| 1117 : | return join("&", @pairs); | ||
| 1118 : | } | ||
| 1119 : | |||
| 1120 : | =item url_authen_args() | ||
| 1121 : | |||
| 1122 : | Use url_args to return a URL query string for request fields used in | ||
| 1123 : | authentication. | ||
| 1124 : | |||
| 1125 : | =cut | ||
| 1126 : | |||
| 1127 : | sub url_authen_args { | ||
| 1128 : | my ($self) = @_; | ||
| 1129 : | |||
| 1130 : | return $self->url_args("user", "effectiveUser", "key"); | ||
| 1131 : | } | ||
| 1132 : | |||
| 1133 : | =item print_form_data($begin, $middle, $end, $omit) | ||
| 1134 : | |||
| 1135 : | Return a string containing every request field not matched by the quoted reguar | ||
| 1136 : | expression $omit, placing $begin before each field name, $middle between each | ||
| 1137 : | field name and its value, and $end after each value. Values are taken from the | ||
| 1138 : | current request. | ||
| 1139 : | |||
| 1140 : | =cut | ||
| 1141 : | |||
| 1142 : | sub print_form_data { | ||
| 1143 : | my ($self, $begin, $middle, $end, $qr_omit) = @_; | ||
| 1144 : | my $r=$self->r; | ||
| 1145 : | my @form_data = $r->param; | ||
| 1146 : | |||
| 1147 : | my $return_string = ""; | ||
| 1148 : | foreach my $name (@form_data) { | ||
| 1149 : | next if ($qr_omit and $name =~ /$qr_omit/); | ||
| 1150 : | my @values = $r->param($name); | ||
| 1151 : | foreach my $variable (qw(begin name middle value end)) { | ||
| 1152 : | # FIXME: can this loop be moved out of the enclosing loop? | ||
| 1153 : | no strict 'refs'; | ||
| 1154 : | ${$variable} = "" unless defined ${$variable}; | ||
| 1155 : | } | ||
| 1156 : | foreach my $value (@values) { | ||
| 1157 : | $return_string .= "$begin$name$middle$value$end"; | ||
| 1158 : | } | ||
| 1159 : | } | ||
| 1160 : | |||
| 1161 : | return $return_string; | ||
| 1162 : | } | ||
| 1163 : | |||
| 1164 : | =back | ||
| 1165 : | |||
| 1166 : | =cut | ||
| 1167 : | |||
| 1168 : | # ------------------------------------------------------------------------------ | ||
| 1169 : | |||
| 1170 : | =head2 Utilities | ||
| 1171 : | |||
| 1172 : | =over | ||
| 1173 : | |||
| 1174 : | =item systemLink($urlpath, %options) | ||
| 1175 : | |||
| 1176 : | Generate a link to another part of the system. $urlpath is WeBWorK::URLPath | ||
| 1177 : | object from which the base path will be taken. %options can consist of: | ||
| 1178 : | |||
| 1179 : | =over | ||
| 1180 : | |||
| 1181 : | sh002i | 1882 | =item params |
| 1182 : | sh002i | 1873 | |
| 1183 : | sh002i | 1911 | Can be either a reference to an array or a reference to a hash. |
| 1184 : | sh002i | 1873 | |
| 1185 : | sh002i | 1911 | If it is a reference to a hash, it maps parmaeter names to values. These |
| 1186 : | parameters will be included in the generated link. If a value is an arrayref, | ||
| 1187 : | the values of the array referenced will be used. If a value is undefined, the | ||
| 1188 : | value from the current request will be used. | ||
| 1189 : | sh002i | 1873 | |
| 1190 : | sh002i | 1911 | If C<params> is an arrayref, it is interpreted as a list of parameter names. |
| 1191 : | These parameters will be included in the generated link, using the values from | ||
| 1192 : | the current request. | ||
| 1193 : | sh002i | 1873 | |
| 1194 : | sh002i | 1911 | Unless C<authen> is false (see below), the authentication parameters (C<user>, |
| 1195 : | C<effectiveUser>, and C<key>) are included with their default values. | ||
| 1196 : | sh002i | 1898 | |
| 1197 : | sh002i | 1882 | =item authen |
| 1198 : | sh002i | 1873 | |
| 1199 : | sh002i | 1911 | If set to a false value, the authentication parameters (C<user>, |
| 1200 : | C<effectiveUser>, and C<key>) are included in the the generated link unless | ||
| 1201 : | explicitly listed in C<params>. | ||
| 1202 : | sh002i | 1873 | |
| 1203 : | =back | ||
| 1204 : | |||
| 1205 : | =cut | ||
| 1206 : | |||
| 1207 : | sh002i | 1882 | # FIXME: there should probably be an option for prepending "http://hostname:port" |
| 1208 : | sh002i | 1873 | sub systemLink { |
| 1209 : | my ($self, $urlpath, %options) = @_; | ||
| 1210 : | my $r = $self->r; | ||
| 1211 : | |||
| 1212 : | sh002i | 1911 | my %params = (); |
| 1213 : | sh002i | 1882 | if (exists $options{params}) { |
| 1214 : | sh002i | 1911 | if (ref $options{params} eq "HASH") { |
| 1215 : | %params = %{ $options{params} }; | ||
| 1216 : | } elsif (ref $options{params} eq "ARRAY") { | ||
| 1217 : | my @names = @{ $options{params} }; | ||
| 1218 : | @params{@names} = (); | ||
| 1219 : | } else { | ||
| 1220 : | croak "option 'params' is not a hashref or an arrayref"; | ||
| 1221 : | } | ||
| 1222 : | sh002i | 1882 | } |
| 1223 : | sh002i | 1873 | |
| 1224 : | sh002i | 1893 | my $authen = exists $options{authen} ? $options{authen} : 1; |
| 1225 : | sh002i | 1911 | if ($authen) { |
| 1226 : | $params{user} = undef unless exists $params{user}; | ||
| 1227 : | $params{effectiveUser} = undef unless exists $params{effectiveUser}; | ||
| 1228 : | $params{key} = undef unless exists $params{key}; | ||
| 1229 : | sh002i | 1882 | } |
| 1230 : | |||
| 1231 : | sh002i | 1873 | my $url = $r->location . $urlpath->path; |
| 1232 : | sh002i | 1911 | my $first = 1; |
| 1233 : | sh002i | 1873 | |
| 1234 : | sh002i | 1911 | foreach my $name (keys %params) { |
| 1235 : | my $value = $params{$name}; | ||
| 1236 : | |||
| 1237 : | my @values; | ||
| 1238 : | if (defined $value) { | ||
| 1239 : | if (ref $value eq "ARRAY") { | ||
| 1240 : | @values = @$value; | ||
| 1241 : | } else { | ||
| 1242 : | @values = $value; | ||
| 1243 : | } | ||
| 1244 : | } elsif (defined $r->param($name)) { | ||
| 1245 : | @values = $r->param($name); | ||
| 1246 : | } | ||
| 1247 : | |||
| 1248 : | if (@values) { | ||
| 1249 : | if ($first) { | ||
| 1250 : | $url .= "?"; | ||
| 1251 : | $first = 0; | ||
| 1252 : | } else { | ||
| 1253 : | $url .= "&"; | ||
| 1254 : | } | ||
| 1255 : | $url .= join "&", map { "$name=$_" } @values; | ||
| 1256 : | } | ||
| 1257 : | sh002i | 1873 | } |
| 1258 : | |||
| 1259 : | return $url; | ||
| 1260 : | } | ||
| 1261 : | |||
| 1262 : | =item nbsp($string) | ||
| 1263 : | |||
| 1264 : | If string consists of only whitespace, the HTML entity C< > is returned. | ||
| 1265 : | Otherwise $string is returned. | ||
| 1266 : | |||
| 1267 : | =cut | ||
| 1268 : | |||
| 1269 : | sub nbsp { | ||
| 1270 : | my $self = shift; | ||
| 1271 : | my $str = shift; | ||
| 1272 : | sh002i | 1974 | (defined $str && $str =~/\S/) ? $str : ' '; |
| 1273 : | sh002i | 1873 | } |
| 1274 : | |||
| 1275 : | =item errorOutput($error, $details) | ||
| 1276 : | |||
| 1277 : | =cut | ||
| 1278 : | |||
| 1279 : | sub errorOutput($$$) { | ||
| 1280 : | my ($self, $error, $details) = @_; | ||
| 1281 : | return | ||
| 1282 : | CGI::h3("Software Error"), | ||
| 1283 : | CGI::p(<<EOF), | ||
| 1284 : | WeBWorK has encountered a software error while attempting to process this | ||
| 1285 : | problem. It is likely that there is an error in the problem itself. If you are | ||
| 1286 : | a student, contact your professor to have the error corrected. If you are a | ||
| 1287 : | professor, please consut the error output below for more informaiton. | ||
| 1288 : | EOF | ||
| 1289 : | # FIXME: this message shouldn't refer the the "problem" since it is for general error reporting | ||
| 1290 : | CGI::h3("Error messages"), CGI::p(CGI::tt($error)), | ||
| 1291 : | CGI::h3("Error context"), CGI::p(CGI::tt($details)); | ||
| 1292 : | } | ||
| 1293 : | |||
| 1294 : | =item warningOutput($warnings) | ||
| 1295 : | |||
| 1296 : | =cut | ||
| 1297 : | |||
| 1298 : | sub warningOutput($$) { | ||
| 1299 : | my ($self, $warnings) = @_; | ||
| 1300 : | |||
| 1301 : | my @warnings = split m/\n+/, $warnings; | ||
| 1302 : | |||
| 1303 : | return | ||
| 1304 : | CGI::h3("Software Warnings"), | ||
| 1305 : | CGI::p(<<EOF), | ||
| 1306 : | WeBWorK has encountered warnings while attempting to process this problem. It | ||
| 1307 : | is likely that this indicates an error or ambiguity in the problem itself. If | ||
| 1308 : | you are a student, contact your professor to have the problem corrected. If you | ||
| 1309 : | are a professor, please consut the warning output below for more informaiton. | ||
| 1310 : | EOF | ||
| 1311 : | # FIXME: this message shouldn't refer the the "problem" since it is for general warning reporting | ||
| 1312 : | CGI::h3("Warning messages"), | ||
| 1313 : | CGI::ul(CGI::li(\@warnings)); | ||
| 1314 : | } | ||
| 1315 : | |||
| 1316 : | =back | ||
| 1317 : | |||
| 1318 : | malsyned | 508 | =head1 AUTHOR |
| 1319 : | |||
| 1320 : | sh002i | 1873 | Written by Dennis Lambe Jr., malsyned (at) math.rochester.edu and Sam Hathaway, |
| 1321 : | sh002i (at) math.rochester.edu. | ||
| 1322 : | malsyned | 508 | |
| 1323 : | =cut | ||
| 1324 : | sh002i | 1873 | |
| 1325 : | 1; |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |