Diff for /loncom/interface/Attic/lonwizard.pm between versions 1.10 and 1.13

version 1.10, 2003/02/20 22:10:06 version 1.13, 2003/02/27 19:42:59
Line 30  use Apache::loncommon; Line 30  use Apache::loncommon;
   
 =head1 Class: lonwizard  =head1 Class: lonwizard
   
   FIXME: Doc the parameters of the wizard well: Title, Data (Query string), URL.
   
 =head2 lonwizard Attributes  =head2 lonwizard Attributes
   
 =over 4  =over 4
Line 59  sub new { Line 61  sub new {
   
     $self->{TITLE} = shift;      $self->{TITLE} = shift;
     $self->{DATA} = shift;      $self->{DATA} = shift;
       $self->{URL} = shift;
     &Apache::loncommon::get_unprocessed_cgi($self->{DATA});      &Apache::loncommon::get_unprocessed_cgi($self->{DATA});
   
   
Line 132  sub declareVars { Line 135  sub declareVars {
   
  # if there's a form in the env, use that instead   # if there's a form in the env, use that instead
  my $envname = "form." . $element;   my $envname = "form." . $element;
  if (defined ($ENV{$envname}))   if (defined ($ENV{$envname})) {
  {  
     $self->{VARS}->{$element} = $ENV{$envname};      $self->{VARS}->{$element} = $ENV{$envname};
  }   }
                   
Line 215  sub display { Line 217  sub display {
     if ($self->{STATE} ne "START" || $ENV{"form.SUBMIT"} eq "Next ->")      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();
     }      }
           
     # Note, to handle errors in a state's input that a user must correct,      # Note, to handle errors in a state's input that a user must correct,
Line 470  These methods should be overridden in de Line 472  These methods should be overridden in de
   
 =item B<render>(): render returns a string of itself to be rendered to the screen, which the wizard will display.  =item B<render>(): render returns a string of itself to be rendered to the screen, which the wizard will display.
   
 =back  
   
 =cut   =cut 
   
 package Apache::lonwizard::state;  package Apache::lonwizard::state;
Line 508  sub preprocess { Line 508  sub preprocess {
     return 1;      return 1;
 }  }
   
   =pod
   
   =item * B<process_multiple_choices>(formname, var_name): A service function that correctly handles resources with multiple selections, such as checkboxes. It delimits the selections with triple pipes and stores them in the given wizard variable. 'formname' is the name of the form element to process.
   
   =back
   
   =cut 
   
   sub process_multiple_choices {
       my $self = shift;
       my $formname = shift;
       my $var = shift;
       my $wizard = $self->{WIZARD};
   
       my $formvalue = $ENV{'form.' . $var};
       if ($formvalue) {
           # Must extract values from $wizard->{DATA} directly, as there
           # may be more then one.
           my @values;
           for my $formparam (split (/&/, $wizard->{DATA})) {
               my ($name, $value) = split(/=/, $formparam);
               if ($name ne $var) {
                   next;
               }
               $value =~ tr/+/ /;
               $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
               push @values, $value;
           }
           $wizard->setVar($var, join('|||', @values));
       }
       
       return;
   }
   
 sub render {  sub render {
     return "This is the empty state. If you can see this, it's a bug.\n"      return "This is the empty state. If you can see this, it's a bug.\n"
 }  }
Line 592  If there is only one choice, the state w Line 626  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, messageAfter, nextState, varName, choiceHash, multichoice): 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. 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 608  sub new { Line 642  sub new {
     $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 652  sub render { Line 687  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 661  sub render { Line 714  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 $multichoice = '';
       if ($self->{MULTICHOICE}) {
           $multichoice = 'multichoice="true" ';
       }
   
       my $type = "radio";
       if ($self->{MULTICHOICE}) { $type = 'checkbox'; }
     foreach (@keys)      foreach (@keys)
     {      {
  $result .= "<option value=\"" . HTML::Entities::encode($choices->{$_})           
             . "\">" . HTML::Entities::encode($_) . "</option>\n";          $result .= "<input type='$type' name='" .
               $self->{VAR_NAME} . '.forminput' .
               "' value=\"" . 
               HTML::Entities::encode($choices->{$_}) 
               . "\"/> " . HTML::Entities::encode($_) . "<br />\n";
     }      }
     $result .= "</select>\n\n";  
   
     if (defined $self->{MESSAGE_AFTER})      if (defined $self->{MESSAGE_AFTER})
     {      {
Line 710  Each choice may have arbitrary HTML asso Line 774  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, 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. 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, messageAfter an optional HTML string that will be placed before.
   
   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 1196  sub postprocess { Line 1262  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 1243  sub render { Line 1311  sub render {
                                                   Apache::lonnavmaps::resource()],                                                    Apache::lonnavmaps::resource()],
                                        'showParts' => 0,                                         'showParts' => 0,
                                        'queryString' => $wizard->queryStringVars() . '&folderManip=1',                                         'queryString' => $wizard->queryStringVars() . '&folderManip=1',
                                        'url' => '/adm/wizard',                                         'url' => $wizard->{URL},
                                        'filterFunc' => $filterFunc } );                                         'filterFunc' => $filterFunc } );
                                                                                                   
     $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});      $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
Line 1265  Note this state will not automatically a Line 1333  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, 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.
Line 1304  sub postprocess { Line 1374  sub postprocess {
     my $self = shift;      my $self = shift;
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
   
     my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};      $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
     if ($formvalue) {                                      $self->{VAR_NAME});
         # Must extract values from $wizard->{DATA} directly, as there  
         # may be more then one.  
         my @values;  
         for my $formparam (split (/&/, $wizard->{DATA})) {  
             my ($name, $value) = split(/=/, $formparam);  
             if ($name ne $self->{VAR_NAME} . '.forminput') {  
                 next;  
             }  
             $value =~ tr/+/ /;  
             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;  
             push @values, $value;  
         }  
         $wizard->setVar($self->{VAR_NAME}, join('|||', @values));  
     }      
   
     # If nothing was selected...      # If nothing was selected...
     if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {      if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
Line 1401  sub new { Line 1457  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, shift);                                          shift, shift, shift, undef, shift);
     return $self;      return $self;
 }  }
   
Line 1576  sub postprocess { Line 1632  sub postprocess {
     my $self = shift;      my $self = shift;
     print $self->{NEXT_STATE};      print $self->{NEXT_STATE};
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
     my $formvalue = $ENV{'form.' . $self->{VAR_NAME} . '.forminput'};  
   
     if ($formvalue) {      $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
         # Must extract values from $wizard->{DATA} directly, as there                                      $self->{VAR_NAME});
         # may be more then one.      
         my @values;      if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
         for my $formparam (split (/&/, $wizard->{DATA})) {  
             my ($name, $value) = split(/=/, $formparam);  
             if ($name ne $self->{VAR_NAME} . '.forminput') {  
                 next;  
             }  
             $value =~ tr/+/ /;  
             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;  
             push @values, $value;  
         }  
         $wizard->setVar($self->{VAR_NAME}, join('|||', @values));  
         $wizard->changeState($self->{NEXT_STATE});  
     } else {  
         $self->{ERROR_MSG} = "Can't continue the wizard because you ".          $self->{ERROR_MSG} = "Can't continue the wizard because you ".
             "must make a selection to continue.";              "must make a selection to continue.";
     }      }

Removed from v.1.10  
changed lines
  Added in v.1.13


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