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

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;
                     27: 
                     28: =pod
                     29: 
                     30: =head1 Class: lonwizard
                     31: 
                     32: =head2 lonwizard Attributes
                     33: 
                     34: =over 4
                     35: 
                     36: =item B<STATE>: The string name of the current state.
                     37: 
                     38: =item B<TITLE>: The human-readable title of the wizard
                     39: 
                     40: =item B<STATES>: A hash mapping the string names of states to references to the actual states.
                     41: 
                     42: =item B<VARS>: Hash that maintains the persistent variable values.
                     43: 
                     44: =item B<HISTORY>: An array containing the names of the previous states. Used for "back" functionality.
                     45: 
                     46: =item B<DONE>: A boolean value, true if the wizard has completed.
                     47: 
                     48: =back
                     49: 
                     50: =cut
                     51: 
                     52: sub new {
                     53:     my $proto = shift;
                     54:     my $class = ref($proto) || $proto;
                     55:     my $self = {};
                     56: 
                     57:     # If there is a state from the previous form, use that. If there is no
                     58:     # state, use the start state parameter.
                     59:     if (defined $ENV{"form.CURRENT_STATE"})
                     60:     {
                     61: 	$self->{STATE} = $ENV{"form.CURRENT_STATE"};
                     62:     }
                     63:     else
                     64:     {
                     65: 	$self->{STATE} = "START";
                     66:     }
                     67: 
                     68:     # set up return URL: Return the user to the referer page, unless the
                     69:     # form has stored a value.
                     70:     if (defined $ENV{"form.RETURN_PAGE"})
                     71:     {
                     72: 	$self->{RETURN_PAGE} = $ENV{"form.RETURN_PAGE"};
                     73:     }
                     74:     else
                     75:     {
                     76: 	$self->{RETURN_PAGE} = $ENV{REFERER};
                     77:     }
                     78: 
                     79:     $self->{TITLE} = shift;
                     80:     $self->{STATES} = {};
                     81:     $self->{VARS} = {};
                     82:     $self->{HISTORY} = {};
                     83:     $self->{DONE} = 0;
                     84:     bless($self, $class);
                     85:     return $self;
                     86: }
                     87: 
                     88: =pod
                     89: 
                     90: =head2 lonwizard methods
                     91: 
                     92: =over 2
                     93: 
1.3       bowersj2   94: =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   95: 
1.3       bowersj2   96: =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. This is a bulk declaration.
1.1       bowersj2   97: 
                     98: =over 2
                     99: 
                    100: =item Note that these 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".
                    101: 
                    102: =back
                    103: 
                    104: =cut
                    105: 
1.7     ! bowersj2  106: # Sometimes the wizard writer will want to use the result of the previous
        !           107: # state to change the text of the next state. In order to do that, it
        !           108: # has to be done during the declaration of the states, or it won't be
        !           109: # available. Therefore, basic form processing must occur before the
        !           110: # actual display routine is called and the actual pre-process is called,
        !           111: # or it won't be available.
        !           112: # This also factors common code out of the preprocess calls.
