Diff for /loncom/interface/Attic/lonwizard.pm between versions 1.12 and 1.19

version 1.12, 2003/02/21 21:27:28 version 1.19, 2003/03/21 18:36:40
Line 9  use Apache::lonnet; Line 9  use Apache::lonnet;
   
 =head1 lonwizard - HTML "Wizard" framework for LON-CAPA  =head1 lonwizard - HTML "Wizard" framework for LON-CAPA
   
 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.  Wizards are a well-established UI widget that users feel comfortable with. It can take a complicated multi-dimensional problem the user has (such as the canonical Course Parameter example) and turn in into a series of bite-sized one-dimensional questions. Or take the some four-question form and put it in a Wizard, and present the same user with the same form outside of the Wizard, and the user will *think* the Wizard is easier.
   
 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.  For the developer, wizards do provide an easy way to bundle easy bits of functionality for the user, without having to write the tedious code for maintaining state between frames. It can be easier to write a Wizard then provide another custom interface.
   
 All classes are in the Apache::lonwizard namespace.  All classes are in the Apache::lonwizard namespace.
   
Line 19  All classes are in the Apache::lonwizard Line 19  All classes are in the Apache::lonwizard
   
 =cut  =cut
   
 # To prevent runaway file counts, this file has lonwizard,  
 # lonwizstate, and other wizard classes.  
 use strict;  use strict;
   
 use HTML::Entities;  use HTML::Entities;
 use Apache::loncommon;  use Apache::loncommon;
   use Digest::MD5 qw(md5_hex);
   use Apache::File;
   
 =pod  =pod
   
