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

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # .helper XML handler to implement the LON-CAPA helper
                      3: #
1.163   ! albertel    4: # $Id: lonhelper.pm,v 1.162 2007/08/29 00:46:39 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.162     albertel 1011:     if (defined($token->[2]{'type'})) {
                   1012:         $paramHash->{TYPE} = $token->[2]{'type'};
                   1013:     }
1.4       bowersj2 1014:     return '';
1.3       bowersj2 1015: }
                   1016: 
1.10      bowersj2 1017: sub end_message {
1.4       bowersj2 1018:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1019: 
                   1020:     if ($target ne 'helper') {
                   1021:         return '';
                   1022:     }
1.10      bowersj2 1023:     Apache::lonhelper::message->new();
                   1024:     return '';
1.5       bowersj2 1025: }
                   1026: 
                   1027: sub render {
                   1028:     my $self = shift;
1.162     albertel 1029:     
                   1030:     if ($self->{TYPE} =~ /^\s*warning\s*$/i) {
                   1031: 	 $self->{MESSAGE_TEXT} = 
                   1032: 	     '<span class="LC_warning">'. $self->{MESSAGE_TEXT}.'</span>';
                   1033:     }
                   1034:     if ($self->{TYPE} =~ /^\s*error\s*$/i) {
                   1035: 	 $self->{MESSAGE_TEXT} = 
                   1036: 	     '<span class="LC_error">'. $self->{MESSAGE_TEXT}.'</span>';
                   1037:     }
1.161     albertel 1038:     return $self->{MESSAGE_TEXT};
1.5       bowersj2 1039: }
                   1040: # If a NEXTSTATE was given, switch to it
                   1041: sub postprocess {
                   1042:     my $self = shift;
                   1043:     if (defined($self->{NEXTSTATE})) {
                   1044:         $helper->changeState($self->{NEXTSTATE});
                   1045:     }
1.6       bowersj2 1046: 
                   1047:     return 1;
1.5       bowersj2 1048: }
                   1049: 1;
                   1050: 
1.159     albertel 1051: package Apache::lonhelper::helpicon;
                   1052: 
                   1053: =pod
                   1054: 
                   1055: =head1 Elements
                   1056: 
                   1057: =head2 Element: helpiconX<helpicon, helper element>
                   1058: 
                   1059: Helpicon elements add a help icon at the current location.
                   1060: Example:
                   1061: 
                   1062:    <helpicon file="Help">
                   1063:      General Help
                   1064:    </helpicon>
                   1065: 
                   1066: In this example will generate a help icon to the Help.hlp url with a
                   1067: description of 'General Help'. The description is not required and if
                   1068: left out (Example: <helpicon file="Help" /> only the icon will be
                   1069: added.)
                   1070: 
                   1071: =head3 Localization
                   1072: 
                   1073: The description text will be run through the normalize_string function
                   1074: and that will be used as a call to &mt.
                   1075: 
                   1076: =cut
                   1077: 
                   1078: no strict;
                   1079: @ISA = ("Apache::lonhelper::element");
                   1080: use strict;
                   1081: use Apache::lonlocal;
                   1082: 
                   1083: BEGIN {
                   1084:     &Apache::lonhelper::register('Apache::lonhelper::helpicon',
                   1085: 				 ('helpicon'));
                   1086: }
                   1087: 
                   1088: sub new {
                   1089:     my $ref = Apache::lonhelper::element->new();
                   1090:     bless($ref);
                   1091: }
                   1092: 
                   1093: # CONSTRUCTION: Construct the message element from the XML
                   1094: sub start_helpicon {
                   1095:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1096: 
                   1097:     if ($target ne 'helper') {
                   1098:         return '';
                   1099:     }
                   1100: 
                   1101:     $paramHash->{HELP_TEXT} = &mtn(&Apache::lonxml::get_all_text('/helpicon',
                   1102: 								 $parser));
                   1103: 
                   1104:     $paramHash->{HELP_TEXT} =~s/^\s+//;
                   1105:     $paramHash->{HELP_TEXT} =~s/\s+$//;
                   1106: 
                   1107:     if (defined($token->[2]{'file'})) {
                   1108:         $paramHash->{HELP_FILE} = $token->[2]{'file'};
                   1109:     }
                   1110:     return '';
                   1111: }
                   1112: 
                   1113: sub end_helpicon {
                   1114:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1115: 
                   1116:     if ($target ne 'helper') {
                   1117:         return '';
                   1118:     }
                   1119:     Apache::lonhelper::helpicon->new();
                   1120:     return '';
                   1121: }
                   1122: 
                   1123: sub render {
                   1124:     my $self = shift;
                   1125: 
                   1126:     my $text;
                   1127:     if ( $self->{HELP_TEXT} ne '') {
1.160     albertel 1128: 	$text=&mtn($self->{HELP_TEXT});
1.159     albertel 1129:     }
                   1130: 
                   1131:     return &Apache::loncommon::help_open_topic($self->{HELP_FILE},
                   1132: 					       $text);
                   1133: }
1.160     albertel 1134: sub postprocess {
                   1135:     my $self = shift;
                   1136:     if (defined($self->{NEXTSTATE})) {
                   1137:         $helper->changeState($self->{NEXTSTATE});
                   1138:     }
                   1139: 
                   1140:     return 1;
                   1141: }
                   1142: 
1.159     albertel 1143: 1;
                   1144: 
1.155     albertel 1145: package Apache::lonhelper::skip;
                   1146: 
                   1147: =pod
                   1148: 
                   1149: =head1 Elements
                   1150: 
                   1151: =head2 Element: skipX<skip>
                   1152: 
                   1153: The <skip> tag allows you define conditions under which the current state 
                   1154: should be skipped over and define what state to skip to.
                   1155: 
                   1156:   <state name="SKIP">
                   1157:     <skip>
                   1158:        <clause>
                   1159:          #some code that decides whether to skip the state or not
                   1160:        </clause>
                   1161:        <nextstate>FINISH</nextstate>
                   1162:     </skip>
                   1163:     <message nextstate="FINISH">A possibly skipped state</message>
                   1164:   </state>
                   1165: 
                   1166: =cut
                   1167: 
                   1168: no strict;
                   1169: @ISA = ("Apache::lonhelper::element");
                   1170: use strict;
                   1171: 
                   1172: BEGIN {
                   1173:     &Apache::lonhelper::register('Apache::lonhelper::skip',
                   1174: 				 ('skip'));
                   1175: }
                   1176: 
                   1177: sub new {
                   1178:     my $ref = Apache::lonhelper::element->new();
                   1179:     bless($ref);
                   1180: }
                   1181: 
                   1182: sub start_skip {
                   1183:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1184: 
                   1185:     if ($target ne 'helper') {
                   1186:         return '';
                   1187:     }
                   1188:     # let <cluase> know what text to skip to
                   1189:     $paramHash->{SKIPTAG}='/skip';
                   1190:     return '';
                   1191: }
                   1192: 
                   1193: sub end_skip {
                   1194:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1195: 
                   1196:     if ($target ne 'helper') {
                   1197:         return '';
                   1198:     }
                   1199:     Apache::lonhelper::skip->new();
                   1200:     return '';
                   1201: }
                   1202: 
                   1203: sub render {
                   1204:     my $self = shift;
                   1205:     return '';
                   1206: }
                   1207: # If a NEXTSTATE is set, switch to it
                   1208: sub preprocess {
                   1209:     my ($self) = @_;
                   1210: 
                   1211:     if (defined($self->{NEXTSTATE})) {
                   1212:         $helper->changeState($self->{NEXTSTATE});
                   1213:     }
                   1214: 
                   1215:     return 1;
                   1216: }
                   1217: 
                   1218: 1;
                   1219: 
1.5       bowersj2 1220: package Apache::lonhelper::choices;
                   1221: 
                   1222: =pod
                   1223: 
1.44      bowersj2 1224: =head2 Element: choicesX<choices, helper element>
1.5       bowersj2 1225: 
                   1226: Choice states provide a single choice to the user as a text selection box.
                   1227: A "choice" is two pieces of text, one which will be displayed to the user
                   1228: (the "human" value), and one which will be passed back to the program
                   1229: (the "computer" value). For instance, a human may choose from a list of
                   1230: resources on disk by title, while your program wants the file name.
                   1231: 
                   1232: <choices> takes an attribute "variable" to control which helper variable
                   1233: the result is stored in.
                   1234: 
                   1235: <choices> takes an attribute "multichoice" which, if set to a true
                   1236: value, will allow the user to select multiple choices.
                   1237: 
1.26      bowersj2 1238: <choices> takes an attribute "allowempty" which, if set to a true 
                   1239: value, will allow the user to select none of the choices without raising
                   1240: an error message.
                   1241: 
1.44      bowersj2 1242: =head3 SUB-TAGS
1.5       bowersj2 1243: 
1.44      bowersj2 1244: <choices> can have the following subtags:X<choice, helper tag>
1.5       bowersj2 1245: 
                   1246: =over 4
                   1247: 
                   1248: =item * <nextstate>state_name</nextstate>: If given, this will cause the
1.44      bowersj2 1249:       choice element to transition to the given state after executing.
                   1250:       This will override the <nextstate> passed to <choices> (if any).
1.5       bowersj2 1251: 
                   1252: =item * <choice />: If the choices are static,
                   1253:       this element will allow you to specify them. Each choice
                   1254:       contains  attribute, "computer", as described above. The
                   1255:       content of the tag will be used as the human label.
                   1256:       For example,  
                   1257:       <choice computer='234-12-7312'>Bobby McDormik</choice>.
                   1258: 
1.44      bowersj2 1259: <choice> can take a parameter "eval", which if set to
                   1260: a true value, will cause the contents of the tag to be
                   1261: evaluated as it would be in an <eval> tag; see <eval> tag
                   1262: below.
1.13      bowersj2 1263: 
1.5       bowersj2 1264: <choice> may optionally contain a 'nextstate' attribute, which
1.44      bowersj2 1265: will be the state transistioned to if the choice is made, if
                   1266: the choice is not multichoice. This will override the nextstate
                   1267: passed to the parent C<choices> tag.
1.5       bowersj2 1268: 
1.136     foxr     1269: <choice> may optionally contain a 'relatedvalue' attribute, which
                   1270: if present will cause a text entry to appear to the right of the
                   1271: selection.  The value of the relatedvalue attribute is a variable
                   1272: into which the text entry will be stored e.g.:
                   1273: <choice computer='numberprovided" relatedvalue="num">Type the number in:</choice>
                   1274: 
                   1275: <choice> may contain a relatededefault atribute which, if the
                   1276: relatedvalue attribute is present will be the initial value of the input
                   1277: box.
                   1278: 
1.5       bowersj2 1279: =back
                   1280: 
                   1281: To create the choices programmatically, either wrap the choices in 
                   1282: <condition> tags (prefered), or use an <exec> block inside the <choice>
                   1283: tag. Store the choices in $state->{CHOICES}, which is a list of list
                   1284: references, where each list has three strings. The first is the human
                   1285: name, the second is the computer name. and the third is the option
                   1286: next state. For example:
                   1287: 
                   1288:  <exec>
                   1289:     for (my $i = 65; $i < 65 + 26; $i++) {
                   1290:         push @{$state->{CHOICES}}, [chr($i), $i, 'next'];
                   1291:     }
                   1292:  </exec>
                   1293: 
                   1294: This will allow the user to select from the letters A-Z (in ASCII), while
                   1295: passing the ASCII value back into the helper variables, and the state
                   1296: will in all cases transition to 'next'.
                   1297: 
                   1298: You can mix and match methods of creating choices, as long as you always 
                   1299: "push" onto the choice list, rather then wiping it out. (You can even 
                   1300: remove choices programmatically, but that would probably be bad form.)
                   1301: 
1.44      bowersj2 1302: =head3 defaultvalue support
1.25      bowersj2 1303: 
                   1304: Choices supports default values both in multichoice and single choice mode.
                   1305: In single choice mode, have the defaultvalue tag's function return the 
                   1306: computer value of the box you want checked. If the function returns a value
                   1307: that does not correspond to any of the choices, the default behavior of selecting
                   1308: the first choice will be preserved.
                   1309: 
                   1310: For multichoice, return a string with the computer values you want checked,
                   1311: delimited by triple pipes. Note this matches how the result of the <choices>
                   1312: tag is stored in the {VARS} hash.
                   1313: 
1.5       bowersj2 1314: =cut
                   1315: 
                   1316: no strict;
                   1317: @ISA = ("Apache::lonhelper::element");
                   1318: use strict;
1.57      albertel 1319: use Apache::lonlocal;
1.100     albertel 1320: use Apache::lonnet;
1.5       bowersj2 1321: 
                   1322: BEGIN {
1.7       bowersj2 1323:     &Apache::lonhelper::register('Apache::lonhelper::choices',
1.5       bowersj2 1324:                               ('choice', 'choices'));
                   1325: }
                   1326: 
                   1327: sub new {
                   1328:     my $ref = Apache::lonhelper::element->new();
                   1329:     bless($ref);
                   1330: }
                   1331: 
                   1332: # CONSTRUCTION: Construct the message element from the XML
                   1333: sub start_choices {
                   1334:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1335: 
                   1336:     if ($target ne 'helper') {
                   1337:         return '';
                   1338:     }
                   1339: 
                   1340:     # Need to initialize the choices list, so everything can assume it exists
1.24      sakharuk 1341:     $paramHash->{'variable'} = $token->[2]{'variable'} if (!defined($paramHash->{'variable'}));
1.5       bowersj2 1342:     $helper->declareVar($paramHash->{'variable'});
                   1343:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.26      bowersj2 1344:     $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.5       bowersj2 1345:     $paramHash->{CHOICES} = [];
                   1346:     return '';
                   1347: }
                   1348: 
                   1349: sub end_choices {
                   1350:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1351: 
                   1352:     if ($target ne 'helper') {
                   1353:         return '';
                   1354:     }
                   1355:     Apache::lonhelper::choices->new();
                   1356:     return '';
                   1357: }
                   1358: 
                   1359: sub start_choice {
                   1360:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1361: 
                   1362:     if ($target ne 'helper') {
                   1363:         return '';
                   1364:     }
                   1365: 
                   1366:     my $computer = $token->[2]{'computer'};
1.69      sakharuk 1367:     my $human = &mt(&Apache::lonxml::get_all_text('/choice',
                   1368:                                               $parser));
1.136     foxr     1369:     my $nextstate  = $token->[2]{'nextstate'};
                   1370:     my $evalFlag   = $token->[2]{'eval'};
                   1371:     my $relatedVar = $token->[2]{'relatedvalue'}; 
                   1372:     my $relatedDefault = $token->[2]{'relateddefault'};
1.94      albertel 1373:     push @{$paramHash->{CHOICES}}, [&mtn($human), $computer, $nextstate, 
1.136     foxr     1374:                                     $evalFlag, $relatedVar, $relatedDefault];
1.5       bowersj2 1375:     return '';
                   1376: }
                   1377: 
                   1378: sub end_choice {
                   1379:     return '';
                   1380: }
                   1381: 
1.87      matthew  1382: {
                   1383:     # used to generate unique id attributes for <input> tags. 
                   1384:     # internal use only.
                   1385:     my $id = 0;
                   1386:     sub new_id { return $id++; }
                   1387: }
                   1388: 
