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

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

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