# The LearningOnline Network with CAPA
# a pile of common routines
#
# $Id: loncommon.pm,v 1.556 2007/07/30 00:31:28 raeburn 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/
#
# Makes a table out of the previous attempts
# Inputs result_from_symbread, user, domain, course_id
# Reads in non-network-related .tab files
# POD header:
=pod
=head1 NAME
Apache::loncommon - pile of common routines
=head1 SYNOPSIS
Common routines for manipulating connections, student answers,
domains, common Javascript fragments, etc.
=head1 OVERVIEW
A collection of commonly used subroutines that don't have a natural
home anywhere else. This collection helps remove
redundancy from other modules and increase efficiency of memory usage.
=cut
# End of POD header
package Apache::loncommon;
use strict;
use Apache::lonnet;
use GDBM_File;
use POSIX qw(strftime mktime);
use Apache::lonmenu();
use Apache::lonenc();
use Apache::lonlocal;
use HTML::Entities;
use Apache::lonhtmlcommon();
use Apache::loncoursedata();
use Apache::lontexconvert();
use Apache::lonclonecourse();
use LONCAPA qw(:DEFAULT :match);
# ---------------------------------------------- Designs
use vars qw(%defaultdesign);
my $readit;
##
## Global Variables
##
# ----------------------------------------------- Filetypes/Languages/Copyright
my %language;
my %supported_language;
my %cprtag;
my %scprtag;
my %fe; my %fd; my %fm;
my %category_extensions;
# ---------------------------------------------- Thesaurus variables
#
# %Keywords:
# A hash used by &keyword to determine if a word is considered a keyword.
# $thesaurus_db_file
# Scalar containing the full path to the thesaurus database.
my %Keywords;
my $thesaurus_db_file;
#
# Initialize values from language.tab, copyright.tab, filetypes.tab,
# thesaurus.tab, and filecategories.tab.
#
BEGIN {
# Variable initialization
$thesaurus_db_file = $Apache::lonnet::perlvar{'lonTabDir'}."/thesaurus.db";
#
unless ($readit) {
# ------------------------------------------------------------------- languages
{
my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/language.tab';
if ( open(my $fh,"<$langtabfile") ) {
while (my $line = <$fh>) {
next if ($line=~/^\#/);
chomp($line);
my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$line));
$language{$key}=$val.' - '.$enc;
if ($sup) {
$supported_language{$key}=$sup;
}
}
close($fh);
}
}
# ------------------------------------------------------------------ copyrights
{
my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
'/copyright.tab';
if ( open (my $fh,"<$copyrightfile") ) {
while (my $line = <$fh>) {
next if ($line=~/^\#/);
chomp($line);
my ($key,$val)=(split(/\s+/,$line,2));
$cprtag{$key}=$val;
}
close($fh);
}
}
# ----------------------------------------------------------- source copyrights
{
my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
'/source_copyright.tab';
if ( open (my $fh,"<$sourcecopyrightfile") ) {
while (my $line = <$fh>) {
next if ($line =~ /^\#/);
chomp($line);
my ($key,$val)=(split(/\s+/,$line,2));
$scprtag{$key}=$val;
}
close($fh);
}
}
# -------------------------------------------------------------- default domain designs
my $designdir=$Apache::lonnet::perlvar{'lonTabDir'}.'/lonDomColors';
my $designfile = $designdir.'/default.tab';
if ( open (my $fh,"<$designfile") ) {
while (my $line = <$fh>) {
next if ($line =~ /^\#/);
chomp($line);
my ($key,$val)=(split(/\=/,$line));
if ($val) { $defaultdesign{$key}=$val; }
}
close($fh);
}
# ------------------------------------------------------------- file categories
{
my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/filecategories.tab';
if ( open (my $fh,"<$categoryfile") ) {
while (my $line = <$fh>) {
next if ($line =~ /^\#/);
chomp($line);
my ($extension,$category)=(split(/\s+/,$line,2));
push @{$category_extensions{lc($category)}},$extension;
}
close($fh);
}
}
# ------------------------------------------------------------------ file types
{
my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/filetypes.tab';
if ( open (my $fh,"<$typesfile") ) {
while (my $line = <$fh>) {
next if ($line =~ /^\#/);
chomp($line);
my ($ending,$emb,$mime,$descr)=split(/\s+/,$line,4);
if ($descr ne '') {
$fe{$ending}=lc($emb);
$fd{$ending}=$descr;
if ($mime ne 'unk') { $fm{$ending}=$mime; }
}
}
close($fh);
}
}
&Apache::lonnet::logthis(
"INFO: Read file types");
$readit=1;
} # end of unless($readit)
}
###############################################################
## HTML and Javascript Helper Functions ##
###############################################################
=pod
=head1 HTML and Javascript Functions
=over 4
=item * browser_and_searcher_javascript ()
XXReturns a string
containing javascript with two functions, C and
C. Returned string does not contain EscriptE
tags.
=item * openbrowser(formname,elementname,only,omit) [javascript]
inputs: formname, elementname, only, omit
formname and elementname indicate the name of the html form and name of
the element that the results of the browsing selection are to be placed in.
Specifying 'only' will restrict the browser to displaying only files
with the given extension. Can be a comma separated list.
Specifying 'omit' will restrict the browser to NOT displaying files
with the given extension. Can be a comma separated list.
=item * opensearcher(formname, elementname) [javascript]
Inputs: formname, elementname
formname and elementname specify the name of the html form and the name
of the element the selection from the search results will be placed in.
=cut
sub browser_and_searcher_javascript {
my ($mode)=@_;
if (!defined($mode)) { $mode='edit'; }
my $resurl=&escape_single(&lastresurl());
return <
END
}
sub lastresurl {
if ($env{'environment.lastresurl'}) {
return $env{'environment.lastresurl'}
} else {
return '/res';
}
}
sub storeresurl {
my $resurl=&Apache::lonnet::clutter(shift);
unless ($resurl=~/^\/res/) { return 0; }
$resurl=~s/\/$//;
&Apache::lonnet::put('environment',{'lastresurl' => $resurl});
&Apache::lonnet::appenv('environment.lastresurl' => $resurl);
return 1;
}
sub studentbrowser_javascript {
unless (
(($env{'request.course.id'}) &&
(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
'/'.$env{'request.course.sec'})
))
|| ($env{'request.role'}=~/^(au|dc|su)/)
) { return ''; }
return (<<'ENDSTDBRW');
ENDSTDBRW
}
sub selectstudent_link {
my ($form,$unameele,$udomele)=@_;
if ($env{'request.course.id'}) {
if (!&Apache::lonnet::allowed('srm',$env{'request.course.id'})
&& !&Apache::lonnet::allowed('srm',$env{'request.course.id'}.
'/'.$env{'request.course.sec'})) {
return '';
}
return "".&mt('Select User')."";
}
if ($env{'request.role'}=~/^(au|dc|su)/) {
return "".&mt('Select User')."";
}
return '';
}
sub coursebrowser_javascript {
my ($domainfilter,$sec_element,$formname)=@_;
my $crs_or_grp_alert = &mt('Please select the type of LON-CAPA entity - Course or Group - for which you wish to add/modify a user role');
my $output = '
';
return $output;
}
sub setsec_javascript {
my ($sec_element,$formname) = @_;
my $setsections = qq|
function setSect(sectionlist) {
var sectionsArray = sectionlist.split(",");
var numSections = sectionsArray.length;
document.$formname.$sec_element.length = 0;
if (numSections == 0) {
document.$formname.$sec_element.multiple=false;
document.$formname.$sec_element.size=1;
document.$formname.$sec_element.options[0] = new Option('No existing sections','',false,false)
} else {
if (numSections == 1) {
document.$formname.$sec_element.multiple=false;
document.$formname.$sec_element.size=1;
document.$formname.$sec_element.options[0] = new Option('Select','',true,true);
document.$formname.$sec_element.options[1] = new Option('No section','',false,false)
document.$formname.$sec_element.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
} else {
for (var i=0; i".&mt('Select Course')."";
}
sub check_uncheck_jscript {
my $jscript = <<"ENDSCRT";
function checkAll(field) {
if (field.length > 0) {
for (i = 0; i < field.length; i++) {
field[i].checked = true ;
}
} else {
field.checked = true
}
}
function uncheckAll(field) {
if (field.length > 0) {
for (i = 0; i < field.length; i++) {
field[i].checked = false ;
}
} else {
field.checked = false ;
}
}
ENDSCRT
return $jscript;
}
=pod
=item * linked_select_forms(...)
linked_select_forms returns a string containing a block
and html for two