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