Annotation of loncom/interface/lonwizard.pm, revision 1.11
1.1 bowersj2 1: # This is the LON-CAPA HTML Wizard framework, for wrapping easy
2: # functionality easily.
3:
4: package Apache::lonwizard;
5:
6: use Apache::Constants qw(:common :http);
7: use Apache::loncommon;
1.4 bowersj2 8: use Apache::lonnet;
1.1 bowersj2 9:
10: =head1 lonwizard - HTML "Wizard" framework for LON-CAPA
11:
12: I know how most developers feel about Wizards, but the fact is they are a well-established UI widget that users feel comfortable with. It can take a complicated multi-dimensional problem the user has (such as the canonical Course Parameter example) and turn in into a series of bite-sized one-dimensional questions. Or take the some four-question form and put it in a Wizard, and present the same user with the same form outside of the Wizard, and the user will *think* the Wizard is easier.
13:
14: For the developer, wizards do provide an easy way to bundle easy bits of functionality for the user. It can be easier to write a Wizard then provide another custom interface.
15:
16: All classes are in the Apache::lonwizard namespace.
17:
18: (For a perldoc'ed example of a wizard you can use as an example, see loncourseparmwizard.pm.)
19:
20: =cut
21:
22: # To prevent runaway file counts, this file has lonwizard,
23: # lonwizstate, and other wizard classes.
24: use strict;
25:
26: use HTML::Entities;
1.10 bowersj2 27: use Apache::loncommon;
1.1 bowersj2 28:
29: =pod
30:
31: =head1 Class: lonwizard
32:
33: =head2 lonwizard Attributes
34:
35: =over 4
36:
37: =item B<STATE>: The string name of the current state.
38:
39: =item B<TITLE>: The human-readable title of the wizard
40:
41: =item B<STATES>: A hash mapping the string names of states to references to the actual states.
42:
43: =item B<VARS>: Hash that maintains the persistent variable values.
44:
45: =item B<HISTORY>: An array containing the names of the previous states. Used for "back" functionality.
46:
47: =item B<DONE>: A boolean value, true if the wizard has completed.
48:
1.10 bowersj2 49: =item B<DATA>: The data the wizard is drawing from, which will be passed to Apache::loncommon::get_unprocessed_cgi, and may be used by states that do multi-selection.
50:
1.1 bowersj2 51: =back
52:
53: =cut
54:
55: sub new {
56: my $proto = shift;
57: my $class = ref($proto) || $proto;
58: my $self = {};
59:
1.10 bowersj2 60: $self->{TITLE} = shift;
61: $self->{DATA} = shift;
62: &Apache::loncommon::get_unprocessed_cgi($self->{DATA});
63:
64:
1.1 bowersj2 65: # If there is a state from the previous form, use that. If there is no
66: # state, use the start state parameter.
67: if (defined $ENV{"form.CURRENT_STATE"})
68: {
69: $self->{STATE} = $ENV{"form.CURRENT_STATE"};
70: }
71: else
72: {
73: $self->{STATE} = "START";
74: }
75:
76: # set up return URL: Return the user to the referer page, unless the
77: # form has stored a value.
78: if (defined $ENV{"form.RETURN_PAGE"})
79: {
80: $self->{RETURN_PAGE} = $ENV{"form.RETURN_PAGE"};
81: }
82: else
83: {
84: $self->{RETURN_PAGE} = $ENV{REFERER};
85: }
86:
87: $self->{STATES} = {};
88: $self->{VARS} = {};
89: $self->{HISTORY} = {};
90: $self->{DONE} = 0;
1.10 bowersj2 91:
1.1 bowersj2 92: bless($self, $class);
93: return $self;
94: }
95:
96: =pod
97:
98: =head2 lonwizard methods
99:
100: =over 2
101:
1.3 bowersj2 102: =item * B<new>(title): Returns a new instance of the given wizard type. "title" is the human-readable name of the wizard. A new wizard always starts on the B<START> state name.
1.1 bowersj2 103:
1.10 bowersj2 104: =item * B<declareVars>(varList): Call this function to declare the var names you want the wizard to maintain for you. The wizard will automatically output the hidden form fields and parse the values for you on the next call.
1.1 bowersj2 105:
106: =over 2
107:
1.10 bowersj2 108: =item * B<Note>: These form variables are reserved for the wizard; if you output other form values in your state, you must use other names. For example, declaring "student" will cause the wizard to emit a form value with the name "student"; if your state emits form entries, do not name them "student". If you use the variable name followed by '.forminput', the wizard will automatically store the user's choice in the appropriate form variable.
109:
110: =item * B<Note>: If you want to preserve incoming form values, such as ones from the remote, you can simply declare them and the wizard will automatically preserve them. For instance, you might want to store 'url' or 'postdata' from the remote; see lonprintout for example.
1.1 bowersj2 111:
112: =back
113:
114: =cut
115:
1.7 bowersj2 116: # Sometimes the wizard writer will want to use the result of the previous
117: # state to change the text of the next state. In order to do that, it
118: # has to be done during the declaration of the states, or it won't be
119: # available. Therefore, basic form processing must occur before the
120: # actual display routine is called and the actual pre-process is called,
121: # or it won't be available.
122: # This also factors common code out of the preprocess calls.
1.1 bowersj2 123: sub declareVars {
124: my $self = shift;
125: my $varlist = shift;
126:
127: # for each string in the passed in list,
128: foreach my $element ( @{$varlist} )
129: {
130: # assign the var the default of ""
131: $self->{VARS}{$element} = "";
132:
133: # if there's a form in the env, use that instead
134: my $envname = "form." . $element;
1.11 ! bowersj2 135: if (defined ($ENV{$envname})) {
1.1 bowersj2 136: $self->{VARS}->{$element} = $ENV{$envname};
137: }
1.7 bowersj2 138:
139: # If there's an incoming form submission, use that
1.9 bowersj2 140: $envname = "form." . $element . ".forminput";
1.7 bowersj2 141: if (defined ($ENV{$envname})) {
142: $self->{VARS}->{$element} = $ENV{$envname};
143: }
1.1 bowersj2 144: }
145: }
146:
147: # Private function; takes all of the declared vars and returns a string
148: # corresponding to the hidden input fields that will re-construct the
149: # variables.
150: sub _saveVars {
151: my $self = shift;
152: my $result = "";
153: foreach my $varname (keys %{$self->{VARS}})
154: {
155: $result .= '<input type="hidden" name="' .
156: HTML::Entities::encode($varname) . '" value="' .
157: HTML::Entities::encode($self->{VARS}{$varname}) .
158: "\" />\n";
159: }
160:
161: # also save state & return page
162: $result .= '<input type="hidden" name="CURRENT_STATE" value="' .
163: HTML::Entities::encode($self->{STATE}) . '" />' . "\n";
164: $result .= '<input type="hidden" name="RETURN_PAGE" value="' .
165: HTML::Entities::encode($self->{RETURN_PAGE}) . '" />' . "\n";
166:
167: return $result;
168: }
169:
170: =pod
171:
172: =item B<registerState>(referenceToStateObj): Registers a state as part of the wizard, so the wizard can use it. The 'referenceToStateObj' should be a reference to an instantiated lonwizstate object. This is normally called at the end of the lonwizstate constructor.
173:
174: =cut
175:
176: sub registerState {
177: my $self = shift;
178: my $state = shift;
179:
180: my $stateName = $state->name();
181: $self->{STATES}{$stateName} = $state;
182: }
183:
184: =pod
185:
186: =item B<changeState>(stateName): Given a string representing the name of some registered state, this causes the wizard to change to that state. Generally, states will call this.
187:
188: =cut
189:
190: sub changeState {
191: my $self = shift;
192: $self->{STATE} = shift;
193: }
194:
195: =pod
196:
197: =item B<display>(): This is the main method that the handler using the wizard calls.
198:
199: =cut
200:
201: # Done in five phases
202: # 1: Do the post processing for the previous state.
203: # 2: Do the preprocessing for the current state.
204: # 3: Check to see if state changed, if so, postprocess current and move to next.
205: # Repeat until state stays stable.
206: # 4: Render the current state to the screen as an HTML page.
207: sub display {
208: my $self = shift;
209:
210: my $result = "";
211:
212: # Phase 1: Post processing for state of previous screen (which is actually
213: # the current state), if it wasn't the beginning state.
214: if ($self->{STATE} ne "START" || $ENV{"form.SUBMIT"} eq "Next ->")
215: {
216: my $prevState = $self->{STATES}{$self->{STATE}};
217: $prevState->postprocess();
218: }
219:
220: # Note, to handle errors in a state's input that a user must correct,
221: # do not transition in the postprocess, and force the user to correct
222: # the error.
223:
224: # Phase 2: Preprocess current state
225: my $startState = $self->{STATE};
226: my $state = $self->{STATES}{$startState};
1.3 bowersj2 227:
228: # Error checking
229: if (!defined($state)) {
230: $result .="Error! The state ". $startState ." is not defined.";
231: return $result;
232: }
1.1 bowersj2 233: $state->preprocess();
234:
235: # Phase 3: While the current state is different from the previous state,
236: # keep processing.
237: while ( $startState ne $self->{STATE} )
238: {
239: $startState = $self->{STATE};
240: $state = $self->{STATES}{$startState};
241: $state->preprocess();
242: }
243:
244: # Phase 4: Display.
245: my $stateTitle = $state->title();
1.3 bowersj2 246: my $bodytag = &Apache::loncommon::bodytag("$self->{TITLE}",'','');
1.1 bowersj2 247:
248: $result .= <<HEADER;
249: <html>
250: <head>
1.3 bowersj2 251: <title>LON-CAPA Wizard: $self->{TITLE}</title>
1.1 bowersj2 252: </head>
1.3 bowersj2 253: $bodytag
254: HEADER
1.10 bowersj2 255: if (!$state->overrideForm()) { $result.="<form name='wizform' method='GET'>"; }
1.3 bowersj2 256: $result .= <<HEADER;
257: <table border="0"><tr><td>
258: <h2><i>$stateTitle</i></h2>
1.1 bowersj2 259: HEADER
260:
1.3 bowersj2 261: if (!$state->overrideForm()) {
262: $result .= $self->_saveVars();
263: }
1.1 bowersj2 264: $result .= $state->render() . "<p> </p>";
265:
1.3 bowersj2 266: if (!$state->overrideForm()) {
267: $result .= '<center>';
268: if ($self->{STATE} ne $self->{START_STATE})
269: {
270: #$result .= '<input name="SUBMIT" type="submit" value="<- Previous" /> ';
271: }
272: if ($self->{DONE})
273: {
274: my $returnPage = $self->{RETURN_PAGE};
275: $result .= "<a href=\"$returnPage\">End Wizard</a>";
276: }
277: else
278: {
1.4 bowersj2 279: $result .= '<input name="back" type="button" ';
280: $result .= 'value="<- Previous" onclick="history.go(-1)" /> ';
1.3 bowersj2 281: $result .= '<input name="SUBMIT" type="submit" value="Next ->" />';
282: }
283: $result .= "</center>\n";
1.1 bowersj2 284: }
285:
286: $result .= <<FOOTER;
1.3 bowersj2 287: </td>
288: </tr>
289: </table>
1.1 bowersj2 290: </form>
291: </body>
292: </html>
293: FOOTER
1.3 bowersj2 294:
295: return $result;
1.1 bowersj2 296: }
297:
298: =pod
299:
300: =item B<name>([name]): Returns the name of the wizard. If a parameter is passed, that will be saved as the name.
301:
302: =cut
303:
304: # Returns/sets the name of this wizard, i.e., "Assignment Parameter"
305: sub title {
306: my $self = shift;
307: if (@_) { $self->{TITLE} = shift};
308: return $self->{TITLE};
309: }
310:
311: =pod
312:
313: =item B<getVars>(): Returns a hash reference containing the stored vars for this wizard. The states use this for variables maintained across states. Example: C<my %vars = %{$wizard-E<gt>getVars()};> This provides read-only access, apparently.
314:
315: =cut
316:
317: sub getVars {
318: my $self = shift;
319: return ($self->{VARS});
320: }
321:
322: =pod
323:
324: =item B<setVar>(key, val): Sets the var named "key" to "val" in the wizard's form array.
325:
326: =cut
327:
1.3 bowersj2 328: # This may look trivial, but it's here as a hook for possible later processing
1.1 bowersj2 329: sub setVar {
330: my $self = shift;
331: my $key = shift;
332: my $val = shift;
333: $self->{VARS}{$key} = $val;
334: }
335:
336: =pod
337:
1.4 bowersj2 338: =item B<queryStringVars>(): Returns a string representing the current state of the wizard, suitable for use directly as part of a query string. (See resource_state for an example.)
339:
340: =cut
341:
342: sub queryStringVars {
343: my $self = shift;
344:
345: my @queryString = ();
346:
347: for my $varname (keys %{$self->{VARS}}) {
348: push @queryString, Apache::lonnet::escape($varname) . "=" .
349: Apache::lonnet::escape($self->{VARS}{$varname});
350: }
351: push @queryString, 'CURRENT_STATE=' . Apache::lonnet::escape($self->{STATE});
352: push @queryString, 'RETURN_PAGE=' . Apache::lonnet::escape($self->{RETURN_PAGE});
353:
354: return join '&', @queryString;
355: }
356:
357: =pod
358:
1.1 bowersj2 359: =item B<setDone>(): If a state calls this, the wizard will consider itself completed. The state should display a friendly "Done" message, and the wizard will display a link returning the user to the invoking page, rather then a "Next" button.
360:
361: =cut
362:
363:
364: # A temp function for debugging
365: sub handler {
366: my $r = shift;
367:
368: Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
369:
1.3 bowersj2 370: if ($r->header_only) {
371: if ($ENV{'browser.mathml'}) {
372: $r->content_type('text/xml');
373: } else {
374: $r->content_type('text/html');
375: }
376: $r->send_http_header;
377: return OK;
378: }
379:
380: # Send header, don't cache this page
381: if ($ENV{'browser.mathml'}) {
382: $r->content_type('text/xml');
383: } else {
384: $r->content_type('text/html');
385: }
386: &Apache::loncommon::no_cache($r);
387: $r->send_http_header;
388: $r->rflush();
389:
390: my $mes = <<WIZBEGIN;
1.7 bowersj2 391: <p>This wizard will allow you to <b>set open, due, and answer dates for problems</b>. You will be asked to select a problem, what kind of date you want to set, and for whom the date should be effective.</p>
1.3 bowersj2 392:
1.7 bowersj2 393: <p>After the wizard is done, you will be shown where in the advanced interface you would have gone to change the parameter you have chosen, so in the future you can do it directly.</p>
1.1 bowersj2 394:
1.7 bowersj2 395: <p>Press <b>Next -></b> to begin, or select <b><- Previous</b> to go back to the previous screen.</p>
1.3 bowersj2 396: WIZBEGIN
1.1 bowersj2 397:
1.3 bowersj2 398: my $wizard = Apache::lonwizard->new("Course Parameter Wizard");
399: $wizard->declareVars(['ACTION_TYPE', 'GRANULARITY', 'TARGETS', 'PARM_DATE', 'RESOURCE_ID', 'USER_NAME', 'SECTION_NAME']);
1.7 bowersj2 400: my %dateTypeHash = ('open_date' => "opening date",
401: 'due_date' => "due date",
402: 'answer_date' => "answer date");
403: my %levelTypeHash = ('whole_course' => "all problems in the course",
404: 'map' => 'the selected folder',
405: 'resource' => 'the selected problem');
1.3 bowersj2 406:
1.7 bowersj2 407: Apache::lonwizard::message_state->new($wizard, "START", "Welcome to the Assignment Parameter Wizard", $mes, "CHOOSE_LEVEL");
408: Apache::lonwizard::switch_state->new($wizard, "CHOOSE_LEVEL", "Which Problem or Problems?", "GRANULARITY", [
409: ["whole_course", "<b>Every problem</b> in the course", "CHOOSE_ACTION"],
410: ["map", "Every problem in a particular <b>folder</b>", "CHOOSE_FOLDER"],
411: ["resource", "One particular <b>problem</b>", "CHOOSE_RESOURCE"]],
412: "Which problems do you wish to change a date for?");
413: Apache::lonwizard::resource_choice->new($wizard, "CHOOSE_FOLDER", "Select Folder", "Select the folder you wish to set the date for:", "", "CHOOSE_ACTION", "RESOURCE_ID", sub {my $res = shift; return $res->is_map();});
414: Apache::lonwizard::resource_choice->new($wizard, "CHOOSE_RESOURCE", "Select Resource", "", "", "CHOOSE_ACTION", "RESOURCE_ID", sub {my $res = shift; return $res->is_map() || $res->is_problem();}, sub {my $res = shift; return $res->is_problem(); });
415: my $levelType = $levelTypeHash{$wizard->{VARS}->{GRANULARITY}};
416: Apache::lonwizard::switch_state->new($wizard, "CHOOSE_ACTION", "Parameter Type", "ACTION_TYPE", [
417: ["open_date", "Set an <b>open date</b>", "CHOOSE_DATE"],
418: ["due_date", "Set a <b>due date</b>", "CHOOSE_DATE"],
419: ["answer_date", "Set an <b>answer open date</b>", "CHOOSE_DATE" ] ],
420: "What parameters do you want to set for $levelType?");
421: my $dateType = $dateTypeHash{$wizard->{VARS}->{ACTION_TYPE}};
422: Apache::lonwizard::date_state->new($wizard, "CHOOSE_DATE", "Set Date", "PARM_DATE", "CHOOSE_STUDENT_LEVEL", "What should the $dateType be set to?");
423: Apache::lonwizard::switch_state->new($wizard, "CHOOSE_STUDENT_LEVEL", "Students Affected", "TARGETS", [
424: ["course", ". . . for <b>all students</b> in the course", "FINISH"],
425: ["section", ". . . for a particular <b>section</b>", "CHOOSE_SECTION"],
426: ["student", ". . . for an individual <b>student</b>", "CHOOSE_STUDENT"]],
427: "Set $dateType of $levelType for. . .");
1.3 bowersj2 428:
1.7 bowersj2 429: Apache::lonwizard::choose_section->new($wizard, "CHOOSE_SECTION", "Select Section", "Please select the section you wish to set the $dateType for:", "", "FINISH", "SECTION_NAME");
430: Apache::lonwizard::choose_student->new($wizard, "CHOOSE_STUDENT", "Select Student", "Please select the student you wish to set the $dateType for:", "", "FINISH", "USER_NAME");
1.3 bowersj2 431: Apache::lonwizard::parmwizfinal->new($wizard, "FINISH", "Confirm Selection");
432:
1.1 bowersj2 433: $r->print($wizard->display());
434:
435: return OK;
436: }
437:
438:
439:
440: 1;
441:
442: =head1 Class: lonwizstate
443:
444: A "lonwizstate" object represents a lonwizard state. A "state" is basically what is visible on the screen. For instance, a state may display a radio button dialog with three buttons, and wait for the user to choose one.
445:
446: Several pre-prepared child classes are include in lonwizard. If you create a new wizard type, be sure to add it to lonwizard.pm so others can use it too.
447:
1.3 bowersj2 448: It is importent to remember when constructing states that the user may use the "Previous" button to go back and revisit a state previously filled out. Thus, states should consult the wizard variables they are intended to set to see if the user has already selected something, and when displaying themselves should reselect the same values, such that the user paging from the end to the beginning, back to the end, will not change any settings.
449:
450: None of the pre-packaged states correctly handle there being B<no> input, as the wizard does not currently have any protection against errors in the states themselves. (The closest thing you can do is set the wizard to be done and display an error message, which should be adequate.)
451:
1.7 bowersj2 452: By default, the wizard framework will take form elements of the form {VAR_NAME}.forminput and automatically insert the contents of that form element into the wizard variable {VAR_NAME}. You only need to use postprocess to do something fancy if that is not sufficient, for instance, processing a multi-element selection. (See resource choice for an example of that.)
453:
1.1 bowersj2 454: =head2 lonwizstate methods
455:
456: These methods should be overridden in derived states, except B<new> which may be sufficient.
457:
458: =over 2
459:
460: =item B<new> (parentLonWizReference, stateName, stateTitle): Creates a new state and returns it. The first argument is a reference to the parent wizard. The second is the name of the state, which I<must> be unique. The third is the title, which will be displayed on the screen to the human.
461:
462: =item B<preprocess>(): preprocess sets up all of the information the state needs to do its job, such as querying data bases to obtain lists of choices, and sets up data for the render method. If preprocess decides to jump to a new state, it is responsible for manually running post-process, if it so desires.
463:
464: =over 2
465:
466: =item If this method calls the parent lonwizard's B<changeState> method to another state, then the state will never be rendered on the screen, and the wizard will move to the specified state. This is useful if the state may only be necessary to clarify an ambiguous input, such as selecting a part from a multi-part problem, which isn't necessary if the problem only has one state.
467:
468: =back
469:
470: =item B<render>(): render returns a string of itself to be rendered to the screen, which the wizard will display.
471:
472: =cut
473:
474: package Apache::lonwizard::state;
475:
476: use strict;
477:
478: sub new {
479: my $proto = shift;
480: my $class = ref($proto) || $proto;
481: my $self = {};
482: $self->{WIZARD} = shift;
483: $self->{NAME} = shift;
484: $self->{TITLE} = shift;
485:
486: bless($self);
487:
488: $self->{WIZARD}->registerState($self);
489: return $self;
490: }
491:
492: sub name {
493: my $self = shift;
494: if (@_) { $self->{NAME} = shift};
495: return $self->{NAME};
496: }
497:
498: sub title {
499: my $self = shift;
500: if (@_) { $self->{TITLE} = shift};
501: return $self->{TITLE};
502: }
503:
504: sub preprocess {
505: return 1;
506: }
507:
1.11 ! bowersj2 508: =pod
! 509:
! 510: =item * B<process_multiple_choices>(formname, var_name): A service function that correctly handles resources with multiple selections, such as checkboxes. It delimits the selections with triple pipes and stores them in the given wizard variable. 'formname' is the name of the form element to process.
! 511:
! 512: =back
! 513:
! 514: =cut
! 515:
! 516: sub process_multiple_choices {
! 517: my $self = shift;
! 518: my $formname = shift;
! 519: my $var = shift;
! 520: my $wizard = $self->{WIZARD};
! 521:
! 522: my $formvalue = $ENV{'form.' . $var};
! 523: if ($formvalue) {
! 524: # Must extract values from $wizard->{DATA} directly, as there
! 525: # may be more then one.
! 526: my @values;
! 527: for my $formparam (split (/&/, $wizard->{DATA})) {
! 528: my ($name, $value) = split(/=/, $formparam);
! 529: if ($name ne $var) {
! 530: next;
! 531: }
! 532: $value =~ tr/+/ /;
! 533: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
! 534: push @values, $value;
! 535: }
! 536: $wizard->setVar($var, join('|||', @values));
! 537: }
! 538:
! 539: return;
! 540: }
! 541:
1.1 bowersj2 542: sub render {
543: return "This is the empty state. If you can see this, it's a bug.\n"
544: }
545:
546: sub postprocess {
547: return 1;
548: }
549:
1.3 bowersj2 550: # If this is 1, the wizard assumes the state will override the
551: # wizard's form, useful for some final states
552: sub overrideForm {
553: return 0;
554: }
555:
1.1 bowersj2 556: 1;
557:
558: =pod
559:
560: =back
561:
562: =head1 Prepackaged States
563:
564: lonwizard provides several pre-packaged states that you can drop into your Wizard and obtain common functionality.
565:
566: =head2 Class: message_state
567:
568: message_state is a state the simply displays a message. It does not do any pre- or postprocessing. It makes a good initial state, which traditionally is a short message telling the user what they are about to accomplish, and may contain warnings or preconditions that should be fulfilled before using the wizard.
569:
570: =over 4
571:
1.3 bowersj2 572: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, message, nextState): Two new parameters "message" will be the HTML message displayed to the user, and "nextState" is the name of the next state.
1.1 bowersj2 573:
574: =back
575:
576: =cut
577:
578: package Apache::lonwizard::message_state;
579:
580: no strict;
581: @ISA = ("Apache::lonwizard::state");
582: use strict;
583:
584: sub new {
585: my $proto = shift;
586: my $class = ref($proto) || $proto;
587:
1.3 bowersj2 588: # This cute looking statement correctly handles subclassing
1.1 bowersj2 589: my $self = bless $proto->SUPER::new(shift, shift, shift);
590:
591: $self->{MESSAGE} = shift;
592: $self->{NEXT_STATE} = shift;
593:
594: return $self;
595: }
596:
597: sub postprocess {
598: my $self = shift;
599: $self->{WIZARD}->changeState($self->{NEXT_STATE});
600: return 1;
601: }
602:
603: sub render {
604: my $self = shift;
605: return $self->{MESSAGE};
606: }
607:
608: 1;
609:
610: package Apache::lonwizard::choice_state;
611:
612: no strict;
613: @ISA = ("Apache::lonwizard::state");
614: use strict;
615:
616: =pod
617:
618: =head2 Class: choice_state
619:
620: Choice state provides a single choice to the user as a text selection box. You pass it a message and hash containing [human_name] -> [computer_name] entries, and it will display the choices and store the result in the provided variable.
621:
622: If there is only one choice, the state will automatically make it and go to the next state.
623:
624: =over 4
625:
626: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, choiceHash): messageBefore is the HTML text that will be displayed before the choice display, messageAfter will display after. Keys will be sorted according to human name. nextState is the state to proceed to after the choice. varName is the name of the wizard var to store the computer_name answer in. choiceHash is the hash described above. It is optional because you may override it.
627:
1.3 bowersj2 628: =back
629:
1.1 bowersj2 630: =cut
631:
1.3 bowersj2 632: sub new {
633: my $proto = shift;
634: my $class = ref($proto) || $proto;
635: my $self = bless $proto->SUPER::new(shift, shift, shift);
636:
637: $self->{MESSAGE_BEFORE} = shift;
638: $self->{MESSAGE_AFTER} = shift;
639: $self->{NEXT_STATE} = shift;
640: $self->{VAR_NAME} = shift;
641: $self->{CHOICE_HASH} = shift;
642: $self->{NO_CHOICES} = 0;
643:
644: return $self;
645: }
646:
1.1 bowersj2 647: sub preprocess {
648: my $self = shift;
1.3 bowersj2 649: my $choices = $self->{CHOICE_HASH};
650: if (!defined($self->{CHOICE_HASH})) {
651: $choices = $self->{CHOICE_HASH} = $self->determineChoices();
652: }
653: my $wizvars = $self->{WIZARD}->getVars();
1.1 bowersj2 654:
1.3 bowersj2 655: my @keys = keys(%$choices);
1.1 bowersj2 656: @keys = sort @keys;
657:
658: if (scalar(@keys) == 0)
659: {
660: # No choices... so prepare to display error message and cancel further execution.
661: $self->{NO_CHOICES} = 1;
1.3 bowersj2 662: $self->{WIZARD}->{DONE} = 1;
1.1 bowersj2 663: return;
664: }
665: if (scalar(@keys) == 1)
666: {
667: # If there is only one choice, pick it and move on.
1.3 bowersj2 668: $wizvars->{$self->{VAR_NAME}} = $choices->{$keys[0]};
1.1 bowersj2 669: $self->{WIZARD}->changeState($self->{NEXT_STATE});
670: return;
671: }
672:
673: # Otherwise, do normal processing in the render routine.
674:
675: return;
676: }
677:
678: sub determineChoices {
679: return {"NO_CHOICE" => "No choices were given."};
680: }
681:
682: sub render {
683: my $self = shift;
684: my $result = "";
685: my $var = $self->{VAR_NAME};
686:
1.3 bowersj2 687: if (defined $self->{ERROR_MSG}) {
688: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
689: }
690:
1.10 bowersj2 691: if (defined $self->{MESSAGE_BEFORE}) {
1.3 bowersj2 692: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
1.1 bowersj2 693: }
694:
1.3 bowersj2 695: my $choices = $self->{CHOICE_HASH};
696: my @keys = keys (%$choices);
697:
698: $result .= "<select name=\"$var.forminput\" size=\"10\">\n";
699: foreach (@keys)
700: {
701: $result .= "<option value=\"" . HTML::Entities::encode($choices->{$_})
1.8 bowersj2 702: . "\">" . HTML::Entities::encode($_) . "</option>\n";
1.3 bowersj2 703: }
704: $result .= "</select>\n\n";
1.1 bowersj2 705:
1.3 bowersj2 706: if (defined $self->{MESSAGE_AFTER})
707: {
708: $result .= '<br /><br />' . $self->{MESSAGE_AFTER};
709: }
710:
711: return $result;
712: }
713:
714: sub postprocess {
715: my $self = shift;
716: my $wizard = $self->{WIZARD};
717: my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
718: if ($formvalue) {
1.7 bowersj2 719: # Value already stored by Wizard
1.3 bowersj2 720: $wizard->changeState($self->{NEXT_STATE});
721: } else {
722: $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
723: . ' a selection to continue.';
724: }
725: return 1;
726: }
727:
728: package Apache::lonwizard::switch_state;
729:
730: no strict;
731: @ISA = ("Apache::lonwizard::state");
732: use strict;
733:
734: =pod
1.1 bowersj2 735:
1.3 bowersj2 736: =head2 Class; switch_state
737:
738: Switch state provides the ability to present the user with several radio-button choices. The state can store the user response in a wizard variable, and can also send the user to a different state for each selection, which is the intended primary purpose.
739:
740: Each choice may have arbitrary HTML associated with it, which will be used as the label. The first choice will be selected by default.
741:
742: =over 4
743:
744: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, varName, choiceList, messageBefore, messageAfter): varName is the name of the wizard variable the state will set with the choice made. choiceHash is list reference of a list of list references to three element lists, where the first element is what the wizard var varName will be set to, the second is the HTML that will be displayed for that choice, and the third is the destination state. messageBefore is an optional HTML string that will be placed before the message, messageAfter an optional HTML string that will be placed before.
745:
746: An example of a legit choiceList: C<my $choicelist = [ ["flunk", "Flunk Student", "FLUNK_STATE"], ["pass", "Pass Student", "PASS_STATE"] ];>
747:
748: =back
749:
750: =cut
751:
752: sub new {
753: my $proto = shift;
754: my $class = ref($proto) || $proto;
755: my $self = bless $proto->SUPER::new(shift, shift, shift);
756:
757: $self->{VAR_NAME} = shift;
758: $self->{CHOICE_LIST} = shift;
759: $self->{MESSAGE_BEFORE} = shift;
760: $self->{MESSAGE_AFTER} = shift;
761:
762: return $self;
763: }
764:
765: # Don't need a preprocess step; we assume we know the choices
766:
767: sub render {
768: my $self = shift;
769: my $result = "";
770: my $var = $self->{VAR_NAME};
771: my @choices = @{$self->{CHOICE_LIST}};
772: my $curVal = $self->{WIZARD}->{VARS}->{$var};
773:
774: $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});
775:
776: if (!$curVal) {
777: $curVal = $self->{CHOICE_LIST}->[0]->[0]; # top is default
778: }
779:
780: $result .= "<table>\n\n";
781:
782: foreach my $choice (@choices)
783: {
784: my $value = $choice->[0];
785: my $text = $choice->[1];
786:
787: $result .= "<tr>\n<td width='20'> </td>\n<td>";
788: $result .= "<td valign=\"top\"><input type=\"radio\" name=\"$var.forminput\"";
789: $result .= " checked" if ($value eq $curVal);
790: $result .= " value=\"$value\"></td>\n<td>$text</td>\n</tr>\n\n";
791: }
792:
793: $result .= "<table>\n\n";
794:
795: $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
796:
797: return $result;
798: }
799:
800: sub postprocess {
1.7 bowersj2 801: # Value already stored by wizard
1.3 bowersj2 802: my $self = shift;
803: my $wizard = $self->{WIZARD};
804: my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . '.forminput'};
805:
806: foreach my $choice (@{$self->{CHOICE_LIST}})
807: {
808: if ($choice->[0] eq $chosenValue)
809: {
810: $wizard->changeState($choice->[2]);
811: }
812: }
813: }
814:
815: # If there is only one choice, make it and move on
816: sub preprocess {
817: my $self = shift;
818: my $choiceList = $self->{CHOICE_LIST};
819: my $wizard = $self->{WIZARD};
820:
821: if (scalar(@{$choiceList}) == 1)
822: {
823: my $choice = $choiceList->[0];
824: my $chosenVal = $choice->[0];
825: my $nextState = $choice->[2];
826:
827: $wizard->setVar($self->{VAR_NAME}, $chosenVal)
828: if (defined ($self->{VAR_NAME}));
829: $wizard->changeState($nextState);
830: }
831: }
832:
833: 1;
834:
835: package Apache::lonwizard::date_state;
836:
837: use Time::localtime;
838: use Time::Local;
839: use Time::tm;
840:
841: no strict;
842: @ISA = ("Apache::lonwizard::state");
843: use strict;
844:
845: my @months = ("January", "February", "March", "April", "May", "June", "July",
846: "August", "September", "October", "November", "December");
847:
848: =pod
849:
850: =head2 Class: date_state
851:
852: Date state provides a state for selecting a date/time, as seen in the course parmset wizard.. You can choose to display date entry if that's what you need.
853:
854: =over 4
855:
856: =item overriddent method B<new>(parentLonWizReference, stateName, stateTitle, varName, nextState, messageBefore, messageAfter, displayJustDate): varName is where the date/time will be stored as seconds since the epoch. messageBefore and messageAfter as other states. displayJustDate is a flag defaulting to false that if true, will only display the date selection (defaulting to midnight on that date). Otherwise, minutes and hours will be shown.
857:
858: =back
859:
860: =cut
861:
862: sub new {
863: my $proto = shift;
864: my $class = ref($proto) || $proto;
865: my $self = bless $proto->SUPER::new(shift, shift, shift);
866:
867: $self->{VAR_NAME} = shift;
868: $self->{NEXT_STATE} = shift;
869: $self->{MESSAGE_BEFORE} = shift;
870: $self->{MESSAGE_AFTER} = shift;
871: $self->{DISPLAY_JUST_DATE} = shift;
872: if (!defined($self->{DISPLAY_JUST_DATE})) {$self->{DISPLAY_JUST_DATE} = 0;}
873: return $self;
874: }
875:
876: sub render {
877: my $self = shift;
878: my $result = "";
879: my $var = $self->{VAR_NAME};
880: my $name = $self->{NAME};
881: my $wizvars = $self->{WIZARD}->getVars();
882:
883: my $date;
884:
885: # Pick default date: Now, or previous choice
886: if (defined ($wizvars->{$var}) && $wizvars->{$var} ne "")
887: {
888: $date = localtime($wizvars->{$var});
889: }
890: else
1.1 bowersj2 891: {
1.3 bowersj2 892: $date = localtime();
893: }
894:
895: if (defined $self->{ERROR_MSG}) {
896: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
897: }
898:
899: if (defined ($self->{MESSAGE_BEFORE})) {
900: $result .= $self->{MESSAGE_BEFORE};
901: $result .= "<br /><br />\n\n";
902: }
903:
904: # Month
905: my $i;
906: $result .= "<select name='$self->{VAR_NAME}month'>\n";
907: for ($i = 0; $i < 12; $i++) {
908: if ($i == $date->mon) {
909: $result .= "<option value='$i' selected>";
910: } else {
911: $result .= "<option value='$i'>";
912: }
1.8 bowersj2 913: $result .= $months[$i] . "</option>\n";
1.3 bowersj2 914: }
915: $result .= "</select>\n";
916:
917: # Day
918: $result .= "<select name='$self->{VAR_NAME}day'>\n";
919: for ($i = 1; $i < 32; $i++) {
920: if ($i == $date->mday) {
921: $result .= '<option selected>';
922: } else {
923: $result .= '<option>';
924: }
1.8 bowersj2 925: $result .= "$i</option>\n";
1.3 bowersj2 926: }
927: $result .= "</select>,\n";
928:
929: # Year
930: $result .= "<select name='$self->{VAR_NAME}year'>\n";
931: for ($i = 2000; $i < 2030; $i++) { # update this after 64-bit dates
932: if ($date->year + 1900 == $i) {
933: $result .= "<option selected>";
934: } else {
935: $result .= "<option>";
936: }
1.8 bowersj2 937: $result .= "$i</option>\n";
1.3 bowersj2 938: }
939: $result .= "</select>,\n";
940:
941: # Display Hours and Minutes if they are called for
942: if (!$self->{DISPLAY_JUST_DATE}) {
1.8 bowersj2 943: # Build hour
1.3 bowersj2 944: $result .= "<select name='$self->{VAR_NAME}hour'>\n";
1.8 bowersj2 945: $result .= "<option " . ($date->hour == 0 ? 'selected ':'') .
946: " value='0'>midnight</option>\n";
1.3 bowersj2 947: for ($i = 1; $i < 12; $i++) {
1.8 bowersj2 948: if ($date->hour == $i) {
949: $result .= "<option selected value='$i'>$i a.m.</option>\n";
950: } else {
951: $result .= "<option value='$i'>$i a.m</option>\n";
952: }
953: }
954: $result .= "<option " . ($date->hour == 12 ? 'selected ':'') .
955: " value='12'>noon</option>\n";
956: for ($i = 13; $i < 24; $i++) {
957: my $printedHour = $i - 12;
958: if ($date->hour == $i) {
959: $result .= "<option selected value='$i'>$printedHour p.m.</option>\n";
1.3 bowersj2 960: } else {
1.8 bowersj2 961: $result .= "<option value='$i'>$printedHour p.m.</option>\n";
1.3 bowersj2 962: }
963: }
1.8 bowersj2 964:
1.3 bowersj2 965: $result .= "</select> :\n";
966:
967: $result .= "<select name='$self->{VAR_NAME}minute'>\n";
968: for ($i = 0; $i < 60; $i++) {
1.8 bowersj2 969: my $printedMinute = $i;
970: if ($i < 10) {
971: $printedMinute = "0" . $printedMinute;
972: }
1.3 bowersj2 973: if ($date->min == $i) {
974: $result .= "<option selected>";
975: } else {
976: $result .= "<option>";
977: }
1.8 bowersj2 978: $result .= "$printedMinute</option>\n";
1.3 bowersj2 979: }
980: $result .= "</select>\n";
981: }
982:
983: if (defined ($self->{MESSAGE_AFTER})) {
984: $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
1.1 bowersj2 985: }
986:
987: return $result;
988: }
989:
1.3 bowersj2 990: # Stick the date stored into the chosen variable.
1.1 bowersj2 991: sub postprocess {
992: my $self = shift;
993: my $wizard = $self->{WIZARD};
1.3 bowersj2 994:
995: my $month = $ENV{'form.' . $self->{VAR_NAME} . 'month'};
996: my $day = $ENV{'form.' . $self->{VAR_NAME} . 'day'};
997: my $year = $ENV{'form.' . $self->{VAR_NAME} . 'year'};
998: my $min = 0;
999: my $hour = 0;
1000: if (!$self->{DISPLAY_JUST_DATE}) {
1001: $min = $ENV{'form.' . $self->{VAR_NAME} . 'minute'};
1002: $hour = $ENV{'form.' . $self->{VAR_NAME} . 'hour'};
1003: }
1004:
1005: my $chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);
1006: # Check to make sure that the date was not automatically co-erced into a
1007: # valid date, as we want to flag that as an error
1008: # This happens for "Feb. 31", for instance, which is coerced to March 2 or
1009: # 3, depending on if it's a leapyear
1010: my $checkDate = localtime($chosenDate);
1011:
1012: if ($checkDate->mon != $month || $checkDate->mday != $day ||
1013: $checkDate->year + 1900 != $year) {
1014: $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
1015: . "date because it doesn't exist. Please enter a valid date.";
1016: return;
1017: }
1018:
1019: $wizard->setVar($self->{VAR_NAME}, $chosenDate);
1020:
1.1 bowersj2 1021: $wizard->changeState($self->{NEXT_STATE});
1.3 bowersj2 1022: }
1023:
1024: 1;
1025:
1026: package Apache::lonwizard::parmwizfinal;
1027:
1028: # This is the final state for the parmwizard. It is not generally useful,
1029: # so it is not perldoc'ed. It does it's own processing.
1030:
1031: no strict;
1032: @ISA = ('Apache::lonwizard::state');
1033: use strict;
1034:
1035: use Time::localtime;
1036:
1037: sub new {
1038: my $proto = shift;
1039: my $class = ref($proto) || $proto;
1040: my $self = bless $proto->SUPER::new(shift, shift, shift);
1041:
1042: # No other variables because it gets it all from the wizard.
1043: }
1044:
1045: # Renders a form that, when submitted, will form the input to lonparmset.pm
1046: sub render {
1047: my $self = shift;
1048: my $wizard = $self->{WIZARD};
1049: my $wizvars = $wizard->{VARS};
1050:
1051: # FIXME: Unify my designators with the standard ones
1052: my %dateTypeHash = ('open_date' => "Opening Date",
1053: 'due_date' => "Due Date",
1054: 'answer_date' => "Answer Date");
1055: my %parmTypeHash = ('open_date' => "0_opendate",
1056: 'due_date' => "0_duedate",
1057: 'answer_date' => "0_answerdate");
1058:
1.10 bowersj2 1059: my $result = "<form name='wizform' method='get' action='/adm/parmset'>\n";
1.3 bowersj2 1060: $result .= '<p>Confirm that this information is correct, then click "Finish Wizard" to complete setting the parameter.<ul>';
1061: my $affectedResourceId = "";
1062: my $parm_name = $parmTypeHash{$wizvars->{ACTION_TYPE}};
1063: my $level = "";
1064:
1065: # Print the type of manipulation:
1066: $result .= '<li>Setting the <b>' . $dateTypeHash{$wizvars->{ACTION_TYPE}}
1067: . "</b></li>\n";
1068: if ($wizvars->{ACTION_TYPE} eq 'due_date' ||
1069: $wizvars->{ACTION_TYPE} eq 'answer_date') {
1070: # for due dates, we default to "date end" type entries
1071: $result .= "<input type='hidden' name='recent_date_end' " .
1072: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1073: $result .= "<input type='hidden' name='pres_value' " .
1074: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1075: $result .= "<input type='hidden' name='pres_type' " .
1076: "value='date_end' />\n";
1077: } elsif ($wizvars->{ACTION_TYPE} eq 'open_date') {
1078: $result .= "<input type='hidden' name='recent_date_start' ".
1079: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1080: $result .= "<input type='hidden' name='pres_value' " .
1081: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1082: $result .= "<input type='hidden' name='pres_type' " .
1083: "value='date_start' />\n";
1084: }
1085:
1086: # Print the granularity, depending on the action
1087: if ($wizvars->{GRANULARITY} eq 'whole_course') {
1088: $result .= '<li>for <b>all resources in the course</b></li>';
1089: $level = 9; # general course, see lonparmset.pm perldoc
1090: $affectedResourceId = "0.0";
1091: } elsif ($wizvars->{GRANULARITY} eq 'map') {
1092: my $navmap = Apache::lonnavmaps::navmap->new(
1093: $ENV{"request.course.fn"}.".db",
1094: $ENV{"request.course.fn"}."_parms.db", 0, 0);
1095: my $res = $navmap->getById($wizvars->{RESOURCE_ID});
1096: my $title = $res->compTitle();
1097: $navmap->untieHashes();
1098: $result .= "<li>for the map named <b>$title</b></li>";
1099: $level = 8;
1100: $affectedResourceId = $wizvars->{RESOURCE_ID};
1101: } else {
1102: my $navmap = Apache::lonnavmaps::navmap->new(
1103: $ENV{"request.course.fn"}.".db",
1104: $ENV{"request.course.fn"}."_parms.db", 0, 0);
1105: my $res = $navmap->getById($wizvars->{RESOURCE_ID});
1106: my $title = $res->compTitle();
1107: $navmap->untieHashes();
1108: $result .= "<li>for the resource named <b>$title</b></li>";
1109: $level = 7;
1110: $affectedResourceId = $wizvars->{RESOURCE_ID};
1111: }
1112:
1113: # Print targets
1114: if ($wizvars->{TARGETS} eq 'course') {
1115: $result .= '<li>for <b>all students in course</b></li>';
1116: } elsif ($wizvars->{TARGETS} eq 'section') {
1117: my $section = $wizvars->{SECTION_NAME};
1118: $result .= "<li>for section <b>$section</b></li>";
1119: $level -= 3;
1120: $result .= "<input type='hidden' name='csec' value='" .
1121: HTML::Entities::encode($section) . "' />\n";
1122: } else {
1123: # FIXME: This is probably wasteful!
1124: my $classlist = Apache::loncoursedata::get_classlist();
1125: my $name = $classlist->{$wizvars->{USER_NAME}}->[6];
1126: $result .= "<li>for <b>$name</b></li>";
1127: $level -= 6;
1128: my ($uname, $udom) = split /:/, $wizvars->{USER_NAME};
1129: $result .= "<input type='hidden' name='uname' value='".
1130: HTML::Entities::encode($uname) . "' />\n";
1131: $result .= "<input type='hidden' name='udom' value='".
1132: HTML::Entities::encode($udom) . "' />\n";
1133: }
1134:
1135: # Print value
1136: $result .= "<li>to <b>" . ctime($wizvars->{PARM_DATE}) . "</b> (" .
1137: Apache::lonnavmaps::timeToHumanString($wizvars->{PARM_DATE})
1138: . ")</li>\n";
1139:
1140: # print pres_marker
1141: $result .= "\n<input type='hidden' name='pres_marker'" .
1142: " value='$affectedResourceId&$parm_name&$level' />\n";
1143:
1144: $result .= "<br /><br /><center><input type='submit' value='Finish Wizard' /></center></form>\n";
1145:
1146: return $result;
1147: }
1148:
1149: sub overrideForm {
1.1 bowersj2 1150: return 1;
1151: }
1152:
1.3 bowersj2 1153: 1;
1154:
1155: package Apache::lonwizard::resource_choice;
1156:
1157: =pod
1158:
1159: =head2 Class: resource_choice
1160:
1.10 bowersj2 1161: resource_choice gives the user an opportunity to select one resource from the current course, and will stick the ID of that choice (#.#) into the desired variable.
1.3 bowersj2 1162:
1163: Note this state will not automatically advance if there is only one choice, because it might confuse the user in this case.
1164:
1165: =over 4
1166:
1.10 bowersj2 1167: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, filterFunction, choiceFunction): messageBefore and messageAfter appear before and after the state choice, respectively. nextState is the state to proceed to after the choice. varName is the wizard variable to store the choice in.
1.3 bowersj2 1168:
1169: filterFunction is a function reference that receives the current resource as an argument, and returns 1 if it should be displayed, and 0 if it should not be displayed. By default, the class will use sub {return 1;}, which will show all resources. choiceFunction is a reference to a function that receives the resource object as a parameter and returns 1 if it should be a *selectable choice*, and 0 if not. By default, this is the same as the filterFunction, which means all displayed choices will be choosable. See parm wizard for an example of this in the resource selection routines.
1170:
1171: =back
1172:
1173: =cut
1.1 bowersj2 1174:
1175: no strict;
1176: @ISA = ("Apache::lonwizard::state");
1177: use strict;
1.3 bowersj2 1178:
1179: sub new {
1180: my $proto = shift;
1181: my $class = ref($proto) || $proto;
1182: my $self = bless $proto->SUPER::new(shift, shift, shift);
1183:
1184: $self->{MESSAGE_BEFORE} = shift;
1185: $self->{MESSAGE_AFTER} = shift;
1186: $self->{NEXT_STATE} = shift;
1187: $self->{VAR_NAME} = shift;
1188: $self->{FILTER_FUNC} = shift;
1189: if (!defined($self->{FILTER_FUNC})) {
1190: $self->{FILTER_FUNC} = sub {return 1;};
1191: }
1192: $self->{CHOICE_FUNC} = shift;
1193: if (!defined($self->{CHOICE_FUNC})) {
1194: $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
1195: }
1196: }
1197:
1198: sub postprocess {
1199: my $self = shift;
1200: my $wizard = $self->{WIZARD};
1.4 bowersj2 1201:
1202: # If we were just manipulating a folder, do not proceed to the
1203: # next state
1204: if ($ENV{'form.folderManip'}) {
1205: return;
1206: }
1207:
1.7 bowersj2 1208: if (!$ENV{'form.' . $self->{VAR_NAME} . '.forminput'}) {
1209: $self->{ERROR_MSG} = "Can't continue wizard because you must ".
1210: "select a resource.";
1211: return;
1212: }
1213:
1214:
1215: # Value stored by wizard framework
1216:
1.3 bowersj2 1217: $wizard->changeState($self->{NEXT_STATE});
1218: }
1219:
1.10 bowersj2 1220: # A note, in case I don't get to this before I leave.
1221: # If someone complains about the "Back" button returning them
1222: # to the previous folder state, instead of returning them to
1223: # the previous wizard state, the *correct* answer is for the wizard
1224: # to keep track of how many times the user has manipulated the folders,
1225: # and feed that to the history.go() call in the wizard rendering routines.
1226: # If done correctly, the wizard itself can keep track of how many times
1227: # it renders the same states, so it doesn't go in just this state, and
1228: # you can lean on the browser back button to make sure it all chains
1229: # correctly.
1230:
1.3 bowersj2 1231: sub render {
1232: my $self = shift;
1.4 bowersj2 1233: my $wizard = $self->{WIZARD};
1.3 bowersj2 1234: my $result = "";
1235: my $var = $self->{VAR_NAME};
1236: my $curVal = $self->{WIZARD}->{VARS}->{$var};
1.4 bowersj2 1237: my $vals = {};
1238: if ($curVal =~ /,/) { # multiple choices
1239: foreach (split /,/, $curVal) {
1240: $vals->{$_} = 1;
1241: }
1242: } else {
1243: $vals->{$curVal} = 1;
1.7 bowersj2 1244: }
1245:
1246: if (defined $self->{ERROR_MSG}) {
1247: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1.4 bowersj2 1248: }
1.3 bowersj2 1249:
1.8 bowersj2 1250: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
1251: if (defined $self->{MESSAGE_BEFORE});
1.3 bowersj2 1252:
1253: my $filterFunc = $self->{FILTER_FUNC};
1254: my $choiceFunc = $self->{CHOICE_FUNC};
1255:
1.4 bowersj2 1256: # Create the composite function that renders the column on the nav map
1257: my $renderColFunc = sub {
1258: my ($resource, $part, $params) = @_;
1259:
1260: if (!&$choiceFunc($resource)) {
1261: return '<td> </td>';
1262: } else {
1263: my $col = "<td><input type='radio' name='${var}.forminput' ";
1264: if ($vals->{$resource->{ID}}) {
1265: $col .= "checked ";
1.3 bowersj2 1266: }
1.4 bowersj2 1267: $col .= "value='" . $resource->{ID} . "' /></td>";
1268: return $col;
1.3 bowersj2 1269: }
1.4 bowersj2 1270: };
1.3 bowersj2 1271:
1.4 bowersj2 1272: $result .=
1.8 bowersj2 1273: &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc,
1.4 bowersj2 1274: Apache::lonnavmaps::resource()],
1275: 'showParts' => 0,
1276: 'queryString' => $wizard->queryStringVars() . '&folderManip=1',
1.8 bowersj2 1277: 'url' => '/adm/wizard',
1278: 'filterFunc' => $filterFunc } );
1.4 bowersj2 1279:
1.3 bowersj2 1280: $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
1281:
1282: return $result;
1283: }
1284:
1285: 1;
1286:
1.10 bowersj2 1287: package Apache::lonwizard::resource_multichoice;
1288:
1289: =pod
1290:
1291: =head2 Class: resource_multichoice
1292:
1293: resource_multichoice gives the user an opportunity to select multiple resources from some map in the current course, and will stick a list of the IDs of those choices in its variable.
1294:
1295: Note this state will not automatically advance if there is only one choice, because it might confuse the user. Also, the state will not advance until at least I<one> choice is taken, because it is generally nonsense to select nothing when this state is used.
1296:
1297: This is generally intended for use on a specific sequence, not the entire course, as for technical reasons the user can't open and close folders, so they must all be shown as open. To fix this would require making the folders image form submitters and remembering the selected state of each resource, which is not impossible but is too much error-prone work to do until it seems many people will want that feature.
1298:
1299: =over 4
1300:
1301: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, filterFunction, choiceFunction, map): Arguments like resource_choice. map is the ID number of a specific map that, if given is all that will be shown to the user, instead of the whole course.
1302:
1303: =back
1304:
1305: =cut
1306:
1307: no strict;
1308: @ISA = ("Apache::lonwizard::state");
1309: use strict;
1310:
1311: sub new {
1312: my $proto = shift;
1313: my $class = ref($proto) || $proto;
1314: my $self = bless $proto->SUPER::new(shift, shift, shift);
1315:
1316: $self->{MESSAGE_BEFORE} = shift;
1317: $self->{MESSAGE_AFTER} = shift;
1318: $self->{NEXT_STATE} = shift;
1319: $self->{VAR_NAME} = shift;
1320: $self->{FILTER_FUNC} = shift;
1321: if (!defined($self->{FILTER_FUNC})) {
1322: $self->{FILTER_FUNC} = sub {return 1;};
1323: }
1324: $self->{CHOICE_FUNC} = shift;
1325: if (!defined($self->{CHOICE_FUNC})) {
1326: $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
1327: }
1328: $self->{MAP} = shift;
1329: if (!defined($self->{MAP})) {
1330: $self->{MAP} = 1; # 0? trying to default to entire course
1331: }
1332: }
1333:
1334: sub postprocess {
1335: my $self = shift;
1336: my $wizard = $self->{WIZARD};
1337:
1.11 ! bowersj2 1338: $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
! 1339: $self->{VAR_NAME});
1.10 bowersj2 1340:
1341: # If nothing was selected...
1342: if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
1343: $self->{ERROR_MSG} = "You must select one or more resources to continue.";
1344: return;
1345: }
1346:
1347: $wizard->changeState($self->{NEXT_STATE});
1348: }
1349:
1350: sub render {
1351: my $self = shift;
1352: my $wizard = $self->{WIZARD};
1353: my $var = $self->{VAR_NAME};
1354: my $result = <<SCRIPT;
1355: <script>
1356: function checkall(value) {
1357: for (i=0; i<document.forms.wizform.elements.length; i++) {
1358: document.forms.wizform.elements[i].checked=value;
1359: }
1360: }
1361: </script>
1362: SCRIPT
1363:
1364: my $buttons = <<BUTTONS;
1365: <input type="button" onclick="checkall(true)" value="Select All" />
1366: <input type="button" onclick="checkall(false)" value="Unselect All" />
1367: <br />
1368: BUTTONS
1369:
1370: if (defined $self->{ERROR_MSG}) {
1371: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1372: }
1373:
1374: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
1375: if (defined $self->{MESSAGE_BEFORE});
1376:
1377: my $filterFunc = $self->{FILTER_FUNC};
1378: my $choiceFunc = $self->{CHOICE_FUNC};
1379:
1380: # Create the composite function that renders the column on the nav map
1381: my $renderColFunc = sub {
1382: my ($resource, $part, $params) = @_;
1383:
1384: if (!&$choiceFunc($resource)) {
1385: return '<td> </td>';
1386: } else {
1387: my $col = "<td><input type='checkbox' name='${var}.forminput'".
1388: " value='" . $resource->{ID} . "' /></td>";
1389: return $col;
1390: }
1391: };
1392:
1393: $result .= $buttons;
1394:
1395: $result .=
1396: &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc,
1397: Apache::lonnavmaps::resource()],
1398: 'showParts' => 0,
1399: 'filterFunc' => $filterFunc,
1400: 'iterator_map' => $self->{MAP},
1401: 'resource_no_folder_link' => 1 } );
1402:
1403: $result .= $buttons;
1404:
1405: $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
1406:
1407: return $result;
1408: }
1409: 1;
1410:
1.3 bowersj2 1411: package Apache::lonwizard::choose_student;
1412:
1413: no strict;
1414: @ISA = ("Apache::lonwizard::choice_state");
1415: use strict;
1416:
1417: sub new {
1418: my $proto = shift;
1419: my $class = ref($proto) || $proto;
1420: my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
1421: shift, shift, shift);
1422: return $self;
1423: }
1424:
1425: sub determineChoices {
1426: my %choices;
1427:
1428: my $classlist = Apache::loncoursedata::get_classlist();
1429: foreach (keys %$classlist) {
1430: $choices{$classlist->{$_}->[6]} = $_;
1431: }
1432:
1433: return \%choices;
1434: }
1435:
1436: 1;
1437:
1438: package Apache::lonwizard::choose_section;
1439:
1440: no strict;
1441: @ISA = ("Apache::lonwizard::choice_state");
1442: use strict;
1443:
1444: sub new {
1445: my $proto = shift;
1446: my $class = ref($proto) || $proto;
1447: my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
1448: shift, shift, shift);
1449: return $self;
1450: }
1451:
1452: sub determineChoices {
1453: my %choices;
1454:
1455: my $classlist = Apache::loncoursedata::get_classlist();
1456: foreach (keys %$classlist) {
1457: my $sectionName = $classlist->{$_}->[5];
1458: if (!$sectionName) {
1459: $choices{"No section assigned"} = "";
1460: } else {
1461: $choices{$sectionName} = $sectionName;
1462: }
1463: }
1464:
1465: return \%choices;
1466: }
1467:
1468: 1;
1.1 bowersj2 1469:
1.10 bowersj2 1470: package Apache::lonwizard::choose_files;
1471:
1472: =pod
1473:
1474: =head2 Class: choose_file
1475:
1476: choose_file offers a choice of files from a given directory. It will store them as a triple-pipe delimited list in its given wizard variable, in the standard HTML multiple-selection tradition. A filter function can be passed, which will examine the filename and return 1 if it should be displayed, or 0 if not.
1477:
1478: =over 4
1479:
1480: =item * overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, subdir, filterFunc): As in previous states, where filterFunc is as described in choose_file. subdir is the name of the subdirectory to offer choices from.
1481:
1482: =back
1483:
1484: =cut
1485:
1486: no strict;
1487: @ISA = ("Apache::lonwizard::state");
1488: use strict;
1489:
1490: sub new {
1491: my $proto = shift;
1492: my $class = ref($proto) || $proto;
1493: my $self = bless $proto->SUPER::new(shift, shift, shift);
1494:
1495: $self->{MESSAGE_BEFORE} = shift;
1496: $self->{MESSAGE_AFTER} = shift;
1497: $self->{NEXT_STATE} = shift;
1498: $self->{VAR_NAME} = shift;
1499: $self->{SUB_DIR} = shift;
1500: $self->{FILTER_FUNC} = shift;
1501:
1502: if (!defined($self->{FILTER_FUNC})) {
1503: $self->{FILTER_FUNC} = sub {return 1;};
1504: }
1505:
1506: return $self;
1507: }
1508:
1509: sub render {
1510: my $self = shift;
1511: my $result = '';
1512: my $var = $self->{VAR_NAME};
1513: my $subdir = $self->{SUB_DIR};
1514: my $filterFunc = $self->{FILTER_FUNC};
1515:
1516: $result = <<SCRIPT;
1517: <script>
1518: function checkall(value) {
1519: for (i=0; i<document.forms.wizform.elements.length; i++) {
1520: ele = document.forms.wizform.elements[i];
1521: if (ele.type == "checkbox") {
1522: document.forms.wizform.elements[i].checked=value;
1523: }
1524: }
1525: }
1526: </script>
1527: SCRIPT
1528:
1529: my $buttons = <<BUTTONS;
1530: <br />
1531: <input type="button" onclick="checkall(true)" value="Select All" />
1532: <input type="button" onclick="checkall(false)" value="Unselect All" />
1533: <br />
1534: BUTTONS
1535:
1536: if (defined $self->{ERROR_MSG}) {
1537: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1538: }
1539:
1540: if ($self->{MESSAGE_BEFORE}) {
1541: $result .= $self->{MESSAGE_BEFORE} . '<br />';
1542: }
1543:
1544: # Get the list of files in this directory.
1545: my @fileList;
1546:
1547: # If the subdirectory is in local CSTR space
1548: if ($subdir =~ m|/home/([^/]+)/public_html|) {
1549: my $user = $1;
1550: my $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1551: @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
1552: } else {
1553: # local library server resource space
1554: @fileList = &Apache::lonnet::dirlist($subdir, $ENV{'user.domain'}, $ENV{'user.name'}, '');
1555: }
1556:
1557: $result .= $buttons;
1558:
1559: $result .= '<table border="0" cellpadding="1" cellspacing="1">';
1560:
1561: # Keeps track if there are no choices, prints appropriate error
1562: # if there are none.
1563: my $choices = 0;
1564: # Print each legitimate file choice.
1565: for my $file (@fileList) {
1566: $file = (split(/&/, $file))[0];
1567: my $fileName = $subdir .'/'. $file;
1568: if (&$filterFunc($file)) {
1569: $result .= '<tr><td align="right">' .
1570: "<input type='checkbox' name='" . $self->{VAR_NAME}
1571: . ".forminput' value='" . HTML::Entities::encode($fileName) .
1572: "' /></td><td>" . $file . "</td></tr>\n";
1573: $choices++;
1574: }
1575: }
1576:
1577: $result .= "</table>\n";
1578:
1579: if (!$choices) {
1580: $result .= '<font color="#FF0000">There are no files available to select in this directory. Please go back and select another option.</font><br /><br />';
1581: }
1582:
1583: $result .= $buttons;
1584:
1585: if ($self->{MESSAGE_AFTER}) {
1586: $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
1587: }
1588:
1589: return $result;
1590: }
1591:
1592: sub postprocess {
1593: my $self = shift;
1594: print $self->{NEXT_STATE};
1595: my $wizard = $self->{WIZARD};
1596:
1.11 ! bowersj2 1597: $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
! 1598: $self->{VAR_NAME});
! 1599:
! 1600: if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
1.10 bowersj2 1601: $self->{ERROR_MSG} = "Can't continue the wizard because you ".
1602: "must make a selection to continue.";
1603: }
1604: return 1;
1605: }
1606:
1607: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>