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