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