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

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

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