1.5       bowersj2 1389: sub render {
                   1390:     my $self = shift;
                   1391:     my $var = $self->{'variable'};
                   1392:     my $buttons = '';
                   1393:     my $result = '';
                   1394: 
                   1395:     if ($self->{'multichoice'}) {
1.6       bowersj2 1396:         $result .= <<SCRIPT;
1.112     albertel 1397: <script type="text/javascript">
                   1398: // <!--
1.18      bowersj2 1399:     function checkall(value, checkName) {
1.15      bowersj2 1400: 	for (i=0; i<document.forms.helpform.elements.length; i++) {
1.18      bowersj2 1401:             ele = document.forms.helpform.elements[i];
1.157     raeburn  1402:             if (ele.name == checkName + '_forminput') {
1.18      bowersj2 1403:                 document.forms.helpform.elements[i].checked=value;
                   1404:             }
1.5       bowersj2 1405:         }
                   1406:     }
1.112     albertel 1407: // -->
1.5       bowersj2 1408: </script>
                   1409: SCRIPT
1.25      bowersj2 1410:     }
                   1411: 
                   1412:     # Only print "select all" and "unselect all" if there are five or
                   1413:     # more choices; fewer then that and it looks silly.
                   1414:     if ($self->{'multichoice'} && scalar(@{$self->{CHOICES}}) > 4) {
1.68      sakharuk 1415:         my %lt=&Apache::lonlocal::texthash(
                   1416: 			'sa'  => "Select All",
                   1417: 		        'ua'  => "Unselect All");
1.5       bowersj2 1418:         $buttons = <<BUTTONS;
                   1419: <br />
1.68      sakharuk 1420: <input type="button" onclick="checkall(true, '$var')" value="$lt{'sa'}" />
                   1421: <input type="button" onclick="checkall(false, '$var')" value="$lt{'ua'}" />
1.6       bowersj2 1422: <br />&nbsp;
1.5       bowersj2 1423: BUTTONS
                   1424:     }
                   1425: 
1.16      bowersj2 1426:     if (defined $self->{ERROR_MSG}) {
1.6       bowersj2 1427:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
1.5       bowersj2 1428:     }
                   1429: 
                   1430:     $result .= $buttons;
1.6       bowersj2 1431:     
1.5       bowersj2 1432:     $result .= "<table>\n\n";
                   1433: 
1.25      bowersj2 1434:     my %checkedChoices;
                   1435:     my $checkedChoicesFunc;
                   1436: 
                   1437:     if (defined($self->{DEFAULT_VALUE})) {
                   1438:         $checkedChoicesFunc = eval ($self->{DEFAULT_VALUE});
                   1439:         die 'Error in default value code for variable ' . 
1.34      bowersj2 1440:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
1.25      bowersj2 1441:     } else {
                   1442:         $checkedChoicesFunc = sub { return ''; };
                   1443:     }
                   1444: 
                   1445:     # Process which choices should be checked.
                   1446:     if ($self->{'multichoice'}) {
                   1447:         for my $selectedChoice (split(/\|\|\|/, (&$checkedChoicesFunc($helper, $self)))) {
                   1448:             $checkedChoices{$selectedChoice} = 1;
                   1449:         }
                   1450:     } else {
                   1451:         # single choice
                   1452:         my $selectedChoice = &$checkedChoicesFunc($helper, $self);
                   1453:         
                   1454:         my $foundChoice = 0;
                   1455:         
                   1456:         # check that the choice is in the list of choices.
                   1457:         for my $choice (@{$self->{CHOICES}}) {
                   1458:             if ($choice->[1] eq $selectedChoice) {
                   1459:                 $checkedChoices{$choice->[1]} = 1;
                   1460:                 $foundChoice = 1;
                   1461:             }
                   1462:         }
                   1463:         
                   1464:         # If we couldn't find the choice, pick the first one 
                   1465:         if (!$foundChoice) {
                   1466:             $checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
                   1467:         }
                   1468:     }
                   1469: 
1.5       bowersj2 1470:     my $type = "radio";
                   1471:     if ($self->{'multichoice'}) { $type = 'checkbox'; }
                   1472:     foreach my $choice (@{$self->{CHOICES}}) {
1.87      matthew  1473:         my $id = &new_id();
1.5       bowersj2 1474:         $result .= "<tr>\n<td width='20'>&nbsp;</td>\n";
1.157     raeburn  1475:         $result .= "<td valign='top'><input type='$type' name='${var}_forminput'"
1.111     albertel 1476:             . " value='" . 
1.89      foxr     1477:             HTML::Entities::encode($choice->[1],"<>&\"'") 
1.5       bowersj2 1478:             . "'";
1.25      bowersj2 1479:         if ($checkedChoices{$choice->[1]}) {
1.111     albertel 1480:             $result .= " checked='checked' ";
1.5       bowersj2 1481:         }
1.111     albertel 1482:         $result .= qq{id="id$id"};
1.13      bowersj2 1483:         my $choiceLabel = $choice->[0];
1.136     foxr     1484:         if ($choice->[3]) {  # if we need to evaluate this choice
1.13      bowersj2 1485:             $choiceLabel = "sub { my $helper = shift; my $state = shift;" .
                   1486:                 $choiceLabel . "}";
                   1487:             $choiceLabel = eval($choiceLabel);
                   1488:             $choiceLabel = &$choiceLabel($helper, $self);
                   1489:         }
1.111     albertel 1490:         $result .= "/></td><td> ".qq{<label for="id$id">}.
1.161     albertel 1491:             &mtn($choiceLabel). "</label></td>";
1.136     foxr     1492: 	if ($choice->[4]) {
                   1493: 	    $result .='<td><input type="text" size="5" name="'
1.157     raeburn  1494: 		.$choice->[4].'_forminput" value="'
1.136     foxr     1495:                 .$choice->[5].'" /></td>';
                   1496: 	}
                   1497: 	$result .= "</tr>\n";
1.5       bowersj2 1498:     }
                   1499:     $result .= "</table>\n\n\n";
                   1500:     $result .= $buttons;
                   1501: 
                   1502:     return $result;
                   1503: }
                   1504: 
                   1505: # If a NEXTSTATE was given or a nextstate for this choice was
                   1506: # given, switch to it
                   1507: sub postprocess {
                   1508:     my $self = shift;
1.157     raeburn  1509:     my $chosenValue = $env{'form.' . $self->{'variable'} . '_forminput'};
1.5       bowersj2 1510: 
1.26      bowersj2 1511:     if (!defined($chosenValue) && !$self->{'allowempty'}) {
1.59      bowersj2 1512:         $self->{ERROR_MSG} = 
                   1513: 	    &mt("You must choose one or more choices to continue.");
1.6       bowersj2 1514:         return 0;
                   1515:     }
                   1516: 
1.28      bowersj2 1517:     if (ref($chosenValue)) {
                   1518:         $helper->{VARS}->{$self->{'variable'}} = join('|||', @$chosenValue);
1.42      bowersj2 1519:     }
                   1520: 
                   1521:     if (defined($self->{NEXTSTATE})) {
                   1522:         $helper->changeState($self->{NEXTSTATE});
                   1523:     }
                   1524:     
                   1525:     foreach my $choice (@{$self->{CHOICES}}) {
                   1526:         if ($choice->[1] eq $chosenValue) {
                   1527:             if (defined($choice->[2])) {
                   1528:                 $helper->changeState($choice->[2]);
                   1529:             }
                   1530:         }
1.136     foxr     1531: 	if ($choice->[4]) {
                   1532: 	    my $varname = $choice->[4];
1.157     raeburn  1533: 	    $helper->{'VARS'}->{$varname} = $env{'form.'."${varname}_forminput"};
1.136     foxr     1534: 	}
1.42      bowersj2 1535:     }
                   1536:     return 1;
                   1537: }
                   1538: 1;
                   1539: 
                   1540: package Apache::lonhelper::dropdown;
                   1541: 
                   1542: =pod
                   1543: 
1.44      bowersj2 1544: =head2 Element: dropdownX<dropdown, helper tag>
1.42      bowersj2 1545: 
                   1546: A drop-down provides a drop-down box instead of a radio button
                   1547: box. Because most people do not know how to use a multi-select
                   1548: drop-down box, that option is not allowed. Otherwise, the arguments
                   1549: are the same as "choices", except "allowempty" is also meaningless.
                   1550: 
                   1551: <dropdown> takes an attribute "variable" to control which helper variable
                   1552: the result is stored in.
                   1553: 
1.44      bowersj2 1554: =head3 SUB-TAGS
1.42      bowersj2 1555: 
                   1556: <choice>, which acts just as it does in the "choices" element.
                   1557: 
                   1558: =cut
                   1559: 
1.57      albertel 1560: # This really ought to be a sibling class to "choice" which is itself
                   1561: # a child of some abstract class.... *shrug*
                   1562: 
1.42      bowersj2 1563: no strict;
                   1564: @ISA = ("Apache::lonhelper::element");
                   1565: use strict;
1.57      albertel 1566: use Apache::lonlocal;
1.100     albertel 1567: use Apache::lonnet;
1.42      bowersj2 1568: 
                   1569: BEGIN {
                   1570:     &Apache::lonhelper::register('Apache::lonhelper::dropdown',
                   1571:                               ('dropdown'));
                   1572: }
                   1573: 
                   1574: sub new {
                   1575:     my $ref = Apache::lonhelper::element->new();
                   1576:     bless($ref);
                   1577: }
                   1578: 
                   1579: # CONSTRUCTION: Construct the message element from the XML
                   1580: sub start_dropdown {
                   1581:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1582: 
                   1583:     if ($target ne 'helper') {
                   1584:         return '';
                   1585:     }
                   1586: 
                   1587:     # Need to initialize the choices list, so everything can assume it exists
                   1588:     $paramHash->{'variable'} = $token->[2]{'variable'} if (!defined($paramHash->{'variable'}));
                   1589:     $helper->declareVar($paramHash->{'variable'});
                   1590:     $paramHash->{CHOICES} = [];
                   1591:     return '';
                   1592: }
                   1593: 
                   1594: sub end_dropdown {
                   1595:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1596: 
                   1597:     if ($target ne 'helper') {
                   1598:         return '';
                   1599:     }
                   1600:     Apache::lonhelper::dropdown->new();
                   1601:     return '';
                   1602: }
                   1603: 
                   1604: sub render {
                   1605:     my $self = shift;
                   1606:     my $var = $self->{'variable'};
                   1607:     my $result = '';
                   1608: 
                   1609:     if (defined $self->{ERROR_MSG}) {
                   1610:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
                   1611:     }
                   1612: 
                   1613:     my %checkedChoices;
                   1614:     my $checkedChoicesFunc;
                   1615: 
                   1616:     if (defined($self->{DEFAULT_VALUE})) {
                   1617:         $checkedChoicesFunc = eval ($self->{DEFAULT_VALUE});
                   1618:         die 'Error in default value code for variable ' . 
                   1619:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
                   1620:     } else {
                   1621:         $checkedChoicesFunc = sub { return ''; };
                   1622:     }
                   1623: 
                   1624:     # single choice
                   1625:     my $selectedChoice = &$checkedChoicesFunc($helper, $self);
                   1626:     
                   1627:     my $foundChoice = 0;
                   1628:     
                   1629:     # check that the choice is in the list of choices.
                   1630:     for my $choice (@{$self->{CHOICES}}) {
                   1631: 	if ($choice->[1] eq $selectedChoice) {
                   1632: 	    $checkedChoices{$choice->[1]} = 1;
                   1633: 	    $foundChoice = 1;
                   1634: 	}
                   1635:     }
                   1636:     
                   1637:     # If we couldn't find the choice, pick the first one 
                   1638:     if (!$foundChoice) {
                   1639: 	$checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
                   1640:     }
                   1641: 
1.157     raeburn  1642:     $result .= "<select name='${var}_forminput'>\n";
1.42      bowersj2 1643:     foreach my $choice (@{$self->{CHOICES}}) {
                   1644:         $result .= "<option value='" . 
1.89      foxr     1645:             HTML::Entities::encode($choice->[1],"<>&\"'") 
1.42      bowersj2 1646:             . "'";
                   1647:         if ($checkedChoices{$choice->[1]}) {
1.111     albertel 1648:             $result .= " selected='selected' ";
1.42      bowersj2 1649:         }
                   1650:         my $choiceLabel = $choice->[0];
                   1651:         if ($choice->[4]) {  # if we need to evaluate this choice
                   1652:             $choiceLabel = "sub { my $helper = shift; my $state = shift;" .
                   1653:                 $choiceLabel . "}";
                   1654:             $choiceLabel = eval($choiceLabel);
                   1655:             $choiceLabel = &$choiceLabel($helper, $self);
                   1656:         }
1.112     albertel 1657:         $result .= ">" . &mtn($choiceLabel) . "</option>\n";
1.42      bowersj2 1658:     }
1.43      bowersj2 1659:     $result .= "</select>\n";
1.42      bowersj2 1660: 
                   1661:     return $result;
                   1662: }
                   1663: 
                   1664: # If a NEXTSTATE was given or a nextstate for this choice was
                   1665: # given, switch to it
                   1666: sub postprocess {
                   1667:     my $self = shift;
1.157     raeburn  1668:     my $chosenValue = $env{'form.' . $self->{'variable'} . '_forminput'};
1.42      bowersj2 1669: 
                   1670:     if (!defined($chosenValue) && !$self->{'allowempty'}) {
                   1671:         $self->{ERROR_MSG} = "You must choose one or more choices to" .
                   1672:             " continue.";
                   1673:         return 0;
1.6       bowersj2 1674:     }
                   1675: 
1.5       bowersj2 1676:     if (defined($self->{NEXTSTATE})) {
                   1677:         $helper->changeState($self->{NEXTSTATE});
                   1678:     }
                   1679:     
                   1680:     foreach my $choice (@{$self->{CHOICES}}) {
                   1681:         if ($choice->[1] eq $chosenValue) {
                   1682:             if (defined($choice->[2])) {
                   1683:                 $helper->changeState($choice->[2]);
                   1684:             }
                   1685:         }
                   1686:     }
1.6       bowersj2 1687:     return 1;
1.5       bowersj2 1688: }
                   1689: 1;
                   1690: 
                   1691: package Apache::lonhelper::date;
                   1692: 
                   1693: =pod
                   1694: 
1.44      bowersj2 1695: =head2 Element: dateX<date, helper element>
1.5       bowersj2 1696: 
                   1697: Date elements allow the selection of a date with a drop down list.
                   1698: 
                   1699: Date elements can take two attributes:
                   1700: 
                   1701: =over 4
                   1702: 
                   1703: =item * B<variable>: The name of the variable to store the chosen
                   1704:         date in. Required.
                   1705: 
                   1706: =item * B<hoursminutes>: If a true value, the date will show hours
                   1707:         and minutes, as well as month/day/year. If false or missing,
                   1708:         the date will only show the month, day, and year.
                   1709: 
                   1710: =back
                   1711: 
                   1712: Date elements contain only an option <nextstate> tag to determine
                   1713: the next state.
                   1714: 
                   1715: Example:
                   1716: 
                   1717:  <date variable="DUE_DATE" hoursminutes="1">
                   1718:    <nextstate>choose_why</nextstate>
                   1719:    </date>
                   1720: 
                   1721: =cut
                   1722: 
                   1723: no strict;
                   1724: @ISA = ("Apache::lonhelper::element");
                   1725: use strict;