1.1       bowersj2  113: sub declareVars {
                    114:     my $self = shift;
                    115:     my $varlist = shift;
                    116: 
                    117:     # for each string in the passed in list,
                    118:     foreach my $element ( @{$varlist} )
                    119:     {
                    120: 	# assign the var the default of ""
                    121: 	$self->{VARS}{$element} = "";
                    122: 
                    123: 	# if there's a form in the env, use that instead
                    124: 	my $envname = "form." . $element;
                    125: 	if (defined ($ENV{$envname}))
                    126: 	{
                    127: 	    $self->{VARS}->{$element} = $ENV{$envname};
                    128: 	}
1.7     ! bowersj2  129:         
        !           130:         # If there's an incoming form submission, use that
        !           131:         my $envname = "form." . $element . ".forminput";
        !           132:         if (defined ($ENV{$envname})) {
        !           133:             $self->{VARS}->{$element} = $ENV{$envname};
        !           134:         }
1.1       bowersj2  135:     }
                    136: }
                    137: 
                    138: # Private function; takes all of the declared vars and returns a string
                    139: # corresponding to the hidden input fields that will re-construct the 
                    140: # variables.
                    141: sub _saveVars {
                    142:     my $self = shift;
                    143:     my $result = "";
                    144:     foreach my $varname (keys %{$self->{VARS}})
                    145:     {
                    146: 	$result .= '<input type="hidden" name="' .
                    147: 	           HTML::Entities::encode($varname) . '" value="' .
                    148: 		   HTML::Entities::encode($self->{VARS}{$varname}) . 
                    149: 		   "\" />\n";
                    150:     }
                    151: 
                    152:     # also save state & return page
                    153:     $result .= '<input type="hidden" name="CURRENT_STATE" value="' .
                    154:                HTML::Entities::encode($self->{STATE}) . '" />' . "\n";
                    155:     $result .= '<input type="hidden" name="RETURN_PAGE" value="' .
                    156:                HTML::Entities::encode($self->{RETURN_PAGE}) . '" />' . "\n";
                    157: 
                    158:     return $result;
                    159: }
                    160: 
                    161: =pod
                    162: 
                    163: =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.
                    164: 
                    165: =cut
                    166: 
                    167: sub registerState {
                    168:     my $self = shift;
                    169:     my $state = shift;
                    170: 
                    171:     my $stateName = $state->name();
                    172:     $self->{STATES}{$stateName} = $state;
                    173: }
                    174: 
                    175: =pod
                    176: 
                    177: =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.
                    178: 
                    179: =cut
                    180: 
                    181: sub changeState {
                    182:     my $self = shift;
                    183:     $self->{STATE} = shift;
                    184: }
                    185: 
                    186: =pod
                    187: 
                    188: =item B<display>(): This is the main method that the handler using the wizard calls.
                    189: 
                    190: =cut
                    191: 
                    192: # Done in five phases
                    193: # 1: Do the post processing for the previous state.
                    194: # 2: Do the preprocessing for the current state.
                    195: # 3: Check to see if state changed, if so, postprocess current and move to next.
                    196: #    Repeat until state stays stable.
                    197: # 4: Render the current state to the screen as an HTML page.
                    198: sub display {
                    199:     my $self = shift;
                    200: 
                    201:     my $result = "";
                    202: 
                    203:     # Phase 1: Post processing for state of previous screen (which is actually
                    204:     # the current state), if it wasn't the beginning state.
                    205:     if ($self->{STATE} ne "START" || $ENV{"form.SUBMIT"} eq "Next ->")
                    206:     {
                    207: 	my $prevState = $self->{STATES}{$self->{STATE}};
                    208: 	$prevState->postprocess();
                    209:     }
                    210:     
                    211:     # Note, to handle errors in a state's input that a user must correct,
                    212:     # do not transition in the postprocess, and force the user to correct
                    213:     # the error.
                    214: 
                    215:     # Phase 2: Preprocess current state
                    216:     my $startState = $self->{STATE};
                    217:     my $state = $self->{STATES}{$startState};
1.3       bowersj2  218:     
                    219:     # Error checking
                    220:     if (!defined($state)) {
                    221:         $result .="Error! The state ". $startState ." is not defined.";
                    222:         return $result;
                    223:     }
1.1       bowersj2  224:     $state->preprocess();
                    225: 
                    226:     # Phase 3: While the current state is different from the previous state,
                    227:     # keep processing.
                    228:     while ( $startState ne $self->{STATE} )
                    229:     {
                    230: 	$startState = $self->{STATE};
                    231: 	$state = $self->{STATES}{$startState};
                    232: 	$state->preprocess();
                    233:     }
                    234: 
                    235:     # Phase 4: Display.
                    236:     my $stateTitle = $state->title();
1.3       bowersj2  237:     my $bodytag = &Apache::loncommon::bodytag("$self->{TITLE}",'','');
1.1       bowersj2  238: 
                    239:     $result .= <<HEADER;
                    240: <html>
                    241:     <head>
1.3       bowersj2  242:         <title>LON-CAPA Wizard: $self->{TITLE}</title>
1.1       bowersj2  243:     </head>
1.3       bowersj2  244:     $bodytag
                    245: HEADER
                    246:     if (!$state->overrideForm()) { $result.="<form method='GET'>"; }
                    247:     $result .= <<HEADER;
                    248:         <table border="0"><tr><td>
                    249:         <h2><i>$stateTitle</i></h2>
1.1       bowersj2  250: HEADER
                    251: 
1.3       bowersj2  252:     if (!$state->overrideForm()) {
                    253:         $result .= $self->_saveVars();
                    254:     }
1.1       bowersj2  255:     $result .= $state->render() . "<p>&nbsp;</p>";
                    256: 
1.3       bowersj2  257:     if (!$state->overrideForm()) {
                    258:         $result .= '<center>';
                    259:         if ($self->{STATE} ne $self->{START_STATE})
                    260:         {
                    261:             #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';
                    262:         }
                    263:         if ($self->{DONE})
                    264:         {
                    265:             my $returnPage = $self->{RETURN_PAGE};
                    266:             $result .= "<a href=\"$returnPage\">End Wizard</a>";
                    267:         }
                    268:         else
                    269:         {
1.4       bowersj2  270:             $result .= '<input name="back" type="button" ';
                    271:             $result .= 'value="&lt;- Previous" onclick="history.go(-1)" /> ';
1.3       bowersj2  272:             $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" />';
                    273:         }
                    274:         $result .= "</center>\n";
1.1       bowersj2  275:     }
                    276: 
                    277:     $result .= <<FOOTER;
1.3       bowersj2  278:               </td>
                    279:             </tr>
                    280:           </table>
1.1       bowersj2  281:         </form>
                    282:     </body>
                    283: </html>
                    284: FOOTER
1.3       bowersj2  285: 
                    286:     return $result;
1.1       bowersj2  287: }
                    288: 
                    289: =pod
                    290: 
                    291: =item B<name>([name]): Returns the name of the wizard. If a parameter is passed, that will be saved as the name.
                    292: 
                    293: =cut
                    294: 
                    295: # Returns/sets the name of this wizard, i.e., "Assignment Parameter"
                    296: sub title {
                    297:     my $self = shift;
                    298:     if (@_) { $self->{TITLE} = shift};
                    299:     return $self->{TITLE};
                    300: }
                    301: 
                    302: =pod
                    303: 
                    304: =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.
                    305: 
                    306: =cut
                    307: 
                    308: sub getVars {
                    309:     my $self = shift;
                    310:     return ($self->{VARS});
                    311: }
                    312: 
                    313: =pod
                    314: 
                    315: =item B<setVar>(key, val): Sets the var named "key" to "val" in the wizard's form array.
                    316: 
                    317: =cut
                    318: 
1.3       bowersj2  319: # This may look trivial, but it's here as a hook for possible later processing
1.1       bowersj2  320: sub setVar {
                    321:     my $self = shift;
                    322:     my $key = shift;
                    323:     my $val = shift;
                    324:     $self->{VARS}{$key} = $val;
                    325: }
                    326: 
                    327: =pod
                    328: 
1.4       bowersj2  329: =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.)
                    330: 
                    331: =cut
                    332: 
                    333: sub queryStringVars {
                    334:     my $self = shift;
                    335: 
                    336:     my @queryString = ();
                    337:     
                    338:     for my $varname (keys %{$self->{VARS}}) {
                    339:         push @queryString, Apache::lonnet::escape($varname) . "=" .
                    340:             Apache::lonnet::escape($self->{VARS}{$varname});
                    341:     }
                    342:     push @queryString, 'CURRENT_STATE=' . Apache::lonnet::escape($self->{STATE});
                    343:     push @queryString, 'RETURN_PAGE=' . Apache::lonnet::escape($self->{RETURN_PAGE});
                    344: 
                    345:     return join '&', @queryString;
                    346: }
                    347: 
                    348: =pod
                    349: 
