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

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

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