File:  [LON-CAPA] / loncom / interface / Attic / lonwizard.pm
Revision 1.8: download - view: text, annotated - select for diffs
Thu Feb 13 23:46:27 2003 UTC (21 years, 3 months ago) by bowersj2
Branches: MAIN
CVS tags: HEAD
Date now uses hours+meridian instead of seperate hours and meridian fields.

Rendering fixed to work more smoothly with nav maps. Resource and
map selection should work, with some possibly user-confusing possibilities.
Will research those in my next user meetings.

    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;
    8: use Apache::lonnet;
    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: 
   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.
   95: 
   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.
   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: 
  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.
  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: 	}
  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:         }
  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};
  218:     
  219:     # Error checking
  220:     if (!defined($state)) {
  221:         $result .="Error! The state ". $startState ." is not defined.";
  222:         return $result;
  223:     }
  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();
  237:     my $bodytag = &Apache::loncommon::bodytag("$self->{TITLE}",'','');
  238: 
  239:     $result .= <<HEADER;
  240: <html>
  241:     <head>
  242:         <title>LON-CAPA Wizard: $self->{TITLE}</title>
  243:     </head>
  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>
  250: HEADER
  251: 
  252:     if (!$state->overrideForm()) {
  253:         $result .= $self->_saveVars();
  254:     }
  255:     $result .= $state->render() . "<p>&nbsp;</p>";
  256: 
  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:         {
  270:             $result .= '<input name="back" type="button" ';
  271:             $result .= 'value="&lt;- Previous" onclick="history.go(-1)" /> ';
  272:             $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" />';
  273:         }
  274:         $result .= "</center>\n";
  275:     }
  276: 
  277:     $result .= <<FOOTER;
  278:               </td>
  279:             </tr>
  280:           </table>
  281:         </form>
  282:     </body>
  283: </html>
  284: FOOTER
  285: 
  286:     return $result;
  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: 
  319: # This may look trivial, but it's here as a hook for possible later processing
  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: 
  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: 
  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: 
  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;
  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>
  383: 
  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>
  385: 
  386: <p>Press <b>Next -&gt;</b> to begin, or select <b>&lt;- Previous</b> to go back to the previous screen.</p>
  387: WIZBEGIN
  388:     
  389:     my $wizard = Apache::lonwizard->new("Course Parameter Wizard");
  390:     $wizard->declareVars(['ACTION_TYPE', 'GRANULARITY', 'TARGETS', 'PARM_DATE', 'RESOURCE_ID', 'USER_NAME', 'SECTION_NAME']);
  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');
  397:     
  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. . .");
  419: 
  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");
  422:     Apache::lonwizard::parmwizfinal->new($wizard, "FINISH", "Confirm Selection");
  423: 
  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: 
  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: 
  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: 
  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: 
  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: 
  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: 
  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.
  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: 
  547:     # This cute looking statement correctly handles subclassing
  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: 
  587: =back
  588: 
  589: =cut
  590: 
  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: 
  606: sub preprocess {
  607:     my $self = shift;
  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();
  613: 
  614:     my @keys = keys(%$choices);
  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;
  621: 	$self->{WIZARD}->{DONE} = 1;
  622: 	return;
  623:     }
  624:     if (scalar(@keys) == 1)
  625:     {
  626: 	# If there is only one choice, pick it and move on.
  627: 	$wizvars->{$self->{VAR_NAME}} = $choices->{$keys[0]};
  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: 
  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})
  651:     {
  652: 	$result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
  653:     }
  654: 
  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($_) . "</option>\n";
  663:     }
  664:     $result .= "</select>\n\n";
  665: 
  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) {
  679:         # Value already stored by Wizard
  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
  695: 
  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 {
  761:     # Value already stored by wizard
  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
  851:     {
  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:         }
  873:         $result .= $months[$i] . "</option>\n";
  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</option>\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</option>\n";
  898:     }
  899:     $result .= "</select>,\n";
  900: 
  901:     # Display Hours and Minutes if they are called for
  902:     if (!$self->{DISPLAY_JUST_DATE}) {
  903:         # Build hour
  904:         $result .= "<select name='$self->{VAR_NAME}hour'>\n";
  905:         $result .= "<option " . ($date->hour == 0 ? 'selected ':'') .
  906:             " value='0'>midnight</option>\n";
  907:         for ($i = 1; $i < 12; $i++) {
  908:             if ($date->hour == $i) {
  909:                 $result .= "<option selected value='$i'>$i a.m.</option>\n";
  910:             } else {
  911:                 $result .= "<option value='$i'>$i a.m</option>\n";
  912:             }
  913:         }
  914:         $result .= "<option " . ($date->hour == 12 ? 'selected ':'') .
  915:             " value='12'>noon</option>\n";
  916:         for ($i = 13; $i < 24; $i++) {
  917:             my $printedHour = $i - 12;
  918:             if ($date->hour == $i) {
  919:                 $result .= "<option selected value='$i'>$printedHour p.m.</option>\n";
  920:             } else {
  921:                 $result .= "<option value='$i'>$printedHour p.m.</option>\n";
  922:             }
  923:         }
  924: 
  925:         $result .= "</select> :\n";
  926: 
  927:         $result .= "<select name='$self->{VAR_NAME}minute'>\n";
  928:         for ($i = 0; $i < 60; $i++) {
  929:             my $printedMinute = $i;
  930:             if ($i < 10) {
  931:                 $printedMinute = "0" . $printedMinute;
  932:             }
  933:             if ($date->min == $i) {
  934:                 $result .= "<option selected>";
  935:             } else {
  936:                 $result .= "<option>";
  937:             }
  938:             $result .= "$printedMinute</option>\n";
  939:         }
  940:         $result .= "</select>\n";
  941:     }
  942: 
  943:     if (defined ($self->{MESSAGE_AFTER})) {
  944:         $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
  945:     }
  946: 
  947:     return $result;
  948: }
  949: 
  950: # Stick the date stored into the chosen variable.
  951: sub postprocess {
  952:     my $self = shift;
  953:     my $wizard = $self->{WIZARD};
  954: 
  955:     my $month = $ENV{'form.' . $self->{VAR_NAME} . 'month'}; 
  956:     my $day = $ENV{'form.' . $self->{VAR_NAME} . 'day'}; 
  957:     my $year = $ENV{'form.' . $self->{VAR_NAME} . 'year'}; 
  958:     my $min = 0; 
  959:     my $hour = 0;
  960:     if (!$self->{DISPLAY_JUST_DATE}) {
  961:         $min = $ENV{'form.' . $self->{VAR_NAME} . 'minute'};
  962:         $hour = $ENV{'form.' . $self->{VAR_NAME} . 'hour'};
  963:     }
  964: 
  965:     my $chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);
  966:     # Check to make sure that the date was not automatically co-erced into a 
  967:     # valid date, as we want to flag that as an error
  968:     # This happens for "Feb. 31", for instance, which is coerced to March 2 or
  969:     # 3, depending on if it's a leapyear
  970:     my $checkDate = localtime($chosenDate);
  971: 
  972:     if ($checkDate->mon != $month || $checkDate->mday != $day ||
  973:         $checkDate->year + 1900 != $year) {
  974:         $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
  975:             . "date because it doesn't exist. Please enter a valid date.";
  976:         return;
  977:     }
  978: 
  979:     $wizard->setVar($self->{VAR_NAME}, $chosenDate);
  980: 
  981:     $wizard->changeState($self->{NEXT_STATE});
  982: }
  983: 
  984: 1;
  985: 
  986: package Apache::lonwizard::parmwizfinal;
  987: 
  988: # This is the final state for the parmwizard. It is not generally useful,
  989: # so it is not perldoc'ed. It does it's own processing.
  990: 
  991: no strict;
  992: @ISA = ('Apache::lonwizard::state');
  993: use strict;
  994: 
  995: use Time::localtime;
  996: 
  997: sub new {
  998:     my $proto = shift;
  999:     my $class = ref($proto) || $proto;
 1000:     my $self = bless $proto->SUPER::new(shift, shift, shift);
 1001: 
 1002:     # No other variables because it gets it all from the wizard.
 1003: }
 1004: 
 1005: # Renders a form that, when submitted, will form the input to lonparmset.pm
 1006: sub render {
 1007:     my $self = shift;
 1008:     my $wizard = $self->{WIZARD};
 1009:     my $wizvars = $wizard->{VARS};
 1010: 
 1011:     # FIXME: Unify my designators with the standard ones
 1012:     my %dateTypeHash = ('open_date' => "Opening Date",
 1013:                         'due_date' => "Due Date",
 1014:                         'answer_date' => "Answer Date");
 1015:     my %parmTypeHash = ('open_date' => "0_opendate",
 1016:                         'due_date' => "0_duedate",
 1017:                         'answer_date' => "0_answerdate");
 1018:     
 1019:     my $result = "<form method='get' action='/adm/parmset'>\n";
 1020:     $result .= '<p>Confirm that this information is correct, then click &quot;Finish Wizard&quot; to complete setting the parameter.<ul>';
 1021:     my $affectedResourceId = "";
 1022:     my $parm_name = $parmTypeHash{$wizvars->{ACTION_TYPE}};
 1023:     my $level = "";
 1024:     
 1025:     # Print the type of manipulation:
 1026:     $result .= '<li>Setting the <b>' . $dateTypeHash{$wizvars->{ACTION_TYPE}}
 1027:                . "</b></li>\n";
 1028:     if ($wizvars->{ACTION_TYPE} eq 'due_date' || 
 1029:         $wizvars->{ACTION_TYPE} eq 'answer_date') {
 1030:         # for due dates, we default to "date end" type entries
 1031:         $result .= "<input type='hidden' name='recent_date_end' " .
 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_end' />\n";
 1037:     } elsif ($wizvars->{ACTION_TYPE} eq 'open_date') {
 1038:         $result .= "<input type='hidden' name='recent_date_start' ".
 1039:             "value='" . $wizvars->{PARM_DATE} . "' />\n";
 1040:         $result .= "<input type='hidden' name='pres_value' " .
 1041:             "value='" . $wizvars->{PARM_DATE} . "' />\n";
 1042:         $result .= "<input type='hidden' name='pres_type' " .
 1043:             "value='date_start' />\n";
 1044:     } 
 1045:     
 1046:     # Print the granularity, depending on the action
 1047:     if ($wizvars->{GRANULARITY} eq 'whole_course') {
 1048:         $result .= '<li>for <b>all resources in the course</b></li>';
 1049:         $level = 9; # general course, see lonparmset.pm perldoc
 1050:         $affectedResourceId = "0.0";
 1051:     } elsif ($wizvars->{GRANULARITY} eq 'map') {
 1052:         my $navmap = Apache::lonnavmaps::navmap->new(
 1053:                            $ENV{"request.course.fn"}.".db",
 1054:                            $ENV{"request.course.fn"}."_parms.db", 0, 0);
 1055:         my $res = $navmap->getById($wizvars->{RESOURCE_ID});
 1056:         my $title = $res->compTitle();
 1057:         $navmap->untieHashes();
 1058:         $result .= "<li>for the map named <b>$title</b></li>";
 1059:         $level = 8;
 1060:         $affectedResourceId = $wizvars->{RESOURCE_ID};
 1061:     } else {
 1062:         my $navmap = Apache::lonnavmaps::navmap->new(
 1063:                            $ENV{"request.course.fn"}.".db",
 1064:                            $ENV{"request.course.fn"}."_parms.db", 0, 0);
 1065:         my $res = $navmap->getById($wizvars->{RESOURCE_ID});
 1066:         my $title = $res->compTitle();
 1067:         $navmap->untieHashes();
 1068:         $result .= "<li>for the resource named <b>$title</b></li>";
 1069:         $level = 7;
 1070:         $affectedResourceId = $wizvars->{RESOURCE_ID};
 1071:     }
 1072: 
 1073:     # Print targets
 1074:     if ($wizvars->{TARGETS} eq 'course') {
 1075:         $result .= '<li>for <b>all students in course</b></li>';
 1076:     } elsif ($wizvars->{TARGETS} eq 'section') {
 1077:         my $section = $wizvars->{SECTION_NAME};
 1078:         $result .= "<li>for section <b>$section</b></li>";
 1079:         $level -= 3;
 1080:         $result .= "<input type='hidden' name='csec' value='" .
 1081:             HTML::Entities::encode($section) . "' />\n";
 1082:     } else {
 1083:         # FIXME: This is probably wasteful! 
 1084:         my $classlist = Apache::loncoursedata::get_classlist();
 1085:         my $name = $classlist->{$wizvars->{USER_NAME}}->[6];
 1086:         $result .= "<li>for <b>$name</b></li>";
 1087:         $level -= 6;
 1088:         my ($uname, $udom) = split /:/, $wizvars->{USER_NAME};
 1089:         $result .= "<input type='hidden' name='uname' value='".
 1090:             HTML::Entities::encode($uname) . "' />\n";
 1091:         $result .= "<input type='hidden' name='udom' value='".
 1092:             HTML::Entities::encode($udom) . "' />\n";
 1093:     }
 1094: 
 1095:     # Print value
 1096:     $result .= "<li>to <b>" . ctime($wizvars->{PARM_DATE}) . "</b> (" .
 1097:         Apache::lonnavmaps::timeToHumanString($wizvars->{PARM_DATE}) 
 1098:         . ")</li>\n";
 1099: 
 1100:     # print pres_marker
 1101:     $result .= "\n<input type='hidden' name='pres_marker'" .
 1102:         " value='$affectedResourceId&$parm_name&$level' />\n";
 1103: 
 1104:     $result .= "<br /><br /><center><input type='submit' value='Finish Wizard' /></center></form>\n";
 1105: 
 1106:     return $result;
 1107: }
 1108:     
 1109: sub overrideForm {
 1110:     return 1;
 1111: }
 1112: 
 1113: 1;
 1114: 
 1115: package Apache::lonwizard::resource_choice;
 1116: 
 1117: =pod 
 1118: 
 1119: =head2 Class: resource_choice
 1120: 
 1121: 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.
 1122: 
 1123: Note this state will not automatically advance if there is only one choice, because it might confuse the user in this case.
 1124: 
 1125: =over 4
 1126: 
 1127: =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.
 1128: 
 1129: 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.
 1130: 
 1131: 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.
 1132: 
 1133: =back
 1134: 
 1135: =cut
 1136: 
 1137: no strict;
 1138: @ISA = ("Apache::lonwizard::state");
 1139: use strict;
 1140: 
 1141: sub new { 
 1142:     my $proto = shift;
 1143:     my $class = ref($proto) || $proto;
 1144:     my $self = bless $proto->SUPER::new(shift, shift, shift);
 1145: 
 1146:     $self->{MESSAGE_BEFORE} = shift;
 1147:     $self->{MESSAGE_AFTER} = shift;
 1148:     $self->{NEXT_STATE} = shift;
 1149:     $self->{VAR_NAME} = shift;
 1150:     $self->{FILTER_FUNC} = shift;
 1151:     if (!defined($self->{FILTER_FUNC})) {
 1152:         $self->{FILTER_FUNC} = sub {return 1;};
 1153:     }
 1154:     $self->{CHOICE_FUNC} = shift;
 1155:     if (!defined($self->{CHOICE_FUNC})) {
 1156:         $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
 1157:     }
 1158:     $self->{MULTICHOICE} = shift;
 1159:     if (!defined($self->{MULTICHOICE})) {
 1160:         $self->{MULTICHOICE} = 0;
 1161:     }
 1162: }
 1163: 
 1164: sub postprocess {
 1165:     my $self = shift;
 1166:     my $wizard = $self->{WIZARD};
 1167: 
 1168:     # If we were just manipulating a folder, do not proceed to the
 1169:     # next state
 1170:     if ($ENV{'form.folderManip'}) {
 1171:         return;
 1172:     }
 1173: 
 1174:     if (!$ENV{'form.' . $self->{VAR_NAME} . '.forminput'}) {
 1175:         $self->{ERROR_MSG} = "Can't continue wizard because you must ".
 1176:             "select a resource.";
 1177:         return;
 1178:     }
 1179:         
 1180: 
 1181:     # Value stored by wizard framework
 1182: 
 1183:     $wizard->changeState($self->{NEXT_STATE});
 1184: }
 1185: 
 1186: sub render {
 1187:     my $self = shift;
 1188:     my $wizard = $self->{WIZARD};
 1189:     my $result = "";
 1190:     my $var = $self->{VAR_NAME};
 1191:     my $curVal = $self->{WIZARD}->{VARS}->{$var};
 1192:     my $vals = {};
 1193:     if ($curVal =~ /,/) { # multiple choices
 1194:         foreach (split /,/, $curVal) {
 1195:             $vals->{$_} = 1;
 1196:         }
 1197:     } else {
 1198:         $vals->{$curVal} = 1;
 1199:     }
 1200: 
 1201:     if (defined $self->{ERROR_MSG}) {
 1202:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
 1203:     }
 1204: 
 1205:     $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
 1206:         if (defined $self->{MESSAGE_BEFORE});
 1207: 
 1208:     my $filterFunc = $self->{FILTER_FUNC};
 1209:     my $choiceFunc = $self->{CHOICE_FUNC};
 1210: 
 1211:     # Create the composite function that renders the column on the nav map
 1212:     my $renderColFunc = sub {
 1213:         my ($resource, $part, $params) = @_;
 1214:         
 1215:         if (!&$choiceFunc($resource)) {
 1216:             return '<td>&nbsp;</td>';
 1217:         } else {
 1218:             my $col = "<td><input type='radio' name='${var}.forminput' ";
 1219:             if ($vals->{$resource->{ID}}) {
 1220:                 $col .= "checked ";
 1221:             }
 1222:             $col .= "value='" . $resource->{ID} . "' /></td>";
 1223:             return $col;
 1224:         }
 1225:     };
 1226: 
 1227:     $result .= 
 1228:         &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc, 
 1229:                                                   Apache::lonnavmaps::resource()],
 1230:                                        'showParts' => 0,
 1231:                                        'queryString' => $wizard->queryStringVars() . '&folderManip=1',
 1232:                                        'url' => '/adm/wizard',
 1233:                                        'filterFunc' => $filterFunc } );
 1234:                                                 
 1235:     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
 1236: 
 1237:     return $result;
 1238: }
 1239:     
 1240: 1;
 1241: 
 1242: package Apache::lonwizard::choose_student;
 1243: 
 1244: no strict;
 1245: @ISA = ("Apache::lonwizard::choice_state");
 1246: use strict;
 1247: 
 1248: sub new {
 1249:     my $proto = shift;
 1250:     my $class = ref($proto) || $proto;
 1251:     my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
 1252:                                         shift, shift, shift);
 1253:     return $self;
 1254: }
 1255: 
 1256: sub determineChoices {
 1257:     my %choices;
 1258: 
 1259:     my $classlist = Apache::loncoursedata::get_classlist();
 1260:     foreach (keys %$classlist) {
 1261:         $choices{$classlist->{$_}->[6]} = $_;
 1262:     }
 1263:     
 1264:     return \%choices;
 1265: }
 1266: 
 1267: 1;
 1268: 
 1269: package Apache::lonwizard::choose_section;
 1270: 
 1271: no strict;
 1272: @ISA = ("Apache::lonwizard::choice_state");
 1273: use strict;
 1274: 
 1275: sub new {
 1276:     my $proto = shift;
 1277:     my $class = ref($proto) || $proto;
 1278:     my $self = bless $proto->SUPER::new(shift, shift, shift, shift,
 1279:                                         shift, shift, shift);
 1280:     return $self;
 1281: }
 1282: 
 1283: sub determineChoices {
 1284:     my %choices;
 1285: 
 1286:     my $classlist = Apache::loncoursedata::get_classlist();
 1287:     foreach (keys %$classlist) {
 1288:         my $sectionName = $classlist->{$_}->[5];
 1289:         if (!$sectionName) {
 1290:             $choices{"No section assigned"} = "";
 1291:         } else {
 1292:             $choices{$sectionName} = $sectionName;
 1293:         }
 1294:     }
 1295:     
 1296:     return \%choices;
 1297: }
 1298: 
 1299: 1;
 1300: 

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