1.1       bowersj2  350: =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.
                    351: 
                    352: =cut
                    353: 
                    354: 
                    355: # A temp function for debugging
                    356: sub handler {
                    357:     my $r = shift;
                    358: 
                    359:     Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                    360: 
1.3       bowersj2  361:     if ($r->header_only) {
                    362:         if ($ENV{'browser.mathml'}) {
                    363:             $r->content_type('text/xml');
                    364:         } else {
                    365:             $r->content_type('text/html');
                    366:         }
                    367:         $r->send_http_header;
                    368:         return OK;
                    369:     }
                    370: 
                    371:     # Send header, don't cache this page
                    372:     if ($ENV{'browser.mathml'}) {
                    373:         $r->content_type('text/xml');
                    374:     } else {
                    375:         $r->content_type('text/html');
                    376:     }
                    377:     &Apache::loncommon::no_cache($r);
                    378:     $r->send_http_header;
                    379:     $r->rflush();
                    380: 
                    381:     my $mes = <<WIZBEGIN;
1.7     ! bowersj2  382: <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  383: 
1.7     ! bowersj2  384: <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  385: 
1.7     ! bowersj2  386: <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  387: WIZBEGIN
1.1       bowersj2  388:     
1.3       bowersj2  389:     my $wizard = Apache::lonwizard->new("Course Parameter Wizard");
                    390:     $wizard->declareVars(['ACTION_TYPE', 'GRANULARITY', 'TARGETS', 'PARM_DATE', 'RESOURCE_ID', 'USER_NAME', 'SECTION_NAME']);
1.7     ! bowersj2  391:     my %dateTypeHash = ('open_date' => "opening date",
        !           392:                         'due_date' => "due date",
        !           393:                         'answer_date' => "answer date");
        !           394:     my %levelTypeHash = ('whole_course' => "all problems in the course",
        !           395:                          'map' => 'the selected folder',
        !           396:                          'resource' => 'the selected problem');
1.3       bowersj2  397:     
1.7     ! bowersj2  398:     Apache::lonwizard::message_state->new($wizard, "START", "Welcome to the Assignment Parameter Wizard", $mes, "CHOOSE_LEVEL");
        !           399:     Apache::lonwizard::switch_state->new($wizard, "CHOOSE_LEVEL", "Which Problem or Problems?", "GRANULARITY", [
        !           400:        ["whole_course", "<b>Every problem</b> in the course", "CHOOSE_ACTION"],
        !           401:        ["map", "Every problem in a particular <b>folder</b>", "CHOOSE_FOLDER"],
        !           402:        ["resource", "One particular <b>problem</b>", "CHOOSE_RESOURCE"]],
        !           403:                                          "Which problems do you wish to change a date for?");
        !           404:     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();});
        !           405:     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(); });
        !           406:     my $levelType = $levelTypeHash{$wizard->{VARS}->{GRANULARITY}};
        !           407:     Apache::lonwizard::switch_state->new($wizard, "CHOOSE_ACTION", "Parameter Type", "ACTION_TYPE", [ 
        !           408:        ["open_date", "Set an <b>open date</b>", "CHOOSE_DATE"], 
        !           409:        ["due_date", "Set a <b>due date</b>", "CHOOSE_DATE"],
        !           410:        ["answer_date", "Set an <b>answer open date</b>", "CHOOSE_DATE" ] ],
        !           411:        "What parameters do you want to set for $levelType?");
        !           412:     my $dateType = $dateTypeHash{$wizard->{VARS}->{ACTION_TYPE}};
        !           413:     Apache::lonwizard::date_state->new($wizard, "CHOOSE_DATE", "Set Date", "PARM_DATE", "CHOOSE_STUDENT_LEVEL", "What should the $dateType be set to?");
        !           414:     Apache::lonwizard::switch_state->new($wizard, "CHOOSE_STUDENT_LEVEL", "Students Affected", "TARGETS", [
        !           415:        ["course", ". . . for <b>all students</b> in the course", "FINISH"],
        !           416:        ["section", ". . . for a particular <b>section</b>", "CHOOSE_SECTION"],
        !           417:        ["student", ". . . for an individual <b>student</b>", "CHOOSE_STUDENT"]],
        !           418:                                        "Set $dateType of $levelType for. . .");
1.3       bowersj2  419: 
1.7     ! bowersj2  420:     Apache::lonwizard::choose_section->new($wizard, "CHOOSE_SECTION", "Select Section", "Please select the section you wish to set the $dateType for:", "", "FINISH", "SECTION_NAME");
        !           421:     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  422:     Apache::lonwizard::parmwizfinal->new($wizard, "FINISH", "Confirm Selection");
                    423: 
1.1       bowersj2  424:     $r->print($wizard->display());
                    425: 
                    426:     return OK;
                    427: }
                    428: 
                    429: 
                    430: 
                    431: 1;
                    432: 
                    433: =head1 Class: lonwizstate
                    434: 
                    435: 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.
                    436: 
                    437: 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.
                    438: 
1.3       bowersj2  439: 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.
                    440: 
                    441: 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.)
                    442: 
1.7     ! bowersj2  443: 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.)
        !           444: 
1.1       bowersj2  445: =head2 lonwizstate methods
                    446: 
                    447: These methods should be overridden in derived states, except B<new> which may be sufficient.
                    448: 
                    449: =over 2
                    450: 
                    451: =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.
                    452: 
                    453: =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.
                    454: 
                    455: =over 2
                    456: 
                    457: =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.
                    458: 
                    459: =back
                    460: 
                    461: =item B<render>(): render returns a string of itself to be rendered to the screen, which the wizard will display.
                    462: 
                    463: =back
                    464: 
                    465: =cut 
                    466: 
                    467: package Apache::lonwizard::state;
                    468: 
                    469: use strict;
                    470: 
                    471: sub new {
                    472:     my $proto = shift;
                    473:     my $class = ref($proto) || $proto;
                    474:     my $self = {};
                    475:     $self->{WIZARD} = shift;
                    476:     $self->{NAME} = shift;
                    477:     $self->{TITLE} = shift;
                    478: 
                    479:     bless($self);
                    480: 
                    481:     $self->{WIZARD}->registerState($self);
                    482:     return $self;
                    483: }
                    484: 
                    485: sub name {
                    486:     my $self = shift;
                    487:     if (@_) { $self->{NAME} = shift};
                    488:     return $self->{NAME};
                    489: }
                    490: 
                    491: sub title {
                    492:     my $self = shift;
                    493:     if (@_) { $self->{TITLE} = shift};
                    494:     return $self->{TITLE};
                    495: }
                    496: 
                    497: sub preprocess {
                    498:     return 1;
                    499: }
                    500: 
                    501: sub render {
                    502:     return "This is the empty state. If you can see this, it's a bug.\n"
                    503: }
                    504: 
                    505: sub postprocess {
                    506:     return 1;
                    507: }
                    508: 
1.3       bowersj2  509: # If this is 1, the wizard assumes the state will override the 
                    510: # wizard's form, useful for some final states
                    511: sub overrideForm {
                    512:     return 0;
                    513: }
                    514: 
1.1       bowersj2  515: 1;
                    516: 
                    517: =pod
                    518: 
                    519: =back
                    520: 
                    521: =head1 Prepackaged States
                    522: 
                    523: lonwizard provides several pre-packaged states that you can drop into your Wizard and obtain common functionality.
                    524: 
                    525: =head2 Class: message_state
                    526: 
                    527: 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.
                    528: 
                    529: =over 4
                    530: 
