Annotation of loncom/interface/lonhelper.pm, revision 1.26
1.1 bowersj2 1: # The LearningOnline Network with CAPA
2: # .helper XML handler to implement the LON-CAPA helper
3: #
1.26 ! bowersj2 4: # $Id: lonhelper.pm,v 1.25 2003/05/08 19:52:43 bowersj2 Exp $
1.1 bowersj2 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # (Page Handler
29: #
30: # (.helper handler
31: #
32:
1.3 bowersj2 33: =pod
34:
35: =head1 lonhelper - HTML Helper framework for LON-CAPA
36:
37: Helpers, often known as "wizards", are well-established UI widgets that users
38: feel comfortable with. It can take a complicated multidimensional problem the
39: user has and turn it into a series of bite-sized one-dimensional questions.
40:
41: For developers, helpers provide an easy way to bundle little bits of functionality
42: for the user, without having to write the tedious state-maintenence code.
43:
44: Helpers are defined as XML documents, placed in the /home/httpd/html/adm/helpers
45: directory and having the .helper file extension. For examples, see that directory.
46:
47: All classes are in the Apache::lonhelper namespace.
48:
49: =head2 lonhelper XML file format
50:
51: A helper consists of a top-level <helper> tag which contains a series of states.
52: Each state contains one or more state elements, which are what the user sees, like
53: messages, resource selections, or date queries.
54:
55: The helper tag is required to have one attribute, "title", which is the name
56: of the helper itself, such as "Parameter helper".
57:
58: =head2 State tags
59:
60: State tags are required to have an attribute "name", which is the symbolic
1.7 bowersj2 61: name of the state and will not be directly seen by the user. The helper is
62: required to have one state named "START", which is the state the helper
1.5 bowersj2 63: will start with. By convention, this state should clearly describe what
1.3 bowersj2 64: the helper will do for the user, and may also include the first information
65: entry the user needs to do for the helper.
66:
67: State tags are also required to have an attribute "title", which is the
68: human name of the state, and will be displayed as the header on top of
69: the screen for the user.
70:
71: =head2 Example Helper Skeleton
72:
73: An example of the tags so far:
74:
75: <helper title="Example Helper">
76: <state name="START" title="Demonstrating the Example Helper">
77: <!-- notice this is the START state the wizard requires -->
78: </state>
79: <state name="GET_NAME" title="Enter Student Name">
80: </state>
81: </helper>
82:
83: Of course this does nothing. In order for the wizard to do something, it is
84: necessary to put actual elements into the wizard. Documentation for each
85: of these elements follows.
86:
1.13 bowersj2 87: =head2 Creating a Helper With Code, Not XML
88:
89: In some situations, such as the printing wizard (see lonprintout.pm),
90: writing the helper in XML would be too complicated, because of scope
91: issues or the fact that the code actually outweighs the XML. It is
92: possible to create a helper via code, though it is a little odd.
93:
94: Creating a helper via code is more like issuing commands to create
95: a helper then normal code writing. For instance, elements will automatically
96: be added to the last state created, so it's important to create the
97: states in the correct order.
98:
99: First, create a new helper:
100:
101: use Apache::lonhelper;
102:
103: my $helper = Apache::lonhelper::new->("Helper Title");
104:
105: Next you'll need to manually add states to the helper:
106:
107: Apache::lonhelper::state->new("STATE_NAME", "State's Human Title");
108:
109: You don't need to save a reference to it because all elements up until
110: the next state creation will automatically be added to this state.
111:
112: Elements are created by populating the $paramHash in
113: Apache::lonhelper::paramhash. To prevent namespace issues, retrieve
114: a reference to that has with getParamHash:
115:
116: my $paramHash = Apache::lonhelper::getParamHash();
117:
118: You will need to do this for each state you create.
119:
120: Populate the $paramHash with the parameters for the element you wish
121: to add next; the easiest way to find out what those entries are is
122: to read the code. Some common ones are 'variable' to record the variable
123: to store the results in, and NEXTSTATE to record a next state transition.
124:
125: Then create your element:
126:
127: $paramHash->{MESSAGETEXT} = "This is a message.";
128: Apache::lonhelper::message->new();
129:
130: The creation will take the $paramHash and bless it into a
131: Apache::lonhelper::message object. To create the next element, you need
132: to get a reference to the new, empty $paramHash:
133:
134: $paramHash = Apache::lonhelper::getParamHash();
135:
136: and you can repeat creating elements that way. You can add states
137: and elements as needed.
138:
139: See lonprintout.pm, subroutine printHelper for an example of this, where
140: we dynamically add some states to prevent security problems, for instance.
141:
142: Normally the machinery in the XML format is sufficient; dynamically
143: adding states can easily be done by wrapping the state in a <condition>
144: tag. This should only be used when the code dominates the XML content,
145: the code is so complicated that it is difficult to get access to
146: all of the information you need because of scoping issues, or so much
147: of the information used is persistent because would-be <exec> or
148: <eval> blocks that using the {DATA} mechanism results in hard-to-read
149: and -maintain code.
150:
151: It is possible to do some of the work with an XML fragment parsed by
1.17 bowersj2 152: lonxml; again, see lonprintout.pm for an example. In that case it is
153: imperative that you call B<Apache::lonhelper::registerHelperTags()>
154: before parsing XML fragments and B<Apache::lonhelper::unregisterHelperTags()>
155: when you are done. See lonprintout.pm for examples of this usage in the
156: printHelper subroutine.
1.13 bowersj2 157:
1.3 bowersj2 158: =cut
159:
1.1 bowersj2 160: package Apache::lonhelper;
1.2 bowersj2 161: use Apache::Constants qw(:common);
162: use Apache::File;
1.3 bowersj2 163: use Apache::lonxml;
1.2 bowersj2 164:
1.7 bowersj2 165: # Register all the tags with the helper, so the helper can
166: # push and pop them
167:
168: my @helperTags;
169:
170: sub register {
171: my ($namespace, @tags) = @_;
172:
173: for my $tag (@tags) {
174: push @helperTags, [$namespace, $tag];
175: }
176: }
177:
1.2 bowersj2 178: BEGIN {
1.7 bowersj2 179: Apache::lonxml::register('Apache::lonhelper',
180: ('helper'));
181: register('Apache::lonhelper', ('state'));
1.2 bowersj2 182: }
183:
1.7 bowersj2 184: # Since all helpers are only three levels deep (helper tag, state tag,
1.3 bowersj2 185: # substate type), it's easier and more readble to explicitly track
186: # those three things directly, rather then futz with the tag stack
187: # every time.
188: my $helper;
189: my $state;
190: my $substate;
1.4 bowersj2 191: # To collect parameters, the contents of the subtags are collected
192: # into this paramHash, then passed to the element object when the
193: # end of the element tag is located.
194: my $paramHash;
1.2 bowersj2 195:
1.25 bowersj2 196: # Note from Jeremy 5-8-2003: It is *vital* that the real handler be called
197: # as a subroutine from the handler, or very mysterious things might happen.
198: # I don't know exactly why, but it seems that the scope where the Apache
199: # server enters the perl handler is treated differently from the rest of
200: # the handler. This also seems to manifest itself in the debugger as entering
201: # the perl handler in seemingly random places (sometimes it starts in the
202: # compiling phase, sometimes in the handler execution phase where it runs
203: # the code and stepping into the "1;" the module ends with goes into the handler,
204: # sometimes starting directly with the handler); I think the cause is related.
205: # In the debugger, this means that breakpoints are ignored until you step into
206: # a function and get out of what must be a "faked up scope" in the Apache->
207: # mod_perl connection. In this code, it was manifesting itself in the existence
208: # of two seperate file-scoped $helper variables, one set to the value of the
209: # helper in the helper constructor, and one referenced by the handler on the
210: # "$helper->process()" line. The second was therefore never set, and was still
211: # undefined when I tried to call process on it.
212: # By pushing the "real handler" down into the "real scope", everybody except the
213: # actual handler function directly below this comment gets the same $helper and
214: # everybody is happy.
215: # The upshot of all of this is that for safety when a handler is using
216: # file-scoped variables in LON-CAPA, the handler should be pushed down one
217: # call level, as I do here, to ensure that the top-level handler function does
218: # not get a different file scope from the rest of the code.
219: sub handler {
220: my $r = shift;
221: return real_handler($r);
222: }
223:
1.13 bowersj2 224: # For debugging purposes, one can send a second parameter into this
225: # function, the 'uri' of the helper you wish to have rendered, and
226: # call this from other handlers.
1.25 bowersj2 227: sub real_handler {
1.3 bowersj2 228: my $r = shift;
1.13 bowersj2 229: my $uri = shift;
230: if (!defined($uri)) { $uri = $r->uri(); }
231: $ENV{'request.uri'} = $uri;
232: my $filename = '/home/httpd/html' . $uri;
1.2 bowersj2 233: my $fh = Apache::File->new($filename);
234: my $file;
1.3 bowersj2 235: read $fh, $file, 100000000;
236:
237: # Send header, don't cache this page
238: if ($r->header_only) {
239: if ($ENV{'browser.mathml'}) {
240: $r->content_type('text/xml');
241: } else {
242: $r->content_type('text/html');
243: }
244: $r->send_http_header;
245: return OK;
246: }
247: if ($ENV{'browser.mathml'}) {
248: $r->content_type('text/xml');
249: } else {
250: $r->content_type('text/html');
251: }
252: $r->send_http_header;
253: $r->rflush();
1.2 bowersj2 254:
1.3 bowersj2 255: # Discard result, we just want the objects that get created by the
256: # xml parsing
257: &Apache::lonxml::xmlparse($r, 'helper', $file);
1.2 bowersj2 258:
1.13 bowersj2 259: $helper->process();
260:
1.3 bowersj2 261: $r->print($helper->display());
1.7 bowersj2 262: return OK;
1.2 bowersj2 263: }
264:
1.13 bowersj2 265: sub registerHelperTags {
266: for my $tagList (@helperTags) {
267: Apache::lonxml::register($tagList->[0], $tagList->[1]);
268: }
269: }
270:
271: sub unregisterHelperTags {
272: for my $tagList (@helperTags) {
273: Apache::lonxml::deregister($tagList->[0], $tagList->[1]);
274: }
275: }
276:
1.2 bowersj2 277: sub start_helper {
278: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
279:
280: if ($target ne 'helper') {
281: return '';
282: }
1.7 bowersj2 283:
1.13 bowersj2 284: registerHelperTags();
285:
286: Apache::lonhelper::helper->new($token->[2]{'title'});
1.4 bowersj2 287: return '';
1.2 bowersj2 288: }
289:
290: sub end_helper {
291: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
292:
1.3 bowersj2 293: if ($target ne 'helper') {
294: return '';
295: }
1.7 bowersj2 296:
1.13 bowersj2 297: unregisterHelperTags();
1.7 bowersj2 298:
1.4 bowersj2 299: return '';
1.2 bowersj2 300: }
1.1 bowersj2 301:
1.3 bowersj2 302: sub start_state {
303: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
304:
305: if ($target ne 'helper') {
306: return '';
307: }
308:
1.13 bowersj2 309: Apache::lonhelper::state->new($token->[2]{'name'},
310: $token->[2]{'title'});
1.3 bowersj2 311: return '';
312: }
313:
1.13 bowersj2 314: # Use this to get the param hash from other files.
315: sub getParamHash {
316: return $paramHash;
317: }
318:
319: # Use this to get the helper, if implementing elements in other files
320: # (like lonprintout.pm)
321: sub getHelper {
322: return $helper;
323: }
324:
1.3 bowersj2 325: # don't need this, so ignore it
326: sub end_state {
327: return '';
328: }
329:
1.1 bowersj2 330: 1;
331:
1.3 bowersj2 332: package Apache::lonhelper::helper;
333:
334: use Digest::MD5 qw(md5_hex);
335: use HTML::Entities;
336: use Apache::loncommon;
337: use Apache::File;
338:
339: sub new {
340: my $proto = shift;
341: my $class = ref($proto) || $proto;
342: my $self = {};
343:
344: $self->{TITLE} = shift;
345:
1.25 bowersj2 346: Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
347:
1.3 bowersj2 348: # If there is a state from the previous form, use that. If there is no
349: # state, use the start state parameter.
350: if (defined $ENV{"form.CURRENT_STATE"})
351: {
352: $self->{STATE} = $ENV{"form.CURRENT_STATE"};
353: }
354: else
355: {
356: $self->{STATE} = "START";
357: }
358:
359: $self->{TOKEN} = $ENV{'form.TOKEN'};
360: # If a token was passed, we load that in. Otherwise, we need to create a
361: # new storage file
362: # Tried to use standard Tie'd hashes, but you can't seem to take a
363: # reference to a tied hash and write to it. I'd call that a wart.
364: if ($self->{TOKEN}) {
365: # Validate the token before trusting it
366: if ($self->{TOKEN} !~ /^[a-f0-9]{32}$/) {
367: # Not legit. Return nothing and let all hell break loose.
368: # User shouldn't be doing that!
369: return undef;
370: }
371:
372: # Get the hash.
373: $self->{FILENAME} = $Apache::lonnet::tmpdir . md5_hex($self->{TOKEN}); # Note the token is not the literal file
374:
375: my $file = Apache::File->new($self->{FILENAME});
376: my $contents = <$file>;
1.5 bowersj2 377:
378: # Now load in the contents
379: for my $value (split (/&/, $contents)) {
380: my ($name, $value) = split(/=/, $value);
381: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
382: $self->{VARS}->{$name} = $value;
383: }
384:
1.3 bowersj2 385: $file->close();
386: } else {
387: # Only valid if we're just starting.
388: if ($self->{STATE} ne 'START') {
389: return undef;
390: }
391: # Must create the storage
392: $self->{TOKEN} = md5_hex($ENV{'user.name'} . $ENV{'user.domain'} .
393: time() . rand());
394: $self->{FILENAME} = $Apache::lonnet::tmpdir . md5_hex($self->{TOKEN});
395: }
396:
397: # OK, we now have our persistent storage.
398:
399: if (defined $ENV{"form.RETURN_PAGE"})
400: {
401: $self->{RETURN_PAGE} = $ENV{"form.RETURN_PAGE"};
402: }
403: else
404: {
405: $self->{RETURN_PAGE} = $ENV{REFERER};
406: }
407:
408: $self->{STATES} = {};
409: $self->{DONE} = 0;
410:
1.9 bowersj2 411: # Used by various helpers for various things; see lonparm.helper
412: # for an example.
413: $self->{DATA} = {};
414:
1.13 bowersj2 415: $helper = $self;
416:
417: # Establish the $paramHash
418: $paramHash = {};
419:
1.3 bowersj2 420: bless($self, $class);
421: return $self;
422: }
423:
424: # Private function; returns a string to construct the hidden fields
425: # necessary to have the helper track state.
426: sub _saveVars {
427: my $self = shift;
428: my $result = "";
429: $result .= '<input type="hidden" name="CURRENT_STATE" value="' .
430: HTML::Entities::encode($self->{STATE}) . "\" />\n";
431: $result .= '<input type="hidden" name="TOKEN" value="' .
432: $self->{TOKEN} . "\" />\n";
433: $result .= '<input type="hidden" name="RETURN_PAGE" value="' .
434: HTML::Entities::encode($self->{RETURN_PAGE}) . "\" />\n";
435:
436: return $result;
437: }
438:
439: # Private function: Create the querystring-like representation of the stored
440: # data to write to disk.
441: sub _varsInFile {
442: my $self = shift;
443: my @vars = ();
444: for my $key (keys %{$self->{VARS}}) {
445: push @vars, &Apache::lonnet::escape($key) . '=' .
446: &Apache::lonnet::escape($self->{VARS}->{$key});
447: }
448: return join ('&', @vars);
449: }
450:
1.5 bowersj2 451: # Use this to declare variables.
452: # FIXME: Document this
453: sub declareVar {
454: my $self = shift;
455: my $var = shift;
456:
457: if (!defined($self->{VARS}->{$var})) {
458: $self->{VARS}->{$var} = '';
459: }
460:
461: my $envname = 'form.' . $var . '.forminput';
462: if (defined($ENV{$envname})) {
463: $self->{VARS}->{$var} = $ENV{$envname};
464: }
465: }
466:
1.3 bowersj2 467: sub changeState {
468: my $self = shift;
469: $self->{STATE} = shift;
470: }
471:
472: sub registerState {
473: my $self = shift;
474: my $state = shift;
475:
476: my $stateName = $state->name();
477: $self->{STATES}{$stateName} = $state;
478: }
479:
1.13 bowersj2 480: sub process {
1.3 bowersj2 481: my $self = shift;
482:
483: # Phase 1: Post processing for state of previous screen (which is actually
484: # the "current state" in terms of the helper variables), if it wasn't the
485: # beginning state.
486: if ($self->{STATE} ne "START" || $ENV{"form.SUBMIT"} eq "Next ->") {
487: my $prevState = $self->{STATES}{$self->{STATE}};
1.13 bowersj2 488: $prevState->postprocess();
1.3 bowersj2 489: }
490:
491: # Note, to handle errors in a state's input that a user must correct,
492: # do not transition in the postprocess, and force the user to correct
493: # the error.
494:
495: # Phase 2: Preprocess current state
496: my $startState = $self->{STATE};
1.17 bowersj2 497: my $state = $self->{STATES}->{$startState};
1.3 bowersj2 498:
1.13 bowersj2 499: # For debugging, print something here to determine if you're going
500: # to an undefined state.
1.3 bowersj2 501: if (!defined($state)) {
1.13 bowersj2 502: return;
1.3 bowersj2 503: }
504: $state->preprocess();
505:
506: # Phase 3: While the current state is different from the previous state,
507: # keep processing.
1.17 bowersj2 508: while ( $startState ne $self->{STATE} &&
509: defined($self->{STATES}->{$self->{STATE}}) )
1.3 bowersj2 510: {
511: $startState = $self->{STATE};
1.17 bowersj2 512: $state = $self->{STATES}->{$startState};
1.3 bowersj2 513: $state->preprocess();
514: }
515:
1.13 bowersj2 516: return;
517: }
518:
519: # 1: Do the post processing for the previous state.
520: # 2: Do the preprocessing for the current state.
521: # 3: Check to see if state changed, if so, postprocess current and move to next.
522: # Repeat until state stays stable.
523: # 4: Render the current state to the screen as an HTML page.
524: sub display {
525: my $self = shift;
526:
527: my $state = $self->{STATES}{$self->{STATE}};
528:
529: my $result = "";
530:
1.17 bowersj2 531: if (!defined($state)) {
532: $result = "<font color='#ff0000'>Error: state '$state' not defined!</font>";
533: return $result;
534: }
535:
1.3 bowersj2 536: # Phase 4: Display.
537: my $stateTitle = $state->title();
538: my $bodytag = &Apache::loncommon::bodytag("$self->{TITLE}",'','');
539:
540: $result .= <<HEADER;
541: <html>
542: <head>
543: <title>LON-CAPA Helper: $self->{TITLE}</title>
544: </head>
545: $bodytag
546: HEADER
1.14 bowersj2 547: if (!$state->overrideForm()) { $result.="<form name='helpform' method='GET'>"; }
1.3 bowersj2 548: $result .= <<HEADER;
549: <table border="0"><tr><td>
550: <h2><i>$stateTitle</i></h2>
551: HEADER
552:
553: if (!$state->overrideForm()) {
554: $result .= $self->_saveVars();
555: }
556: $result .= $state->render() . "<p> </p>";
557:
558: if (!$state->overrideForm()) {
559: $result .= '<center>';
560: if ($self->{STATE} ne $self->{START_STATE}) {
561: #$result .= '<input name="SUBMIT" type="submit" value="<- Previous" /> ';
562: }
563: if ($self->{DONE}) {
564: my $returnPage = $self->{RETURN_PAGE};
565: $result .= "<a href=\"$returnPage\">End Helper</a>";
566: }
567: else {
568: $result .= '<input name="back" type="button" ';
569: $result .= 'value="<- Previous" onclick="history.go(-1)" /> ';
570: $result .= '<input name="SUBMIT" type="submit" value="Next ->" />';
571: }
572: $result .= "</center>\n";
573: }
574:
1.13 bowersj2 575: #foreach my $key (keys %{$self->{VARS}}) {
576: # $result .= "|$key| -> " . $self->{VARS}->{$key} . "<br />";
577: #}
1.5 bowersj2 578:
1.3 bowersj2 579: $result .= <<FOOTER;
580: </td>
581: </tr>
582: </table>
583: </form>
584: </body>
585: </html>
586: FOOTER
587:
588: # Handle writing out the vars to the file
589: my $file = Apache::File->new('>'.$self->{FILENAME});
590: print $file $self->_varsInFile();
591:
592: return $result;
593: }
594:
595: 1;
596:
597: package Apache::lonhelper::state;
598:
599: # States bundle things together and are responsible for compositing the
1.4 bowersj2 600: # various elements together. It is not generally necessary for users to
601: # use the state object directly, so it is not perldoc'ed.
602:
603: # Basically, all the states do is pass calls to the elements and aggregate
604: # the results.
1.3 bowersj2 605:
606: sub new {
607: my $proto = shift;
608: my $class = ref($proto) || $proto;
609: my $self = {};
610:
611: $self->{NAME} = shift;
612: $self->{TITLE} = shift;
613: $self->{ELEMENTS} = [];
614:
615: bless($self, $class);
616:
617: $helper->registerState($self);
618:
1.13 bowersj2 619: $state = $self;
620:
1.3 bowersj2 621: return $self;
622: }
623:
624: sub name {
625: my $self = shift;
626: return $self->{NAME};
627: }
628:
629: sub title {
630: my $self = shift;
631: return $self->{TITLE};
632: }
633:
1.4 bowersj2 634: sub preprocess {
635: my $self = shift;
636: for my $element (@{$self->{ELEMENTS}}) {
637: $element->preprocess();
638: }
639: }
640:
1.6 bowersj2 641: # FIXME: Document that all postprocesses must return a true value or
642: # the state transition will be overridden
1.4 bowersj2 643: sub postprocess {
644: my $self = shift;
1.6 bowersj2 645:
646: # Save the state so we can roll it back if we need to.
647: my $originalState = $helper->{STATE};
648: my $everythingSuccessful = 1;
649:
1.4 bowersj2 650: for my $element (@{$self->{ELEMENTS}}) {
1.6 bowersj2 651: my $result = $element->postprocess();
652: if (!$result) { $everythingSuccessful = 0; }
653: }
654:
655: # If not all the postprocesses were successful, override
656: # any state transitions that may have occurred. It is the
657: # responsibility of the states to make sure they have
658: # error handling in that case.
659: if (!$everythingSuccessful) {
660: $helper->{STATE} = $originalState;
1.4 bowersj2 661: }
662: }
663:
1.13 bowersj2 664: # Override the form if any element wants to.
665: # two elements overriding the form will make a mess, but that should
666: # be considered helper author error ;-)
1.4 bowersj2 667: sub overrideForm {
1.13 bowersj2 668: my $self = shift;
669: for my $element (@{$self->{ELEMENTS}}) {
670: if ($element->overrideForm()) {
671: return 1;
672: }
673: }
1.4 bowersj2 674: return 0;
675: }
676:
677: sub addElement {
678: my $self = shift;
679: my $element = shift;
680:
681: push @{$self->{ELEMENTS}}, $element;
682: }
683:
684: sub render {
685: my $self = shift;
686: my @results = ();
687:
688: for my $element (@{$self->{ELEMENTS}}) {
689: push @results, $element->render();
690: }
691: return join("\n", @results);
692: }
693:
694: 1;
695:
696: package Apache::lonhelper::element;
697: # Support code for elements
698:
699: =pod
700:
701: =head2 Element Base Class
702:
703: The Apache::lonhelper::element base class provides support methods for
704: the elements to use, such as a multiple value processer.
705:
706: B<Methods>:
707:
708: =over 4
709:
710: =item * process_multiple_choices(formName, varName): Process the form
711: element named "formName" and place the selected items into the helper
712: variable named varName. This is for things like checkboxes or
713: multiple-selection listboxes where the user can select more then
714: one entry. The selected entries are delimited by triple pipes in
1.5 bowersj2 715: the helper variables, like this:
716:
717: CHOICE_1|||CHOICE_2|||CHOICE_3
1.4 bowersj2 718:
719: =back
720:
1.25 bowersj2 721: B<finalcode tag>
722:
723: Each element can contain a "finalcode" tag that, when the special FINAL
724: helper state is used, will be executed, surrounded by "sub { my $helper = shift;"
725: and "}". It is expected to return a string describing what it did, which
726: may be an empty string. See course initialization helper for an example. This is
727: generally intended for helpers like the course initialization helper, which consist
728: of several panels, each of which is performing some sort of bite-sized functionality.
729:
730: B<defaultvalue tag>
731:
732: Each element that accepts user input can contain a "defaultvalue" tag that,
733: when surrounded by "sub { my $helper = shift; my $state = shift; " and "}",
734: will form a subroutine that when called will provide a default value for
735: the element. How this value is interpreted by the element is specific to
736: the element itself, and possibly the settings the element has (such as
737: multichoice vs. single choice for <choices> tags).
738:
739: This is also intended for things like the course initialization wizard, where the
740: user is setting various parameters. By correctly grabbing current settings
741: and including them into the helper, it allows the user to come back to the
742: helper later and re-execute it, without needing to worry about overwriting
743: some setting accidentally.
744:
745: Again, see the course initialization helper for examples.
746:
1.4 bowersj2 747: =cut
748:
1.5 bowersj2 749: BEGIN {
1.7 bowersj2 750: &Apache::lonhelper::register('Apache::lonhelper::element',
1.25 bowersj2 751: ('nextstate', 'finalcode',
752: 'defaultvalue'));
1.5 bowersj2 753: }
754:
1.4 bowersj2 755: # Because we use the param hash, this is often a sufficent
756: # constructor
757: sub new {
758: my $proto = shift;
759: my $class = ref($proto) || $proto;
760: my $self = $paramHash;
761: bless($self, $class);
762:
763: $self->{PARAMS} = $paramHash;
764: $self->{STATE} = $state;
765: $state->addElement($self);
766:
767: # Ensure param hash is not reused
768: $paramHash = {};
769:
770: return $self;
771: }
772:
1.5 bowersj2 773: sub start_nextstate {
774: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
775:
776: if ($target ne 'helper') {
777: return '';
778: }
779:
780: $paramHash->{NEXTSTATE} = &Apache::lonxml::get_all_text('/nextstate',
781: $parser);
782: return '';
783: }
784:
785: sub end_nextstate { return ''; }
786:
1.25 bowersj2 787: sub start_finalcode {
788: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
789:
790: if ($target ne 'helper') {
791: return '';
792: }
793:
794: $paramHash->{FINAL_CODE} = &Apache::lonxml::get_all_text('/finalcode',
795: $parser);
796: return '';
797: }
798:
799: sub end_finalcode { return ''; }
800:
801: sub start_defaultvalue {
802: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
803:
804: if ($target ne 'helper') {
805: return '';
806: }
807:
808: $paramHash->{DEFAULT_VALUE} = &Apache::lonxml::get_all_text('/defaultvalue',
809: $parser);
810: $paramHash->{DEFAULT_VALUE} = 'sub { my $helper = shift; my $state = shift;' .
811: $paramHash->{DEFAULT_VALUE} . '}';
812: return '';
813: }
814:
815: sub end_defaultvalue { return ''; }
816:
1.4 bowersj2 817: sub preprocess {
818: return 1;
819: }
820:
821: sub postprocess {
822: return 1;
823: }
824:
825: sub render {
826: return '';
827: }
828:
1.13 bowersj2 829: sub overrideForm {
830: return 0;
831: }
832:
1.3 bowersj2 833: sub process_multiple_choices {
834: my $self = shift;
835: my $formname = shift;
836: my $var = shift;
837:
1.14 bowersj2 838: # Must extract values from data directly, as there
839: # may be more then one.
840: my @values;
841: for my $formparam (split (/&/, $ENV{QUERY_STRING})) {
842: my ($name, $value) = split(/=/, $formparam);
843: if ($name ne $formname) {
844: next;
1.3 bowersj2 845: }
1.14 bowersj2 846: $value =~ tr/+/ /;
847: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
848: push @values, $value;
1.3 bowersj2 849: }
1.14 bowersj2 850: $helper->{VARS}->{$var} = join('|||', @values);
1.3 bowersj2 851:
852: return;
853: }
854:
1.4 bowersj2 855: 1;
856:
857: package Apache::lonhelper::message;
858:
859: =pod
860:
861: =head2 Element: message
862:
863: Message elements display the contents of their <message_text> tags, and
1.5 bowersj2 864: transition directly to the state in the <nextstate> tag. Example:
1.4 bowersj2 865:
866: <message>
1.5 bowersj2 867: <nextstate>GET_NAME</nextstate>
1.4 bowersj2 868: <message_text>This is the <b>message</b> the user will see,
869: <i>HTML allowed</i>.</message_text>
870: </message>
871:
1.5 bowersj2 872: This will display the HTML message and transition to the <nextstate> if
1.7 bowersj2 873: given. The HTML will be directly inserted into the helper, so if you don't
1.4 bowersj2 874: want text to run together, you'll need to manually wrap the <message_text>
875: in <p> tags, or whatever is appropriate for your HTML.
876:
1.5 bowersj2 877: Message tags do not add in whitespace, so if you want it, you'll need to add
878: it into states. This is done so you can inline some elements, such as
879: the <date> element, right between two messages, giving the appearence that
880: the <date> element appears inline. (Note the elements can not be embedded
881: within each other.)
882:
1.4 bowersj2 883: This is also a good template for creating your own new states, as it has
884: very little code beyond the state template.
885:
886: =cut
887:
888: no strict;
889: @ISA = ("Apache::lonhelper::element");
890: use strict;
891:
892: BEGIN {
1.7 bowersj2 893: &Apache::lonhelper::register('Apache::lonhelper::message',
1.10 bowersj2 894: ('message'));
1.3 bowersj2 895: }
896:
1.5 bowersj2 897: sub new {
898: my $ref = Apache::lonhelper::element->new();
899: bless($ref);
900: }
1.4 bowersj2 901:
902: # CONSTRUCTION: Construct the message element from the XML
903: sub start_message {
904: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
905:
906: if ($target ne 'helper') {
907: return '';
908: }
1.10 bowersj2 909:
910: $paramHash->{MESSAGE_TEXT} = &Apache::lonxml::get_all_text('/message',
911: $parser);
912:
913: if (defined($token->[2]{'nextstate'})) {
914: $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
915: }
1.4 bowersj2 916: return '';
1.3 bowersj2 917: }
918:
1.10 bowersj2 919: sub end_message {
1.4 bowersj2 920: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
921:
922: if ($target ne 'helper') {
923: return '';
924: }
1.10 bowersj2 925: Apache::lonhelper::message->new();
926: return '';
1.5 bowersj2 927: }
928:
929: sub render {
930: my $self = shift;
931:
932: return $self->{MESSAGE_TEXT};
933: }
934: # If a NEXTSTATE was given, switch to it
935: sub postprocess {
936: my $self = shift;
937: if (defined($self->{NEXTSTATE})) {
938: $helper->changeState($self->{NEXTSTATE});
939: }
1.6 bowersj2 940:
941: return 1;
1.5 bowersj2 942: }
943: 1;
944:
945: package Apache::lonhelper::choices;
946:
947: =pod
948:
949: =head2 Element: choices
950:
951: Choice states provide a single choice to the user as a text selection box.
952: A "choice" is two pieces of text, one which will be displayed to the user
953: (the "human" value), and one which will be passed back to the program
954: (the "computer" value). For instance, a human may choose from a list of
955: resources on disk by title, while your program wants the file name.
956:
957: <choices> takes an attribute "variable" to control which helper variable
958: the result is stored in.
959:
960: <choices> takes an attribute "multichoice" which, if set to a true
961: value, will allow the user to select multiple choices.
962:
1.26 ! bowersj2 963: <choices> takes an attribute "allowempty" which, if set to a true
! 964: value, will allow the user to select none of the choices without raising
! 965: an error message.
! 966:
1.5 bowersj2 967: B<SUB-TAGS>
968:
969: <choices> can have the following subtags:
970:
971: =over 4
972:
973: =item * <nextstate>state_name</nextstate>: If given, this will cause the
974: choice element to transition to the given state after executing. If
975: this is used, do not pass nextstates to the <choice> tag.
976:
977: =item * <choice />: If the choices are static,
978: this element will allow you to specify them. Each choice
979: contains attribute, "computer", as described above. The
980: content of the tag will be used as the human label.
981: For example,
982: <choice computer='234-12-7312'>Bobby McDormik</choice>.
983:
1.13 bowersj2 984: <choice> can take a parameter "eval", which if set to
985: a true value, will cause the contents of the tag to be
986: evaluated as it would be in an <eval> tag; see <eval> tag
987: below.
988:
1.5 bowersj2 989: <choice> may optionally contain a 'nextstate' attribute, which
990: will be the state transisitoned to if the choice is made, if
991: the choice is not multichoice.
992:
993: =back
994:
995: To create the choices programmatically, either wrap the choices in
996: <condition> tags (prefered), or use an <exec> block inside the <choice>
997: tag. Store the choices in $state->{CHOICES}, which is a list of list
998: references, where each list has three strings. The first is the human
999: name, the second is the computer name. and the third is the option
1000: next state. For example:
1001:
1002: <exec>
1003: for (my $i = 65; $i < 65 + 26; $i++) {
1004: push @{$state->{CHOICES}}, [chr($i), $i, 'next'];
1005: }
1006: </exec>
1007:
1008: This will allow the user to select from the letters A-Z (in ASCII), while
1009: passing the ASCII value back into the helper variables, and the state
1010: will in all cases transition to 'next'.
1011:
1012: You can mix and match methods of creating choices, as long as you always
1013: "push" onto the choice list, rather then wiping it out. (You can even
1014: remove choices programmatically, but that would probably be bad form.)
1015:
1.25 bowersj2 1016: B<defaultvalue support>
1017:
1018: Choices supports default values both in multichoice and single choice mode.
1019: In single choice mode, have the defaultvalue tag's function return the
1020: computer value of the box you want checked. If the function returns a value
1021: that does not correspond to any of the choices, the default behavior of selecting
1022: the first choice will be preserved.
1023:
1024: For multichoice, return a string with the computer values you want checked,
1025: delimited by triple pipes. Note this matches how the result of the <choices>
1026: tag is stored in the {VARS} hash.
1027:
1.5 bowersj2 1028: =cut
1029:
1030: no strict;
1031: @ISA = ("Apache::lonhelper::element");
1032: use strict;
1033:
1034: BEGIN {
1.7 bowersj2 1035: &Apache::lonhelper::register('Apache::lonhelper::choices',
1.5 bowersj2 1036: ('choice', 'choices'));
1037: }
1038:
1039: sub new {
1040: my $ref = Apache::lonhelper::element->new();
1041: bless($ref);
1042: }
1043:
1044: # CONSTRUCTION: Construct the message element from the XML
1045: sub start_choices {
1046: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1047:
1048: if ($target ne 'helper') {
1049: return '';
1050: }
1051:
1052: # Need to initialize the choices list, so everything can assume it exists
1.24 sakharuk 1053: $paramHash->{'variable'} = $token->[2]{'variable'} if (!defined($paramHash->{'variable'}));
1.5 bowersj2 1054: $helper->declareVar($paramHash->{'variable'});
1055: $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.26 ! bowersj2 1056: $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.5 bowersj2 1057: $paramHash->{CHOICES} = [];
1058: return '';
1059: }
1060:
1061: sub end_choices {
1062: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1063:
1064: if ($target ne 'helper') {
1065: return '';
1066: }
1067: Apache::lonhelper::choices->new();
1068: return '';
1069: }
1070:
1071: sub start_choice {
1072: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1073:
1074: if ($target ne 'helper') {
1075: return '';
1076: }
1077:
1078: my $computer = $token->[2]{'computer'};
1079: my $human = &Apache::lonxml::get_all_text('/choice',
1080: $parser);
1081: my $nextstate = $token->[2]{'nextstate'};
1.13 bowersj2 1082: my $evalFlag = $token->[2]{'eval'};
1083: push @{$paramHash->{CHOICES}}, [$human, $computer, $nextstate,
1084: $evalFlag];
1.5 bowersj2 1085: return '';
1086: }
1087:
1088: sub end_choice {
1089: return '';
1090: }
1091:
1092: sub render {
1093: # START HERE: Replace this with correct choices code.
1094: my $self = shift;
1095: my $var = $self->{'variable'};
1096: my $buttons = '';
1097: my $result = '';
1098:
1099: if ($self->{'multichoice'}) {
1.6 bowersj2 1100: $result .= <<SCRIPT;
1.5 bowersj2 1101: <script>
1.18 bowersj2 1102: function checkall(value, checkName) {
1.15 bowersj2 1103: for (i=0; i<document.forms.helpform.elements.length; i++) {
1.18 bowersj2 1104: ele = document.forms.helpform.elements[i];
1105: if (ele.name == checkName + '.forminput') {
1106: document.forms.helpform.elements[i].checked=value;
1107: }
1.5 bowersj2 1108: }
1109: }
1110: </script>
1111: SCRIPT
1.25 bowersj2 1112: }
1113:
1114: # Only print "select all" and "unselect all" if there are five or
1115: # more choices; fewer then that and it looks silly.
1116: if ($self->{'multichoice'} && scalar(@{$self->{CHOICES}}) > 4) {
1.5 bowersj2 1117: $buttons = <<BUTTONS;
1118: <br />
1.18 bowersj2 1119: <input type="button" onclick="checkall(true, '$var')" value="Select All" />
1120: <input type="button" onclick="checkall(false, '$var')" value="Unselect All" />
1.6 bowersj2 1121: <br />
1.5 bowersj2 1122: BUTTONS
1123: }
1124:
1.16 bowersj2 1125: if (defined $self->{ERROR_MSG}) {
1.6 bowersj2 1126: $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
1.5 bowersj2 1127: }
1128:
1129: $result .= $buttons;
1.6 bowersj2 1130:
1.5 bowersj2 1131: $result .= "<table>\n\n";
1132:
1.25 bowersj2 1133: my %checkedChoices;
1134: my $checkedChoicesFunc;
1135:
1136: if (defined($self->{DEFAULT_VALUE})) {
1137: $checkedChoicesFunc = eval ($self->{DEFAULT_VALUE});
1138: die 'Error in default value code for variable ' .
1139: {'variable'} . ', Perl said:' . $@ if $@;
1140: } else {
1141: $checkedChoicesFunc = sub { return ''; };
1142: }
1143:
1144: # Process which choices should be checked.
1145: if ($self->{'multichoice'}) {
1146: for my $selectedChoice (split(/\|\|\|/, (&$checkedChoicesFunc($helper, $self)))) {
1147: $checkedChoices{$selectedChoice} = 1;
1148: }
1149: } else {
1150: # single choice
1151: my $selectedChoice = &$checkedChoicesFunc($helper, $self);
1152:
1153: my $foundChoice = 0;
1154:
1155: # check that the choice is in the list of choices.
1156: for my $choice (@{$self->{CHOICES}}) {
1157: if ($choice->[1] eq $selectedChoice) {
1158: $checkedChoices{$choice->[1]} = 1;
1159: $foundChoice = 1;
1160: }
1161: }
1162:
1163: # If we couldn't find the choice, pick the first one
1164: if (!$foundChoice) {
1165: $checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
1166: }
1167: }
1168:
1.5 bowersj2 1169: my $type = "radio";
1170: if ($self->{'multichoice'}) { $type = 'checkbox'; }
1171: foreach my $choice (@{$self->{CHOICES}}) {
1172: $result .= "<tr>\n<td width='20'> </td>\n";
1173: $result .= "<td valign='top'><input type='$type' name='$var.forminput'"
1174: . "' value='" .
1175: HTML::Entities::encode($choice->[1])
1176: . "'";
1.25 bowersj2 1177: if ($checkedChoices{$choice->[1]}) {
1.5 bowersj2 1178: $result .= " checked ";
1179: }
1.13 bowersj2 1180: my $choiceLabel = $choice->[0];
1181: if ($choice->[4]) { # if we need to evaluate this choice
1182: $choiceLabel = "sub { my $helper = shift; my $state = shift;" .
1183: $choiceLabel . "}";
1184: $choiceLabel = eval($choiceLabel);
1185: $choiceLabel = &$choiceLabel($helper, $self);
1186: }
1187: $result .= "/></td><td> " . $choiceLabel . "</td></tr>\n";
1.5 bowersj2 1188: }
1189: $result .= "</table>\n\n\n";
1190: $result .= $buttons;
1191:
1192: return $result;
1193: }
1194:
1195: # If a NEXTSTATE was given or a nextstate for this choice was
1196: # given, switch to it
1197: sub postprocess {
1198: my $self = shift;
1199: my $chosenValue = $ENV{'form.' . $self->{'variable'} . '.forminput'};
1200:
1.26 ! bowersj2 1201: if (!defined($chosenValue) && !$self->{'allowempty'}) {
1.6 bowersj2 1202: $self->{ERROR_MSG} = "You must choose one or more choices to" .
1203: " continue.";
1204: return 0;
1205: }
1206:
1207: if ($self->{'multichoice'}) {
1208: $self->process_multiple_choices($self->{'variable'}.'.forminput',
1209: $self->{'variable'});
1210: }
1211:
1.5 bowersj2 1212: if (defined($self->{NEXTSTATE})) {
1213: $helper->changeState($self->{NEXTSTATE});
1214: }
1215:
1216: foreach my $choice (@{$self->{CHOICES}}) {
1217: if ($choice->[1] eq $chosenValue) {
1218: if (defined($choice->[2])) {
1219: $helper->changeState($choice->[2]);
1220: }
1221: }
1222: }
1.6 bowersj2 1223: return 1;
1.5 bowersj2 1224: }
1225: 1;
1226:
1227: package Apache::lonhelper::date;
1228:
1229: =pod
1230:
1231: =head2 Element: date
1232:
1233: Date elements allow the selection of a date with a drop down list.
1234:
1235: Date elements can take two attributes:
1236:
1237: =over 4
1238:
1239: =item * B<variable>: The name of the variable to store the chosen
1240: date in. Required.
1241:
1242: =item * B<hoursminutes>: If a true value, the date will show hours
1243: and minutes, as well as month/day/year. If false or missing,
1244: the date will only show the month, day, and year.
1245:
1246: =back
1247:
1248: Date elements contain only an option <nextstate> tag to determine
1249: the next state.
1250:
1251: Example:
1252:
1253: <date variable="DUE_DATE" hoursminutes="1">
1254: <nextstate>choose_why</nextstate>
1255: </date>
1256:
1257: =cut
1258:
1259: no strict;
1260: @ISA = ("Apache::lonhelper::element");
1261: use strict;
1262:
1263: use Time::localtime;
1264:
1265: BEGIN {
1.7 bowersj2 1266: &Apache::lonhelper::register('Apache::lonhelper::date',
1.5 bowersj2 1267: ('date'));
1268: }
1269:
1270: # Don't need to override the "new" from element
1271: sub new {
1272: my $ref = Apache::lonhelper::element->new();
1273: bless($ref);
1274: }
1275:
1276: my @months = ("January", "February", "March", "April", "May", "June", "July",
1277: "August", "September", "October", "November", "December");
1278:
1279: # CONSTRUCTION: Construct the message element from the XML
1280: sub start_date {
1281: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1282:
1283: if ($target ne 'helper') {
1284: return '';
1285: }
1286:
1287: $paramHash->{'variable'} = $token->[2]{'variable'};
1288: $helper->declareVar($paramHash->{'variable'});
1289: $paramHash->{'hoursminutes'} = $token->[2]{'hoursminutes'};
1290: }
1291:
1292: sub end_date {
1293: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1294:
1295: if ($target ne 'helper') {
1296: return '';
1297: }
1298: Apache::lonhelper::date->new();
1299: return '';
1300: }
1301:
1302: sub render {
1303: my $self = shift;
1304: my $result = "";
1305: my $var = $self->{'variable'};
1306:
1307: my $date;
1308:
1309: # Default date: The current hour.
1310: $date = localtime();
1311: $date->min(0);
1312:
1313: if (defined $self->{ERROR_MSG}) {
1314: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1315: }
1316:
1317: # Month
1318: my $i;
1319: $result .= "<select name='${var}month'>\n";
1320: for ($i = 0; $i < 12; $i++) {
1321: if ($i == $date->mon) {
1322: $result .= "<option value='$i' selected>";
1323: } else {
1324: $result .= "<option value='$i'>";
1325: }
1326: $result .= $months[$i] . "</option>\n";
1327: }
1328: $result .= "</select>\n";
1329:
1330: # Day
1331: $result .= "<select name='${var}day'>\n";
1332: for ($i = 1; $i < 32; $i++) {
1333: if ($i == $date->mday) {
1334: $result .= '<option selected>';
1335: } else {
1336: $result .= '<option>';
1337: }
1338: $result .= "$i</option>\n";
1339: }
1340: $result .= "</select>,\n";
1341:
1342: # Year
1343: $result .= "<select name='${var}year'>\n";
1344: for ($i = 2000; $i < 2030; $i++) { # update this after 64-bit dates
1345: if ($date->year + 1900 == $i) {
1346: $result .= "<option selected>";
1347: } else {
1348: $result .= "<option>";
1349: }
1350: $result .= "$i</option>\n";
1351: }
1352: $result .= "</select>,\n";
1353:
1354: # Display Hours and Minutes if they are called for
1355: if ($self->{'hoursminutes'}) {
1356: # Build hour
1357: $result .= "<select name='${var}hour'>\n";
1358: $result .= "<option " . ($date->hour == 0 ? 'selected ':'') .
1359: " value='0'>midnight</option>\n";
1360: for ($i = 1; $i < 12; $i++) {
1361: if ($date->hour == $i) {
1362: $result .= "<option selected value='$i'>$i a.m.</option>\n";
1363: } else {
1364: $result .= "<option value='$i'>$i a.m</option>\n";
1365: }
1366: }
1367: $result .= "<option " . ($date->hour == 12 ? 'selected ':'') .
1368: " value='12'>noon</option>\n";
1369: for ($i = 13; $i < 24; $i++) {
1370: my $printedHour = $i - 12;
1371: if ($date->hour == $i) {
1372: $result .= "<option selected value='$i'>$printedHour p.m.</option>\n";
1373: } else {
1374: $result .= "<option value='$i'>$printedHour p.m.</option>\n";
1375: }
1376: }
1377:
1378: $result .= "</select> :\n";
1379:
1380: $result .= "<select name='${var}minute'>\n";
1381: for ($i = 0; $i < 60; $i++) {
1382: my $printedMinute = $i;
1383: if ($i < 10) {
1384: $printedMinute = "0" . $printedMinute;
1385: }
1386: if ($date->min == $i) {
1387: $result .= "<option selected>";
1388: } else {
1389: $result .= "<option>";
1390: }
1391: $result .= "$printedMinute</option>\n";
1392: }
1393: $result .= "</select>\n";
1394: }
1395:
1396: return $result;
1397:
1398: }
1399: # If a NEXTSTATE was given, switch to it
1400: sub postprocess {
1401: my $self = shift;
1402: my $var = $self->{'variable'};
1403: my $month = $ENV{'form.' . $var . 'month'};
1404: my $day = $ENV{'form.' . $var . 'day'};
1405: my $year = $ENV{'form.' . $var . 'year'};
1406: my $min = 0;
1407: my $hour = 0;
1408: if ($self->{'hoursminutes'}) {
1409: $min = $ENV{'form.' . $var . 'minute'};
1410: $hour = $ENV{'form.' . $var . 'hour'};
1411: }
1412:
1413: my $chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);
1414: # Check to make sure that the date was not automatically co-erced into a
1415: # valid date, as we want to flag that as an error
1416: # This happens for "Feb. 31", for instance, which is coerced to March 2 or
1417: # 3, depending on if it's a leapyear
1418: my $checkDate = localtime($chosenDate);
1419:
1420: if ($checkDate->mon != $month || $checkDate->mday != $day ||
1421: $checkDate->year + 1900 != $year) {
1422: $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
1423: . "date because it doesn't exist. Please enter a valid date.";
1.6 bowersj2 1424: return 0;
1.5 bowersj2 1425: }
1426:
1427: $helper->{VARS}->{$var} = $chosenDate;
1428:
1429: if (defined($self->{NEXTSTATE})) {
1430: $helper->changeState($self->{NEXTSTATE});
1431: }
1.6 bowersj2 1432:
1433: return 1;
1.5 bowersj2 1434: }
1435: 1;
1436:
1437: package Apache::lonhelper::resource;
1438:
1439: =pod
1440:
1441: =head2 Element: resource
1442:
1443: <resource> elements allow the user to select one or multiple resources
1444: from the current course. You can filter out which resources they can view,
1445: and filter out which resources they can select. The course will always
1446: be displayed fully expanded, because of the difficulty of maintaining
1447: selections across folder openings and closings. If this is fixed, then
1448: the user can manipulate the folders.
1449:
1450: <resource> takes the standard variable attribute to control what helper
1451: variable stores the results. It also takes a "multichoice" attribute,
1.17 bowersj2 1452: which controls whether the user can select more then one resource. The
1453: "toponly" attribute controls whether the resource display shows just the
1454: resources in that sequence, or recurses into all sub-sequences, defaulting
1455: to false.
1.5 bowersj2 1456:
1457: B<SUB-TAGS>
1458:
1459: =over 4
1460:
1461: =item * <filterfunc>: If you want to filter what resources are displayed
1462: to the user, use a filter func. The <filterfunc> tag should contain
1463: Perl code that when wrapped with "sub { my $res = shift; " and "}" is
1464: a function that returns true if the resource should be displayed,
1465: and false if it should be skipped. $res is a resource object.
1466: (See Apache::lonnavmaps documentation for information about the
1467: resource object.)
1468:
1469: =item * <choicefunc>: Same as <filterfunc>, except that controls whether
1470: the given resource can be chosen. (It is almost always a good idea to
1471: show the user the folders, for instance, but you do not always want to
1472: let the user select them.)
1473:
1474: =item * <nextstate>: Standard nextstate behavior.
1475:
1476: =item * <valuefunc>: This function controls what is returned by the resource
1477: when the user selects it. Like filterfunc and choicefunc, it should be
1478: a function fragment that when wrapped by "sub { my $res = shift; " and
1479: "}" returns a string representing what you want to have as the value. By
1480: default, the value will be the resource ID of the object ($res->{ID}).
1481:
1.13 bowersj2 1482: =item * <mapurl>: If the URL of a map is given here, only that map
1483: will be displayed, instead of the whole course.
1484:
1.5 bowersj2 1485: =back
1486:
1487: =cut
1488:
1489: no strict;
1490: @ISA = ("Apache::lonhelper::element");
1491: use strict;
1492:
1493: BEGIN {
1.7 bowersj2 1494: &Apache::lonhelper::register('Apache::lonhelper::resource',
1.5 bowersj2 1495: ('resource', 'filterfunc',
1.13 bowersj2 1496: 'choicefunc', 'valuefunc',
1497: 'mapurl'));
1.5 bowersj2 1498: }
1499:
1500: sub new {
1501: my $ref = Apache::lonhelper::element->new();
1502: bless($ref);
1503: }
1504:
1505: # CONSTRUCTION: Construct the message element from the XML
1506: sub start_resource {
1507: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1508:
1509: if ($target ne 'helper') {
1510: return '';
1511: }
1512:
1513: $paramHash->{'variable'} = $token->[2]{'variable'};
1514: $helper->declareVar($paramHash->{'variable'});
1.14 bowersj2 1515: $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.17 bowersj2 1516: $paramHash->{'toponly'} = $token->[2]{'toponly'};
1.5 bowersj2 1517: return '';
1518: }
1519:
1520: sub end_resource {
1521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1522:
1523: if ($target ne 'helper') {
1524: return '';
1525: }
1526: if (!defined($paramHash->{FILTER_FUNC})) {
1527: $paramHash->{FILTER_FUNC} = sub {return 1;};
1528: }
1529: if (!defined($paramHash->{CHOICE_FUNC})) {
1530: $paramHash->{CHOICE_FUNC} = sub {return 1;};
1531: }
1532: if (!defined($paramHash->{VALUE_FUNC})) {
1533: $paramHash->{VALUE_FUNC} = sub {my $res = shift; return $res->{ID}; };
1534: }
1535: Apache::lonhelper::resource->new();
1.4 bowersj2 1536: return '';
1537: }
1538:
1.5 bowersj2 1539: sub start_filterfunc {
1540: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1541:
1542: if ($target ne 'helper') {
1543: return '';
1544: }
1545:
1546: my $contents = Apache::lonxml::get_all_text('/filterfunc',
1547: $parser);
1548: $contents = 'sub { my $res = shift; ' . $contents . '}';
1549: $paramHash->{FILTER_FUNC} = eval $contents;
1550: }
1551:
1552: sub end_filterfunc { return ''; }
1553:
1554: sub start_choicefunc {
1555: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1556:
1557: if ($target ne 'helper') {
1558: return '';
1559: }
1560:
1561: my $contents = Apache::lonxml::get_all_text('/choicefunc',
1562: $parser);
1563: $contents = 'sub { my $res = shift; ' . $contents . '}';
1564: $paramHash->{CHOICE_FUNC} = eval $contents;
1565: }
1566:
1567: sub end_choicefunc { return ''; }
1568:
1569: sub start_valuefunc {
1570: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1571:
1572: if ($target ne 'helper') {
1573: return '';
1574: }
1575:
1576: my $contents = Apache::lonxml::get_all_text('/valuefunc',
1577: $parser);
1578: $contents = 'sub { my $res = shift; ' . $contents . '}';
1579: $paramHash->{VALUE_FUNC} = eval $contents;
1580: }
1581:
1582: sub end_valuefunc { return ''; }
1583:
1.13 bowersj2 1584: sub start_mapurl {
1585: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1586:
1587: if ($target ne 'helper') {
1588: return '';
1589: }
1590:
1591: my $contents = Apache::lonxml::get_all_text('/mapurl',
1592: $parser);
1.14 bowersj2 1593: $paramHash->{MAP_URL} = $contents;
1.13 bowersj2 1594: }
1595:
1596: sub end_mapurl { return ''; }
1597:
1.5 bowersj2 1598: # A note, in case I don't get to this before I leave.
1599: # If someone complains about the "Back" button returning them
1600: # to the previous folder state, instead of returning them to
1601: # the previous helper state, the *correct* answer is for the helper
1602: # to keep track of how many times the user has manipulated the folders,
1603: # and feed that to the history.go() call in the helper rendering routines.
1604: # If done correctly, the helper itself can keep track of how many times
1605: # it renders the same states, so it doesn't go in just this state, and
1606: # you can lean on the browser back button to make sure it all chains
1607: # correctly.
1608: # Right now, though, I'm just forcing all folders open.
1609:
1610: sub render {
1611: my $self = shift;
1612: my $result = "";
1613: my $var = $self->{'variable'};
1614: my $curVal = $helper->{VARS}->{$var};
1615:
1.15 bowersj2 1616: my $buttons = '';
1617:
1618: if ($self->{'multichoice'}) {
1619: $result = <<SCRIPT;
1620: <script>
1.18 bowersj2 1621: function checkall(value, checkName) {
1.15 bowersj2 1622: for (i=0; i<document.forms.helpform.elements.length; i++) {
1623: ele = document.forms.helpform.elements[i];
1.18 bowersj2 1624: if (ele.name == checkName + '.forminput') {
1.15 bowersj2 1625: document.forms.helpform.elements[i].checked=value;
1626: }
1627: }
1628: }
1629: </script>
1630: SCRIPT
1631: $buttons = <<BUTTONS;
1632: <br />
1.18 bowersj2 1633: <input type="button" onclick="checkall(true, '$var')" value="Select All Resources" />
1634: <input type="button" onclick="checkall(false, '$var')" value="Unselect All Resources" />
1.15 bowersj2 1635: <br />
1636: BUTTONS
1637: }
1638:
1.5 bowersj2 1639: if (defined $self->{ERROR_MSG}) {
1.14 bowersj2 1640: $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1.5 bowersj2 1641: }
1642:
1.15 bowersj2 1643: $result .= $buttons;
1644:
1.5 bowersj2 1645: my $filterFunc = $self->{FILTER_FUNC};
1646: my $choiceFunc = $self->{CHOICE_FUNC};
1647: my $valueFunc = $self->{VALUE_FUNC};
1.13 bowersj2 1648: my $mapUrl = $self->{MAP_URL};
1.14 bowersj2 1649: my $multichoice = $self->{'multichoice'};
1.5 bowersj2 1650:
1651: # Create the composite function that renders the column on the nav map
1652: # have to admit any language that lets me do this can't be all bad
1653: # - Jeremy (Pythonista) ;-)
1654: my $checked = 0;
1655: my $renderColFunc = sub {
1656: my ($resource, $part, $params) = @_;
1.14 bowersj2 1657:
1658: my $inputType;
1659: if ($multichoice) { $inputType = 'checkbox'; }
1660: else {$inputType = 'radio'; }
1661:
1.5 bowersj2 1662: if (!&$choiceFunc($resource)) {
1663: return '<td> </td>';
1664: } else {
1.14 bowersj2 1665: my $col = "<td><input type='$inputType' name='${var}.forminput' ";
1666: if (!$checked && !$multichoice) {
1.5 bowersj2 1667: $col .= "checked ";
1668: $checked = 1;
1669: }
1670: $col .= "value='" .
1671: HTML::Entities::encode(&$valueFunc($resource))
1672: . "' /></td>";
1673: return $col;
1674: }
1675: };
1676:
1.17 bowersj2 1677: $ENV{'form.condition'} = !$self->{'toponly'};
1.5 bowersj2 1678: $result .=
1679: &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc,
1680: Apache::lonnavmaps::resource()],
1681: 'showParts' => 0,
1682: 'filterFunc' => $filterFunc,
1.13 bowersj2 1683: 'resource_no_folder_link' => 1,
1684: 'iterator_map' => $mapUrl }
1.5 bowersj2 1685: );
1.15 bowersj2 1686:
1687: $result .= $buttons;
1.5 bowersj2 1688:
1689: return $result;
1690: }
1691:
1692: sub postprocess {
1693: my $self = shift;
1.14 bowersj2 1694:
1695: if ($self->{'multichoice'}) {
1696: $self->process_multiple_choices($self->{'variable'}.'.forminput',
1697: $self->{'variable'});
1698: }
1699:
1700: if ($self->{'multichoice'} && !$helper->{VARS}->{$self->{'variable'}}) {
1701: $self->{ERROR_MSG} = 'You must choose at least one resource to continue.';
1702: return 0;
1703: }
1704:
1.5 bowersj2 1705: if (defined($self->{NEXTSTATE})) {
1706: $helper->changeState($self->{NEXTSTATE});
1707: }
1.6 bowersj2 1708:
1709: return 1;
1.5 bowersj2 1710: }
1711:
1712: 1;
1713:
1714: package Apache::lonhelper::student;
1715:
1716: =pod
1717:
1718: =head2 Element: student
1719:
1720: Student elements display a choice of students enrolled in the current
1721: course. Currently it is primitive; this is expected to evolve later.
1722:
1723: Student elements take two attributes: "variable", which means what
1724: it usually does, and "multichoice", which if true allows the user
1725: to select multiple students.
1726:
1727: =cut
1728:
1729: no strict;
1730: @ISA = ("Apache::lonhelper::element");
1731: use strict;
1732:
1733:
1734:
1735: BEGIN {
1.7 bowersj2 1736: &Apache::lonhelper::register('Apache::lonhelper::student',
1.5 bowersj2 1737: ('student'));
1738: }
1739:
1740: sub new {
1741: my $ref = Apache::lonhelper::element->new();
1742: bless($ref);
1743: }
1.4 bowersj2 1744:
1.5 bowersj2 1745: sub start_student {
1.4 bowersj2 1746: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1747:
1748: if ($target ne 'helper') {
1749: return '';
1750: }
1751:
1.5 bowersj2 1752: $paramHash->{'variable'} = $token->[2]{'variable'};
1753: $helper->declareVar($paramHash->{'variable'});
1754: $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.12 bowersj2 1755: if (defined($token->[2]{'nextstate'})) {
1756: $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
1757: }
1758:
1.5 bowersj2 1759: }
1760:
1761: sub end_student {
1762: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1763:
1764: if ($target ne 'helper') {
1765: return '';
1766: }
1767: Apache::lonhelper::student->new();
1.3 bowersj2 1768: }
1.5 bowersj2 1769:
1770: sub render {
1771: my $self = shift;
1772: my $result = '';
1773: my $buttons = '';
1.18 bowersj2 1774: my $var = $self->{'variable'};
1.5 bowersj2 1775:
1776: if ($self->{'multichoice'}) {
1777: $result = <<SCRIPT;
1778: <script>
1.18 bowersj2 1779: function checkall(value, checkName) {
1.15 bowersj2 1780: for (i=0; i<document.forms.helpform.elements.length; i++) {
1.18 bowersj2 1781: ele = document.forms.helpform.elements[i];
1782: if (ele.name == checkName + '.forminput') {
1783: document.forms.helpform.elements[i].checked=value;
1784: }
1.5 bowersj2 1785: }
1786: }
1787: </script>
1788: SCRIPT
1789: $buttons = <<BUTTONS;
1790: <br />
1.18 bowersj2 1791: <input type="button" onclick="checkall(true, '$var')" value="Select All Students" />
1792: <input type="button" onclick="checkall(false, '$var')" value="Unselect All Students" />
1.5 bowersj2 1793: <br />
1794: BUTTONS
1795: }
1796:
1797: if (defined $self->{ERROR_MSG}) {
1798: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1799: }
1800:
1801: # Load up the students
1802: my $choices = &Apache::loncoursedata::get_classlist();
1803: my @keys = keys %{$choices};
1804:
1805: # Constants
1806: my $section = Apache::loncoursedata::CL_SECTION();
1807: my $fullname = Apache::loncoursedata::CL_FULLNAME();
1808:
1809: # Sort by: Section, name
1810: @keys = sort {
1811: if ($choices->{$a}->[$section] ne $choices->{$b}->[$section]) {
1812: return $choices->{$a}->[$section] cmp $choices->{$b}->[$section];
1813: }
1814: return $choices->{$a}->[$fullname] cmp $choices->{$b}->[$fullname];
1815: } @keys;
1816:
1817: my $type = 'radio';
1818: if ($self->{'multichoice'}) { $type = 'checkbox'; }
1819: $result .= "<table cellspacing='2' cellpadding='2' border='0'>\n";
1820: $result .= "<tr><td></td><td align='center'><b>Student Name</b></td>".
1821: "<td align='center'><b>Section</b></td></tr>";
1822:
1823: my $checked = 0;
1824: foreach (@keys) {
1825: $result .= "<tr><td><input type='$type' name='" .
1826: $self->{'variable'} . '.forminput' . "'";
1827:
1828: if (!$self->{'multichoice'} && !$checked) {
1829: $result .= " checked ";
1830: $checked = 1;
1831: }
1832: $result .=
1.19 bowersj2 1833: " value='" . HTML::Entities::encode($_ . ':' . $choices->{$_}->[$section])
1.5 bowersj2 1834: . "' /></td><td>"
1835: . HTML::Entities::encode($choices->{$_}->[$fullname])
1836: . "</td><td align='center'>"
1837: . HTML::Entities::encode($choices->{$_}->[$section])
1838: . "</td></tr>\n";
1839: }
1840:
1841: $result .= "</table>\n\n";
1842: $result .= $buttons;
1.4 bowersj2 1843:
1.5 bowersj2 1844: return $result;
1845: }
1846:
1.6 bowersj2 1847: sub postprocess {
1848: my $self = shift;
1849:
1850: my $result = $ENV{'form.' . $self->{'variable'} . '.forminput'};
1851: if (!$result) {
1852: $self->{ERROR_MSG} = 'You must choose at least one student '.
1853: 'to continue.';
1854: return 0;
1855: }
1856:
1857: if ($self->{'multichoice'}) {
1858: $self->process_multiple_choices($self->{'variable'}.'.forminput',
1859: $self->{'variable'});
1860: }
1861: if (defined($self->{NEXTSTATE})) {
1862: $helper->changeState($self->{NEXTSTATE});
1863: }
1864:
1865: return 1;
1866: }
1867:
1.5 bowersj2 1868: 1;
1869:
1870: package Apache::lonhelper::files;
1871:
1872: =pod
1873:
1874: =head2 Element: files
1875:
1876: files allows the users to choose files from a given directory on the
1877: server. It is always multichoice and stores the result as a triple-pipe
1878: delimited entry in the helper variables.
1879:
1880: Since it is extremely unlikely that you can actually code a constant
1881: representing the directory you wish to allow the user to search, <files>
1882: takes a subroutine that returns the name of the directory you wish to
1883: have the user browse.
1884:
1885: files accepts the attribute "variable" to control where the files chosen
1886: are put. It accepts the attribute "multichoice" as the other attribute,
1887: defaulting to false, which if true will allow the user to select more
1888: then one choice.
1889:
1890: <files> accepts three subtags. One is the "nextstate" sub-tag that works
1891: as it does with the other tags. Another is a <filechoice> sub tag that
1892: is Perl code that, when surrounded by "sub {" and "}" will return a
1893: string representing what directory on the server to allow the user to
1894: choose files from. Finally, the <filefilter> subtag should contain Perl
1895: code that when surrounded by "sub { my $filename = shift; " and "}",
1896: returns a true value if the user can pick that file, or false otherwise.
1897: The filename passed to the function will be just the name of the file,
1898: with no path info.
1899:
1900: =cut
1901:
1902: no strict;
1903: @ISA = ("Apache::lonhelper::element");
1904: use strict;
1905:
1906: BEGIN {
1.7 bowersj2 1907: &Apache::lonhelper::register('Apache::lonhelper::files',
1908: ('files', 'filechoice', 'filefilter'));
1.5 bowersj2 1909: }
1910:
1911: sub new {
1912: my $ref = Apache::lonhelper::element->new();
1913: bless($ref);
1914: }
1915:
1916: sub start_files {
1917: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1918:
1919: if ($target ne 'helper') {
1920: return '';
1921: }
1922: $paramHash->{'variable'} = $token->[2]{'variable'};
1923: $helper->declareVar($paramHash->{'variable'});
1924: $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1925: }
1926:
1927: sub end_files {
1928: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1929:
1930: if ($target ne 'helper') {
1931: return '';
1932: }
1933: if (!defined($paramHash->{FILTER_FUNC})) {
1934: $paramHash->{FILTER_FUNC} = sub { return 1; };
1935: }
1936: Apache::lonhelper::files->new();
1937: }
1938:
1939: sub start_filechoice {
1940: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1941:
1942: if ($target ne 'helper') {
1943: return '';
1944: }
1945: $paramHash->{'filechoice'} = Apache::lonxml::get_all_text('/filechoice',
1946: $parser);
1947: }
1948:
1949: sub end_filechoice { return ''; }
1950:
1951: sub start_filefilter {
1952: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1953:
1954: if ($target ne 'helper') {
1955: return '';
1956: }
1957:
1958: my $contents = Apache::lonxml::get_all_text('/filefilter',
1959: $parser);
1960: $contents = 'sub { my $filename = shift; ' . $contents . '}';
1961: $paramHash->{FILTER_FUNC} = eval $contents;
1962: }
1963:
1964: sub end_filefilter { return ''; }
1.3 bowersj2 1965:
1966: sub render {
1967: my $self = shift;
1.5 bowersj2 1968: my $result = '';
1969: my $var = $self->{'variable'};
1970:
1971: my $subdirFunc = eval('sub {' . $self->{'filechoice'} . '}');
1.11 bowersj2 1972: die 'Error in resource filter code for variable ' .
1973: {'variable'} . ', Perl said:' . $@ if $@;
1974:
1.5 bowersj2 1975: my $subdir = &$subdirFunc();
1976:
1977: my $filterFunc = $self->{FILTER_FUNC};
1978: my $buttons = '';
1.22 bowersj2 1979: my $type = 'radio';
1980: if ($self->{'multichoice'}) {
1981: $type = 'checkbox';
1982: }
1.5 bowersj2 1983:
1984: if ($self->{'multichoice'}) {
1985: $result = <<SCRIPT;
1986: <script>
1.18 bowersj2 1987: function checkall(value, checkName) {
1.15 bowersj2 1988: for (i=0; i<document.forms.helpform.elements.length; i++) {
1989: ele = document.forms.helpform.elements[i];
1.18 bowersj2 1990: if (ele.name == checkName + '.forminput') {
1.15 bowersj2 1991: document.forms.helpform.elements[i].checked=value;
1.5 bowersj2 1992: }
1993: }
1994: }
1.21 bowersj2 1995:
1.22 bowersj2 1996: function checkallclass(value, className) {
1.21 bowersj2 1997: for (i=0; i<document.forms.helpform.elements.length; i++) {
1998: ele = document.forms.helpform.elements[i];
1.22 bowersj2 1999: if (ele.type == "$type" && ele.onclick) {
1.21 bowersj2 2000: document.forms.helpform.elements[i].checked=value;
2001: }
2002: }
2003: }
1.5 bowersj2 2004: </script>
2005: SCRIPT
1.15 bowersj2 2006: $buttons = <<BUTTONS;
1.5 bowersj2 2007: <br />
1.18 bowersj2 2008: <input type="button" onclick="checkall(true, '$var')" value="Select All Files" />
2009: <input type="button" onclick="checkall(false, '$var')" value="Unselect All Files" />
1.23 bowersj2 2010: BUTTONS
2011:
2012: if ($helper->{VARS}->{'construction'}) {
2013: $buttons .= <<BUTTONS;
1.22 bowersj2 2014: <input type="button" onclick="checkallclass(true, 'Published')" value="Select All Published" />
2015: <input type="button" onclick="checkallclass(false, 'Published')" value="Unselect All Published" />
1.5 bowersj2 2016: <br />
2017: BUTTONS
1.23 bowersj2 2018: }
1.5 bowersj2 2019: }
2020:
2021: # Get the list of files in this directory.
2022: my @fileList;
2023:
2024: # If the subdirectory is in local CSTR space
2025: if ($subdir =~ m|/home/([^/]+)/public_html|) {
2026: my $user = $1;
2027: my $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
2028: @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
2029: } else {
2030: # local library server resource space
2031: @fileList = &Apache::lonnet::dirlist($subdir, $ENV{'user.domain'}, $ENV{'user.name'}, '');
2032: }
1.3 bowersj2 2033:
1.5 bowersj2 2034: $result .= $buttons;
2035:
1.6 bowersj2 2036: if (defined $self->{ERROR_MSG}) {
2037: $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
2038: }
2039:
1.20 bowersj2 2040: $result .= '<table border="0" cellpadding="2" cellspacing="0">';
1.5 bowersj2 2041:
2042: # Keeps track if there are no choices, prints appropriate error
2043: # if there are none.
2044: my $choices = 0;
2045: # Print each legitimate file choice.
2046: for my $file (@fileList) {
2047: $file = (split(/&/, $file))[0];
2048: if ($file eq '.' || $file eq '..') {
2049: next;
2050: }
2051: my $fileName = $subdir .'/'. $file;
2052: if (&$filterFunc($file)) {
1.24 sakharuk 2053: my $status;
2054: my $color;
2055: if ($helper->{VARS}->{'construction'}) {
2056: ($status, $color) = @{fileState($subdir, $file)};
2057: } else {
2058: $status = '';
2059: $color = '';
2060: }
1.22 bowersj2 2061:
2062: # Netscape 4 is stupid and there's nowhere to put the
2063: # information on the input tag that the file is Published,
2064: # Unpublished, etc. In *real* browsers we can just say
2065: # "class='Published'" and check the className attribute of
2066: # the input tag, but Netscape 4 is too stupid to understand
2067: # that attribute, and un-comprehended attributes are not
2068: # reflected into the object model. So instead, what I do
2069: # is either have or don't have an "onclick" handler that
2070: # does nothing, give Published files the onclick handler, and
2071: # have the checker scripts check for that. Stupid and clumsy,
2072: # and only gives us binary "yes/no" information (at least I
2073: # couldn't figure out how to reach into the event handler's
2074: # actual code to retreive a value), but it works well enough
2075: # here.
1.23 bowersj2 2076:
1.22 bowersj2 2077: my $onclick = '';
1.23 bowersj2 2078: if ($status eq 'Published' && $helper->{VARS}->{'construction'}) {
1.22 bowersj2 2079: $onclick = 'onclick="a=1" ';
2080: }
1.20 bowersj2 2081: $result .= '<tr><td align="right"' . " bgcolor='$color'>" .
1.22 bowersj2 2082: "<input $onclick type='$type' name='" . $var
1.5 bowersj2 2083: . ".forminput' value='" . HTML::Entities::encode($fileName) .
2084: "'";
2085: if (!$self->{'multichoice'} && $choices == 0) {
2086: $result .= ' checked';
2087: }
1.20 bowersj2 2088: $result .= "/></td><td bgcolor='$color'>" . $file .
2089: "</td><td bgcolor='$color'>$status</td></tr>\n";
1.5 bowersj2 2090: $choices++;
2091: }
2092: }
2093:
2094: $result .= "</table>\n";
2095:
2096: if (!$choices) {
2097: $result .= '<font color="#FF0000">There are no files available to select in this directory. Please go back and select another option.</font><br /><br />';
2098: }
2099:
2100: $result .= $buttons;
2101:
2102: return $result;
1.20 bowersj2 2103: }
2104:
2105: # Determine the state of the file: Published, unpublished, modified.
2106: # Return the color it should be in and a label as a two-element array
2107: # reference.
2108: # Logic lifted from lonpubdir.pm, even though I don't know that it's still
2109: # the most right thing to do.
2110:
2111: sub fileState {
2112: my $constructionSpaceDir = shift;
2113: my $file = shift;
2114:
2115: my $docroot = $Apache::lonnet::perlvar{'lonDocRoot'};
2116: my $subdirpart = $constructionSpaceDir;
2117: $subdirpart =~ s/^\/home\/$ENV{'user.name'}\/public_html//;
2118: my $resdir = $docroot . '/res/' . $ENV{'user.domain'} . '/' . $ENV{'user.name'} .
2119: $subdirpart;
2120:
2121: my @constructionSpaceFileStat = stat($constructionSpaceDir . '/' . $file);
2122: my @resourceSpaceFileStat = stat($resdir . '/' . $file);
2123: if (!@resourceSpaceFileStat) {
2124: return ['Unpublished', '#FFCCCC'];
2125: }
2126:
2127: my $constructionSpaceFileModified = $constructionSpaceFileStat[9];
2128: my $resourceSpaceFileModified = $resourceSpaceFileStat[9];
2129:
2130: if ($constructionSpaceFileModified > $resourceSpaceFileModified) {
2131: return ['Modified', '#FFFFCC'];
2132: }
2133: return ['Published', '#CCFFCC'];
1.4 bowersj2 2134: }
1.5 bowersj2 2135:
1.4 bowersj2 2136: sub postprocess {
2137: my $self = shift;
1.6 bowersj2 2138: my $result = $ENV{'form.' . $self->{'variable'} . '.forminput'};
2139: if (!$result) {
2140: $self->{ERROR_MSG} = 'You must choose at least one file '.
2141: 'to continue.';
2142: return 0;
2143: }
2144:
1.5 bowersj2 2145: if ($self->{'multichoice'}) {
2146: $self->process_multiple_choices($self->{'variable'}.'.forminput',
2147: $self->{'variable'});
2148: }
2149: if (defined($self->{NEXTSTATE})) {
2150: $helper->changeState($self->{NEXTSTATE});
1.3 bowersj2 2151: }
1.6 bowersj2 2152:
2153: return 1;
1.3 bowersj2 2154: }
1.8 bowersj2 2155:
2156: 1;
2157:
1.11 bowersj2 2158: package Apache::lonhelper::section;
2159:
2160: =pod
2161:
2162: =head2 Element: section
2163:
2164: <section> allows the user to choose one or more sections from the current
2165: course.
2166:
2167: It takes the standard attributes "variable", "multichoice", and
2168: "nextstate", meaning what they do for most other elements.
2169:
2170: =cut
2171:
2172: no strict;
2173: @ISA = ("Apache::lonhelper::choices");
2174: use strict;
2175:
2176: BEGIN {
2177: &Apache::lonhelper::register('Apache::lonhelper::section',
2178: ('section'));
2179: }
2180:
2181: sub new {
2182: my $ref = Apache::lonhelper::choices->new();
2183: bless($ref);
2184: }
2185:
2186: sub start_section {
2187: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2188:
2189: if ($target ne 'helper') {
2190: return '';
2191: }
1.12 bowersj2 2192:
2193: $paramHash->{CHOICES} = [];
2194:
1.11 bowersj2 2195: $paramHash->{'variable'} = $token->[2]{'variable'};
2196: $helper->declareVar($paramHash->{'variable'});
2197: $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
2198: if (defined($token->[2]{'nextstate'})) {
1.12 bowersj2 2199: $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
1.11 bowersj2 2200: }
2201:
2202: # Populate the CHOICES element
2203: my %choices;
2204:
2205: my $section = Apache::loncoursedata::CL_SECTION();
2206: my $classlist = Apache::loncoursedata::get_classlist();
2207: foreach (keys %$classlist) {
2208: my $sectionName = $classlist->{$_}->[$section];
2209: if (!$sectionName) {
2210: $choices{"No section assigned"} = "";
2211: } else {
2212: $choices{$sectionName} = $sectionName;
2213: }
1.12 bowersj2 2214: }
2215:
1.11 bowersj2 2216: for my $sectionName (sort(keys(%choices))) {
1.12 bowersj2 2217:
1.11 bowersj2 2218: push @{$paramHash->{CHOICES}}, [$sectionName, $sectionName];
2219: }
2220: }
2221:
1.12 bowersj2 2222: sub end_section {
2223: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.11 bowersj2 2224:
1.12 bowersj2 2225: if ($target ne 'helper') {
2226: return '';
2227: }
2228: Apache::lonhelper::section->new();
2229: }
1.11 bowersj2 2230: 1;
2231:
1.8 bowersj2 2232: package Apache::lonhelper::general;
2233:
2234: =pod
2235:
2236: =head2 General-purpose tag: <exec>
2237:
2238: The contents of the exec tag are executed as Perl code, not inside a
2239: safe space, so the full range of $ENV and such is available. The code
2240: will be executed as a subroutine wrapped with the following code:
2241:
2242: "sub { my $helper = shift; my $state = shift;" and
2243:
2244: "}"
2245:
2246: The return value is ignored.
2247:
2248: $helper is the helper object. Feel free to add methods to the helper
2249: object to support whatever manipulation you may need to do (for instance,
2250: overriding the form location if the state is the final state; see
2251: lonparm.helper for an example).
2252:
2253: $state is the $paramHash that has currently been generated and may
2254: be manipulated by the code in exec. Note that the $state is not yet
2255: an actual state B<object>, it is just a hash, so do not expect to
2256: be able to call methods on it.
2257:
2258: =cut
2259:
2260: BEGIN {
2261: &Apache::lonhelper::register('Apache::lonhelper::general',
1.11 bowersj2 2262: 'exec', 'condition', 'clause',
2263: 'eval');
1.8 bowersj2 2264: }
2265:
2266: sub start_exec {
2267: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2268:
2269: if ($target ne 'helper') {
2270: return '';
2271: }
2272:
2273: my $code = &Apache::lonxml::get_all_text('/exec', $parser);
2274:
2275: $code = eval ('sub { my $helper = shift; my $state = shift; ' .
2276: $code . "}");
1.11 bowersj2 2277: die 'Error in <exec>, Perl said: '. $@ if $@;
1.8 bowersj2 2278: &$code($helper, $paramHash);
2279: }
2280:
2281: sub end_exec { return ''; }
2282:
2283: =pod
2284:
2285: =head2 General-purpose tag: <condition>
2286:
2287: The <condition> tag allows you to mask out parts of the helper code
2288: depending on some programatically determined condition. The condition
2289: tag contains a tag <clause> which contains perl code that when wrapped
2290: with "sub { my $helper = shift; my $state = shift; " and "}", returns
2291: a true value if the XML in the condition should be evaluated as a normal
2292: part of the helper, or false if it should be completely discarded.
2293:
2294: The <clause> tag must be the first sub-tag of the <condition> tag or
2295: it will not work as expected.
2296:
2297: =cut
2298:
2299: # The condition tag just functions as a marker, it doesn't have
2300: # to "do" anything. Technically it doesn't even have to be registered
2301: # with the lonxml code, but I leave this here to be explicit about it.
2302: sub start_condition { return ''; }
2303: sub end_condition { return ''; }
2304:
2305: sub start_clause {
2306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2307:
2308: if ($target ne 'helper') {
2309: return '';
2310: }
2311:
2312: my $clause = Apache::lonxml::get_all_text('/clause', $parser);
2313: $clause = eval('sub { my $helper = shift; my $state = shift; '
2314: . $clause . '}');
1.11 bowersj2 2315: die 'Error in clause of condition, Perl said: ' . $@ if $@;
1.8 bowersj2 2316: if (!&$clause($helper, $paramHash)) {
2317: # Discard all text until the /condition.
2318: &Apache::lonxml::get_all_text('/condition', $parser);
2319: }
2320: }
2321:
2322: sub end_clause { return ''; }
1.11 bowersj2 2323:
2324: =pod
2325:
2326: =head2 General-purpose tag: <eval>
2327:
2328: The <eval> tag will be evaluated as a subroutine call passed in the
2329: current helper object and state hash as described in <condition> above,
2330: but is expected to return a string to be printed directly to the
2331: screen. This is useful for dynamically generating messages.
2332:
2333: =cut
2334:
2335: # This is basically a type of message.
2336: # Programmatically setting $paramHash->{NEXTSTATE} would work, though
2337: # it's probably bad form.
2338:
2339: sub start_eval {
2340: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2341:
2342: if ($target ne 'helper') {
2343: return '';
2344: }
2345:
2346: my $program = Apache::lonxml::get_all_text('/eval', $parser);
2347: $program = eval('sub { my $helper = shift; my $state = shift; '
2348: . $program . '}');
2349: die 'Error in eval code, Perl said: ' . $@ if $@;
2350: $paramHash->{MESSAGE_TEXT} = &$program($helper, $paramHash);
2351: }
2352:
2353: sub end_eval {
2354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2355:
2356: if ($target ne 'helper') {
2357: return '';
2358: }
2359:
2360: Apache::lonhelper::message->new();
2361: }
2362:
1.13 bowersj2 2363: 1;
2364:
2365: package Apache::lonhelper::parmwizfinal;
2366:
2367: # This is the final state for the parmwizard. It is not generally useful,
2368: # so it is not perldoc'ed. It does its own processing.
2369: # It is represented with <parmwizfinal />, and
2370: # should later be moved to lonparmset.pm .
2371:
2372: no strict;
2373: @ISA = ('Apache::lonhelper::element');
2374: use strict;
1.11 bowersj2 2375:
1.13 bowersj2 2376: BEGIN {
2377: &Apache::lonhelper::register('Apache::lonhelper::parmwizfinal',
2378: ('parmwizfinal'));
2379: }
2380:
2381: use Time::localtime;
2382:
2383: sub new {
2384: my $ref = Apache::lonhelper::choices->new();
2385: bless ($ref);
2386: }
2387:
2388: sub start_parmwizfinal { return ''; }
2389:
2390: sub end_parmwizfinal {
2391: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2392:
2393: if ($target ne 'helper') {
2394: return '';
2395: }
2396: Apache::lonhelper::parmwizfinal->new();
2397: }
2398:
2399: # Renders a form that, when submitted, will form the input to lonparmset.pm
2400: sub render {
2401: my $self = shift;
2402: my $vars = $helper->{VARS};
2403:
2404: # FIXME: Unify my designators with the standard ones
2405: my %dateTypeHash = ('open_date' => "Opening Date",
2406: 'due_date' => "Due Date",
2407: 'answer_date' => "Answer Date");
2408: my %parmTypeHash = ('open_date' => "0_opendate",
2409: 'due_date' => "0_duedate",
2410: 'answer_date' => "0_answerdate");
2411:
1.14 bowersj2 2412: my $result = "<form name='helpform' method='get' action='/adm/parmset'>\n";
1.13 bowersj2 2413: $result .= '<p>Confirm that this information is correct, then click "Finish Wizard" to complete setting the parameter.<ul>';
2414: my $affectedResourceId = "";
2415: my $parm_name = $parmTypeHash{$vars->{ACTION_TYPE}};
2416: my $level = "";
2417:
2418: # Print the type of manipulation:
2419: $result .= '<li>Setting the <b>' . $dateTypeHash{$vars->{ACTION_TYPE}}
2420: . "</b></li>\n";
2421: if ($vars->{ACTION_TYPE} eq 'due_date' ||
2422: $vars->{ACTION_TYPE} eq 'answer_date') {
2423: # for due dates, we default to "date end" type entries
2424: $result .= "<input type='hidden' name='recent_date_end' " .
2425: "value='" . $vars->{PARM_DATE} . "' />\n";
2426: $result .= "<input type='hidden' name='pres_value' " .
2427: "value='" . $vars->{PARM_DATE} . "' />\n";
2428: $result .= "<input type='hidden' name='pres_type' " .
2429: "value='date_end' />\n";
2430: } elsif ($vars->{ACTION_TYPE} eq 'open_date') {
2431: $result .= "<input type='hidden' name='recent_date_start' ".
2432: "value='" . $vars->{PARM_DATE} . "' />\n";
2433: $result .= "<input type='hidden' name='pres_value' " .
2434: "value='" . $vars->{PARM_DATE} . "' />\n";
2435: $result .= "<input type='hidden' name='pres_type' " .
2436: "value='date_start' />\n";
2437: }
2438:
2439: # Print the granularity, depending on the action
2440: if ($vars->{GRANULARITY} eq 'whole_course') {
2441: $result .= '<li>for <b>all resources in the course</b></li>';
2442: $level = 9; # general course, see lonparmset.pm perldoc
2443: $affectedResourceId = "0.0";
2444: } elsif ($vars->{GRANULARITY} eq 'map') {
2445: my $navmap = Apache::lonnavmaps::navmap->new(
2446: $ENV{"request.course.fn"}.".db",
2447: $ENV{"request.course.fn"}."_parms.db", 0, 0);
2448: my $res = $navmap->getById($vars->{RESOURCE_ID});
2449: my $title = $res->compTitle();
2450: $navmap->untieHashes();
2451: $result .= "<li>for the map named <b>$title</b></li>";
2452: $level = 8;
2453: $affectedResourceId = $vars->{RESOURCE_ID};
2454: } else {
2455: my $navmap = Apache::lonnavmaps::navmap->new(
2456: $ENV{"request.course.fn"}.".db",
2457: $ENV{"request.course.fn"}."_parms.db", 0, 0);
2458: my $res = $navmap->getById($vars->{RESOURCE_ID});
2459: my $title = $res->compTitle();
2460: $navmap->untieHashes();
2461: $result .= "<li>for the resource named <b>$title</b></li>";
2462: $level = 7;
2463: $affectedResourceId = $vars->{RESOURCE_ID};
2464: }
2465:
2466: # Print targets
2467: if ($vars->{TARGETS} eq 'course') {
2468: $result .= '<li>for <b>all students in course</b></li>';
2469: } elsif ($vars->{TARGETS} eq 'section') {
2470: my $section = $vars->{SECTION_NAME};
2471: $result .= "<li>for section <b>$section</b></li>";
2472: $level -= 3;
2473: $result .= "<input type='hidden' name='csec' value='" .
2474: HTML::Entities::encode($section) . "' />\n";
2475: } else {
2476: # FIXME: This is probably wasteful! Store the name!
2477: my $classlist = Apache::loncoursedata::get_classlist();
2478: my $name = $classlist->{$vars->{USER_NAME}}->[6];
2479: $result .= "<li>for <b>$name</b></li>";
2480: $level -= 6;
2481: my ($uname, $udom) = split /:/, $vars->{USER_NAME};
2482: $result .= "<input type='hidden' name='uname' value='".
2483: HTML::Entities::encode($uname) . "' />\n";
2484: $result .= "<input type='hidden' name='udom' value='".
2485: HTML::Entities::encode($udom) . "' />\n";
2486: }
2487:
2488: # Print value
2489: $result .= "<li>to <b>" . ctime($vars->{PARM_DATE}) . "</b> (" .
2490: Apache::lonnavmaps::timeToHumanString($vars->{PARM_DATE})
2491: . ")</li>\n";
2492:
2493: # print pres_marker
2494: $result .= "\n<input type='hidden' name='pres_marker'" .
2495: " value='$affectedResourceId&$parm_name&$level' />\n";
2496:
2497: $result .= "<br /><br /><center><input type='submit' value='Finish Helper' /></center></form>\n";
2498:
2499: return $result;
2500: }
2501:
2502: sub overrideForm {
2503: return 1;
2504: }
1.5 bowersj2 2505:
1.4 bowersj2 2506: 1;
1.3 bowersj2 2507:
1.1 bowersj2 2508: __END__
1.3 bowersj2 2509:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>