Annotation of loncom/interface/lonhelper.pm, revision 1.160

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # .helper XML handler to implement the LON-CAPA helper
                      3: #
1.160   ! albertel    4: # $Id: lonhelper.pm,v 1.159 2006/12/12 20:41:08 albertel Exp $
1.1       bowersj2    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
1.3       bowersj2   29: =pod
                     30: 
1.44      bowersj2   31: =head1 NAME
1.3       bowersj2   32: 
1.44      bowersj2   33: lonhelper - implements helper framework
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: lonhelper implements the helper framework for LON-CAPA, and provides
                     38:     many generally useful components for that framework.
                     39: 
                     40: Helpers are little programs which present the user with a sequence of
                     41:     simple choices, instead of one monolithic multi-dimensional
                     42:     choice. They are also referred to as "wizards", "druids", and
                     43:     other potentially trademarked or semantically-loaded words.
                     44: 
                     45: =head1 OVERVIEWX<lonhelper>
                     46: 
                     47: Helpers are well-established UI widgets that users
1.3       bowersj2   48: feel comfortable with. It can take a complicated multidimensional problem the
                     49: user has and turn it into a series of bite-sized one-dimensional questions.
                     50: 
                     51: For developers, helpers provide an easy way to bundle little bits of functionality
                     52: for the user, without having to write the tedious state-maintenence code.
                     53: 
                     54: Helpers are defined as XML documents, placed in the /home/httpd/html/adm/helpers 
                     55: directory and having the .helper file extension. For examples, see that directory.
                     56: 
                     57: All classes are in the Apache::lonhelper namespace.
                     58: 
1.44      bowersj2   59: =head1 lonhelper XML file formatX<lonhelper, XML file format>
1.3       bowersj2   60: 
                     61: A helper consists of a top-level <helper> tag which contains a series of states.
                     62: Each state contains one or more state elements, which are what the user sees, like
                     63: messages, resource selections, or date queries.
                     64: 
                     65: The helper tag is required to have one attribute, "title", which is the name
1.31      bowersj2   66: of the helper itself, such as "Parameter helper". The helper tag may optionally
                     67: have a "requiredpriv" attribute, specifying the priviledge a user must have
                     68: to use the helper, or get denied access. See loncom/auth/rolesplain.tab for
                     69: useful privs. Default is full access, which is often wrong!
1.3       bowersj2   70: 
                     71: =head2 State tags
                     72: 
                     73: State tags are required to have an attribute "name", which is the symbolic
1.7       bowersj2   74: name of the state and will not be directly seen by the user. The helper is
                     75: required to have one state named "START", which is the state the helper
1.5       bowersj2   76: will start with. By convention, this state should clearly describe what
1.3       bowersj2   77: the helper will do for the user, and may also include the first information
                     78: entry the user needs to do for the helper.
                     79: 
                     80: State tags are also required to have an attribute "title", which is the
                     81: human name of the state, and will be displayed as the header on top of 
                     82: the screen for the user.
                     83: 
1.160   ! albertel   84: State tags may also optionally have an attribute "help" which should be
        !            85: the filename of a help file, this will add a blue ? to the title.
        !            86: 
1.3       bowersj2   87: =head2 Example Helper Skeleton
                     88: 
                     89: An example of the tags so far:
                     90: 
                     91:  <helper title="Example Helper">
                     92:    <state name="START" title="Demonstrating the Example Helper">
1.160   ! albertel   93:      <!-- notice this is the START state the helper requires -->
1.3       bowersj2   94:      </state>
                     95:    <state name="GET_NAME" title="Enter Student Name">
                     96:      </state>
                     97:    </helper>
                     98: 
1.160   ! albertel   99: Of course this does nothing. In order for the helper to do something, it is
        !           100: necessary to put actual elements into the helper. Documentation for each
1.3       bowersj2  101: of these elements follows.
                    102: 
1.44      bowersj2  103: =head1 Creating a Helper With Code, Not XML
1.13      bowersj2  104: 
1.160   ! albertel  105: In some situations, such as the printing helper (see lonprintout.pm), 
1.13      bowersj2  106: writing the helper in XML would be too complicated, because of scope 
                    107: issues or the fact that the code actually outweighs the XML. It is
                    108: possible to create a helper via code, though it is a little odd.
                    109: 
                    110: Creating a helper via code is more like issuing commands to create
                    111: a helper then normal code writing. For instance, elements will automatically
                    112: be added to the last state created, so it's important to create the 
                    113: states in the correct order.
                    114: 
                    115: First, create a new helper:
                    116: 
                    117:  use Apache::lonhelper;
                    118: 
                    119:  my $helper = Apache::lonhelper::new->("Helper Title");
                    120: 
                    121: Next you'll need to manually add states to the helper:
                    122: 
                    123:  Apache::lonhelper::state->new("STATE_NAME", "State's Human Title");
                    124: 
                    125: You don't need to save a reference to it because all elements up until
                    126: the next state creation will automatically be added to this state.
                    127: 
                    128: Elements are created by populating the $paramHash in 
                    129: Apache::lonhelper::paramhash. To prevent namespace issues, retrieve 
                    130: a reference to that has with getParamHash:
                    131: 
                    132:  my $paramHash = Apache::lonhelper::getParamHash();
                    133: 
                    134: You will need to do this for each state you create.
                    135: 
                    136: Populate the $paramHash with the parameters for the element you wish
                    137: to add next; the easiest way to find out what those entries are is
                    138: to read the code. Some common ones are 'variable' to record the variable
                    139: to store the results in, and NEXTSTATE to record a next state transition.
                    140: 
                    141: Then create your element:
                    142: 
                    143:  $paramHash->{MESSAGETEXT} = "This is a message.";
                    144:  Apache::lonhelper::message->new();
                    145: 
                    146: The creation will take the $paramHash and bless it into a
                    147: Apache::lonhelper::message object. To create the next element, you need
                    148: to get a reference to the new, empty $paramHash:
                    149: 
                    150:  $paramHash = Apache::lonhelper::getParamHash();
                    151: 
                    152: and you can repeat creating elements that way. You can add states
                    153: and elements as needed.
                    154: 
                    155: See lonprintout.pm, subroutine printHelper for an example of this, where
                    156: we dynamically add some states to prevent security problems, for instance.
                    157: 
                    158: Normally the machinery in the XML format is sufficient; dynamically 
                    159: adding states can easily be done by wrapping the state in a <condition>
                    160: tag. This should only be used when the code dominates the XML content,
                    161: the code is so complicated that it is difficult to get access to
1.44      bowersj2  162: all of the information you need because of scoping issues, or would-be <exec> or 
                    163: <eval> blocks using the {DATA} mechanism results in hard-to-read
                    164: and -maintain code. (See course.initialization.helper for a borderline
                    165: case.)
1.13      bowersj2  166: 
                    167: It is possible to do some of the work with an XML fragment parsed by
1.17      bowersj2  168: lonxml; again, see lonprintout.pm for an example. In that case it is 
                    169: imperative that you call B<Apache::lonhelper::registerHelperTags()>
                    170: before parsing XML fragments and B<Apache::lonhelper::unregisterHelperTags()>
                    171: when you are done. See lonprintout.pm for examples of this usage in the
                    172: printHelper subroutine.
1.13      bowersj2  173: 
1.57      albertel  174: =head2 Localization
                    175: 
                    176: The helper framework tries to handle as much localization as
                    177: possible. The text is always run through
                    178: Apache::lonlocal::normalize_string, so be sure to run the keys through
                    179: that function for maximum usefulness and robustness.
                    180: 
1.3       bowersj2  181: =cut
                    182: 
1.1       bowersj2  183: package Apache::lonhelper;
1.2       bowersj2  184: use Apache::Constants qw(:common);
                    185: use Apache::File;
1.3       bowersj2  186: use Apache::lonxml;
1.57      albertel  187: use Apache::lonlocal;
1.100     albertel  188: use Apache::lonnet;
1.151     raeburn   189: use Apache::longroup;
1.148     foxr      190: use Apache::lonselstudent;
1.152     www       191: use LONCAPA;
1.139     foxr      192: 
1.7       bowersj2  193: # Register all the tags with the helper, so the helper can 
                    194: # push and pop them
                    195: 
                    196: my @helperTags;
                    197: 
                    198: sub register {
                    199:     my ($namespace, @tags) = @_;
                    200: 
                    201:     for my $tag (@tags) {
                    202:         push @helperTags, [$namespace, $tag];
                    203:     }
                    204: }
                    205: 
1.2       bowersj2  206: BEGIN {
1.7       bowersj2  207:     Apache::lonxml::register('Apache::lonhelper', 
                    208:                              ('helper'));
                    209:       register('Apache::lonhelper', ('state'));
1.2       bowersj2  210: }
                    211: 
1.7       bowersj2  212: # Since all helpers are only three levels deep (helper tag, state tag, 
1.3       bowersj2  213: # substate type), it's easier and more readble to explicitly track 
                    214: # those three things directly, rather then futz with the tag stack 
                    215: # every time.
                    216: my $helper;
                    217: my $state;
                    218: my $substate;
1.4       bowersj2  219: # To collect parameters, the contents of the subtags are collected
                    220: # into this paramHash, then passed to the element object when the 
                    221: # end of the element tag is located.
                    222: my $paramHash; 
1.2       bowersj2  223: 
1.25      bowersj2  224: # Note from Jeremy 5-8-2003: It is *vital* that the real handler be called
                    225: # as a subroutine from the handler, or very mysterious things might happen.
                    226: # I don't know exactly why, but it seems that the scope where the Apache
                    227: # server enters the perl handler is treated differently from the rest of
                    228: # the handler. This also seems to manifest itself in the debugger as entering
                    229: # the perl handler in seemingly random places (sometimes it starts in the
                    230: # compiling phase, sometimes in the handler execution phase where it runs
                    231: # the code and stepping into the "1;" the module ends with goes into the handler,
                    232: # sometimes starting directly with the handler); I think the cause is related.
                    233: # In the debugger, this means that breakpoints are ignored until you step into
                    234: # a function and get out of what must be a "faked up scope" in the Apache->
                    235: # mod_perl connection. In this code, it was manifesting itself in the existence
1.65      www       236: # of two separate file-scoped $helper variables, one set to the value of the
1.25      bowersj2  237: # helper in the helper constructor, and one referenced by the handler on the
1.44      bowersj2  238: # "$helper->process()" line. Using the debugger, one could actually
                    239: # see the two different $helper variables, as hashes at completely
                    240: # different addresses. The second was therefore never set, and was still
1.25      bowersj2  241: # undefined when I tried to call process on it.
                    242: # By pushing the "real handler" down into the "real scope", everybody except the 
                    243: # actual handler function directly below this comment gets the same $helper and
                    244: # everybody is happy.
                    245: # The upshot of all of this is that for safety when a handler is  using 
                    246: # file-scoped variables in LON-CAPA, the handler should be pushed down one 
                    247: # call level, as I do here, to ensure that the top-level handler function does
                    248: # not get a different file scope from the rest of the code.
                    249: sub handler {
                    250:     my $r = shift;
                    251:     return real_handler($r);
                    252: }
                    253: 
1.13      bowersj2  254: # For debugging purposes, one can send a second parameter into this
                    255: # function, the 'uri' of the helper you wish to have rendered, and
                    256: # call this from other handlers.
1.25      bowersj2  257: sub real_handler {
1.3       bowersj2  258:     my $r = shift;
1.13      bowersj2  259:     my $uri = shift;
                    260:     if (!defined($uri)) { $uri = $r->uri(); }
1.100     albertel  261:     $env{'request.uri'} = $uri;
1.13      bowersj2  262:     my $filename = '/home/httpd/html' . $uri;
1.2       bowersj2  263:     my $fh = Apache::File->new($filename);
                    264:     my $file;
1.3       bowersj2  265:     read $fh, $file, 100000000;
                    266: 
1.27      bowersj2  267: 
1.3       bowersj2  268:     # Send header, don't cache this page
1.100     albertel  269:     if ($env{'browser.mathml'}) {
1.70      sakharuk  270: 	&Apache::loncommon::content_type($r,'text/xml');
                    271:     } else {
                    272: 	&Apache::loncommon::content_type($r,'text/html');
                    273:     }
                    274:     $r->send_http_header;
                    275:     return OK if $r->header_only;
1.3       bowersj2  276:     $r->rflush();
1.2       bowersj2  277: 
1.3       bowersj2  278:     # Discard result, we just want the objects that get created by the
                    279:     # xml parsing
                    280:     &Apache::lonxml::xmlparse($r, 'helper', $file);
1.2       bowersj2  281: 
1.31      bowersj2  282:     my $allowed = $helper->allowedCheck();
                    283:     if (!$allowed) {
1.100     albertel  284:         $env{'user.error.msg'} = $env{'request.uri'}.':'.$helper->{REQUIRED_PRIV}.
1.31      bowersj2  285:             ":0:0:Permission denied to access this helper.";
                    286:         return HTTP_NOT_ACCEPTABLE;
                    287:     }
                    288: 
1.13      bowersj2  289:     $helper->process();
                    290: 
1.3       bowersj2  291:     $r->print($helper->display());
1.31      bowersj2  292:     return OK;
1.2       bowersj2  293: }
                    294: 
1.13      bowersj2  295: sub registerHelperTags {
                    296:     for my $tagList (@helperTags) {
                    297:         Apache::lonxml::register($tagList->[0], $tagList->[1]);
                    298:     }
                    299: }
                    300: 
                    301: sub unregisterHelperTags {
                    302:     for my $tagList (@helperTags) {
                    303:         Apache::lonxml::deregister($tagList->[0], $tagList->[1]);
                    304:     }
                    305: }
                    306: 
1.2       bowersj2  307: sub start_helper {
                    308:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    309: 
                    310:     if ($target ne 'helper') {
                    311:         return '';
                    312:     }
1.7       bowersj2  313: 
1.13      bowersj2  314:     registerHelperTags();
                    315: 
1.31      bowersj2  316:     Apache::lonhelper::helper->new($token->[2]{'title'}, $token->[2]{'requiredpriv'});
1.4       bowersj2  317:     return '';
1.2       bowersj2  318: }
                    319: 
                    320: sub end_helper {
                    321:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    322:     
1.3       bowersj2  323:     if ($target ne 'helper') {
                    324:         return '';
                    325:     }
1.7       bowersj2  326: 
1.13      bowersj2  327:     unregisterHelperTags();
1.7       bowersj2  328: 
1.4       bowersj2  329:     return '';
1.2       bowersj2  330: }
1.1       bowersj2  331: 
1.3       bowersj2  332: sub start_state {
                    333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    334: 
                    335:     if ($target ne 'helper') {
                    336:         return '';
                    337:     }
                    338: 
1.13      bowersj2  339:     Apache::lonhelper::state->new($token->[2]{'name'},
1.160   ! albertel  340:                                   $token->[2]{'title'},
        !           341: 				  $token->[2]{'help'});
1.3       bowersj2  342:     return '';
                    343: }
                    344: 
1.13      bowersj2  345: # Use this to get the param hash from other files.
                    346: sub getParamHash {
                    347:     return $paramHash;
                    348: }
                    349: 
                    350: # Use this to get the helper, if implementing elements in other files
                    351: # (like lonprintout.pm)
                    352: sub getHelper {
                    353:     return $helper;
                    354: }
                    355: 
1.3       bowersj2  356: # don't need this, so ignore it
                    357: sub end_state {
                    358:     return '';
                    359: }
                    360: 
1.1       bowersj2  361: 1;
                    362: 
1.3       bowersj2  363: package Apache::lonhelper::helper;
                    364: 
                    365: use Digest::MD5 qw(md5_hex);
1.57      albertel  366: use HTML::Entities();
1.3       bowersj2  367: use Apache::loncommon;
                    368: use Apache::File;
1.57      albertel  369: use Apache::lonlocal;
1.100     albertel  370: use Apache::lonnet;
1.154     albertel  371: use LONCAPA;
1.3       bowersj2  372: 
                    373: sub new {
                    374:     my $proto = shift;
                    375:     my $class = ref($proto) || $proto;
                    376:     my $self = {};
                    377: 
                    378:     $self->{TITLE} = shift;
1.31      bowersj2  379:     $self->{REQUIRED_PRIV} = shift;
1.3       bowersj2  380:     
                    381:     # If there is a state from the previous form, use that. If there is no
                    382:     # state, use the start state parameter.
1.100     albertel  383:     if (defined $env{"form.CURRENT_STATE"})
1.3       bowersj2  384:     {
1.100     albertel  385: 	$self->{STATE} = $env{"form.CURRENT_STATE"};
1.3       bowersj2  386:     }
                    387:     else
                    388:     {
                    389: 	$self->{STATE} = "START";
                    390:     }
                    391: 
1.100     albertel  392:     $self->{TOKEN} = $env{'form.TOKEN'};
1.3       bowersj2  393:     # If a token was passed, we load that in. Otherwise, we need to create a 
                    394:     # new storage file
                    395:     # Tried to use standard Tie'd hashes, but you can't seem to take a 
                    396:     # reference to a tied hash and write to it. I'd call that a wart.
                    397:     if ($self->{TOKEN}) {
                    398:         # Validate the token before trusting it
                    399:         if ($self->{TOKEN} !~ /^[a-f0-9]{32}$/) {
                    400:             # Not legit. Return nothing and let all hell break loose.
                    401:             # User shouldn't be doing that!
                    402:             return undef;
                    403:         }
                    404: 
                    405:         # Get the hash.
                    406:         $self->{FILENAME} = $Apache::lonnet::tmpdir . md5_hex($self->{TOKEN}); # Note the token is not the literal file
                    407:         
                    408:         my $file = Apache::File->new($self->{FILENAME});
                    409:         my $contents = <$file>;
1.5       bowersj2  410: 
                    411:         # Now load in the contents
                    412:         for my $value (split (/&/, $contents)) {
                    413:             my ($name, $value) = split(/=/, $value);
                    414:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                    415:             $self->{VARS}->{$name} = $value;
                    416:         }
                    417: 
1.3       bowersj2  418:         $file->close();
                    419:     } else {
                    420:         # Only valid if we're just starting.
                    421:         if ($self->{STATE} ne 'START') {
                    422:             return undef;
                    423:         }
                    424:         # Must create the storage
1.100     albertel  425:         $self->{TOKEN} = md5_hex($env{'user.name'} . $env{'user.domain'} .
1.3       bowersj2  426:                                  time() . rand());
                    427:         $self->{FILENAME} = $Apache::lonnet::tmpdir . md5_hex($self->{TOKEN});
                    428:     }
                    429: 
                    430:     # OK, we now have our persistent storage.
                    431: 
1.100     albertel  432:     if (defined $env{"form.RETURN_PAGE"})
1.3       bowersj2  433:     {
1.100     albertel  434: 	$self->{RETURN_PAGE} = $env{"form.RETURN_PAGE"};
1.3       bowersj2  435:     }
                    436:     else
                    437:     {
                    438: 	$self->{RETURN_PAGE} = $ENV{REFERER};
                    439:     }
                    440: 
                    441:     $self->{STATES} = {};
                    442:     $self->{DONE} = 0;
                    443: 
1.9       bowersj2  444:     # Used by various helpers for various things; see lonparm.helper
                    445:     # for an example.
                    446:     $self->{DATA} = {};
                    447: 
1.13      bowersj2  448:     $helper = $self;
                    449: 
                    450:     # Establish the $paramHash
                    451:     $paramHash = {};
                    452: 
1.3       bowersj2  453:     bless($self, $class);
                    454:     return $self;
                    455: }
                    456: 
                    457: # Private function; returns a string to construct the hidden fields
                    458: # necessary to have the helper track state.
                    459: sub _saveVars {
                    460:     my $self = shift;
                    461:     my $result = "";
                    462:     $result .= '<input type="hidden" name="CURRENT_STATE" value="' .
1.67      albertel  463:         HTML::Entities::encode($self->{STATE},'<>&"') . "\" />\n";
1.3       bowersj2  464:     $result .= '<input type="hidden" name="TOKEN" value="' .
                    465:         $self->{TOKEN} . "\" />\n";
                    466:     $result .= '<input type="hidden" name="RETURN_PAGE" value="' .
1.67      albertel  467:         HTML::Entities::encode($self->{RETURN_PAGE},'<>&"') . "\" />\n";
1.3       bowersj2  468: 
                    469:     return $result;
                    470: }
                    471: 
                    472: # Private function: Create the querystring-like representation of the stored
                    473: # data to write to disk.
                    474: sub _varsInFile {
                    475:     my $self = shift;
                    476:     my @vars = ();
1.154     albertel  477:     for my $key (keys(%{$self->{VARS}})) {
                    478:         push(@vars, &escape($key) . '=' . &escape($self->{VARS}->{$key}));
1.3       bowersj2  479:     }
                    480:     return join ('&', @vars);
                    481: }
                    482: 
1.5       bowersj2  483: # Use this to declare variables.
                    484: # FIXME: Document this
                    485: sub declareVar {
                    486:     my $self = shift;
                    487:     my $var = shift;
                    488: 
                    489:     if (!defined($self->{VARS}->{$var})) {
                    490:         $self->{VARS}->{$var} = '';
                    491:     }
                    492: 
1.157     raeburn   493:     my $envname = 'form.' . $var . '_forminput';
1.100     albertel  494:     if (defined($env{$envname})) {
                    495:         if (ref($env{$envname})) {
                    496:             $self->{VARS}->{$var} = join('|||', @{$env{$envname}});
1.28      bowersj2  497:         } else {
1.100     albertel  498:             $self->{VARS}->{$var} = $env{$envname};
1.28      bowersj2  499:         }
1.5       bowersj2  500:     }
                    501: }
                    502: 
