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

version 1.8, 2003/02/13 23:46:27 version 1.13, 2003/02/27 19:42:59
Line 24  All classes are in the Apache::lonwizard Line 24  All classes are in the Apache::lonwizard
 use strict;  use strict;
   
 use HTML::Entities;  use HTML::Entities;
   use Apache::loncommon;
   
 =pod  =pod
   
 =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 45  use HTML::Entities; Line 48  use HTML::Entities;
   
 =item B<DONE>: A boolean value, true if the wizard has completed.  =item B<DONE>: A boolean value, true if the wizard has completed.
   
   =item B<DATA>: The data the wizard is drawing from, which will be passed to Apache::loncommon::get_unprocessed_cgi, and may be used by states that do multi-selection.
   
 =back  =back
   
 =cut  =cut
Line 54  sub new { Line 59  sub new {
     my $class = ref($proto) || $proto;      my $class = ref($proto) || $proto;
     my $self = {};      my $self = {};
   
       $self->{TITLE} = shift;
       $self->{DATA} = shift;
       $self->{URL} = shift;
       &Apache::loncommon::get_unprocessed_cgi($self->{DATA});
   
   
     # If there is a state from the previous form, use that. If there is no      # If there is a state from the previous form, use that. If there is no
     # state, use the start state parameter.      # state, use the start state parameter.
     if (defined $ENV{"form.CURRENT_STATE"})      if (defined $ENV{"form.CURRENT_STATE"})
Line 76  sub new { Line 87  sub new {
  $self->{RETURN_PAGE} = $ENV{REFERER};   $self->{RETURN_PAGE} = $ENV{REFERER};
     }      }
   
     $self->{TITLE} = shift;  
     $self->{STATES} = {};      $self->{STATES} = {};
     $self->{VARS} = {};      $self->{VARS} = {};
     $self->{HISTORY} = {};      $self->{HISTORY} = {};
     $self->{DONE} = 0;      $self->{DONE} = 0;
   
     bless($self, $class);      bless($self, $class);
     return $self;      return $self;
 }  }
Line 93  sub new { Line 104  sub new {
   
 =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.  =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.
   
 =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.  =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. 
   
 =over 2  =over 2
   
 =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".  =item * B<Note>: These form 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". If you use the variable name followed by '.forminput', the wizard will automatically store the user's choice in the appropriate form variable.
   
   =item * B<Note>: If you want to preserve incoming form values, such as ones from the remote, you can simply declare them and the wizard will automatically preserve them. For instance, you might want to store 'url' or 'postdata' from the remote; see lonprintout for example.
   
 =back  =back
   
Line 122  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};
  }   }
                   
         # If there's an incoming form submission, use that          # If there's an incoming form submission, use that
         my $envname = "form." . $element . ".forminput";          $envname = "form." . $element . ".forminput";
         if (defined ($ENV{$envname})) {          if (defined ($ENV{$envname})) {
             $self->{VARS}->{$element} = $ENV{$envname};              $self->{VARS}->{$element} = $ENV{$envname};
         }          }
