--- loncom/interface/lonhtmlcommon.pm 2002/07/22 20:35:05 1.1
+++ loncom/interface/lonhtmlcommon.pm 2009/01/22 14:44:40 1.200
@@ -1,246 +1,2096 @@
+# The LearningOnline Network with CAPA
+# a pile of common html routines
+#
+# $Id: lonhtmlcommon.pm,v 1.200 2009/01/22 14:44:40 bisitz Exp $
+#
+# Copyright Michigan State University Board of Trustees
+#
+# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
+#
+# LON-CAPA is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# LON-CAPA is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with LON-CAPA; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# /home/httpd/html/adm/gpl.txt
+#
+# http://www.lon-capa.org/
+#
+######################################################################
+######################################################################
+
+=pod
+
+=head1 NAME
+
+Apache::lonhtmlcommon - routines to do common html things
+
+=head1 SYNOPSIS
+
+Referenced by other mod_perl Apache modules.
+
+=head1 INTRODUCTION
+
+lonhtmlcommon is a collection of subroutines used to present information
+in a consistent html format, or provide other functionality related to
+html.
+
+=head2 General Subroutines
+
+=over 4
+
+=cut
+
+######################################################################
+######################################################################
+
package Apache::lonhtmlcommon;
use strict;
+use Time::Local;
+use Time::HiRes;
+use Apache::lonlocal;
+use Apache::lonnet;
+use LONCAPA;
-sub MapOptions {
- my ($data, $page)=@_;
- my $Str = '';
- $Str .= ''."\n";
+##############################################
- return $Str;
+=pod
+
+=item dragmath_js
+
+Javascript used to open pop-up window containing dragmath applet which
+can be used to paste LaTeX into a textarea.
+
+=cut
+
+sub dragmath_js {
+ my ($popup) = @_;
+ return <
+ function mathedit(textarea, doc) {
+ targetEntry = textarea;
+ targetDoc = doc;
+ newwin = window.open("/adm/dragmath/applet/$popup.html","","width=565,height=500,resizable");
+ }
+
+
+ENDDRAGMATHJS
}
-sub StudentOptions {
- my ($cache, $students, $selectedName, $page)=@_;
- my $Str = '';
- $Str = '\n";
+ return $return;
+}
+
+sub get_recent {
+ my ($area, $n) = @_;
+ my %recent=&Apache::lonnet::dump(&recent_filename($area));
+
+# Create hash with key as time and recent as value
+# Begin filling return_hash with any 'always_include' option
+ my %time_hash = ();
+ my %return_hash = ();
+ foreach my $item (keys %recent) {
+ my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
+ if ($thistime eq 'always_include') {
+ $return_hash{$item} = &unescape($thisvalue);
+ $n--;
+ } else {
+ $time_hash{$thistime} = $item;
}
- $Str .= '>';
- $Str .= $cache->{$_.':fullname'};
- $Str .= ''."\n";
}
- $Str .= ''."\n";
- $Str .= ''."\n";
+ return %return_hash;
+}
- return $Str;
+sub get_recent_frozen {
+ my ($area) = @_;
+ my %recent=&Apache::lonnet::dump(&recent_filename($area));
+
+# Create hash with all 'frozen' items
+ my %return_hash = ();
+ foreach my $item (keys(%recent)) {
+ my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
+ if ($thistime eq 'always_include') {
+ $return_hash{$item} = &unescape($thisvalue);
+ }
+ }
+ return %return_hash;
}
-sub StatusOptions {
- my ($status, $formName)=@_;
- my $OpSel1 = '';
- my $OpSel2 = '';
- my $OpSel3 = '';
-
- if($status eq 'Any') { $OpSel3 = ' selected'; }
- elsif($status eq 'Expired' ) { $OpSel2 = ' selected'; }
- else { $OpSel1 = ' selected'; }
- my $Str = '';
- $Str .= ''."\n";
+=pod
+
+=item textbox
+
+=cut
+
+##############################################
+##############################################
+sub textbox {
+ my ($name,$value,$size,$special) = @_;
+ $size = 40 if (! defined($size));
+ $value = &HTML::Entities::encode($value,'<>&"');
+ my $Str = '';
+ return $Str;
}
-sub Title {
- my ($pageName)=@_;
+##############################################
+##############################################
- my $Str = '';
+=pod
- $Str .= ''.$pageName.''."\n";
- $Str .= ''."\n";
- $Str .= ''."\n";
- $Str .= '
'."\n";
+ }
+ #
+ # Return the @Crumbs stack to what we started with
+ push(@Crumbs,$last);
+ shift(@Crumbs);
+ #
+ return "$Str\n$Str1";
+ }
+
+ sub clear_breadcrumbs {
+ undef(@Crumbs);
+ }
+
+ sub add_breadcrumb {
+ push (@Crumbs,@_);
+ }
+
+} # End of scope for @Crumbs
+
+############################################################
+############################################################
+
+# Nested table routines.
+#
+# Routines to display form items in a multi-row table with 2 columns.
+# Uses nested tables to divide form elements into segments.
+# For examples of use see loncom/interface/lonnotify.pm
+#
+# Can be used in following order: ...
+# &start_pick_box()
+# row1
+# row2
+# row3 ... etc.
+# &submit_row()
+# &end_pick_box()
+#
+# where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
+# &status_select_row and &email_default_row
+#
+# Can also be used in following order:
+#
+# &start_pick_box()
+# &row_title()
+# &row_closure()
+# &row_title()
+# &row_closure() ... etc.
+# &submit_row()
+# &end_pick_box()
+#
+# In general a &submit_row() call should proceed the call to &end_pick_box(),
+# as this routine adds a button for form submission.
+# &submit_row() does not require a &row_closure after it.
+#
+# &start_pick_box() creates a bounding table with 1-pixel wide black border.
+# rows should be placed between calls to &start_pick_box() and &end_pick_box.
+#
+# &row_title() adds a title in the left column for each segment.
+# &row_closure() closes a row with a 1-pixel wide black line.
+#
+# &role_select_row() provides a select box from which to choose 1 or more roles
+# &course_select_row provides ways of picking groups of courses
+# radio buttons: all, by category or by picking from a course picker pop-up
+# note: by category option is only displayed if a domain has implemented
+# selection by year, semester, department, number etc.
+#
+# &status_select_row() provides a select box from which to choose 1 or more
+# access types (current access, prior access, and future access)
+#
+# &email_default_row() provides text boxes for default e-mail suffixes for
+# different authentication types in a domain.
+#
+# &row_title() and &row_closure() are called internally by the &*_select_row
+# routines, but can also be called directly to start and end rows which have
+# needs that are not accommodated by the *_select_row() routines.
+
+{ # Start: row_count block for pick_box
+my @row_count;
+
+sub start_pick_box {
+ my ($css_class) = @_;
+ if (defined($css_class)) {
+ $css_class = 'class="'.$css_class.'"';
+ } else {
+ $css_class= 'class="LC_pick_box"';
+ }
+ unshift(@row_count,0);
+ my $output = <<"END";
+
+ \n|;
+ return $output;
+}
+
+sub course_custom_roles {
+ my ($cdom,$cnum) = @_;
+ my %returnhash=();
+ my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
+ foreach my $person (sort(keys(%coursepersonnel))) {
+ my ($role) = ($person =~ /^([^:]+):/);
+ my ($end,$start) = split(/:/,$coursepersonnel{$person});
+ if ($end == -1 && $start == -1) {
next;
}
- $data=$cache->{$name.':'.$studentInformation->[$index]};
- $Str .= $data;
+ if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
+ $returnhash{$role} ++;
+ }
+ }
+ return %returnhash;
+}
+
+
+##############################################
+##############################################
- my @dataLength=split(//,$data);
- my $length=scalar @dataLength;
- $Str .= (' 'x($cache->{$studentInformation->[$index].'Length'}-
- $length));
- $Str .= $spacePadding;
+# topic_bar
+#
+# Generates a div containing a numbered (static image) followed by a title
+# with a background color defined in the corresponding CSS: LC_topic_bar
+#
+sub topic_bar {
+ my ($imgnum,$title) = @_;
+ return '
+
+
+ '.$title.'
+
+';
+}
+
+##############################################
+##############################################
+
+# echo_form_input
+#
+# Generates html markup to add form elements from the referrer page
+# as hidden form elements (values encoded) in the new page.
+#
+# Intended to support two types of use
+# (a) to allow backing up to earlier pages in a multi-page
+# form submission process using a breadcrumb trail.
+#
+# (b) to allow the current page to be reloaded with form elements
+# set on previous page to remain unchanged. An example would
+# be where the a page containing a dynamically-built table of data is
+# is to be redisplayed, with only the sort order of the data changed.
+#
+# Inputs:
+# 1. Reference to array of form elements in the submitted form on
+# the referrer page which are to be excluded from the echoed elements.
+#
+# 2. Reference to array of regular expressions, which if matched in the
+# name of the form element n the referrer page will be omitted from echo.
+#
+# Outputs: A scalar containing the html markup for the echoed form
+# elements (all as hidden elements, with values encoded).
+
+
+sub echo_form_input {
+ my ($excluded,$regexps) = @_;
+ my $output = '';
+ foreach my $key (keys(%env)) {
+ if ($key =~ /^form\.(.+)$/) {
+ my $name = $1;
+ my $match = 0;
+ if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
+ if (defined($regexps)) {
+ if (@{$regexps} > 0) {
+ foreach my $regexp (@{$regexps}) {
+ if ($name =~ /\Q$regexp\E/) {
+ $match = 1;
+ last;
+ }
+ }
+ }
+ }
+ if (!$match) {
+ if (ref($env{$key})) {
+ foreach my $value (@{$env{$key}}) {
+ $value = &HTML::Entities::encode($value,'<>&"');
+ $output .= ''."\n";
+ }
+ } else {
+ my $value = &HTML::Entities::encode($env{$key},'<>&"');
+ $output .= ''."\n";
+ }
+ }
+ }
+ }
}
+ return $output;
+}
- return $Str;
+##############################################
+##############################################
+
+# set_form_elements
+#
+# Generates javascript to set form elements to values based on
+# corresponding values for the same form elements when the page was
+# previously submitted.
+#
+# Last submission values are read from hidden form elements in referring
+# page which have the same name, i.e., generated by &echo_form_input().
+#
+# Intended to be called by onload event.
+#
+# Inputs:
+# (a) Reference to hash of echoed form elements to be set.
+#
+# In the hash, keys are the form element names, and the values are the
+# element type (selectbox, radio, checkbox or text -for textbox, textarea or
+# hidden).
+#
+# (b) Optional reference to hash of stored elements to be set.
+#
+# If the page being displayed is a page which permits modification of
+# previously stored data, e.g., the first page in a multi-page submission,
+# then if stored is supplied, form elements will be set to the last stored
+# values. If user supplied values are also available for the same elements
+# these will replace the stored values.
+#
+# Output:
+#
+# javascript function - set_form_elements() which sets form elements,
+# expects an argument: formname - the name of the form according to
+# the DOM, e.g., document.compose
+
+sub set_form_elements {
+ my ($elements,$stored) = @_;
+ my %values;
+ my $output .= 'function setFormElements(courseForm) {
+';
+ if (defined($stored)) {
+ foreach my $name (keys(%{$stored})) {
+ if (exists($$elements{$name})) {
+ if (ref($$stored{$name}) eq 'ARRAY') {
+ $values{$name} = $$stored{$name};
+ } else {
+ @{$values{$name}} = ($$stored{$name});
+ }
+ }
+ }
+ }
+
+ foreach my $key (keys(%env)) {
+ if ($key =~ /^form\.(.+)$/) {
+ my $name = $1;
+ if (exists($$elements{$name})) {
+ @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
+ }
+ }
+ }
+
+ foreach my $name (keys(%values)) {
+ for (my $i=0; $i<@{$values{$name}}; $i++) {
+ $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
+ $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
+ $values{$name}[$i] =~ s/"/\\"/g;
+ }
+ if ($$elements{$name} eq 'text') {
+ my $numvalues = @{$values{$name}};
+ if ($numvalues > 1) {
+ my $valuestring = join('","',@{$values{$name}});
+ $output .= qq|
+ var textvalues = new Array ("$valuestring");
+ var total = courseForm.elements['$name'].length;
+ if (total > $numvalues) {
+ total = $numvalues;
+ }
+ for (var i=0; i= 0) {
+ return true;
+ }
+ return false;
+ }
+}
+END
+ return $scripttag;
+}
+
+##############################################
+##############################################
+
+# generate_menu
+#
+# Generates html markup for a menu.
+#
+# Inputs:
+# An array of following structure:
+# ({ categorytitle => 'Categorytitle',
+# items => [
+# { linktext => 'Text to be displayed',
+# url => 'URL the link is pointing to, i.e. /adm/site?action=dosomething',
+# permission => 'Contains permissions as returned from lonnet::allowed(),
+# must evaluate to true in order to activate the link',
+# icon => 'icon filename',
+# alttext => 'alt text for the icon',
+# help => 'Name of the corresponding helpfile',
+# linktitle => 'Description of the link (used for title tag)'
+# },
+# ...
+# ]
+# },
+# ...
+# )
+#
+# Outputs: A scalar containing the html markup for the menu.
+
+# ---- Remove when done ----
+# This routine is part of the redesign of LON-CAPA and it's
+# subject to change during this project.
+# Don't rely on its current functionality as it might be
+# changed or removed.
+# TODO:
+# check for empty values
+# --------------------------
+
+sub generate_menu {
+ my @menu = @_;
+ my $menu_html = qq|
|;
+
+ foreach my $category (@menu) { #FIXME: insert appropriate classnames for styles when they're finished.
+ $menu_html .='
+
'.mt($category->{'categorytitle'}).'
+
';
+ foreach my $item ( @{ $category->{items} } ) {
+ next unless $item->{'permission'};
+ $menu_html .= qq|