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