--- loncom/interface/loncommon.pm 2006/04/20 02:24:08 1.344
+++ loncom/interface/loncommon.pm 2006/05/30 12:46:08 1.376
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# a pile of common routines
#
-# $Id: loncommon.pm,v 1.344 2006/04/20 02:24:08 albertel Exp $
+# $Id: loncommon.pm,v 1.376 2006/05/30 12:46:08 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -64,6 +64,7 @@ use HTML::Entities;
use Apache::lonhtmlcommon();
use Apache::loncoursedata();
use Apache::lontexconvert();
+use LONCAPA;
my $readit;
@@ -76,7 +77,7 @@ my %language;
my %supported_language;
my %cprtag;
my %scprtag;
-my %fe; my %fd;
+my %fe; my %fd; my %fm;
my %category_extensions;
# ---------------------------------------------- Designs
@@ -107,10 +108,10 @@ BEGIN {
my $langtabfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/language.tab';
if ( open(my $fh,"<$langtabfile") ) {
- while (<$fh>) {
- next if /^\#/;
- chomp;
- my ($key,$two,$country,$three,$enc,$val,$sup)=(split(/\t/,$_));
+ 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;
@@ -124,24 +125,24 @@ BEGIN {
my $copyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
'/copyright.tab';
if ( open (my $fh,"<$copyrightfile") ) {
- while (<$fh>) {
- next if /^\#/;
- chomp;
- my ($key,$val)=(split(/\s+/,$_,2));
+ while (my $line = <$fh>) {
+ next if ($line=~/^\#/);
+ chomp($line);
+ my ($key,$val)=(split(/\s+/,$line,2));
$cprtag{$key}=$val;
}
close($fh);
}
}
-# ------------------------------------------------------------------ source copyrights
+# ----------------------------------------------------------- source copyrights
{
my $sourcecopyrightfile = $Apache::lonnet::perlvar{'lonIncludes'}.
'/source_copyright.tab';
if ( open (my $fh,"<$sourcecopyrightfile") ) {
- while (<$fh>) {
- next if /^\#/;
- chomp;
- my ($key,$val)=(split(/\s+/,$_,2));
+ while (my $line = <$fh>) {
+ next if ($line =~ /^\#/);
+ chomp($line);
+ my ($key,$val)=(split(/\s+/,$line,2));
$scprtag{$key}=$val;
}
close($fh);
@@ -159,10 +160,10 @@ BEGIN {
{
my $designfile = $designdir.'/'.$filename;
if ( open (my $fh,"<$designfile") ) {
- while (<$fh>) {
- next if /^\#/;
- chomp;
- my ($key,$val)=(split(/\=/,$_));
+ while (my $line = <$fh>) {
+ next if ($line =~ /^\#/);
+ chomp($line);
+ my ($key,$val)=(split(/\=/,$line));
if ($val) { $designhash{$domain.'.'.$key}=$val; }
}
close($fh);
@@ -178,10 +179,10 @@ BEGIN {
my $categoryfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/filecategories.tab';
if ( open (my $fh,"<$categoryfile") ) {
- while (<$fh>) {
- next if /^\#/;
- chomp;
- my ($extension,$category)=(split(/\s+/,$_,2));
+ while (my $line = <$fh>) {
+ next if ($line =~ /^\#/);
+ chomp($line);
+ my ($extension,$category)=(split(/\s+/,$line,2));
push @{$category_extensions{lc($category)}},$extension;
}
close($fh);
@@ -193,13 +194,14 @@ BEGIN {
my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/filetypes.tab';
if ( open (my $fh,"<$typesfile") ) {
- while (<$fh>) {
- next if (/^\#/);
- chomp;
- my ($ending,$emb,$descr)=split(/\s+/,$_,3);
+ 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);
@@ -706,8 +708,9 @@ sub help_open_menu {
my $origurl = $ENV{'REQUEST_URI'};
$origurl=~s|^/~|/priv/|;
my $timestamp = time;
- foreach (\$color,\$function,\$topic,\$component_help,\$faq,\$bug,\$origurl) {
- $$_ = &Apache::lonnet::escape($$_);
+ foreach my $datum (\$color,\$function,\$topic,\$component_help,\$faq,
+ \$bug,\$origurl) {
+ $$datum = &escape($$datum);
}
if (!$stayOnPage) {
$link = "javascript:helpMenu('open')";
@@ -773,7 +776,7 @@ ENDTEMPLATE
$width,$height).' '.$template;
} else {
my $help_text;
- $help_text=&Apache::lonnet::unescape($topic);
+ $help_text=&unescape($topic);
$template='
';
+ }
+}
+
###############################################
=pod
@@ -3435,7 +3709,7 @@ specific user. Roles can be active, prev
Inputs:
user's domain, user's username, course's domain,
-course's number, optional section/group.
+course's number, optional section ID.
Outputs:
role status: active, previous or future.
@@ -3486,32 +3760,40 @@ sub check_user_status {
Determines all the sections for a course including
sections with students and sections containing other roles.
-Incoming parameters: domain, course number, reference to
-section hash (keys to be section/group IDs), reference to
-array containing roles for which sections should be gathered
-(optional). If the fourth argument is undefined, sections
-are gathered for any role.
+Incoming parameters: domain, course number,
+reference to array containing roles for which sections should
+be gathered (optional). If the third argument is undefined,
+sections are gathered for any role.
-Returns number of sections.
+Returns section hash (keys are section IDs, values are
+number of users in each section), subject to the
+optional roles filter.
=cut
###############################################
sub get_sections {
- my ($cdom,$cnum,$sectioncount,$possible_roles) = @_;
- if (!($cdom && $cnum)) { return 0; }
- my $numsections = 0;
+ my ($cdom,$cnum,$possible_roles) = @_;
+ if (!defined($cdom) || !defined($cnum)) {
+ my $cid = $env{'request.course.id'};
+
+ return if (!defined($cid));
- if (!defined($possible_roles) || (grep/^st$/,@$possible_roles)) {
+ $cdom = $env{'course.'.$cid.'.domain'};
+ $cnum = $env{'course.'.$cid.'.num'};
+ }
+
+ my %sectioncount;
+
+ if (!defined($possible_roles) || (grep(/^st$/,@$possible_roles))) {
my ($classlist) = &Apache::loncoursedata::get_classlist($cdom,$cnum);
my $sec_index = &Apache::loncoursedata::CL_SECTION();
my $status_index = &Apache::loncoursedata::CL_STATUS();
- while (my ($student,$data) = each %$classlist) {
+ while (my ($student,$data) = each(%$classlist)) {
my ($section,$status) = ($data->[$sec_index],
$data->[$status_index]);
unless ($section eq '-1' || $section =~ /^\s*$/) {
- if (!defined($$sectioncount{$section})) { $numsections++; }
- $$sectioncount{$section}++;
+ $sectioncount{$section}++;
}
}
}
@@ -3527,157 +3809,9 @@ sub get_sections {
}
if ($user =~ /^$role:[^:]*:[^:]*:(\w+)/) { $section=$1; }
if (!defined($section) || $section eq '-1') { next; }
- if (!defined($$sectioncount{$section})) { $numsections++; }
- $$sectioncount{$section}++;
+ $sectioncount{$section}++;
}
- return $numsections;
-}
-
-###############################################
-
-=pod
-
-=item coursegroups
-
-Retrieve information about groups in a course,
-
-Input:
-1. Reference to hash to populate with group information.
-2. Optional course domain
-3. Optional course number
-4. Optional group name
-
-Course domain and number will be taken from user's
-environment if not supplied. Optional group name will'
-be passed to lonnet::get_coursegroups() as a regexp to
-use in the call to the dump function.
-
-Output
-Returns number of groups in the course (subject to the
-optional group name filter).
-
-Side effects:
-Populates the referenced curr_groups hash, with key,
-value pairs. Keys are group names, corresponding values
-are scalars containing group information in XML. This
-can be sent to &get_group_settings() to be parsed.
-
-=cut
-
-###############################################
-
-sub coursegroups {
- my ($curr_groups,$cdom,$cnum,$group) = @_;
- my $numgroups;
- if (!defined($cdom) || !defined($cnum)) {
- my $cid = $env{'request.course.id'};
- $cdom = $env{'course.'.$cid.'.domain'};
- $cnum = $env{'course.'.$cid.'.num'};
- }
- %{$curr_groups} = &Apache::lonnet::get_coursegroups($cdom,$cnum,$group);
- my ($tmp) = keys(%{$curr_groups});
- if ($tmp=~/^error:/) {
- unless ($tmp eq 'error: 2 tie(GDBM) Failed while attempting dump') {
- &logthis('Error retrieving groups: '.$tmp.' in '.$cnum.':'.
- $cdom);
- }
- $numgroups = 0;
- } else {
- $numgroups = keys(%{$curr_groups});
- }
- return $numgroups;
-}
-
-###############################################
-
-=pod
-
-=item get_group_settings
-
-Uses TokeParser to extract group information from the
-XML used to describe course groups.
-
-Input:
-Scalar containing XML - as retrieved from &coursegroups().
-
-Output:
-Hash containing group information as key=values for (a), and
-hash of hashes for (b)
-
-Keys (in two categories):
-(a) groupname, creator, creation, modified, startdate,enddate.
-Corresponding values are name of the group, creator of the group
-(username:domain), UNIX time for date group was created, and
-settings were last modified, and default start and end access
-times for group members.
-
-(b) functions returned in hash of hashes.
-Outer hash key is functions.
-Inner hash keys are chat,discussion,email,files,homepage,roster.
-Corresponding values are either on or off, depending on
-whether this type of functionality is available for the group.
-
-=cut
-
-###############################################
-
-sub get_group_settings {
- my ($groupinfo)=@_;
- my $parser=HTML::TokeParser->new(\$groupinfo);
- my $token;
- my $tool = '';
- my $role = '';
- my %content=();
- while ($token=$parser->get_token) {
- if ($token->[0] eq 'S') {
- my $entry=$token->[1];
- if ($entry eq 'functions' || $entry eq 'autosec') {
- %{$content{$entry}} = ();
- $tool = $entry;
- } elsif ($entry eq 'role') {
- if ($tool eq 'autosec') {
- $role = $token->[2]{id};
- }
- } else {
- my $value=$parser->get_text('/'.$entry);
- if ($entry eq 'name') {
- if ($tool eq 'functions') {
- my $function = $token->[2]{id};
- $content{$tool}{$function} = $value;
- }
- } elsif ($entry eq 'groupname') {
- $content{$entry}=&Apache::lonnet::unescape($value);
- } elsif (($entry eq 'roles') || ($entry eq 'types') ||
- ($entry eq 'sectionpick') || ($entry eq 'defpriv')) {
- push(@{$content{$entry}},$value);
- } elsif ($entry eq 'section') {
- if ($tool eq 'autosec' && $role ne '') {
- push(@{$content{$tool}{$role}},$value);
- }
- } else {
- $content{$entry}=$value;
- }
- }
- } elsif ($token->[0] eq 'E') {
- if ($token->[1] eq 'functions' || $token->[1] eq 'autosec') {
- $tool = '';
- } elsif ($token->[1] eq 'role') {
- $role = '';
- }
-
- }
- }
- return %content;
-}
-
-sub check_group_access {
- my ($group) = @_;
- my $access = 1;
- my $now = time;
- my ($start,$end) = split(/\./,$env{'user.role.gr/'.$env{'request.course,id'}.'/'.$group});
- if (($end!=0) && ($end<$now)) { $access = 0; }
- if (($start!=0) && ($start>$now)) { $access=0; }
- return $access;
+ return %sectioncount;
}
###############################################
@@ -3831,9 +3965,9 @@ will result in $env{'form.uname'} and $e
sub get_unprocessed_cgi {
my ($query,$possible_names)= @_;
# $Apache::lonxml::debug=1;
- foreach (split(/&/,$query)) {
- my ($name, $value) = split(/=/,$_);
- $name = &Apache::lonnet::unescape($name);
+ foreach my $pair (split(/&/,$query)) {
+ my ($name, $value) = split(/=/,$pair);
+ $name = &unescape($name);
if (!defined($possible_names) || (grep {$_ eq $name} @$possible_names)) {
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
@@ -4049,8 +4183,7 @@ sub record_sep {
if ($env{'form.upfiletype'} eq 'xml') {
} elsif ($env{'form.upfiletype'} eq 'space') {
my $i=0;
- foreach (split(/\s+/,$record)) {
- my $field=$_;
+ foreach my $field (split(/\s+/,$record)) {
$field=~s/^(\"|\')//;
$field=~s/(\"|\')$//;
$components{&takeleft($i)}=$field;
@@ -4058,8 +4191,7 @@ sub record_sep {
}
} elsif ($env{'form.upfiletype'} eq 'tab') {
my $i=0;
- foreach (split(/\t/,$record)) {
- my $field=$_;
+ foreach my $field (split(/\t/,$record)) {
$field=~s/^(\"|\')//;
$field=~s/(\"|\')$//;
$components{&takeleft($i)}=$field;
@@ -4153,14 +4285,14 @@ sub csv_print_samples {
my $samples = &get_samples($records,3);
$r->print(&mt('Samples').'