1.57      albertel 1726: use Apache::lonlocal; # A localization nightmare
1.100     albertel 1727: use Apache::lonnet;
1.5       bowersj2 1728: use Time::localtime;
                   1729: 
                   1730: BEGIN {
1.7       bowersj2 1731:     &Apache::lonhelper::register('Apache::lonhelper::date',
1.5       bowersj2 1732:                               ('date'));
                   1733: }
                   1734: 
                   1735: # Don't need to override the "new" from element
                   1736: sub new {
                   1737:     my $ref = Apache::lonhelper::element->new();
                   1738:     bless($ref);
                   1739: }
                   1740: 
                   1741: my @months = ("January", "February", "March", "April", "May", "June", "July",
                   1742: 	      "August", "September", "October", "November", "December");
                   1743: 
                   1744: # CONSTRUCTION: Construct the message element from the XML
                   1745: sub start_date {
                   1746:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1747: 
                   1748:     if ($target ne 'helper') {
                   1749:         return '';
                   1750:     }
                   1751: 
                   1752:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   1753:     $helper->declareVar($paramHash->{'variable'});
                   1754:     $paramHash->{'hoursminutes'} = $token->[2]{'hoursminutes'};
1.118     albertel 1755:     $paramHash->{'anytime'} = $token->[2]{'anytime'};
1.5       bowersj2 1756: }
                   1757: 
                   1758: sub end_date {
                   1759:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1760: 
                   1761:     if ($target ne 'helper') {
                   1762:         return '';
                   1763:     }
                   1764:     Apache::lonhelper::date->new();
                   1765:     return '';
                   1766: }
                   1767: 
                   1768: sub render {
                   1769:     my $self = shift;
                   1770:     my $result = "";
                   1771:     my $var = $self->{'variable'};
                   1772: 
                   1773:     my $date;
1.118     albertel 1774: 
                   1775:     my $time=time;
1.119     albertel 1776:     my ($anytime,$onclick);
1.118     albertel 1777: 
1.137     albertel 1778: 
                   1779:     # first check VARS for a valid new value from the user
                   1780:     # then check DEFAULT_VALUE for a valid default time value
                   1781:     # otherwise pick now as reasonably good time
                   1782: 
                   1783:     if (defined($helper->{VARS}{$var})
                   1784: 	&&  $helper->{VARS}{$var} > 0) {
                   1785: 	$date = localtime($helper->{VARS}{$var});
                   1786:     } elsif (defined($self->{DEFAULT_VALUE})) {
1.118     albertel 1787:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   1788:         die('Error in default value code for variable ' . 
                   1789:             $self->{'variable'} . ', Perl said: ' . $@) if $@;
                   1790:         $time = &$valueFunc($helper, $self);
1.130     albertel 1791: 	if (lc($time) eq 'anytime') {
                   1792: 	    $anytime=1;
1.137     albertel 1793: 	    $date = localtime(time);
                   1794: 	    $date->min(0);
                   1795: 	} elsif (defined($time) && $time ne 0) {
                   1796: 	    $date = localtime($time);
1.130     albertel 1797: 	} else {
1.137     albertel 1798: 	    # leave date undefined so it'll default to now
1.130     albertel 1799: 	}
1.137     albertel 1800:     }
1.130     albertel 1801: 
1.138     albertel 1802:     if (!defined($date)) {
1.137     albertel 1803: 	$date = localtime(time);
                   1804: 	$date->min(0);
1.118     albertel 1805:     }
1.137     albertel 1806: 
1.119     albertel 1807:     if ($anytime) {
                   1808: 	$onclick = "onclick=\"javascript:updateCheck(this.form,'${var}anytime',false)\"";
                   1809:     }
1.5       bowersj2 1810:     # Default date: The current hour.
                   1811: 
                   1812:     if (defined $self->{ERROR_MSG}) {
                   1813:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                   1814:     }
                   1815: 
                   1816:     # Month
                   1817:     my $i;
1.119     albertel 1818:     $result .= "<select $onclick name='${var}month'>\n";
1.5       bowersj2 1819:     for ($i = 0; $i < 12; $i++) {
                   1820:         if ($i == $date->mon) {
1.111     albertel 1821:             $result .= "<option value='$i' selected='selected'>";
1.5       bowersj2 1822:         } else {
                   1823:             $result .= "<option value='$i'>";
                   1824:         }
1.57      albertel 1825:         $result .= &mt($months[$i]) . "</option>\n";
1.5       bowersj2 1826:     }
                   1827:     $result .= "</select>\n";
                   1828: 
                   1829:     # Day
1.119     albertel 1830:     $result .= "<select $onclick name='${var}day'>\n";
1.5       bowersj2 1831:     for ($i = 1; $i < 32; $i++) {
                   1832:         if ($i == $date->mday) {
1.111     albertel 1833:             $result .= '<option selected="selected">';
1.5       bowersj2 1834:         } else {
                   1835:             $result .= '<option>';
                   1836:         }
                   1837:         $result .= "$i</option>\n";
                   1838:     }
                   1839:     $result .= "</select>,\n";
                   1840: 
                   1841:     # Year
1.119     albertel 1842:     $result .= "<select $onclick name='${var}year'>\n";
1.5       bowersj2 1843:     for ($i = 2000; $i < 2030; $i++) { # update this after 64-bit dates
                   1844:         if ($date->year + 1900 == $i) {
1.111     albertel 1845:             $result .= "<option selected='selected'>";
1.5       bowersj2 1846:         } else {
                   1847:             $result .= "<option>";
                   1848:         }
                   1849:         $result .= "$i</option>\n";
                   1850:     }
                   1851:     $result .= "</select>,\n";
                   1852: 
                   1853:     # Display Hours and Minutes if they are called for
                   1854:     if ($self->{'hoursminutes'}) {
1.59      bowersj2 1855: 	# This needs parameterization for times.
                   1856: 	my $am = &mt('a.m.');
                   1857: 	my $pm = &mt('p.m.');
1.5       bowersj2 1858:         # Build hour
1.119     albertel 1859:         $result .= "<select $onclick name='${var}hour'>\n";
1.111     albertel 1860:         $result .= "<option " . ($date->hour == 0 ? 'selected="selected" ':'') .
1.59      bowersj2 1861:             " value='0'>" . &mt('midnight') . "</option>\n";
1.5       bowersj2 1862:         for ($i = 1; $i < 12; $i++) {
                   1863:             if ($date->hour == $i) {
1.111     albertel 1864:                 $result .= "<option selected='selected' value='$i'>$i $am</option>\n";
1.5       bowersj2 1865:             } else {
1.59      bowersj2 1866:                 $result .= "<option value='$i'>$i $am</option>\n";
1.5       bowersj2 1867:             }
                   1868:         }
1.111     albertel 1869:         $result .= "<option " . ($date->hour == 12 ? 'selected="selected" ':'') .
1.59      bowersj2 1870:             " value='12'>" . &mt('noon') . "</option>\n";
1.5       bowersj2 1871:         for ($i = 13; $i < 24; $i++) {
                   1872:             my $printedHour = $i - 12;
                   1873:             if ($date->hour == $i) {
1.111     albertel 1874:                 $result .= "<option selected='selected' value='$i'>$printedHour $pm</option>\n";
1.5       bowersj2 1875:             } else {
1.59      bowersj2 1876:                 $result .= "<option value='$i'>$printedHour $pm</option>\n";
1.5       bowersj2 1877:             }
                   1878:         }
                   1879: 
                   1880:         $result .= "</select> :\n";
                   1881: 
1.119     albertel 1882:         $result .= "<select $onclick name='${var}minute'>\n";
1.120     albertel 1883: 	my $selected=0;
                   1884:         for my $i ((0,15,30,45,59,undef,0..59)) {
1.5       bowersj2 1885:             my $printedMinute = $i;
1.117     albertel 1886:             if (defined($i) && $i < 10) {
1.5       bowersj2 1887:                 $printedMinute = "0" . $printedMinute;
                   1888:             }
1.120     albertel 1889:             if (!$selected && $date->min == $i) {
1.111     albertel 1890:                 $result .= "<option selected='selected'>";
1.120     albertel 1891: 		$selected=1;
1.5       bowersj2 1892:             } else {
                   1893:                 $result .= "<option>";
                   1894:             }
                   1895:             $result .= "$printedMinute</option>\n";
                   1896:         }
                   1897:         $result .= "</select>\n";
                   1898:     }
1.118     albertel 1899:     if ($self->{'anytime'}) {
1.121     albertel 1900: 	$result.=(<<CHECK);
1.119     albertel 1901: <script type="text/javascript">
                   1902: // <!--
                   1903:     function updateCheck(form,name,value) {
                   1904: 	var checkbox=form[name];
                   1905: 	checkbox.checked = value;
                   1906:     }
                   1907: // -->
                   1908: </script>
                   1909: CHECK
1.118     albertel 1910: 	$result.="&nbsp;or&nbsp;<label><input type='checkbox' ";
                   1911: 	if ($anytime) {
                   1912: 	    $result.=' checked="checked" '
                   1913: 	}
1.138     albertel 1914: 	$result.="name='${var}anytime'/>".&mt('Any time').'</label>'
1.118     albertel 1915:     }
1.5       bowersj2 1916:     return $result;
                   1917: 
                   1918: }
                   1919: # If a NEXTSTATE was given, switch to it
                   1920: sub postprocess {
                   1921:     my $self = shift;
                   1922:     my $var = $self->{'variable'};
1.118     albertel 1923:     if ($env{'form.' . $var . 'anytime'}) {
                   1924: 	$helper->{VARS}->{$var} = undef;
                   1925:     } else {
                   1926: 	my $month = $env{'form.' . $var . 'month'}; 
                   1927: 	my $day = $env{'form.' . $var . 'day'}; 
                   1928: 	my $year = $env{'form.' . $var . 'year'}; 
                   1929: 	my $min = 0; 
                   1930: 	my $hour = 0;
                   1931: 	if ($self->{'hoursminutes'}) {
                   1932: 	    $min = $env{'form.' . $var . 'minute'};
                   1933: 	    $hour = $env{'form.' . $var . 'hour'};
                   1934: 	}
                   1935: 
                   1936: 	my $chosenDate;
                   1937: 	eval {$chosenDate = Time::Local::timelocal(0, $min, $hour, $day, $month, $year);};
                   1938: 	my $error = $@;
                   1939: 
                   1940: 	# Check to make sure that the date was not automatically co-erced into a 
                   1941: 	# valid date, as we want to flag that as an error
                   1942: 	# This happens for "Feb. 31", for instance, which is coerced to March 2 or
                   1943: 	# 3, depending on if it's a leap year
                   1944: 	my $checkDate = localtime($chosenDate);
                   1945: 	
                   1946: 	if ($error || $checkDate->mon != $month || $checkDate->mday != $day ||
                   1947: 	    $checkDate->year + 1900 != $year) {
                   1948: 	    unless (Apache::lonlocal::current_language()== ~/^en/) {
                   1949: 		$self->{ERROR_MSG} = &mt("Invalid date entry");
                   1950: 		return 0;
                   1951: 	    }
                   1952: 	    # LOCALIZATION FIXME: Needs to be parameterized
                   1953: 	    $self->{ERROR_MSG} = "Can't use " . $months[$month] . " $day, $year as a "
                   1954: 		. "date because it doesn't exist. Please enter a valid date.";
1.5       bowersj2 1955: 
1.57      albertel 1956: 	    return 0;
                   1957: 	}
1.118     albertel 1958: 	$helper->{VARS}->{$var} = $chosenDate;
1.5       bowersj2 1959:     }
                   1960: 
1.137     albertel 1961:     if (defined($self->{VALIDATOR})) {
                   1962: 	my $validator = eval($self->{VALIDATOR});
1.138     albertel 1963: 	die 'Died during evaluation of validator code; Perl said: ' . $@ if $@;
1.137     albertel 1964: 	my $invalid = &$validator($helper, $state, $self, $self->getValue());
                   1965: 	if ($invalid) {
                   1966: 	    $self->{ERROR_MSG} = $invalid;
                   1967: 	    return 0;
                   1968: 	}
                   1969:     }
                   1970: 
1.5       bowersj2 1971:     if (defined($self->{NEXTSTATE})) {
                   1972:         $helper->changeState($self->{NEXTSTATE});
                   1973:     }
1.6       bowersj2 1974: 
                   1975:     return 1;
1.5       bowersj2 1976: }
                   1977: 1;
                   1978: 
                   1979: package Apache::lonhelper::resource;
                   1980: 
                   1981: =pod
                   1982: 
1.44      bowersj2 1983: =head2 Element: resourceX<resource, helper element>
1.5       bowersj2 1984: 
                   1985: <resource> elements allow the user to select one or multiple resources
                   1986: from the current course. You can filter out which resources they can view,
                   1987: and filter out which resources they can select. The course will always
                   1988: be displayed fully expanded, because of the difficulty of maintaining
                   1989: selections across folder openings and closings. If this is fixed, then
                   1990: the user can manipulate the folders.
                   1991: 
                   1992: <resource> takes the standard variable attribute to control what helper
1.44      bowersj2 1993: variable stores the results. It also takes a "multichoice"X<multichoice> attribute,
1.17      bowersj2 1994: which controls whether the user can select more then one resource. The 
                   1995: "toponly" attribute controls whether the resource display shows just the
                   1996: resources in that sequence, or recurses into all sub-sequences, defaulting
1.29      bowersj2 1997: to false. The "suppressEmptySequences" attribute reflects the 
                   1998: suppressEmptySequences argument to the render routine, which will cause
                   1999: folders that have all of their contained resources filtered out to also
1.46      bowersj2 2000: be filtered out. The 'addstatus' attribute, if true, will add the icon
1.95      albertel 2001: and long status display columns to the display. The 'addparts'
                   2002: attribute will add in a part selector beside problems that have more
1.163   ! albertel 2003: than 1 part. The 'includecourse' attribute if true, will include
        !          2004: the toplevel default.sequence in the results.
1.5       bowersj2 2005: 
1.44      bowersj2 2006: =head3 SUB-TAGS
1.5       bowersj2 2007: 
                   2008: =over 4
                   2009: 
1.44      bowersj2 2010: =item * <filterfunc>X<filterfunc>: If you want to filter what resources are displayed
1.5       bowersj2 2011:   to the user, use a filter func. The <filterfunc> tag should contain
                   2012:   Perl code that when wrapped with "sub { my $res = shift; " and "}" is 
                   2013:   a function that returns true if the resource should be displayed, 
                   2014:   and false if it should be skipped. $res is a resource object. 
                   2015:   (See Apache::lonnavmaps documentation for information about the 
                   2016:   resource object.)
                   2017: 
1.44      bowersj2 2018: =item * <choicefunc>X<choicefunc>: Same as <filterfunc>, except that controls whether
1.5       bowersj2 2019:   the given resource can be chosen. (It is almost always a good idea to
                   2020:   show the user the folders, for instance, but you do not always want to 
                   2021:   let the user select them.)
                   2022: 
                   2023: =item * <nextstate>: Standard nextstate behavior.
                   2024: 
1.44      bowersj2 2025: =item * <valuefunc>X<valuefunc>: This function controls what is returned by the resource
1.5       bowersj2 2026:   when the user selects it. Like filterfunc and choicefunc, it should be
                   2027:   a function fragment that when wrapped by "sub { my $res = shift; " and
                   2028:   "}" returns a string representing what you want to have as the value. By
                   2029:   default, the value will be the resource ID of the object ($res->{ID}).
                   2030: 
1.44      bowersj2 2031: =item * <mapurl>X<mapurl>: If the URL of a map is given here, only that map
1.48      bowersj2 2032:   will be displayed, instead of the whole course. If the attribute
                   2033:   "evaluate" is given and is true, the contents of the mapurl will be
                   2034:   evaluated with "sub { my $helper = shift; my $state = shift;" and
                   2035:   "}", with the return value used as the mapurl.
1.13      bowersj2 2036: 
1.5       bowersj2 2037: =back
                   2038: 
                   2039: =cut
                   2040: 
                   2041: no strict;
                   2042: @ISA = ("Apache::lonhelper::element");
                   2043: use strict;
1.100     albertel 2044: use Apache::lonnet;
1.5       bowersj2 2045: 
                   2046: BEGIN {
1.7       bowersj2 2047:     &Apache::lonhelper::register('Apache::lonhelper::resource',
1.5       bowersj2 2048:                               ('resource', 'filterfunc', 
1.13      bowersj2 2049:                                'choicefunc', 'valuefunc',
1.90      foxr     2050:                                'mapurl','option'));
1.5       bowersj2 2051: }
                   2052: 
                   2053: sub new {
                   2054:     my $ref = Apache::lonhelper::element->new();
                   2055:     bless($ref);
                   2056: }
                   2057: 
                   2058: # CONSTRUCTION: Construct the message element from the XML
                   2059: sub start_resource {
                   2060:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2061: 
                   2062:     if ($target ne 'helper') {
                   2063:         return '';
                   2064:     }
                   2065: 
                   2066:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   2067:     $helper->declareVar($paramHash->{'variable'});
1.14      bowersj2 2068:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.29      bowersj2 2069:     $paramHash->{'suppressEmptySequences'} = $token->[2]{'suppressEmptySequences'};
1.17      bowersj2 2070:     $paramHash->{'toponly'} = $token->[2]{'toponly'};
1.46      bowersj2 2071:     $paramHash->{'addstatus'} = $token->[2]{'addstatus'};
1.95      albertel 2072:     $paramHash->{'addparts'} = $token->[2]{'addparts'};
                   2073:     if ($paramHash->{'addparts'}) {
                   2074: 	$helper->declareVar($paramHash->{'variable'}.'_part');
                   2075:     }
1.66      albertel 2076:     $paramHash->{'closeallpages'} = $token->[2]{'closeallpages'};
1.163   ! albertel 2077:     $paramHash->{'include_top_level_map'} = $token->[2]{'includecourse'};
1.5       bowersj2 2078:     return '';
                   2079: }
                   2080: 
                   2081: sub end_resource {
                   2082:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2083: 
                   2084:     if ($target ne 'helper') {
                   2085:         return '';
                   2086:     }
                   2087:     if (!defined($paramHash->{FILTER_FUNC})) {
                   2088:         $paramHash->{FILTER_FUNC} = sub {return 1;};
                   2089:     }
                   2090:     if (!defined($paramHash->{CHOICE_FUNC})) {
                   2091:         $paramHash->{CHOICE_FUNC} = sub {return 1;};
                   2092:     }
                   2093:     if (!defined($paramHash->{VALUE_FUNC})) {
                   2094:         $paramHash->{VALUE_FUNC} = sub {my $res = shift; return $res->{ID}; };
                   2095:     }
                   2096:     Apache::lonhelper::resource->new();
1.4       bowersj2 2097:     return '';
                   2098: }
                   2099: 