Line 205  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 243  sub display { Line 255  sub display {
     </head>      </head>
     $bodytag      $bodytag
 HEADER  HEADER
     if (!$state->overrideForm()) { $result.="<form method='GET'>"; }      if (!$state->overrideForm()) { $result.="<form name='wizform' method='GET'>"; }
     $result .= <<HEADER;      $result .= <<HEADER;
         <table border="0"><tr><td>          <table border="0"><tr><td>
         <h2><i>$stateTitle</i></h2>          <h2><i>$stateTitle</i></h2>
Line 460  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 498  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 582  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 598  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 642  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 />';
     }      }
   
     if (defined $self->{MESSAGE_BEFORE})      if (defined $self->{MESSAGE_BEFORE}) {
     {  
  $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 701  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 1016  sub render { Line 1091  sub render {
                         'due_date' => "0_duedate",                          'due_date' => "0_duedate",
                         'answer_date' => "0_answerdate");                          'answer_date' => "0_answerdate");
           
     my $result = "<form method='get' action='/adm/parmset'>\n";      my $result = "<form name='wizform' method='get' action='/adm/parmset'>\n";
     $result .= '<p>Confirm that this information is correct, then click &quot;Finish Wizard&quot; to complete setting the parameter.<ul>';      $result .= '<p>Confirm that this information is correct, then click &quot;Finish Wizard&quot; to complete setting the parameter.<ul>';
     my $affectedResourceId = "";      my $affectedResourceId = "";
     my $parm_name = $parmTypeHash{$wizvars->{ACTION_TYPE}};      my $parm_name = $parmTypeHash{$wizvars->{ACTION_TYPE}};
Line 1118  package Apache::lonwizard::resource_choi Line 1193  package Apache::lonwizard::resource_choi
   
 =head2 Class: resource_choice  =head2 Class: resource_choice
   
 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.  resource_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.
   
 Note this state will not automatically advance if there is only one choice, because it might confuse the user in this case.  Note this state will not automatically advance if there is only one choice, because it might confuse the user in this case.
   
 =over 4  =over 4
   
 =item overriddent method B<new>(parentLonWizReference, stateName, stateTitle, messageBefore, messageAfter, nextState, varName, filterFunction, choiceFunction, multichoice): messageBefore and messageAfter appear before and after the state choice, respectively. nextState is the state to proceed to after the choice. varName is the wizard variable to store the choice in.  =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.
   
 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.
   
 multichoice specifies whether the state should provide radio buttons, allowing the user one choice, or checkboxes, allowing the user multiple choices, and automatically including some convenience buttons the user can choose (like "Check All" and "Uncheck All"), implemented with Javascript. Defaults to false, allow just one choice.  
   
 =back  =back
   
 =cut  =cut
Line 1155  sub new { Line 1228  sub new {
     if (!defined($self->{CHOICE_FUNC})) {      if (!defined($self->{CHOICE_FUNC})) {
         $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};          $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
     }      }
     $self->{MULTICHOICE} = shift;  
     if (!defined($self->{MULTICHOICE})) {  
         $self->{MULTICHOICE} = 0;  
     }  
 }  }
   
 sub postprocess {  sub postprocess {
Line 1183  sub postprocess { Line 1252  sub postprocess {
     $wizard->changeState($self->{NEXT_STATE});      $wizard->changeState($self->{NEXT_STATE});
 }  }
   
   # A note, in case I don't get to this before I leave.
   # If someone complains about the "Back" button returning them
   # to the previous folder state, instead of returning them to
   # the previous wizard state, the *correct* answer is for the wizard
   # to keep track of how many times the user has manipulated the folders,
   # and feed that to the history.go() call in the wizard rendering routines.
   # If done correctly, the wizard itself can keep track of how many times
   # 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
   # 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;
     my $wizard = $self->{WIZARD};      my $wizard = $self->{WIZARD};
Line 1229  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 1239  sub render { Line 1321  sub render {
           
 1;  1;
   
   package Apache::lonwizard::resource_multichoice;
   
   =pod
   
   =head2 Class: resource_multichoice
   
   resource_multichoice gives the user an opportunity to select multiple resources from some map in the current course, and will stick a list of the IDs of those choices in its variable.
   
   Note this state will not automatically advance if there is only one choice, because it might confuse the user. Also, the state will not advance until at least I<one> choice is taken, because it is generally nonsense to select nothing when this state is used.
   
   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
   
   =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.
   
   =back
   
   =cut
   
   no strict;
   @ISA = ("Apache::lonwizard::state");
   use strict;
   
   sub new {
       my $proto = shift;
       my $class = ref($proto) || $proto;
       my $self = bless $proto->SUPER::new(shift, shift, shift);
   
       $self->{MESSAGE_BEFORE} = shift;
       $self->{MESSAGE_AFTER} = shift;
       $self->{NEXT_STATE} = shift;
       $self->{VAR_NAME} = shift;
       $self->{FILTER_FUNC} = shift;
       if (!defined($self->{FILTER_FUNC})) {
           $self->{FILTER_FUNC} = sub {return 1;};
       }
       $self->{CHOICE_FUNC} = shift;
       if (!defined($self->{CHOICE_FUNC})) {
           $self->{CHOICE_FUNC} = $self->{FILTER_FUNC};
       }
       $self->{MAP} = shift;
       if (!defined($self->{MAP})) {
           $self->{MAP} = 1; # 0? trying to default to entire course
       }
   }
   
   sub postprocess {
       my $self = shift;
       my $wizard = $self->{WIZARD};
   
       $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
                                       $self->{VAR_NAME});
   
       # If nothing was selected...
       if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
           $self->{ERROR_MSG} = "You must select one or more resources to continue.";
           return;
       }
   
       $wizard->changeState($self->{NEXT_STATE});
   }
   
   sub render {
       my $self = shift;
       my $wizard = $self->{WIZARD};
       my $var = $self->{VAR_NAME};
       my $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
   
       my $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 />';
       }
   
       $result .= $self->{MESSAGE_BEFORE} . '<br /><br />'
           if (defined $self->{MESSAGE_BEFORE});
   
       my $filterFunc = $self->{FILTER_FUNC};
       my $choiceFunc = $self->{CHOICE_FUNC};
   
       # Create the composite function that renders the column on the nav map
       my $renderColFunc = sub {
           my ($resource, $part, $params) = @_;
   
           if (!&$choiceFunc($resource)) {
               return '<td>&nbsp;</td>';
           } else {
               my $col = "<td><input type='checkbox' name='${var}.forminput'".
                   " value='" . $resource->{ID} . "' /></td>";
               return $col;
           }
       };
   
       $result .= $buttons;
   
       $result .= 
           &Apache::lonnavmaps::render( { 'cols' => [$renderColFunc,
                                                   Apache::lonnavmaps::resource()],
                                          'showParts' => 0,
                                          'filterFunc' => $filterFunc,
                                          'iterator_map' => $self->{MAP},
                                          'resource_no_folder_link' => 1 } );
   
       $result .= $buttons;
   
       $result .= $self->{MESSAGE_AFTER} if (defined $self->{MESSAGE_AFTER});
   
       return $result;
   }
   1;
   
 package Apache::lonwizard::choose_student;  package Apache::lonwizard::choose_student;
   
 no strict;  no strict;
Line 1249  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 1298  sub determineChoices { Line 1506  sub determineChoices {
   
 1;  1;
   
   package Apache::lonwizard::choose_files;
   
   =pod
   
   =head2 Class: choose_file
   
   choose_file offers a choice of files from a given directory. It will store them as a triple-pipe delimited list in its given wizard variable, in the standard HTML multiple-selection tradition. A filter function can be passed, which will examine the filename and return 1 if it should be displayed, or 0 if not. 
   
   =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.
   
   =back
   
   =cut
   
   no strict;
   @ISA = ("Apache::lonwizard::state");
   use strict;
   
   sub new {
       my $proto = shift;
       my $class = ref($proto) || $proto;
       my $self = bless $proto->SUPER::new(shift, shift, shift);
       
       $self->{MESSAGE_BEFORE} = shift;
       $self->{MESSAGE_AFTER} = shift;
       $self->{NEXT_STATE} = shift;
       $self->{VAR_NAME} = shift;
       $self->{SUB_DIR} = shift;
       $self->{FILTER_FUNC} = shift;
   
       if (!defined($self->{FILTER_FUNC})) {
           $self->{FILTER_FUNC} = sub {return 1;};
       }
   
       return $self;
   }
   
   sub render {
       my $self = shift;
       my $result = '';
       my $var = $self->{VAR_NAME};
       my $subdir = $self->{SUB_DIR};
       my $filterFunc = $self->{FILTER_FUNC};
   
       $result = <<SCRIPT;
   <script>
       function checkall(value) {
    for (i=0; i<document.forms.wizform.elements.length; i++) {
               ele = document.forms.wizform.elements[i];
               if (ele.type == "checkbox") {
                   document.forms.wizform.elements[i].checked=value;
               }
           }
       }
   </script>
   SCRIPT
   
       my $buttons = <<BUTTONS;
   <br /> &nbsp;
   <input type="button" onclick="checkall(true)" value="Select All" />
   <input type="button" onclick="checkall(false)" value="Unselect All" />
   <br /> &nbsp;
   BUTTONS
   
       if (defined $self->{ERROR_MSG}) {
           $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
       }
   
       if ($self->{MESSAGE_BEFORE}) {
           $result .= $self->{MESSAGE_BEFORE} . '<br />';
       }
   
       # Get the list of files in this directory.
       my @fileList;
   
       # If the subdirectory is in local CSTR space
       if ($subdir =~ m|/home/([^/]+)/public_html|) {
           my $user = $1;
           my $domain = $Apache::lonnet::perlvar{'lonDefDomain'};
           @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
       } else {
           # local library server resource space
           @fileList = &Apache::lonnet::dirlist($subdir, $ENV{'user.domain'}, $ENV{'user.name'}, '');
       }
   
       $result .= $buttons;
       
       $result .= '<table border="0" cellpadding="1" cellspacing="1">';
   
       # Keeps track if there are no choices, prints appropriate error
       # if there are none. 
       my $choices = 0;
       # Print each legitimate file choice.
       for my $file (@fileList) {
           $file = (split(/&/, $file))[0];
           my $fileName = $subdir .'/'. $file;
           if (&$filterFunc($file)) {
               $result .= '<tr><td align="right">' .
                   "<input type='checkbox' name='" . $self->{VAR_NAME}
               . ".forminput' value='" . HTML::Entities::encode($fileName) .
                   "' /></td><td>" . $file . "</td></tr>\n";
               $choices++;
           }
       }
   
       $result .= "</table>\n";
   
       if (!$choices) {
           $result .= '<font color="#FF0000">There are no files available to select in this directory. Please go back and select another option.</font><br /><br />';
       }
   
       $result .= $buttons;
   
       if ($self->{MESSAGE_AFTER}) {
           $result .= "<br /><br />" . $self->{MESSAGE_AFTER};
       }
   
       return $result;
   }
   
   sub postprocess {
       my $self = shift;
       print $self->{NEXT_STATE};
       my $wizard = $self->{WIZARD};
   
       $self->process_multiple_choices($self->{VAR_NAME}.'.forminput',
                                       $self->{VAR_NAME});
       
       if (!$wizard->{VARS}->{$self->{VAR_NAME}}) {
           $self->{ERROR_MSG} = "Can't continue the wizard because you ".
               "must make a selection to continue.";
       }
       return 1;
   }
   
   1;

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


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