1.31      bowersj2  503: sub allowedCheck {
                    504:     my $self = shift;
                    505: 
                    506:     if (!defined($self->{REQUIRED_PRIV})) { 
                    507:         return 1;
                    508:     }
                    509: 
1.100     albertel  510:     return Apache::lonnet::allowed($self->{REQUIRED_PRIV}, $env{'request.course.id'});
1.31      bowersj2  511: }
                    512: 
1.3       bowersj2  513: sub changeState {
                    514:     my $self = shift;
                    515:     $self->{STATE} = shift;
                    516: }
                    517: 
                    518: sub registerState {
                    519:     my $self = shift;
                    520:     my $state = shift;
                    521: 
                    522:     my $stateName = $state->name();
                    523:     $self->{STATES}{$stateName} = $state;
                    524: }
                    525: 
1.13      bowersj2  526: sub process {
1.3       bowersj2  527:     my $self = shift;
                    528: 
                    529:     # Phase 1: Post processing for state of previous screen (which is actually
                    530:     # the "current state" in terms of the helper variables), if it wasn't the 
                    531:     # beginning state.
1.100     albertel  532:     if ($self->{STATE} ne "START" || $env{"form.SUBMIT"} eq &mt("Next ->")) {
1.3       bowersj2  533: 	my $prevState = $self->{STATES}{$self->{STATE}};
1.13      bowersj2  534:         $prevState->postprocess();
1.3       bowersj2  535:     }
                    536:     
                    537:     # Note, to handle errors in a state's input that a user must correct,
                    538:     # do not transition in the postprocess, and force the user to correct
                    539:     # the error.
                    540: 
                    541:     # Phase 2: Preprocess current state
                    542:     my $startState = $self->{STATE};
1.17      bowersj2  543:     my $state = $self->{STATES}->{$startState};
1.3       bowersj2  544:     
1.13      bowersj2  545:     # For debugging, print something here to determine if you're going
                    546:     # to an undefined state.
1.3       bowersj2  547:     if (!defined($state)) {
1.13      bowersj2  548:         return;
1.3       bowersj2  549:     }
                    550:     $state->preprocess();
                    551: 
                    552:     # Phase 3: While the current state is different from the previous state,
                    553:     # keep processing.
1.17      bowersj2  554:     while ( $startState ne $self->{STATE} && 
                    555:             defined($self->{STATES}->{$self->{STATE}}) )
1.3       bowersj2  556:     {
                    557: 	$startState = $self->{STATE};
1.17      bowersj2  558: 	$state = $self->{STATES}->{$startState};
1.3       bowersj2  559: 	$state->preprocess();
                    560:     }
                    561: 
1.13      bowersj2  562:     return;
                    563: } 
                    564: 
                    565: # 1: Do the post processing for the previous state.
                    566: # 2: Do the preprocessing for the current state.
                    567: # 3: Check to see if state changed, if so, postprocess current and move to next.
                    568: #    Repeat until state stays stable.
                    569: # 4: Render the current state to the screen as an HTML page.
                    570: sub display {
                    571:     my $self = shift;
                    572: 
                    573:     my $state = $self->{STATES}{$self->{STATE}};
                    574: 
                    575:     my $result = "";
                    576: 
1.17      bowersj2  577:     if (!defined($state)) {
                    578:         $result = "<font color='#ff0000'>Error: state '$state' not defined!</font>";
                    579:         return $result;
                    580:     }
                    581: 
1.3       bowersj2  582:     # Phase 4: Display.
1.68      sakharuk  583:     my $stateTitle=&mt($state->title());
1.160   ! albertel  584:     my $stateHelp=     $state->help();
1.135     albertel  585:     my $browser_searcher_js = 
                    586: 	'<script type="text/javascript">'."\n".
                    587: 	&Apache::loncommon::browser_and_searcher_javascript().
                    588: 	"\n".'</script>';
                    589: 
                    590:     $result .= &Apache::loncommon::start_page($self->{TITLE},
                    591: 					      $browser_searcher_js);
                    592:     
1.57      albertel  593:     my $previous = HTML::Entities::encode(&mt("<- Previous"), '<>&"');
                    594:     my $next = HTML::Entities::encode(&mt("Next ->"), '<>&"');
                    595:     # FIXME: This should be parameterized, not concatenated - Jeremy
1.3       bowersj2  596: 
1.135     albertel  597: 
1.28      bowersj2  598:     if (!$state->overrideForm()) { $result.="<form name='helpform' method='POST'>"; }
1.160   ! albertel  599:     if ($stateHelp) {
        !           600: 	$stateHelp = &Apache::loncommon::help_open_topic($stateHelp);
        !           601:     }
1.3       bowersj2  602:     $result .= <<HEADER;
1.31      bowersj2  603:         <table border="0" width='100%'><tr><td>
1.160   ! albertel  604:         <h2><i>$stateTitle</i>$stateHelp</h2>
1.3       bowersj2  605: HEADER
                    606: 
1.31      bowersj2  607:     $result .= "<table cellpadding='10' width='100%'><tr><td rowspan='2' valign='top'>";
1.30      bowersj2  608: 
1.3       bowersj2  609:     if (!$state->overrideForm()) {
                    610:         $result .= $self->_saveVars();
                    611:     }
1.30      bowersj2  612:     $result .= $state->render();
                    613: 
1.31      bowersj2  614:     $result .= "</td><td valign='top' align='right'>";
1.3       bowersj2  615: 
1.30      bowersj2  616:     # Warning: Copy and pasted from below, because it's too much trouble to 
                    617:     # turn this into a subroutine
1.3       bowersj2  618:     if (!$state->overrideForm()) {
                    619:         if ($self->{STATE} ne $self->{START_STATE}) {
                    620:             #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';
                    621:         }
                    622:         if ($self->{DONE}) {
                    623:             my $returnPage = $self->{RETURN_PAGE};
1.57      albertel  624:             $result .= "<a href=\"$returnPage\">" . &mt("End Helper") . "</a>";
1.3       bowersj2  625:         }
                    626:         else {
1.30      bowersj2  627:             $result .= '<nobr><input name="back" type="button" ';
1.57      albertel  628:             $result .= 'value="' . $previous . '" onclick="history.go(-1)" /> ';
                    629:             $result .= '<input name="SUBMIT" type="submit" value="' . $next . '" /></nobr>';
1.30      bowersj2  630:         }
                    631:     }
                    632: 
1.31      bowersj2  633:     $result .= "</td></tr><tr><td valign='bottom' align='right'>";
1.30      bowersj2  634: 
                    635:     # Warning: Copy and pasted from above, because it's too much trouble to 
                    636:     # turn this into a subroutine
                    637:     if (!$state->overrideForm()) {
                    638:         if ($self->{STATE} ne $self->{START_STATE}) {
                    639:             #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';
                    640:         }
                    641:         if ($self->{DONE}) {
                    642:             my $returnPage = $self->{RETURN_PAGE};
1.57      albertel  643:             $result .= "<a href=\"$returnPage\">" . &mt('End Helper') . "</a>";
1.30      bowersj2  644:         }
                    645:         else {
                    646:             $result .= '<nobr><input name="back" type="button" ';
1.57      albertel  647:             $result .= 'value="' . $previous . '" onclick="history.go(-1)" /> ';
                    648:             $result .= '<input name="SUBMIT" type="submit" value="' . $next . '" /></nobr>';
1.3       bowersj2  649:         }
                    650:     }
                    651: 
1.13      bowersj2  652:     #foreach my $key (keys %{$self->{VARS}}) {
                    653:     #    $result .= "|$key| -> " . $self->{VARS}->{$key} . "<br />";
                    654:     #}
1.5       bowersj2  655: 
1.30      bowersj2  656:     $result .= "</td></tr></table>";
                    657: 
1.3       bowersj2  658:     $result .= <<FOOTER;
                    659:               </td>
                    660:             </tr>
                    661:           </table>
                    662:         </form>
                    663: FOOTER
                    664: 
1.135     albertel  665:     $result .= &Apache::loncommon::end_page();
1.3       bowersj2  666:     # Handle writing out the vars to the file
                    667:     my $file = Apache::File->new('>'.$self->{FILENAME});
1.33      bowersj2  668:     print $file $self->_varsInFile();
1.3       bowersj2  669: 
                    670:     return $result;
                    671: }
                    672: 
                    673: 1;
                    674: 
                    675: package Apache::lonhelper::state;
                    676: 
                    677: # States bundle things together and are responsible for compositing the
1.4       bowersj2  678: # various elements together. It is not generally necessary for users to
                    679: # use the state object directly, so it is not perldoc'ed.
                    680: 
                    681: # Basically, all the states do is pass calls to the elements and aggregate
                    682: # the results.
1.3       bowersj2  683: 
                    684: sub new {
                    685:     my $proto = shift;
                    686:     my $class = ref($proto) || $proto;
                    687:     my $self = {};
                    688: 
                    689:     $self->{NAME} = shift;
                    690:     $self->{TITLE} = shift;
1.160   ! albertel  691:     $self->{HELP} = shift;
1.3       bowersj2  692:     $self->{ELEMENTS} = [];
                    693: 
                    694:     bless($self, $class);
                    695: 
                    696:     $helper->registerState($self);
                    697: 
1.13      bowersj2  698:     $state = $self;
                    699: 
1.3       bowersj2  700:     return $self;
                    701: }
                    702: 
                    703: sub name {
                    704:     my $self = shift;
                    705:     return $self->{NAME};
                    706: }
                    707: 
                    708: sub title {
                    709:     my $self = shift;
                    710:     return $self->{TITLE};
                    711: }
                    712: 
1.160   ! albertel  713: sub help {
        !           714:     my $self = shift;
        !           715:     return $self->{HELP};
        !           716: }
        !           717: 
1.4       bowersj2  718: sub preprocess {
                    719:     my $self = shift;
                    720:     for my $element (@{$self->{ELEMENTS}}) {
                    721:         $element->preprocess();
                    722:     }
                    723: }
                    724: 
1.6       bowersj2  725: # FIXME: Document that all postprocesses must return a true value or
                    726: # the state transition will be overridden
1.4       bowersj2  727: sub postprocess {
                    728:     my $self = shift;
1.6       bowersj2  729: 
                    730:     # Save the state so we can roll it back if we need to.
                    731:     my $originalState = $helper->{STATE};
                    732:     my $everythingSuccessful = 1;
                    733: 
1.4       bowersj2  734:     for my $element (@{$self->{ELEMENTS}}) {
1.6       bowersj2  735:         my $result = $element->postprocess();
                    736:         if (!$result) { $everythingSuccessful = 0; }
                    737:     }
                    738: 
                    739:     # If not all the postprocesses were successful, override
                    740:     # any state transitions that may have occurred. It is the
                    741:     # responsibility of the states to make sure they have 
                    742:     # error handling in that case.
                    743:     if (!$everythingSuccessful) {
                    744:         $helper->{STATE} = $originalState;
1.4       bowersj2  745:     }
                    746: }
                    747: 
1.13      bowersj2  748: # Override the form if any element wants to.
                    749: # two elements overriding the form will make a mess, but that should
                    750: # be considered helper author error ;-)
1.4       bowersj2  751: sub overrideForm {
1.13      bowersj2  752:     my $self = shift;
                    753:     for my $element (@{$self->{ELEMENTS}}) {
                    754:         if ($element->overrideForm()) {
                    755:             return 1;
                    756:         }
                    757:     }
1.4       bowersj2  758:     return 0;
                    759: }
                    760: 
                    761: sub addElement {
                    762:     my $self = shift;
                    763:     my $element = shift;
                    764:     
                    765:     push @{$self->{ELEMENTS}}, $element;
                    766: }
                    767: 
                    768: sub render {
                    769:     my $self = shift;
                    770:     my @results = ();
                    771: 
                    772:     for my $element (@{$self->{ELEMENTS}}) {
                    773:         push @results, $element->render();
                    774:     }
1.28      bowersj2  775: 
1.4       bowersj2  776:     return join("\n", @results);
                    777: }
                    778: 
                    779: 1;
                    780: 
                    781: package Apache::lonhelper::element;
                    782: # Support code for elements
                    783: 
                    784: =pod
                    785: 
1.44      bowersj2  786: =head1 Element Base Class
1.4       bowersj2  787: 
1.28      bowersj2  788: The Apache::lonhelper::element base class provides support for elements
                    789: and defines some generally useful tags for use in elements.
1.4       bowersj2  790: 
1.44      bowersj2  791: =head2 finalcode tagX<finalcode>
1.25      bowersj2  792: 
                    793: Each element can contain a "finalcode" tag that, when the special FINAL
                    794: helper state is used, will be executed, surrounded by "sub { my $helper = shift;"
                    795: and "}". It is expected to return a string describing what it did, which 
                    796: may be an empty string. See course initialization helper for an example. This is
                    797: generally intended for helpers like the course initialization helper, which consist
                    798: of several panels, each of which is performing some sort of bite-sized functionality.
                    799: 
1.44      bowersj2  800: =head2 defaultvalue tagX<defaultvalue>
1.25      bowersj2  801: 
                    802: Each element that accepts user input can contain a "defaultvalue" tag that,
                    803: when surrounded by "sub { my $helper = shift; my $state = shift; " and "}",
                    804: will form a subroutine that when called will provide a default value for
                    805: the element. How this value is interpreted by the element is specific to
                    806: the element itself, and possibly the settings the element has (such as 
                    807: multichoice vs. single choice for <choices> tags). 
                    808: 
1.160   ! albertel  809: This is also intended for things like the course initialization helper, where the
1.25      bowersj2  810: user is setting various parameters. By correctly grabbing current settings 
                    811: and including them into the helper, it allows the user to come back to the
                    812: helper later and re-execute it, without needing to worry about overwriting
                    813: some setting accidentally.
                    814: 
                    815: Again, see the course initialization helper for examples.
                    816: 
1.44      bowersj2  817: =head2 validator tagX<validator>
1.38      bowersj2  818: 
                    819: Some elements that accepts user input can contain a "validator" tag that,
                    820: when surrounded by "sub { my $helper = shift; my $state = shift; my $element = shift; my $val = shift " 
                    821: and "}", where "$val" is the value the user entered, will form a subroutine 
                    822: that when called will verify whether the given input is valid or not. If it 
                    823: is valid, the routine will return a false value. If invalid, the routine 
                    824: will return an error message to be displayed for the user.
                    825: 
                    826: Consult the documentation for each element to see whether it supports this 
                    827: tag.
                    828: 
1.44      bowersj2  829: =head2 getValue methodX<getValue (helper elements)>
1.31      bowersj2  830: 
                    831: If the element stores the name of the variable in a 'variable' member, which
                    832: the provided ones all do, you can retreive the value of the variable by calling
                    833: this method.
                    834: 
1.4       bowersj2  835: =cut
                    836: 
1.5       bowersj2  837: BEGIN {
1.7       bowersj2  838:     &Apache::lonhelper::register('Apache::lonhelper::element',
1.25      bowersj2  839:                                  ('nextstate', 'finalcode',
1.38      bowersj2  840:                                   'defaultvalue', 'validator'));
1.5       bowersj2  841: }
                    842: 
1.4       bowersj2  843: # Because we use the param hash, this is often a sufficent
                    844: # constructor
                    845: sub new {
                    846:     my $proto = shift;
                    847:     my $class = ref($proto) || $proto;
                    848:     my $self = $paramHash;
                    849:     bless($self, $class);
                    850: 
                    851:     $self->{PARAMS} = $paramHash;
                    852:     $self->{STATE} = $state;
                    853:     $state->addElement($self);
                    854:     
                    855:     # Ensure param hash is not reused
                    856:     $paramHash = {};
                    857: 
                    858:     return $self;
                    859: }   
                    860: 
1.5       bowersj2  861: sub start_nextstate {
                    862:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    863: 
                    864:     if ($target ne 'helper') {
                    865:         return '';
                    866:     }
                    867:     
                    868:     $paramHash->{NEXTSTATE} = &Apache::lonxml::get_all_text('/nextstate',
                    869:                                                              $parser);
                    870:     return '';
                    871: }
                    872: 
                    873: sub end_nextstate { return ''; }
                    874: 
1.25      bowersj2  875: sub start_finalcode {
                    876:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    877: 
                    878:     if ($target ne 'helper') {
                    879:         return '';
                    880:     }
                    881:     
                    882:     $paramHash->{FINAL_CODE} = &Apache::lonxml::get_all_text('/finalcode',
                    883:                                                              $parser);
                    884:     return '';
                    885: }
                    886: 
                    887: sub end_finalcode { return ''; }
                    888: 
                    889: sub start_defaultvalue {
                    890:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    891: 
                    892:     if ($target ne 'helper') {
                    893:         return '';
                    894:     }
                    895:     
                    896:     $paramHash->{DEFAULT_VALUE} = &Apache::lonxml::get_all_text('/defaultvalue',
                    897:                                                              $parser);
                    898:     $paramHash->{DEFAULT_VALUE} = 'sub { my $helper = shift; my $state = shift;' .
                    899:         $paramHash->{DEFAULT_VALUE} . '}';
                    900:     return '';
                    901: }
                    902: 
                    903: sub end_defaultvalue { return ''; }
                    904: 
1.57      albertel  905: # Validators may need to take language specifications
1.38      bowersj2  906: sub start_validator {
                    907:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    908: 
                    909:     if ($target ne 'helper') {
                    910:         return '';
                    911:     }
                    912:     
                    913:     $paramHash->{VALIDATOR} = &Apache::lonxml::get_all_text('/validator',
                    914:                                                              $parser);
                    915:     $paramHash->{VALIDATOR} = 'sub { my $helper = shift; my $state = shift; my $element = shift; my $val = shift;' .
                    916:         $paramHash->{VALIDATOR} . '}';
                    917:     return '';
                    918: }
                    919: 
                    920: sub end_validator { return ''; }
                    921: 
1.4       bowersj2  922: sub preprocess {
                    923:     return 1;
                    924: }
                    925: 
                    926: sub postprocess {
                    927:     return 1;
                    928: }
                    929: 
                    930: sub render {
                    931:     return '';
                    932: }
                    933: 
1.13      bowersj2  934: sub overrideForm {
                    935:     return 0;
                    936: }
                    937: 
1.31      bowersj2  938: sub getValue {
                    939:     my $self = shift;
                    940:     return $helper->{VARS}->{$self->{'variable'}};
                    941: }
                    942: 
1.4       bowersj2  943: 1;
                    944: 
                    945: package Apache::lonhelper::message;
                    946: 
                    947: =pod
                    948: 
1.48      bowersj2  949: =head1 Elements
                    950: 
                    951: =head2 Element: messageX<message, helper element>
1.4       bowersj2  952: 
1.44      bowersj2  953: Message elements display their contents, and
                    954: transition directly to the state in the <nextstate> attribute. Example:
1.4       bowersj2  955: 
1.44      bowersj2  956:  <message nextstate='GET_NAME'>
                    957:    This is the <b>message</b> the user will see, 
                    958:                  <i>HTML allowed</i>.
1.4       bowersj2  959:    </message>
                    960: 
1.44      bowersj2  961: This will display the HTML message and transition to the 'nextstate' if
1.7       bowersj2  962: given. The HTML will be directly inserted into the helper, so if you don't
1.44      bowersj2  963: want text to run together, you'll need to manually wrap the message text
1.4       bowersj2  964: in <p> tags, or whatever is appropriate for your HTML.
                    965: 
1.5       bowersj2  966: Message tags do not add in whitespace, so if you want it, you'll need to add
                    967: it into states. This is done so you can inline some elements, such as 
                    968: the <date> element, right between two messages, giving the appearence that 
                    969: the <date> element appears inline. (Note the elements can not be embedded
                    970: within each other.)
                    971: 
1.4       bowersj2  972: This is also a good template for creating your own new states, as it has
                    973: very little code beyond the state template.
                    974: 
1.57      albertel  975: =head3 Localization
                    976: 
                    977: The contents of the message tag will be run through the
                    978: normalize_string function and that will be used as a call to &mt.
                    979: 
1.4       bowersj2  980: =cut
                    981: 
                    982: no strict;
                    983: @ISA = ("Apache::lonhelper::element");
                    984: use strict;
1.57      albertel  985: use Apache::lonlocal;
1.4       bowersj2  986: 
                    987: BEGIN {
1.7       bowersj2  988:     &Apache::lonhelper::register('Apache::lonhelper::message',
1.10      bowersj2  989:                               ('message'));
1.3       bowersj2  990: }
                    991: 
1.5       bowersj2  992: sub new {
                    993:     my $ref = Apache::lonhelper::element->new();
                    994:     bless($ref);
                    995: }
1.4       bowersj2  996: 
                    997: # CONSTRUCTION: Construct the message element from the XML
                    998: sub start_message {
                    999:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1000: 
                   1001:     if ($target ne 'helper') {
                   1002:         return '';
                   1003:     }
1.10      bowersj2 1004: 
1.69      sakharuk 1005:     $paramHash->{MESSAGE_TEXT} = &mtn(&Apache::lonxml::get_all_text('/message',
                   1006:                                                                $parser));
1.10      bowersj2 1007: 
                   1008:     if (defined($token->[2]{'nextstate'})) {
                   1009:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
                   1010:     }
1.4       bowersj2 1011:     return '';
1.3       bowersj2 1012: }
                   1013: 
