Annotation of loncom/interface/lonwizard.pm, revision 1.13

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>