1.3       bowersj2  531: =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  532: 
                    533: =back
                    534: 
                    535: =cut
                    536: 
                    537: package Apache::lonwizard::message_state;
                    538: 
                    539: no strict;
                    540: @ISA = ("Apache::lonwizard::state");
                    541: use strict;
                    542: 
                    543: sub new {
                    544:     my $proto = shift;
                    545:     my $class = ref($proto) || $proto;
                    546: 
1.3       bowersj2  547:     # This cute looking statement correctly handles subclassing
1.1       bowersj2  548:     my $self = bless $proto->SUPER::new(shift, shift, shift);
                    549: 
                    550:     $self->{MESSAGE} = shift;
                    551:     $self->{NEXT_STATE} = shift;
                    552: 
                    553:     return $self;
                    554: }
                    555: 
                    556: sub postprocess {
                    557:     my $self = shift;
                    558:     $self->{WIZARD}->changeState($self->{NEXT_STATE});
                    559:     return 1;
                    560: }
                    561: 
                    562: sub render {
                    563:     my $self = shift;
                    564:     return $self->{MESSAGE};
                    565: }
                    566: 
                    567: 1;
                    568: 
                    569: package Apache::lonwizard::choice_state;
                    570: 
                    571: no strict;
                    572: @ISA = ("Apache::lonwizard::state");
                    573: use strict;
                    574: 
                    575: =pod
                    576: 
                    577: =head2 Class: choice_state
                    578: 
                    579: 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.
                    580: 
                    581: If there is only one choice, the state will automatically make it and go to the next state.
                    582: 
                    583: =over 4
                    584: 
                    585: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, choiceHash): messageBefore is the HTML text that will be displayed before the choice display, messageAfter will display after. Keys will be sorted according to human name. nextState is the state to proceed to after the choice. varName is the name of the wizard var to store the computer_name answer in. choiceHash is the hash described above. It is optional because you may override it.
                    586: 
1.3       bowersj2  587: =back
                    588: 
1.1       bowersj2  589: =cut
                    590: 
1.3       bowersj2  591: sub new {
                    592:     my $proto = shift;
                    593:     my $class = ref($proto) || $proto;
                    594:     my $self = bless $proto->SUPER::new(shift, shift, shift);
                    595: 
                    596:     $self->{MESSAGE_BEFORE} = shift;
                    597:     $self->{MESSAGE_AFTER} = shift;
                    598:     $self->{NEXT_STATE} = shift;
                    599:     $self->{VAR_NAME} = shift;
                    600:     $self->{CHOICE_HASH} = shift;
                    601:     $self->{NO_CHOICES} = 0;
                    602:     
                    603:     return $self;
                    604: }
                    605: 
1.1       bowersj2  606: sub preprocess {
                    607:     my $self = shift;
1.3       bowersj2  608:     my $choices = $self->{CHOICE_HASH};
                    609:     if (!defined($self->{CHOICE_HASH})) {
                    610:         $choices = $self->{CHOICE_HASH} = $self->determineChoices();
                    611:     }
                    612:     my $wizvars = $self->{WIZARD}->getVars();
1.1       bowersj2  613: 
1.3       bowersj2  614:     my @keys = keys(%$choices);
1.1       bowersj2  615:     @keys = sort @keys;
                    616: 
                    617:     if (scalar(@keys) == 0)
                    618:     {
                    619: 	# No choices... so prepare to display error message and cancel further execution.
                    620: 	$self->{NO_CHOICES} = 1;
1.3       bowersj2  621: 	$self->{WIZARD}->{DONE} = 1;
1.1       bowersj2  622: 	return;
                    623:     }
                    624:     if (scalar(@keys) == 1)
                    625:     {
                    626: 	# If there is only one choice, pick it and move on.
1.3       bowersj2  627: 	$wizvars->{$self->{VAR_NAME}} = $choices->{$keys[0]};
1.1       bowersj2  628: 	$self->{WIZARD}->changeState($self->{NEXT_STATE});
                    629: 	return;
                    630:     }
                    631: 
                    632:     # Otherwise, do normal processing in the render routine.
                    633: 
                    634:     return;
                    635: }
                    636: 
                    637: sub determineChoices {
                    638:     return {"NO_CHOICE" => "No choices were given."};
                    639: }
                    640: 
                    641: sub render { 
                    642:     my $self = shift;
                    643:     my $result = "";
                    644:     my $var = $self->{VAR_NAME};
                    645: 
1.3       bowersj2  646:     if (defined $self->{ERROR_MSG}) {
                    647:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                    648:     }
                    649: 
                    650:     if (defined $self->{MESSAGE_BEFORE})
1.1       bowersj2  651:     {
1.3       bowersj2  652: 	$result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
1.1       bowersj2  653:     }
                    654: 
1.3       bowersj2  655:     my $choices = $self->{CHOICE_HASH};
                    656:     my @keys = keys (%$choices);
                    657: 
                    658:     $result .= "<select name=\"$var.forminput\" size=\"10\">\n";
                    659:     foreach (@keys)
                    660:     {
                    661: 	$result .= "<option value=\"" . HTML::Entities::encode($choices->{$_}) 
                    662:             . "\">" . HTML::Entities::encode($_) . "\n";
                    663:     }
                    664:     $result .= "</select>\n\n";
1.1       bowersj2  665: 
1.3       bowersj2  666:     if (defined $self->{MESSAGE_AFTER})
                    667:     {
                    668: 	$result .= '<br /><br />' . $self->{MESSAGE_AFTER};
                    669:     }
                    670: 
                    671:     return $result;
                    672: }
                    673: 
                    674: sub postprocess {
                    675:     my $self = shift;
                    676:     my $wizard = $self->{WIZARD};
                    677:     my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
                    678:     if ($formvalue) {
1.7     ! bowersj2  679:         # Value already stored by Wizard
1.3       bowersj2  680:         $wizard->changeState($self->{NEXT_STATE});
                    681:     } else {
                    682:         $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
                    683:             . ' a selection to continue.';
                    684:     }
                    685:     return 1;
                    686: }
                    687: 
                    688: package Apache::lonwizard::switch_state;
                    689: 
                    690: no strict;
                    691: @ISA = ("Apache::lonwizard::state");
                    692: use strict;
                    693: 
                    694: =pod