1.10      bowersj2 1014: sub end_message {
1.4       bowersj2 1015:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1016: 
                   1017:     if ($target ne 'helper') {
                   1018:         return '';
                   1019:     }
1.10      bowersj2 1020:     Apache::lonhelper::message->new();
                   1021:     return '';
1.5       bowersj2 1022: }
                   1023: 
                   1024: sub render {
                   1025:     my $self = shift;
                   1026: 
1.57      albertel 1027:     return &mtn($self->{MESSAGE_TEXT});
1.5       bowersj2 1028: }
                   1029: # If a NEXTSTATE was given, switch to it
                   1030: sub postprocess {
                   1031:     my $self = shift;
                   1032:     if (defined($self->{NEXTSTATE})) {
                   1033:         $helper->changeState($self->{NEXTSTATE});
                   1034:     }
1.6       bowersj2 1035: 
                   1036:     return 1;
1.5       bowersj2 1037: }
                   1038: 1;
                   1039: 
1.159     albertel 1040: package Apache::lonhelper::helpicon;
                   1041: 
                   1042: =pod
                   1043: 
                   1044: =head1 Elements
                   1045: 
                   1046: =head2 Element: helpiconX<helpicon, helper element>
                   1047: 
                   1048: Helpicon elements add a help icon at the current location.
                   1049: Example:
                   1050: 
                   1051:    <helpicon file="Help">
                   1052:      General Help
                   1053:    </helpicon>
                   1054: 
                   1055: In this example will generate a help icon to the Help.hlp url with a
                   1056: description of 'General Help'. The description is not required and if
                   1057: left out (Example: <helpicon file="Help" /> only the icon will be
                   1058: added.)
                   1059: 
                   1060: =head3 Localization
                   1061: 
                   1062: The description text will be run through the normalize_string function
                   1063: and that will be used as a call to &mt.
                   1064: 
                   1065: =cut
                   1066: 
                   1067: no strict;
                   1068: @ISA = ("Apache::lonhelper::element");
                   1069: use strict;
                   1070: use Apache::lonlocal;
                   1071: 
                   1072: BEGIN {
                   1073:     &Apache::lonhelper::register('Apache::lonhelper::helpicon',
                   1074: 				 ('helpicon'));
                   1075: }
                   1076: 
                   1077: sub new {
                   1078:     my $ref = Apache::lonhelper::element->new();
                   1079:     bless($ref);
                   1080: }
                   1081: 
                   1082: # CONSTRUCTION: Construct the message element from the XML
                   1083: sub start_helpicon {
                   1084:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1085: 
                   1086:     if ($target ne 'helper') {
                   1087:         return '';
                   1088:     }
                   1089: 
                   1090:     $paramHash->{HELP_TEXT} = &mtn(&Apache::lonxml::get_all_text('/helpicon',
                   1091: 								 $parser));
                   1092: 
                   1093:     $paramHash->{HELP_TEXT} =~s/^\s+//;
                   1094:     $paramHash->{HELP_TEXT} =~s/\s+$//;
                   1095: 
                   1096:     if (defined($token->[2]{'file'})) {
                   1097:         $paramHash->{HELP_FILE} = $token->[2]{'file'};
                   1098:     }
                   1099:     return '';
                   1100: }
                   1101: 
                   1102: sub end_helpicon {
                   1103:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1104: 
                   1105:     if ($target ne 'helper') {
                   1106:         return '';
                   1107:     }
                   1108:     Apache::lonhelper::helpicon->new();
                   1109:     return '';
                   1110: }
                   1111: 
                   1112: sub render {
                   1113:     my $self = shift;
                   1114: 
                   1115:     my $text;
                   1116:     if ( $self->{HELP_TEXT} ne '') {
1.160   ! albertel 1117: 	$text=&mtn($self->{HELP_TEXT});
1.159     albertel 1118:     }
                   1119: 
                   1120:     return &Apache::loncommon::help_open_topic($self->{HELP_FILE},
                   1121: 					       $text);
                   1122: }
1.160   ! albertel 1123: sub postprocess {
        !          1124:     my $self = shift;
        !          1125:     if (defined($self->{NEXTSTATE})) {
        !          1126:         $helper->changeState($self->{NEXTSTATE});
        !          1127:     }
        !          1128: 
        !          1129:     return 1;
        !          1130: }
        !          1131: 
1.159     albertel 1132: 1;
                   1133: 
1.155     albertel 1134: package Apache::lonhelper::skip;
                   1135: 
                   1136: =pod
                   1137: 
                   1138: =head1 Elements
                   1139: 
                   1140: =head2 Element: skipX<skip>
                   1141: 
                   1142: The <skip> tag allows you define conditions under which the current state 
                   1143: should be skipped over and define what state to skip to.
                   1144: 
                   1145:   <state name="SKIP">
                   1146:     <skip>
                   1147:        <clause>
                   1148:          #some code that decides whether to skip the state or not
                   1149:        </clause>
                   1150:        <nextstate>FINISH</nextstate>
                   1151:     </skip>
                   1152:     <message nextstate="FINISH">A possibly skipped state</message>
                   1153:   </state>
                   1154: 
                   1155: =cut
                   1156: 
                   1157: no strict;
                   1158: @ISA = ("Apache::lonhelper::element");
                   1159: use strict;
                   1160: 
                   1161: BEGIN {
                   1162:     &Apache::lonhelper::register('Apache::lonhelper::skip',
                   1163: 				 ('skip'));
                   1164: }
                   1165: 
                   1166: sub new {
                   1167:     my $ref = Apache::lonhelper::element->new();
                   1168:     bless($ref);
                   1169: }
                   1170: 
                   1171: sub start_skip {
                   1172:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1173: 
                   1174:     if ($target ne 'helper') {
                   1175:         return '';
                   1176:     }
                   1177:     # let <cluase> know what text to skip to
                   1178:     $paramHash->{SKIPTAG}='/skip';
                   1179:     return '';
                   1180: }
                   1181: 
                   1182: sub end_skip {
                   1183:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1184: 
                   1185:     if ($target ne 'helper') {
                   1186:         return '';
                   1187:     }
                   1188:     Apache::lonhelper::skip->new();
                   1189:     return '';
                   1190: }
                   1191: 
                   1192: sub render {
                   1193:     my $self = shift;
                   1194:     return '';
                   1195: }
                   1196: # If a NEXTSTATE is set, switch to it
                   1197: sub preprocess {
                   1198:     my ($self) = @_;
                   1199: 
                   1200:     if (defined($self->{NEXTSTATE})) {
                   1201:         $helper->changeState($self->{NEXTSTATE});
                   1202:     }
                   1203: 
                   1204:     return 1;
                   1205: }
                   1206: 
                   1207: 1;
                   1208: 
1.5       bowersj2 1209: package Apache::lonhelper::choices;
                   1210: 
                   1211: =pod
                   1212: 
1.44      bowersj2 1213: =head2 Element: choicesX<choices, helper element>
1.5       bowersj2 1214: 
                   1215: Choice states provide a single choice to the user as a text selection box.
                   1216: A "choice" is two pieces of text, one which will be displayed to the user
                   1217: (the "human" value), and one which will be passed back to the program
                   1218: (the "computer" value). For instance, a human may choose from a list of
                   1219: resources on disk by title, while your program wants the file name.
                   1220: 
                   1221: <choices> takes an attribute "variable" to control which helper variable
                   1222: the result is stored in.
                   1223: 
                   1224: <choices> takes an attribute "multichoice" which, if set to a true
                   1225: value, will allow the user to select multiple choices.
                   1226: 
1.26      bowersj2 1227: <choices> takes an attribute "allowempty" which, if set to a true 
                   1228: value, will allow the user to select none of the choices without raising
                   1229: an error message.
                   1230: 
1.44      bowersj2 1231: =head3 SUB-TAGS
1.5       bowersj2 1232: 
1.44      bowersj2 1233: <choices> can have the following subtags:X<choice, helper tag>
1.5       bowersj2 1234: 
                   1235: =over 4
                   1236: 
                   1237: =item * <nextstate>state_name</nextstate>: If given, this will cause the
1.44      bowersj2 1238:       choice element to transition to the given state after executing.
                   1239:       This will override the <nextstate> passed to <choices> (if any).
1.5       bowersj2 1240: 
                   1241: =item * <choice />: If the choices are static,
                   1242:       this element will allow you to specify them. Each choice
                   1243:       contains  attribute, "computer", as described above. The
                   1244:       content of the tag will be used as the human label.
                   1245:       For example,  
                   1246:       <choice computer='234-12-7312'>Bobby McDormik</choice>.
                   1247: 
1.44      bowersj2 1248: <choice> can take a parameter "eval", which if set to
                   1249: a true value, will cause the contents of the tag to be
                   1250: evaluated as it would be in an <eval> tag; see <eval> tag
                   1251: below.
1.13      bowersj2 1252: 
1.5       bowersj2 1253: <choice> may optionally contain a 'nextstate' attribute, which
1.44      bowersj2 1254: will be the state transistioned to if the choice is made, if
                   1255: the choice is not multichoice. This will override the nextstate
                   1256: passed to the parent C<choices> tag.
1.5       bowersj2 1257: 
1.136     foxr     1258: <choice> may optionally contain a 'relatedvalue' attribute, which
                   1259: if present will cause a text entry to appear to the right of the
                   1260: selection.  The value of the relatedvalue attribute is a variable
                   1261: into which the text entry will be stored e.g.:
                   1262: <choice computer='numberprovided" relatedvalue="num">Type the number in:</choice>
                   1263: 
                   1264: <choice> may contain a relatededefault atribute which, if the
                   1265: relatedvalue attribute is present will be the initial value of the input
                   1266: box.
                   1267: 
1.5       bowersj2 1268: =back
                   1269: 
                   1270: To create the choices programmatically, either wrap the choices in 
                   1271: <condition> tags (prefered), or use an <exec> block inside the <choice>
                   1272: tag. Store the choices in $state->{CHOICES}, which is a list of list
                   1273: references, where each list has three strings. The first is the human
                   1274: name, the second is the computer name. and the third is the option
                   1275: next state. For example:
                   1276: 
                   1277:  <exec>
                   1278:     for (my $i = 65; $i < 65 + 26; $i++) {
                   1279:         push @{$state->{CHOICES}}, [chr($i), $i, 'next'];
                   1280:     }
                   1281:  </exec>
                   1282: 
                   1283: This will allow the user to select from the letters A-Z (in ASCII), while
                   1284: passing the ASCII value back into the helper variables, and the state
                   1285: will in all cases transition to 'next'.
                   1286: 
                   1287: You can mix and match methods of creating choices, as long as you always 
                   1288: "push" onto the choice list, rather then wiping it out. (You can even 
                   1289: remove choices programmatically, but that would probably be bad form.)
                   1290: 
1.44      bowersj2 1291: =head3 defaultvalue support
1.25      bowersj2 1292: 
                   1293: Choices supports default values both in multichoice and single choice mode.
                   1294: In single choice mode, have the defaultvalue tag's function return the 
                   1295: computer value of the box you want checked. If the function returns a value
                   1296: that does not correspond to any of the choices, the default behavior of selecting
                   1297: the first choice will be preserved.
                   1298: 
                   1299: For multichoice, return a string with the computer values you want checked,
                   1300: delimited by triple pipes. Note this matches how the result of the <choices>
                   1301: tag is stored in the {VARS} hash.
                   1302: 
1.5       bowersj2 1303: =cut
                   1304: 
                   1305: no strict;
                   1306: @ISA = ("Apache::lonhelper::element");
                   1307: use strict;
1.57      albertel 1308: use Apache::lonlocal;
1.100     albertel 1309: use Apache::lonnet;
1.5       bowersj2 1310: 
                   1311: BEGIN {
1.7       bowersj2 1312:     &Apache::lonhelper::register('Apache::lonhelper::choices',
1.5       bowersj2 1313:                               ('choice', 'choices'));
                   1314: }
                   1315: 
                   1316: sub new {
                   1317:     my $ref = Apache::lonhelper::element->new();
                   1318:     bless($ref);
                   1319: }
                   1320: 
                   1321: # CONSTRUCTION: Construct the message element from the XML
                   1322: sub start_choices {
                   1323:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1324: 
                   1325:     if ($target ne 'helper') {
                   1326:         return '';
                   1327:     }
                   1328: 
                   1329:     # Need to initialize the choices list, so everything can assume it exists
1.24      sakharuk 1330:     $paramHash->{'variable'} = $token->[2]{'variable'} if (!defined($paramHash->{'variable'}));
1.5       bowersj2 1331:     $helper->declareVar($paramHash->{'variable'});
                   1332:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.26      bowersj2 1333:     $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.5       bowersj2 1334:     $paramHash->{CHOICES} = [];
                   1335:     return '';
                   1336: }
                   1337: 
                   1338: sub end_choices {
                   1339:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1340: 
                   1341:     if ($target ne 'helper') {
                   1342:         return '';
                   1343:     }
                   1344:     Apache::lonhelper::choices->new();
                   1345:     return '';
                   1346: }
                   1347: 
                   1348: sub start_choice {
                   1349:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1350: 
                   1351:     if ($target ne 'helper') {
                   1352:         return '';
                   1353:     }
                   1354: 
                   1355:     my $computer = $token->[2]{'computer'};
1.69      sakharuk 1356:     my $human = &mt(&Apache::lonxml::get_all_text('/choice',
                   1357:                                               $parser));
1.136     foxr     1358:     my $nextstate  = $token->[2]{'nextstate'};
                   1359:     my $evalFlag   = $token->[2]{'eval'};
                   1360:     my $relatedVar = $token->[2]{'relatedvalue'}; 
                   1361:     my $relatedDefault = $token->[2]{'relateddefault'};
1.94      albertel 1362:     push @{$paramHash->{CHOICES}}, [&mtn($human), $computer, $nextstate, 
1.136     foxr     1363:                                     $evalFlag, $relatedVar, $relatedDefault];
1.5       bowersj2 1364:     return '';
                   1365: }
                   1366: 
                   1367: sub end_choice {
                   1368:     return '';
                   1369: }
                   1370: 
1.87      matthew  1371: {
                   1372:     # used to generate unique id attributes for <input> tags. 
                   1373:     # internal use only.
                   1374:     my $id = 0;
                   1375:     sub new_id { return $id++; }
                   1376: }
                   1377: 
1.5       bowersj2 1378: sub render {
                   1379:     my $self = shift;
                   1380:     my $var = $self->{'variable'};
                   1381:     my $buttons = '';
                   1382:     my $result = '';
                   1383: 
                   1384:     if ($self->{'multichoice'}) {
1.6       bowersj2 1385:         $result .= <<SCRIPT;
1.112     albertel 1386: <script type="text/javascript">
                   1387: // <!--
1.18      bowersj2 1388:     function checkall(value, checkName) {
1.15      bowersj2 1389: 	for (i=0; i<document.forms.helpform.elements.length; i++) {
1.18      bowersj2 1390:             ele = document.forms.helpform.elements[i];
1.157     raeburn  1391:             if (ele.name == checkName + '_forminput') {
1.18      bowersj2 1392:                 document.forms.helpform.elements[i].checked=value;
                   1393:             }
1.5       bowersj2 1394:         }
                   1395:     }
1.112     albertel 1396: // -->
1.5       bowersj2 1397: </script>
                   1398: SCRIPT
1.25      bowersj2 1399:     }
                   1400: 
                   1401:     # Only print "select all" and "unselect all" if there are five or
                   1402:     # more choices; fewer then that and it looks silly.
                   1403:     if ($self->{'multichoice'} && scalar(@{$self->{CHOICES}}) > 4) {
1.68      sakharuk 1404:         my %lt=&Apache::lonlocal::texthash(
                   1405: 			'sa'  => "Select All",
                   1406: 		        'ua'  => "Unselect All");
1.5       bowersj2 1407:         $buttons = <<BUTTONS;
                   1408: <br />
1.68      sakharuk 1409: <input type="button" onclick="checkall(true, '$var')" value="$lt{'sa'}" />
                   1410: <input type="button" onclick="checkall(false, '$var')" value="$lt{'ua'}" />
1.6       bowersj2 1411: <br />&nbsp;
1.5       bowersj2 1412: BUTTONS
                   1413:     }
                   1414: 
1.16      bowersj2 1415:     if (defined $self->{ERROR_MSG}) {
1.6       bowersj2 1416:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
1.5       bowersj2 1417:     }
                   1418: 
                   1419:     $result .= $buttons;
1.6       bowersj2 1420:     
1.5       bowersj2 1421:     $result .= "<table>\n\n";
                   1422: 
1.25      bowersj2 1423:     my %checkedChoices;
                   1424:     my $checkedChoicesFunc;
                   1425: 
                   1426:     if (defined($self->{DEFAULT_VALUE})) {
                   1427:         $checkedChoicesFunc = eval ($self->{DEFAULT_VALUE});
                   1428:         die 'Error in default value code for variable ' . 
1.34      bowersj2 1429:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
1.25      bowersj2 1430:     } else {
                   1431:         $checkedChoicesFunc = sub { return ''; };
                   1432:     }
                   1433: 
                   1434:     # Process which choices should be checked.
                   1435:     if ($self->{'multichoice'}) {
                   1436:         for my $selectedChoice (split(/\|\|\|/, (&$checkedChoicesFunc($helper, $self)))) {
                   1437:             $checkedChoices{$selectedChoice} = 1;
                   1438:         }
                   1439:     } else {
                   1440:         # single choice
                   1441:         my $selectedChoice = &$checkedChoicesFunc($helper, $self);
                   1442:         
                   1443:         my $foundChoice = 0;
                   1444:         
                   1445:         # check that the choice is in the list of choices.
                   1446:         for my $choice (@{$self->{CHOICES}}) {
                   1447:             if ($choice->[1] eq $selectedChoice) {
                   1448:                 $checkedChoices{$choice->[1]} = 1;
                   1449:                 $foundChoice = 1;
                   1450:             }
                   1451:         }
                   1452:         
                   1453:         # If we couldn't find the choice, pick the first one 
                   1454:         if (!$foundChoice) {
                   1455:             $checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
                   1456:         }
                   1457:     }
                   1458: 
1.5       bowersj2 1459:     my $type = "radio";
                   1460:     if ($self->{'multichoice'}) { $type = 'checkbox'; }
                   1461:     foreach my $choice (@{$self->{CHOICES}}) {
1.87      matthew  1462:         my $id = &new_id();
1.5       bowersj2 1463:         $result .= "<tr>\n<td width='20'>&nbsp;</td>\n";
1.157     raeburn  1464:         $result .= "<td valign='top'><input type='$type' name='${var}_forminput'"
1.111     albertel 1465:             . " value='" . 
1.89      foxr     1466:             HTML::Entities::encode($choice->[1],"<>&\"'") 
1.5       bowersj2 1467:             . "'";
1.25      bowersj2 1468:         if ($checkedChoices{$choice->[1]}) {
1.111     albertel 1469:             $result .= " checked='checked' ";
1.5       bowersj2 1470:         }
1.111     albertel 1471:         $result .= qq{id="id$id"};
1.13      bowersj2 1472:         my $choiceLabel = $choice->[0];
1.136     foxr     1473:         if ($choice->[3]) {  # if we need to evaluate this choice
1.13      bowersj2 1474:             $choiceLabel = "sub { my $helper = shift; my $state = shift;" .
                   1475:                 $choiceLabel . "}";
                   1476:             $choiceLabel = eval($choiceLabel);
                   1477:             $choiceLabel = &$choiceLabel($helper, $self);
                   1478:         }
1.111     albertel 1479:         $result .= "/></td><td> ".qq{<label for="id$id">}.
1.136     foxr     1480:             $choiceLabel. "</label></td>";
                   1481: 	if ($choice->[4]) {
                   1482: 	    $result .='<td><input type="text" size="5" name="'
1.157     raeburn  1483: 		.$choice->[4].'_forminput" value="'
1.136     foxr     1484:                 .$choice->[5].'" /></td>';
                   1485: 	}
                   1486: 	$result .= "</tr>\n";
1.5       bowersj2 1487:     }
                   1488:     $result .= "</table>\n\n\n";
                   1489:     $result .= $buttons;
                   1490: 
                   1491:     return $result;
                   1492: }
                   1493: 
                   1494: # If a NEXTSTATE was given or a nextstate for this choice was
                   1495: # given, switch to it
                   1496: sub postprocess {
                   1497:     my $self = shift;
1.157     raeburn  1498:     my $chosenValue = $env{'form.' . $self->{'variable'} . '_forminput'};
1.5       bowersj2 1499: 
1.26      bowersj2 1500:     if (!defined($chosenValue) && !$self->{'allowempty'}) {
1.59      bowersj2 1501:         $self->{ERROR_MSG} = 
                   1502: 	    &mt("You must choose one or more choices to continue.");
1.6       bowersj2 1503:         return 0;
                   1504:     }
                   1505: 
1.28      bowersj2 1506:     if (ref($chosenValue)) {
                   1507:         $helper->{VARS}->{$self->{'variable'}} = join('|||', @$chosenValue);
1.42      bowersj2 1508:     }
                   1509: 
                   1510:     if (defined($self->{NEXTSTATE})) {
                   1511:         $helper->changeState($self->{NEXTSTATE});
                   1512:     }
                   1513:     
                   1514:     foreach my $choice (@{$self->{CHOICES}}) {
                   1515:         if ($choice->[1] eq $chosenValue) {
                   1516:             if (defined($choice->[2])) {
                   1517:                 $helper->changeState($choice->[2]);
                   1518:             }
                   1519:         }
1.136     foxr     1520: 	if ($choice->[4]) {
                   1521: 	    my $varname = $choice->[4];
1.157     raeburn  1522: 	    $helper->{'VARS'}->{$varname} = $env{'form.'."${varname}_forminput"};
1.136     foxr     1523: 	}
1.42      bowersj2 1524:     }
                   1525:     return 1;
                   1526: }
                   1527: 1;
                   1528: 
                   1529: package Apache::lonhelper::dropdown;
                   1530: 
                   1531: =pod
                   1532: 
