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

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

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