1.1       bowersj2  695: 
1.3       bowersj2  696: =head2 Class; switch_state
                    697: 
                    698: 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.
                    699: 
                    700: Each choice may have arbitrary HTML associated with it, which will be used as the label. The first choice will be selected by default.
                    701: 
                    702: =over 4
                    703: 
                    704: =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, varName, choiceList, messageBefore, messageAfter): varName is the name of the wizard variable the state will set with the choice made. choiceHash is list reference of a list of list references to three element lists, where the first element is what the wizard var varName will be set to, the second is the HTML that will be displayed for that choice, and the third is the destination state. messageBefore is an optional HTML string that will be placed before the message, messageAfter an optional HTML string that will be placed before.
                    705: 
                    706: An example of a legit choiceList: C<my $choicelist = [ ["flunk", "Flunk Student", "FLUNK_STATE"], ["pass", "Pass Student", "PASS_STATE"] ];>
                    707: 
                    708: =back
                    709: 
                    710: =cut
                    711: 
                    712: sub new {
                    713:     my $proto = shift;
                    714:     my $class = ref($proto) || $proto;
                    715:     my $self = bless $proto->SUPER::new(shift, shift, shift);
                    716: 
                    717:     $self->{VAR_NAME} = shift;
                    718:     $self->{CHOICE_LIST} = shift;
                    719:     $self->{MESSAGE_BEFORE} = shift;
                    720:     $self->{MESSAGE_AFTER} = shift;
                    721: 
                    722:     return $self;
                    723: }
                    724: 
                    725: # Don't need a preprocess step; we assume we know the choices
                    726: 
                    727: sub render {
                    728:     my $self = shift;
                    729:     my $result = "";
                    730:     my $var = $self->{VAR_NAME};
                    731:     my @choices = @{$self->{CHOICE_LIST}};
                    732:     my $curVal = $self->{WIZARD}->{VARS}->{$var};
                    733: 
                    734:     $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});
                    735: 
                    736:     if (!$curVal) {
                    737:         $curVal = $self->{CHOICE_LIST}->[0]->[0]; # top is default
                    738:     }
                    739: 
                    740:     $result .= "<table>\n\n";
                    741: 
                    742:     foreach my $choice (@choices)
                    743:     {
                    744: 	my $value = $choice->[0];
                    745: 	my $text = $choice->[1];
                    746:     
                    747: 	$result .= "<tr>\n<td width='20'>&nbsp;</td>\n<td>";
                    748: 	$result .= "<td valign=\"top\"><input type=\"radio\" name=\"$var.forminput\"";
                    749: 	$result .= " checked" if ($value eq $curVal);
                    750: 	$result .= " value=\"$value\"></td>\n<td>$text</td>\n</tr>\n\n";
                    751:     }
                    752: 
                    753:     $result .= "<table>\n\n";
                    754: 
                    755:     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
                    756: 
                    757:     return $result;
                    758: }
                    759: 
                    760: sub postprocess {
1.7     ! bowersj2  761:     # Value already stored by wizard
1.3       bowersj2  762:     my $self = shift;
                    763:     my $wizard = $self->{WIZARD};
                    764:     my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . '.forminput'};
                    765: 
                    766:     foreach my $choice (@{$self->{CHOICE_LIST}})
                    767:     {
                    768: 	if ($choice->[0] eq $chosenValue)
                    769: 	{
                    770: 	    $wizard->changeState($choice->[2]);
                    771: 	}
                    772:     }
                    773: }
                    774: 
                    775: # If there is only one choice, make it and move on
                    776: sub preprocess {
                    777:     my $self = shift;
                    778:     my $choiceList = $self->{CHOICE_LIST};
                    779:     my $wizard = $self->{WIZARD};
                    780:     
                    781:     if (scalar(@{$choiceList}) == 1)
                    782:     {
                    783: 	my $choice = $choiceList->[0];
                    784: 	my $chosenVal = $choice->[0];
                    785: 	my $nextState = $choice->[2];
                    786: 
                    787: 	$wizard->setVar($self->{VAR_NAME}, $chosenVal)
                    788: 	    if (defined ($self->{VAR_NAME}));
                    789: 	$wizard->changeState($nextState);
                    790:     }
                    791: }
                    792: 
                    793: 1;
                    794: 
                    795: package Apache::lonwizard::date_state;
                    796: 
                    797: use Time::localtime;
                    798: use Time::Local;
                    799: use Time::tm;
                    800: 
                    801: no strict;
                    802: @ISA = ("Apache::lonwizard::state");
                    803: use strict;
                    804: 
                    805: my @months = ("January", "February", "March", "April", "May", "June", "July",
                    806: 	      "August", "September", "October", "November", "December");
                    807: 
                    808: =pod
                    809: 
                    810: =head2 Class: date_state
                    811: 
                    812: 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.
                    813: 
                    814: =over 4
                    815: 
                    816: =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.
                    817: 
                    818: =back
                    819: 
                    820: =cut
                    821: 
                    822: sub new {
                    823:     my $proto = shift;
                    824:     my $class = ref($proto) || $proto;
                    825:     my $self = bless $proto->SUPER::new(shift, shift, shift);
                    826: 
                    827:     $self->{VAR_NAME} = shift;
                    828:     $self->{NEXT_STATE} = shift;
                    829:     $self->{MESSAGE_BEFORE} = shift;
                    830:     $self->{MESSAGE_AFTER} = shift;
                    831:     $self->{DISPLAY_JUST_DATE} = shift;
                    832:     if (!defined($self->{DISPLAY_JUST_DATE})) {$self->{DISPLAY_JUST_DATE} = 0;}
                    833:     return $self;
                    834: }
                    835: 
                    836: sub render {
                    837:     my $self = shift;
                    838:     my $result = "";
                    839:     my $var = $self->{VAR_NAME};
                    840:     my $name = $self->{NAME};
                    841:     my $wizvars = $self->{WIZARD}->getVars();
                    842: 
                    843:     my $date;
                    844:     
                    845:     # Pick default date: Now, or previous choice
                    846:     if (defined ($wizvars->{$var}) && $wizvars->{$var} ne "")
                    847:     { 
                    848: 	$date = localtime($wizvars->{$var});
                    849:     }
                    850:     else
1.1       bowersj2  851:     {
1.3       bowersj2  852: 	$date = localtime();
                    853:     }
                    854: 
                    855:     if (defined $self->{ERROR_MSG}) {
                    856:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                    857:     }
                    858: 
                    859:     if (defined ($self->{MESSAGE_BEFORE})) {
                    860:         $result .= $self->{MESSAGE_BEFORE};
                    861:         $result .= "<br /><br />\n\n";
                    862:     }
                    863: 
                    864:     # Month
                    865:     my $i;
                    866:     $result .= "<select name='$self->{VAR_NAME}month'>\n";
                    867:     for ($i = 0; $i < 12; $i++) {
                    868:         if ($i == $date->mon) {
                    869:             $result .= "<option value='$i' selected>";
                    870:         } else {
                    871:             $result .= "<option value='$i'>";
                    872:         }
1.5       albertel  873:         $result .= $months[$i] . "\n";
1.3       bowersj2  874:     }
                    875:     $result .= "</select>\n";
                    876: 
                    877:     # Day
                    878:     $result .= "<select name='$self->{VAR_NAME}day'>\n";
                    879:     for ($i = 1; $i < 32; $i++) {
                    880:         if ($i == $date->mday) {
                    881:             $result .= '<option selected>';
                    882:         } else {
                    883:             $result .= '<option>';
                    884:         }
                    885:         $result .= "$i\n";
                    886:     }
                    887:     $result .= "</select>,\n";
                    888: 
                    889:     # Year
                    890:     $result .= "<select name='$self->{VAR_NAME}year'>\n";
                    891:     for ($i = 2000; $i < 2030; $i++) { # update this after 64-bit dates
                    892:         if ($date->year + 1900 == $i) {
                    893:             $result .= "<option selected>";
                    894:         } else {
                    895:             $result .= "<option>";
                    896:         }
                    897:         $result .= "$i\n";
                    898:     }
                    899:     $result .= "</select>,\n";
                    900: 
                    901:     # Display Hours and Minutes if they are called for
                    902:     if (!$self->{DISPLAY_JUST_DATE}) {
                    903:         $result .= "<select name='$self->{VAR_NAME}hour'>\n";
                    904:         if ($date->hour == 12) { $result .= "<option selected>12\n"; }
                    905:         else { $result .= "<option>12\n" }
                    906:         for ($i = 1; $i < 12; $i++) {
                    907:             if (($date->hour) % 12 == $i % 12) {
                    908:                 $result .= "<option selected>";
                    909:             } else {
                    910:                 $result .= "<option>";
                    911:             }
                    912:             $result .= "$i\n";
                    913:         }
                    914:         $result .= "</select> :\n";
                    915: 
                    916:         $result .= "<select name='$self->{VAR_NAME}minute'>\n";
                    917:         for ($i = 0; $i < 60; $i++) {
                    918:             if ($date->min == $i) {
                    919:                 $result .= "<option selected>";
                    920:             } else {
                    921:                 $result .= "<option>";
                    922:             }
                    923:             $result .= "$i\n";
                    924:         }
                    925:         $result .= "</select>\n";
                    926: 
                    927:         $result .= "<select name='$self->{VAR_NAME}meridian'>\n";
                    928:         if ($date->hour < 12) {
                    929:             $result .= "<option selected>A.M.\n<option>P.M.\n";
                    930:         } else {
                    931:             $result .= "<option>A.M.\n<option selected>P.M.\n";
                    932:         }
                    933:         $result .= "</select>";
                    934:     }
                    935: 
                    936:     if (defined ($self->{MESSAGE_AFTER})) {
                    937:         $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
1.1       bowersj2  938:     }
                    939: 
                    940:     return $result;
                    941: }
                    942: 
