--- loncom/interface/lonstatistics.pm	2002/07/25 19:30:24	1.31
+++ loncom/interface/lonstatistics.pm	2002/07/26 16:22:09	1.33
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # (Publication Handler
 #
-# $Id: lonstatistics.pm,v 1.31 2002/07/25 19:30:24 minaeibi Exp $
+# $Id: lonstatistics.pm,v 1.33 2002/07/26 16:22:09 stredwic Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -94,13 +94,20 @@ sub ProcessFormData{
     } else {
         $cache->{'StudentAssessmentMove'} = 'selected';
     }
-    &CheckFormElement($cache, 'StudentAssessmentMap', 'StudentAssessmentMap', 
-                      'All Maps');
     &CheckFormElement($cache, 'StudentAssessmentStudent', 
                       'StudentAssessmentStudent', 'All Students');
     $cache->{'StudentAssessmentStudent'} = 
         &Apache::lonnet::unescape($cache->{'StudentAssessmentStudent'});
 
+    if(defined($ENV{'form.Section'})) {
+        my @sectionsSelected = (ref($ENV{'form.Section'}) ?
+                               @{$ENV{'form.Section'}} :
+                                ($ENV{'form.Section'}));
+        $cache->{'sectionsSelected'} = join(':', @sectionsSelected);
+    } elsif(!defined($cache->{'sectionsSelected'})) {
+        $cache->{'sectionsSelected'} = $cache->{'sectionList'};
+    }
+
     foreach (keys(%ENV)) {
         if(/form\.Analyze:::/) {
 #            $cache->{'reportSelected'} = 'Analyze';
@@ -195,8 +202,57 @@ sub SortStudents {
     return \@order;
 }
 
+=pod
+
+=item &SpaceColumns()
+
+Determines the width of all the columns in the chart.  It is based on
+the max of the data for that column and its header.
+
+=over 4
+
+Input: $students, $studentInformation, $headings, $ChartDB
+
+$students: An array pointer to a list of students (username:domain)
+
+$studentInformatin: The type of data for the student information.  It is
+used as part of the key in $CacheData.
+
+$headings: The name of the student information columns.
+
+$ChartDB: The name of the cache database which is opened for read/write.
+
+Output: None - All data stored in cache.
+
+=back
+
+=cut
+
+sub SpaceColumns {
+    my ($students,$studentInformation,$headings,$cache)=@_;
+
+    # Initialize Lengths
+    for(my $index=0; $index<(scalar @$headings); $index++) {
+        my @titleLength=split(//,$headings->[$index]);
+        $cache->{$studentInformation->[$index].':columnWidth'}=
+            scalar @titleLength;
+    }
+
+    foreach my $name (@$students) {
+        foreach (@$studentInformation) {
+            my @dataLength=split(//,$cache->{$name.':'.$_});
+            my $length=(scalar @dataLength);
+            if($length > $cache->{$_.':columnWidth'}) {
+                $cache->{$_.':columnWidth'}=$length;
+            }
+        }
+    }
+
+    return;
+}
+
 sub PrepareData {
-    my ($c, $cacheDB)=@_;
+    my ($c, $cacheDB, $studentInformation, $headings)=@_;
 
     # Test for access to the cache data
     my $courseID=$ENV{'request.course.id'};
@@ -255,6 +311,8 @@ sub PrepareData {
 
     &ProcessFormData(\%cache);
     my $students = &SortStudents(\%cache);
+    &SpaceColumns($students, $studentInformation, $headings, \%cache);
+    $cache{'updateTime:columnWidth'}=24;
 
     if($cache{'download'} ne 'false') {
         my $who = $cache{'download'};
@@ -291,11 +349,6 @@ sub PrepareData {
         return 'aborted'; 
     }
 
-    if($c->aborted()) {
-        untie(%cache);
-        return 'aborted'; 
-    }
-
     untie(%cache);
 
     return ('OK', $students);
@@ -359,7 +412,7 @@ sub setbgcolor {
 }
 
 sub BuildClasslist {
-    my ($cacheDB,$students,$studentInformation,$headings,$spacePadding)=@_;
+    my ($cacheDB,$students,$studentInformation,$headings)=@_;
 
     my %cache;
     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER,0640)) {
@@ -372,51 +425,42 @@ sub BuildClasslist {
 
     my $displayString = '<td align="left"><a href="/adm/statistics?';
     $displayString .= 'sort=LINKDATA">DISPLAYDATA&nbsp</a></td>'."\n";
-    $Str .= &Apache::lonhtmlcommon::CreateStudentInformationHeadings(\%cache,
-                                                           $studentInformation,
-                                                           $headings,
-                                                           $displayString);
-    $Str .= '<td align="left">';
-    $Str .= '<a href="/adm/statistics?sort=lastDownloadTime">';
-    $Str .= 'Last Updated&nbsp</a></td>'."\n";
+    $Str .= &Apache::lonhtmlcommon::CreateHeadings(\%cache, $studentInformation,
+                                                   $headings, $displayString);
     $Str .= '</tr>'."\n";
     my $alternate=0;
     foreach (@$students) {
         my ($username, $domain) = split(':', $_);
         if($alternate) {
-            $Str .= '<tr bgcolor="#ffffe6"><td>';
+            $Str .= '<tr bgcolor="#ffffe6">';
         } else {
-            $Str .= '<tr bgcolor="#ffffc6"><td>';
+            $Str .= '<tr bgcolor="#ffffc6">';
         }
         $alternate = ($alternate + 1) % 2;
         foreach my $data (@$studentInformation) {
+            $Str .= '<td>';
             if($data eq 'fullname') {
                 $Str .= '<a href="/adm/statistics?reportSelected=';
                 $Str .= &Apache::lonnet::escape('Student Assessment');
                 $Str .= '&StudentAssessmentStudent=';
                 $Str .= &Apache::lonnet::escape($cache{$_.':'.$data}).'">';
-                #$Str .= 'Student Assessment'.'">';
-            }
-
-            $Str .= $cache{$_.':'.$data}.'&nbsp';
-
-            if($data eq 'fullname') {
+                $Str .= $cache{$_.':'.$data}.'&nbsp';
                 $Str .= '</a>';
+            } elsif($data eq 'updateTime') {
+                $Str .= '<a href="/adm/statistics?reportSelected=';
+                $Str .= &Apache::lonnet::escape('Class list');
+                $Str .= '&download='.$_.'">';
+                $Str .= $cache{$_.':'.$data}.'&nbsp';
+                $Str .= '&nbsp</a>';
+            } else {
+                $Str .= $cache{$_.':'.$data}.'&nbsp';
             }
 
-            $Str .= '</td><td>';
-        }
-
-        $Str .= '<a href="/adm/statistics?download='.$_.'">';
-        my $downloadTime = $cache{$_.':lastDownloadTime'};
-        if($downloadTime ne 'Not downloaded') {
-            $downloadTime = localtime($downloadTime);
+            $Str .= '</td>'."\n";
         }
-        $Str .= $downloadTime;
-
-        $Str .= '&nbsp</a></td></tr>'."\n";
     }
 
+    $Str .= '</tr>'."\n";
     $Str .= '</table></td></tr></table>'."\n";
 
     untie(%cache);
@@ -424,13 +468,53 @@ sub BuildClasslist {
     return $Str;
 }
 
+sub CreateMainMenu {
+    my ($status, $reports)=@_;
+
+    my $Str = '';
+
+    $Str .= '<table border="0"><tbody><tr>'."\n";
+    $Str .= '<td></td><td></td>'."\n";
+    $Str .= '<td align="center"><b>Analysis Reports:</b></td>'."\n";
+    $Str .= '<td align="center"><b>Student Status:</b></td></tr>'."\n";
+    $Str .= '<tr>'."\n";
+    $Str .= '<td align="center"><input type="submit" name="Refresh" ';
+    $Str .= 'value="Refresh" /></td>'."\n";
+    $Str .= '<td align="center"><input type="submit" name="DownloadAll" ';
+    $Str .= 'value="Update All Student Data" /></td>'."\n";
+    $Str .= '<td align="center">';
+    $Str .= '<select name="reportSelected" onchange="document.';
+    $Str .= 'Statistics.submit()">'."\n";
+
+    foreach (sort(keys(%$reports))) {
+        next if($_ eq 'reportSelected');
+        $Str .= '<option name="'.$_.'"';
+        if($reports->{'reportSelected'} eq $reports->{$_}) {
+            $Str .= ' selected=""';
+        }
+        $Str .= '>'.$reports->{$_}.'</option>'."\n";
+    }
+    $Str .= '</select></td>'."\n";
+
+    $Str .= '<td align="center">';
+    $Str .= &Apache::lonhtmlcommon::StatusOptions($status, 'Statistics');
+    $Str .= '</td>'."\n";
+
+    $Str .= '</tr></tbody></table>'."\n";
+    $Str .= '<hr>'."\n";
+
+    return $Str;
+}
+
 sub BuildStatistics {
     my ($r)=@_;
 
     my $c = $r->connection;
-    my @studentInformation=('fullname','section','id','domain','username');
-    my @headings=('Full Name', 'Section', 'PID', 'Domain', 'User Name');
-    my $spacePadding = '   ';
+    my @studentInformation=('fullname','section','id','domain','username',
+                            'updateTime');
+    my @headings=('Full Name', 'Section', 'PID', 'Domain', 'User Name',
+                  'Last Updated');
+    my $spacing = '   ';
     my %reports = ('classlist'          => 'Class list',
                    'problem_statistics' => 'Problem Statistics',
                    'student_assessment' => 'Student Assessment',
@@ -442,7 +526,9 @@ sub BuildStatistics {
                   "_$ENV{'user.domain'}_$courseID\_statistics.db";
 
     my %color=&setbgcolor(0);
-    my ($returnValue, $students) = &PrepareData($c, $cacheDB);
+    my ($returnValue, $students) = &PrepareData($c, $cacheDB, 
+                                                \@studentInformation, 
+                                                \@headings);
     if($returnValue ne 'OK') {
         $r->print('<html><body>'.$returnValue."\n".'</body></html>');
         return OK;
@@ -463,9 +549,7 @@ sub BuildStatistics {
         $r->print(&Apache::lonhtmlcommon::Title('LON-CAPA Statistics'));
         $r->print('<form name="Statistics" ');
         $r->print('method="post" action="/adm/statistics">');
-        $r->print(&Apache::lonhtmlcommon::CreateStatisticsMainMenu(
-                                                             $cache{'Status'}, 
-                                                             \%reports));
+        $r->print(&CreateMainMenu($cache{'Status'}, \%reports));
         untie(%cache);
     } else {
         $r->print('<html><body>Unable to tie database.</body></html>');
@@ -487,9 +571,13 @@ sub BuildStatistics {
     } elsif($GoToPage eq 'Student Assessment') {
         $r->print(
             &Apache::lonstudentassessment::BuildStudentAssessmentPage($cacheDB,
-                                                                     $students,
-                                                                     $courseID,
-                                                                     $c));
+                                                            $students,
+                                                            $courseID,
+                                                            'Statistics',
+                                                            \@headings,
+                                                            $spacing,
+                                                            \@studentInformation,
+                                                            $r, $c));
     } elsif($GoToPage eq 'Analyze') {
         $r->print(&Apache::lonproblemanalysis::BuildAnalyzePage($cacheDB, 
                                                                 $students, 
@@ -500,7 +588,7 @@ sub BuildStatistics {
         &Apache::lonproblemstatistics::BuildWrongGraph($r);
     } elsif($GoToPage eq 'Class list') {
         $r->print(&BuildClasslist($cacheDB, $students, \@studentInformation,
-                                  \@headings, $spacePadding));
+                                  \@headings));
     }
 
     $r->print('</form>'."\n");