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

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

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