--- loncom/interface/loncommon.pm 2006/04/25 20:48:38 1.354
+++ loncom/interface/loncommon.pm 2006/05/17 23:25:56 1.374
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# a pile of common routines
#
-# $Id: loncommon.pm,v 1.354 2006/04/25 20:48:38 albertel Exp $
+# $Id: loncommon.pm,v 1.374 2006/05/17 23:25:56 raeburn 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;
@@ -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,10 +125,10 @@ 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);
@@ -138,10 +139,10 @@ BEGIN {
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,10 +194,10 @@ BEGIN {
my $typesfile = $Apache::lonnet::perlvar{'lonTabDir'}.
'/filetypes.tab';
if ( open (my $fh,"<$typesfile") ) {
- while (<$fh>) {
- next if (/^\#/);
- chomp;
- my ($ending,$emb,$mime,$descr)=split(/\s+/,$_,4);
+ 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;
@@ -707,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')";
@@ -774,7 +776,7 @@ ENDTEMPLATE
$width,$height).' '.$template;
} else {
my $help_text;
- $help_text=&Apache::lonnet::unescape($topic);
+ $help_text=&unescape($topic);
$template='
';
+ }
}
###############################################
@@ -3684,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));
+
+ $cdom = $env{'course.'.$cid.'.domain'};
+ $cnum = $env{'course.'.$cid.'.num'};
+ }
+
+ my %sectioncount;
- if (!defined($possible_roles) || (grep/^st$/,@$possible_roles)) {
+ 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}++;
}
}
}
@@ -3725,10 +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;
+ return %sectioncount;
}
###############################################
@@ -3740,10 +3823,9 @@ sub get_sections {
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
+1. Optional course domain
+2. Optional course number
+3. Optional group name
Course domain and number will be taken from user's
environment if not supplied. Optional group name will'
@@ -3751,39 +3833,37 @@ be passed to lonnet::get_coursegroups()
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
+Returns hash of groups in the course (subject to the
+optional group name filter). In the hash, the keys are
+group names, and their corresponding values
are scalars containing group information in XML. This
-can be sent to &get_group_settings() to be parsed.
+can be sent to &get_group_settings() to be parsed.
+Side effects:
+None.
=cut
###############################################
sub coursegroups {
- my ($curr_groups,$cdom,$cnum,$group) = @_;
- my $numgroups;
+ my ($cdom,$cnum,$group) = @_;
if (!defined($cdom) || !defined($cnum)) {
my $cid = $env{'request.course.id'};
+
+ return if (!defined($cid));
+
$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});
+ my %curr_groups = &Apache::lonnet::get_coursegroups($cdom,$cnum,$group);
+ my ($tmp) = keys(%curr_groups);
+ if ($tmp=~/^(con_lost|no_such_host|error: [^2] )/) {
+ undef(%curr_groups);
+ &logthis('Error retrieving groups: '.$tmp.' in '.$cnum.':'.$cdom);
+ } elsif ($tmp=~/^error: 2 /) {
+ undef(%curr_groups);
}
- return $numgroups;
+ return %curr_groups;
}
###############################################
@@ -3835,6 +3915,7 @@ sub get_group_settings {
} elsif ($entry eq 'role') {
if ($tool eq 'autosec') {
$role = $token->[2]{id};
+ @{$content{$tool}{$role}} = ();
}
} else {
my $value=$parser->get_text('/'.$entry);
@@ -3844,7 +3925,7 @@ sub get_group_settings {
$content{$tool}{$function} = $value;
}
} elsif ($entry eq 'groupname') {
- $content{$entry}=&Apache::lonnet::unescape($value);
+ $content{$entry}=&unescape($value);
} elsif (($entry eq 'roles') || ($entry eq 'types') ||
($entry eq 'sectionpick') || ($entry eq 'defpriv')) {
push(@{$content{$entry}},$value);
@@ -4029,9 +4110,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;
@@ -4247,8 +4328,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;
@@ -4256,8 +4336,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;
@@ -4351,14 +4430,14 @@ sub csv_print_samples {
my $samples = &get_samples($records,3);
$r->print(&mt('Samples').'