1.5       bowersj2 2100: sub start_filterfunc {
                   2101:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2102: 
                   2103:     if ($target ne 'helper') {
                   2104:         return '';
                   2105:     }
                   2106: 
                   2107:     my $contents = Apache::lonxml::get_all_text('/filterfunc',
                   2108:                                                 $parser);
                   2109:     $contents = 'sub { my $res = shift; ' . $contents . '}';
                   2110:     $paramHash->{FILTER_FUNC} = eval $contents;
                   2111: }
                   2112: 
                   2113: sub end_filterfunc { return ''; }
                   2114: 
                   2115: sub start_choicefunc {
                   2116:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2117: 
                   2118:     if ($target ne 'helper') {
                   2119:         return '';
                   2120:     }
                   2121: 
                   2122:     my $contents = Apache::lonxml::get_all_text('/choicefunc',
                   2123:                                                 $parser);
                   2124:     $contents = 'sub { my $res = shift; ' . $contents . '}';
                   2125:     $paramHash->{CHOICE_FUNC} = eval $contents;
                   2126: }
                   2127: 
                   2128: sub end_choicefunc { return ''; }
                   2129: 
                   2130: sub start_valuefunc {
                   2131:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2132: 
                   2133:     if ($target ne 'helper') {
                   2134:         return '';
                   2135:     }
                   2136: 
                   2137:     my $contents = Apache::lonxml::get_all_text('/valuefunc',
                   2138:                                                 $parser);
                   2139:     $contents = 'sub { my $res = shift; ' . $contents . '}';
                   2140:     $paramHash->{VALUE_FUNC} = eval $contents;
                   2141: }
                   2142: 
                   2143: sub end_valuefunc { return ''; }
                   2144: 
1.13      bowersj2 2145: sub start_mapurl {
                   2146:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2147: 
                   2148:     if ($target ne 'helper') {
                   2149:         return '';
                   2150:     }
                   2151: 
                   2152:     my $contents = Apache::lonxml::get_all_text('/mapurl',
                   2153:                                                 $parser);
1.48      bowersj2 2154:     $paramHash->{EVAL_MAP_URL} = $token->[2]{'evaluate'};
1.14      bowersj2 2155:     $paramHash->{MAP_URL} = $contents;
1.13      bowersj2 2156: }
                   2157: 
                   2158: sub end_mapurl { return ''; }
                   2159: 
1.90      foxr     2160: 
                   2161: sub start_option {
                   2162:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2163:     if (!defined($paramHash->{OPTION_TEXTS})) {
                   2164: 	$paramHash->{OPTION_TEXTS} = [ ];
                   2165: 	$paramHash->{OPTION_VARS}  = [ ];
1.91      foxr     2166: 
1.90      foxr     2167:     }
1.91      foxr     2168:     # OPTION_TEXTS is a list of the text attribute
                   2169:     #               values used to create column headings.
                   2170:     # OPTION_VARS is a list of the variable names, used to create the checkbox
                   2171:     #             inputs.
1.90      foxr     2172:     #  We're ok with empty elements. as place holders
                   2173:     # Although the 'variable' element should really exist.
1.91      foxr     2174:     #
                   2175: 
1.90      foxr     2176:     my $option_texts  = $paramHash->{OPTION_TEXTS};
                   2177:     my $option_vars   = $paramHash->{OPTION_VARS};
                   2178:     push(@$option_texts,  $token->[2]{'text'});
                   2179:     push(@$option_vars,   $token->[2]{'variable'});
                   2180: 
1.91      foxr     2181:     #  Need to create and declare the option variables as well to make them
                   2182:     # persistent.
                   2183:     #
                   2184:     my $varname = $token->[2]{'variable'};
                   2185:     $helper->declareVar($varname);
                   2186: 
                   2187: 
1.90      foxr     2188:     return '';
                   2189: }
                   2190: 
                   2191: sub end_option {
                   2192:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2193:     return '';
                   2194: }
                   2195: 
1.5       bowersj2 2196: # A note, in case I don't get to this before I leave.
                   2197: # If someone complains about the "Back" button returning them
                   2198: # to the previous folder state, instead of returning them to
                   2199: # the previous helper state, the *correct* answer is for the helper
                   2200: # to keep track of how many times the user has manipulated the folders,
                   2201: # and feed that to the history.go() call in the helper rendering routines.
                   2202: # If done correctly, the helper itself can keep track of how many times
                   2203: # it renders the same states, so it doesn't go in just this state, and
                   2204: # you can lean on the browser back button to make sure it all chains
                   2205: # correctly.
                   2206: # Right now, though, I'm just forcing all folders open.
                   2207: 
                   2208: sub render {
                   2209:     my $self = shift;
                   2210:     my $result = "";
                   2211:     my $var = $self->{'variable'};
                   2212:     my $curVal = $helper->{VARS}->{$var};
                   2213: 
1.15      bowersj2 2214:     my $buttons = '';
                   2215: 
                   2216:     if ($self->{'multichoice'}) {
                   2217:         $result = <<SCRIPT;
1.112     albertel 2218: <script type="text/javascript">
                   2219: // <!--
1.18      bowersj2 2220:     function checkall(value, checkName) {
1.15      bowersj2 2221: 	for (i=0; i<document.forms.helpform.elements.length; i++) {
                   2222:             ele = document.forms.helpform.elements[i];
1.157     raeburn  2223:             if (ele.name == checkName + '_forminput') {
1.15      bowersj2 2224:                 document.forms.helpform.elements[i].checked=value;
                   2225:             }
                   2226:         }
                   2227:     }
1.112     albertel 2228: // -->
1.15      bowersj2 2229: </script>
                   2230: SCRIPT
1.68      sakharuk 2231:         my %lt=&Apache::lonlocal::texthash(
                   2232: 			'sar'  => "Select All Resources",
                   2233: 		        'uar'  => "Unselect All Resources");
                   2234: 
1.15      bowersj2 2235:         $buttons = <<BUTTONS;
                   2236: <br /> &nbsp;
1.68      sakharuk 2237: <input type="button" onclick="checkall(true, '$var')" value="$lt{'sar'}" />
                   2238: <input type="button" onclick="checkall(false, '$var')" value="$lt{'uar'}" />
1.15      bowersj2 2239: <br /> &nbsp;
                   2240: BUTTONS
                   2241:     }
                   2242: 
1.5       bowersj2 2243:     if (defined $self->{ERROR_MSG}) {
1.14      bowersj2 2244:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
1.5       bowersj2 2245:     }
                   2246: 
1.15      bowersj2 2247:     $result .= $buttons;
                   2248: 
1.90      foxr     2249:     my $filterFunc     = $self->{FILTER_FUNC};
                   2250:     my $choiceFunc     = $self->{CHOICE_FUNC};
                   2251:     my $valueFunc      = $self->{VALUE_FUNC};
1.95      albertel 2252:     my $multichoice    = $self->{'multichoice'};
1.90      foxr     2253:     my $option_vars    = $self->{OPTION_VARS};
                   2254:     my $option_texts   = $self->{OPTION_TEXTS};
1.95      albertel 2255:     my $addparts       = $self->{'addparts'};
1.90      foxr     2256:     my $headings_done  = 0;
1.5       bowersj2 2257: 
1.48      bowersj2 2258:     # Evaluate the map url as needed
                   2259:     my $mapUrl;
1.49      bowersj2 2260:     if ($self->{EVAL_MAP_URL}) {
1.48      bowersj2 2261: 	my $mapUrlFunc = eval('sub { my $helper = shift; my $state = shift; ' . 
                   2262: 	    $self->{MAP_URL} . '}');
                   2263: 	$mapUrl = &$mapUrlFunc($helper, $self);
                   2264:     } else {
                   2265: 	$mapUrl = $self->{MAP_URL};
                   2266:     }
                   2267: 
1.125     albertel 2268:     my %defaultSymbs;
1.124     albertel 2269:     if (defined($self->{DEFAULT_VALUE})) {
                   2270:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   2271:         die 'Error in default value code for variable ' . 
                   2272:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
1.125     albertel 2273:         my @defaultSymbs = &$valueFunc($helper, $self);
                   2274: 	if (!$multichoice && @defaultSymbs) { # only allowed 1
1.124     albertel 2275: 	    @defaultSymbs = ($defaultSymbs[0]);
                   2276: 	}
1.125     albertel 2277: 	%defaultSymbs = map { if ($_) {($_,1) } } @defaultSymbs;
                   2278: 	delete($defaultSymbs{''});
1.124     albertel 2279:     }
                   2280: 
1.5       bowersj2 2281:     # Create the composite function that renders the column on the nav map
                   2282:     # have to admit any language that lets me do this can't be all bad
                   2283:     #  - Jeremy (Pythonista) ;-)
                   2284:     my $checked = 0;
                   2285:     my $renderColFunc = sub {
                   2286:         my ($resource, $part, $params) = @_;
1.90      foxr     2287: 	my $result = "";
                   2288: 
                   2289: 	if(!$headings_done) {
                   2290: 	    if ($option_texts) {
                   2291: 		foreach my $text (@$option_texts) {
                   2292: 		    $result .= "<th>$text</th>";
                   2293: 		}
                   2294: 	    }
                   2295: 	    $result .= "<th>Select</th>";
                   2296: 	    $result .= "</tr><tr>"; # Close off the extra row and start a new one.
                   2297: 	    $headings_done = 1;
                   2298: 	}
1.14      bowersj2 2299: 
                   2300:         my $inputType;
                   2301:         if ($multichoice) { $inputType = 'checkbox'; }
                   2302:         else {$inputType = 'radio'; }
                   2303: 
1.5       bowersj2 2304:         if (!&$choiceFunc($resource)) {
1.90      foxr     2305: 	    $result .= '<td>&nbsp;</td>';
                   2306:             return $result;
1.5       bowersj2 2307:         } else {
1.90      foxr     2308: 	    my $col = "";
1.98      foxr     2309: 	    my $raw_name = &$valueFunc($resource);
1.90      foxr     2310: 	    my $resource_name =   
1.98      foxr     2311:                    HTML::Entities::encode($raw_name,"<>&\"'");
1.90      foxr     2312: 	    if($option_vars) {
1.91      foxr     2313: 		foreach my $option_var (@$option_vars) {
1.99      foxr     2314: 		    my $var_value = "\|\|\|" . $helper->{VARS}->{$option_var} . 
                   2315: 			"\|\|\|";
1.98      foxr     2316: 		    my $checked ="";
1.99      foxr     2317: 		    if($var_value =~ /\Q|||$raw_name|||\E/) {
1.111     albertel 2318: 			$checked = "checked='checked'";
1.98      foxr     2319: 		    }
1.90      foxr     2320: 		    $col .= 
1.91      foxr     2321:                         "<td align='center'><input type='checkbox' name ='$option_var".
1.157     raeburn  2322: 			"_forminput' value='".
1.98      foxr     2323: 			$resource_name . "' $checked /> </td>";
1.90      foxr     2324: 		}
                   2325: 	    }
                   2326: 
1.157     raeburn  2327:             $col .= "<td align='center'><input type='$inputType' name='${var}_forminput' ";
1.125     albertel 2328: 	    if (%defaultSymbs) {
1.124     albertel 2329: 		my $symb=$resource->symb();
1.125     albertel 2330: 		if (exists($defaultSymbs{$symb})) {
1.124     albertel 2331: 		    $col .= "checked='checked' ";
                   2332: 		    $checked = 1;
                   2333: 		}
                   2334: 	    } else {
                   2335: 		if (!$checked && !$multichoice) {
                   2336: 		    $col .= "checked='checked' ";
                   2337: 		    $checked = 1;
                   2338: 		}
                   2339: 		if ($multichoice) { # all resources start checked; see bug 1174
                   2340: 		    $col .= "checked='checked' ";
                   2341: 		    $checked = 1;
                   2342: 		}
1.37      bowersj2 2343: 	    }
1.90      foxr     2344:             $col .= "value='" . $resource_name  . "' /></td>";
1.95      albertel 2345: 
1.90      foxr     2346:             return $result.$col;
1.5       bowersj2 2347:         }
                   2348:     };
1.95      albertel 2349:     my $renderPartsFunc = sub {
                   2350:         my ($resource, $part, $params) = @_;
                   2351: 	my $col= "<td>";
                   2352: 	my $id=$resource->{ID};
                   2353: 	my $resource_name =   
                   2354: 	    &HTML::Entities::encode(&$valueFunc($resource),"<>&\"'");
                   2355: 	if ($addparts && (scalar(@{$resource->parts}) > 1)) {
1.157     raeburn  2356: 	    $col .= "<select onclick=\"javascript:updateRadio(this.form,'${var}_forminput','$resource_name');updateHidden(this.form,'$id','${var}');\" name='part_${id}_forminput'>\n";
1.95      albertel 2357: 	    $col .= "<option value=\"$part\">All Parts</option>\n";
                   2358: 	    foreach my $part (@{$resource->parts}) {
                   2359: 		$col .= "<option value=\"$part\">Part: $part</option>\n";
                   2360: 	    }
                   2361: 	    $col .= "</select>";
                   2362: 	}
                   2363: 	$col .= "</td>";
                   2364:     };
                   2365:     $result.=(<<RADIO);
                   2366: <script type="text/javascript">
1.112     albertel 2367: // <!--
1.95      albertel 2368:     function updateRadio(form,name,value) {
                   2369: 	var radiobutton=form[name];
                   2370: 	for (var i=0; i<radiobutton.length; i++) {
                   2371: 	    if (radiobutton[i].value == value) {
                   2372: 		radiobutton[i].checked = true;
                   2373: 		break;
                   2374: 	    }
                   2375: 	}
                   2376:     }
                   2377:     function updateHidden(form,id,name) {
1.157     raeburn  2378: 	var select=form['part_'+id+'_forminput'];
                   2379: 	var hidden=form[name+'_part_forminput'];
1.95      albertel 2380: 	var which=select.selectedIndex;
                   2381: 	hidden.value=select.options[which].value;
                   2382:     }
1.112     albertel 2383: // -->
1.95      albertel 2384: </script>
1.157     raeburn  2385: <input type="hidden" name="${var}_part_forminput" />
1.5       bowersj2 2386: 
1.95      albertel 2387: RADIO
1.100     albertel 2388:     $env{'form.condition'} = !$self->{'toponly'};
1.95      albertel 2389:     my $cols = [$renderColFunc];
                   2390:     if ($self->{'addparts'}) { push(@$cols, $renderPartsFunc); }
                   2391:     push(@$cols, Apache::lonnavmaps::resource());
1.46      bowersj2 2392:     if ($self->{'addstatus'}) {
                   2393: 	push @$cols, (Apache::lonnavmaps::part_status_summary());
                   2394: 	
                   2395:     }
1.5       bowersj2 2396:     $result .= 
1.46      bowersj2 2397:         &Apache::lonnavmaps::render( { 'cols' => $cols,
1.5       bowersj2 2398:                                        'showParts' => 0,
                   2399:                                        'filterFunc' => $filterFunc,
1.13      bowersj2 2400:                                        'resource_no_folder_link' => 1,
1.66      albertel 2401: 				       'closeAllPages' => $self->{'closeallpages'},
1.29      bowersj2 2402:                                        'suppressEmptySequences' => $self->{'suppressEmptySequences'},
1.163   ! albertel 2403: 				       'include_top_level_map' => $self->{'include_top_level_map'},
1.13      bowersj2 2404:                                        'iterator_map' => $mapUrl }
1.5       bowersj2 2405:                                        );
1.15      bowersj2 2406: 
                   2407:     $result .= $buttons;
1.5       bowersj2 2408:                                                 
                   2409:     return $result;
                   2410: }
                   2411:     
                   2412: sub postprocess {
                   2413:     my $self = shift;
1.14      bowersj2 2414: 
                   2415:     if ($self->{'multichoice'} && !$helper->{VARS}->{$self->{'variable'}}) {
                   2416:         $self->{ERROR_MSG} = 'You must choose at least one resource to continue.';
                   2417:         return 0;
                   2418:     }
                   2419: 
1.5       bowersj2 2420:     if (defined($self->{NEXTSTATE})) {
                   2421:         $helper->changeState($self->{NEXTSTATE});
                   2422:     }
1.6       bowersj2 2423: 
                   2424:     return 1;
1.5       bowersj2 2425: }
                   2426: 
                   2427: 1;
                   2428: 
                   2429: package Apache::lonhelper::student;
                   2430: 
                   2431: =pod
                   2432: 
