--- loncom/interface/lonhelper.pm 2006/04/24 23:05:35 1.137
+++ loncom/interface/lonhelper.pm 2014/02/11 19:11:24 1.191
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# .helper XML handler to implement the LON-CAPA helper
#
-# $Id: lonhelper.pm,v 1.137 2006/04/24 23:05:35 albertel Exp $
+# $Id: lonhelper.pm,v 1.191 2014/02/11 19:11:24 bisitz Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -81,25 +81,28 @@ State tags are also required to have an
human name of the state, and will be displayed as the header on top of
the screen for the user.
+State tags may also optionally have an attribute "help" which should be
+the filename of a help file, this will add a blue ? to the title.
+
=head2 Example Helper Skeleton
An example of the tags so far:
-
+
-Of course this does nothing. In order for the wizard to do something, it is
-necessary to put actual elements into the wizard. Documentation for each
+Of course this does nothing. In order for the helper to do something, it is
+necessary to put actual elements into the helper. Documentation for each
of these elements follows.
=head1 Creating a Helper With Code, Not XML
-In some situations, such as the printing wizard (see lonprintout.pm),
+In some situations, such as the printing helper (see lonprintout.pm),
writing the helper in XML would be too complicated, because of scope
issues or the fact that the code actually outweighs the XML. It is
possible to create a helper via code, though it is a little odd.
@@ -183,6 +186,11 @@ use Apache::File;
use Apache::lonxml;
use Apache::lonlocal;
use Apache::lonnet;
+use Apache::longroup;
+use Apache::lonselstudent;
+
+
+use LONCAPA;
# Register all the tags with the helper, so the helper can
# push and pop them
@@ -253,7 +261,7 @@ sub real_handler {
my $uri = shift;
if (!defined($uri)) { $uri = $r->uri(); }
$env{'request.uri'} = $uri;
- my $filename = '/home/httpd/html' . $uri;
+ my $filename = $r->dir_config('lonDocRoot').$uri;
my $fh = Apache::File->new($filename);
my $file;
read $fh, $file, 100000000;
@@ -331,7 +339,8 @@ sub start_state {
}
Apache::lonhelper::state->new($token->[2]{'name'},
- $token->[2]{'title'});
+ $token->[2]{'title'},
+ $token->[2]{'help'});
return '';
}
@@ -361,6 +370,7 @@ use Apache::loncommon;
use Apache::File;
use Apache::lonlocal;
use Apache::lonnet;
+use LONCAPA;
sub new {
my $proto = shift;
@@ -466,9 +476,8 @@ sub _saveVars {
sub _varsInFile {
my $self = shift;
my @vars = ();
- for my $key (keys %{$self->{VARS}}) {
- push @vars, &Apache::lonnet::escape($key) . '=' .
- &Apache::lonnet::escape($self->{VARS}->{$key});
+ for my $key (keys(%{$self->{VARS}})) {
+ push(@vars, &escape($key) . '=' . &escape($self->{VARS}->{$key}));
}
return join ('&', @vars);
}
@@ -483,7 +492,7 @@ sub declareVar {
$self->{VARS}->{$var} = '';
}
- my $envname = 'form.' . $var . '.forminput';
+ my $envname = 'form.' . $var . '_forminput';
if (defined($env{$envname})) {
if (ref($env{$envname})) {
$self->{VARS}->{$var} = join('|||', @{$env{$envname}});
@@ -522,7 +531,7 @@ sub process {
# Phase 1: Post processing for state of previous screen (which is actually
# the "current state" in terms of the helper variables), if it wasn't the
# beginning state.
- if ($self->{STATE} ne "START" || $env{"form.SUBMIT"} eq &mt("Next ->")) {
+ if ($self->{STATE} ne "START" || $env{"form.SUBMIT"} eq &mt("Next")) {
my $prevState = $self->{STATES}{$self->{STATE}};
$prevState->postprocess();
}
@@ -562,7 +571,7 @@ sub process {
# 4: Render the current state to the screen as an HTML page.
sub display {
my $self = shift;
-
+ my $footer = shift;
my $state = $self->{STATES}{$self->{STATE}};
my $result = "";
@@ -574,84 +583,85 @@ sub display {
# Phase 4: Display.
my $stateTitle=&mt($state->title());
+ my $stateHelp= $state->help();
my $browser_searcher_js =
'';
- $result .= &Apache::loncommon::start_page($self->{TITLE},
- $browser_searcher_js);
-
- my $previous = HTML::Entities::encode(&mt("<- Previous"), '<>&"');
- my $next = HTML::Entities::encode(&mt("Next ->"), '<>&"');
- # FIXME: This should be parameterized, not concatenated - Jeremy
+ # Breadcrumbs
+ my $brcrum = [{'href' => '',
+ 'text' => 'Helper'}];
+ # FIXME: Dynamically add context sensitive breadcrumbs
+ # depending on the caller,
+ # e.g. printing, parametrization, etc.
+ # FIXME: Add breadcrumbs to reflect current helper state
+ $result .= &Apache::loncommon::start_page($self->{TITLE},
+ $browser_searcher_js,
+ {'bread_crumbs' => $brcrum,});
- if (!$state->overrideForm()) { $result.="
FOOTER
- $result .= &Apache::loncommon::end_page();
+ $result .= $footer.&Apache::loncommon::end_page();
# Handle writing out the vars to the file
my $file = Apache::File->new('>'.$self->{FILENAME});
print $file $self->_varsInFile();
@@ -677,6 +687,7 @@ sub new {
$self->{NAME} = shift;
$self->{TITLE} = shift;
+ $self->{HELP} = shift;
$self->{ELEMENTS} = [];
bless($self, $class);
@@ -698,6 +709,11 @@ sub title {
return $self->{TITLE};
}
+sub help {
+ my $self = shift;
+ return $self->{HELP};
+}
+
sub preprocess {
my $self = shift;
for my $element (@{$self->{ELEMENTS}}) {
@@ -789,7 +805,7 @@ the element. How this value is interpret
the element itself, and possibly the settings the element has (such as
multichoice vs. single choice for tags).
-This is also intended for things like the course initialization wizard, where the
+This is also intended for things like the course initialization helper, where the
user is setting various parameters. By correctly grabbing current settings
and including them into the helper, it allows the user to come back to the
helper later and re-execute it, without needing to worry about overwriting
@@ -991,6 +1007,9 @@ sub start_message {
if (defined($token->[2]{'nextstate'})) {
$paramHash->{NEXTSTATE} = $token->[2]{'nextstate'};
}
+ if (defined($token->[2]{'type'})) {
+ $paramHash->{TYPE} = $token->[2]{'type'};
+ }
return '';
}
@@ -1006,8 +1025,16 @@ sub end_message {
sub render {
my $self = shift;
-
- return &mtn($self->{MESSAGE_TEXT});
+
+ if ($self->{TYPE} =~ /^\s*warning\s*$/i) {
+ $self->{MESSAGE_TEXT} =
+ ''. $self->{MESSAGE_TEXT}.'';
+ }
+ if ($self->{TYPE} =~ /^\s*error\s*$/i) {
+ $self->{MESSAGE_TEXT} =
+ ''. $self->{MESSAGE_TEXT}.'';
+ }
+ return $self->{MESSAGE_TEXT};
}
# If a NEXTSTATE was given, switch to it
sub postprocess {
@@ -1020,6 +1047,175 @@ sub postprocess {
}
1;
+package Apache::lonhelper::helpicon;
+
+=pod
+
+=head1 Elements
+
+=head2 Element: helpiconX
+
+Helpicon elements add a help icon at the current location.
+Example:
+
+
+ General Help
+
+
+In this example will generate a help icon to the Help.hlp url with a
+description of 'General Help'. The description is not required and if
+left out (Example: only the icon will be
+added.)
+
+=head3 Localization
+
+The description text will be run through the normalize_string function
+and that will be used as a call to &mt.
+
+=cut
+
+no strict;
+@ISA = ("Apache::lonhelper::element");
+use strict;
+use Apache::lonlocal;
+
+BEGIN {
+ &Apache::lonhelper::register('Apache::lonhelper::helpicon',
+ ('helpicon'));
+}
+
+sub new {
+ my $ref = Apache::lonhelper::element->new();
+ bless($ref);
+}
+
+# CONSTRUCTION: Construct the message element from the XML
+sub start_helpicon {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+
+ if ($target ne 'helper') {
+ return '';
+ }
+
+ $paramHash->{HELP_TEXT} = &mtn(&Apache::lonxml::get_all_text('/helpicon',
+ $parser));
+
+ $paramHash->{HELP_TEXT} =~s/^\s+//;
+ $paramHash->{HELP_TEXT} =~s/\s+$//;
+
+ if (defined($token->[2]{'file'})) {
+ $paramHash->{HELP_FILE} = $token->[2]{'file'};
+ }
+ return '';
+}
+
+sub end_helpicon {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+
+ if ($target ne 'helper') {
+ return '';
+ }
+ Apache::lonhelper::helpicon->new();
+ return '';
+}
+
+sub render {
+ my $self = shift;
+
+ my $text;
+ if ( $self->{HELP_TEXT} ne '') {
+ $text=&mtn($self->{HELP_TEXT});
+ }
+
+ return &Apache::loncommon::help_open_topic($self->{HELP_FILE},
+ $text);
+}
+sub postprocess {
+ my $self = shift;
+ if (defined($self->{NEXTSTATE})) {
+ $helper->changeState($self->{NEXTSTATE});
+ }
+
+ return 1;
+}
+
+1;
+
+package Apache::lonhelper::skip;
+
+=pod
+
+=head1 Elements
+
+=head2 Element: skipX
+
+The tag allows you define conditions under which the current state
+should be skipped over and define what state to skip to.
+
+
+
+
+ #some code that decides whether to skip the state or not
+
+ FINISH
+
+ A possibly skipped state
+
+
+=cut
+
+no strict;
+@ISA = ("Apache::lonhelper::element");
+use strict;
+
+BEGIN {
+ &Apache::lonhelper::register('Apache::lonhelper::skip',
+ ('skip'));
+}
+
+sub new {
+ my $ref = Apache::lonhelper::element->new();
+ bless($ref);
+}
+
+sub start_skip {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+
+ if ($target ne 'helper') {
+ return '';
+ }
+ # let know what text to skip to
+ $paramHash->{SKIPTAG}='/skip';
+ return '';
+}
+
+sub end_skip {
+ my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+
+ if ($target ne 'helper') {
+ return '';
+ }
+ Apache::lonhelper::skip->new();
+ return '';
+}
+
+sub render {
+ my $self = shift;
+ return '';
+}
+# If a NEXTSTATE is set, switch to it
+sub preprocess {
+ my ($self) = @_;
+
+ if (defined($self->{NEXTSTATE})) {
+ $helper->changeState($self->{NEXTSTATE});
+ }
+
+ return 1;
+}
+
+1;
+
package Apache::lonhelper::choices;
=pod
@@ -1202,7 +1398,7 @@ sub render {
function checkall(value, checkName) {
for (i=0; i{CHOICES}}) {
my $id = &new_id();
$result .= "
\n
\n";
- $result .= "
[1]}) {
- $result .= " checked='checked' ";
+ $result .= " checked='checked'";
}
- $result .= qq{id="id$id"};
+ $result .= qq{ id="id$id"};
my $choiceLabel = $choice->[0];
if ($choice->[3]) { # if we need to evaluate this choice
$choiceLabel = "sub { my $helper = shift; my $state = shift;" .
@@ -1290,11 +1486,11 @@ BUTTONS
$choiceLabel = eval($choiceLabel);
$choiceLabel = &$choiceLabel($helper, $self);
}
- $result .= "/>
".qq{
";
+ $result .= " />
".qq{
";
if ($choice->[4]) {
$result .='
';
}
$result .= "
\n";
@@ -1309,7 +1505,8 @@ BUTTONS
# given, switch to it
sub postprocess {
my $self = shift;
- my $chosenValue = $env{'form.' . $self->{'variable'} . '.forminput'};
+ my $chosenValue = $env{'form.' . $self->{'variable'} . '_forminput'};
+
if (!defined($chosenValue) && !$self->{'allowempty'}) {
$self->{ERROR_MSG} =
@@ -1317,6 +1514,8 @@ sub postprocess {
return 0;
}
+
+
if (ref($chosenValue)) {
$helper->{VARS}->{$self->{'variable'}} = join('|||', @$chosenValue);
}
@@ -1333,7 +1532,7 @@ sub postprocess {
}
if ($choice->[4]) {
my $varname = $choice->[4];
- $helper->{'VARS'}->{$varname} = $env{'form.'."$varname.forminput"};
+ $helper->{'VARS'}->{$varname} = $env{'form.'."${varname}_forminput"};
}
}
return 1;
@@ -1442,7 +1641,7 @@ sub render {
$checkedChoices{$self->{CHOICES}->[0]->[1]} = 1;
}
- $result .= "