1.44      bowersj2 1533: =head2 Element: dropdownX<dropdown, helper tag>
1.42      bowersj2 1534: 
                   1535: A drop-down provides a drop-down box instead of a radio button
                   1536: box. Because most people do not know how to use a multi-select
                   1537: drop-down box, that option is not allowed. Otherwise, the arguments
                   1538: are the same as "choices", except "allowempty" is also meaningless.
                   1539: 
                   1540: <dropdown> takes an attribute "variable" to control which helper variable
                   1541: the result is stored in.
                   1542: 
1.44      bowersj2 1543: =head3 SUB-TAGS
1.42      bowersj2 1544: 
                   1545: <choice>, which acts just as it does in the "choices" element.
                   1546: 
                   1547: =cut
                   1548: 
1.57      albertel 1549: # This really ought to be a sibling class to "choice" which is itself
                   1550: # a child of some abstract class.... *shrug*
                   1551: 
1.42      bowersj2 1552: no strict;
                   1553: @ISA = ("Apache::lonhelper::element");
                   1554: use strict;
1.57      albertel 1555: use Apache::lonlocal;
1.100     albertel 1556: use Apache::lonnet;
1.42      bowersj2 1557: 
                   1558: BEGIN {
                   1559:     &Apache::lonhelper::register('Apache::lonhelper::dropdown',
                   1560:                               ('dropdown'));
                   1561: }
                   1562: 
                   1563: sub new {
                   1564:     my $ref = Apache::lonhelper::element->new();
                   1565:     bless($ref);
                   1566: }
                   1567: 
                   1568: # CONSTRUCTION: Construct the message element from the XML
                   1569: sub start_dropdown {
                   1570:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1571: 
                   1572:     if ($target ne 'helper') {
                   1573:         return '';
                   1574:     }
                   1575: 
                   1576:     # Need to initialize the choices list, so everything can assume it exists
                   1577:     $paramHash->{'variable'} = $token->[2]{'variable'} if (!defined($paramHash->{'variable'}));
                   1578:     $helper->declareVar($paramHash->{'variable'});
                   1579:     $paramHash->{CHOICES} = [];
                   1580:     return '';
                   1581: }
                   1582: 
                   1583: sub end_dropdown {
                   1584:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1585: 
                   1586:     if ($target ne 'helper') {
                   1587:         return '';
                   1588:     }
                   1589:     Apache::lonhelper::dropdown->new();
                   1590:     return '';
                   1591: }
                   1592: 
                   1593: sub render {
                   1594:     my $self = shift;
                   1595:     my $var = $self->{'variable'};
                   1596:     my $result = '';
                   1597: 
                   1598:     if (defined $self->{ERROR_MSG}) {
                   1599:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
                   1600:     }
                   1601: 
                   1602:     my %checkedChoices;
                   1603:     my $checkedChoicesFunc;
                   1604: 
                   1605:     if (defined($self->{DEFAULT_VALUE})) {
                   1606:         $checkedChoicesFunc = eval ($self->{DEFAULT_VALUE});
                   1607:         die 'Error in default value code for variable ' . 
                   1608:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
                   1609:     } else {
                   1610:         $checkedChoicesFunc = sub { return ''; };
                   1611:     }
                   1612: 
                   1613:     # single choice
                   1614:     my $selectedChoice = &$checkedChoicesFunc($helper, $self);
                   1615:     
                   1616:     my $foundChoice = 0;
                   1617:     
                   1618:     # check that the choice is in the list of choices.
                   1619:     for my $choice (@{$self->{CHOICES}}) {
                   1620: 	if ($choice->[1] eq $selectedChoice) {
                   1621: 	    $checkedChoices{$choice->[1]} = 1;
                   1622: 	    $foundChoice = 1;
                   1623: 	}
                   1624:     }
                   1625:     
                   1626:     # If we couldn't find the choice, pick the first one 
                   1627:     if (!$foundChoice) {
                   1628: 	$checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
                   1629:     }
                   1630: 
1.157     raeburn  1631:     $result .= "<select name='${var}_forminput'>\n";
1.42      bowersj2 1632:     foreach my $choice (@{$self->{CHOICES}}) {
                   1633:         $result .= "<option value='" . 
1.89      foxr     1634:             HTML::Entities::encode($choice->[1],"<>&\"'") 
1.42      bowersj2 1635:             . "'";
                   1636:         if ($checkedChoices{$choice->[1]}) {
1.111     albertel 1637:             $result .= " selected='selected' ";
1.42      bowersj2 1638:         }
                   1639:         my $choiceLabel = $choice->[0];
                   1640:         if ($choice->[4]) {  # if we need to evaluate this choice
                   1641:             $choiceLabel = "sub { my $helper = shift; my $state = shift;" .
                   1642:                 $choiceLabel . "}";
                   1643:             $choiceLabel = eval($choiceLabel);
                   1644:             $choiceLabel = &$choiceLabel($helper, $self);
                   1645:         }
1.112     albertel 1646:         $result .= ">" . &mtn($choiceLabel) . "</option>\n";
1.42      bowersj2 1647:     }
1.43      bowersj2 1648:     $result .= "</select>\n";
1.42      bowersj2 1649: 
                   1650:     return $result;
                   1651: }
                   1652: 
                   1653: # If a NEXTSTATE was given or a nextstate for this choice was
                   1654: # given, switch to it
                   1655: sub postprocess {
                   1656:     my $self = shift;
1.157     raeburn  1657:     my $chosenValue = $env{'form.' . $self->{'variable'} . '_forminput'};
1.42      bowersj2 1658: 
                   1659:     if (!defined($chosenValue) && !$self->{'allowempty'}) {
                   1660:         $self->{ERROR_MSG} = "You must choose one or more choices to" .
                   1661:             " continue.";
                   1662:         return 0;
1.6       bowersj2 1663:     }
                   1664: 
1.5       bowersj2 1665:     if (defined($self->{NEXTSTATE})) {
                   1666:         $helper->changeState($self->{NEXTSTATE});
                   1667:     }
                   1668:     
                   1669:     foreach my $choice (@{$self->{CHOICES}}) {
                   1670:         if ($choice->[1] eq $chosenValue) {
                   1671:             if (defined($choice->[2])) {
                   1672:                 $helper->changeState($choice->[2]);
                   1673:             }
                   1674:         }
                   1675:     }
1.6       bowersj2 1676:     return 1;
1.5       bowersj2 1677: }
                   1678: 1;
                   1679: 
                   1680: package Apache::lonhelper::date;
                   1681: 
                   1682: =pod
                   1683: 
1.44      bowersj2 1684: =head2 Element: dateX<date, helper element>
1.5       bowersj2 1685: 
                   1686: Date elements allow the selection of a date with a drop down list.
                   1687: 
                   1688: Date elements can take two attributes:
                   1689: 
                   1690: =over 4
                   1691: 
                   1692: =item * B<variable>: The name of the variable to store the chosen
                   1693:         date in. Required.
                   1694: 
                   1695: =item * B<hoursminutes>: If a true value, the date will show hours
                   1696:         and minutes, as well as month/day/year. If false or missing,
                   1697:         the date will only show the month, day, and year.
                   1698: 
                   1699: =back
                   1700: 
                   1701: Date elements contain only an option <nextstate> tag to determine
                   1702: the next state.
                   1703: 
                   1704: Example:
                   1705: 
                   1706:  <date variable="DUE_DATE" hoursminutes="1">
                   1707:    <nextstate>choose_why</nextstate>
                   1708:    </date>
                   1709: 
                   1710: =cut
                   1711: 
                   1712: no strict;
                   1713: @ISA = ("Apache::lonhelper::element");
                   1714: use strict;
1.57      albertel 1715: use Apache::lonlocal; # A localization nightmare
1.100     albertel 1716: use Apache::lonnet;
1.5       bowersj2 1717: use Time::localtime;
                   1718: 
                   1719: BEGIN {
1.7       bowersj2 1720:     &Apache::lonhelper::register('Apache::lonhelper::date',
1.5       bowersj2 1721:                               ('date'));
                   1722: }
                   1723: 
                   1724: # Don't need to override the "new" from element
                   1725: sub new {
                   1726:     my $ref = Apache::lonhelper::element->new();
                   1727:     bless($ref);
                   1728: }
                   1729: 
                   1730: my @months = ("January", "February", "March", "April", "May", "June", "July",
                   1731: 	      "August", "September", "October", "November", "December");
                   1732: 
                   1733: # CONSTRUCTION: Construct the message element from the XML
                   1734: sub start_date {
                   1735:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1736: 
                   1737:     if ($target ne 'helper') {
                   1738:         return '';
                   1739:     }
                   1740: 
                   1741:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   1742:     $helper->declareVar($paramHash->{'variable'});
                   1743:     $paramHash->{'hoursminutes'} = $token->[2]{'hoursminutes'};
1.118     albertel 1744:     $paramHash->{'anytime'} = $token->[2]{'anytime'};
1.5       bowersj2 1745: }
                   1746: 
                   1747: sub end_date {
                   1748:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1749: 
                   1750:     if ($target ne 'helper') {
                   1751:         return '';
                   1752:     }
                   1753:     Apache::lonhelper::date->new();
                   1754:     return '';
                   1755: }
                   1756: 
                   1757: sub render {
                   1758:     my $self = shift;
                   1759:     my $result = "";
                   1760:     my $var = $self->{'variable'};
                   1761: 
                   1762:     my $date;
1.118     albertel 1763: 
                   1764:     my $time=time;
1.119     albertel 1765:     my ($anytime,$onclick);
1.118     albertel 1766: 
1.137     albertel 1767: 
                   1768:     # first check VARS for a valid new value from the user
                   1769:     # then check DEFAULT_VALUE for a valid default time value
                   1770:     # otherwise pick now as reasonably good time
                   1771: 
                   1772:     if (defined($helper->{VARS}{$var})
                   1773: 	&&  $helper->{VARS}{$var} > 0) {
                   1774: 	$date = localtime($helper->{VARS}{$var});
                   1775:     } elsif (defined($self->{DEFAULT_VALUE})) {
1.118     albertel 1776:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   1777:         die('Error in default value code for variable ' . 
                   1778:             $self->{'variable'} . ', Perl said: ' . $@) if $@;
                   1779:         $time = &$valueFunc($helper, $self);
1.130     albertel 1780: 	if (lc($time) eq 'anytime') {
                   1781: 	    $anytime=1;
1.137     albertel 1782: 	    $date = localtime(time);
                   1783: 	    $date->min(0);
                   1784: 	} elsif (defined($time) && $time ne 0) {
                   1785: 	    $date = localtime($time);
1.130     albertel 1786: 	} else {
1.137     albertel 1787: 	    # leave date undefined so it'll default to now
1.130     albertel 1788: 	}
1.137     albertel 1789:     }
1.130     albertel 1790: 
1.138     albertel 1791:     if (!defined($date)) {
1.137     albertel 1792: 	$date = localtime(time);
                   1793: 	$date->min(0);
1.118     albertel 1794:     }
1.137     albertel 1795: 
1.119     albertel 1796:     if ($anytime) {
                   1797: 	$onclick = "onclick=\"javascript:updateCheck(this.form,'${var}anytime',false)\"";
                   1798:     }
1.5       bowersj2 1799:     # Default date: The current hour.
                   1800: 
                   1801:     if (defined $self->{ERROR_MSG}) {
                   1802:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                   1803:     }
                   1804: 
                   1805:     # Month
                   1806:     my $i;
1.119     albertel 1807:     $result .= "<select $onclick name='${var}month'>\n";
1.5       bowersj2 1808:     for ($i = 0; $i < 12; $i++) {
                   1809:         if ($i == $date->mon) {
1.111     albertel 1810:             $result .= "<option value='$i' selected='selected'>";
1.5       bowersj2 1811:         } else {
                   1812:             $result .= "<option value='$i'>";
                   1813:         }
1.57      albertel 1814:         $result .= &mt($months[$i]) . "</option>\n";
1.5       bowersj2 1815:     }
                   1816:     $result .= "</select>\n";
                   1817: 
                   1818:     # Day
1.119     albertel 1819:     $result .= "<select $onclick name='${var}day'>\n";
1.5       bowersj2 1820:     for ($i = 1; $i < 32; $i++) {
                   1821:         if ($i == $date->mday) {
1.111     albertel 1822:             $result .= '<option selected="selected">';
1.5       bowersj2 1823:         } else {
                   1824:             $result .= '<option>';
                   1825:         }
                   1826:         $result .= "$i</option>\n";
                   1827:     }
                   1828:     $result .= "</select>,\n";
                   1829: 
                   1830:     # Year
1.119     albertel 1831:     $result .= "<select $onclick name='${var}year'>\n";
1.5       bowersj2 1832:     for ($i = 2000; $i < 2030; $i++) { # update this after 64-bit dates
                   1833:         if ($date->year + 1900 == $i) {
1.111     albertel 1834:             $result .= "<option selected='selected'>";
1.5       bowersj2 1835:         } else {
                   1836:             $result .= "<option>";
                   1837:         }
                   1838:         $result .= "$i</option>\n";
                   1839:     }
                   1840:     $result .= "</select>,\n";
                   1841: 
                   1842:     # Display Hours and Minutes if they are called for
                   1843:     if ($self->{'hoursminutes'}) {
1.59      bowersj2 1844: 	# This needs parameterization for times.
                   1845: 	my $am = &mt('a.m.');
                   1846: 	my $pm = &mt('p.m.');
1.5       bowersj2 1847:         # Build hour
1.119     albertel 1848:         $result .= "<select $onclick name='${var}hour'>\n";
1.111     albertel 1849:         $result .= "<option " . ($date->hour == 0 ? 'selected="selected" ':'') .
1.59      bowersj2 1850:             " value='0'>" . &mt('midnight') . "</option>\n";
1.5       bowersj2 1851:         for ($i = 1; $i < 12; $i++) {
                   1852:             if ($date->hour == $i) {
1.111     albertel 1853:                 $result .= "<option selected='selected' value='$i'>$i $am</option>\n";
1.5       bowersj2 1854:             } else {
1.59      bowersj2 1855:                 $result .= "<option value='$i'>$i $am</option>\n";
1.5       bowersj2 1856:             }
                   1857:         }
1.111     albertel 1858:         $result .= "<option " . ($date->hour == 12 ? 'selected="selected" ':'') .
1.59      bowersj2 1859:             " value='12'>" . &mt('noon') . "</option>\n";
1.5       bowersj2 1860:         for ($i = 13; $i < 24; $i++) {
                   1861:             my $printedHour = $i - 12;
                   1862:             if ($date->hour == $i) {
1.111     albertel 1863:                 $result .= "<option selected='selected' value='$i'>$printedHour $pm</option>\n";
1.5       bowersj2 1864:             } else {
1.59      bowersj2 1865:                 $result .= "<option value='$i'>$printedHour $pm</option>\n";
1.5       bowersj2 1866:             }
                   1867:         }
                   1868: 
                   1869:         $result .= "</select> :\n";
                   1870: 
1.119     albertel 1871:         $result .= "<select $onclick name='${var}minute'>\n";
1.120     albertel 1872: 	my $selected=0;
                   1873:         for my $i ((0,15,30,45,59,undef,0..59)) {
1.5       bowersj2 1874:             my $printedMinute = $i;
1.117     albertel 1875:             if (defined($i) && $i < 10) {
1.5       bowersj2 1876:                 $printedMinute = "0" . $printedMinute;
                   1877:             }
1.120     albertel 1878:             if (!$selected && $date->min == $i) {
1.111     albertel 1879:                 $result .= "<option selected='selected'>";
1.120     albertel 1880: 		$selected=1;
1.5       bowersj2 1881:             } else {
                   1882:                 $result .= "<option>";
                   1883:             }
                   1884:             $result .= "$printedMinute</option>\n";
                   1885:         }
                   1886:         $result .= "</select>\n";
                   1887:     }
1.118     albertel 1888:     if ($self->{'anytime'}) {
1.121     albertel 1889: 	$result.=(<<CHECK);
1.119     albertel 1890: <script type="text/javascript">
                   1891: // <!--
                   1892:     function updateCheck(form,name,value) {
                   1893: 	var checkbox=form[name];
                   1894: 	checkbox.checked = value;
                   1895:     }
                   1896: // -->
                   1897: </script>
                   1898: CHECK
1.118     albertel 1899: 	$result.="&nbsp;or&nbsp;<label><input type='checkbox' ";
                   1900: 	if ($anytime) {
                   1901: 	    $result.=' checked="checked" '
                   1902: 	}
1.138     albertel 1903: 	$result.="name='${var}anytime'/>".&mt('Any time').'</label>'
1.118     albertel 1904:     }
1.5       bowersj2 1905:     return $result;
                   1906: 
                   1907: }
                   1908: # If a NEXTSTATE was given, switch to it
                   1909: sub postprocess {
                   1910:     my $self = shift;
                   1911:     my $var = $self->{'variable'};
1.118     albertel 1912:     if ($env{'form.' . $var . 'anytime'}) {
                   1913: 	$helper->{VARS}->{$var} = undef;
                   1914:     } else {
                   1915: 	my $month = $env{'form.' . $var . 'month'}; 
                   1916: 	my $day = $env{'form.' . $var . 'day'}; 
                   1917: 	my $year = $env{'form.' . $var . 'year'}; 
                   1918: 	my $min = 0; 
                   1919: 	my $hour = 0;
                   1920: 	if ($self->{'hoursminutes'}) {
                   1921: 	    $min = $env{'form.' . $var . 'minute'};
                   1922: 	    $hour = $env{'form.' . $var . 'hour'};
                   1923: 	}
                   1924: 
                   1925: 	my $chosenDate;
                   1926: 	eval {$chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);};
                   1927: 	my $error = $@;
                   1928: 
                   1929: 	# Check to make sure that the date was not automatically co-erced into a 
                   1930: 	# valid date, as we want to flag that as an error
                   1931: 	# This happens for "Feb. 31", for instance, which is coerced to March 2 or
                   1932: 	# 3, depending on if it's a leap year
                   1933: 	my $checkDate = localtime($chosenDate);
                   1934: 	
                   1935: 	if ($error || $checkDate->mon != $month || $checkDate->mday != $day ||
                   1936: 	    $checkDate->year + 1900 != $year) {
                   1937: 	    unless (Apache::lonlocal::current_language()== ~/^en/) {
                   1938: 		$self->{ERROR_MSG} = &mt("Invalid date entry");
                   1939: 		return 0;
                   1940: 	    }
                   1941: 	    # LOCALIZATION FIXME: Needs to be parameterized
                   1942: 	    $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
                   1943: 		. "date because it doesn't exist. Please enter a valid date.";
1.5       bowersj2 1944: 
1.57      albertel 1945: 	    return 0;
                   1946: 	}
1.118     albertel 1947: 	$helper->{VARS}->{$var} = $chosenDate;
1.5       bowersj2 1948:     }
                   1949: 
1.137     albertel 1950:     if (defined($self->{VALIDATOR})) {
                   1951: 	my $validator = eval($self->{VALIDATOR});
1.138     albertel 1952: 	die 'Died during evaluation of validator code; Perl said: ' . $@ if $@;
1.137     albertel 1953: 	my $invalid = &$validator($helper, $state, $self, $self->getValue());
                   1954: 	if ($invalid) {
                   1955: 	    $self->{ERROR_MSG} = $invalid;
                   1956: 	    return 0;
                   1957: 	}
                   1958:     }
                   1959: 
1.5       bowersj2 1960:     if (defined($self->{NEXTSTATE})) {
                   1961:         $helper->changeState($self->{NEXTSTATE});
                   1962:     }
1.6       bowersj2 1963: 
                   1964:     return 1;
1.5       bowersj2 1965: }
                   1966: 1;
                   1967: 
                   1968: package Apache::lonhelper::resource;
                   1969: 
                   1970: =pod
                   1971: 
1.44      bowersj2 1972: =head2 Element: resourceX<resource, helper element>
1.5       bowersj2 1973: 
                   1974: <resource> elements allow the user to select one or multiple resources
                   1975: from the current course. You can filter out which resources they can view,
                   1976: and filter out which resources they can select. The course will always
                   1977: be displayed fully expanded, because of the difficulty of maintaining
                   1978: selections across folder openings and closings. If this is fixed, then
                   1979: the user can manipulate the folders.
                   1980: 
                   1981: <resource> takes the standard variable attribute to control what helper