1.44      bowersj2 2433: =head2 Element: studentX<student, helper element>
1.5       bowersj2 2434: 
                   2435: Student elements display a choice of students enrolled in the current
                   2436: course. Currently it is primitive; this is expected to evolve later.
                   2437: 
1.48      bowersj2 2438: Student elements take the following attributes: 
                   2439: 
                   2440: =over 4
                   2441: 
                   2442: =item * B<variable>: 
                   2443: 
                   2444: Does what it usually does: declare which helper variable to put the
                   2445: result in.
                   2446: 
                   2447: =item * B<multichoice>: 
                   2448: 
                   2449: If true allows the user to select multiple students. Defaults to false.
                   2450: 
                   2451: =item * B<coursepersonnel>: 
                   2452: 
                   2453: If true adds the course personnel to the top of the student
                   2454: selection. Defaults to false.
                   2455: 
                   2456: =item * B<activeonly>:
                   2457: 
                   2458: If true, only active students and course personnel will be
                   2459: shown. Defaults to false.
                   2460: 
1.123     albertel 2461: =item * B<emptyallowed>:
                   2462: 
                   2463: If true, the selection of no users is allowed. Defaults to false.
                   2464: 
1.48      bowersj2 2465: =back
1.5       bowersj2 2466: 
                   2467: =cut
                   2468: 
                   2469: no strict;
                   2470: @ISA = ("Apache::lonhelper::element");
                   2471: use strict;
1.59      bowersj2 2472: use Apache::lonlocal;
1.100     albertel 2473: use Apache::lonnet;
1.139     foxr     2474: 
1.5       bowersj2 2475: BEGIN {
1.7       bowersj2 2476:     &Apache::lonhelper::register('Apache::lonhelper::student',
1.5       bowersj2 2477:                               ('student'));
                   2478: }
                   2479: 
                   2480: sub new {
                   2481:     my $ref = Apache::lonhelper::element->new();
                   2482:     bless($ref);
                   2483: }
