--- loncom/interface/Attic/lonwizard.pm	2003/02/07 19:30:43	1.4
+++ loncom/interface/Attic/lonwizard.pm	2003/02/13 23:46:27	1.8
@@ -103,6 +103,13 @@ sub new {
 
 =cut
 
+# Sometimes the wizard writer will want to use the result of the previous
+# state to change the text of the next state. In order to do that, it
+# has to be done during the declaration of the states, or it won't be
+# available. Therefore, basic form processing must occur before the
+# actual display routine is called and the actual pre-process is called,
+# or it won't be available.
+# This also factors common code out of the preprocess calls.
 sub declareVars {
     my $self = shift;
     my $varlist = shift;
@@ -119,6 +126,12 @@ sub declareVars {
 	{
 	    $self->{VARS}->{$element} = $ENV{$envname};
 	}
+        
+        # If there's an incoming form submission, use that
+        my $envname = "form." . $element . ".forminput";
+        if (defined ($ENV{$envname})) {
+            $self->{VARS}->{$element} = $ENV{$envname};
+        }
     }
 }
 
@@ -366,48 +379,46 @@ sub handler {
     $r->rflush();
 
     my $mes = <<WIZBEGIN;
-<p>This wizard will allow you to</p>
-
-<ul>
-  <li>Change assignment parameters, such as due date or open date...</li>
-  <li>... for a whole class</li>
-  <li>... for a whole section</li>
-  <li>... for an individual student</li>
-  <li>... by folder</li>
-  <li>... by individual assignment</li>
-</ul>
+<p>This wizard will allow you to <b>set open, due, and answer dates for problems</b>. You will be asked to select a problem, what kind of date you want to set, and for whom the date should be effective.</p>
 
 <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>
+
+<p>Press <b>Next -&gt;</b> to begin, or select <b>&lt;- Previous</b> to go back to the previous screen.</p>
 WIZBEGIN
     
     my $wizard = Apache::lonwizard->new("Course Parameter Wizard");
     $wizard->declareVars(['ACTION_TYPE', 'GRANULARITY', 'TARGETS', 'PARM_DATE', 'RESOURCE_ID', 'USER_NAME', 'SECTION_NAME']);
-    my %dateTypeHash = ('open_date' => "Opening Date",
-                        'due_date' => "Due Date",
-                        'answer_date' => "Answer Date");
+    my %dateTypeHash = ('open_date' => "opening date",
+                        'due_date' => "due date",
+                        'answer_date' => "answer date");
+    my %levelTypeHash = ('whole_course' => "all problems in the course",
+                         'map' => 'the selected folder',
+                         'resource' => 'the selected problem');
     
-    Apache::lonwizard::message_state->new($wizard, "START", "Welcome to the Assignment Parameter Wizard", $mes, "CHOOSE_ACTION");
-    Apache::lonwizard::switch_state->new($wizard, "CHOOSE_ACTION", "What do you want to do?", "ACTION_TYPE", [ 
-       ["open_date", "Set an Open Date for a problem", "CHOOSE_LEVEL"], 
-       ["due_date", "Set a Due Date for a problem", "CHOOSE_LEVEL"],
-       ["answer_date", "Set an Answer Open Date for a problem", "CHOOSE_LEVEL" ] ]);
-    Apache::lonwizard::switch_state->new($wizard, "CHOOSE_LEVEL", "Parameter Granularity", "GRANULARITY", [
-       ["whole_course", "Set for Whole Course", "CHOOSE_STUDENT_LEVEL"],
-       ["map", "Set for a Folder/Map", "CHOOSE_FOLDER"],
-       ["resource", "Set for a Particular Problem", "CHOOSE_RESOURCE"]],
-                                         "How general should this setting be?");
-    Apache::lonwizard::resource_choice->new($wizard, "CHOOSE_FOLDER", "Select Folder", "", "", "CHOOSE_STUDENT_LEVEL", "RESOURCE_ID", sub {my $res = shift; return $res->is_map();});
-    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(); });
-    Apache::lonwizard::switch_state->new($wizard, "CHOOSE_STUDENT_LEVEL", "Parameter Targets", "TARGETS", [
-       ["course", "Set for All Students in Course", "CHOOSE_DATE"],
-       ["section", "Set for Section", "CHOOSE_SECTION"],
-       ["student", "Set for an Individual Student", "CHOOSE_STUDENT"]],
-                                       "Whom should this setting affect?");
-
+    Apache::lonwizard::message_state->new($wizard, "START", "Welcome to the Assignment Parameter Wizard", $mes, "CHOOSE_LEVEL");
+    Apache::lonwizard::switch_state->new($wizard, "CHOOSE_LEVEL", "Which Problem or Problems?", "GRANULARITY", [
+       ["whole_course", "<b>Every problem</b> in the course", "CHOOSE_ACTION"],
+       ["map", "Every problem in a particular <b>folder</b>", "CHOOSE_FOLDER"],
+       ["resource", "One particular <b>problem</b>", "CHOOSE_RESOURCE"]],
+                                         "Which problems do you wish to change a date for?");
+    Apache::lonwizard::resource_choice->new($wizard, "CHOOSE_FOLDER", "Select Folder", "Select the folder you wish to set the date for:", "", "CHOOSE_ACTION", "RESOURCE_ID", sub {my $res = shift; return $res->is_map();});
+    Apache::lonwizard::resource_choice->new($wizard, "CHOOSE_RESOURCE", "Select Resource", "", "", "CHOOSE_ACTION", "RESOURCE_ID", sub {my $res = shift; return $res->is_map() || $res->is_problem();}, sub {my $res = shift; return $res->is_problem(); });
+    my $levelType = $levelTypeHash{$wizard->{VARS}->{GRANULARITY}};
+    Apache::lonwizard::switch_state->new($wizard, "CHOOSE_ACTION", "Parameter Type", "ACTION_TYPE", [ 
+       ["open_date", "Set an <b>open date</b>", "CHOOSE_DATE"], 
+       ["due_date", "Set a <b>due date</b>", "CHOOSE_DATE"],
+       ["answer_date", "Set an <b>answer open date</b>", "CHOOSE_DATE" ] ],
+       "What parameters do you want to set for $levelType?");
     my $dateType = $dateTypeHash{$wizard->{VARS}->{ACTION_TYPE}};