1.3       bowersj2  943: # Stick the date stored into the chosen variable.
1.1       bowersj2  944: sub postprocess {
                    945:     my $self = shift;
                    946:     my $wizard = $self->{WIZARD};
1.3       bowersj2  947: 
                    948:     my $month = $ENV{'form.' . $self->{VAR_NAME} . 'month'}; 
                    949:     my $day = $ENV{'form.' . $self->{VAR_NAME} . 'day'}; 
                    950:     my $year = $ENV{'form.' . $self->{VAR_NAME} . 'year'}; 
                    951:     my $min = 0; 
                    952:     my $hour = 0;
                    953:     if (!$self->{DISPLAY_JUST_DATE}) {
                    954:         $min = $ENV{'form.' . $self->{VAR_NAME} . 'minute'};
                    955:         $hour = $ENV{'form.' . $self->{VAR_NAME} . 'hour'};
                    956:     }
                    957: 
                    958:     my $chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);
                    959:     # Check to make sure that the date was not automatically co-erced into a 
                    960:     # valid date, as we want to flag that as an error
                    961:     # This happens for "Feb. 31", for instance, which is coerced to March 2 or
                    962:     # 3, depending on if it's a leapyear
                    963:     my $checkDate = localtime($chosenDate);
                    964: 
                    965:     if ($checkDate->mon != $month || $checkDate->mday != $day ||
                    966:         $checkDate->year + 1900 != $year) {
                    967:         $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
                    968:             . "date because it doesn't exist. Please enter a valid date.";
                    969:         return;
                    970:     }
                    971: 
                    972:     $wizard->setVar($self->{VAR_NAME}, $chosenDate);
                    973: 
