Annotation of loncom/interface/lonwizard.pm, revision 1.14
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:
1.14 ! bowersj2 525: my $formvalue = $ENV{'form.' . $formname};
1.11 bowersj2 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);
1.14 ! bowersj2 532: if ($name ne $formname) {
1.11 bowersj2 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:
1.13 bowersj2 629: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, choiceHash, multichoice): 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. multichoice is true if the user can make multiple choices, false otherwise. (Multiple choices will be seperated with ||| in the wizard variable.
1.1 bowersj2 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;
1.13 bowersj2 645: $self->{MULTICHOICE} = shift;
1.3 bowersj2 646: $self->{NO_CHOICES} = 0;
647:
648: return $self;
649: }
650:
1.1 bowersj2 651: sub preprocess {
652: my $self = shift;
1.3 bowersj2 653: my $choices = $self->{CHOICE_HASH};
654: if (!defined($self->{CHOICE_HASH})) {
655: $choices = $self->{CHOICE_HASH} = $self->determineChoices();
656: }
657: my $wizvars = $self->{WIZARD}->getVars();
1.1 bowersj2 658:
1.3 bowersj2 659: my @keys = keys(%$choices);
1.1 bowersj2 660: @keys = sort @keys;
661:
662: if (scalar(@keys) == 0)
663: {
664: # No choices... so prepare to display error message and cancel further execution.
665: $self->{NO_CHOICES} = 1;
1.3 bowersj2 666: $self->{WIZARD}->{DONE} = 1;
1.1 bowersj2 667: return;
668: }
669: if (scalar(@keys) == 1)
670: {
671: # If there is only one choice, pick it and move on.
1.3 bowersj2 672: $wizvars->{$self->{VAR_NAME}} = $choices->{$keys[0]};
1.1 bowersj2 673: $self->{WIZARD}->changeState($self->{NEXT_STATE});
674: return;
675: }
676:
677: # Otherwise, do normal processing in the render routine.
678:
679: return;
680: }
681:
682: sub determineChoices {
683: return {"NO_CHOICE" => "No choices were given."};
684: }
685:
686: sub render {
687: my $self = shift;
688: my $result = "";
689: my $var = $self->{VAR_NAME};
1.13 bowersj2 690: my $buttons = '';
1.1 bowersj2 691:
1.13 bowersj2 692: if ($self->{MULTICHOICE}) {
693: $result = <<SCRIPT;
694: <script>
695: function checkall(value) {
696: for (i=0; i<document.forms.wizform.elements.length; i++) {
697: document.forms.wizform.elements[i].checked=value;
698: }
699: }
700: </script>
701: SCRIPT
702: $buttons = <<BUTTONS;
703: <input type="button" onclick="checkall(true)" value="Select All" />
704: <input type="button" onclick="checkall(false)" value="Unselect All" />
705: <br />
706: BUTTONS
707: }
708:
1.3 bowersj2 709: if (defined $self->{ERROR_MSG}) {
710: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
711: }
712:
1.10 bowersj2 713: if (defined $self->{MESSAGE_BEFORE}) {
1.3 bowersj2 714: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
1.1 bowersj2 715: }
716:
1.13 bowersj2 717: $result .= $buttons;
718:
1.3 bowersj2 719: my $choices = $self->{CHOICE_HASH};
720: my @keys = keys (%$choices);
721:
1.13 bowersj2 722: my $type = "radio";
723: if ($self->{MULTICHOICE}) { $type = 'checkbox'; }
1.14 ! bowersj2 724: foreach (@keys) {
1.13 bowersj2 725:
726: $result .= "<input type='$type' name='" .
727: $self->{VAR_NAME} . '.forminput' .
728: "' value=\"" .
729: HTML::Entities::encode($choices->{$_})
730: . "\"/> " . HTML::Entities::encode($_) . "<br />\n";
1.3 bowersj2 731: }
1.1 bowersj2 732:
1.14 ! bowersj2 733: if (defined $self->{MESSAGE_AFTER}) {
1.3 bowersj2 734: $result .= '<br /><br />' . $self->{MESSAGE_AFTER};
735: }
736:
737: return $result;
738: }
739:
740: sub postprocess {
741: my $self = shift;
742: my $wizard = $self->{WIZARD};
743: my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
744: if ($formvalue) {
1.14 ! bowersj2 745: if ($self->{MULTICHOICE}) {
! 746: $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
! 747: $self->{VAR_NAME});
! 748: }
! 749: # For non-multichoice, value already stored by Wizard
1.3 bowersj2 750: $wizard->changeState($self->{NEXT_STATE});
751: } else {
752: $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
753: . ' a selection to continue.';
754: }
755: return 1;
756: }
757:
758: package Apache::lonwizard::switch_state;
759:
760: no strict;
761: @ISA = ("Apache::lonwizard::state");
762: use strict;
763:
764: =pod
1.1 bowersj2 765:
1.3 bowersj2 766: =head2 Class; switch_state
767:
768: 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.
769:
770: Each choice may have arbitrary HTML associated with it, which will be used as the label. The first choice will be selected by default.
771:
772: =over 4
773:
1.13 bowersj2 774: =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. The special setting 'ILLEGAL' can be used in the first place to state that it is not a legal chocie (see lonprintout.pm for real-life usage of that). messageBefore is an optional HTML string that will be placed before the message, messageAfter an optional HTML string that will be placed before.
1.3 bowersj2 775:
1.13 bowersj2 776: Note that ILLEGAL is desirable because some choices may not always be good choices, but they should not necessarily disappear with no explanantion of why they are no good. In lonprintout.pm, for instance, the choice "Problems from current sequence" may be no good because there are no problems in the sequence, but it should not silently disappear; it should announce that there are no problems in the sequence.
777:
778: An example of a legit choiceList: C<my $choicelist = [ ["flunk", "Flunk Student", "FLUNK_STATE"], ["pass", "Pass Student", "PASS_STATE"] ];>
1.3 bowersj2 779:
780: =back
781:
782: =cut
783:
784: sub new {
785: my $proto = shift;
786: my $class = ref($proto) || $proto;
787: my $self = bless $proto->SUPER::new(shift, shift, shift);
788:
789: $self->{VAR_NAME} = shift;
790: $self->{CHOICE_LIST} = shift;
791: $self->{MESSAGE_BEFORE} = shift;
792: $self->{MESSAGE_AFTER} = shift;
793:
794: return $self;
795: }
796:
797: # Don't need a preprocess step; we assume we know the choices
798:
799: sub render {
800: my $self = shift;
801: my $result = "";
802: my $var = $self->{VAR_NAME};
803: my @choices = @{$self->{CHOICE_LIST}};
804: my $curVal = $self->{WIZARD}->{VARS}->{$var};
805:
806: $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});
807:
808: if (!$curVal) {
809: $curVal = $self->{CHOICE_LIST}->[0]->[0]; # top is default
810: }
811:
812: $result .= "<table>\n\n";
813:
814: foreach my $choice (@choices)
815: {
816: my $value = $choice->[0];
817: my $text = $choice->[1];
818:
819: $result .= "<tr>\n<td width='20'> </td>\n<td>";
820: $result .= "<td valign=\"top\"><input type=\"radio\" name=\"$var.forminput\"";
821: $result .= " checked" if ($value eq $curVal);
822: $result .= " value=\"$value\"></td>\n<td>$text</td>\n</tr>\n\n";
823: }
824:
825: $result .= "<table>\n\n";
826:
827: $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
828:
829: return $result;
830: }
831:
832: sub postprocess {
1.7 bowersj2 833: # Value already stored by wizard
1.3 bowersj2 834: my $self = shift;
835: my $wizard = $self->{WIZARD};
836: my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . '.forminput'};
837:
838: foreach my $choice (@{$self->{CHOICE_LIST}})
839: {
840: if ($choice->[0] eq $chosenValue)
841: {
842: $wizard->changeState($choice->[2]);
843: }
844: }
845: }
846:
847: # If there is only one choice, make it and move on
848: sub preprocess {
849: my $self = shift;
850: my $choiceList = $self->{CHOICE_LIST};
851: my $wizard = $self->{WIZARD};
852:
853: if (scalar(@{$choiceList}) == 1)
854: {
855: my $choice = $choiceList->[0];
856: my $chosenVal = $choice->[0];
857: my $nextState = $choice->[2];
858:
859: $wizard->setVar($self->{VAR_NAME}, $chosenVal)
860: if (defined ($self->{VAR_NAME}));
861: $wizard->changeState($nextState);
862: }
863: }
864:
865: 1;
866:
867: package Apache::lonwizard::date_state;
868:
869: use Time::localtime;
870: use Time::Local;
871: use Time::tm;
872:
873: no strict;
874: @ISA = ("Apache::lonwizard::state");
875: use strict;
876:
877: my @months = ("January", "February", "March", "April", "May", "June", "July",
878: "August", "September", "October", "November", "December");
879:
880: =pod
881:
882: =head2 Class: date_state
883:
884: 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.
885:
886: =over 4
887:
888: =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.
889:
890: =back
891:
892: =cut
893:
894: sub new {
895: my $proto = shift;
896: my $class = ref($proto) || $proto;
897: my $self = bless $proto->SUPER::new(shift, shift, shift);
898:
899: $self->{VAR_NAME} = shift;
900: $self->{NEXT_STATE} = shift;
901: $self->{MESSAGE_BEFORE} = shift;
902: $self->{MESSAGE_AFTER} = shift;
903: $self->{DISPLAY_JUST_DATE} = shift;
904: if (!defined($self->{DISPLAY_JUST_DATE})) {$self->{DISPLAY_JUST_DATE} = 0;}
905: return $self;
906: }
907:
908: sub render {
909: my $self = shift;
910: my $result = "";
911: my $var = $self->{VAR_NAME};
912: my $name = $self->{NAME};
913: my $wizvars = $self->{WIZARD}->getVars();
914:
915: my $date;
916:
917: # Pick default date: Now, or previous choice
918: if (defined ($wizvars->{$var}) && $wizvars->{$var} ne "")
919: {
920: $date = localtime($wizvars->{$var});
921: }
922: else
1.1 bowersj2 923: {
1.3 bowersj2 924: $date = localtime();
925: }
926:
927: if (defined $self->{ERROR_MSG}) {
928: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
929: }
930:
931: if (defined ($self->{MESSAGE_BEFORE})) {
932: $result .= $self->{MESSAGE_BEFORE};
933: $result .= "<br /><br />\n\n";
934: }
935:
936: # Month
937: my $i;
938: $result .= "<select name='$self->{VAR_NAME}month'>\n";
939: for ($i = 0; $i < 12; $i++) {
940: if ($i == $date->mon) {
941: $result .= "<option value='$i' selected>";
942: } else {
943: $result .= "<option value='$i'>";
944: }
1.8 bowersj2 945: $result .= $months[$i] . "</option>\n";
1.3 bowersj2 946: }
947: $result .= "</select>\n";
948:
949: # Day
950: $result .= "<select name='$self->{VAR_NAME}day'>\n";
951: for ($i = 1; $i < 32; $i++) {
952: if ($i == $date->mday) {
953: $result .= '<option selected>';
954: } else {
955: $result .= '<option>';
956: }
1.8 bowersj2 957: $result .= "$i</option>\n";
1.3 bowersj2 958: }
959: $result .= "</select>,\n";
960:
961: # Year
962: $result .= "<select name='$self->{VAR_NAME}year'>\n";
963: for ($i = 2000; $i < 2030; $i++) { # update this after 64-bit dates
964: if ($date->year + 1900 == $i) {
965: $result .= "<option selected>";
966: } else {
967: $result .= "<option>";
968: }
1.8 bowersj2 969: $result .= "$i</option>\n";
1.3 bowersj2 970: }
971: $result .= "</select>,\n";
972:
973: # Display Hours and Minutes if they are called for
974: if (!$self->{DISPLAY_JUST_DATE}) {
1.8 bowersj2 975: # Build hour
1.3 bowersj2 976: $result .= "<select name='$self->{VAR_NAME}hour'>\n";
1.8 bowersj2 977: $result .= "<option " . ($date->hour == 0 ? 'selected ':'') .
978: " value='0'>midnight</option>\n";
1.3 bowersj2 979: for ($i = 1; $i < 12; $i++) {
1.8 bowersj2 980: if ($date->hour == $i) {
981: $result .= "<option selected value='$i'>$i a.m.</option>\n";
982: } else {
983: $result .= "<option value='$i'>$i a.m</option>\n";
984: }
985: }
986: $result .= "<option " . ($date->hour == 12 ? 'selected ':'') .
987: " value='12'>noon</option>\n";
988: for ($i = 13; $i < 24; $i++) {
989: my $printedHour = $i - 12;
990: if ($date->hour == $i) {
991: $result .= "<option selected value='$i'>$printedHour p.m.</option>\n";
1.3 bowersj2 992: } else {
1.8 bowersj2 993: $result .= "<option value='$i'>$printedHour p.m.</option>\n";
1.3 bowersj2 994: }
995: }
1.8 bowersj2 996:
1.3 bowersj2 997: $result .= "</select> :\n";
998:
999: $result .= "<select name='$self->{VAR_NAME}minute'>\n";
1000: for ($i = 0; $i < 60; $i++) {
1.8 bowersj2 1001: my $printedMinute = $i;
1002: if ($i < 10) {
1003: $printedMinute = "0" . $printedMinute;
1004: }
1.3 bowersj2 1005: if ($date->min == $i) {
1006: $result .= "<option selected>";
1007: } else {
1008: $result .= "<option>";
1009: }
1.8 bowersj2 1010: $result .= "$printedMinute</option>\n";
1.3 bowersj2 1011: }
1012: $result .= "</select>\n";
1013: }
1014:
1015: if (defined ($self->{MESSAGE_AFTER})) {
1016: $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
1.1 bowersj2 1017: }
1018:
1019: return $result;
1020: }
1021:
1.3 bowersj2 1022: # Stick the date stored into the chosen variable.
1.1 bowersj2 1023: sub postprocess {
1024: my $self = shift;
1025: my $wizard = $self->{WIZARD};
1.3 bowersj2 1026:
1027: my $month = $ENV{'form.' . $self->{VAR_NAME} . 'month'};
1028: my $day = $ENV{'form.' . $self->{VAR_NAME} . 'day'};
1029: my $year = $ENV{'form.' . $self->{VAR_NAME} . 'year'};
1030: my $min = 0;
1031: my $hour = 0;
1032: if (!$self->{DISPLAY_JUST_DATE}) {
1033: $min = $ENV{'form.' . $self->{VAR_NAME} . 'minute'};
1034: $hour = $ENV{'form.' . $self->{VAR_NAME} . 'hour'};
1035: }
1036:
1037: my $chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);
1038: # Check to make sure that the date was not automatically co-erced into a
1039: # valid date, as we want to flag that as an error
1040: # This happens for "Feb. 31", for instance, which is coerced to March 2 or
1041: # 3, depending on if it's a leapyear
1042: my $checkDate = localtime($chosenDate);
1043:
1044: if ($checkDate->mon != $month || $checkDate->mday != $day ||
1045: $checkDate->year + 1900 != $year) {
1046: $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
1047: . "date because it doesn't exist. Please enter a valid date.";
1048: return;
1049: }
1050:
1051: $wizard->setVar($self->{VAR_NAME}, $chosenDate);
1052:
1.1 bowersj2 1053: $wizard->changeState($self->{NEXT_STATE});
1.3 bowersj2 1054: }
1055:
1056: 1;
1057:
1058: package Apache::lonwizard::parmwizfinal;
1059:
1060: # This is the final state for the parmwizard. It is not generally useful,
1061: # so it is not perldoc'ed. It does it's own processing.
1062:
1063: no strict;
1064: @ISA = ('Apache::lonwizard::state');
1065: use strict;
1066:
1067: use Time::localtime;
1068:
1069: sub new {
1070: my $proto = shift;
1071: my $class = ref($proto) || $proto;
1072: my $self = bless $proto->SUPER::new(shift, shift, shift);
1073:
1074: # No other variables because it gets it all from the wizard.
1075: }
1076:
1077: # Renders a form that, when submitted, will form the input to lonparmset.pm
1078: sub render {
1079: my $self = shift;
1080: my $wizard = $self->{WIZARD};
1081: my $wizvars = $wizard->{VARS};
1082:
1083: # FIXME: Unify my designators with the standard ones
1084: my %dateTypeHash = ('open_date' => "Opening Date",
1085: 'due_date' => "Due Date",
1086: 'answer_date' => "Answer Date");
1087: my %parmTypeHash = ('open_date' => "0_opendate",
1088: 'due_date' => "0_duedate",
1089: 'answer_date' => "0_answerdate");
1090:
1.10 bowersj2 1091: my $result = "<form name='wizform' method='get' action='/adm/parmset'>\n";
1.3 bowersj2 1092: $result .= '<p>Confirm that this information is correct, then click "Finish Wizard" to complete setting the parameter.<ul>';
1093: my $affectedResourceId = "";
1094: my $parm_name = $parmTypeHash{$wizvars->{ACTION_TYPE}};
1095: my $level = "";
1096:
1097: # Print the type of manipulation:
1098: $result .= '<li>Setting the <b>' . $dateTypeHash{$wizvars->{ACTION_TYPE}}
1099: . "</b></li>\n";
1100: if ($wizvars->{ACTION_TYPE} eq 'due_date' ||
1101: $wizvars->{ACTION_TYPE} eq 'answer_date') {
1102: # for due dates, we default to "date end" type entries
1103: $result .= "<input type='hidden' name='recent_date_end' " .
1104: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1105: $result .= "<input type='hidden' name='pres_value' " .
1106: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1107: $result .= "<input type='hidden' name='pres_type' " .
1108: "value='date_end' />\n";
1109: } elsif ($wizvars->{ACTION_TYPE} eq 'open_date') {
1110: $result .= "<input type='hidden' name='recent_date_start' ".
1111: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1112: $result .= "<input type='hidden' name='pres_value' " .
1113: "value='" . $wizvars->{PARM_DATE} . "' />\n";
1114: $result .= "<input type='hidden' name='pres_type' " .
1115: "value='date_start' />\n";
1116: }
1117:
1118: # Print the granularity, depending on the action
1119: if ($wizvars->{GRANULARITY} eq 'whole_course') {
1120: $result .= '<li>for <b>all resources in the course</b></li>';
1121: $level = 9; # general course, see lonparmset.pm perldoc
1122: $affectedResourceId = "0.0";
1123: } elsif ($wizvars->{GRANULARITY} eq 'map') {
1124: my $navmap = Apache::lonnavmaps::navmap->new(
1125: $ENV{"request.course.fn"}.".db",
1126: $ENV{"request.course.fn"}."_parms.db", 0, 0);
1127: my $res = $navmap->getById($wizvars->{RESOURCE_ID});
1128: my $title = $res->compTitle();
1129: $navmap->untieHashes();
1130: $result .= "<li>for the map named <b>$title</b></li>";
1131: $level = 8;
1132: $affectedResourceId = $wizvars->{RESOURCE_ID};
1133: } else {
1134: my $navmap = Apache::lonnavmaps::navmap->new(
1135: $ENV{"request.course.fn"}.".db",
1136: $ENV{"request.course.fn"}."_parms.db", 0, 0);
1137: my $res = $navmap->getById($wizvars->{RESOURCE_ID});
1138: my $title = $res->compTitle();
1139: $navmap->untieHashes();
1140: $result .= "<li>for the resource named <b>$title</b></li>";
1141: $level = 7;
1142: $affectedResourceId = $wizvars->{RESOURCE_ID};
1143: }
1144:
1145: # Print targets
1146: if ($wizvars->{TARGETS} eq 'course') {
1147: $result .= '<li>for <b>all students in course</b></li>';
1148: } elsif ($wizvars->{TARGETS} eq 'section') {
1149: my $section = $wizvars->{SECTION_NAME};
1150: $result .= "<li>for section <b>$section</b></li>";
1151: $level -= 3;
1152: $result .= "<input type='hidden' name='csec' value='" .
1153: HTML::Entities::encode($section) . "' />\n";
1154: } else {
1155: # FIXME: This is probably wasteful!
1156: my $classlist = Apache::loncoursedata::get_classlist();
1157: my $name = $classlist->{$wizvars->{USER_NAME}}->[6];
1158: $result .= "<li>for <b>$name</b></li>";
1159: $level -= 6;
1160: my ($uname, $udom) = split /:/, $wizvars->{USER_NAME};
1161: $result .= "<input type='hidden' name='uname' value='".
1162: HTML::Entities::encode($uname) . "' />\n";
1163: $result .= "<input type='hidden' name='udom' value='".
1164: HTML::Entities::encode($udom) . "' />\n";
1165: }
1166:
1167: # Print value
1168: $result .= "<li>to <b>" . ctime($wizvars->{PARM_DATE}) . "</b> (" .
1169: Apache::lonnavmaps::timeToHumanString($wizvars->{PARM_DATE})
1170: . ")</li>\n";
1171:
1172: # print pres_marker
1173: $result .= "\n<input type='hidden' name='pres_marker'" .
1174: " value='$affectedResourceId&$parm_name&$level' />\n";
1175:
1176: $result .= "<br /><br /><center><input type='submit' value='Finish Wizard' /></center></form>\n";
1177:
1178: return $result;
1179: }
1180:
1181: sub overrideForm {
1.1 bowersj2 1182: return 1;
1183: }
1184:
1.3 bowersj2 1185: 1;
1186:
1187: package Apache::lonwizard::resource_choice;
1188:
1189: =pod
1190:
1191: =head2 Class: resource_choice
1192:
1.10 bowersj2 1193: 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 1194:
1195: Note this state will not automatically advance if there is only one choice, because it might confuse the user in this case.
1196:
1197: =over 4
1198:
1.10 bowersj2 1199: =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 1200:
1201: 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.
1202:
1203: =back
1204:
1205: =cut
1.1 bowersj2 1206:
1207: no strict;
1208: @ISA = ("Apache::lonwizard::state");
1209: use strict;
1.3 bowersj2 1210:
1211: sub new {
1212: my $proto = shift;
1213: my $class = ref($proto) || $proto;
1214: my $self = bless $proto->SUPER::new(shift, shift, shift);
1215:
1216: $self->{MESSAGE_BEFORE} = shift;
1217: $self->{MESSAGE_AFTER} = shift;
1218: $self->{NEXT_STATE} = shift;
1219: $self->{VAR_NAME} = shift;
1220: $self->{FILTER_FUNC} = shift;
1221: if (!defined($self->{FILTER_FUNC})) {
1222: $self->{FILTER_FUNC} = sub {return 1;};
1223: }
1224: $self->{CHOICE_FUNC} = shift;
1225: if (!defined($self->{CHOICE_FUNC})) {
1226: $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
1227: }
1228: }
1229:
1230: sub postprocess {
1231: my $self = shift;
1232: my $wizard = $self->{WIZARD};
1.4 bowersj2 1233:
1234: # If we were just manipulating a folder, do not proceed to the
1235: # next state
1236: if ($ENV{'form.folderManip'}) {
1237: return;
1238: }
1239:
1.7 bowersj2 1240: if (!$ENV{'form.' . $self->{VAR_NAME} . '.forminput'}) {
1241: $self->{ERROR_MSG} = "Can't continue wizard because you must ".
1242: "select a resource.";
1243: return;
1244: }
1245:
1246:
1247: # Value stored by wizard framework
1248:
1.3 bowersj2 1249: $wizard->changeState($self->{NEXT_STATE});
1250: }
1251:
1.10 bowersj2 1252: # A note, in case I don't get to this before I leave.
1253: # If someone complains about the "Back" button returning them
1254: # to the previous folder state, instead of returning them to
1255: # the previous wizard state, the *correct* answer is for the wizard
1256: # to keep track of how many times the user has manipulated the folders,
1257: # and feed that to the history.go() call in the wizard rendering routines.
1258: # If done correctly, the wizard itself can keep track of how many times
1259: # it renders the same states, so it doesn't go in just this state, and
1260: # you can lean on the browser back button to make sure it all chains
1261: # correctly.
1.13 bowersj2 1262: # Either that, or force all folders open and don't allow the user
1263: # to close them.
1.10 bowersj2 1264:
1.3 bowersj2 1265: sub render {
1266: my $self = shift;
1.4 bowersj2 1267: my $wizard = $self->{WIZARD};
1.3 bowersj2 1268: my $result = "";
1269: my $var = $self->{VAR_NAME};
1270: my $curVal = $self->{WIZARD}->{VARS}->{$var};
1.4 bowersj2 1271: my $vals = {};
1272: if ($curVal =~ /,/) { # multiple choices
1273: foreach (split /,/, $curVal) {
1274: $vals->{$_} = 1;
1275: }
1276: } else {
1277: $vals->{$curVal} = 1;
1.7 bowersj2 1278: }
1279:
1280: if (defined $self->{ERROR_MSG}) {
1281: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1.4 bowersj2 1282: }
1.3 bowersj2 1283:
1.8 bowersj2 1284: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
1285: if (defined $self->{MESSAGE_BEFORE});
1.3 bowersj2 1286:
1287: my $filterFunc = $self->{FILTER_FUNC};
1288: my $choiceFunc = $self->{CHOICE_FUNC};
1289:
1.4 bowersj2 1290: # Create the composite function that renders the column on the nav map
1291: my $renderColFunc = sub {
1292: my ($resource, $part, $params) = @_;
1293:
1294: if (!&$choiceFunc($resource)) {
1295: return '<td> </td>';
1296: } else {
1297: my $col = "<td><input type='radio' name='${var}.forminput' ";
1298: if ($vals->{$resource->{ID}}) {
1299: $col .= "checked ";
1.3 bowersj2 1300: }
1.4 bowersj2 1301: $col .= "value='" . $resource->{ID} . "' /></td>";
1302: return $col;
1.3 bowersj2 1303: }
1.4 bowersj2 1304: };
1.3 bowersj2 1305:
1.4 bowersj2 1306: $result .=
1.8 bowersj2 1307: &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc,
1.4 bowersj2 1308: Apache::lonnavmaps::resource()],
1309: 'showParts' => 0,
1310: 'queryString' => $wizard->queryStringVars() . '&folderManip=1',
1.12 bowersj2 1311: 'url' => $wizard->{URL},
1.8 bowersj2 1312: 'filterFunc' => $filterFunc } );
1.4 bowersj2 1313:
1.3 bowersj2 1314: $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
1315:
1316: return $result;
1317: }
1318:
1319: 1;
1320:
1.10 bowersj2 1321: package Apache::lonwizard::resource_multichoice;
1322:
1323: =pod
1324:
1325: =head2 Class: resource_multichoice
1326:
1327: 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.
1328:
1329: 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.
1330:
1331: 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.
1332:
1.13 bowersj2 1333: Note this class is generally useful for multi-choice selections, by overridding "determineChoices" to return the choice hash.
1334:
1.10 bowersj2 1335: =over 4
1336:
1337: =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.
1338:
1339: =back
1340:
1341: =cut
1342:
1343: no strict;
1344: @ISA = ("Apache::lonwizard::state");
1345: use strict;
1346:
1347: sub new {
1348: my $proto = shift;
1349: my $class = ref($proto) || $proto;
1350: my $self = bless $proto->SUPER::new(shift, shift, shift);
1351:
1352: $self->{MESSAGE_BEFORE} = shift;
1353: $self->{MESSAGE_AFTER} = shift;
1354: $self->{NEXT_STATE} = shift;
1355: $self->{VAR_NAME} = shift;
1356: $self->{FILTER_FUNC} = shift;
1357: if (!defined($self->{FILTER_FUNC})) {
1358: $self->{FILTER_FUNC} = sub {return 1;};
1359: }
1360: $self->{CHOICE_FUNC} = shift;
1361: if (!defined($self->{CHOICE_FUNC})) {
1362: $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
1363: }
1364: $self->{MAP} = shift;
1365: if (!defined($self->{MAP})) {
1366: $self->{MAP} = 1; # 0? trying to default to entire course
1367: }
1368: }
1369:
1370: sub postprocess {
1371: my $self = shift;
1372: my $wizard = $self->{WIZARD};
1373:
1.11 bowersj2 1374: $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
1375: $self->{VAR_NAME});
1.10 bowersj2 1376:
1377: # If nothing was selected...
1378: if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
1379: $self->{ERROR_MSG} = "You must select one or more resources to continue.";
1380: return;
1381: }
1382:
1383: $wizard->changeState($self->{NEXT_STATE});
1384: }
1385:
1386: sub render {
1387: my $self = shift;
1388: my $wizard = $self->{WIZARD};
1389: my $var = $self->{VAR_NAME};
1390: my $result = <<SCRIPT;
1391: <script>
1392: function checkall(value) {
1393: for (i=0; i<document.forms.wizform.elements.length; i++) {
1394: document.forms.wizform.elements[i].checked=value;
1395: }
1396: }
1397: </script>
1398: SCRIPT
1399:
1400: my $buttons = <<BUTTONS;
1401: <input type="button" onclick="checkall(true)" value="Select All" />
1402: <input type="button" onclick="checkall(false)" value="Unselect All" />
1403: <br />
1404: BUTTONS
1405:
1406: if (defined $self->{ERROR_MSG}) {
1407: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1408: }
1409:
1410: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
1411: if (defined $self->{MESSAGE_BEFORE});
1412:
1413: my $filterFunc = $self->{FILTER_FUNC};
1414: my $choiceFunc = $self->{CHOICE_FUNC};
1415:
1416: # Create the composite function that renders the column on the nav map
1417: my $renderColFunc = sub {
1418: my ($resource, $part, $params) = @_;
1419:
1420: if (!&$choiceFunc($resource)) {
1421: return '<td> </td>';
1422: } else {
1423: my $col = "<td><input type='checkbox' name='${var}.forminput'".
1424: " value='" . $resource->{ID} . "' /></td>";
1425: return $col;
1426: }
1427: };
1428:
1429: $result .= $buttons;
1430:
1431: $result .=
1432: &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc,
1433: Apache::lonnavmaps::resource()],
1434: 'showParts' => 0,
1435: 'filterFunc' => $filterFunc,
1436: 'iterator_map' => $self->{MAP},
1437: 'resource_no_folder_link' => 1 } );
1438:
1439: $result .= $buttons;
1440:
1441: $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
1442:
1443: return $result;
1444: }
1445: 1;
1446:
1.3 bowersj2 1447: package Apache::lonwizard::choose_student;
1448:
1449: no strict;
1.14 ! bowersj2 1450: @ISA = ("Apache::lonwizard::state");
1.3 bowersj2 1451: use strict;
1452:
1453: sub new {
1454: my $proto = shift;
1455: my $class = ref($proto) || $proto;
1.14 ! bowersj2 1456: my $self = bless $proto->SUPER::new(shift, shift, shift);
! 1457:
! 1458: $self->{MESSAGE_BEFORE} = shift;
! 1459: $self->{NEXT_STATE} = shift;
! 1460: $self->{VAR_NAME} = shift;
! 1461: $self->{MULTICHOICE} = shift;
! 1462:
1.3 bowersj2 1463: return $self;
1464: }
1465:
1.14 ! bowersj2 1466: sub render {
! 1467: my $self = shift;
! 1468: my $result = '';
! 1469: my $var = $self->{VAR_NAME};
! 1470: my $buttons = '';
! 1471:
! 1472: if ($self->{MULTICHOICE}) {
! 1473: $result = <<SCRIPT;
! 1474: <script>
! 1475: function checkall(value) {
! 1476: for (i=0; i<document.forms.wizform.elements.length; i++) {
! 1477: document.forms.wizform.elements[i].checked=value;
! 1478: }
! 1479: }
! 1480: </script>
! 1481: SCRIPT
! 1482: $buttons = <<BUTTONS;
! 1483: <input type="button" onclick="checkall(true)" value="Select All" />
! 1484: <input type="button" onclick="checkall(false)" value="Unselect All" />
! 1485: <br />
! 1486: BUTTONS
! 1487: }
! 1488:
! 1489: if (defined $self->{ERROR_MSG}) {
! 1490: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
! 1491: }
! 1492:
! 1493: if (defined $self->{MESSAGE_BEFORE}) {
! 1494: $result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
! 1495: }
! 1496:
! 1497: $result .= $buttons;
! 1498:
! 1499: my $choices = &Apache::loncoursedata::get_classlist();
! 1500:
! 1501: my @keys = keys %{$choices};
! 1502: # Sort by: Section, name
! 1503:
! 1504: @keys = sort {
! 1505: if ($choices->{$a}->[3] ne $choices->{$b}->[3]) {
! 1506: return $choices->{$a}->[3] cmp $choices->{$b}->[3];
! 1507: }
! 1508: return $choices->{$a}->[6] cmp $choices->{$b}->[6];
! 1509: } @keys;
! 1510:
! 1511: my $type = 'radio';
! 1512: if ($self->{MULTICHOICE}) { $type = 'checkbox'; }
! 1513: $result .= "<table cellspacing='2' cellpadding='2' border='0'>\n";
! 1514: $result .= "<tr><td></td><td align='center'><b>Student Name</b></td>".
! 1515: "<td align='center'><b>Section</b></td></tr>";
! 1516:
! 1517: foreach (@keys) {
! 1518: $result .= "<tr><td><input type='$type' name='" .
! 1519: $self->{VAR_NAME} . '.forminput' .
! 1520: "' value='" . HTML::Entities::encode($_)
! 1521: . "' /></td><td>" . HTML::Entities::encode($choices->{$_}->[6])
! 1522: . "</td><td align='center'>"
! 1523: . HTML::Entities::encode($choices->{$_}->[5])
! 1524: . "</td></tr>\n";
! 1525: }
! 1526:
! 1527: $result .= "</table>\n\n";
! 1528: $result .= $buttons;
! 1529:
! 1530: return $result;
! 1531: }
1.3 bowersj2 1532:
1.14 ! bowersj2 1533: sub postprocess {
! 1534: my $self = shift;
! 1535: my $wizard = $self->{WIZARD};
! 1536: my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
! 1537: if ($formvalue) {
! 1538: if ($self->{MULTICHOICE}) {
! 1539: $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
! 1540: $self->{VAR_NAME});
! 1541: }
! 1542: $wizard->changeState($self->{NEXT_STATE});
! 1543: } else {
! 1544: $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
! 1545: . ' a selection to continue.';
1.3 bowersj2 1546: }
1.14 ! bowersj2 1547: return 1;
1.3 bowersj2 1548: }
1.14 ! bowersj2 1549:
1.3 bowersj2 1550:
1551: 1;
1552:
1553: package Apache::lonwizard::choose_section;
1554:
1555: no strict;
1556: @ISA = ("Apache::lonwizard::choice_state");
1557: use strict;
1558:
1559: sub new {
1560: my $proto = shift;
1561: my $class = ref($proto) || $proto;
1562: my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
1563: shift, shift, shift);
1564: return $self;
1565: }
1566:
1567: sub determineChoices {
1568: my %choices;
1569:
1570: my $classlist = Apache::loncoursedata::get_classlist();
1571: foreach (keys %$classlist) {
1572: my $sectionName = $classlist->{$_}->[5];
1573: if (!$sectionName) {
1574: $choices{"No section assigned"} = "";
1575: } else {
1576: $choices{$sectionName} = $sectionName;
1577: }
1578: }
1579:
1580: return \%choices;
1581: }
1582:
1583: 1;
1.1 bowersj2 1584:
1.10 bowersj2 1585: package Apache::lonwizard::choose_files;
1586:
1587: =pod
1588:
1589: =head2 Class: choose_file
1590:
1591: 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.
1592:
1593: =over 4
1594:
1595: =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.
1596:
1597: =back
1598:
1599: =cut
1600:
1601: no strict;
1602: @ISA = ("Apache::lonwizard::state");
1603: use strict;
1604:
1605: sub new {
1606: my $proto = shift;
1607: my $class = ref($proto) || $proto;
1608: my $self = bless $proto->SUPER::new(shift, shift, shift);
1609:
1610: $self->{MESSAGE_BEFORE} = shift;
1611: $self->{MESSAGE_AFTER} = shift;
1612: $self->{NEXT_STATE} = shift;
1613: $self->{VAR_NAME} = shift;
1614: $self->{SUB_DIR} = shift;
1615: $self->{FILTER_FUNC} = shift;
1616:
1617: if (!defined($self->{FILTER_FUNC})) {
1618: $self->{FILTER_FUNC} = sub {return 1;};
1619: }
1620:
1621: return $self;
1622: }
1623:
1624: sub render {
1625: my $self = shift;
1626: my $result = '';
1627: my $var = $self->{VAR_NAME};
1628: my $subdir = $self->{SUB_DIR};
1629: my $filterFunc = $self->{FILTER_FUNC};
1630:
1631: $result = <<SCRIPT;
1632: <script>
1633: function checkall(value) {
1634: for (i=0; i<document.forms.wizform.elements.length; i++) {
1635: ele = document.forms.wizform.elements[i];
1636: if (ele.type == "checkbox") {
1637: document.forms.wizform.elements[i].checked=value;
1638: }
1639: }
1640: }
1641: </script>
1642: SCRIPT
1643:
1644: my $buttons = <<BUTTONS;
1645: <br />
1646: <input type="button" onclick="checkall(true)" value="Select All" />
1647: <input type="button" onclick="checkall(false)" value="Unselect All" />
1648: <br />
1649: BUTTONS
1650:
1651: if (defined $self->{ERROR_MSG}) {
1652: $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1653: }
1654:
1655: if ($self->{MESSAGE_BEFORE}) {
1656: $result .= $self->{MESSAGE_BEFORE} . '<br />';
1657: }
1658:
1659: # Get the list of files in this directory.
1660: my @fileList;
1661:
1662: # If the subdirectory is in local CSTR space
1663: if ($subdir =~ m|/home/([^/]+)/public_html|) {
1664: my $user = $1;
1665: my $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
1666: @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
1667: } else {
1668: # local library server resource space
1669: @fileList = &Apache::lonnet::dirlist($subdir, $ENV{'user.domain'}, $ENV{'user.name'}, '');
1670: }
1671:
1672: $result .= $buttons;
1673:
1674: $result .= '<table border="0" cellpadding="1" cellspacing="1">';
1675:
1676: # Keeps track if there are no choices, prints appropriate error
1677: # if there are none.
1678: my $choices = 0;
1679: # Print each legitimate file choice.
1680: for my $file (@fileList) {
1681: $file = (split(/&/, $file))[0];
1682: my $fileName = $subdir .'/'. $file;
1683: if (&$filterFunc($file)) {
1684: $result .= '<tr><td align="right">' .
1685: "<input type='checkbox' name='" . $self->{VAR_NAME}
1686: . ".forminput' value='" . HTML::Entities::encode($fileName) .
1687: "' /></td><td>" . $file . "</td></tr>\n";
1688: $choices++;
1689: }
1690: }
1691:
1692: $result .= "</table>\n";
1693:
1694: if (!$choices) {
1695: $result .= '<font color="#FF0000">There are no files available to select in this directory. Please go back and select another option.</font><br /><br />';
1696: }
1697:
1698: $result .= $buttons;
1699:
1700: if ($self->{MESSAGE_AFTER}) {
1701: $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
1702: }
1703:
1704: return $result;
1705: }
1706:
1707: sub postprocess {
1708: my $self = shift;
1709: print $self->{NEXT_STATE};
1710: my $wizard = $self->{WIZARD};
1711:
1.11 bowersj2 1712: $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
1713: $self->{VAR_NAME});
1714:
1715: if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
1.10 bowersj2 1716: $self->{ERROR_MSG} = "Can't continue the wizard because you ".
1717: "must make a selection to continue.";
1718: }
1719: return 1;
1720: }
1721:
1722: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>