-    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");
-    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");
-    Apache::lonwizard::date_state->new($wizard, "CHOOSE_DATE", "Set Date", "PARM_DATE", "FINISH", "What should the $dateType be set to?");
+    Apache::lonwizard::date_state->new($wizard, "CHOOSE_DATE", "Set Date", "PARM_DATE", "CHOOSE_STUDENT_LEVEL", "What should the $dateType be set to?");
+    Apache::lonwizard::switch_state->new($wizard, "CHOOSE_STUDENT_LEVEL", "Students Affected", "TARGETS", [
+       ["course", ". . . for <b>all students</b> in the course", "FINISH"],
+       ["section", ". . . for a particular <b>section</b>", "CHOOSE_SECTION"],
+       ["student", ". . . for an individual <b>student</b>", "CHOOSE_STUDENT"]],
+                                       "Set $dateType of $levelType for. . .");
+
+    Apache::lonwizard::choose_section->new($wizard, "CHOOSE_SECTION", "Select Section", "Please select the section you wish to set the $dateType for:", "", "FINISH", "SECTION_NAME");
+    Apache::lonwizard::choose_student->new($wizard, "CHOOSE_STUDENT", "Select Student", "Please select the student you wish to set the $dateType for:", "", "FINISH", "USER_NAME");
     Apache::lonwizard::parmwizfinal->new($wizard, "FINISH", "Confirm Selection");
 
     $r->print($wizard->display());
@@ -429,6 +440,8 @@ It is importent to remember when constru
 
 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.)
 
+By default, the wizard framework will take form elements of the form {VAR_NAME}.forminput and automatically insert the contents of that form element into the wizard variable {VAR_NAME}. You only need to use postprocess to do something fancy if that is not sufficient, for instance, processing a multi-element selection. (See resource choice for an example of that.)
+
 =head2 lonwizstate methods
 
 These methods should be overridden in derived states, except B<new> which may be sufficient.