1.1       bowersj2  974:     $wizard->changeState($self->{NEXT_STATE});
1.3       bowersj2  975: }
                    976: 
                    977: 1;
                    978: 
                    979: package Apache::lonwizard::parmwizfinal;
                    980: 
                    981: # This is the final state for the parmwizard. It is not generally useful,
                    982: # so it is not perldoc'ed. It does it's own processing.
                    983: 
                    984: no strict;
                    985: @ISA = ('Apache::lonwizard::state');
                    986: use strict;
                    987: 
                    988: use Time::localtime;
                    989: 
                    990: sub new {
                    991:     my $proto = shift;
                    992:     my $class = ref($proto) || $proto;
                    993:     my $self = bless $proto->SUPER::new(shift, shift, shift);
                    994: 
                    995:     # No other variables because it gets it all from the wizard.
                    996: }
                    997: 
                    998: # Renders a form that, when submitted, will form the input to lonparmset.pm
                    999: sub render {
                   1000:     my $self = shift;
                   1001:     my $wizard = $self->{WIZARD};
                   1002:     my $wizvars = $wizard->{VARS};
                   1003: 
                   1004:     # FIXME: Unify my designators with the standard ones
                   1005:     my %dateTypeHash = ('open_date' => "Opening Date",
                   1006:                         'due_date' => "Due Date",
                   1007:                         'answer_date' => "Answer Date");
                   1008:     my %parmTypeHash = ('open_date' => "0_opendate",
                   1009:                         'due_date' => "0_duedate",
                   1010:                         'answer_date' => "0_answerdate");
                   1011:     
                   1012:     my $result = "<form method='get' action='/adm/parmset'>\n";
                   1013:     $result .= '<p>Confirm that this information is correct, then click &quot;Finish Wizard&quot; to complete setting the parameter.<ul>';
                   1014:     my $affectedResourceId = "";
                   1015:     my $parm_name = $parmTypeHash{$wizvars->{ACTION_TYPE}};
                   1016:     my $level = "";
                   1017:     
                   1018:     # Print the type of manipulation:
                   1019:     $result .= '<li>Setting the <b>' . $dateTypeHash{$wizvars->{ACTION_TYPE}}
                   1020:                . "</b></li>\n";
                   1021:     if ($wizvars->{ACTION_TYPE} eq 'due_date' || 
                   1022:         $wizvars->{ACTION_TYPE} eq 'answer_date') {
                   1023:         # for due dates, we default to "date end" type entries
                   1024:         $result .= "<input type='hidden' name='recent_date_end' " .
                   1025:             "value='" . $wizvars->{PARM_DATE} . "' />\n";
                   1026:         $result .= "<input type='hidden' name='pres_value' " . 
                   1027:             "value='" . $wizvars->{PARM_DATE} . "' />\n";
                   1028:         $result .= "<input type='hidden' name='pres_type' " .
                   1029:             "value='date_end' />\n";
                   1030:     } elsif ($wizvars->{ACTION_TYPE} eq 'open_date') {
                   1031:         $result .= "<input type='hidden' name='recent_date_start' ".
                   1032:             "value='" . $wizvars->{PARM_DATE} . "' />\n";
                   1033:         $result .= "<input type='hidden' name='pres_value' " .
                   1034:             "value='" . $wizvars->{PARM_DATE} . "' />\n";
                   1035:         $result .= "<input type='hidden' name='pres_type' " .
                   1036:             "value='date_start' />\n";
                   1037:     } 
                   1038:     
                   1039:     # Print the granularity, depending on the action
                   1040:     if ($wizvars->{GRANULARITY} eq 'whole_course') {
                   1041:         $result .= '<li>for <b>all resources in the course</b></li>';
                   1042:         $level = 9; # general course, see lonparmset.pm perldoc
                   1043:         $affectedResourceId = "0.0";
                   1044:     } elsif ($wizvars->{GRANULARITY} eq 'map') {
                   1045:         my $navmap = Apache::lonnavmaps::navmap->new(
                   1046:                            $ENV{"request.course.fn"}.".db",
                   1047:                            $ENV{"request.course.fn"}."_parms.db", 0, 0);
                   1048:         my $res = $navmap->getById($wizvars->{RESOURCE_ID});
                   1049:         my $title = $res->compTitle();
                   1050:         $navmap->untieHashes();
                   1051:         $result .= "<li>for the map named <b>$title</b></li>";
                   1052:         $level = 8;
                   1053:         $affectedResourceId = $wizvars->{RESOURCE_ID};
                   1054:     } else {
                   1055:         my $navmap = Apache::lonnavmaps::navmap->new(
                   1056:                            $ENV{"request.course.fn"}.".db",
                   1057:                            $ENV{"request.course.fn"}."_parms.db", 0, 0);
                   1058:         my $res = $navmap->getById($wizvars->{RESOURCE_ID});
                   1059:         my $title = $res->compTitle();
                   1060:         $navmap->untieHashes();
                   1061:         $result .= "<li>for the resource named <b>$title</b></li>";
                   1062:         $level = 7;
                   1063:         $affectedResourceId = $wizvars->{RESOURCE_ID};
                   1064:     }
                   1065: 
                   1066:     # Print targets
                   1067:     if ($wizvars->{TARGETS} eq 'course') {
                   1068:         $result .= '<li>for <b>all students in course</b></li>';
                   1069:     } elsif ($wizvars->{TARGETS} eq 'section') {
                   1070:         my $section = $wizvars->{SECTION_NAME};
                   1071:         $result .= "<li>for section <b>$section</b></li>";
                   1072:         $level -= 3;
                   1073:         $result .= "<input type='hidden' name='csec' value='" .
                   1074:             HTML::Entities::encode($section) . "' />\n";
                   1075:     } else {
                   1076:         # FIXME: This is probably wasteful! 
                   1077:         my $classlist = Apache::loncoursedata::get_classlist();
                   1078:         my $name = $classlist->{$wizvars->{USER_NAME}}->[6];
                   1079:         $result .= "<li>for <b>$name</b></li>";
                   1080:         $level -= 6;
                   1081:         my ($uname, $udom) = split /:/, $wizvars->{USER_NAME};
                   1082:         $result .= "<input type='hidden' name='uname' value='".
                   1083:             HTML::Entities::encode($uname) . "' />\n";
                   1084:         $result .= "<input type='hidden' name='udom' value='".
                   1085:             HTML::Entities::encode($udom) . "' />\n";
                   1086:     }
                   1087: 
                   1088:     # Print value
                   1089:     $result .= "<li>to <b>" . ctime($wizvars->{PARM_DATE}) . "</b> (" .
                   1090:         Apache::lonnavmaps::timeToHumanString($wizvars->{PARM_DATE}) 
                   1091:         . ")</li>\n";
                   1092: 
                   1093:     # print pres_marker
                   1094:     $result .= "\n<input type='hidden' name='pres_marker'" .
                   1095:         " value='$affectedResourceId&$parm_name&$level' />\n";
                   1096: 
                   1097:     $result .= "<br /><br /><center><input type='submit' value='Finish Wizard' /></center></form>\n";
                   1098: 
                   1099:     return $result;
                   1100: }
                   1101:     
                   1102: sub overrideForm {
1.1       bowersj2 1103:     return 1;
                   1104: }
                   1105: 
1.3       bowersj2 1106: 1;
                   1107: 
                   1108: package Apache::lonwizard::resource_choice;
                   1109: 
                   1110: =pod 
                   1111: 
                   1112: =head2 Class: resource_choice
                   1113: 
                   1114: folder_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.
                   1115: 
                   1116: Note this state will not automatically advance if there is only one choice, because it might confuse the user in this case.
                   1117: 
                   1118: =over 4
                   1119: 
1.4       bowersj2 1120: =item overriddent method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, filterFunction, choiceFunction, multichoice): 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 1121: 
                   1122: 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.
                   1123: 
1.4       bowersj2 1124: multichoice specifies whether the state should provide radio buttons, allowing the user one choice, or checkboxes, allowing the user multiple choices, and automatically including some convenience buttons the user can choose (like "Check All" and "Uncheck All"), implemented with Javascript. Defaults to false, allow just one choice.
                   1125: 
1.3       bowersj2 1126: =back
                   1127: 
                   1128: =cut
1.1       bowersj2 1129: 
                   1130: no strict;
                   1131: @ISA = ("Apache::lonwizard::state");
                   1132: use strict;
