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

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

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