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

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

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