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