1.44      bowersj2 1982: variable stores the results. It also takes a "multichoice"X<multichoice> attribute,
1.17      bowersj2 1983: which controls whether the user can select more then one resource. The 
                   1984: "toponly" attribute controls whether the resource display shows just the
                   1985: resources in that sequence, or recurses into all sub-sequences, defaulting
1.29      bowersj2 1986: to false. The "suppressEmptySequences" attribute reflects the 
                   1987: suppressEmptySequences argument to the render routine, which will cause
                   1988: folders that have all of their contained resources filtered out to also
1.46      bowersj2 1989: be filtered out. The 'addstatus' attribute, if true, will add the icon
1.95      albertel 1990: and long status display columns to the display. The 'addparts'
                   1991: attribute will add in a part selector beside problems that have more
                   1992: than 1 part.
1.5       bowersj2 1993: 
1.44      bowersj2 1994: =head3 SUB-TAGS
1.5       bowersj2 1995: 
                   1996: =over 4
                   1997: 
1.44      bowersj2 1998: =item * <filterfunc>X<filterfunc>: If you want to filter what resources are displayed
1.5       bowersj2 1999:   to the user, use a filter func. The <filterfunc> tag should contain
                   2000:   Perl code that when wrapped with "sub { my $res = shift; " and "}" is 
                   2001:   a function that returns true if the resource should be displayed, 
                   2002:   and false if it should be skipped. $res is a resource object. 
                   2003:   (See Apache::lonnavmaps documentation for information about the 
                   2004:   resource object.)
                   2005: 
1.44      bowersj2 2006: =item * <choicefunc>X<choicefunc>: Same as <filterfunc>, except that controls whether
1.5       bowersj2 2007:   the given resource can be chosen. (It is almost always a good idea to
                   2008:   show the user the folders, for instance, but you do not always want to 
                   2009:   let the user select them.)
                   2010: 
                   2011: =item * <nextstate>: Standard nextstate behavior.
                   2012: 
1.44      bowersj2 2013: =item * <valuefunc>X<valuefunc>: This function controls what is returned by the resource
1.5       bowersj2 2014:   when the user selects it. Like filterfunc and choicefunc, it should be
                   2015:   a function fragment that when wrapped by "sub { my $res = shift; " and
                   2016:   "}" returns a string representing what you want to have as the value. By
                   2017:   default, the value will be the resource ID of the object ($res->{ID}).
                   2018: 
1.44      bowersj2 2019: =item * <mapurl>X<mapurl>: If the URL of a map is given here, only that map
1.48      bowersj2 2020:   will be displayed, instead of the whole course. If the attribute
                   2021:   "evaluate" is given and is true, the contents of the mapurl will be
                   2022:   evaluated with "sub { my $helper = shift; my $state = shift;" and
                   2023:   "}", with the return value used as the mapurl.
1.13      bowersj2 2024: 
1.5       bowersj2 2025: =back
                   2026: 
                   2027: =cut
                   2028: 
                   2029: no strict;
                   2030: @ISA = ("Apache::lonhelper::element");
                   2031: use strict;
1.100     albertel 2032: use Apache::lonnet;
1.5       bowersj2 2033: 
                   2034: BEGIN {
1.7       bowersj2 2035:     &Apache::lonhelper::register('Apache::lonhelper::resource',
1.5       bowersj2 2036:                               ('resource', 'filterfunc', 
1.13      bowersj2 2037:                                'choicefunc', 'valuefunc',
1.90      foxr     2038:                                'mapurl','option'));
1.5       bowersj2 2039: }
                   2040: 
                   2041: sub new {
                   2042:     my $ref = Apache::lonhelper::element->new();
                   2043:     bless($ref);
                   2044: }
                   2045: 
                   2046: # CONSTRUCTION: Construct the message element from the XML
                   2047: sub start_resource {
                   2048:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2049: 
                   2050:     if ($target ne 'helper') {
                   2051:         return '';
                   2052:     }
                   2053: 
                   2054:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   2055:     $helper->declareVar($paramHash->{'variable'});
1.14      bowersj2 2056:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.29      bowersj2 2057:     $paramHash->{'suppressEmptySequences'} = $token->[2]{'suppressEmptySequences'};
1.17      bowersj2 2058:     $paramHash->{'toponly'} = $token->[2]{'toponly'};
1.46      bowersj2 2059:     $paramHash->{'addstatus'} = $token->[2]{'addstatus'};
1.95      albertel 2060:     $paramHash->{'addparts'} = $token->[2]{'addparts'};
                   2061:     if ($paramHash->{'addparts'}) {
                   2062: 	$helper->declareVar($paramHash->{'variable'}.'_part');
                   2063:     }
1.66      albertel 2064:     $paramHash->{'closeallpages'} = $token->[2]{'closeallpages'};
1.5       bowersj2 2065:     return '';
                   2066: }
                   2067: 
                   2068: sub end_resource {
                   2069:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2070: 
                   2071:     if ($target ne 'helper') {
                   2072:         return '';
                   2073:     }
                   2074:     if (!defined($paramHash->{FILTER_FUNC})) {
                   2075:         $paramHash->{FILTER_FUNC} = sub {return 1;};
                   2076:     }
                   2077:     if (!defined($paramHash->{CHOICE_FUNC})) {
                   2078:         $paramHash->{CHOICE_FUNC} = sub {return 1;};
                   2079:     }
                   2080:     if (!defined($paramHash->{VALUE_FUNC})) {
                   2081:         $paramHash->{VALUE_FUNC} = sub {my $res = shift; return $res->{ID}; };
                   2082:     }
                   2083:     Apache::lonhelper::resource->new();
1.4       bowersj2 2084:     return '';
                   2085: }
                   2086: 
1.5       bowersj2 2087: sub start_filterfunc {
                   2088:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2089: 
                   2090:     if ($target ne 'helper') {
                   2091:         return '';
                   2092:     }
                   2093: 
                   2094:     my $contents = Apache::lonxml::get_all_text('/filterfunc',
                   2095:                                                 $parser);
                   2096:     $contents = 'sub { my $res = shift; ' . $contents . '}';
                   2097:     $paramHash->{FILTER_FUNC} = eval $contents;
                   2098: }
                   2099: 
                   2100: sub end_filterfunc { return ''; }
                   2101: 
                   2102: sub start_choicefunc {
                   2103:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2104: 
                   2105:     if ($target ne 'helper') {
                   2106:         return '';
                   2107:     }
                   2108: 
                   2109:     my $contents = Apache::lonxml::get_all_text('/choicefunc',
                   2110:                                                 $parser);
                   2111:     $contents = 'sub { my $res = shift; ' . $contents . '}';
                   2112:     $paramHash->{CHOICE_FUNC} = eval $contents;
                   2113: }
                   2114: 
                   2115: sub end_choicefunc { return ''; }
                   2116: 
                   2117: sub start_valuefunc {
                   2118:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2119: 
                   2120:     if ($target ne 'helper') {
                   2121:         return '';
                   2122:     }
                   2123: 
                   2124:     my $contents = Apache::lonxml::get_all_text('/valuefunc',
                   2125:                                                 $parser);
                   2126:     $contents = 'sub { my $res = shift; ' . $contents . '}';
                   2127:     $paramHash->{VALUE_FUNC} = eval $contents;
                   2128: }
                   2129: 
                   2130: sub end_valuefunc { return ''; }
                   2131: 
1.13      bowersj2 2132: sub start_mapurl {
                   2133:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2134: 
                   2135:     if ($target ne 'helper') {
                   2136:         return '';
                   2137:     }
                   2138: 
                   2139:     my $contents = Apache::lonxml::get_all_text('/mapurl',
                   2140:                                                 $parser);
1.48      bowersj2 2141:     $paramHash->{EVAL_MAP_URL} = $token->[2]{'evaluate'};
1.14      bowersj2 2142:     $paramHash->{MAP_URL} = $contents;
1.13      bowersj2 2143: }
                   2144: 
                   2145: sub end_mapurl { return ''; }
                   2146: 
1.90      foxr     2147: 
                   2148: sub start_option {
                   2149:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2150:     if (!defined($paramHash->{OPTION_TEXTS})) {
                   2151: 	$paramHash->{OPTION_TEXTS} = [ ];
                   2152: 	$paramHash->{OPTION_VARS}  = [ ];
1.91      foxr     2153: 
1.90      foxr     2154:     }
1.91      foxr     2155:     # OPTION_TEXTS is a list of the text attribute
                   2156:     #               values used to create column headings.
                   2157:     # OPTION_VARS is a list of the variable names, used to create the checkbox
                   2158:     #             inputs.
1.90      foxr     2159:     #  We're ok with empty elements. as place holders
                   2160:     # Although the 'variable' element should really exist.
1.91      foxr     2161:     #
                   2162: 
1.90      foxr     2163:     my $option_texts  = $paramHash->{OPTION_TEXTS};
                   2164:     my $option_vars   = $paramHash->{OPTION_VARS};
                   2165:     push(@$option_texts,  $token->[2]{'text'});
                   2166:     push(@$option_vars,   $token->[2]{'variable'});
                   2167: 
1.91      foxr     2168:     #  Need to create and declare the option variables as well to make them
                   2169:     # persistent.
                   2170:     #
                   2171:     my $varname = $token->[2]{'variable'};
                   2172:     $helper->declareVar($varname);
                   2173: 
                   2174: 
1.90      foxr     2175:     return '';
                   2176: }
                   2177: 
                   2178: sub end_option {
                   2179:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2180:     return '';
                   2181: }
                   2182: 
1.5       bowersj2 2183: # A note, in case I don't get to this before I leave.
                   2184: # If someone complains about the "Back" button returning them
                   2185: # to the previous folder state, instead of returning them to
                   2186: # the previous helper state, the *correct* answer is for the helper
                   2187: # to keep track of how many times the user has manipulated the folders,
                   2188: # and feed that to the history.go() call in the helper rendering routines.
                   2189: # If done correctly, the helper itself can keep track of how many times
                   2190: # it renders the same states, so it doesn't go in just this state, and
                   2191: # you can lean on the browser back button to make sure it all chains
                   2192: # correctly.
                   2193: # Right now, though, I'm just forcing all folders open.
                   2194: 
                   2195: sub render {
                   2196:     my $self = shift;
                   2197:     my $result = "";
                   2198:     my $var = $self->{'variable'};
                   2199:     my $curVal = $helper->{VARS}->{$var};
                   2200: 
1.15      bowersj2 2201:     my $buttons = '';
                   2202: 
                   2203:     if ($self->{'multichoice'}) {
                   2204:         $result = <<SCRIPT;
1.112     albertel 2205: <script type="text/javascript">
                   2206: // <!--
1.18      bowersj2 2207:     function checkall(value, checkName) {
1.15      bowersj2 2208: 	for (i=0; i<document.forms.helpform.elements.length; i++) {
                   2209:             ele = document.forms.helpform.elements[i];
1.157     raeburn  2210:             if (ele.name == checkName + '_forminput') {
1.15      bowersj2 2211:                 document.forms.helpform.elements[i].checked=value;
                   2212:             }
                   2213:         }
                   2214:     }
1.112     albertel 2215: // -->
1.15      bowersj2 2216: </script>
                   2217: SCRIPT
1.68      sakharuk 2218:         my %lt=&Apache::lonlocal::texthash(
                   2219: 			'sar'  => "Select All Resources",
                   2220: 		        'uar'  => "Unselect All Resources");
                   2221: 
1.15      bowersj2 2222:         $buttons = <<BUTTONS;
                   2223: <br /> &nbsp;
1.68      sakharuk 2224: <input type="button" onclick="checkall(true, '$var')" value="$lt{'sar'}" />
                   2225: <input type="button" onclick="checkall(false, '$var')" value="$lt{'uar'}" />
1.15      bowersj2 2226: <br /> &nbsp;
                   2227: BUTTONS
                   2228:     }
                   2229: 
1.5       bowersj2 2230:     if (defined $self->{ERROR_MSG}) {
1.14      bowersj2 2231:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1.5       bowersj2 2232:     }
                   2233: 
1.15      bowersj2 2234:     $result .= $buttons;
                   2235: 
1.90      foxr     2236:     my $filterFunc     = $self->{FILTER_FUNC};
                   2237:     my $choiceFunc     = $self->{CHOICE_FUNC};
                   2238:     my $valueFunc      = $self->{VALUE_FUNC};
1.95      albertel 2239:     my $multichoice    = $self->{'multichoice'};
1.90      foxr     2240:     my $option_vars    = $self->{OPTION_VARS};
                   2241:     my $option_texts   = $self->{OPTION_TEXTS};
1.95      albertel 2242:     my $addparts       = $self->{'addparts'};
1.90      foxr     2243:     my $headings_done  = 0;
1.5       bowersj2 2244: 
1.48      bowersj2 2245:     # Evaluate the map url as needed
                   2246:     my $mapUrl;
1.49      bowersj2 2247:     if ($self->{EVAL_MAP_URL}) {
1.48      bowersj2 2248: 	my $mapUrlFunc = eval('sub { my $helper = shift; my $state = shift; ' . 
                   2249: 	    $self->{MAP_URL} . '}');
                   2250: 	$mapUrl = &$mapUrlFunc($helper, $self);
                   2251:     } else {
                   2252: 	$mapUrl = $self->{MAP_URL};
                   2253:     }
                   2254: 
1.125     albertel 2255:     my %defaultSymbs;
1.124     albertel 2256:     if (defined($self->{DEFAULT_VALUE})) {
                   2257:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   2258:         die 'Error in default value code for variable ' . 
                   2259:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
1.125     albertel 2260:         my @defaultSymbs = &$valueFunc($helper, $self);
                   2261: 	if (!$multichoice && @defaultSymbs) { # only allowed 1
1.124     albertel 2262: 	    @defaultSymbs = ($defaultSymbs[0]);
                   2263: 	}
1.125     albertel 2264: 	%defaultSymbs = map { if ($_) {($_,1) } } @defaultSymbs;
                   2265: 	delete($defaultSymbs{''});
1.124     albertel 2266:     }
                   2267: 
1.5       bowersj2 2268:     # Create the composite function that renders the column on the nav map
                   2269:     # have to admit any language that lets me do this can't be all bad
                   2270:     #  - Jeremy (Pythonista) ;-)
                   2271:     my $checked = 0;
                   2272:     my $renderColFunc = sub {
                   2273:         my ($resource, $part, $params) = @_;
1.90      foxr     2274: 	my $result = "";
                   2275: 
                   2276: 	if(!$headings_done) {
                   2277: 	    if ($option_texts) {
                   2278: 		foreach my $text (@$option_texts) {
                   2279: 		    $result .= "<th>$text</th>";
                   2280: 		}
                   2281: 	    }
                   2282: 	    $result .= "<th>Select</th>";
                   2283: 	    $result .= "</tr><tr>"; # Close off the extra row and start a new one.
                   2284: 	    $headings_done = 1;
                   2285: 	}
1.14      bowersj2 2286: 
                   2287:         my $inputType;
                   2288:         if ($multichoice) { $inputType = 'checkbox'; }
                   2289:         else {$inputType = 'radio'; }
                   2290: 
1.5       bowersj2 2291:         if (!&$choiceFunc($resource)) {
1.90      foxr     2292: 	    $result .= '<td>&nbsp;</td>';
                   2293:             return $result;
1.5       bowersj2 2294:         } else {
1.90      foxr     2295: 	    my $col = "";
1.98      foxr     2296: 	    my $raw_name = &$valueFunc($resource);
1.90      foxr     2297: 	    my $resource_name =   
1.98      foxr     2298:                    HTML::Entities::encode($raw_name,"<>&\"'");
1.90      foxr     2299: 	    if($option_vars) {
1.91      foxr     2300: 		foreach my $option_var (@$option_vars) {
1.99      foxr     2301: 		    my $var_value = "\|\|\|" . $helper->{VARS}->{$option_var} . 
                   2302: 			"\|\|\|";
1.98      foxr     2303: 		    my $checked ="";
1.99      foxr     2304: 		    if($var_value =~ /\Q|||$raw_name|||\E/) {
1.111     albertel 2305: 			$checked = "checked='checked'";
1.98      foxr     2306: 		    }
1.90      foxr     2307: 		    $col .= 
1.91      foxr     2308:                         "<td align='center'><input type='checkbox' name ='$option_var".
1.157     raeburn  2309: 			"_forminput' value='".
1.98      foxr     2310: 			$resource_name . "' $checked /> </td>";
1.90      foxr     2311: 		}
                   2312: 	    }
                   2313: 
1.157     raeburn  2314:             $col .= "<td align='center'><input type='$inputType' name='${var}_forminput' ";
1.125     albertel 2315: 	    if (%defaultSymbs) {
1.124     albertel 2316: 		my $symb=$resource->symb();
1.125     albertel 2317: 		if (exists($defaultSymbs{$symb})) {
1.124     albertel 2318: 		    $col .= "checked='checked' ";
                   2319: 		    $checked = 1;
                   2320: 		}
                   2321: 	    } else {
                   2322: 		if (!$checked && !$multichoice) {
                   2323: 		    $col .= "checked='checked' ";
                   2324: 		    $checked = 1;
                   2325: 		}
                   2326: 		if ($multichoice) { # all resources start checked; see bug 1174
                   2327: 		    $col .= "checked='checked' ";
                   2328: 		    $checked = 1;
                   2329: 		}
1.37      bowersj2 2330: 	    }
1.90      foxr     2331:             $col .= "value='" . $resource_name  . "' /></td>";
1.95      albertel 2332: 
1.90      foxr     2333:             return $result.$col;
1.5       bowersj2 2334:         }
                   2335:     };
1.95      albertel 2336:     my $renderPartsFunc = sub {
                   2337:         my ($resource, $part, $params) = @_;
                   2338: 	my $col= "<td>";
                   2339: 	my $id=$resource->{ID};
                   2340: 	my $resource_name =   
                   2341: 	    &HTML::Entities::encode(&$valueFunc($resource),"<>&\"'");
                   2342: 	if ($addparts && (scalar(@{$resource->parts}) > 1)) {
1.157     raeburn  2343: 	    $col .= "<select onclick=\"javascript:updateRadio(this.form,'${var}_forminput','$resource_name');updateHidden(this.form,'$id','${var}');\" name='part_${id}_forminput'>\n";
1.95      albertel 2344: 	    $col .= "<option value=\"$part\">All Parts</option>\n";
                   2345: 	    foreach my $part (@{$resource->parts}) {
                   2346: 		$col .= "<option value=\"$part\">Part: $part</option>\n";
                   2347: 	    }
                   2348: 	    $col .= "</select>";
                   2349: 	}
                   2350: 	$col .= "</td>";
                   2351:     };
                   2352:     $result.=(<<RADIO);
                   2353: <script type="text/javascript">
1.112     albertel 2354: // <!--
1.95      albertel 2355:     function updateRadio(form,name,value) {
                   2356: 	var radiobutton=form[name];
                   2357: 	for (var i=0; i<radiobutton.length; i++) {
                   2358: 	    if (radiobutton[i].value == value) {
                   2359: 		radiobutton[i].checked = true;
                   2360: 		break;
                   2361: 	    }
                   2362: 	}
                   2363:     }
                   2364:     function updateHidden(form,id,name) {
1.157     raeburn  2365: 	var select=form['part_'+id+'_forminput'];
                   2366: 	var hidden=form[name+'_part_forminput'];
1.95      albertel 2367: 	var which=select.selectedIndex;
                   2368: 	hidden.value=select.options[which].value;
                   2369:     }
1.112     albertel 2370: // -->
1.95      albertel 2371: </script>
1.157     raeburn  2372: <input type="hidden" name="${var}_part_forminput" />
1.5       bowersj2 2373: 
1.95      albertel 2374: RADIO
1.100     albertel 2375:     $env{'form.condition'} = !$self->{'toponly'};
1.95      albertel 2376:     my $cols = [$renderColFunc];
                   2377:     if ($self->{'addparts'}) { push(@$cols, $renderPartsFunc); }
                   2378:     push(@$cols, Apache::lonnavmaps::resource());
1.46      bowersj2 2379:     if ($self->{'addstatus'}) {
                   2380: 	push @$cols, (Apache::lonnavmaps::part_status_summary());
                   2381: 	
                   2382:     }
1.5       bowersj2 2383:     $result .= 
1.46      bowersj2 2384:         &Apache::lonnavmaps::render( { 'cols' => $cols,
1.5       bowersj2 2385:                                        'showParts' => 0,
                   2386:                                        'filterFunc' => $filterFunc,
1.13      bowersj2 2387:                                        'resource_no_folder_link' => 1,
1.66      albertel 2388: 				       'closeAllPages' => $self->{'closeallpages'},
1.29      bowersj2 2389:                                        'suppressEmptySequences' => $self->{'suppressEmptySequences'},
1.13      bowersj2 2390:                                        'iterator_map' => $mapUrl }
1.5       bowersj2 2391:                                        );
1.15      bowersj2 2392: 
                   2393:     $result .= $buttons;
1.5       bowersj2 2394:                                                 
                   2395:     return $result;
                   2396: }
                   2397:     
                   2398: sub postprocess {
                   2399:     my $self = shift;
1.14      bowersj2 2400: 
                   2401:     if ($self->{'multichoice'} && !$helper->{VARS}->{$self->{'variable'}}) {
                   2402:         $self->{ERROR_MSG} = 'You must choose at least one resource to continue.';
                   2403:         return 0;
                   2404:     }
                   2405: 
1.5       bowersj2 2406:     if (defined($self->{NEXTSTATE})) {
                   2407:         $helper->changeState($self->{NEXTSTATE});
                   2408:     }
1.6       bowersj2 2409: 
                   2410:     return 1;
1.5       bowersj2 2411: }
                   2412: 
                   2413: 1;
                   2414: 
                   2415: package Apache::lonhelper::student;
                   2416: 
                   2417: =pod
                   2418: 