Line 76  sub new { Line 76  sub new {
  $self->{STATE} = "START";   $self->{STATE} = "START";
     }      }
   
     # set up return URL: Return the user to the referer page, unless the      $self->{TOKEN} = $ENV{'form.TOKEN'};
     # form has stored a value.      # If a token was passed, we load that in. Otherwise, we need to create a 
       # new storage file
       # Tried to use standard Tie'd hashes, but you can't seem to take a 
       # reference to a tied hash and write to it. I'd call that a wart.
       if ($self->{TOKEN}) {
           # Validate the token before trusting it
           if ($self->{TOKEN} !~ /^[a-f0-9]{32}$/) {
               # Not legit. Return nothing and let all hell break loose.
               # User shouldn't be doing that!
               return undef;
           }
   
           # Get the hash.
           $self->{FILENAME} = $Apache::lonnet::tmpdir . md5_hex($self->{TOKEN}); # Note the token is not the literal file
           
           my $file = Apache::File->new($self->{FILENAME});
           my $contents = <$file>;
           &Apache::loncommon::get_unprocessed_cgi($contents);
           $file->close();
           
           # Marks whether this is a new wizard.
           $self->{NEW_WIZARD} = 0;
       } else {
           # Only valid if we're just starting.
           if ($self->{STATE} ne 'START') {
               return undef;
           }
           # Must create the storage
           $self->{TOKEN} = md5_hex($ENV{'user.name'} . $ENV{'user.domain'} .
                                    time() . rand());
           $self->{FILENAME} = $Apache::lonnet::tmpdir . md5_hex($self->{TOKEN});
   
           # Marks whether this is a new wizard.
           $self->{NEW_WIZARD} = 1;
       }
   
       # OK, we now have our persistent storage.
   
     if (defined $ENV{"form.RETURN_PAGE"})      if (defined $ENV{"form.RETURN_PAGE"})
     {      {
  $self->{RETURN_PAGE} = $ENV{"form.RETURN_PAGE"};   $self->{RETURN_PAGE} = $ENV{"form.RETURN_PAGE"};
Line 88  sub new { Line 125  sub new {
     }      }
   
     $self->{STATES} = {};      $self->{STATES} = {};
     $self->{VARS} = {};  
     $self->{HISTORY} = {};      $self->{HISTORY} = {};
     $self->{DONE} = 0;      $self->{DONE} = 0;
   
Line 131  sub declareVars { Line 167  sub declareVars {
     foreach my $element ( @{$varlist} )      foreach my $element ( @{$varlist} )
     {      {
  # assign the var the default of ""   # assign the var the default of ""
  $self->{VARS}{$element} = "";   $self->{VARS}->{$element} = "";
   
  # if there's a form in the env, use that instead          my $envname;
  my $envname = "form." . $element;  
  if (defined ($ENV{$envname})) {  
     $self->{VARS}->{$element} = $ENV{$envname};  
  }  
                   
           $envname = "form." . $element;
           if (defined ($ENV{$envname})) {
               $self->{VARS}->{$element} = $ENV{$envname};
           }
   
         # If there's an incoming form submission, use that          # If there's an incoming form submission, use that
         $envname = "form." . $element . ".forminput";          $envname = "form." . $element . ".forminput";
         if (defined ($ENV{$envname})) {          if (defined ($ENV{$envname})) {
Line 147  sub declareVars { Line 184  sub declareVars {
     }      }
 }  }
   
 # Private function; takes all of the declared vars and returns a string  # Private function; returns a string to construct the hidden fields
 # corresponding to the hidden input fields that will re-construct the   # necessary to have the wizard track state.
 # variables.  
 sub _saveVars {  sub _saveVars {
     my $self = shift;      my $self = shift;
     my $result = "";      my $result = "";
     foreach my $varname (keys %{$self->{VARS}})  
     {  
  $result .= '<input type="hidden" name="' .  
            HTML::Entities::encode($varname) . '" value="' .  
    HTML::Entities::encode($self->{VARS}{$varname}) .   
    "\" />\n";  
     }  
   
     # also save state & return page  
     $result .= '<input type="hidden" name="CURRENT_STATE" value="' .      $result .= '<input type="hidden" name="CURRENT_STATE" value="' .
                HTML::Entities::encode($self->{STATE}) . '" />' . "\n";          HTML::Entities::encode($self->{STATE}) . "\" />\n";
       $result .= '<input type="hidden" name="TOKEN" value="' .
           $self->{TOKEN} . "\" />\n";
     $result .= '<input type="hidden" name="RETURN_PAGE" value="' .      $result .= '<input type="hidden" name="RETURN_PAGE" value="' .
                HTML::Entities::encode($self->{RETURN_PAGE}) . '" />' . "\n";          HTML::Entities::encode($self->{RETURN_PAGE}) . "\" />\n";
   
     return $result;      return $result;
 }  }
   
   # Private function: Create the querystring-like representation of the stored
   # data to write to disk.
   sub _varsInFile {
       my $self = shift;
       my @vars = ();
       for my $key (keys %{$self->{VARS}}) {
           push @vars, &Apache::lonnet::escape($key) . '=' .
               &Apache::lonnet::escape($self->{VARS}->{$key});
       }
       return join ('&', @vars);
   }
   
 =pod  =pod
   
 =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.  =item B<registerState>(referenceToStateObj): Registers a state as part of the wizard, so the wizard can use it. The 'referenceToStateObj' should be a reference to an instantiated lonwizstate object. This is normally called at the end of the lonwizard::state constructor, so you should not normally need it as a user.
   
 =cut  =cut
   
Line 197  sub changeState { Line 238  sub changeState {
   
 =pod  =pod
   
 =item B<display>(): This is the main method that the handler using the wizard calls.  =item B<display>(): This is the main method that the handler using the wizard calls. It must always be called, and called last, because it takes care of closing a hash that needs to be closed.
   sxsd
 =cut  =cut
   
 # Done in five phases  # Done in four phases
 # 1: Do the post processing for the previous state.  # 1: Do the post processing for the previous state.
 # 2: Do the preprocessing for the current state.  # 2: Do the preprocessing for the current state.
 # 3: Check to see if state changed, if so, postprocess current and move to next.  # 3: Check to see if state changed, if so, postprocess current and move to next.
Line 213  sub display { Line 254  sub display {
     my $result = "";      my $result = "";
   
     # Phase 1: Post processing for state of previous screen (which is actually      # Phase 1: Post processing for state of previous screen (which is actually
     # the current state), if it wasn't the beginning state.      # the "current state" in terms of the wizard variables), if it wasn't the 
     if ($self->{STATE} ne "START" || $ENV{"form.SUBMIT"} eq "Next ->")      # beginning state.
     {      if ($self->{STATE} ne "START" || $ENV{"form.SUBMIT"} eq "Next ->") {
  my $prevState = $self->{STATES}{$self->{STATE}};   my $prevState = $self->{STATES}{$self->{STATE}};
             $prevState->postprocess();              $prevState->postprocess();
     }      }
Line 228  sub display { Line 269  sub display {
     my $startState = $self->{STATE};      my $startState = $self->{STATE};
     my $state = $self->{STATES}{$startState};      my $state = $self->{STATES}{$startState};
           
     # Error checking      # Error checking; it is intended that the developer will have
       # checked all paths and the user can't see this!
     if (!defined($state)) {      if (!defined($state)) {
         $result .="Error! The state ". $startState ." is not defined.";          $result .="Error! The state ". $startState ." is not defined.";
         return $result;          return $result;
Line 268  HEADER Line 310  HEADER
   
     if (!$state->overrideForm()) {      if (!$state->overrideForm()) {
         $result .= '<center>';          $result .= '<center>';
         if ($self->{STATE} ne $self->{START_STATE})          if ($self->{STATE} ne $self->{START_STATE}) {
         {  
             #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';              #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';
         }          }
         if ($self->{DONE})          if ($self->{DONE}) {
         {  
             my $returnPage = $self->{RETURN_PAGE};              my $returnPage = $self->{RETURN_PAGE};
             $result .= "<a href=\"$returnPage\">End Wizard</a>";              $result .= "<a href=\"$returnPage\">End Wizard</a>";
         }          }
         else          else {
         {  
             $result .= '<input name="back" type="button" ';              $result .= '<input name="back" type="button" ';
             $result .= 'value="&lt;- Previous" onclick="history.go(-1)" /> ';              $result .= 'value="&lt;- Previous" onclick="history.go(-1)" /> ';
             $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" />';              $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" />';
Line 295  HEADER Line 334  HEADER
 </html>  </html>
 FOOTER  FOOTER
   
       # Handle writing out the vars to the file
       my $file = Apache::File->new('>'.$self->{FILENAME});
       print $file $self->_varsInFile();
   
     return $result;      return $result;
 }  }
   
Line 328  sub getVars { Line 371  sub getVars {
   
 =cut  =cut
   
 # This may look trivial, but it's here as a hook for possible later processing  
 sub setVar {  sub setVar {
     my $self = shift;      my $self = shift;
     my $key = shift;      my $key = shift;
     my $val = shift;      my $val = shift;
     $self->{VARS}{$key} = $val;      $self->{VARS}->{$key} = $val;
 }  }
   
 =pod  =pod
Line 345  sub setVar { Line 387  sub setVar {
 sub queryStringVars {  sub queryStringVars {
     my $self = shift;      my $self = shift;
   
       my @storedVars = ('STATE', 'TOKEN', 'RETURN_PAGE');
     my @queryString = ();      my @queryString = ();
           
     for my $varname (keys %{$self->{VARS}}) {      push @queryString, 'TOKEN=' .
         push @queryString, Apache::lonnet::escape($varname) . "=" .          Apache::lonnet::escape($self->{TOKEN});
             Apache::lonnet::escape($self->{VARS}{$varname});  
     }  
     push @queryString, 'CURRENT_STATE=' . Apache::lonnet::escape($self->{STATE});      push @queryString, 'CURRENT_STATE=' . Apache::lonnet::escape($self->{STATE});
     push @queryString, 'RETURN_PAGE=' . Apache::lonnet::escape($self->{RETURN_PAGE});      push @queryString, 'RETURN_PAGE=' . Apache::lonnet::escape($self->{RETURN_PAGE});
   
Line 363  sub queryStringVars { Line 404  sub queryStringVars {
   
 =cut  =cut
   
   
 # A temp function for debugging  # A temp function for debugging
 sub handler {  sub handler {
     my $r = shift;      my $r = shift;
Line 413  WIZBEGIN Line 453  WIZBEGIN
        ["map", "Every problem in a particular <b>folder</b>", "CHOOSE_FOLDER"],         ["map", "Every problem in a particular <b>folder</b>", "CHOOSE_FOLDER"],
        ["resource", "One particular <b>problem</b>", "CHOOSE_RESOURCE"]],         ["resource", "One particular <b>problem</b>", "CHOOSE_RESOURCE"]],
                                          "Which problems do you wish to change a date for?");                                           "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_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(); });      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}};      my $levelType = $levelTypeHash{$wizard->{VARS}->{GRANULARITY}};
     Apache::lonwizard::switch_state->new($wizard, "CHOOSE_ACTION", "Parameter Type", "ACTION_TYPE", [       Apache::lonwizard::switch_state->new($wizard, "CHOOSE_ACTION", "Parameter Type", "ACTION_TYPE", [ 
        ["open_date", "Set an <b>open date</b>", "CHOOSE_DATE"],          ["open_date", "Set an <b>open date</b>", "CHOOSE_DATE"], 
Line 429  WIZBEGIN Line 469  WIZBEGIN
        ["student", ". . . for an individual <b>student</b>", "CHOOSE_STUDENT"]],         ["student", ". . . for an individual <b>student</b>", "CHOOSE_STUDENT"]],
                                        "Set $dateType of $levelType for. . .");                                         "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_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::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");      Apache::lonwizard::parmwizfinal->new($wizard, "FINISH", "Confirm Selection");
   
     $r->print($wizard->display());      $r->print($wizard->display());
Line 522  sub process_multiple_choices { Line 562  sub process_multiple_choices {
     my $var = shift;      my $var = shift;
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
   
     my $formvalue = $ENV{'form.' . $var};      my $formvalue = $ENV{'form.' . $formname};
     if ($formvalue) {      if ($formvalue) {
         # Must extract values from $wizard->{DATA} directly, as there          # Must extract values from $wizard->{DATA} directly, as there
         # may be more then one.          # may be more then one.
         my @values;          my @values;
         for my $formparam (split (/&/, $wizard->{DATA})) {          for my $formparam (split (/&/, $wizard->{DATA})) {
             my ($name, $value) = split(/=/, $formparam);              my ($name, $value) = split(/=/, $formparam);
             if ($name ne $var) {              if ($name ne $formname) {
                 next;                  next;
             }              }
             $value =~ tr/+/ /;              $value =~ tr/+/ /;
Line 626  If there is only one choice, the state w Line 666  If there is only one choice, the state w
   
 =over 4  =over 4
   
 =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.  =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, nextState, varName, choiceHash, multichoice): messageBefore is the HTML text that will be displayed before the choice display. Keys will be sorted according to human name. nextState is the state to proceed to after the choice. varName is the name of the wizard var to store the computer_name answer in. choiceHash is the hash described above. It is optional because you may override it. multichoice is true if the user can make multiple choices, false otherwise. (Multiple choices will be seperated with ||| in the wizard variable.
   
 =back  =back
   
Line 638  sub new { Line 678  sub new {
     my $self = bless $proto->SUPER::new(shift, shift, shift);      my $self = bless $proto->SUPER::new(shift, shift, shift);
   
     $self->{MESSAGE_BEFORE} = shift;      $self->{MESSAGE_BEFORE} = shift;
     $self->{MESSAGE_AFTER} = shift;  
     $self->{NEXT_STATE} = shift;      $self->{NEXT_STATE} = shift;
     $self->{VAR_NAME} = shift;      $self->{VAR_NAME} = shift;
     $self->{CHOICE_HASH} = shift;      $self->{CHOICE_HASH} = shift;
       $self->{MULTICHOICE} = shift;
     $self->{NO_CHOICES} = 0;      $self->{NO_CHOICES} = 0;
           
     return $self;      return $self;
Line 679  sub preprocess { Line 719  sub preprocess {
 }  }
   
 sub determineChoices {  sub determineChoices {
     return {"NO_CHOICE" => "No choices were given."};      # Return no choices, which will terminate the wizard
       return {};
 }  }
   
 sub render {   sub render { 
     my $self = shift;      my $self = shift;
     my $result = "";      my $result = "";
     my $var = $self->{VAR_NAME};      my $var = $self->{VAR_NAME};
       my $buttons = '';
   
       if ($self->{MULTICHOICE}) {
           $result = <<SCRIPT;
   <script>
       function checkall(value) {
    for (i=0; i<document.forms.wizform.elements.length; i++) {
               document.forms.wizform.elements[i].checked=value;
           }
       }
   </script>
   SCRIPT
           $buttons = <<BUTTONS;
   <input type="button" onclick="checkall(true)" value="Select All" />
   <input type="button" onclick="checkall(false)" value="Unselect All" />
   <br />
   BUTTONS
       }
           
     if (defined $self->{ERROR_MSG}) {      if (defined $self->{ERROR_MSG}) {
         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';          $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
     }      }
Line 695  sub render { Line 754  sub render {
  $result .= $self->{MESSAGE_BEFORE} . '<br /><br />';   $result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
     }      }
   
       $result .= $buttons;
   
     my $choices = $self->{CHOICE_HASH};      my $choices = $self->{CHOICE_HASH};
     my @keys = keys (%$choices);      my @keys = keys (%$choices);
   
     $result .= "<select name=\"$var.forminput\" size=\"10\">\n";      my $type = "radio";
     foreach (@keys)      if ($self->{MULTICHOICE}) { $type = 'checkbox'; }
     {      foreach (@keys) {
  $result .= "<option value=\"" . HTML::Entities::encode($choices->{$_})           
             . "\">" . HTML::Entities::encode($_) . "</option>\n";          $result .= "<input type='$type' name='" .
     }              $self->{VAR_NAME} . '.forminput' .
     $result .= "</select>\n\n";              "' value=\"" . 
               HTML::Entities::encode($choices->{$_}) 
     if (defined $self->{MESSAGE_AFTER})              . "\"/> " . HTML::Entities::encode($_) . "<br />\n";
     {  
  $result .= '<br /><br />' . $self->{MESSAGE_AFTER};  
     }      }
   
     return $result;      return $result;
Line 719  sub postprocess { Line 778  sub postprocess {
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
     my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};      my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
     if ($formvalue) {      if ($formvalue) {
         # Value already stored by Wizard          if ($self->{MULTICHOICE}) {
               $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
                                               $self->{VAR_NAME});
           }
           # For non-multichoice, value already stored by Wizard
         $wizard->changeState($self->{NEXT_STATE});          $wizard->changeState($self->{NEXT_STATE});
     } else {      } else {
         $self->{ERROR_MSG} = "Can't continue the wizard because you must make"          $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
Line 744  Each choice may have arbitrary HTML asso Line 807  Each choice may have arbitrary HTML asso
   
 =over 4  =over 4
   
 =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.  =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, varName, choiceList, messageBefore): varName is the name of the wizard variable the state will set with the choice made. choiceHash is list reference of a list of list references to three element lists, where the first element is what the wizard var varName will be set to, the second is the HTML that will be displayed for that choice, and the third is the destination state. The special setting 'ILLEGAL' can be used in the first place to state that it is not a legal chocie (see lonprintout.pm for real-life usage of that). messageBefore is an optional HTML string that will be placed before the message.
   
   Note that ILLEGAL is desirable because some choices may not always be good choices, but they should not necessarily disappear with no explanantion of why they are no good. In lonprintout.pm, for instance, the choice "Problems from current sequence" may be no good because there are no problems in the sequence, but it should not silently disappear; it should announce that there are no problems in the sequence.
   
 An example of a legit choiceList: C<my $choicelist = [ ["flunk", "Flunk Student", "FLUNK_STATE"], ["pass", "Pass Student", "PASS_STATE"] ];>  An example of a legit choiceList: C<my $choicelist = [ ["flunk", "Flunk Student", "FLUNK_STATE"], ["pass", "Pass Student", "PASS_STATE"]  ];>
   
 =back  =back
   
Line 760  sub new { Line 825  sub new {
     $self->{VAR_NAME} = shift;      $self->{VAR_NAME} = shift;
     $self->{CHOICE_LIST} = shift;      $self->{CHOICE_LIST} = shift;
     $self->{MESSAGE_BEFORE} = shift;      $self->{MESSAGE_BEFORE} = shift;
     $self->{MESSAGE_AFTER} = shift;  
   
     return $self;      return $self;
 }  }
Line 776  sub render { Line 840  sub render {
   
     $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});      $result .= $self->{MESSAGE_BEFORE} if (defined $self->{MESSAGE_BEFORE});
   
     if (!$curVal) {  
         $curVal = $self->{CHOICE_LIST}->[0]->[0]; # top is default  
     }  
   
     $result .= "<table>\n\n";      $result .= "<table>\n\n";
   
     foreach my $choice (@choices)      my $checked = 0;
     {      foreach my $choice (@choices) {
  my $value = $choice->[0];   my $value = $choice->[0];
  my $text = $choice->[1];   my $text = $choice->[1];
           
  $result .= "<tr>\n<td width='20'>&nbsp;</td>\n<td>";   $result .= "<tr>\n<td width='20'>&nbsp;</td>\n<td>";
  $result .= "<td valign=\"top\"><input type=\"radio\" name=\"$var.forminput\"";   $result .= "<td valign=\"top\"><input type=\"radio\" name=\"$var.forminput\"";
  $result .= " checked" if ($value eq $curVal);          if (!$checked) {
               $result .= " checked";
               $checked = 1;
           }
  $result .= " value=\"$value\"></td>\n<td>$text</td>\n</tr>\n\n";   $result .= " value=\"$value\"></td>\n<td>$text</td>\n</tr>\n\n";
     }      }
   
     $result .= "<table>\n\n";      $result .= "<table>\n\n";
   
     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});  
   
     return $result;      return $result;
 }  }
   
Line 806  sub postprocess { Line 867  sub postprocess {
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
     my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . '.forminput'};      my $chosenValue = $ENV{"form." . $self->{VAR_NAME} . '.forminput'};
   
     foreach my $choice (@{$self->{CHOICE_LIST}})      foreach my $choice (@{$self->{CHOICE_LIST}}) {
     {   if ($choice->[0] eq $chosenValue) {
  if ($choice->[0] eq $chosenValue)  
  {  
     $wizard->changeState($choice->[2]);      $wizard->changeState($choice->[2]);
  }   }
     }      }
Line 821  sub preprocess { Line 880  sub preprocess {
     my $choiceList = $self->{CHOICE_LIST};      my $choiceList = $self->{CHOICE_LIST};
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
           
     if (scalar(@{$choiceList}) == 1)      if (scalar(@{$choiceList}) == 1) {
     {  
  my $choice = $choiceList->[0];   my $choice = $choiceList->[0];
  my $chosenVal = $choice->[0];   my $chosenVal = $choice->[0];
  my $nextState = $choice->[2];   my $nextState = $choice->[2];
Line 856  Date state provides a state for selectin Line 914  Date state provides a state for selectin
   
 =over 4  =over 4
   
 =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.  =item overriddent method B<new>(parentLonWizReference, stateName, stateTitle, varName, nextState, messageBefore, displayJustDate): varName is where the date/time will be stored as seconds since the epoch. messageBefore and messageAfter as other states. displayJustDate is a flag defaulting to false that if true, will only display the date selection (defaulting to midnight on that date). Otherwise, minutes and hours will be shown.
   
 =back  =back
   
Line 870  sub new { Line 928  sub new {
     $self->{VAR_NAME} = shift;      $self->{VAR_NAME} = shift;
     $self->{NEXT_STATE} = shift;      $self->{NEXT_STATE} = shift;
     $self->{MESSAGE_BEFORE} = shift;      $self->{MESSAGE_BEFORE} = shift;
     $self->{MESSAGE_AFTER} = shift;  
     $self->{DISPLAY_JUST_DATE} = shift;      $self->{DISPLAY_JUST_DATE} = shift;
     if (!defined($self->{DISPLAY_JUST_DATE})) {$self->{DISPLAY_JUST_DATE} = 0;}      if (!defined($self->{DISPLAY_JUST_DATE})) {$self->{DISPLAY_JUST_DATE} = 0;}
     return $self;      return $self;
Line 885  sub render { Line 942  sub render {
   
     my $date;      my $date;
           
     # Pick default date: Now, or previous choice      # Default date: Now
     if (defined ($wizvars->{$var}) && $wizvars->{$var} ne "")      $date = localtime();
     {       $date->min = 0;
  $date = localtime($wizvars->{$var});  
     }  
     else  
     {  
  $date = localtime();  
     }  
   
     if (defined $self->{ERROR_MSG}) {      if (defined $self->{ERROR_MSG}) {
         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';          $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
Line 1029  sub postprocess { Line 1080  sub postprocess {
 package Apache::lonwizard::parmwizfinal;  package Apache::lonwizard::parmwizfinal;
   
 # This is the final state for the parmwizard. It is not generally useful,  # This is the final state for the parmwizard. It is not generally useful,
 # so it is not perldoc'ed. It does it's own processing.  # so it is not perldoc'ed. It does its own processing.
   
 no strict;  no strict;
 @ISA = ('Apache::lonwizard::state');  @ISA = ('Apache::lonwizard::state');
Line 1167  Note this state will not automatically a Line 1218  Note this state will not automatically a
   
 =over 4  =over 4
   
 =item overridden 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.  =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, nextState, varName, filterFunction, choiceFunction): messageBefore and messageAfter appear before and after the state choice, respectively. nextState is the state to proceed to after the choice. varName is the wizard variable to store the choice in.
   
 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.  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.
   
Line 1185  sub new { Line 1236  sub new {
     my $self = bless $proto->SUPER::new(shift, shift, shift);      my $self = bless $proto->SUPER::new(shift, shift, shift);
   
     $self->{MESSAGE_BEFORE} = shift;      $self->{MESSAGE_BEFORE} = shift;
     $self->{MESSAGE_AFTER} = shift;  
     $self->{NEXT_STATE} = shift;      $self->{NEXT_STATE} = shift;
     $self->{VAR_NAME} = shift;      $self->{VAR_NAME} = shift;
     $self->{FILTER_FUNC} = shift;      $self->{FILTER_FUNC} = shift;
Line 1230  sub postprocess { Line 1280  sub postprocess {
 # it renders the same states, so it doesn't go in just this state, and  # it renders the same states, so it doesn't go in just this state, and
 # you can lean on the browser back button to make sure it all chains  # you can lean on the browser back button to make sure it all chains
 # correctly.  # correctly.
   # Either that, or force all folders open and don't allow the user
   # to close them.
   
 sub render {  sub render {
     my $self = shift;      my $self = shift;
Line 1257  sub render { Line 1309  sub render {
     my $choiceFunc = $self->{CHOICE_FUNC};      my $choiceFunc = $self->{CHOICE_FUNC};
   
     # Create the composite function that renders the column on the nav map      # Create the composite function that renders the column on the nav map
       # have to admit any language that lets me do this can't be all bad
       #  - Jeremy (Pythonista) ;-)
       my $checked = 0;
     my $renderColFunc = sub {      my $renderColFunc = sub {
         my ($resource, $part, $params) = @_;          my ($resource, $part, $params) = @_;
                   
Line 1264  sub render { Line 1319  sub render {
             return '<td>&nbsp;</td>';              return '<td>&nbsp;</td>';
         } else {          } else {
             my $col = "<td><input type='radio' name='${var}.forminput' ";              my $col = "<td><input type='radio' name='${var}.forminput' ";
             if ($vals->{$resource->{ID}}) {              if (!$checked) {
                 $col .= "checked ";                  $col .= "checked ";
                   $checked = 1;
             }              }
             $col .= "value='" . $resource->{ID} . "' /></td>";              $col .= "value='" . $resource->symb() . "' /></td>";
             return $col;              return $col;
         }          }
     };      };
Line 1280  sub render { Line 1336  sub render {
                                        'url' => $wizard->{URL},                                         'url' => $wizard->{URL},
                                        'filterFunc' => $filterFunc } );                                         'filterFunc' => $filterFunc } );
                                                                                                   
     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});  
   
     return $result;      return $result;
 }  }
           
Line 1299  Note this state will not automatically a Line 1353  Note this state will not automatically a
   
 This is generally intended for use on a specific sequence, not the entire course, as for technical reasons the user can't open and close folders, so they must all be shown as open. To fix this would require making the folders image form submitters and remembering the selected state of each resource, which is not impossible but is too much error-prone work to do until it seems many people will want that feature.  This is generally intended for use on a specific sequence, not the entire course, as for technical reasons the user can't open and close folders, so they must all be shown as open. To fix this would require making the folders image form submitters and remembering the selected state of each resource, which is not impossible but is too much error-prone work to do until it seems many people will want that feature.
   
   Note this class is generally useful for multi-choice selections, by overridding "determineChoices" to return the choice hash.
   
 =over 4  =over 4
   
 =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, filterFunction, choiceFunction, map): Arguments like resource_choice. map is the ID number of a specific map that, if given is all that will be shown to the user, instead of the whole course.  =item overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, nextState, varName, filterFunction, choiceFunction, map): Arguments like resource_choice. map is the ID number of a specific map that, if given is all that will be shown to the user, instead of the whole course.
   
 =back  =back
   
Line 1317  sub new { Line 1373  sub new {
     my $self = bless $proto->SUPER::new(shift, shift, shift);      my $self = bless $proto->SUPER::new(shift, shift, shift);
   
     $self->{MESSAGE_BEFORE} = shift;      $self->{MESSAGE_BEFORE} = shift;
     $self->{MESSAGE_AFTER} = shift;  
     $self->{NEXT_STATE} = shift;      $self->{NEXT_STATE} = shift;
     $self->{VAR_NAME} = shift;      $self->{VAR_NAME} = shift;
     $self->{FILTER_FUNC} = shift;      $self->{FILTER_FUNC} = shift;
Line 1405  BUTTONS Line 1460  BUTTONS
   
     $result .= $buttons;      $result .= $buttons;
   
     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});  
   
     return $result;      return $result;
 }  }
 1;  1;
Line 1414  BUTTONS Line 1467  BUTTONS
 package Apache::lonwizard::choose_student;  package Apache::lonwizard::choose_student;
   
 no strict;  no strict;
 @ISA = ("Apache::lonwizard::choice_state");  @ISA = ("Apache::lonwizard::state");
 use strict;  use strict;
   
 sub new {  sub new {
     my $proto = shift;      my $proto = shift;
     my $class = ref($proto) || $proto;      my $class = ref($proto) || $proto;
     my $self = bless $proto->SUPER::new(shift, shift, shift, shift,      my $self = bless $proto->SUPER::new(shift, shift, shift);
                                         shift, shift, shift);  
       $self->{MESSAGE_BEFORE} = shift;
       $self->{NEXT_STATE} = shift;
       $self->{VAR_NAME} = shift;
       $self->{MULTICHOICE} = shift;
   
     return $self;      return $self;
 }  }
   
 sub determineChoices {  sub render {
     my %choices;      my $self = shift;
       my $result = '';
       my $var = $self->{VAR_NAME};
       my $buttons = '';
   
     my $classlist = Apache::loncoursedata::get_classlist();      if ($self->{MULTICHOICE}) {
     foreach (keys %$classlist) {          $result = <<SCRIPT;
         $choices{$classlist->{$_}->[6]} = $_;  <script>
       function checkall(value) {
    for (i=0; i<document.forms.wizform.elements.length; i++) {
               document.forms.wizform.elements[i].checked=value;
           }
     }      }
       </script>
     return \%choices;  SCRIPT
           $buttons = <<BUTTONS;
   <input type="button" onclick="checkall(true)" value="Select All" />
   <input type="button" onclick="checkall(false)" value="Unselect All" />
   <br />
   BUTTONS
       }
   
       if (defined $self->{ERROR_MSG}) {
           $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
       }
   
       if (defined $self->{MESSAGE_BEFORE}) {
           $result .= $self->{MESSAGE_BEFORE} . '<br /><br />';
       }
   
       $result .= $buttons;
   
       my $choices = &Apache::loncoursedata::get_classlist();
   
       my @keys = keys %{$choices};
       # Sort by: Section, name
   
       my $section = Apache::loncoursedata::CL_SECTION();
       my $fullname = Apache::loncoursedata::CL_FULLNAME();
   
       @keys = sort {
           if ($choices->{$a}->[$section] ne $choices->{$b}->[$section]) {
               return $choices->{$a}->[$section] cmp $choices->{$b}->[$section];
           }
           return $choices->{$a}->[$fullname] cmp $choices->{$b}->[$fullname];
       } @keys;
   
       my $type = 'radio';
       if ($self->{MULTICHOICE}) { $type = 'checkbox'; }
       $result .= "<table cellspacing='2' cellpadding='2' border='0'>\n";
       $result .= "<tr><td></td><td align='center'><b>Student Name</b></td>".
           "<td align='center'><b>Section</b></td></tr>";
   
       foreach (@keys) {
           $result .= "<tr><td><input type='$type' name='" .
               $self->{VAR_NAME} . '.forminput' .
               "' value='" . HTML::Entities::encode($_)
               . "' /></td><td>"
               . HTML::Entities::encode($choices->{$_}->[$fullname])
               . "</td><td align='center'>" 
               . HTML::Entities::encode($choices->{$_}->[$section])
               . "</td></tr>\n";
       }
   
       $result .= "</table>\n\n";
       $result .= $buttons;
   
       return $result;
 }  }
   
   sub postprocess {
       my $self = shift;
       my $wizard = $self->{WIZARD};
       my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};
       if ($formvalue) {
           if ($self->{MULTICHOICE}) {
               $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
                                               $self->{VAR_NAME});
           }
           $wizard->changeState($self->{NEXT_STATE});
       } else {
           $self->{ERROR_MSG} = "Can't continue the wizard because you must make"
               . ' a selection to continue.';
       }
       return 1;
   }
   
   
 1;  1;
   
 package Apache::lonwizard::choose_section;  package Apache::lonwizard::choose_section;
Line 1480  choose_file offers a choice of files fro Line 1616  choose_file offers a choice of files fro
   
 =over 4  =over 4
   
 =item * overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, subdir, filterFunc): As in previous states, where filterFunc is as described in choose_file. subdir is the name of the subdirectory to offer choices from.  =item * overridden method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, nextState, varName, subdir, filterFunc): As in previous states, where filterFunc is as described in choose_file. subdir is the name of the subdirectory to offer choices from.
   
 =back  =back
   
Line 1496  sub new { Line 1632  sub new {
     my $self = bless $proto->SUPER::new(shift, shift, shift);      my $self = bless $proto->SUPER::new(shift, shift, shift);
           
     $self->{MESSAGE_BEFORE} = shift;      $self->{MESSAGE_BEFORE} = shift;
     $self->{MESSAGE_AFTER} = shift;  
     $self->{NEXT_STATE} = shift;      $self->{NEXT_STATE} = shift;
     $self->{VAR_NAME} = shift;      $self->{VAR_NAME} = shift;
     $self->{SUB_DIR} = shift;      $self->{SUB_DIR} = shift;
Line 1585  BUTTONS Line 1720  BUTTONS
   
     $result .= $buttons;      $result .= $buttons;
   
     if ($self->{MESSAGE_AFTER}) {  
         $result .= "<br /><br />" . $self->{MESSAGE_AFTER};  
     }  
   
     return $result;      return $result;
 }  }
   

Removed from v.1.12  
changed lines
  Added in v.1.19


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