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