1.44      bowersj2 2419: =head2 Element: studentX<student, helper element>
1.5       bowersj2 2420: 
                   2421: Student elements display a choice of students enrolled in the current
                   2422: course. Currently it is primitive; this is expected to evolve later.
                   2423: 
1.48      bowersj2 2424: Student elements take the following attributes: 
                   2425: 
                   2426: =over 4
                   2427: 
                   2428: =item * B<variable>: 
                   2429: 
                   2430: Does what it usually does: declare which helper variable to put the
                   2431: result in.
                   2432: 
                   2433: =item * B<multichoice>: 
                   2434: 
                   2435: If true allows the user to select multiple students. Defaults to false.
                   2436: 
                   2437: =item * B<coursepersonnel>: 
                   2438: 
                   2439: If true adds the course personnel to the top of the student
                   2440: selection. Defaults to false.
                   2441: 
                   2442: =item * B<activeonly>:
                   2443: 
                   2444: If true, only active students and course personnel will be
                   2445: shown. Defaults to false.
                   2446: 
1.123     albertel 2447: =item * B<emptyallowed>:
                   2448: 
                   2449: If true, the selection of no users is allowed. Defaults to false.
                   2450: 
1.48      bowersj2 2451: =back
1.5       bowersj2 2452: 
                   2453: =cut
                   2454: 
                   2455: no strict;
                   2456: @ISA = ("Apache::lonhelper::element");
                   2457: use strict;
1.59      bowersj2 2458: use Apache::lonlocal;
1.100     albertel 2459: use Apache::lonnet;
1.139     foxr     2460: 
1.5       bowersj2 2461: BEGIN {
1.7       bowersj2 2462:     &Apache::lonhelper::register('Apache::lonhelper::student',
1.5       bowersj2 2463:                               ('student'));
                   2464: }
                   2465: 
                   2466: sub new {
                   2467:     my $ref = Apache::lonhelper::element->new();
                   2468:     bless($ref);
                   2469: }
1.4       bowersj2 2470: 
1.5       bowersj2 2471: sub start_student {
1.4       bowersj2 2472:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2473: 
                   2474:     if ($target ne 'helper') {
                   2475:         return '';
                   2476:     }
                   2477: 
1.5       bowersj2 2478:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   2479:     $helper->declareVar($paramHash->{'variable'});
                   2480:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.39      bowersj2 2481:     $paramHash->{'coursepersonnel'} = $token->[2]{'coursepersonnel'};
1.93      albertel 2482:     $paramHash->{'activeonly'} = $token->[2]{'activeonly'};
1.12      bowersj2 2483:     if (defined($token->[2]{'nextstate'})) {
                   2484:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
                   2485:     }
1.123     albertel 2486:     $paramHash->{'emptyallowed'} = $token->[2]{'emptyallowed'};
1.12      bowersj2 2487:     
1.5       bowersj2 2488: }    
                   2489: 
                   2490: sub end_student {
                   2491:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2492: 
                   2493:     if ($target ne 'helper') {
                   2494:         return '';
                   2495:     }
                   2496:     Apache::lonhelper::student->new();
1.3       bowersj2 2497: }
1.5       bowersj2 2498: 
                   2499: sub render {
                   2500:     my $self = shift;
                   2501:     my $result = '';
                   2502:     my $buttons = '';
1.18      bowersj2 2503:     my $var = $self->{'variable'};
1.5       bowersj2 2504: 
                   2505: 
                   2506:     if (defined $self->{ERROR_MSG}) {
                   2507:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                   2508:     }
                   2509: 
1.126     albertel 2510:     my %defaultUsers;
                   2511:     if (defined($self->{DEFAULT_VALUE})) {
                   2512:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   2513:         die 'Error in default value code for variable ' . 
                   2514:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
                   2515:         my @defaultUsers = &$valueFunc($helper, $self);
                   2516: 	if (!$self->{'multichoice'} && @defaultUsers) { # only allowed 1
                   2517: 	    @defaultUsers = ($defaultUsers[0]);
                   2518: 	}
                   2519: 	%defaultUsers = map { if ($_) {($_,1) } } @defaultUsers;
                   2520: 	delete($defaultUsers{''});
                   2521:     }
1.139     foxr     2522: 
                   2523: 
1.147     foxr     2524:     my ($course_personnel, 
                   2525: 	$current_members, 
                   2526: 	$expired_members, 
1.153     foxr     2527: 	$future_members) = 
                   2528: 	    &Apache::lonselstudent::get_people_in_class($env{'request.course.sec'});
1.139     foxr     2529: 
                   2530: 
1.39      bowersj2 2531: 
                   2532:     # Load up the non-students, if necessary
1.147     foxr     2533: 
1.39      bowersj2 2534:     if ($self->{'coursepersonnel'}) {
1.147     foxr     2535: 	unshift @$current_members, (@$course_personnel);
1.39      bowersj2 2536:     }
1.5       bowersj2 2537: 
                   2538: 
1.139     foxr     2539:     #   Current personel
                   2540: 
1.158     albertel 2541:     $result .= '<h4>'.&mt('Select Currently Enrolled Students and Active Course Personnel').'</h4>';
1.148     foxr     2542:     $result .= &Apache::lonselstudent::render_student_list( $current_members,
1.149     foxr     2543: 							    "helpform",
                   2544: 							    "current", 
                   2545: 							    \%defaultUsers,
                   2546: 							    $self->{'multichoice'},
                   2547: 							    $self->{'variable'},
                   2548: 							    1);
1.139     foxr     2549: 
1.132     foxr     2550: 
1.139     foxr     2551:     # If activeonly is not set then we can also give the expired students:
                   2552:     #
1.158     albertel 2553:     if (!$self->{'activeonly'} && ((scalar(@$future_members)) > 0)) {
1.132     foxr     2554: 
1.140     albertel 2555: 	# And future.
                   2556: 
1.158     albertel 2557: 	$result .= '<h4>'.&mt('Select Future Enrolled Students and Future Course Personnel').'</h4>';
1.156     foxr     2558:        
1.148     foxr     2559: 	$result .= &Apache::lonselstudent::render_student_list( $future_members,
1.149     foxr     2560: 								"helpform",
                   2561: 								"future",
                   2562: 								\%defaultUsers,
                   2563: 								$self->{'multichoice'},
                   2564: 								$self->{'variable'},
                   2565: 								0);
1.158     albertel 2566:     }
                   2567:     if (!$self->{'activeonly'} && ((scalar(@$expired_members)) > 0)) {
1.139     foxr     2568: 	# Past 
1.39      bowersj2 2569: 
1.158     albertel 2570: 	$result .= '<h4>'.&mt('Select Previously Enrolled Students and Inactive Course Personnel').'</h4>';
1.148     foxr     2571: 	$result .= &Apache::lonselstudent::render_student_list($expired_members,
1.149     foxr     2572: 							       "helpform",
                   2573: 							       "past",
                   2574: 							       \%defaultUsers,
                   2575: 							       $self->{'multichoice'},
                   2576: 							       $self->{'variable'},
                   2577: 							       0);
1.132     foxr     2578:     }
1.5       bowersj2 2579: 
1.113     foxr     2580: 
                   2581: 
1.5       bowersj2 2582:     return $result;
                   2583: }
                   2584: 
1.6       bowersj2 2585: sub postprocess {
                   2586:     my $self = shift;
                   2587: 
1.157     raeburn  2588:     my $result = $env{'form.' . $self->{'variable'} . '_forminput'};
1.123     albertel 2589:     if (!$result && !$self->{'emptyallowed'}) {
                   2590: 	if ($self->{'coursepersonnel'}) {
                   2591: 	    $self->{ERROR_MSG} = 
                   2592: 		&mt('You must choose at least one user to continue.');
                   2593: 	} else {
                   2594: 	    $self->{ERROR_MSG} = 
                   2595: 		&mt('You must choose at least one student to continue.');
                   2596: 	}
1.6       bowersj2 2597:         return 0;
                   2598:     }
                   2599: 
                   2600:     if (defined($self->{NEXTSTATE})) {
                   2601:         $helper->changeState($self->{NEXTSTATE});
                   2602:     }
                   2603: 
                   2604:     return 1;
                   2605: }
                   2606: 
1.5       bowersj2 2607: 1;
                   2608: 
                   2609: package Apache::lonhelper::files;
                   2610: 
                   2611: =pod
                   2612: 
1.44      bowersj2 2613: =head2 Element: filesX<files, helper element>
1.5       bowersj2 2614: 
                   2615: files allows the users to choose files from a given directory on the
                   2616: server. It is always multichoice and stores the result as a triple-pipe
                   2617: delimited entry in the helper variables. 
                   2618: 
                   2619: Since it is extremely unlikely that you can actually code a constant
                   2620: representing the directory you wish to allow the user to search, <files>
                   2621: takes a subroutine that returns the name of the directory you wish to
                   2622: have the user browse.
                   2623: 
                   2624: files accepts the attribute "variable" to control where the files chosen
                   2625: are put. It accepts the attribute "multichoice" as the other attribute,
                   2626: defaulting to false, which if true will allow the user to select more
                   2627: then one choice. 
                   2628: 
1.44      bowersj2 2629: <files> accepts three subtags: 
                   2630: 
                   2631: =over 4
                   2632: 
                   2633: =item * B<nextstate>: works as it does with the other tags. 
                   2634: 
                   2635: =item * B<filechoice>: When the contents of this tag are surrounded by
                   2636:     "sub {" and "}", will return a string representing what directory
                   2637:     on the server to allow the user to choose files from. 
                   2638: 
                   2639: =item * B<filefilter>: Should contain Perl code that when surrounded
                   2640:     by "sub { my $filename = shift; " and "}", returns a true value if
                   2641:     the user can pick that file, or false otherwise. The filename
                   2642:     passed to the function will be just the name of the file, with no
                   2643:     path info. By default, a filter function will be used that will
                   2644:     mask out old versions of files. This function is available as
                   2645:     Apache::lonhelper::files::not_old_version if you want to use it to
                   2646:     composite your own filters.
                   2647: 
                   2648: =back
                   2649: 
                   2650: B<General security note>: You should ensure the user can not somehow 
                   2651: pass something into your code that would allow them to look places 
                   2652: they should not be able to see, like the C</etc/> directory. However,
                   2653: the security impact would be minimal, since it would only expose
                   2654: the existence of files, there should be no way to parlay that into
                   2655: viewing the files. 
1.5       bowersj2 2656: 
                   2657: =cut
                   2658: 
                   2659: no strict;
                   2660: @ISA = ("Apache::lonhelper::element");
                   2661: use strict;
1.59      bowersj2 2662: use Apache::lonlocal;
1.100     albertel 2663: use Apache::lonnet;
1.32      bowersj2 2664: use Apache::lonpubdir; # for getTitleString
                   2665: 
1.5       bowersj2 2666: BEGIN {
1.7       bowersj2 2667:     &Apache::lonhelper::register('Apache::lonhelper::files',
                   2668:                                  ('files', 'filechoice', 'filefilter'));
1.5       bowersj2 2669: }
                   2670: 
1.44      bowersj2 2671: sub not_old_version {
                   2672:     my $file = shift;
                   2673:     
                   2674:     # Given a file name, return false if it is an "old version" of a
                   2675:     # file, or true if it is not.
                   2676: 
                   2677:     if ($file =~ /^.*\.[0-9]+\.[A-Za-z]+(\.meta)?$/) {
                   2678: 	return 0;
                   2679:     }
                   2680:     return 1;
                   2681: }
                   2682: 
1.5       bowersj2 2683: sub new {
                   2684:     my $ref = Apache::lonhelper::element->new();
                   2685:     bless($ref);
                   2686: }
                   2687: 
                   2688: sub start_files {
                   2689:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2690: 
                   2691:     if ($target ne 'helper') {
                   2692:         return '';
                   2693:     }
                   2694:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   2695:     $helper->declareVar($paramHash->{'variable'});
                   2696:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
                   2697: }    
                   2698: 
                   2699: sub end_files {
                   2700:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2701: 
                   2702:     if ($target ne 'helper') {
                   2703:         return '';
                   2704:     }
                   2705:     if (!defined($paramHash->{FILTER_FUNC})) {
                   2706:         $paramHash->{FILTER_FUNC} = sub { return 1; };
                   2707:     }
                   2708:     Apache::lonhelper::files->new();
                   2709: }    
                   2710: 
                   2711: sub start_filechoice {
                   2712:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2713: 
                   2714:     if ($target ne 'helper') {
                   2715:         return '';
                   2716:     }
                   2717:     $paramHash->{'filechoice'} = Apache::lonxml::get_all_text('/filechoice',
                   2718:                                                               $parser);
                   2719: }
                   2720: 
                   2721: sub end_filechoice { return ''; }
                   2722: 
                   2723: sub start_filefilter {
                   2724:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2725: 
                   2726:     if ($target ne 'helper') {
                   2727:         return '';
                   2728:     }
                   2729: 
                   2730:     my $contents = Apache::lonxml::get_all_text('/filefilter',
                   2731:                                                 $parser);
                   2732:     $contents = 'sub { my $filename = shift; ' . $contents . '}';
                   2733:     $paramHash->{FILTER_FUNC} = eval $contents;
                   2734: }
                   2735: 
                   2736: sub end_filefilter { return ''; }
1.3       bowersj2 2737: 
1.87      matthew  2738: { 
                   2739:     # used to generate unique id attributes for <input> tags. 
                   2740:     # internal use only.
                   2741:     my $id=0;
                   2742:     sub new_id { return $id++;}
                   2743: }
                   2744: 