@@ -646,7 +659,7 @@ sub render {
     foreach (@keys)
     {
 	$result .= "<option value=\"" . HTML::Entities::encode($choices->{$_}) 
-            . "\">" . HTML::Entities::encode($_) . "\n";
+            . "\">" . HTML::Entities::encode($_) . "</option>\n";
     }
     $result .= "</select>\n\n";
 
@@ -663,7 +676,7 @@ sub postprocess {
     my $wizard = $self->{WIZARD};
     my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
     if ($formvalue) {
-        $wizard->setVar($self->{VAR_NAME}, $formvalue);
+        # Value already stored by Wizard
         $wizard->changeState($self->{NEXT_STATE});
     } else {
         $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
@@ -745,11 +758,10 @@ sub render {
 }
 
 sub postprocess {
+    # Value already stored by wizard
     my $self = shift;
     my $wizard = $self->{WIZARD};
     my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . '.forminput'};
-    $wizard->setVar($self->{VAR_NAME}, $chosenValue)
-	if (defined ($self->{VAR_NAME}));
 
     foreach my $choice (@{$self->{CHOICE_LIST}})
     {
@@ -858,7 +870,7 @@ sub render {
         } else {
             $result .= "<option value='$i'>";
         }
-        $result .= @months[$i] . "\n";
+        $result .= $months[$i] . "</option>\n";
     }
     $result .= "</select>\n";
 
@@ -870,7 +882,7 @@ sub render {
         } else {
             $result .= '<option>';
         }
-        $result .= "$i\n";
+        $result .= "$i</option>\n";
     }
     $result .= "</select>,\n";
 
@@ -882,43 +894,50 @@ sub render {
         } else {
             $result .= "<option>";
         }
-        $result .= "$i\n";
+        $result .= "$i</option>\n";
     }
     $result .= "</select>,\n";
 
     # Display Hours and Minutes if they are called for
     if (!$self->{DISPLAY_JUST_DATE}) {
+        # Build hour
         $result .= "<select name='$self->{VAR_NAME}hour'>\n";
-        if ($date->hour == 12) { $result .= "<option selected>12\n"; }
-        else { $result .= "<option>12\n" }
+        $result .= "<option " . ($date->hour == 0 ? 'selected ':'') .
+            " value='0'>midnight</option>\n";
         for ($i = 1; $i < 12; $i++) {
-            if (($date->hour) % 12 == $i % 12) {
-                $result .= "<option selected>";
+            if ($date->hour == $i) {
+                $result .= "<option selected value='$i'>$i a.m.</option>\n";
             } else {
-                $result .= "<option>";
+                $result .= "<option value='$i'>$i a.m</option>\n";
             }
-            $result .= "$i\n";
         }
+        $result .= "<option " . ($date->hour == 12 ? 'selected ':'') .
+            " value='12'>noon</option>\n";
+        for ($i = 13; $i < 24; $i++) {
+            my $printedHour = $i - 12;
+            if ($date->hour == $i) {
+                $result .= "<option selected value='$i'>$printedHour p.m.</option>\n";
+            } else {
+                $result .= "<option value='$i'>$printedHour p.m.</option>\n";
+            }
+        }
+
         $result .= "</select> :\n";
 
         $result .= "<select name='$self->{VAR_NAME}minute'>\n";
         for ($i = 0; $i < 60; $i++) {
+            my $printedMinute = $i;
+            if ($i < 10) {
+                $printedMinute = "0" . $printedMinute;
+            }
             if ($date->min == $i) {
                 $result .= "<option selected>";
             } else {
                 $result .= "<option>";
             }
-            $result .= "$i\n";
+            $result .= "$printedMinute</option>\n";
         }
         $result .= "</select>\n";
-
-        $result .= "<select name='$self->{VAR_NAME}meridian'>\n";
-        if ($date->hour < 12) {
-            $result .= "<option selected>A.M.\n<option>P.M.\n";
-        } else {
-            $result .= "<option>A.M.\n<option selected>P.M.\n";
-        }
-        $result .= "</select>";
     }
 
     if (defined ($self->{MESSAGE_AFTER})) {
@@ -1152,10 +1171,15 @@ sub postprocess {
         return;
     }
 
-    my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . ".forminput"};
-    $wizard->setVar($self->{VAR_NAME}, $chosenValue)
-        if (defined($self->{VAR_NAME}));
-    
+    if (!$ENV{'form.' . $self->{VAR_NAME} . '.forminput'}) {
+        $self->{ERROR_MSG} = "Can't continue wizard because you must ".
+            "select a resource.";
+        return;
+    }
+        
+
+    # Value stored by wizard framework
+
     $wizard->changeState($self->{NEXT_STATE});
 }
 
@@ -1174,18 +1198,13 @@ sub render {
         $vals->{$curVal} = 1;
     }
 
-    $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});
-
-    # Get the course nav map
-    my $navmap = Apache::lonnavmaps::navmap->new(
-                           $ENV{"request.course.fn"}.".db",
-                           $ENV{"request.course.fn"}."_parms.db", 0, 0);
-
-    if (!defined($navmap)) {
-        return "<font color='red' size='+1'>Something has gone wrong with the map selection feature. Please contact your administrator.</font>";
+    if (defined $self->{ERROR_MSG}) {
+        $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
     }
 
-    my $iterator = $navmap->getIterator(undef, undef, undef, 0, 0);
+    $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
+        if (defined $self->{MESSAGE_BEFORE});
+
     my $filterFunc = $self->{FILTER_FUNC};
     my $choiceFunc = $self->{CHOICE_FUNC};
 
@@ -1206,15 +1225,13 @@ sub render {
     };
 
     $result .= 
-        &Apache::lonnavmaps::render( { "iterator" => $iterator,
-                                       'cols' => [$renderColFunc, 
+        &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc, 
                                                   Apache::lonnavmaps::resource()],
                                        'showParts' => 0,
                                        'queryString' => $wizard->queryStringVars() . '&folderManip=1',
-                                       'url' => '/adm/wizard'} );
+                                       'url' => '/adm/wizard',
+                                       'filterFunc' => $filterFunc } );
                                                 
-    $navmap->untieHashes();
-
     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
 
     return $result;