--- loncom/interface/Attic/lonchart.pm 2002/06/28 21:12:46 1.46 +++ loncom/interface/Attic/lonchart.pm 2002/07/01 21:12:06 1.49 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # (Publication Handler # -# $Id: lonchart.pm,v 1.46 2002/06/28 21:12:46 stredwic Exp $ +# $Id: lonchart.pm,v 1.49 2002/07/01 21:12:06 stredwic Exp $ # # Copyright Michigan State University Board of Trustees # @@ -59,16 +59,20 @@ my $jr; # ----- FORMAT PRINT DATA ---------------------------------------------- sub FormatStudentInformation { - my ($cache,$name,$studentInformation,$spacePadding)=@_; + my ($cache,$name,$studentInformation,$reselected,$spacePadding)=@_; my $Str='
';
 
-    foreach (@$studentInformation) {
-	my $data=$cache->{$name.':'.$_};
+    for(my $index=0; $index<(scalar @$studentInformation); $index++) {
+        if(!&ShouldShowColumn($reselected, 'heading', $index)) {
+            next;
+        }
+	my $data=$cache->{$name.':'.$studentInformation->[$index]};
 	$Str .= $data;
 
 	my @dataLength=split(//,$data);
 	my $length=scalar @dataLength;
-	$Str .= (' 'x($cache->{$_.'Length'}-$length));
+	$Str .= (' 'x($cache->{$studentInformation->[$index].'Length'}-
+                      $length));
 	$Str .= $spacePadding;
     }
 
@@ -76,7 +80,7 @@ sub FormatStudentInformation {
 }
 
 sub FormatStudentData {
-    my ($name,$coid,$studentInformation,$spacePadding,$ChartDB)=@_;
+    my ($reselected,$name,$coid,$studentInformation,$spacePadding,$ChartDB)=@_;
     my ($sname,$sdom) = split(/\:/,$name);
     my $Str;
     my %CacheData;
@@ -87,7 +91,7 @@ sub FormatStudentData {
     # Handle Student information ------------------------------------------
     # Handle user data
     $Str=&FormatStudentInformation(\%CacheData, $name, $studentInformation, 
-                                   $spacePadding);
+                                   $reselected, $spacePadding);
 
     # Handle errors
     if($CacheData{$name.':error'} =~ /environment/) {
@@ -116,6 +120,10 @@ sub FormatStudentData {
     my $problemsSolved  = 0;
     my $numberOfParts   = 0;
     foreach my $sequence (split(/\:/,$CacheData{'orderedSequences'})) {
+        if(!&ShouldShowColumn($reselected, 'sequence', $sequence)) {
+            next;
+        }
+
 	my $characterCount=0;
 	foreach my $problemID (split(/\:/,$CacheData{$sequence.':problems'})) {
 	    my $problem = $CacheData{$problemID.':problem'};
@@ -215,10 +223,14 @@ sub FormatStudentData {
 }
 
 sub CreateTableHeadings {
-    my ($CacheData,$studentInformation,$headings,$spacePadding)=@_;
+    my ($CacheData,$studentInformation,$headings,$reselected,$spacePadding)=@_;
     my $Str='
';
 
     for(my $index=0; $index<(scalar @$headings); $index++) {
+        if(!&ShouldShowColumn($reselected, 'heading', $index)) {
+            next;
+        }
+
 	my $data=$$headings[$index];
 	$Str .= $data;
 
@@ -230,7 +242,12 @@ sub CreateTableHeadings {
     }
 
     foreach my $sequence (split(/\:/,$CacheData->{'orderedSequences'})) {
-	$Str .= $CacheData->{$sequence.':title'};
+        if(!&ShouldShowColumn($reselected, 'sequence', $sequence)) {
+            next;
+        }
+
+        my $name = $CacheData->{$sequence.':title'};
+	$Str .= $name;
 	my @titleLength=split(//,$CacheData->{$sequence.':title'});
 	my $leftover=$CacheData->{$sequence.':columnWidth'}-
                      (scalar @titleLength);
@@ -244,36 +261,90 @@ sub CreateTableHeadings {
     return $Str;
 }
 
-sub CreateColumnSelectors {
-    my ($CacheData,$studentInformation,$headings,$spacePadding)=@_;
-    my $Str='';
+sub CreateColumnSelectionBox {
+    my ($CacheData,$studentInformation,$headings,$reselected,$spacePadding)=@_;
 
-    $Str .= '
'."\n"; - $Str .= ''; - $Str .= '
'."\n"; - return $Str; + my $missing=0; + my $notThere='

Select column to view:

'; + my $name; + $notThere .= '   '; + $notThere .= '

'; + } else { + $notThere=''; + } + + return $notThere; +} + +sub CreateColumnSelectors { + my ($CacheData,$studentInformation,$headings,$reselected,$spacePadding)=@_; + + my $found=0; + my ($name, $length, $position); + my $present='
';
+    for(my $index=0; $index<(scalar @$headings); $index++) {
+        if(!&ShouldShowColumn($reselected, 'heading', $index)) {
+            next;
+        }
+        $name = $headings->[$index];
+        $length=$CacheData->{$$studentInformation[$index].'Length'};
+        $position=int($length/2);
+	$present .= (' 'x($position));
+        $present .= '';
+        $position+=2;
+	$present .= (' 'x($length-$position));
+	$present .= $spacePadding;
+        $found++;
     }
 
     foreach my $sequence (split(/\:/,$CacheData->{'orderedSequences'})) {
-	$Str .= $CacheData->{$sequence.':title'};
-	my @titleLength=split(//,$CacheData->{$sequence.':title'});
-	my $leftover=$CacheData->{$sequence.':columnWidth'}-
-                     (scalar @titleLength);
-	$Str .= (' 'x$leftover);
-	$Str .= $spacePadding;
+        if(!&ShouldShowColumn($reselected, 'sequence', $sequence)) {
+            next;
+        }
+        $name = $CacheData->{$sequence.':title'};
+        $length=$CacheData->{$sequence.':columnWidth'};
+        $position=int($length/2);
+	$present .= (' 'x($position));
+        $present .= '';
+        $position+=2;
+	$present .= (' 'x($length-$position));
+	$present .= $spacePadding;
+        $found++;
     }
 
-    return $Str;
+    if($found) {
+        $present .= '
'; + $present = $present; + } else { + $present = ''; + } + + return $present.''."\n";; } sub CreateForm { @@ -286,6 +357,12 @@ sub CreateForm { else { $OpSel1 = 'selected'; } my $Ptr = '
'."\n"; + $Ptr .= ''; + $Ptr .= "\n"; + $Ptr .= '   '; + $Ptr .= ''; + $Ptr .= "\n"; + $Ptr .= '

'; $Ptr .= ' Sort by:   '."\n"; $Ptr .= '   '; $Ptr .= ''."\n"; @@ -295,17 +372,11 @@ sub CreateForm { $Ptr .= ''."\n"; $Ptr .= '

'; $Ptr .= ' Student Status:   '."\n". + '   '. ' '."\n"; - $Ptr .= '

'; - $Ptr .= ''; - $Ptr .= "\n"; - $Ptr .= '   '; - $Ptr .= ''; - $Ptr .= "\n"; - $Ptr .= '
'."\n"; return $Ptr; } @@ -513,9 +584,12 @@ sub ProcessTopResourceMap { $hash{'title_'.$currentResourceID}; my $totalProblems=0; - foreach (split(/\:/,$CacheData{$currentSequence. + foreach my $currentProblem (split(/\:/, + $CacheData{$currentSequence. ':problems'})) { - foreach ($CacheData{$currentSequence.':'.$_.':parts'}) { + foreach (split(/\:/,$CacheData{$currentSequence.':'. + $currentProblem. + ':parts'})) { $totalProblems++; } } @@ -665,7 +739,8 @@ sub ProcessClassList { my %CacheData; if(tie(%CacheData,'GDBM_File',$ChartDB,&GDBM_WRCREAT,0640)) { foreach my $name (keys(%$classlist)) { - if($name =~ /\:section/ || $name =~ /\:studentInformation/) { + if($name =~ /\:section/ || $name =~ /\:studentInformation/ || + $name eq '') { next; } if($c->aborted()) { @@ -680,8 +755,6 @@ sub ProcessClassList { $name,$courseID,$c); } - $CacheData{'NamesOfStudents'}=join(":::",@names); -# $CacheData{'NamesOfStudents'}=&Apache::lonnet::arrayref2str(\@names); untie(%CacheData); } @@ -760,12 +833,10 @@ sub ProcessFullName { } sub SortStudents { - my ($CacheData)=@_; - my @students = split(/:::/,$CacheData->{'NamesOfStudents'}); -# my @students=&Apache::lonnet::str2array($CacheData->{'NamesOfStudents'}); + my ($students,$CacheData)=@_; my @sorted1Students=(); - foreach (@students) { + foreach (@$students) { my ($end,$start)=split(/\:/,$CacheData->{$_.':date'}); my $active=1; my $now=time; @@ -802,7 +873,7 @@ sub SortStudents { } my @order = (); - foreach my $key (sort keys(%sortData)) { + foreach my $key (sort(keys(%sortData))) { push (@order,$sortData{$key}); } @@ -855,6 +926,11 @@ sub ExtractStudentData { foreach my $key (keys (%$courseData)) { $CacheData{$name.':'.$key}=$courseData->{$key}; } + if(defined($CacheData{'NamesOfStudents'})) { + $CacheData{'NamesOfStudents'}.=':::'.$name; + } else { + $CacheData{'NamesOfStudents'}=$name; + } } untie(%CacheData); } @@ -862,6 +938,31 @@ sub ExtractStudentData { return; } +sub ShouldShowColumn { + my ($reselected,$type,$value)=@_; + + if($ENV{'form.sort'} eq 'Recalculate Chart') { + return 1; + } + + if(defined($ENV{'form.'.$type.$value})) { + return 1; + } + + return &CheckForStringInArray($reselected, $type.$value); +} + +sub CheckForStringInArray { + my ($inputArray,$checkString)=@_; + foreach (@$inputArray) { +# $jr->print('a:'.$_.' b:'.$checkString.'
'); + if($_ eq $checkString) { + return 1; + } + } + return 0; +} + # ----- END HELPER FUNCTIONS -------------------------------------------- sub BuildChart { @@ -879,6 +980,10 @@ sub BuildChart { my $cid=$ENV{'request.course.id'}; my $ChartDB = "/home/httpd/perl/tmp/$ENV{'user.name'}". "_$ENV{'user.domain'}_$cid\_chart.db"; + # $ENV{'form.domains'} can be either a scalar or an array reference. + # We need an array. + my @reselected = (ref($ENV{'form.reselect'}) ? @{$ENV{'form.reselect'}} + : ($ENV{'form.reselect'})); $isCached=&TestCacheData($ChartDB); if($isCached < 0) { @@ -888,6 +993,7 @@ sub BuildChart { } # Download class list information if not using cached data + my %CacheData; my @students=(); my @studentInformation=('username','domain','section','id','fullname'); my @headings=('User Name','Domain','Section','PID','Full Name'); @@ -911,27 +1017,40 @@ sub BuildChart { &SpaceColumns(\@students,\@studentInformation,\@headings, $ChartDB); if($c->aborted()) { return; } + } else { + if(!$c->aborted() && tie(%CacheData,'GDBM_File',$ChartDB, + &GDBM_READER,0640)) { + @students=split(/:::/,$CacheData{'NamesOfStudents'}); + } } # Sort students and print out table desciptive data - my %CacheData; if(tie(%CacheData,'GDBM_File',$ChartDB,&GDBM_READER,0640)) { - if(!$c->aborted()) { @students=&SortStudents(\%CacheData); } + if(!$c->aborted()) { @students=&SortStudents(\@students,\%CacheData); } if(!$c->aborted()) { $r->print(&CreateLegend()); } + if(!$c->aborted()) { $r->rflush(); } if(!$c->aborted()) { $r->print(&CreateForm()); } - if(!$c->aborted()) { $r->print('

'.(scalar @students). + if(!$c->aborted()) { $r->print(&CreateColumnSelectionBox( + \%CacheData, + \@studentInformation, + \@headings, + \@reselected, + $spacePadding)); } + if(!$c->aborted()) { $r->print('

'.(scalar @students). ' students

'); } - if(!$c->aborted()) { $r->rflush(); } -# if(!$c->aborted()) { $r->print(&CreateColumnSelectors( -# \%CacheData, -# \@studentInformation, -# \@headings, -# $spacePadding)); } + if(!$c->aborted()) { $r->print(&CreateColumnSelectors( + \%CacheData, + \@studentInformation, + \@headings, + \@reselected, + $spacePadding)); } if(!$c->aborted()) { $r->print(&CreateTableHeadings( \%CacheData, \@studentInformation, \@headings, + \@reselected, $spacePadding)); } + if(!$c->aborted()) { $r->rflush(); } untie(%CacheData); } else { $r->print("Init2: Unable to tie hash to db file"); @@ -958,7 +1077,8 @@ sub BuildChart { push(@updateStudentList, $_); &ExtractStudentData($courseData, $_, $ChartDB); } - $r->print(&FormatStudentData($_, $cid, \@studentInformation, + $r->print(&FormatStudentData(\@reselected, $_, $cid, + \@studentInformation, $spacePadding, $ChartDB)); $r->rflush(); }