1.3       bowersj2 2745: sub render {
                   2746:     my $self = shift;
1.5       bowersj2 2747:     my $result = '';
                   2748:     my $var = $self->{'variable'};
                   2749:     
                   2750:     my $subdirFunc = eval('sub {' . $self->{'filechoice'} . '}');
1.11      bowersj2 2751:     die 'Error in resource filter code for variable ' . 
                   2752:         {'variable'} . ', Perl said:' . $@ if $@;
                   2753: 
1.5       bowersj2 2754:     my $subdir = &$subdirFunc();
                   2755: 
                   2756:     my $filterFunc = $self->{FILTER_FUNC};
1.44      bowersj2 2757:     if (!defined($filterFunc)) {
                   2758: 	$filterFunc = &not_old_version;
                   2759:     }
1.5       bowersj2 2760:     my $buttons = '';
1.22      bowersj2 2761:     my $type = 'radio';
                   2762:     if ($self->{'multichoice'}) {
                   2763:         $type = 'checkbox';
                   2764:     }
1.5       bowersj2 2765: 
                   2766:     if ($self->{'multichoice'}) {
                   2767:         $result = <<SCRIPT;
1.112     albertel 2768: <script type="text/javascript">
                   2769: // <!--
1.18      bowersj2 2770:     function checkall(value, checkName) {
1.15      bowersj2 2771: 	for (i=0; i<document.forms.helpform.elements.length; i++) {
                   2772:             ele = document.forms.helpform.elements[i];
1.157     raeburn  2773:             if (ele.name == checkName + '_forminput') {
1.15      bowersj2 2774:                 document.forms.helpform.elements[i].checked=value;
1.5       bowersj2 2775:             }
                   2776:         }
                   2777:     }
1.21      bowersj2 2778: 
1.22      bowersj2 2779:     function checkallclass(value, className) {
1.21      bowersj2 2780:         for (i=0; i<document.forms.helpform.elements.length; i++) {
                   2781:             ele = document.forms.helpform.elements[i];
1.22      bowersj2 2782:             if (ele.type == "$type" && ele.onclick) {
1.21      bowersj2 2783:                 document.forms.helpform.elements[i].checked=value;
                   2784:             }
                   2785:         }
                   2786:     }
1.112     albertel 2787: // -->
1.5       bowersj2 2788: </script>
                   2789: SCRIPT
1.68      sakharuk 2790:        my %lt=&Apache::lonlocal::texthash(
                   2791: 			'saf'  => "Select All Files",
                   2792: 		        'uaf'  => "Unselect All Files");
                   2793:        $buttons = <<BUTTONS;
1.5       bowersj2 2794: <br /> &nbsp;
1.68      sakharuk 2795: <input type="button" onclick="checkall(true, '$var')" value="$lt{'saf'}" />
                   2796: <input type="button" onclick="checkall(false, '$var')" value="$lt{'uaf'}" />
1.23      bowersj2 2797: BUTTONS
                   2798: 
1.69      sakharuk 2799:        %lt=&Apache::lonlocal::texthash(
1.68      sakharuk 2800: 			'sap'  => "Select All Published",
                   2801: 		        'uap'  => "Unselect All Published");
1.23      bowersj2 2802:         if ($helper->{VARS}->{'construction'}) {
1.68      sakharuk 2803:        $buttons .= <<BUTTONS;
                   2804: <input type="button" onclick="checkallclass(true, 'Published')" value="$lt{'sap'}" />
                   2805: <input type="button" onclick="checkallclass(false, 'Published')" value="$lt{'uap'}" />
1.5       bowersj2 2806: <br /> &nbsp;
                   2807: BUTTONS
1.23      bowersj2 2808:        }
1.5       bowersj2 2809:     }
                   2810: 
                   2811:     # Get the list of files in this directory.
                   2812:     my @fileList;
                   2813: 
                   2814:     # If the subdirectory is in local CSTR space
1.47      albertel 2815:     my $metadir;
                   2816:     if ($subdir =~ m|/home/([^/]+)/public_html/(.*)|) {
1.110     albertel 2817: 	my ($user,$domain)= 
                   2818: 	    &Apache::loncacc::constructaccess($subdir,
                   2819: 				     $Apache::lonnet::perlvar{'lonDefDomain'});
1.47      albertel 2820: 	$metadir='/res/'.$domain.'/'.$user.'/'.$2;
                   2821:         @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
                   2822:     } elsif ($subdir =~ m|^~([^/]+)/(.*)$|) {
                   2823: 	$subdir='/home/'.$1.'/public_html/'.$2;
1.110     albertel 2824: 	my ($user,$domain)= 
                   2825: 	    &Apache::loncacc::constructaccess($subdir,
                   2826: 				     $Apache::lonnet::perlvar{'lonDefDomain'});
1.47      albertel 2827: 	$metadir='/res/'.$domain.'/'.$user.'/'.$2;
1.5       bowersj2 2828:         @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
                   2829:     } else {
                   2830:         # local library server resource space
1.100     albertel 2831:         @fileList = &Apache::lonnet::dirlist($subdir, $env{'user.domain'}, $env{'user.name'}, '');
1.5       bowersj2 2832:     }
1.3       bowersj2 2833: 
1.44      bowersj2 2834:     # Sort the fileList into order
1.85      albertel 2835:     @fileList = sort {lc($a) cmp lc($b)} @fileList;
1.44      bowersj2 2836: 
1.5       bowersj2 2837:     $result .= $buttons;
                   2838: 
1.6       bowersj2 2839:     if (defined $self->{ERROR_MSG}) {
                   2840:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                   2841:     }
                   2842: 
1.20      bowersj2 2843:     $result .= '<table border="0" cellpadding="2" cellspacing="0">';
1.5       bowersj2 2844: 
                   2845:     # Keeps track if there are no choices, prints appropriate error
                   2846:     # if there are none. 
                   2847:     my $choices = 0;
                   2848:     # Print each legitimate file choice.
                   2849:     for my $file (@fileList) {
                   2850:         $file = (split(/&/, $file))[0];
                   2851:         if ($file eq '.' || $file eq '..') {
                   2852:             next;
                   2853:         }
                   2854:         my $fileName = $subdir .'/'. $file;
                   2855:         if (&$filterFunc($file)) {
1.24      sakharuk 2856: 	    my $status;
                   2857: 	    my $color;
                   2858: 	    if ($helper->{VARS}->{'construction'}) {
                   2859: 		($status, $color) = @{fileState($subdir, $file)};
                   2860: 	    } else {
                   2861: 		$status = '';
                   2862: 		$color = '';
                   2863: 	    }
1.22      bowersj2 2864: 
1.32      bowersj2 2865:             # Get the title
1.47      albertel 2866:             my $title = Apache::lonpubdir::getTitleString(($metadir?$metadir:$subdir) .'/'. $file);
1.32      bowersj2 2867: 
1.22      bowersj2 2868:             # Netscape 4 is stupid and there's nowhere to put the
                   2869:             # information on the input tag that the file is Published,
                   2870:             # Unpublished, etc. In *real* browsers we can just say
                   2871:             # "class='Published'" and check the className attribute of
                   2872:             # the input tag, but Netscape 4 is too stupid to understand
                   2873:             # that attribute, and un-comprehended attributes are not
                   2874:             # reflected into the object model. So instead, what I do 
                   2875:             # is either have or don't have an "onclick" handler that 
                   2876:             # does nothing, give Published files the onclick handler, and
                   2877:             # have the checker scripts check for that. Stupid and clumsy,
                   2878:             # and only gives us binary "yes/no" information (at least I
                   2879:             # couldn't figure out how to reach into the event handler's
                   2880:             # actual code to retreive a value), but it works well enough
                   2881:             # here.
1.23      bowersj2 2882:         
1.22      bowersj2 2883:             my $onclick = '';
1.23      bowersj2 2884:             if ($status eq 'Published' && $helper->{VARS}->{'construction'}) {
1.22      bowersj2 2885:                 $onclick = 'onclick="a=1" ';
                   2886:             }
1.87      matthew  2887:             my $id = &new_id();
1.20      bowersj2 2888:             $result .= '<tr><td align="right"' . " bgcolor='$color'>" .
1.22      bowersj2 2889:                 "<input $onclick type='$type' name='" . $var
1.157     raeburn  2890:             . "_forminput' ".qq{id="$id"}." value='" . HTML::Entities::encode($fileName,"<>&\"'").
1.5       bowersj2 2891:                 "'";
                   2892:             if (!$self->{'multichoice'} && $choices == 0) {
1.111     albertel 2893:                 $result .= ' checked="checked"';
1.5       bowersj2 2894:             }
1.87      matthew  2895:             $result .= "/></td><td bgcolor='$color'>".
                   2896:                 qq{<label for="$id">}. $file . "</label></td>" .
1.32      bowersj2 2897:                 "<td bgcolor='$color'>$title</td>" .
                   2898:                 "<td bgcolor='$color'>$status</td>" . "</tr>\n";
1.5       bowersj2 2899:             $choices++;
                   2900:         }
                   2901:     }
                   2902: 
                   2903:     $result .= "</table>\n";
                   2904: 
                   2905:     if (!$choices) {
1.47      albertel 2906:         $result .= '<font color="#FF0000">There are no files available to select in this directory ('.$subdir.'). Please go back and select another option.</font><br /><br />';
1.5       bowersj2 2907:     }
                   2908: 
                   2909:     $result .= $buttons;
                   2910: 
                   2911:     return $result;
1.20      bowersj2 2912: }
                   2913: 
                   2914: # Determine the state of the file: Published, unpublished, modified.
                   2915: # Return the color it should be in and a label as a two-element array
                   2916: # reference.
                   2917: # Logic lifted from lonpubdir.pm, even though I don't know that it's still
                   2918: # the most right thing to do.
                   2919: 
                   2920: sub fileState {
                   2921:     my $constructionSpaceDir = shift;
                   2922:     my $file = shift;
                   2923:     
1.100     albertel 2924:     my ($uname,$udom)=($env{'user.name'},$env{'user.domain'});
                   2925:     if ($env{'request.role'}=~/^ca\./) {
                   2926: 	(undef,$udom,$uname)=split(/\//,$env{'request.role'});
1.86      albertel 2927:     }
1.20      bowersj2 2928:     my $docroot = $Apache::lonnet::perlvar{'lonDocRoot'};
                   2929:     my $subdirpart = $constructionSpaceDir;
1.86      albertel 2930:     $subdirpart =~ s/^\/home\/$uname\/public_html//;
                   2931:     my $resdir = $docroot . '/res/' . $udom . '/' . $uname .
1.20      bowersj2 2932:         $subdirpart;
                   2933: 
                   2934:     my @constructionSpaceFileStat = stat($constructionSpaceDir . '/' . $file);
                   2935:     my @resourceSpaceFileStat = stat($resdir . '/' . $file);
                   2936:     if (!@resourceSpaceFileStat) {
                   2937:         return ['Unpublished', '#FFCCCC'];
                   2938:     }
                   2939: 
                   2940:     my $constructionSpaceFileModified = $constructionSpaceFileStat[9];
                   2941:     my $resourceSpaceFileModified = $resourceSpaceFileStat[9];
                   2942:     
                   2943:     if ($constructionSpaceFileModified > $resourceSpaceFileModified) {
                   2944:         return ['Modified', '#FFFFCC'];
                   2945:     }
                   2946:     return ['Published', '#CCFFCC'];
1.4       bowersj2 2947: }
1.5       bowersj2 2948: 
1.4       bowersj2 2949: sub postprocess {
                   2950:     my $self = shift;
1.157     raeburn  2951:     my $result = $env{'form.' . $self->{'variable'} . '_forminput'};
1.6       bowersj2 2952:     if (!$result) {
                   2953:         $self->{ERROR_MSG} = 'You must choose at least one file '.
                   2954:             'to continue.';
                   2955:         return 0;
                   2956:     }
                   2957: 
1.5       bowersj2 2958:     if (defined($self->{NEXTSTATE})) {
                   2959:         $helper->changeState($self->{NEXTSTATE});
1.3       bowersj2 2960:     }
1.6       bowersj2 2961: 
                   2962:     return 1;
1.3       bowersj2 2963: }
1.8       bowersj2 2964: 
                   2965: 1;
                   2966: 
1.11      bowersj2 2967: package Apache::lonhelper::section;
                   2968: 
                   2969: =pod
                   2970: 
1.44      bowersj2 2971: =head2 Element: sectionX<section, helper element>
1.11      bowersj2 2972: 
                   2973: <section> allows the user to choose one or more sections from the current
                   2974: course.
                   2975: 
1.134     albertel 2976: It takes the standard attributes "variable", "multichoice",
                   2977: "allowempty" and "nextstate", meaning what they do for most other
                   2978: elements.
                   2979: 
                   2980: also takes a boolean 'onlysections' whcih will restrict this to only
                   2981: have sections and not include groups
1.11      bowersj2 2982: 
                   2983: =cut
                   2984: 
                   2985: no strict;
                   2986: @ISA = ("Apache::lonhelper::choices");
                   2987: use strict;
                   2988: 
                   2989: BEGIN {
                   2990:     &Apache::lonhelper::register('Apache::lonhelper::section',
                   2991:                                  ('section'));
                   2992: }
                   2993: 
                   2994: sub new {
                   2995:     my $ref = Apache::lonhelper::choices->new();
                   2996:     bless($ref);
                   2997: }
                   2998: 
                   2999: sub start_section {
                   3000:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3001: 
                   3002:     if ($target ne 'helper') {
                   3003:         return '';
                   3004:     }
1.12      bowersj2 3005: 
                   3006:     $paramHash->{CHOICES} = [];
                   3007: 
1.11      bowersj2 3008:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   3009:     $helper->declareVar($paramHash->{'variable'});
                   3010:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.133     albertel 3011:     $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.11      bowersj2 3012:     if (defined($token->[2]{'nextstate'})) {
1.12      bowersj2 3013:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
1.11      bowersj2 3014:     }
                   3015: 
                   3016:     # Populate the CHOICES element
                   3017:     my %choices;
                   3018: 
                   3019:     my $section = Apache::loncoursedata::CL_SECTION();
                   3020:     my $classlist = Apache::loncoursedata::get_classlist();
1.143     albertel 3021:     foreach my $user (keys(%$classlist)) {
                   3022:         my $section_name = $classlist->{$user}[$section];
                   3023:         if (!$section_name) {
1.11      bowersj2 3024:             $choices{"No section assigned"} = "";
                   3025:         } else {
1.143     albertel 3026:             $choices{$section_name} = $section_name;
1.11      bowersj2 3027:         }
1.12      bowersj2 3028:     } 
                   3029:    
1.143     albertel 3030:     if (exists($choices{"No section assigned"})) {
                   3031: 	push(@{$paramHash->{CHOICES}}, 
                   3032: 	     ['No section assigned','No section assigned']);
                   3033: 	delete($choices{"No section assigned"});
                   3034:     }
                   3035:     for my $section_name (sort {lc($a) cmp lc($b) } (keys(%choices))) {
                   3036: 	push @{$paramHash->{CHOICES}}, [$section_name, $section_name];
1.134     albertel 3037:     }
                   3038:     return if ($token->[2]{'onlysections'});
                   3039: 
                   3040:     # add in groups to the end of the list
1.151     raeburn  3041:     my %curr_groups = &Apache::longroup::coursegroups();
1.142     albertel 3042:     foreach my $group_name (sort(keys(%curr_groups))) {
                   3043: 	push(@{$paramHash->{CHOICES}}, [$group_name, $group_name]);
1.11      bowersj2 3044:     }
                   3045: }    
                   3046: 
1.12      bowersj2 3047: sub end_section {
                   3048:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.11      bowersj2 3049: 
1.12      bowersj2 3050:     if ($target ne 'helper') {
                   3051:         return '';
                   3052:     }
                   3053:     Apache::lonhelper::section->new();
                   3054: }    
1.11      bowersj2 3055: 1;
                   3056: 
1.128     raeburn  3057: package Apache::lonhelper::group;
                   3058: 
                   3059: =pod
                   3060:  
                   3061: =head2 Element: groupX<group, helper element>
                   3062:  
1.134     albertel 3063: <group> allows the user to choose one or more groups from the current course.
                   3064: 
                   3065: It takes the standard attributes "variable", "multichoice",
                   3066:  "allowempty" and "nextstate", meaning what they do for most other
                   3067:  elements.
1.128     raeburn  3068: 
                   3069: =cut
                   3070: 
                   3071: no strict;
                   3072: @ISA = ("Apache::lonhelper::choices");
                   3073: use strict;
                   3074: 
                   3075: BEGIN {
                   3076:     &Apache::lonhelper::register('Apache::lonhelper::group',
                   3077:                                  ('group'));
                   3078: }
                   3079: 
                   3080: sub new {
                   3081:     my $ref = Apache::lonhelper::choices->new();
                   3082:     bless($ref);
                   3083: }
                   3084:  
                   3085: sub start_group {
                   3086:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3087:  
                   3088:     if ($target ne 'helper') {
                   3089:         return '';
                   3090:     }
                   3091: 
                   3092:     $paramHash->{CHOICES} = [];
                   3093: 
                   3094:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   3095:     $helper->declareVar($paramHash->{'variable'});
                   3096:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.133     albertel 3097:     $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.128     raeburn  3098:     if (defined($token->[2]{'nextstate'})) {
                   3099:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
                   3100:     }
                   3101: 
                   3102:     # Populate the CHOICES element
                   3103:     my %choices;
                   3104: 
1.151     raeburn  3105:     my %curr_groups = &Apache::longroup::coursegroups();
1.143     albertel 3106:     foreach my $group_name (sort {lc($a) cmp lc($b)} (keys(%curr_groups))) {
1.142     albertel 3107: 	push(@{$paramHash->{CHOICES}}, [$group_name, $group_name]);
1.128     raeburn  3108:     }
                   3109: }
1.134     albertel 3110: 
1.128     raeburn  3111: sub end_group {
                   3112:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3113: 
                   3114:     if ($target ne 'helper') {
                   3115:         return '';
                   3116:     }
                   3117:     Apache::lonhelper::group->new();
                   3118: }
                   3119: 1;
                   3120: 
1.34      bowersj2 3121: package Apache::lonhelper::string;
                   3122: 
                   3123: =pod
                   3124: 
1.44      bowersj2 3125: =head2 Element: stringX<string, helper element>
1.34      bowersj2 3126: 
                   3127: string elements provide a string entry field for the user. string elements
                   3128: take the usual 'variable' and 'nextstate' parameters. string elements
                   3129: also pass through 'maxlength' and 'size' attributes to the input tag.
                   3130: 
                   3131: string honors the defaultvalue tag, if given.
                   3132: 
1.38      bowersj2 3133: string honors the validation function, if given.
                   3134: 
1.34      bowersj2 3135: =cut
                   3136: 
                   3137: no strict;
                   3138: @ISA = ("Apache::lonhelper::element");
                   3139: use strict;
1.76      sakharuk 3140: use Apache::lonlocal;
1.34      bowersj2 3141: 
                   3142: BEGIN {
                   3143:     &Apache::lonhelper::register('Apache::lonhelper::string',
                   3144:                               ('string'));
                   3145: }
                   3146: 
                   3147: sub new {
                   3148:     my $ref = Apache::lonhelper::element->new();
                   3149:     bless($ref);
                   3150: }
                   3151: 
                   3152: # CONSTRUCTION: Construct the message element from the XML
                   3153: sub start_string {
                   3154:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3155: 
                   3156:     if ($target ne 'helper') {
                   3157:         return '';
                   3158:     }
                   3159: 
                   3160:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   3161:     $helper->declareVar($paramHash->{'variable'});
                   3162:     $paramHash->{'nextstate'} = $token->[2]{'nextstate'};
                   3163:     $paramHash->{'maxlength'} = $token->[2]{'maxlength'};
                   3164:     $paramHash->{'size'} = $token->[2]{'size'};
                   3165: 
                   3166:     return '';
                   3167: }
                   3168: 
                   3169: sub end_string {
                   3170:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3171: 
                   3172:     if ($target ne 'helper') {
                   3173:         return '';
                   3174:     }
                   3175:     Apache::lonhelper::string->new();
                   3176:     return '';
                   3177: }
                   3178: 
                   3179: sub render {
                   3180:     my $self = shift;
1.38      bowersj2 3181:     my $result = '';
                   3182: 
                   3183:     if (defined $self->{ERROR_MSG}) {
1.97      albertel 3184:         $result .= '<p><font color="#FF0000">' . $self->{ERROR_MSG} . '</font></p>';
1.38      bowersj2 3185:     }
                   3186: 
1.157     raeburn  3187:     $result .= '<input type="string" name="' . $self->{'variable'} . '_forminput"';
1.34      bowersj2 3188: 
                   3189:     if (defined($self->{'size'})) {
                   3190:         $result .= ' size="' . $self->{'size'} . '"';
                   3191:     }
                   3192:     if (defined($self->{'maxlength'})) {
                   3193:         $result .= ' maxlength="' . $self->{'maxlength'} . '"';
                   3194:     }
                   3195: 
                   3196:     if (defined($self->{DEFAULT_VALUE})) {
                   3197:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   3198:         die 'Error in default value code for variable ' . 
                   3199:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
                   3200:         $result .= ' value="' . &$valueFunc($helper, $self) . '"';
                   3201:     }
                   3202: 
                   3203:     $result .= ' />';
                   3204: 
                   3205:     return $result;
                   3206: }
                   3207: 
                   3208: # If a NEXTSTATE was given, switch to it
                   3209: sub postprocess {
                   3210:     my $self = shift;
1.38      bowersj2 3211: 
                   3212:     if (defined($self->{VALIDATOR})) {
                   3213: 	my $validator = eval($self->{VALIDATOR});
1.138     albertel 3214: 	die 'Died during evaluation of validator code; Perl said: ' . $@ if $@;
1.38      bowersj2 3215: 	my $invalid = &$validator($helper, $state, $self, $self->getValue());
                   3216: 	if ($invalid) {
                   3217: 	    $self->{ERROR_MSG} = $invalid;
                   3218: 	    return 0;
                   3219: 	}
                   3220:     }
                   3221: 
                   3222:     if (defined($self->{'nextstate'})) {
                   3223:         $helper->changeState($self->{'nextstate'});
1.34      bowersj2 3224:     }
                   3225: 
                   3226:     return 1;
                   3227: }
                   3228: 
                   3229: 1;
                   3230: 
1.8       bowersj2 3231: package Apache::lonhelper::general;
                   3232: 
                   3233: =pod
                   3234: 
1.44      bowersj2 3235: =head2 General-purpose tag: <exec>X<exec, helper tag>
1.8       bowersj2 3236: 
1.44      bowersj2 3237: The contents of the exec tag are executed as Perl code, B<not> inside a 
1.100     albertel 3238: safe space, so the full range of $env and such is available. The code
1.8       bowersj2 3239: will be executed as a subroutine wrapped with the following code:
                   3240: 
                   3241: "sub { my $helper = shift; my $state = shift;" and
                   3242: 
                   3243: "}"
                   3244: 
                   3245: The return value is ignored.
                   3246: 
                   3247: $helper is the helper object. Feel free to add methods to the helper
                   3248: object to support whatever manipulation you may need to do (for instance,
                   3249: overriding the form location if the state is the final state; see 
1.44      bowersj2 3250: parameter.helper for an example).
1.8       bowersj2 3251: 
                   3252: $state is the $paramHash that has currently been generated and may
                   3253: be manipulated by the code in exec. Note that the $state is not yet
                   3254: an actual state B<object>, it is just a hash, so do not expect to
                   3255: be able to call methods on it.
                   3256: 
                   3257: =cut
                   3258: 
1.83      sakharuk 3259: use Apache::lonlocal;
1.102     albertel 3260: use Apache::lonnet;
1.83      sakharuk 3261: 
1.8       bowersj2 3262: BEGIN {
                   3263:     &Apache::lonhelper::register('Apache::lonhelper::general',
1.11      bowersj2 3264:                                  'exec', 'condition', 'clause',
                   3265:                                  'eval');
1.8       bowersj2 3266: }
                   3267: 
                   3268: sub start_exec {
                   3269:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3270: 
                   3271:     if ($target ne 'helper') {
                   3272:         return '';
                   3273:     }
                   3274:     
                   3275:     my $code = &Apache::lonxml::get_all_text('/exec', $parser);
                   3276:     
                   3277:     $code = eval ('sub { my $helper = shift; my $state = shift; ' .
                   3278:         $code . "}");
1.11      bowersj2 3279:     die 'Error in <exec>, Perl said: '. $@ if $@;
1.8       bowersj2 3280:     &$code($helper, $paramHash);
                   3281: }
                   3282: 
                   3283: sub end_exec { return ''; }
                   3284: 
                   3285: =pod
                   3286: 
                   3287: =head2 General-purpose tag: <condition>
                   3288: 
                   3289: The <condition> tag allows you to mask out parts of the helper code
                   3290: depending on some programatically determined condition. The condition
                   3291: tag contains a tag <clause> which contains perl code that when wrapped
                   3292: with "sub { my $helper = shift; my $state = shift; " and "}", returns
                   3293: a true value if the XML in the condition should be evaluated as a normal
                   3294: part of the helper, or false if it should be completely discarded.
                   3295: 
                   3296: The <clause> tag must be the first sub-tag of the <condition> tag or
                   3297: it will not work as expected.
                   3298: 
                   3299: =cut
                   3300: 
                   3301: # The condition tag just functions as a marker, it doesn't have
                   3302: # to "do" anything. Technically it doesn't even have to be registered
                   3303: # with the lonxml code, but I leave this here to be explicit about it.
                   3304: sub start_condition { return ''; }
                   3305: sub end_condition { return ''; }
                   3306: 
                   3307: sub start_clause {
                   3308:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3309: 
                   3310:     if ($target ne 'helper') {
                   3311:         return '';
                   3312:     }
                   3313:     
                   3314:     my $clause = Apache::lonxml::get_all_text('/clause', $parser);
                   3315:     $clause = eval('sub { my $helper = shift; my $state = shift; '
                   3316:         . $clause . '}');
1.11      bowersj2 3317:     die 'Error in clause of condition, Perl said: ' . $@ if $@;
1.8       bowersj2 3318:     if (!&$clause($helper, $paramHash)) {
                   3319:         # Discard all text until the /condition.
1.155     albertel 3320: 	my $end_tag = $paramHash->{SKIPTAG} || '/condition';
                   3321:         &Apache::lonxml::get_all_text($end_tag, $parser);
1.8       bowersj2 3322:     }
                   3323: }
                   3324: 
                   3325: sub end_clause { return ''; }
1.11      bowersj2 3326: 
                   3327: =pod
                   3328: 
1.44      bowersj2 3329: =head2 General-purpose tag: <eval>X<eval, helper tag>
1.11      bowersj2 3330: 
                   3331: The <eval> tag will be evaluated as a subroutine call passed in the
                   3332: current helper object and state hash as described in <condition> above,
                   3333: but is expected to return a string to be printed directly to the
                   3334: screen. This is useful for dynamically generating messages. 
                   3335: 
                   3336: =cut
                   3337: 
                   3338: # This is basically a type of message.
                   3339: # Programmatically setting $paramHash->{NEXTSTATE} would work, though
                   3340: # it's probably bad form.
                   3341: 
                   3342: sub start_eval {
                   3343:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3344: 
                   3345:     if ($target ne 'helper') {
                   3346:         return '';
                   3347:     }
                   3348:     
                   3349:     my $program = Apache::lonxml::get_all_text('/eval', $parser);
                   3350:     $program = eval('sub { my $helper = shift; my $state = shift; '
                   3351:         . $program . '}');
                   3352:     die 'Error in eval code, Perl said: ' . $@ if $@;
                   3353:     $paramHash->{MESSAGE_TEXT} = &$program($helper, $paramHash);
                   3354: }
                   3355: 
                   3356: sub end_eval { 
                   3357:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3358: 
                   3359:     if ($target ne 'helper') {
                   3360:         return '';
                   3361:     }
                   3362: 
                   3363:     Apache::lonhelper::message->new();
                   3364: }
                   3365: 
1.13      bowersj2 3366: 1;
                   3367: 
1.27      bowersj2 3368: package Apache::lonhelper::final;
                   3369: 
                   3370: =pod
                   3371: 
1.44      bowersj2 3372: =head2 Element: finalX<final, helper tag>
1.27      bowersj2 3373: 
                   3374: <final> is a special element that works with helpers that use the <finalcode>
1.44      bowersj2 3375: tagX<finalcode, helper tag>. It goes through all the states and elements, executing the <finalcode>
1.27      bowersj2 3376: snippets and collecting the results. Finally, it takes the user out of the
                   3377: helper, going to a provided page.
                   3378: 
1.34      bowersj2 3379: If the parameter "restartCourse" is true, this will override the buttons and
                   3380: will make a "Finish Helper" button that will re-initialize the course for them,
                   3381: which is useful for the Course Initialization helper so the users never see
                   3382: the old values taking effect.
                   3383: 
1.93      albertel 3384: If the parameter "restartCourse" is not true a 'Finish' Button will be
                   3385: presented that takes the user back to whatever was defined as <exitpage>
                   3386: 
1.27      bowersj2 3387: =cut
                   3388: 
                   3389: no strict;
                   3390: @ISA = ("Apache::lonhelper::element");
                   3391: use strict;
1.62      matthew  3392: use Apache::lonlocal;
1.100     albertel 3393: use Apache::lonnet;
1.27      bowersj2 3394: BEGIN {
                   3395:     &Apache::lonhelper::register('Apache::lonhelper::final',
                   3396:                                  ('final', 'exitpage'));
                   3397: }
                   3398: 
                   3399: sub new {
                   3400:     my $ref = Apache::lonhelper::element->new();
                   3401:     bless($ref);
                   3402: }
                   3403: 
1.34      bowersj2 3404: sub start_final { 
                   3405:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3406: 
                   3407:     if ($target ne 'helper') {
                   3408:         return '';
                   3409:     }
                   3410: 
                   3411:     $paramHash->{'restartCourse'} = $token->[2]{'restartCourse'};
                   3412: 
                   3413:     return ''; 
                   3414: }
1.27      bowersj2 3415: 
                   3416: sub end_final {
                   3417:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3418: 
                   3419:     if ($target ne 'helper') {
                   3420:         return '';
                   3421:     }
                   3422: 
                   3423:     Apache::lonhelper::final->new();
                   3424:    
                   3425:     return '';
                   3426: }
                   3427: 
                   3428: sub start_exitpage {
                   3429:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3430: 
                   3431:     if ($target ne 'helper') {
                   3432:         return '';
                   3433:     }
                   3434: 
                   3435:     $paramHash->{EXIT_PAGE} = &Apache::lonxml::get_all_text('/exitpage',
                   3436:                                                             $parser);
                   3437: 
                   3438:     return '';
                   3439: }
                   3440: 
                   3441: sub end_exitpage { return ''; }
                   3442: 
                   3443: sub render {
                   3444:     my $self = shift;
                   3445: 
                   3446:     my @results;
                   3447: 
                   3448:     # Collect all the results
                   3449:     for my $stateName (keys %{$helper->{STATES}}) {
                   3450:         my $state = $helper->{STATES}->{$stateName};
                   3451:         
                   3452:         for my $element (@{$state->{ELEMENTS}}) {
                   3453:             if (defined($element->{FINAL_CODE})) {
                   3454:                 # Compile the code.
1.31      bowersj2 3455:                 my $code = 'sub { my $helper = shift; my $element = shift; ' 
                   3456:                     . $element->{FINAL_CODE} . '}';
1.27      bowersj2 3457:                 $code = eval($code);
                   3458:                 die 'Error while executing final code for element with var ' .
                   3459:                     $element->{'variable'} . ', Perl said: ' . $@ if $@;
                   3460: 
1.31      bowersj2 3461:                 my $result = &$code($helper, $element);
1.27      bowersj2 3462:                 if ($result) {
                   3463:                     push @results, $result;
                   3464:                 }
                   3465:             }
                   3466:         }
                   3467:     }
                   3468: 
1.40      bowersj2 3469:     my $result;
1.27      bowersj2 3470: 
1.40      bowersj2 3471:     if (scalar(@results) != 0) {
                   3472: 	$result .= "<ul>\n";
                   3473: 	for my $re (@results) {
                   3474: 	    $result .= '    <li>' . $re . "</li>\n";
                   3475: 	}
                   3476: 	
                   3477: 	if (!@results) {
1.59      bowersj2 3478: 	    $result .= '    <li>' . 
                   3479: 		&mt('No changes were made to current settings.') . '</li>';
1.40      bowersj2 3480: 	}
                   3481: 	
                   3482: 	$result .= '</ul>';
1.34      bowersj2 3483:     }
                   3484: 
1.93      albertel 3485:     my $actionURL = $self->{EXIT_PAGE};
                   3486:     my $targetURL = '';
                   3487:     my $finish=&mt('Finish');
1.34      bowersj2 3488:     if ($self->{'restartCourse'}) {
1.103     albertel 3489: 	$actionURL = '/adm/roles';
1.93      albertel 3490: 	$targetURL = '/adm/menu';
1.100     albertel 3491: 	if ($env{'course.'.$env{'request.course.id'}.'.url'}=~/^uploaded/) {
1.64      albertel 3492: 	    $targetURL = '/adm/coursedocs';
                   3493: 	} else {
                   3494: 	    $targetURL = '/adm/navmaps';
                   3495: 	}
1.100     albertel 3496: 	if ($env{'course.'.$env{'request.course.id'}.'.clonedfrom'}) {
1.45      bowersj2 3497: 	    $targetURL = '/adm/parmset?overview=1';
                   3498: 	}
1.93      albertel 3499: 	my $finish=&mt('Finish Course Initialization');
1.34      bowersj2 3500:     }
1.93      albertel 3501:     my $previous = HTML::Entities::encode(&mt("<- Previous"), '<>&"');
                   3502:     my $next = HTML::Entities::encode(&mt("Next ->"), '<>&"');
1.127     albertel 3503:     my $target = " target='loncapaclient'";
                   3504:     if (($env{'browser.interface'} eq 'textual') ||
                   3505:         ($env{'environment.remote'} eq 'off')) {  $target='';  }
1.93      albertel 3506:     $result .= "<center>\n" .
1.127     albertel 3507: 	"<form action='".$actionURL."' method='post' $target>\n" .
1.93      albertel 3508: 	"<input type='button' onclick='history.go(-1)' value='$previous' />" .
                   3509: 	"<input type='hidden' name='orgurl' value='$targetURL' />" .
                   3510: 	"<input type='hidden' name='selectrole' value='1' />\n" .
1.100     albertel 3511: 	"<input type='hidden' name='" . $env{'request.role'} . 
1.93      albertel 3512: 	"' value='1' />\n<input type='submit' value='" . $finish . "' />\n" .
                   3513: 	"</form></center>";
1.34      bowersj2 3514: 
1.40      bowersj2 3515:     return $result;
1.34      bowersj2 3516: }
                   3517: 
                   3518: sub overrideForm {
1.93      albertel 3519:     return 1;
1.27      bowersj2 3520: }
                   3521: 
                   3522: 1;
                   3523: 
1.13      bowersj2 3524: package Apache::lonhelper::parmwizfinal;
                   3525: 
1.160   ! albertel 3526: # This is the final state for the parm helper. It is not generally useful,
1.13      bowersj2 3527: # so it is not perldoc'ed. It does its own processing.
                   3528: # It is represented with <parmwizfinal />, and
                   3529: # should later be moved to lonparmset.pm .
                   3530: 
                   3531: no strict;
                   3532: @ISA = ('Apache::lonhelper::element');
                   3533: use strict;
1.69      sakharuk 3534: use Apache::lonlocal;
1.102     albertel 3535: use Apache::lonnet;
1.11      bowersj2 3536: 
1.13      bowersj2 3537: BEGIN {
                   3538:     &Apache::lonhelper::register('Apache::lonhelper::parmwizfinal',
                   3539:                                  ('parmwizfinal'));
                   3540: }
                   3541: 
                   3542: use Time::localtime;
                   3543: 
                   3544: sub new {
                   3545:     my $ref = Apache::lonhelper::choices->new();
                   3546:     bless ($ref);
                   3547: }
                   3548: 
                   3549: sub start_parmwizfinal { return ''; }
                   3550: 
                   3551: sub end_parmwizfinal {
                   3552:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3553: 
                   3554:     if ($target ne 'helper') {
                   3555:         return '';
                   3556:     }
                   3557:     Apache::lonhelper::parmwizfinal->new();
                   3558: }
                   3559: 
                   3560: # Renders a form that, when submitted, will form the input to lonparmset.pm
                   3561: sub render {
                   3562:     my $self = shift;
                   3563:     my $vars = $helper->{VARS};
                   3564: 
                   3565:     # FIXME: Unify my designators with the standard ones
1.48      bowersj2 3566:     my %dateTypeHash = ('open_date' => "opening date",
                   3567:                         'due_date' => "due date",
                   3568:                         'answer_date' => "answer date",
                   3569: 			'tries' => 'number of tries',
                   3570: 			'weight' => 'problem weight'
1.38      bowersj2 3571: 			);
1.13      bowersj2 3572:     my %parmTypeHash = ('open_date' => "0_opendate",
                   3573:                         'due_date' => "0_duedate",
1.38      bowersj2 3574:                         'answer_date' => "0_answerdate",
1.48      bowersj2 3575: 			'tries' => '0_maxtries',
                   3576: 			'weight' => '0_weight' );
1.107     albertel 3577:     my %realParmName = ('open_date' => "opendate",
                   3578:                         'due_date' => "duedate",
                   3579:                         'answer_date' => "answerdate",
                   3580: 			'tries' => 'maxtries',
                   3581: 			'weight' => 'weight' );
1.13      bowersj2 3582:     
                   3583:     my $affectedResourceId = "";
                   3584:     my $parm_name = $parmTypeHash{$vars->{ACTION_TYPE}};
                   3585:     my $level = "";
1.27      bowersj2 3586:     my $resourceString;
                   3587:     my $symb;
                   3588:     my $paramlevel;
1.95      albertel 3589:     
1.13      bowersj2 3590:     # Print the granularity, depending on the action
                   3591:     if ($vars->{GRANULARITY} eq 'whole_course') {
1.76      sakharuk 3592:         $resourceString .= '<li>'.&mt('for <b>all resources in the course</b>').'</li>';
1.104     albertel 3593: 	if ($vars->{TARGETS} eq 'course') {
1.150     albertel 3594: 	    $level = 14; # general course, see lonparmset.pm perldoc
1.104     albertel 3595: 	} elsif ($vars->{TARGETS} eq 'section') {
1.150     albertel 3596: 	    $level = 9;
                   3597: 	} elsif ($vars->{TARGETS} eq 'group') {
1.104     albertel 3598: 	    $level = 6;
                   3599: 	} else {
                   3600: 	    $level = 3;
                   3601: 	}
1.13      bowersj2 3602:         $affectedResourceId = "0.0";
1.27      bowersj2 3603:         $symb = 'a';
                   3604:         $paramlevel = 'general';
1.13      bowersj2 3605:     } elsif ($vars->{GRANULARITY} eq 'map') {
1.41      bowersj2 3606:         my $navmap = Apache::lonnavmaps::navmap->new();
1.35      bowersj2 3607:         my $res = $navmap->getByMapPc($vars->{RESOURCE_ID});
1.13      bowersj2 3608:         my $title = $res->compTitle();
1.27      bowersj2 3609:         $symb = $res->symb();
1.78      sakharuk 3610:         $resourceString .= '<li>'.&mt('for the map named [_1]',"<b>$title</b>").'</li>';
1.104     albertel 3611: 	if ($vars->{TARGETS} eq 'course') {
1.150     albertel 3612: 	    $level = 13; # general course, see lonparmset.pm perldoc
1.104     albertel 3613: 	} elsif ($vars->{TARGETS} eq 'section') {
1.150     albertel 3614: 	    $level = 8;
                   3615: 	} elsif ($vars->{TARGETS} eq 'group') {
1.104     albertel 3616: 	    $level = 5;
                   3617: 	} else {
                   3618: 	    $level = 2;
                   3619: 	}
1.13      bowersj2 3620:         $affectedResourceId = $vars->{RESOURCE_ID};
1.27      bowersj2 3621:         $paramlevel = 'map';
1.13      bowersj2 3622:     } else {
1.41      bowersj2 3623:         my $navmap = Apache::lonnavmaps::navmap->new();
1.13      bowersj2 3624:         my $res = $navmap->getById($vars->{RESOURCE_ID});
1.95      albertel 3625:         my $part = $vars->{RESOURCE_ID_part};
                   3626: 	if ($part ne 'All Parts' && $part) { $parm_name=~s/^0/$part/; } else { $part=&mt('All Parts'); }
1.27      bowersj2 3627:         $symb = $res->symb();
1.13      bowersj2 3628:         my $title = $res->compTitle();
1.95      albertel 3629:         $resourceString .= '<li>'.&mt('for the resource named [_1] part [_2]',"<b>$title</b>","<b>$part</b>").'</li>';
1.104     albertel 3630: 	if ($vars->{TARGETS} eq 'course') {
1.150     albertel 3631: 	    $level = 10; # general course, see lonparmset.pm perldoc
1.104     albertel 3632: 	} elsif ($vars->{TARGETS} eq 'section') {
1.150     albertel 3633: 	    $level = 7;
                   3634: 	} elsif ($vars->{TARGETS} eq 'group') {
1.104     albertel 3635: 	    $level = 4;
                   3636: 	} else {
                   3637: 	    $level = 1;
                   3638: 	}
1.13      bowersj2 3639:         $affectedResourceId = $vars->{RESOURCE_ID};
1.27      bowersj2 3640:         $paramlevel = 'full';
1.13      bowersj2 3641:     }
                   3642: 
1.105     albertel 3643:     my $result = "<form name='helpform' method='POST' action='/adm/parmset#$affectedResourceId&$parm_name&$level'>\n";
1.104     albertel 3644:     $result .= "<input type='hidden' name='action' value='settable' />\n";
                   3645:     $result .= "<input type='hidden' name='dis' value='helper' />\n";
1.107     albertel 3646:     $result .= "<input type='hidden' name='pscat' value='".
                   3647: 	$realParmName{$vars->{ACTION_TYPE}}."' />\n";
1.95      albertel 3648:     if ($vars->{GRANULARITY} eq 'resource') {
                   3649: 	$result .= "<input type='hidden' name='symb' value='".
                   3650: 	    HTML::Entities::encode($symb,"'<>&\"") . "' />\n";
1.108     albertel 3651:     } elsif ($vars->{GRANULARITY} eq 'map') {
                   3652: 	$result .= "<input type='hidden' name='pschp' value='".
                   3653: 	    $affectedResourceId."' />\n";
1.95      albertel 3654:     }
1.104     albertel 3655:     my $part = $vars->{RESOURCE_ID_part};
                   3656:     if ($part eq 'All Parts' || !$part) { $part=0; }
                   3657:     $result .= "<input type='hidden' name='psprt' value='".
                   3658: 	HTML::Entities::encode($part,"'<>&\"") . "' />\n";
                   3659: 
1.69      sakharuk 3660:     $result .= '<p>'.&mt('Confirm that this information is correct, then click &quot;Finish Helper&quot; to complete setting the parameter.').'<ul>';
1.27      bowersj2 3661:     
                   3662:     # Print the type of manipulation:
1.73      albertel 3663:     my $extra;
1.38      bowersj2 3664:     if ($vars->{ACTION_TYPE} eq 'tries') {
1.73      albertel 3665: 	$extra =  $vars->{TRIES};
1.38      bowersj2 3666:     }
1.48      bowersj2 3667:     if ($vars->{ACTION_TYPE} eq 'weight') {
1.73      albertel 3668: 	$extra =  $vars->{WEIGHT};
                   3669:     }
                   3670:     $result .= "<li>";
1.74      matthew  3671:     my $what = &mt($dateTypeHash{$vars->{ACTION_TYPE}});
1.73      albertel 3672:     if ($extra) {
                   3673: 	$result .= &mt('Setting the [_1] to [_2]',"<b>$what</b>",$extra);
                   3674:     } else {
                   3675: 	$result .= &mt('Setting the [_1]',"<b>$what</b>");
1.48      bowersj2 3676:     }
1.38      bowersj2 3677:     $result .= "</li>\n";
1.27      bowersj2 3678:     if ($vars->{ACTION_TYPE} eq 'due_date' || 
                   3679:         $vars->{ACTION_TYPE} eq 'answer_date') {
                   3680:         # for due dates, we default to "date end" type entries
                   3681:         $result .= "<input type='hidden' name='recent_date_end' " .
                   3682:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3683:         $result .= "<input type='hidden' name='pres_value' " . 
                   3684:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3685:         $result .= "<input type='hidden' name='pres_type' " .
                   3686:             "value='date_end' />\n";
                   3687:     } elsif ($vars->{ACTION_TYPE} eq 'open_date') {
                   3688:         $result .= "<input type='hidden' name='recent_date_start' ".
                   3689:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3690:         $result .= "<input type='hidden' name='pres_value' " .
                   3691:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3692:         $result .= "<input type='hidden' name='pres_type' " .
                   3693:             "value='date_start' />\n";
1.38      bowersj2 3694:     } elsif ($vars->{ACTION_TYPE} eq 'tries') {
                   3695: 	$result .= "<input type='hidden' name='pres_value' " .
                   3696: 	    "value='" . $vars->{TRIES} . "' />\n";
1.104     albertel 3697:         $result .= "<input type='hidden' name='pres_type' " .
                   3698:             "value='int_pos' />\n";
1.48      bowersj2 3699:     } elsif ($vars->{ACTION_TYPE} eq 'weight') {
                   3700: 	$result .= "<input type='hidden' name='pres_value' " .
                   3701: 	    "value='" . $vars->{WEIGHT} . "' />\n";
1.38      bowersj2 3702:     }
1.27      bowersj2 3703: 
                   3704:     $result .= $resourceString;
                   3705:     
1.13      bowersj2 3706:     # Print targets
                   3707:     if ($vars->{TARGETS} eq 'course') {
1.76      sakharuk 3708:         $result .= '<li>'.&mt('for <b>all students in course</b>').'</li>';
1.13      bowersj2 3709:     } elsif ($vars->{TARGETS} eq 'section') {
                   3710:         my $section = $vars->{SECTION_NAME};
1.79      sakharuk 3711:         $result .= '<li>'.&mt('for section [_1]',"<b>$section</b>").'</li>';
1.104     albertel 3712: 	$result .= "<input type='hidden' name='csec' value='" .
1.89      foxr     3713:             HTML::Entities::encode($section,"'<>&\"") . "' />\n";
1.128     raeburn  3714:     } elsif ($vars->{TARGETS} eq 'group') {
                   3715:         my $group = $vars->{GROUP_NAME};
                   3716:         $result .= '<li>'.&mt('for group [_1]',"<b>$group</b>").'</li>';
                   3717:         $result .= "<input type='hidden' name='cgroup' value='" .
                   3718:             HTML::Entities::encode($group,"'<>&\"") . "' />\n";
1.13      bowersj2 3719:     } else {
                   3720:         # FIXME: This is probably wasteful! Store the name!
                   3721:         my $classlist = Apache::loncoursedata::get_classlist();
1.109     albertel 3722: 	my ($uname,$udom)=split(':',$vars->{USER_NAME});
1.106     albertel 3723:         my $name = $classlist->{$uname.':'.$udom}->[6];
1.79      sakharuk 3724:         $result .= '<li>'.&mt('for [_1]',"<b>$name</b>").'</li>';
1.13      bowersj2 3725:         $result .= "<input type='hidden' name='uname' value='".
1.89      foxr     3726:             HTML::Entities::encode($uname,"'<>&\"") . "' />\n";
1.13      bowersj2 3727:         $result .= "<input type='hidden' name='udom' value='".
1.89      foxr     3728:             HTML::Entities::encode($udom,"'<>&\"") . "' />\n";
1.13      bowersj2 3729:     }
                   3730: 
                   3731:     # Print value
1.48      bowersj2 3732:     if ($vars->{ACTION_TYPE} ne 'tries' && $vars->{ACTION_TYPE} ne 'weight') {
1.81      sakharuk 3733: 	$result .= '<li>'.&mt('to [_1] ([_2])',"<b>".ctime($vars->{PARM_DATE})."</b>",Apache::lonnavmaps::timeToHumanString($vars->{PARM_DATE}))."</li>\n";
1.38      bowersj2 3734:     }
                   3735:  
1.13      bowersj2 3736:     # print pres_marker
                   3737:     $result .= "\n<input type='hidden' name='pres_marker'" .
                   3738:         " value='$affectedResourceId&$parm_name&$level' />\n";
1.27      bowersj2 3739:     
                   3740:     # Make the table appear
                   3741:     $result .= "\n<input type='hidden' value='true' name='prevvisit' />";
                   3742:     $result .= "\n<input type='hidden' value='$symb' name='pssymb' />";
                   3743:     $result .= "\n<input type='hidden' value='$paramlevel' name='parmlev' />";
1.13      bowersj2 3744: 
1.71      sakharuk 3745:     $result .= "<br /><br /><center><input type='submit' value='".&mt('Finish Helper')."' /></center></form>\n";
1.13      bowersj2 3746: 
                   3747:     return $result;
                   3748: }
                   3749:     
                   3750: sub overrideForm {
                   3751:     return 1;
                   3752: }
1.5       bowersj2 3753: 
1.4       bowersj2 3754: 1;
1.3       bowersj2 3755: 
1.1       bowersj2 3756: __END__
1.3       bowersj2 3757: 

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