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