1.3       bowersj2 1133: 
                   1134: sub new { 
                   1135:     my $proto = shift;
                   1136:     my $class = ref($proto) || $proto;
                   1137:     my $self = bless $proto->SUPER::new(shift, shift, shift);
                   1138: 
                   1139:     $self->{MESSAGE_BEFORE} = shift;
                   1140:     $self->{MESSAGE_AFTER} = shift;
                   1141:     $self->{NEXT_STATE} = shift;
                   1142:     $self->{VAR_NAME} = shift;
                   1143:     $self->{FILTER_FUNC} = shift;
                   1144:     if (!defined($self->{FILTER_FUNC})) {
                   1145:         $self->{FILTER_FUNC} = sub {return 1;};
                   1146:     }
                   1147:     $self->{CHOICE_FUNC} = shift;
                   1148:     if (!defined($self->{CHOICE_FUNC})) {
                   1149:         $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
                   1150:     }
1.4       bowersj2 1151:     $self->{MULTICHOICE} = shift;
                   1152:     if (!defined($self->{MULTICHOICE})) {
                   1153:         $self->{MULTICHOICE} = 0;
                   1154:     }
1.3       bowersj2 1155: }
                   1156: 
                   1157: sub postprocess {
                   1158:     my $self = shift;
                   1159:     my $wizard = $self->{WIZARD};
1.4       bowersj2 1160: 
                   1161:     # If we were just manipulating a folder, do not proceed to the
                   1162:     # next state
                   1163:     if ($ENV{'form.folderManip'}) {
                   1164:         return;
                   1165:     }
                   1166: 
1.7     ! bowersj2 1167:     if (!$ENV{'form.' . $self->{VAR_NAME} . '.forminput'}) {
        !          1168:         $self->{ERROR_MSG} = "Can't continue wizard because you must ".
        !          1169:             "select a resource.";
        !          1170:         return;
        !          1171:     }
        !          1172:         
        !          1173: 
        !          1174:     # Value stored by wizard framework
        !          1175: 
1.3       bowersj2 1176:     $wizard->changeState($self->{NEXT_STATE});
                   1177: }
                   1178: 
                   1179: sub render {
                   1180:     my $self = shift;
1.4       bowersj2 1181:     my $wizard = $self->{WIZARD};
1.3       bowersj2 1182:     my $result = "";
                   1183:     my $var = $self->{VAR_NAME};
                   1184:     my $curVal = $self->{WIZARD}->{VARS}->{$var};
1.4       bowersj2 1185:     my $vals = {};
                   1186:     if ($curVal =~ /,/) { # multiple choices
                   1187:         foreach (split /,/, $curVal) {
                   1188:             $vals->{$_} = 1;
                   1189:         }
                   1190:     } else {
                   1191:         $vals->{$curVal} = 1;
1.7     ! bowersj2 1192:     }
        !          1193: 
        !          1194:     if (defined $self->{ERROR_MSG}) {
        !          1195:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1.4       bowersj2 1196:     }
1.3       bowersj2 1197: 
                   1198:     $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});
                   1199: 
                   1200:     # Get the course nav map
                   1201:     my $navmap = Apache::lonnavmaps::navmap->new(
                   1202:                            $ENV{"request.course.fn"}.".db",
                   1203:                            $ENV{"request.course.fn"}."_parms.db", 0, 0);
                   1204: 
                   1205:     if (!defined($navmap)) {
                   1206:         return "<font color='red' size='+1'>Something has gone wrong with the map selection feature. Please contact your administrator.</font>";
                   1207:     }
                   1208: 
1.4       bowersj2 1209:     my $iterator = $navmap->getIterator(undef, undef, undef, 0, 0);
1.3       bowersj2 1210:     my $filterFunc = $self->{FILTER_FUNC};
                   1211:     my $choiceFunc = $self->{CHOICE_FUNC};
                   1212: 
1.4       bowersj2 1213:     # Create the composite function that renders the column on the nav map
                   1214:     my $renderColFunc = sub {
                   1215:         my ($resource, $part, $params) = @_;
                   1216:         
                   1217:         if (!&$choiceFunc($resource)) {
                   1218:             return '<td>&nbsp;</td>';
                   1219:         } else {
                   1220:             my $col = "<td><input type='radio' name='${var}.forminput' ";
                   1221:             if ($vals->{$resource->{ID}}) {
                   1222:                 $col .= "checked ";
1.3       bowersj2 1223:             }
1.4       bowersj2 1224:             $col .= "value='" . $resource->{ID} . "' /></td>";
                   1225:             return $col;
1.3       bowersj2 1226:         }
1.4       bowersj2 1227:     };
1.3       bowersj2 1228: 
1.4       bowersj2 1229:     $result .= 
                   1230:         &Apache::lonnavmaps::render( { "iterator" => $iterator,
                   1231:                                        'cols' => [$renderColFunc, 
                   1232:                                                   Apache::lonnavmaps::resource()],
                   1233:                                        'showParts' => 0,
                   1234:                                        'queryString' => $wizard->queryStringVars() . '&folderManip=1',
                   1235:                                        'url' => '/adm/wizard'} );
                   1236:                                                 
1.3       bowersj2 1237:     $navmap->untieHashes();
                   1238: 
                   1239:     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
                   1240: 
                   1241:     return $result;
                   1242: }
                   1243:     
                   1244: 1;
                   1245: 
                   1246: package Apache::lonwizard::choose_student;
                   1247: 
                   1248: no strict;
                   1249: @ISA = ("Apache::lonwizard::choice_state");
                   1250: use strict;
                   1251: 
                   1252: sub new {
                   1253:     my $proto = shift;
                   1254:     my $class = ref($proto) || $proto;
                   1255:     my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
                   1256:                                         shift, shift, shift);
                   1257:     return $self;
                   1258: }
                   1259: 
                   1260: sub determineChoices {
                   1261:     my %choices;
                   1262: 
                   1263:     my $classlist = Apache::loncoursedata::get_classlist();
                   1264:     foreach (keys %$classlist) {
                   1265:         $choices{$classlist->{$_}->[6]} = $_;
                   1266:     }
                   1267:     
                   1268:     return \%choices;
                   1269: }
                   1270: 
                   1271: 1;
                   1272: 
                   1273: package Apache::lonwizard::choose_section;
                   1274: 
                   1275: no strict;
                   1276: @ISA = ("Apache::lonwizard::choice_state");
                   1277: use strict;
                   1278: 
                   1279: sub new {
                   1280:     my $proto = shift;
                   1281:     my $class = ref($proto) || $proto;
                   1282:     my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
                   1283:                                         shift, shift, shift);
                   1284:     return $self;
                   1285: }
                   1286: 
                   1287: sub determineChoices {
                   1288:     my %choices;
                   1289: 
                   1290:     my $classlist = Apache::loncoursedata::get_classlist();
                   1291:     foreach (keys %$classlist) {
                   1292:         my $sectionName = $classlist->{$_}->[5];
                   1293:         if (!$sectionName) {
                   1294:             $choices{"No section assigned"} = "";
                   1295:         } else {
                   1296:             $choices{$sectionName} = $sectionName;
                   1297:         }
                   1298:     }
                   1299:     
                   1300:     return \%choices;
                   1301: }
                   1302: 
                   1303: 1;
1.1       bowersj2 1304: 

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