1.4       bowersj2 2484: 
1.5       bowersj2 2485: sub start_student {
1.4       bowersj2 2486:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2487: 
                   2488:     if ($target ne 'helper') {
                   2489:         return '';
                   2490:     }
                   2491: 
1.5       bowersj2 2492:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   2493:     $helper->declareVar($paramHash->{'variable'});
                   2494:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.39      bowersj2 2495:     $paramHash->{'coursepersonnel'} = $token->[2]{'coursepersonnel'};
1.93      albertel 2496:     $paramHash->{'activeonly'} = $token->[2]{'activeonly'};
1.12      bowersj2 2497:     if (defined($token->[2]{'nextstate'})) {
                   2498:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
                   2499:     }
1.123     albertel 2500:     $paramHash->{'emptyallowed'} = $token->[2]{'emptyallowed'};
1.12      bowersj2 2501:     
1.5       bowersj2 2502: }    
                   2503: 
                   2504: sub end_student {
                   2505:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2506: 
                   2507:     if ($target ne 'helper') {
                   2508:         return '';
                   2509:     }
                   2510:     Apache::lonhelper::student->new();
1.3       bowersj2 2511: }
1.5       bowersj2 2512: 
                   2513: sub render {
                   2514:     my $self = shift;
                   2515:     my $result = '';
                   2516:     my $buttons = '';
1.18      bowersj2 2517:     my $var = $self->{'variable'};
1.5       bowersj2 2518: 
                   2519: 
                   2520:     if (defined $self->{ERROR_MSG}) {
                   2521:         $result .= '<font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                   2522:     }
                   2523: 
1.126     albertel 2524:     my %defaultUsers;
                   2525:     if (defined($self->{DEFAULT_VALUE})) {
                   2526:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   2527:         die 'Error in default value code for variable ' . 
                   2528:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
                   2529:         my @defaultUsers = &$valueFunc($helper, $self);
                   2530: 	if (!$self->{'multichoice'} && @defaultUsers) { # only allowed 1
                   2531: 	    @defaultUsers = ($defaultUsers[0]);
                   2532: 	}
                   2533: 	%defaultUsers = map { if ($_) {($_,1) } } @defaultUsers;
                   2534: 	delete($defaultUsers{''});
                   2535:     }
1.139     foxr     2536: 
                   2537: 
1.147     foxr     2538:     my ($course_personnel, 
                   2539: 	$current_members, 
                   2540: 	$expired_members, 
1.153     foxr     2541: 	$future_members) = 
                   2542: 	    &Apache::lonselstudent::get_people_in_class($env{'request.course.sec'});
1.139     foxr     2543: 
                   2544: 
1.39      bowersj2 2545: 
                   2546:     # Load up the non-students, if necessary
1.147     foxr     2547: 
1.39      bowersj2 2548:     if ($self->{'coursepersonnel'}) {
1.147     foxr     2549: 	unshift @$current_members, (@$course_personnel);
1.39      bowersj2 2550:     }
1.5       bowersj2 2551: 
                   2552: 
1.139     foxr     2553:     #   Current personel
                   2554: 
1.158     albertel 2555:     $result .= '<h4>'.&mt('Select Currently Enrolled Students and Active Course Personnel').'</h4>';
1.148     foxr     2556:     $result .= &Apache::lonselstudent::render_student_list( $current_members,
1.149     foxr     2557: 							    "helpform",
                   2558: 							    "current", 
                   2559: 							    \%defaultUsers,
                   2560: 							    $self->{'multichoice'},
                   2561: 							    $self->{'variable'},
                   2562: 							    1);
1.139     foxr     2563: 
1.132     foxr     2564: 
1.139     foxr     2565:     # If activeonly is not set then we can also give the expired students:
                   2566:     #
1.158     albertel 2567:     if (!$self->{'activeonly'} && ((scalar(@$future_members)) > 0)) {
1.132     foxr     2568: 
1.140     albertel 2569: 	# And future.
                   2570: 
1.158     albertel 2571: 	$result .= '<h4>'.&mt('Select Future Enrolled Students and Future Course Personnel').'</h4>';
1.156     foxr     2572:        
1.148     foxr     2573: 	$result .= &Apache::lonselstudent::render_student_list( $future_members,
1.149     foxr     2574: 								"helpform",
                   2575: 								"future",
                   2576: 								\%defaultUsers,
                   2577: 								$self->{'multichoice'},
                   2578: 								$self->{'variable'},
                   2579: 								0);
1.158     albertel 2580:     }
                   2581:     if (!$self->{'activeonly'} && ((scalar(@$expired_members)) > 0)) {
1.139     foxr     2582: 	# Past 
1.39      bowersj2 2583: 
1.158     albertel 2584: 	$result .= '<h4>'.&mt('Select Previously Enrolled Students and Inactive Course Personnel').'</h4>';
1.148     foxr     2585: 	$result .= &Apache::lonselstudent::render_student_list($expired_members,
1.149     foxr     2586: 							       "helpform",
                   2587: 							       "past",
                   2588: 							       \%defaultUsers,
                   2589: 							       $self->{'multichoice'},
                   2590: 							       $self->{'variable'},
                   2591: 							       0);
1.132     foxr     2592:     }
1.5       bowersj2 2593: 
1.113     foxr     2594: 
                   2595: 
1.5       bowersj2 2596:     return $result;
                   2597: }
                   2598: 
1.6       bowersj2 2599: sub postprocess {
                   2600:     my $self = shift;
                   2601: 
1.157     raeburn  2602:     my $result = $env{'form.' . $self->{'variable'} . '_forminput'};
1.123     albertel 2603:     if (!$result && !$self->{'emptyallowed'}) {
                   2604: 	if ($self->{'coursepersonnel'}) {
                   2605: 	    $self->{ERROR_MSG} = 
                   2606: 		&mt('You must choose at least one user to continue.');
                   2607: 	} else {
                   2608: 	    $self->{ERROR_MSG} = 
                   2609: 		&mt('You must choose at least one student to continue.');
                   2610: 	}
1.6       bowersj2 2611:         return 0;
                   2612:     }
                   2613: 
                   2614:     if (defined($self->{NEXTSTATE})) {
                   2615:         $helper->changeState($self->{NEXTSTATE});
                   2616:     }
                   2617: 
                   2618:     return 1;
                   2619: }
                   2620: 
1.5       bowersj2 2621: 1;
                   2622: 
                   2623: package Apache::lonhelper::files;
                   2624: 
                   2625: =pod
                   2626: 
1.44      bowersj2 2627: =head2 Element: filesX<files, helper element>
1.5       bowersj2 2628: 
                   2629: files allows the users to choose files from a given directory on the
                   2630: server. It is always multichoice and stores the result as a triple-pipe
                   2631: delimited entry in the helper variables. 
                   2632: 
                   2633: Since it is extremely unlikely that you can actually code a constant
                   2634: representing the directory you wish to allow the user to search, <files>
                   2635: takes a subroutine that returns the name of the directory you wish to
                   2636: have the user browse.
                   2637: 
                   2638: files accepts the attribute "variable" to control where the files chosen
                   2639: are put. It accepts the attribute "multichoice" as the other attribute,
                   2640: defaulting to false, which if true will allow the user to select more
                   2641: then one choice. 
                   2642: 
1.44      bowersj2 2643: <files> accepts three subtags: 
                   2644: 
                   2645: =over 4
                   2646: 
                   2647: =item * B<nextstate>: works as it does with the other tags. 
                   2648: 
                   2649: =item * B<filechoice>: When the contents of this tag are surrounded by
                   2650:     "sub {" and "}", will return a string representing what directory
                   2651:     on the server to allow the user to choose files from. 
                   2652: 
                   2653: =item * B<filefilter>: Should contain Perl code that when surrounded
                   2654:     by "sub { my $filename = shift; " and "}", returns a true value if
                   2655:     the user can pick that file, or false otherwise. The filename
                   2656:     passed to the function will be just the name of the file, with no
                   2657:     path info. By default, a filter function will be used that will
                   2658:     mask out old versions of files. This function is available as
                   2659:     Apache::lonhelper::files::not_old_version if you want to use it to
                   2660:     composite your own filters.
                   2661: 
                   2662: =back
                   2663: 
                   2664: B<General security note>: You should ensure the user can not somehow 
                   2665: pass something into your code that would allow them to look places 
                   2666: they should not be able to see, like the C</etc/> directory. However,
                   2667: the security impact would be minimal, since it would only expose
                   2668: the existence of files, there should be no way to parlay that into
                   2669: viewing the files. 
1.5       bowersj2 2670: 
                   2671: =cut
                   2672: 
                   2673: no strict;
                   2674: @ISA = ("Apache::lonhelper::element");
                   2675: use strict;
1.59      bowersj2 2676: use Apache::lonlocal;
1.100     albertel 2677: use Apache::lonnet;
1.32      bowersj2 2678: use Apache::lonpubdir; # for getTitleString
                   2679: 
1.5       bowersj2 2680: BEGIN {
1.7       bowersj2 2681:     &Apache::lonhelper::register('Apache::lonhelper::files',
                   2682:                                  ('files', 'filechoice', 'filefilter'));
1.5       bowersj2 2683: }
                   2684: 
1.44      bowersj2 2685: sub not_old_version {
                   2686:     my $file = shift;
                   2687:     
                   2688:     # Given a file name, return false if it is an "old version" of a
                   2689:     # file, or true if it is not.
                   2690: 
                   2691:     if ($file =~ /^.*\.[0-9]+\.[A-Za-z]+(\.meta)?$/) {
                   2692: 	return 0;
                   2693:     }
                   2694:     return 1;
                   2695: }
                   2696: 
1.5       bowersj2 2697: sub new {
                   2698:     my $ref = Apache::lonhelper::element->new();
                   2699:     bless($ref);
                   2700: }
                   2701: 
                   2702: sub start_files {
                   2703:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2704: 
                   2705:     if ($target ne 'helper') {
                   2706:         return '';
                   2707:     }
                   2708:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   2709:     $helper->declareVar($paramHash->{'variable'});
                   2710:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
                   2711: }    
                   2712: 
                   2713: sub end_files {
                   2714:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2715: 
                   2716:     if ($target ne 'helper') {
                   2717:         return '';
                   2718:     }
                   2719:     if (!defined($paramHash->{FILTER_FUNC})) {
                   2720:         $paramHash->{FILTER_FUNC} = sub { return 1; };
                   2721:     }
                   2722:     Apache::lonhelper::files->new();
                   2723: }    
                   2724: 
                   2725: sub start_filechoice {
                   2726:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2727: 
                   2728:     if ($target ne 'helper') {
                   2729:         return '';
                   2730:     }
                   2731:     $paramHash->{'filechoice'} = Apache::lonxml::get_all_text('/filechoice',
                   2732:                                                               $parser);
                   2733: }
                   2734: 
                   2735: sub end_filechoice { return ''; }
                   2736: 
                   2737: sub start_filefilter {
                   2738:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2739: 
                   2740:     if ($target ne 'helper') {
                   2741:         return '';
                   2742:     }
                   2743: 
                   2744:     my $contents = Apache::lonxml::get_all_text('/filefilter',
                   2745:                                                 $parser);
                   2746:     $contents = 'sub { my $filename = shift; ' . $contents . '}';
                   2747:     $paramHash->{FILTER_FUNC} = eval $contents;
                   2748: }
                   2749: 
                   2750: sub end_filefilter { return ''; }
1.3       bowersj2 2751: 
1.87      matthew  2752: { 
                   2753:     # used to generate unique id attributes for <input> tags. 
                   2754:     # internal use only.
                   2755:     my $id=0;
                   2756:     sub new_id { return $id++;}
                   2757: }
                   2758: 
1.3       bowersj2 2759: sub render {
                   2760:     my $self = shift;
1.5       bowersj2 2761:     my $result = '';
                   2762:     my $var = $self->{'variable'};
                   2763:     
                   2764:     my $subdirFunc = eval('sub {' . $self->{'filechoice'} . '}');
1.11      bowersj2 2765:     die 'Error in resource filter code for variable ' . 
                   2766:         {'variable'} . ', Perl said:' . $@ if $@;
                   2767: 
1.5       bowersj2 2768:     my $subdir = &$subdirFunc();
                   2769: 
                   2770:     my $filterFunc = $self->{FILTER_FUNC};
1.44      bowersj2 2771:     if (!defined($filterFunc)) {
                   2772: 	$filterFunc = &not_old_version;
                   2773:     }
1.5       bowersj2 2774:     my $buttons = '';
1.22      bowersj2 2775:     my $type = 'radio';
                   2776:     if ($self->{'multichoice'}) {
                   2777:         $type = 'checkbox';
                   2778:     }
1.5       bowersj2 2779: 
                   2780:     if ($self->{'multichoice'}) {
                   2781:         $result = <<SCRIPT;
1.112     albertel 2782: <script type="text/javascript">
                   2783: // <!--
1.18      bowersj2 2784:     function checkall(value, checkName) {
1.15      bowersj2 2785: 	for (i=0; i<document.forms.helpform.elements.length; i++) {
                   2786:             ele = document.forms.helpform.elements[i];
1.157     raeburn  2787:             if (ele.name == checkName + '_forminput') {
1.15      bowersj2 2788:                 document.forms.helpform.elements[i].checked=value;
1.5       bowersj2 2789:             }
                   2790:         }
                   2791:     }
1.21      bowersj2 2792: 
1.22      bowersj2 2793:     function checkallclass(value, className) {
1.21      bowersj2 2794:         for (i=0; i<document.forms.helpform.elements.length; i++) {
                   2795:             ele = document.forms.helpform.elements[i];
1.22      bowersj2 2796:             if (ele.type == "$type" && ele.onclick) {
1.21      bowersj2 2797:                 document.forms.helpform.elements[i].checked=value;
                   2798:             }
                   2799:         }
                   2800:     }
1.112     albertel 2801: // -->
1.5       bowersj2 2802: </script>
                   2803: SCRIPT
1.68      sakharuk 2804:        my %lt=&Apache::lonlocal::texthash(
                   2805: 			'saf'  => "Select All Files",
                   2806: 		        'uaf'  => "Unselect All Files");
                   2807:        $buttons = <<BUTTONS;
1.5       bowersj2 2808: <br /> &nbsp;
1.68      sakharuk 2809: <input type="button" onclick="checkall(true, '$var')" value="$lt{'saf'}" />
                   2810: <input type="button" onclick="checkall(false, '$var')" value="$lt{'uaf'}" />
1.23      bowersj2 2811: BUTTONS
                   2812: 
1.69      sakharuk 2813:        %lt=&Apache::lonlocal::texthash(
1.68      sakharuk 2814: 			'sap'  => "Select All Published",
                   2815: 		        'uap'  => "Unselect All Published");
1.23      bowersj2 2816:         if ($helper->{VARS}->{'construction'}) {
1.68      sakharuk 2817:        $buttons .= <<BUTTONS;
                   2818: <input type="button" onclick="checkallclass(true, 'Published')" value="$lt{'sap'}" />
                   2819: <input type="button" onclick="checkallclass(false, 'Published')" value="$lt{'uap'}" />
1.5       bowersj2 2820: <br /> &nbsp;
                   2821: BUTTONS
1.23      bowersj2 2822:        }
1.5       bowersj2 2823:     }
                   2824: 
                   2825:     # Get the list of files in this directory.
                   2826:     my @fileList;
                   2827: 
                   2828:     # If the subdirectory is in local CSTR space
1.47      albertel 2829:     my $metadir;
                   2830:     if ($subdir =~ m|/home/([^/]+)/public_html/(.*)|) {
1.110     albertel 2831: 	my ($user,$domain)= 
                   2832: 	    &Apache::loncacc::constructaccess($subdir,
                   2833: 				     $Apache::lonnet::perlvar{'lonDefDomain'});
1.47      albertel 2834: 	$metadir='/res/'.$domain.'/'.$user.'/'.$2;
                   2835:         @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
                   2836:     } elsif ($subdir =~ m|^~([^/]+)/(.*)$|) {
                   2837: 	$subdir='/home/'.$1.'/public_html/'.$2;
1.110     albertel 2838: 	my ($user,$domain)= 
                   2839: 	    &Apache::loncacc::constructaccess($subdir,
                   2840: 				     $Apache::lonnet::perlvar{'lonDefDomain'});
1.47      albertel 2841: 	$metadir='/res/'.$domain.'/'.$user.'/'.$2;
1.5       bowersj2 2842:         @fileList = &Apache::lonnet::dirlist($subdir, $domain, $user, '');
                   2843:     } else {
                   2844:         # local library server resource space
1.100     albertel 2845:         @fileList = &Apache::lonnet::dirlist($subdir, $env{'user.domain'}, $env{'user.name'}, '');
1.5       bowersj2 2846:     }
1.3       bowersj2 2847: 
1.44      bowersj2 2848:     # Sort the fileList into order
1.85      albertel 2849:     @fileList = sort {lc($a) cmp lc($b)} @fileList;
1.44      bowersj2 2850: 
1.5       bowersj2 2851:     $result .= $buttons;
                   2852: 
1.6       bowersj2 2853:     if (defined $self->{ERROR_MSG}) {
                   2854:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
                   2855:     }
                   2856: 
1.20      bowersj2 2857:     $result .= '<table border="0" cellpadding="2" cellspacing="0">';
1.5       bowersj2 2858: 
                   2859:     # Keeps track if there are no choices, prints appropriate error
                   2860:     # if there are none. 
                   2861:     my $choices = 0;
                   2862:     # Print each legitimate file choice.
                   2863:     for my $file (@fileList) {
                   2864:         $file = (split(/&/, $file))[0];
                   2865:         if ($file eq '.' || $file eq '..') {
                   2866:             next;
                   2867:         }
                   2868:         my $fileName = $subdir .'/'. $file;
                   2869:         if (&$filterFunc($file)) {
1.24      sakharuk 2870: 	    my $status;
                   2871: 	    my $color;
                   2872: 	    if ($helper->{VARS}->{'construction'}) {
                   2873: 		($status, $color) = @{fileState($subdir, $file)};
                   2874: 	    } else {
                   2875: 		$status = '';
                   2876: 		$color = '';
                   2877: 	    }
1.22      bowersj2 2878: 
1.32      bowersj2 2879:             # Get the title
1.47      albertel 2880:             my $title = Apache::lonpubdir::getTitleString(($metadir?$metadir:$subdir) .'/'. $file);
1.32      bowersj2 2881: 
1.22      bowersj2 2882:             # Netscape 4 is stupid and there's nowhere to put the
                   2883:             # information on the input tag that the file is Published,
                   2884:             # Unpublished, etc. In *real* browsers we can just say
                   2885:             # "class='Published'" and check the className attribute of
                   2886:             # the input tag, but Netscape 4 is too stupid to understand
                   2887:             # that attribute, and un-comprehended attributes are not
                   2888:             # reflected into the object model. So instead, what I do 
                   2889:             # is either have or don't have an "onclick" handler that 
                   2890:             # does nothing, give Published files the onclick handler, and
                   2891:             # have the checker scripts check for that. Stupid and clumsy,
                   2892:             # and only gives us binary "yes/no" information (at least I
                   2893:             # couldn't figure out how to reach into the event handler's
                   2894:             # actual code to retreive a value), but it works well enough
                   2895:             # here.
1.23      bowersj2 2896:         
1.22      bowersj2 2897:             my $onclick = '';
1.23      bowersj2 2898:             if ($status eq 'Published' && $helper->{VARS}->{'construction'}) {
1.22      bowersj2 2899:                 $onclick = 'onclick="a=1" ';
                   2900:             }
1.87      matthew  2901:             my $id = &new_id();
1.20      bowersj2 2902:             $result .= '<tr><td align="right"' . " bgcolor='$color'>" .
1.22      bowersj2 2903:                 "<input $onclick type='$type' name='" . $var
1.157     raeburn  2904:             . "_forminput' ".qq{id="$id"}." value='" . HTML::Entities::encode($fileName,"<>&\"'").
1.5       bowersj2 2905:                 "'";
                   2906:             if (!$self->{'multichoice'} && $choices == 0) {
1.111     albertel 2907:                 $result .= ' checked="checked"';
1.5       bowersj2 2908:             }
1.87      matthew  2909:             $result .= "/></td><td bgcolor='$color'>".
                   2910:                 qq{<label for="$id">}. $file . "</label></td>" .
1.32      bowersj2 2911:                 "<td bgcolor='$color'>$title</td>" .
                   2912:                 "<td bgcolor='$color'>$status</td>" . "</tr>\n";
1.5       bowersj2 2913:             $choices++;
                   2914:         }
                   2915:     }
                   2916: 
                   2917:     $result .= "</table>\n";
                   2918: 
                   2919:     if (!$choices) {
1.47      albertel 2920:         $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 2921:     }
                   2922: 
                   2923:     $result .= $buttons;
                   2924: 
                   2925:     return $result;
1.20      bowersj2 2926: }
                   2927: 
                   2928: # Determine the state of the file: Published, unpublished, modified.
                   2929: # Return the color it should be in and a label as a two-element array
                   2930: # reference.
                   2931: # Logic lifted from lonpubdir.pm, even though I don't know that it's still
                   2932: # the most right thing to do.
                   2933: 
                   2934: sub fileState {
                   2935:     my $constructionSpaceDir = shift;
                   2936:     my $file = shift;
                   2937:     
1.100     albertel 2938:     my ($uname,$udom)=($env{'user.name'},$env{'user.domain'});
                   2939:     if ($env{'request.role'}=~/^ca\./) {
                   2940: 	(undef,$udom,$uname)=split(/\//,$env{'request.role'});
1.86      albertel 2941:     }
1.20      bowersj2 2942:     my $docroot = $Apache::lonnet::perlvar{'lonDocRoot'};
                   2943:     my $subdirpart = $constructionSpaceDir;
1.86      albertel 2944:     $subdirpart =~ s/^\/home\/$uname\/public_html//;
                   2945:     my $resdir = $docroot . '/res/' . $udom . '/' . $uname .
1.20      bowersj2 2946:         $subdirpart;
                   2947: 
                   2948:     my @constructionSpaceFileStat = stat($constructionSpaceDir . '/' . $file);
                   2949:     my @resourceSpaceFileStat = stat($resdir . '/' . $file);
                   2950:     if (!@resourceSpaceFileStat) {
                   2951:         return ['Unpublished', '#FFCCCC'];
                   2952:     }
                   2953: 
                   2954:     my $constructionSpaceFileModified = $constructionSpaceFileStat[9];
                   2955:     my $resourceSpaceFileModified = $resourceSpaceFileStat[9];
                   2956:     
                   2957:     if ($constructionSpaceFileModified > $resourceSpaceFileModified) {
                   2958:         return ['Modified', '#FFFFCC'];
                   2959:     }
                   2960:     return ['Published', '#CCFFCC'];
1.4       bowersj2 2961: }
1.5       bowersj2 2962: 
1.4       bowersj2 2963: sub postprocess {
                   2964:     my $self = shift;
1.157     raeburn  2965:     my $result = $env{'form.' . $self->{'variable'} . '_forminput'};
1.6       bowersj2 2966:     if (!$result) {
                   2967:         $self->{ERROR_MSG} = 'You must choose at least one file '.
                   2968:             'to continue.';
                   2969:         return 0;
                   2970:     }
                   2971: 
1.5       bowersj2 2972:     if (defined($self->{NEXTSTATE})) {
                   2973:         $helper->changeState($self->{NEXTSTATE});
1.3       bowersj2 2974:     }
1.6       bowersj2 2975: 
                   2976:     return 1;
1.3       bowersj2 2977: }
1.8       bowersj2 2978: 
                   2979: 1;
                   2980: 
1.11      bowersj2 2981: package Apache::lonhelper::section;
                   2982: 
                   2983: =pod
                   2984: 
1.44      bowersj2 2985: =head2 Element: sectionX<section, helper element>
1.11      bowersj2 2986: 
                   2987: <section> allows the user to choose one or more sections from the current
                   2988: course.
                   2989: 
1.134     albertel 2990: It takes the standard attributes "variable", "multichoice",
                   2991: "allowempty" and "nextstate", meaning what they do for most other
                   2992: elements.
                   2993: 
                   2994: also takes a boolean 'onlysections' whcih will restrict this to only
                   2995: have sections and not include groups
1.11      bowersj2 2996: 
                   2997: =cut
                   2998: 
                   2999: no strict;
                   3000: @ISA = ("Apache::lonhelper::choices");
                   3001: use strict;
                   3002: 
                   3003: BEGIN {
                   3004:     &Apache::lonhelper::register('Apache::lonhelper::section',
                   3005:                                  ('section'));
                   3006: }
                   3007: 
                   3008: sub new {
                   3009:     my $ref = Apache::lonhelper::choices->new();
                   3010:     bless($ref);
                   3011: }
                   3012: 
                   3013: sub start_section {
                   3014:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3015: 
                   3016:     if ($target ne 'helper') {
                   3017:         return '';
                   3018:     }
1.12      bowersj2 3019: 
                   3020:     $paramHash->{CHOICES} = [];
                   3021: 
1.11      bowersj2 3022:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   3023:     $helper->declareVar($paramHash->{'variable'});
                   3024:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.133     albertel 3025:     $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.11      bowersj2 3026:     if (defined($token->[2]{'nextstate'})) {
1.12      bowersj2 3027:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
1.11      bowersj2 3028:     }
                   3029: 
                   3030:     # Populate the CHOICES element
                   3031:     my %choices;
                   3032: 
                   3033:     my $section = Apache::loncoursedata::CL_SECTION();
                   3034:     my $classlist = Apache::loncoursedata::get_classlist();
1.143     albertel 3035:     foreach my $user (keys(%$classlist)) {
                   3036:         my $section_name = $classlist->{$user}[$section];
                   3037:         if (!$section_name) {
1.11      bowersj2 3038:             $choices{"No section assigned"} = "";
                   3039:         } else {
1.143     albertel 3040:             $choices{$section_name} = $section_name;
1.11      bowersj2 3041:         }
1.12      bowersj2 3042:     } 
                   3043:    
1.143     albertel 3044:     if (exists($choices{"No section assigned"})) {
                   3045: 	push(@{$paramHash->{CHOICES}}, 
                   3046: 	     ['No section assigned','No section assigned']);
                   3047: 	delete($choices{"No section assigned"});
                   3048:     }
                   3049:     for my $section_name (sort {lc($a) cmp lc($b) } (keys(%choices))) {
                   3050: 	push @{$paramHash->{CHOICES}}, [$section_name, $section_name];
1.134     albertel 3051:     }
                   3052:     return if ($token->[2]{'onlysections'});
                   3053: 
                   3054:     # add in groups to the end of the list
1.151     raeburn  3055:     my %curr_groups = &Apache::longroup::coursegroups();
1.142     albertel 3056:     foreach my $group_name (sort(keys(%curr_groups))) {
                   3057: 	push(@{$paramHash->{CHOICES}}, [$group_name, $group_name]);
1.11      bowersj2 3058:     }
                   3059: }    
                   3060: 
1.12      bowersj2 3061: sub end_section {
                   3062:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.11      bowersj2 3063: 
1.12      bowersj2 3064:     if ($target ne 'helper') {
                   3065:         return '';
                   3066:     }
                   3067:     Apache::lonhelper::section->new();
                   3068: }    
1.11      bowersj2 3069: 1;
                   3070: 
1.128     raeburn  3071: package Apache::lonhelper::group;
                   3072: 
                   3073: =pod
                   3074:  
                   3075: =head2 Element: groupX<group, helper element>
                   3076:  
1.134     albertel 3077: <group> allows the user to choose one or more groups from the current course.
                   3078: 
                   3079: It takes the standard attributes "variable", "multichoice",
                   3080:  "allowempty" and "nextstate", meaning what they do for most other
                   3081:  elements.
1.128     raeburn  3082: 
                   3083: =cut
                   3084: 
                   3085: no strict;
                   3086: @ISA = ("Apache::lonhelper::choices");
                   3087: use strict;
                   3088: 
                   3089: BEGIN {
                   3090:     &Apache::lonhelper::register('Apache::lonhelper::group',
                   3091:                                  ('group'));
                   3092: }
                   3093: 
                   3094: sub new {
                   3095:     my $ref = Apache::lonhelper::choices->new();
                   3096:     bless($ref);
                   3097: }
                   3098:  
                   3099: sub start_group {
                   3100:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3101:  
                   3102:     if ($target ne 'helper') {
                   3103:         return '';
                   3104:     }
                   3105: 
                   3106:     $paramHash->{CHOICES} = [];
                   3107: 
                   3108:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   3109:     $helper->declareVar($paramHash->{'variable'});
                   3110:     $paramHash->{'multichoice'} = $token->[2]{'multichoice'};
1.133     albertel 3111:     $paramHash->{'allowempty'} = $token->[2]{'allowempty'};
1.128     raeburn  3112:     if (defined($token->[2]{'nextstate'})) {
                   3113:         $paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
                   3114:     }
                   3115: 
                   3116:     # Populate the CHOICES element
                   3117:     my %choices;
                   3118: 
1.151     raeburn  3119:     my %curr_groups = &Apache::longroup::coursegroups();
1.143     albertel 3120:     foreach my $group_name (sort {lc($a) cmp lc($b)} (keys(%curr_groups))) {
1.142     albertel 3121: 	push(@{$paramHash->{CHOICES}}, [$group_name, $group_name]);
1.128     raeburn  3122:     }
                   3123: }
1.134     albertel 3124: 
1.128     raeburn  3125: sub end_group {
                   3126:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3127: 
                   3128:     if ($target ne 'helper') {
                   3129:         return '';
                   3130:     }
                   3131:     Apache::lonhelper::group->new();
                   3132: }
                   3133: 1;
                   3134: 
1.34      bowersj2 3135: package Apache::lonhelper::string;
                   3136: 
                   3137: =pod
                   3138: 
1.44      bowersj2 3139: =head2 Element: stringX<string, helper element>
1.34      bowersj2 3140: 
                   3141: string elements provide a string entry field for the user. string elements
                   3142: take the usual 'variable' and 'nextstate' parameters. string elements
                   3143: also pass through 'maxlength' and 'size' attributes to the input tag.
                   3144: 
                   3145: string honors the defaultvalue tag, if given.
                   3146: 
1.38      bowersj2 3147: string honors the validation function, if given.
                   3148: 
1.34      bowersj2 3149: =cut
                   3150: 
                   3151: no strict;
                   3152: @ISA = ("Apache::lonhelper::element");
                   3153: use strict;
1.76      sakharuk 3154: use Apache::lonlocal;
1.34      bowersj2 3155: 
                   3156: BEGIN {
                   3157:     &Apache::lonhelper::register('Apache::lonhelper::string',
                   3158:                               ('string'));
                   3159: }
                   3160: 
                   3161: sub new {
                   3162:     my $ref = Apache::lonhelper::element->new();
                   3163:     bless($ref);
                   3164: }
                   3165: 
                   3166: # CONSTRUCTION: Construct the message element from the XML
                   3167: sub start_string {
                   3168:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3169: 
                   3170:     if ($target ne 'helper') {
                   3171:         return '';
                   3172:     }
                   3173: 
                   3174:     $paramHash->{'variable'} = $token->[2]{'variable'};
                   3175:     $helper->declareVar($paramHash->{'variable'});
                   3176:     $paramHash->{'nextstate'} = $token->[2]{'nextstate'};
                   3177:     $paramHash->{'maxlength'} = $token->[2]{'maxlength'};
                   3178:     $paramHash->{'size'} = $token->[2]{'size'};
                   3179: 
                   3180:     return '';
                   3181: }
                   3182: 
                   3183: sub end_string {
                   3184:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3185: 
                   3186:     if ($target ne 'helper') {
                   3187:         return '';
                   3188:     }
                   3189:     Apache::lonhelper::string->new();
                   3190:     return '';
                   3191: }
                   3192: 
                   3193: sub render {
                   3194:     my $self = shift;
1.38      bowersj2 3195:     my $result = '';
                   3196: 
                   3197:     if (defined $self->{ERROR_MSG}) {
1.97      albertel 3198:         $result .= '<p><font color="#FF0000">' . $self->{ERROR_MSG} . '</font></p>';
1.38      bowersj2 3199:     }
                   3200: 
1.157     raeburn  3201:     $result .= '<input type="string" name="' . $self->{'variable'} . '_forminput"';
1.34      bowersj2 3202: 
                   3203:     if (defined($self->{'size'})) {
                   3204:         $result .= ' size="' . $self->{'size'} . '"';
                   3205:     }
                   3206:     if (defined($self->{'maxlength'})) {
                   3207:         $result .= ' maxlength="' . $self->{'maxlength'} . '"';
                   3208:     }
                   3209: 
                   3210:     if (defined($self->{DEFAULT_VALUE})) {
                   3211:         my $valueFunc = eval($self->{DEFAULT_VALUE});
                   3212:         die 'Error in default value code for variable ' . 
                   3213:             $self->{'variable'} . ', Perl said: ' . $@ if $@;
                   3214:         $result .= ' value="' . &$valueFunc($helper, $self) . '"';
                   3215:     }
                   3216: 
                   3217:     $result .= ' />';
                   3218: 
                   3219:     return $result;
                   3220: }
                   3221: 
                   3222: # If a NEXTSTATE was given, switch to it
                   3223: sub postprocess {
                   3224:     my $self = shift;
1.38      bowersj2 3225: 
                   3226:     if (defined($self->{VALIDATOR})) {
                   3227: 	my $validator = eval($self->{VALIDATOR});
1.138     albertel 3228: 	die 'Died during evaluation of validator code; Perl said: ' . $@ if $@;
1.38      bowersj2 3229: 	my $invalid = &$validator($helper, $state, $self, $self->getValue());
                   3230: 	if ($invalid) {
                   3231: 	    $self->{ERROR_MSG} = $invalid;
                   3232: 	    return 0;
                   3233: 	}
                   3234:     }
                   3235: 
                   3236:     if (defined($self->{'nextstate'})) {
                   3237:         $helper->changeState($self->{'nextstate'});
1.34      bowersj2 3238:     }
                   3239: 
                   3240:     return 1;
                   3241: }
                   3242: 
                   3243: 1;
                   3244: 
1.8       bowersj2 3245: package Apache::lonhelper::general;
                   3246: 
                   3247: =pod
                   3248: 
1.44      bowersj2 3249: =head2 General-purpose tag: <exec>X<exec, helper tag>
1.8       bowersj2 3250: 
1.44      bowersj2 3251: The contents of the exec tag are executed as Perl code, B<not> inside a 
1.100     albertel 3252: safe space, so the full range of $env and such is available. The code
1.8       bowersj2 3253: will be executed as a subroutine wrapped with the following code:
                   3254: 
                   3255: "sub { my $helper = shift; my $state = shift;" and
                   3256: 
                   3257: "}"
                   3258: 
                   3259: The return value is ignored.
                   3260: 
                   3261: $helper is the helper object. Feel free to add methods to the helper
                   3262: object to support whatever manipulation you may need to do (for instance,
                   3263: overriding the form location if the state is the final state; see 
1.44      bowersj2 3264: parameter.helper for an example).
1.8       bowersj2 3265: 
                   3266: $state is the $paramHash that has currently been generated and may
                   3267: be manipulated by the code in exec. Note that the $state is not yet
                   3268: an actual state B<object>, it is just a hash, so do not expect to
                   3269: be able to call methods on it.
                   3270: 
                   3271: =cut
                   3272: 
1.83      sakharuk 3273: use Apache::lonlocal;
1.102     albertel 3274: use Apache::lonnet;
1.83      sakharuk 3275: 
1.8       bowersj2 3276: BEGIN {
                   3277:     &Apache::lonhelper::register('Apache::lonhelper::general',
1.11      bowersj2 3278:                                  'exec', 'condition', 'clause',
                   3279:                                  'eval');
1.8       bowersj2 3280: }
                   3281: 
                   3282: sub start_exec {
                   3283:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3284: 
                   3285:     if ($target ne 'helper') {
                   3286:         return '';
                   3287:     }
                   3288:     
                   3289:     my $code = &Apache::lonxml::get_all_text('/exec', $parser);
                   3290:     
                   3291:     $code = eval ('sub { my $helper = shift; my $state = shift; ' .
                   3292:         $code . "}");
1.11      bowersj2 3293:     die 'Error in <exec>, Perl said: '. $@ if $@;
1.8       bowersj2 3294:     &$code($helper, $paramHash);
                   3295: }
                   3296: 
                   3297: sub end_exec { return ''; }
                   3298: 
                   3299: =pod
                   3300: 
                   3301: =head2 General-purpose tag: <condition>
                   3302: 
                   3303: The <condition> tag allows you to mask out parts of the helper code
                   3304: depending on some programatically determined condition. The condition
                   3305: tag contains a tag <clause> which contains perl code that when wrapped
                   3306: with "sub { my $helper = shift; my $state = shift; " and "}", returns
                   3307: a true value if the XML in the condition should be evaluated as a normal
                   3308: part of the helper, or false if it should be completely discarded.
                   3309: 
                   3310: The <clause> tag must be the first sub-tag of the <condition> tag or
                   3311: it will not work as expected.
                   3312: 
                   3313: =cut
                   3314: 
                   3315: # The condition tag just functions as a marker, it doesn't have
                   3316: # to "do" anything. Technically it doesn't even have to be registered
                   3317: # with the lonxml code, but I leave this here to be explicit about it.
                   3318: sub start_condition { return ''; }
                   3319: sub end_condition { return ''; }
                   3320: 
                   3321: sub start_clause {
                   3322:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3323: 
                   3324:     if ($target ne 'helper') {
                   3325:         return '';
                   3326:     }
                   3327:     
                   3328:     my $clause = Apache::lonxml::get_all_text('/clause', $parser);
                   3329:     $clause = eval('sub { my $helper = shift; my $state = shift; '
                   3330:         . $clause . '}');
1.11      bowersj2 3331:     die 'Error in clause of condition, Perl said: ' . $@ if $@;
1.8       bowersj2 3332:     if (!&$clause($helper, $paramHash)) {
                   3333:         # Discard all text until the /condition.
1.155     albertel 3334: 	my $end_tag = $paramHash->{SKIPTAG} || '/condition';
                   3335:         &Apache::lonxml::get_all_text($end_tag, $parser);
1.8       bowersj2 3336:     }
                   3337: }
                   3338: 
                   3339: sub end_clause { return ''; }
1.11      bowersj2 3340: 
                   3341: =pod
                   3342: 
1.44      bowersj2 3343: =head2 General-purpose tag: <eval>X<eval, helper tag>
1.11      bowersj2 3344: 
                   3345: The <eval> tag will be evaluated as a subroutine call passed in the
                   3346: current helper object and state hash as described in <condition> above,
                   3347: but is expected to return a string to be printed directly to the
                   3348: screen. This is useful for dynamically generating messages. 
                   3349: 
                   3350: =cut
                   3351: 
                   3352: # This is basically a type of message.
                   3353: # Programmatically setting $paramHash->{NEXTSTATE} would work, though
                   3354: # it's probably bad form.
                   3355: 
                   3356: sub start_eval {
                   3357:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3358: 
                   3359:     if ($target ne 'helper') {
                   3360:         return '';
                   3361:     }
                   3362:     
                   3363:     my $program = Apache::lonxml::get_all_text('/eval', $parser);
                   3364:     $program = eval('sub { my $helper = shift; my $state = shift; '
                   3365:         . $program . '}');
                   3366:     die 'Error in eval code, Perl said: ' . $@ if $@;
                   3367:     $paramHash->{MESSAGE_TEXT} = &$program($helper, $paramHash);
                   3368: }
                   3369: 
                   3370: sub end_eval { 
                   3371:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3372: 
                   3373:     if ($target ne 'helper') {
                   3374:         return '';
                   3375:     }
                   3376: 
                   3377:     Apache::lonhelper::message->new();
                   3378: }
                   3379: 
1.13      bowersj2 3380: 1;
                   3381: 
1.27      bowersj2 3382: package Apache::lonhelper::final;
                   3383: 
                   3384: =pod
                   3385: 
1.44      bowersj2 3386: =head2 Element: finalX<final, helper tag>
1.27      bowersj2 3387: 
                   3388: <final> is a special element that works with helpers that use the <finalcode>
1.44      bowersj2 3389: tagX<finalcode, helper tag>. It goes through all the states and elements, executing the <finalcode>
1.27      bowersj2 3390: snippets and collecting the results. Finally, it takes the user out of the
                   3391: helper, going to a provided page.
                   3392: 
1.34      bowersj2 3393: If the parameter "restartCourse" is true, this will override the buttons and
                   3394: will make a "Finish Helper" button that will re-initialize the course for them,
                   3395: which is useful for the Course Initialization helper so the users never see
                   3396: the old values taking effect.
                   3397: 
1.93      albertel 3398: If the parameter "restartCourse" is not true a 'Finish' Button will be
                   3399: presented that takes the user back to whatever was defined as <exitpage>
                   3400: 
1.27      bowersj2 3401: =cut
                   3402: 
                   3403: no strict;
                   3404: @ISA = ("Apache::lonhelper::element");
                   3405: use strict;
1.62      matthew  3406: use Apache::lonlocal;
1.100     albertel 3407: use Apache::lonnet;
1.27      bowersj2 3408: BEGIN {
                   3409:     &Apache::lonhelper::register('Apache::lonhelper::final',
                   3410:                                  ('final', 'exitpage'));
                   3411: }
                   3412: 
                   3413: sub new {
                   3414:     my $ref = Apache::lonhelper::element->new();
                   3415:     bless($ref);
                   3416: }
                   3417: 
1.34      bowersj2 3418: sub start_final { 
                   3419:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3420: 
                   3421:     if ($target ne 'helper') {
                   3422:         return '';
                   3423:     }
                   3424: 
                   3425:     $paramHash->{'restartCourse'} = $token->[2]{'restartCourse'};
                   3426: 
                   3427:     return ''; 
                   3428: }
1.27      bowersj2 3429: 
                   3430: sub end_final {
                   3431:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3432: 
                   3433:     if ($target ne 'helper') {
                   3434:         return '';
                   3435:     }
                   3436: 
                   3437:     Apache::lonhelper::final->new();
                   3438:    
                   3439:     return '';
                   3440: }
                   3441: 
                   3442: sub start_exitpage {
                   3443:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3444: 
                   3445:     if ($target ne 'helper') {
                   3446:         return '';
                   3447:     }
                   3448: 
                   3449:     $paramHash->{EXIT_PAGE} = &Apache::lonxml::get_all_text('/exitpage',
                   3450:                                                             $parser);
                   3451: 
                   3452:     return '';
                   3453: }
                   3454: 
                   3455: sub end_exitpage { return ''; }
                   3456: 
                   3457: sub render {
                   3458:     my $self = shift;
                   3459: 
                   3460:     my @results;
                   3461: 
                   3462:     # Collect all the results
                   3463:     for my $stateName (keys %{$helper->{STATES}}) {
                   3464:         my $state = $helper->{STATES}->{$stateName};
                   3465:         
                   3466:         for my $element (@{$state->{ELEMENTS}}) {
                   3467:             if (defined($element->{FINAL_CODE})) {
                   3468:                 # Compile the code.
1.31      bowersj2 3469:                 my $code = 'sub { my $helper = shift; my $element = shift; ' 
                   3470:                     . $element->{FINAL_CODE} . '}';
1.27      bowersj2 3471:                 $code = eval($code);
                   3472:                 die 'Error while executing final code for element with var ' .
                   3473:                     $element->{'variable'} . ', Perl said: ' . $@ if $@;
                   3474: 
1.31      bowersj2 3475:                 my $result = &$code($helper, $element);
1.27      bowersj2 3476:                 if ($result) {
                   3477:                     push @results, $result;
                   3478:                 }
                   3479:             }
                   3480:         }
                   3481:     }
                   3482: 
1.40      bowersj2 3483:     my $result;
1.27      bowersj2 3484: 
1.40      bowersj2 3485:     if (scalar(@results) != 0) {
                   3486: 	$result .= "<ul>\n";
                   3487: 	for my $re (@results) {
                   3488: 	    $result .= '    <li>' . $re . "</li>\n";
                   3489: 	}
                   3490: 	
                   3491: 	if (!@results) {
1.59      bowersj2 3492: 	    $result .= '    <li>' . 
                   3493: 		&mt('No changes were made to current settings.') . '</li>';
1.40      bowersj2 3494: 	}
                   3495: 	
                   3496: 	$result .= '</ul>';
1.34      bowersj2 3497:     }
                   3498: 
1.93      albertel 3499:     my $actionURL = $self->{EXIT_PAGE};
                   3500:     my $targetURL = '';
                   3501:     my $finish=&mt('Finish');
1.34      bowersj2 3502:     if ($self->{'restartCourse'}) {
1.103     albertel 3503: 	$actionURL = '/adm/roles';
1.93      albertel 3504: 	$targetURL = '/adm/menu';
1.100     albertel 3505: 	if ($env{'course.'.$env{'request.course.id'}.'.url'}=~/^uploaded/) {
1.64      albertel 3506: 	    $targetURL = '/adm/coursedocs';
                   3507: 	} else {
                   3508: 	    $targetURL = '/adm/navmaps';
                   3509: 	}
1.100     albertel 3510: 	if ($env{'course.'.$env{'request.course.id'}.'.clonedfrom'}) {
1.45      bowersj2 3511: 	    $targetURL = '/adm/parmset?overview=1';
                   3512: 	}
1.93      albertel 3513: 	my $finish=&mt('Finish Course Initialization');
1.34      bowersj2 3514:     }
1.93      albertel 3515:     my $previous = HTML::Entities::encode(&mt("<- Previous"), '<>&"');
                   3516:     my $next = HTML::Entities::encode(&mt("Next ->"), '<>&"');
1.127     albertel 3517:     my $target = " target='loncapaclient'";
                   3518:     if (($env{'browser.interface'} eq 'textual') ||
                   3519:         ($env{'environment.remote'} eq 'off')) {  $target='';  }
1.93      albertel 3520:     $result .= "<center>\n" .
1.127     albertel 3521: 	"<form action='".$actionURL."' method='post' $target>\n" .
1.93      albertel 3522: 	"<input type='button' onclick='history.go(-1)' value='$previous' />" .
                   3523: 	"<input type='hidden' name='orgurl' value='$targetURL' />" .
                   3524: 	"<input type='hidden' name='selectrole' value='1' />\n" .
1.100     albertel 3525: 	"<input type='hidden' name='" . $env{'request.role'} . 
1.93      albertel 3526: 	"' value='1' />\n<input type='submit' value='" . $finish . "' />\n" .
                   3527: 	"</form></center>";
1.34      bowersj2 3528: 
1.40      bowersj2 3529:     return $result;
1.34      bowersj2 3530: }
                   3531: 
                   3532: sub overrideForm {
1.93      albertel 3533:     return 1;
1.27      bowersj2 3534: }
                   3535: 
                   3536: 1;
                   3537: 
1.13      bowersj2 3538: package Apache::lonhelper::parmwizfinal;
                   3539: 
1.160     albertel 3540: # This is the final state for the parm helper. It is not generally useful,
1.13      bowersj2 3541: # so it is not perldoc'ed. It does its own processing.
                   3542: # It is represented with <parmwizfinal />, and
                   3543: # should later be moved to lonparmset.pm .
                   3544: 
                   3545: no strict;
                   3546: @ISA = ('Apache::lonhelper::element');
                   3547: use strict;
1.69      sakharuk 3548: use Apache::lonlocal;
1.102     albertel 3549: use Apache::lonnet;
1.11      bowersj2 3550: 
1.13      bowersj2 3551: BEGIN {
                   3552:     &Apache::lonhelper::register('Apache::lonhelper::parmwizfinal',
                   3553:                                  ('parmwizfinal'));
                   3554: }
                   3555: 
                   3556: use Time::localtime;
                   3557: 
                   3558: sub new {
                   3559:     my $ref = Apache::lonhelper::choices->new();
                   3560:     bless ($ref);
                   3561: }
                   3562: 
                   3563: sub start_parmwizfinal { return ''; }
                   3564: 
                   3565: sub end_parmwizfinal {
                   3566:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   3567: 
                   3568:     if ($target ne 'helper') {
                   3569:         return '';
                   3570:     }
                   3571:     Apache::lonhelper::parmwizfinal->new();
                   3572: }
                   3573: 
                   3574: # Renders a form that, when submitted, will form the input to lonparmset.pm
                   3575: sub render {
                   3576:     my $self = shift;
                   3577:     my $vars = $helper->{VARS};
                   3578: 
                   3579:     # FIXME: Unify my designators with the standard ones
1.48      bowersj2 3580:     my %dateTypeHash = ('open_date' => "opening date",
                   3581:                         'due_date' => "due date",
                   3582:                         'answer_date' => "answer date",
                   3583: 			'tries' => 'number of tries',
                   3584: 			'weight' => 'problem weight'
1.38      bowersj2 3585: 			);
1.13      bowersj2 3586:     my %parmTypeHash = ('open_date' => "0_opendate",
                   3587:                         'due_date' => "0_duedate",
1.38      bowersj2 3588:                         'answer_date' => "0_answerdate",
1.48      bowersj2 3589: 			'tries' => '0_maxtries',
                   3590: 			'weight' => '0_weight' );
1.107     albertel 3591:     my %realParmName = ('open_date' => "opendate",
                   3592:                         'due_date' => "duedate",
                   3593:                         'answer_date' => "answerdate",
                   3594: 			'tries' => 'maxtries',
                   3595: 			'weight' => 'weight' );
1.13      bowersj2 3596:     
                   3597:     my $affectedResourceId = "";
                   3598:     my $parm_name = $parmTypeHash{$vars->{ACTION_TYPE}};
                   3599:     my $level = "";
1.27      bowersj2 3600:     my $resourceString;
                   3601:     my $symb;
                   3602:     my $paramlevel;
1.95      albertel 3603:     
1.13      bowersj2 3604:     # Print the granularity, depending on the action
                   3605:     if ($vars->{GRANULARITY} eq 'whole_course') {
1.76      sakharuk 3606:         $resourceString .= '<li>'.&mt('for <b>all resources in the course</b>').'</li>';
1.104     albertel 3607: 	if ($vars->{TARGETS} eq 'course') {
1.150     albertel 3608: 	    $level = 14; # general course, see lonparmset.pm perldoc
1.104     albertel 3609: 	} elsif ($vars->{TARGETS} eq 'section') {
1.150     albertel 3610: 	    $level = 9;
                   3611: 	} elsif ($vars->{TARGETS} eq 'group') {
1.104     albertel 3612: 	    $level = 6;
                   3613: 	} else {
                   3614: 	    $level = 3;
                   3615: 	}
1.13      bowersj2 3616:         $affectedResourceId = "0.0";
1.27      bowersj2 3617:         $symb = 'a';
                   3618:         $paramlevel = 'general';
1.13      bowersj2 3619:     } elsif ($vars->{GRANULARITY} eq 'map') {
1.41      bowersj2 3620:         my $navmap = Apache::lonnavmaps::navmap->new();
1.35      bowersj2 3621:         my $res = $navmap->getByMapPc($vars->{RESOURCE_ID});
1.13      bowersj2 3622:         my $title = $res->compTitle();
1.27      bowersj2 3623:         $symb = $res->symb();
1.78      sakharuk 3624:         $resourceString .= '<li>'.&mt('for the map named [_1]',"<b>$title</b>").'</li>';
1.104     albertel 3625: 	if ($vars->{TARGETS} eq 'course') {
1.150     albertel 3626: 	    $level = 13; # general course, see lonparmset.pm perldoc
1.104     albertel 3627: 	} elsif ($vars->{TARGETS} eq 'section') {
1.150     albertel 3628: 	    $level = 8;
                   3629: 	} elsif ($vars->{TARGETS} eq 'group') {
1.104     albertel 3630: 	    $level = 5;
                   3631: 	} else {
                   3632: 	    $level = 2;
                   3633: 	}
1.13      bowersj2 3634:         $affectedResourceId = $vars->{RESOURCE_ID};
1.27      bowersj2 3635:         $paramlevel = 'map';
1.13      bowersj2 3636:     } else {
1.41      bowersj2 3637:         my $navmap = Apache::lonnavmaps::navmap->new();
1.13      bowersj2 3638:         my $res = $navmap->getById($vars->{RESOURCE_ID});
1.95      albertel 3639:         my $part = $vars->{RESOURCE_ID_part};
                   3640: 	if ($part ne 'All Parts' && $part) { $parm_name=~s/^0/$part/; } else { $part=&mt('All Parts'); }
1.27      bowersj2 3641:         $symb = $res->symb();
1.13      bowersj2 3642:         my $title = $res->compTitle();
1.95      albertel 3643:         $resourceString .= '<li>'.&mt('for the resource named [_1] part [_2]',"<b>$title</b>","<b>$part</b>").'</li>';
1.104     albertel 3644: 	if ($vars->{TARGETS} eq 'course') {
1.150     albertel 3645: 	    $level = 10; # general course, see lonparmset.pm perldoc
1.104     albertel 3646: 	} elsif ($vars->{TARGETS} eq 'section') {
1.150     albertel 3647: 	    $level = 7;
                   3648: 	} elsif ($vars->{TARGETS} eq 'group') {
1.104     albertel 3649: 	    $level = 4;
                   3650: 	} else {
                   3651: 	    $level = 1;
                   3652: 	}
1.13      bowersj2 3653:         $affectedResourceId = $vars->{RESOURCE_ID};
1.27      bowersj2 3654:         $paramlevel = 'full';
1.13      bowersj2 3655:     }
                   3656: 
1.105     albertel 3657:     my $result = "<form name='helpform' method='POST' action='/adm/parmset#$affectedResourceId&$parm_name&$level'>\n";
1.104     albertel 3658:     $result .= "<input type='hidden' name='action' value='settable' />\n";
                   3659:     $result .= "<input type='hidden' name='dis' value='helper' />\n";
1.107     albertel 3660:     $result .= "<input type='hidden' name='pscat' value='".
                   3661: 	$realParmName{$vars->{ACTION_TYPE}}."' />\n";
1.95      albertel 3662:     if ($vars->{GRANULARITY} eq 'resource') {
                   3663: 	$result .= "<input type='hidden' name='symb' value='".
                   3664: 	    HTML::Entities::encode($symb,"'<>&\"") . "' />\n";
1.108     albertel 3665:     } elsif ($vars->{GRANULARITY} eq 'map') {
                   3666: 	$result .= "<input type='hidden' name='pschp' value='".
                   3667: 	    $affectedResourceId."' />\n";
1.95      albertel 3668:     }
1.104     albertel 3669:     my $part = $vars->{RESOURCE_ID_part};
                   3670:     if ($part eq 'All Parts' || !$part) { $part=0; }
                   3671:     $result .= "<input type='hidden' name='psprt' value='".
                   3672: 	HTML::Entities::encode($part,"'<>&\"") . "' />\n";
                   3673: 
1.69      sakharuk 3674:     $result .= '<p>'.&mt('Confirm that this information is correct, then click &quot;Finish Helper&quot; to complete setting the parameter.').'<ul>';
1.27      bowersj2 3675:     
                   3676:     # Print the type of manipulation:
1.73      albertel 3677:     my $extra;
1.38      bowersj2 3678:     if ($vars->{ACTION_TYPE} eq 'tries') {
1.73      albertel 3679: 	$extra =  $vars->{TRIES};
1.38      bowersj2 3680:     }
1.48      bowersj2 3681:     if ($vars->{ACTION_TYPE} eq 'weight') {
1.73      albertel 3682: 	$extra =  $vars->{WEIGHT};
                   3683:     }
                   3684:     $result .= "<li>";
1.74      matthew  3685:     my $what = &mt($dateTypeHash{$vars->{ACTION_TYPE}});
1.73      albertel 3686:     if ($extra) {
                   3687: 	$result .= &mt('Setting the [_1] to [_2]',"<b>$what</b>",$extra);
                   3688:     } else {
                   3689: 	$result .= &mt('Setting the [_1]',"<b>$what</b>");
1.48      bowersj2 3690:     }
1.38      bowersj2 3691:     $result .= "</li>\n";
1.27      bowersj2 3692:     if ($vars->{ACTION_TYPE} eq 'due_date' || 
                   3693:         $vars->{ACTION_TYPE} eq 'answer_date') {
                   3694:         # for due dates, we default to "date end" type entries
                   3695:         $result .= "<input type='hidden' name='recent_date_end' " .
                   3696:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3697:         $result .= "<input type='hidden' name='pres_value' " . 
                   3698:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3699:         $result .= "<input type='hidden' name='pres_type' " .
                   3700:             "value='date_end' />\n";
                   3701:     } elsif ($vars->{ACTION_TYPE} eq 'open_date') {
                   3702:         $result .= "<input type='hidden' name='recent_date_start' ".
                   3703:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3704:         $result .= "<input type='hidden' name='pres_value' " .
                   3705:             "value='" . $vars->{PARM_DATE} . "' />\n";
                   3706:         $result .= "<input type='hidden' name='pres_type' " .
                   3707:             "value='date_start' />\n";
1.38      bowersj2 3708:     } elsif ($vars->{ACTION_TYPE} eq 'tries') {
                   3709: 	$result .= "<input type='hidden' name='pres_value' " .
                   3710: 	    "value='" . $vars->{TRIES} . "' />\n";
1.104     albertel 3711:         $result .= "<input type='hidden' name='pres_type' " .
                   3712:             "value='int_pos' />\n";
1.48      bowersj2 3713:     } elsif ($vars->{ACTION_TYPE} eq 'weight') {
                   3714: 	$result .= "<input type='hidden' name='pres_value' " .
                   3715: 	    "value='" . $vars->{WEIGHT} . "' />\n";
1.38      bowersj2 3716:     }
1.27      bowersj2 3717: 
                   3718:     $result .= $resourceString;
                   3719:     
1.13      bowersj2 3720:     # Print targets
                   3721:     if ($vars->{TARGETS} eq 'course') {
1.76      sakharuk 3722:         $result .= '<li>'.&mt('for <b>all students in course</b>').'</li>';
1.13      bowersj2 3723:     } elsif ($vars->{TARGETS} eq 'section') {
                   3724:         my $section = $vars->{SECTION_NAME};
1.79      sakharuk 3725:         $result .= '<li>'.&mt('for section [_1]',"<b>$section</b>").'</li>';
1.104     albertel 3726: 	$result .= "<input type='hidden' name='csec' value='" .
1.89      foxr     3727:             HTML::Entities::encode($section,"'<>&\"") . "' />\n";
1.128     raeburn  3728:     } elsif ($vars->{TARGETS} eq 'group') {
                   3729:         my $group = $vars->{GROUP_NAME};
                   3730:         $result .= '<li>'.&mt('for group [_1]',"<b>$group</b>").'</li>';
                   3731:         $result .= "<input type='hidden' name='cgroup' value='" .
                   3732:             HTML::Entities::encode($group,"'<>&\"") . "' />\n";
1.13      bowersj2 3733:     } else {
                   3734:         # FIXME: This is probably wasteful! Store the name!
                   3735:         my $classlist = Apache::loncoursedata::get_classlist();
1.109     albertel 3736: 	my ($uname,$udom)=split(':',$vars->{USER_NAME});
1.106     albertel 3737:         my $name = $classlist->{$uname.':'.$udom}->[6];
1.79      sakharuk 3738:         $result .= '<li>'.&mt('for [_1]',"<b>$name</b>").'</li>';
1.13      bowersj2 3739:         $result .= "<input type='hidden' name='uname' value='".
1.89      foxr     3740:             HTML::Entities::encode($uname,"'<>&\"") . "' />\n";
1.13      bowersj2 3741:         $result .= "<input type='hidden' name='udom' value='".
1.89      foxr     3742:             HTML::Entities::encode($udom,"'<>&\"") . "' />\n";
1.13      bowersj2 3743:     }
                   3744: 
                   3745:     # Print value
1.48      bowersj2 3746:     if ($vars->{ACTION_TYPE} ne 'tries' && $vars->{ACTION_TYPE} ne 'weight') {
1.81      sakharuk 3747: 	$result .= '<li>'.&mt('to [_1] ([_2])',"<b>".ctime($vars->{PARM_DATE})."</b>",Apache::lonnavmaps::timeToHumanString($vars->{PARM_DATE}))."</li>\n";
1.38      bowersj2 3748:     }
                   3749:  
1.13      bowersj2 3750:     # print pres_marker
                   3751:     $result .= "\n<input type='hidden' name='pres_marker'" .
                   3752:         " value='$affectedResourceId&$parm_name&$level' />\n";
1.27      bowersj2 3753:     
                   3754:     # Make the table appear
                   3755:     $result .= "\n<input type='hidden' value='true' name='prevvisit' />";
                   3756:     $result .= "\n<input type='hidden' value='$symb' name='pssymb' />";
                   3757:     $result .= "\n<input type='hidden' value='$paramlevel' name='parmlev' />";
1.13      bowersj2 3758: 
1.71      sakharuk 3759:     $result .= "<br /><br /><center><input type='submit' value='".&mt('Finish Helper')."' /></center></form>\n";
1.13      bowersj2 3760: 
                   3761:     return $result;
                   3762: }
                   3763:     
                   3764: sub overrideForm {
                   3765:     return 1;
                   3766: }
1.5       bowersj2 3767: 
1.4       bowersj2 3768: 1;
1.3       bowersj2 3769: 
1.1       bowersj2 3770: __END__
1.3       bowersj2 3771: 

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