File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.102: download - view: text, annotated - select for diffs
Tue Jul 13 15:03:34 2004 UTC (19 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: version_1_2_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, HEAD
Make total sum adjust to width of total.

# The LearningOnline Network with CAPA
#
# $Id: lonstudentassessment.pm,v 1.102 2004/07/13 15:03:34 matthew 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/
#
# (Navigate problems for statistical reports
#
#######################################################
#######################################################

=pod

=head1 NAME

lonstudentassessment

=head1 SYNOPSIS

Presents assessment data about a student or a group of students.

=head1 Subroutines

=over 4 

=cut

#######################################################
#######################################################

package Apache::lonstudentassessment;

use strict;
use Apache::lonstatistics;
use Apache::lonhtmlcommon;
use Apache::loncommon();
use Apache::loncoursedata;
use Apache::lonnet; # for logging porpoises
use Apache::lonlocal;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Utility();

#######################################################
#######################################################
=pod

=item Package Variables

=over 4

=item $Statistics Hash ref to store student data.  Indexed by symb,
      contains hashes with keys 'score' and 'max'.

=cut

#######################################################
#######################################################

my $Statistics;

#######################################################
#######################################################

=pod

=item $show_links 'yes' or 'no' for linking to student performance data

=item $output_mode 'html', 'excel', or 'csv' for output mode

=item $show 'all', 'totals', or 'scores' determines how much data is output

=item $single_student_mode evaluates to true if we are showing only one
student.

=cut

#######################################################
#######################################################
my $show_links;
my $output_mode;
my $chosen_output;
my $single_student_mode;

#######################################################
#######################################################
# End of package variable declarations

=pod

=back

=cut

#######################################################
#######################################################

=pod

=item &BuildStudentAssessmentPage()

Inputs: 

=over 4

=item $r Apache Request

=item $c Apache Connection 

=back

=cut

#######################################################
#######################################################
sub BuildStudentAssessmentPage {
    my ($r,$c)=@_;
    #
    undef($Statistics);
    undef($show_links);
    undef($output_mode);
    undef($chosen_output);
    undef($single_student_mode);
    #
    my %Saveable_Parameters = ('Status' => 'scalar',
                               'chartoutputmode' => 'scalar',
                               'chartoutputdata' => 'scalar',
                               'Section' => 'array',
                               'StudentData' => 'array',
                               'Maps' => 'array');
    &Apache::loncommon::store_course_settings('chart',\%Saveable_Parameters);
    &Apache::loncommon::restore_course_settings('chart',\%Saveable_Parameters);
    #
    &Apache::lonstatistics::PrepareClasslist();
    #
    $single_student_mode = 0;
    $single_student_mode = 1 if ($ENV{'form.SelectedStudent'});
    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                                            ['selectstudent']);
    if ($ENV{'form.selectstudent'}) {
        &Apache::lonstatistics::DisplayClasslist($r);
        return;
    }
    #
    # Print out the HTML headers for the interface
    #    This also parses the output mode selector
    #    This step must *always* be done.
    $r->print(&CreateInterface());
    $r->print('<input type="hidden" name="notfirstrun" value="true" />');
    $r->print('<input type="hidden" name="sort" value="'.
              $ENV{'form.sort'}.'" />');
    $r->rflush();
    #
    if (! exists($ENV{'form.notfirstrun'}) && ! $single_student_mode) {
        return;
    }
    #
    my $initialize     = \&html_initialize;
    my $output_student = \&html_outputstudent;
    my $finish         = \&html_finish;
    #
    if ($output_mode eq 'excel') {
        $initialize     = \&excel_initialize;
        $output_student = \&excel_outputstudent;
        $finish         = \&excel_finish;
    } elsif ($output_mode eq 'csv') {
        $initialize     = \&csv_initialize;
        $output_student = \&csv_outputstudent;
        $finish         = \&csv_finish;
    }
    #
    if($c->aborted()) {  return ; }
    #
    # Determine which students we want to look at
    my @Students;
    if ($single_student_mode) {
        @Students = (&Apache::lonstatistics::current_student());
        $r->print(&next_and_previous_buttons());
        $r->rflush();
    } else {
        @Students = @Apache::lonstatistics::Students;
    }
    #
    # Perform generic initialization tasks
    #       Since we use lonnet::EXT to retrieve problem weights,
    #       to ensure current data we must clear the caches out.
    #       This makes sure that parameter changes at the student level
    #       are immediately reflected in the chart.
    &Apache::lonnet::clear_EXT_cache_status();
    #
    # Clean out loncoursedata's package data, just to be safe.
    &Apache::loncoursedata::clear_internal_caches();
    #
    # Call the initialize routine selected above
    $initialize->($r);
    foreach my $student (@Students) {
        if($c->aborted()) { 
            $finish->($r);
            return ; 
        }
        # Call the output_student routine selected above
        $output_student->($r,$student);
    }
    # Call the "finish" routine selected above
    $finish->($r);
    #
    return;
}

#######################################################
#######################################################
sub next_and_previous_buttons {
    my $Str = '';
    $Str .= '<input type="hidden" name="SelectedStudent" value="'.
        $ENV{'form.SelectedStudent'}.'" />';
    #
    # Build the previous student link
    my $previous = &Apache::lonstatistics::previous_student();
    my $previousbutton = '';
    if (defined($previous)) {
        my $sname = $previous->{'username'}.':'.$previous->{'domain'};
        $previousbutton .= '<input type="button" value="'.
            'Previous Student ('.
            $previous->{'username'}.'@'.$previous->{'domain'}.')'.
            '" onclick="document.Statistics.SelectedStudent.value='.
            "'".$sname."'".';'.
            'document.Statistics.submit();" />';
    } else {
        $previousbutton .= '<input type="button" value="'.
            'Previous student (none)'.'" />';
    }
    #
    # Build the next student link
    my $next = &Apache::lonstatistics::next_student();
    my $nextbutton = '';
    if (defined($next)) {
        my $sname = $next->{'username'}.':'.$next->{'domain'};
        $nextbutton .= '<input type="button" value="'.
            'Next Student ('.
            $next->{'username'}.'@'.$next->{'domain'}.')'.
            '" onclick="document.Statistics.SelectedStudent.value='.
            "'".$sname."'".';'.
            'document.Statistics.submit();" />';
    } else {
        $nextbutton .= '<input type="button" value="'.
            'Next student (none)'.'" />';
    }
    #
    # Build the 'all students' button
    my $all = '';
    $all .= '<input type="button" value="All Students" '.
            '" onclick="document.Statistics.SelectedStudent.value='.
            "''".';'.'document.Statistics.submit();" />';
    $Str .= $previousbutton.('&nbsp;'x5).$all.('&nbsp;'x5).$nextbutton;
    return $Str;
}

#######################################################
#######################################################

sub get_student_fields_to_show {
    my @to_show = @Apache::lonstatistics::SelectedStudentData;
    foreach (@to_show) {
        if ($_ eq 'all') {
            @to_show = @Apache::lonstatistics::StudentDataOrder;
            last;
        }
    }
    return @to_show;
}

#######################################################
#######################################################

=pod

=item &CreateInterface()

Called by &BuildStudentAssessmentPage to create the top part of the
page which displays the chart.

Inputs: None

Returns:  A string containing the HTML for the headers and top table for 
the chart page.

=cut

#######################################################
#######################################################
sub CreateInterface {
    my $Str = '';
    $Str .= &Apache::lonhtmlcommon::breadcrumbs(undef,'Chart');
#    $Str .= &CreateLegend();
    $Str .= '<table cellspacing="5">'."\n";
    $Str .= '<tr>';
    $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
    $Str .= '<td align="center"><b>'.&mt('Student Data</b>').'</td>';
    $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
    $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
    $Str .= '<td align="center"><b>'.&mt('Output Format').'</b>'.
        &Apache::loncommon::help_open_topic("Chart_Output_Formats").
        '</td>';
    $Str .= '<td align="center"><b>'.&mt('Output Data').'</b>'.
        &Apache::loncommon::help_open_topic("Chart_Output_Data").
        '</td>';
    $Str .= '</tr>'."\n";
    #
    $Str .= '<tr><td align="center">'."\n";
    $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
    $Str .= '</td><td align="center">';
    my $only_seq_with_assessments = sub { 
        my $s=shift;
        if ($s->{'num_assess'} < 1) { 
            return 0;
        } else { 
            return 1;
        }
    };
    $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',
                                                      5,undef);
    $Str .= '</td><td>'."\n";
    $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
    $Str .= '</td><td>'."\n";
    $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
                                              $only_seq_with_assessments);
    $Str .= '</td><td>'."\n";
    $Str .= &CreateAndParseOutputSelector();
    $Str .= '</td><td>'."\n";
    $Str .= &CreateAndParseOutputDataSelector();
    $Str .= '</td></tr>'."\n";
    $Str .= '</table>'."\n";
    $Str .= '<input type="submit" name="Generate Chart" value="'.
        &mt('Generate Chart').'" />';
    $Str .= '&nbsp;'x5;
    $Str .= '<input type="submit" name="selectstudent" value="'.
        &mt('Select One Student').'" />';
    $Str .= '&nbsp;'x5;
    $Str .= '<input type="submit" name="ClearCache" value="'.
        &mt('Clear Caches').'" />';
    $Str .= '&nbsp;'x5;
    $Str .= '<br />';
    return $Str;
}

#######################################################
#######################################################

=pod

=item &CreateAndParseOutputSelector()

=cut

#######################################################
#######################################################
my @OutputOptions = 
    ({ name  => 'HTML, with links',
       value => 'html, with links',
       description => 'Output HTML with each symbol linked to the problem '.
	   'which generated it.',
       mode => 'html',
       show_links => 'yes',
       },
     { name  => 'HTML, with all links',
       value => 'html, with all links',
       description => 'Output HTML with each symbol linked to the problem '.
	   'which generated it.  '.
           'This includes links for unattempted problems.',
       mode => 'html',
       show_links => 'all',
       },
     { name  => 'HTML, without links',
       value => 'html, without links',
       description => 'Output HTML.  By not including links, the size of the'.
	   ' web page is greatly reduced.  If your browser crashes on the '.
	   'full display, try this.',
       mode => 'html',
       show_links => 'no',
           },
     { name  => 'Excel',
       value => 'excel',
       description => 'Output an Excel file (compatable with Excel 95).',
       mode => 'excel',
       show_links => 'no',
   },
     { name  => 'CSV',
       value => 'csv',
       description => 'Output a comma separated values file suitable for '.
           'import into a spreadsheet program.  Using this method as opposed '.
           'to Excel output allows you to organize your data before importing'.
           ' it into a spreadsheet program.',
       mode => 'csv',
       show_links => 'no',
           },
     );

sub OutputDescriptions {
    my $Str = '';
    $Str .= "<h2>Output Formats</h2>\n";
    $Str .= "<dl>\n";
    foreach my $outputmode (@OutputOptions) {
	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
    }
    $Str .= "</dl>\n";
    return $Str;
}

sub CreateAndParseOutputSelector {
    my $Str = '';
    my $elementname = 'chartoutputmode';
    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                                            [$elementname]);
    #
    # Format for output options is 'mode, restrictions';
    my $selected = 'html, without links';
    if (exists($ENV{'form.'.$elementname})) {
        if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
            $selected = $ENV{'form.'.$elementname}->[0];
        } else {
            $selected = $ENV{'form.'.$elementname};
        }
    }
    #
    # Set package variables describing output mode
    $show_links  = 'no';
    $output_mode = 'html';
    foreach my $option (@OutputOptions) {
        next if ($option->{'value'} ne $selected);
        $output_mode = $option->{'mode'};
        $show_links  = $option->{'show_links'};
    }

    #
    # Build the form element
    $Str = qq/<select size="5" name="$elementname">/;
    foreach my $option (@OutputOptions) {
        $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
        $Str .= " selected " if ($option->{'value'} eq $selected);
        $Str .= ">".&mt($option->{'name'})."<\/option>";
    }
    $Str .= "\n</select>";
    return $Str;
}

##
## Data selector stuff
##
my @OutputDataOptions =
    (
     { name  => 'Scores Summary',
       base  => 'scores',
       value => 'sum and total',
       scores => 1,
       tries  => 0,
       every_problem => 0,
       sequence_sum => 1,
       sequence_max => 1,
       grand_total => 1,
       grand_maximum => 1,
       summary_table => 1,
       maximum_row => 1,
       shortdesc => 'Total Score and Maximum Possible for each '.
           'Sequence or Folder',
       longdesc => 'The score of each student as well as the '.
           ' maximum possible on each Sequence or Folder.',
       },
     { name  => 'Scores Per Problem',
       base  => 'scores',
       value => 'scores',
       scores => 1,
       tries  => 0,
       correct => 0,
       every_problem => 1,
       sequence_sum => 1,
       sequence_max => 1,
       grand_total => 1,
       grand_maximum => 1,
       summary_table => 1,
       maximum_row => 1,
       shortdesc => 'Score on each Problem Part',
       longdesc =>'The students score on each problem part, computed as'.
           'the part weight * part awarded',
       },
     { name  =>'Tries',
       base  =>'tries',
       value => 'tries',
       scores => 0,
       tries  => 1,
       correct => 0,
       every_problem => 1,
       sequence_sum => 0,
       sequence_max => 0,
       grand_total => 0,
       grand_maximum => 0,
       summary_table => 0,
       maximum_row => 0,
       shortdesc => 'Number of Tries before success on each Problem Part',
       longdesc =>'The number of tries before success on each problem part.',
       },
     { name  =>'Parts Correct',
       base  =>'tries',
       value => 'parts correct total',
       scores => 0,
       tries  => 0,
       correct => 1,
       every_problem => 1,
       sequence_sum => 1,
       sequence_max => 1,
       grand_total => 1,
       grand_maximum => 1,
       summary_table => 1,
       maximum_row => 0,
       shortdesc => 'Number of Problem Parts completed successfully.',
       longdesc => 'The Number of Problem Parts completed successfully and '.
           'the maximum possible for each student',
       },
     );

sub HTMLifyOutputDataDescriptions {
    my $Str = '';
    $Str .= "<h2>Output Data</h2>\n";
    $Str .= "<dl>\n";
    foreach my $option (@OutputDataOptions) {
        $Str .= '    <dt>'.$option->{'name'}.'</dt>';
        $Str .= '<dd>'.$option->{'longdesc'}.'</dd>'."\n";
    }
    $Str .= "</dl>\n";
    return $Str;
}

sub CreateAndParseOutputDataSelector {
    my $Str = '';
    my $elementname = 'chartoutputdata';
    #
    my $selected = 'scores';
    if (exists($ENV{'form.'.$elementname})) {
        if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
            $selected = $ENV{'form.'.$elementname}->[0];
        } else {
            $selected = $ENV{'form.'.$elementname};
        }
    }
    #
    $chosen_output = $OutputDataOptions[0];
    foreach my $option (@OutputDataOptions) {
        if ($option->{'value'} eq $selected) {
            $chosen_output = $option;
        }
    }
    #
    # Build the form element
    $Str = qq/<select size="5" name="$elementname">/;
    foreach my $option (@OutputDataOptions) {
        $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
        $Str .= " selected " if ($option->{'value'} eq $chosen_output->{'value'});
        $Str .= ">".&mt($option->{'name'})."<\/option>";
    }
    $Str .= "\n</select>";
    return $Str;

}

#######################################################
#######################################################

=pod

=head2 HTML output routines

=item &html_initialize($r)

Create labels for the columns of student data to show.

=item &html_outputstudent($r,$student)

Return a line of the chart for a student.

=item &html_finish($r)

=cut

#######################################################
#######################################################
{
    my $padding;
    my $count;

    my $nodata_count; # The number of students for which there is no data
    my %prog_state;   # progress state used by loncommon PrgWin routines
    my $total_sum_width;

sub html_initialize {
    my ($r) = @_;
    #
    $padding = ' 'x3;
    $count = 0;
    $nodata_count = 0;
    undef(%prog_state);
    #
    $r->print("<h3>".$ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
              "&nbsp;&nbsp;".localtime(time)."</h3>");

    if ($chosen_output->{'base'} !~ /^final table/) {
        $r->print("<h3>".$chosen_output->{'shortdesc'}."</h3>");        
    }
    my $Str = "<pre>\n";
    # First, the @StudentData fields need to be listed
    my @to_show = &get_student_fields_to_show();
    foreach my $field (@to_show) {
        my $title=$Apache::lonstatistics::StudentData{$field}->{'title'};
        my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'};
        my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
        $Str .= $title.' 'x($width-$base).$padding;
    }
    #
    # Compute the column widths and output the sequence titles
    my $total_count;
    foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()){
        #
        # Comptue column widths
        $sequence->{'width_sum'} = 0;
        if ($chosen_output->{'sequence_sum'}) {
            if ($chosen_output->{'every_problem'}) {
                # Use 1 digit for a space
                $sequence->{'width_sum'} += 1;            
            }
	    $total_count += $sequence->{'num_assess_parts'};
            # Use 3 digits for the sum
            $sequence->{'width_sum'} += 3;
        }
        if ($chosen_output->{'sequence_max'}) {
            if ($sequence->{'width_sum'}>0) {
                # One digit for the '/'
                $sequence->{'width_sum'} +=1;
            }
            # Use 3 digits for the total
            $sequence->{'width_sum'}+=3;
        }
	#
        if ($chosen_output->{'every_problem'}) {
            # one problem per digit
            $sequence->{'width_problem'} = $sequence->{'num_assess_parts'};
        } else {
            $sequence->{'width_problem'} = 0;
        }
        $sequence->{'width_total'} = $sequence->{'width_problem'} + 
                                     $sequence->{'width_sum'};
        if ($sequence->{'width_total'} < length(&HTML::Entities::decode($sequence->{'title'}))) {
            $sequence->{'width_total'} = length(&HTML::Entities::decode($sequence->{'title'}));
        }
        #
        # Output the sequence titles
        $Str .= 
            $sequence->{'title'}.' 'x($sequence->{'width_total'}-
                                      length($sequence->{'title'})
                                      ).$padding;
    }
    $total_sum_width = length($total_count)+1;
    $Str .= "    total</pre>\n";
    $Str .= "<pre>";
    $r->print($Str);
    $r->rflush();
    return;
}

sub html_outputstudent {
    my ($r,$student) = @_;
    my $Str = '';
    #
    if($count++ % 5 == 0 && $count > 0) {
        $r->print("</pre><pre>");
    }
    # First, the @StudentData fields need to be listed
    my @to_show = &get_student_fields_to_show();
    foreach my $field (@to_show) {
        my $title=$student->{$field};
        my $base = length($title);
        my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
        $Str .= $title.' 'x($width-$base).$padding;
    }
    # Get ALL the students data
    my %StudentsData;
    my @tmp = &Apache::loncoursedata::get_current_state
        ($student->{'username'},$student->{'domain'},undef,
         $ENV{'request.course.id'});
    if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
        %StudentsData = @tmp;
    }
    if (scalar(@tmp) < 1) {
        $nodata_count++;
        $Str .= '<font color="blue">No Course Data</font>'."\n";
        $r->print($Str);
        $r->rflush();
        return;
    }
    #
    # By sequence build up the data
    my $studentstats;
    my $PerformanceStr = '';
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        my ($performance,$performance_length,$score,$seq_max,$rawdata);
        if ($chosen_output->{'tries'}) {
            ($performance,$performance_length,$score,$seq_max,$rawdata) =
                &StudentTriesOnSequence($student,\%StudentsData,
                                        $seq,$show_links);
        } else {
            ($performance,$performance_length,$score,$seq_max,$rawdata) =
                &StudentPerformanceOnSequence($student,\%StudentsData,
                                              $seq,$show_links);
        }
        my $ratio='';
        if ($chosen_output->{'every_problem'}) {
            $ratio .= ' ';
        }
        if ($chosen_output->{'sequence_sum'} && $score ne ' ') {
            $ratio .= sprintf("%3.0f",$score);
        } elsif($chosen_output->{'sequence_sum'}) {
            $ratio .= ' 'x3;
        }
        if ($chosen_output->{'sequence_max'}) {
            if ($chosen_output->{'sequence_sum'}) {
                $ratio .= '/';
            }
            $ratio .= sprintf("%3.0f",$seq_max);
        }
        #
        if (! $chosen_output->{'every_problem'}) {
            $performance = '';
	    $performance_length=0;
        }
        $performance .= ' 'x($seq->{'width_total'}-$performance_length-$seq->{'width_sum'}).
            $ratio;
        #
        $Str .= $performance.$padding;
        #
        $studentstats->{$seq->{'symb'}}->{'score'}= $score;
        $studentstats->{$seq->{'symb'}}->{'max'}  = $seq_max;
    }
    #
    # Total it up and store the statistics info.
    my ($score,$max);
    while (my ($symb,$seq_stats) = each (%{$studentstats})) {
        $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
        if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) {
            $Statistics->{$symb}->{'max'} = $seq_stats->{'max'};
        }
        if ($seq_stats->{'score'} ne ' ') {
            $score += $seq_stats->{'score'};
            $Statistics->{$symb}->{'num_students'}++;
        }
        $max   += $seq_stats->{'max'};
    }
    if (! defined($score)) {
        $score = ' ' x $total_sum_width;
    }
    $Str .= ' '.' 'x($total_sum_width-length($score)).$score.' / '.$max;
    $Str .= " \n";
    #
    $r->print($Str);
    #
    $r->rflush();
    return;
}    

sub html_finish {
    my ($r) = @_;
    #
    # Check for suppressed output and close the progress window if so
    $r->print("</pre>\n"); 
    if ($chosen_output->{'summary_table'}) {
        if ($single_student_mode) {
            $r->print(&SingleStudentTotal());
        } else {
            $r->print(&StudentAverageTotal());
        }
    }
    $r->rflush();
    return;
}

sub StudentAverageTotal {
    my $Str = "<h3>Summary Tables</h3>\n";
#    my $max_students;
#    my $total_ave = 0;
#    my $total_max = 0;
    $Str .= '<table border=2 cellspacing="1">'."\n";
    $Str .= "<tr><th>Title</th><th>Average</th><th>Maximum</th></tr>\n";
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        my $ave;
        my $num_students = $Statistics->{$seq->{'symb'}}->{'num_students'};
#        if ($num_students > $max_students) {
#            $max_students = $num_students;
#        }
        if ($num_students > 0) {
            $ave = int(100*
                       ($Statistics->{$seq->{'symb'}}->{'score'}/$num_students)
                       )/100;
        } else {
            $ave = 0;
        }
#        $total_ave += $ave;
        my $max = $Statistics->{$seq->{'symb'}}->{'max'};
#        $total_max += $max;
        $ave = sprintf("%.2f",$ave);
        $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
            '<td align="right">'.$ave.'&nbsp;</td>'.
            '<td align="right">'.$max.'&nbsp;'.'</td></tr>'."\n";
    }
#    $total_ave = sprintf('%.2f',$total_ave); # only two digit
    $Str .= "</table>\n";
#    $Str .= '<table border=2 cellspacing="1">'."\n";
#    $Str .= '<tr><th>Number of Students</th><th>Average</th>'.
#        "<th>Maximum</th></tr>\n";
#    $Str .= '<tr>'.
#        '<td align="right">'.$max_students.'</td>'.
#        '<td align="right">'.$total_ave.'&nbsp;'.'</td>'.
#        '<td align="right">'.$total_max.'&nbsp;'.'</td>';
#    $Str .= "</table>\n";
    return $Str;
}

sub SingleStudentTotal {
    my $student = &Apache::lonstatistics::current_student();
    my $Str = "<h3>Summary table for ".$student->{'fullname'}." ".
        $student->{'username'}.'@'.$student->{'domain'}."</h3>\n";
    $Str .= '<table border=2 cellspacing="1">'."\n";
    $Str .= 
        "<tr><th>Sequence or Folder</th><th>Score</th><th>Maximum</th></tr>\n";
    my $total = 0;
    my $total_max = 0;
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        my $value = $Statistics->{$seq->{'symb'}}->{'score'};
        my $max = $Statistics->{$seq->{'symb'}}->{'max'};
        $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
            '<td align="right">'.$value.'</td>'.
                '<td align="right">'.$max.'</td></tr>'."\n";
        $total += $value;
        $total_max +=$max;
    }
    $Str .= '<tr><td><b>Total</b></td>'.
        '<td align="right">'.$total.'</td>'.
        '<td align="right">'.$total_max."</td></tr>\n";
    $Str .= "</table>\n";
    return $Str;
}

}

#######################################################
#######################################################

=pod

=head2 EXCEL subroutines

=item &excel_initialize($r)

=item &excel_outputstudent($r,$student)

=item &excel_finish($r)

=cut

#######################################################
#######################################################
{

my $excel_sheet;
my $excel_workbook;

my $filename;
my $rows_output;
my $cols_output;

my %prog_state; # progress window state
my $request_aborted;

my $total_formula;
my $maximum_formula;

sub excel_initialize {
    my ($r) = @_;
    #
    undef ($excel_sheet);
    undef ($excel_workbook);
    undef ($filename);
    undef ($rows_output);
    undef ($cols_output);
    undef (%prog_state);
    undef ($request_aborted);
    undef ($total_formula);
    undef ($maximum_formula);
    #
    my $total_columns = scalar(&get_student_fields_to_show());
    my $num_students = scalar(@Apache::lonstatistics::Students);
    #
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        if ($chosen_output->{'every_problem'}) {
            $total_columns += $seq->{'num_assess_parts'};
        }
        # Add 2 because we need a 'sequence_sum' and 'total' column for each
        $total_columns += 2;
    }
    if ($chosen_output->{'base'} eq 'tries' && $total_columns > 255) {
        $r->print(<<END);
<h2>Unable to Complete Request</h2>
<p>
LON-CAPA is unable to produce your Excel spreadsheet because your selections
will result in more than 255 columns.  Excel allows only 255 columns in a
spreadsheet.
</p><p>
You may consider reducing the number of <b>Sequences or Folders</b> you
have selected.  
</p><p>
LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the
summary data (<b>Parts Correct</b> or <b>Parts Correct & Totals</b>).
</p>
END
       $request_aborted = 1;
    }
    if ($chosen_output->{'base'} eq 'scores' && $total_columns > 255) {
        $r->print(<<END);
<h2>Unable to Complete Request</h2>
<p>
LON-CAPA is unable to produce your Excel spreadsheet because your selections
will result in more than 255 columns.  Excel allows only 255 columns in a
spreadsheet.
</p><p>
You may consider reducing the number of <b>Sequences or Folders</b> you
have selected.  
</p><p>
LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the
<b>Scores Summary</b> data.
</p>
END
       $request_aborted = 1;
    }
    return if ($request_aborted);
    #
    $filename = '/prtspool/'.
        $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
            time.'_'.rand(1000000000).'.xls';
    #
    $excel_workbook = undef;
    $excel_sheet = undef;
    #
    $rows_output = 0;
    $cols_output = 0;
    #
    # Determine rows 
    my $header_row = $rows_output++;
    my $description_row = $rows_output++;
    $rows_output++;        # blank row
    my $summary_header_row;
    if ($chosen_output->{'summary_table'}) {
        $summary_header_row = $rows_output++;
        $rows_output+= scalar(&Apache::lonstatistics::Sequences_with_Assess());
        $rows_output++;
    }
    my $sequence_name_row = $rows_output++;
    my $resource_name_row = $rows_output++;
    my $maximum_data_row = $rows_output++;
    if (! $chosen_output->{'maximum_row'}) {
        $rows_output--;
    }
    my $first_data_row = $rows_output++;
    #
    # Create sheet
    $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
    #
    # Check for errors
    if (! defined($excel_workbook)) {
        $r->log_error("Error creating excel spreadsheet $filename: $!");
        $r->print("Problems creating new Excel file.  ".
                  "This error has been logged.  ".
                  "Please alert your LON-CAPA administrator");
        return ;
    }
    #
    # The excel spreadsheet stores temporary data in files, then put them
    # together.  If needed we should be able to disable this (memory only).
    # The temporary directory must be specified before calling 'addworksheet'.
    # File::Temp is used to determine the temporary directory.
    $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
    #
    my $format = &Apache::loncommon::define_excel_formats($excel_workbook);
    #
    # Add a worksheet
    my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
    $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
    $excel_sheet = $excel_workbook->addworksheet($sheetname);
    #
    # Put the course description in the header
    $excel_sheet->write($header_row,$cols_output++,
                   $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
                        $format->{'h1'});
    $cols_output += 3;
    #
    # Put a description of the sections listed
    my $sectionstring = '';
    my @Sections = @Apache::lonstatistics::SelectedSections;
    if (scalar(@Sections) > 1) {
        if (scalar(@Sections) > 2) {
            my $last = pop(@Sections);
            $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
        } else {
            $sectionstring = "Sections ".join(' and ',@Sections);
        }
    } else {
        if ($Sections[0] eq 'all') {
            $sectionstring = "All sections";
        } else {
            $sectionstring = "Section ".$Sections[0];
        }
    }
    $excel_sheet->write($header_row,$cols_output++,$sectionstring,
                        $format->{'h3'});
    $cols_output += scalar(@Sections);
    #
    # Put the date in there too
    $excel_sheet->write($header_row,$cols_output++,
                        'Compiled on '.localtime(time),$format->{'h3'});
    #
    $cols_output = 0;
    $excel_sheet->write($description_row,$cols_output++,
                        $chosen_output->{'shortdesc'},
                        $format->{'h3'});
    ##############################################
    # Output headings for the raw data
    ##############################################
    #
    # Add the student headers
    $cols_output = 0;
    foreach my $field (&get_student_fields_to_show()) {
        $excel_sheet->write($resource_name_row,$cols_output++,$field,
                            $format->{'bold'});
    }
    #
    # Add the remaining column headers
    my $total_formula_string = '=0';
    my $maximum_formula_string = '=0';
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        $excel_sheet->write($sequence_name_row,,
                            $cols_output,$seq->{'title'},$format->{'bold'});
        # Determine starting cell
        $seq->{'Excel:startcell'}=
            &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
            ($first_data_row,$cols_output);
        $seq->{'Excel:startcol'}=$cols_output;
        my $count = 0;
        if ($chosen_output->{'every_problem'}) {
            # Put the names of the problems and parts into the sheet
            foreach my $res (@{$seq->{'contents'}}) {
                if ($res->{'type'} ne 'assessment'  || 
                    ! exists($res->{'parts'})       ||
                    ref($res->{'parts'}) ne 'ARRAY' ||
                    scalar(@{$res->{'parts'}}) < 1) {
                    next;
                }
                if (scalar(@{$res->{'parts'}}) > 1) {
                    foreach my $part (@{$res->{'parts'}}) {
                        $excel_sheet->write($resource_name_row,
                                            $cols_output++,
                                            $res->{'title'}.' part '.$part,
                                            $format->{'bold'});
                    }
                } else {
                    $excel_sheet->write($resource_name_row,
                                        $cols_output++,
                                        $res->{'title'},$format->{'bold'});
                }
                $count++;
            }
        }
        # Determine ending cell
        if ($count <= 1) {
            $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
            $seq->{'Excel:endcol'}  = $seq->{'Excel:startcol'};
        } else {
            $seq->{'Excel:endcell'} = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($first_data_row,$cols_output-1);
            $seq->{'Excel:endcol'} = $cols_output-1;
        }
        # Create the formula for summing up this sequence
        if (! exists($seq->{'Excel:endcell'}) ||
            ! defined($seq->{'Excel:endcell'})) {
            $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
        }
        $seq->{'Excel:sum'}= $excel_sheet->store_formula
            ('=SUM('.$seq->{'Excel:startcell'}.
             ':'.$seq->{'Excel:endcell'}.')');
        # Determine cell the score is held in
        $seq->{'Excel:scorecell'} = 
            &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
            ($first_data_row,$cols_output);
        $seq->{'Excel:scorecol'}=$cols_output;
        if ($chosen_output->{'base'} eq 'parts correct total') {
            $excel_sheet->write($resource_name_row,$cols_output++,
                                'parts correct',
                                $format->{'bold'});
        } elsif ($chosen_output->{'sequence_sum'}) {
            if ($chosen_output->{'correct'}) {
                # Only reporting the number correct, so do not call it score
                $excel_sheet->write($resource_name_row,$cols_output++,
                                    'sum',
                                    $format->{'bold'});
            } else {
                $excel_sheet->write($resource_name_row,$cols_output++,
                                    'score',
                                    $format->{'bold'});
            }
        }
        #
        $total_formula_string.='+'.
            &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
            ($first_data_row,$cols_output-1);
        if ($chosen_output->{'sequence_max'}) {
            $excel_sheet->write($resource_name_row,$cols_output,
                                'maximum',
                                $format->{'bold'});
            $seq->{'Excel:maxcell'} = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($first_data_row,$cols_output);
            $seq->{'Excel:maxcol'}=$cols_output;
            $maximum_formula_string.='+'.
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($first_data_row,$cols_output);
            $cols_output++;

        }
    }
    if ($chosen_output->{'grand_total'}) {
        $excel_sheet->write($resource_name_row,$cols_output++,'Total',
                            $format->{'bold'});
    }
    if ($chosen_output->{'grand_maximum'}) {
        $excel_sheet->write($resource_name_row,$cols_output++,'Max. Total',
                            $format->{'bold'});
    }
    $total_formula = $excel_sheet->store_formula($total_formula_string);
    $maximum_formula = $excel_sheet->store_formula($maximum_formula_string);
    ##############################################
    # Output a row for MAX, if appropriate
    ##############################################
    if ($chosen_output->{'maximum_row'}) {
        $cols_output = 0;
        foreach my $field (&get_student_fields_to_show()) {
            if ($field eq 'username' || $field eq 'fullname' || 
                $field eq 'id') {
                $excel_sheet->write($maximum_data_row,$cols_output++,'Maximum',
                                    $format->{'bold'});
            } else {
                $excel_sheet->write($maximum_data_row,$cols_output++,'');
            }
        }
        #
        # Add the maximums for each sequence or assessment
        my %total_cell_translation;
        my %maximum_cell_translation;
        foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
            $cols_output=$seq->{'Excel:startcol'};
            $total_cell_translation{$seq->{'Excel:scorecell'}} = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($maximum_data_row,$seq->{'Excel:scorecol'});
            $maximum_cell_translation{$seq->{'Excel:maxcell'}} = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($maximum_data_row,$seq->{'Excel:maxcol'});
            my $weight;
            my $max = 0;
            foreach my $resource (@{$seq->{'contents'}}) {
                next if ($resource->{'type'} ne 'assessment');
                foreach my $part (@{$resource->{'parts'}}) {
                    $weight = 1;
                    if ($chosen_output->{'scores'}) {
                        $weight = &Apache::lonnet::EXT
                            ('resource.'.$part.'.weight',$resource->{'symb'},
                             undef,undef,undef);
                        if (!defined($weight) || ($weight eq '')) { 
                            $weight=1;
                        }
                    }
                    if ($chosen_output->{'scores'} &&
                        $chosen_output->{'every_problem'}) {
                        $excel_sheet->write($maximum_data_row,$cols_output++,
                                            $weight);
                    }
                    $max += $weight;
                }
            } 
            #
            if ($chosen_output->{'sequence_sum'} && 
                $chosen_output->{'every_problem'}) {
                my %replaceCells;
                $replaceCells{$seq->{'Excel:startcell'}} = 
                    &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                    ($maximum_data_row,$seq->{'Excel:startcol'});
                $replaceCells{$seq->{'Excel:endcell'}} = 
                    &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                    ($maximum_data_row,$seq->{'Excel:endcol'});
                $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
                                             $seq->{'Excel:sum'},undef,
                                             %replaceCells);
            } elsif ($chosen_output->{'sequence_sum'}) {
                $excel_sheet->write($maximum_data_row,$cols_output++,$max);
            }
            if ($chosen_output->{'sequence_max'}) {
                $excel_sheet->write($maximum_data_row,$cols_output++,$max);
            }
            #
        }
        if ($chosen_output->{'grand_total'}) {
            $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
                                         $total_formula,undef,
                                         %total_cell_translation);
        }
        if ($chosen_output->{'grand_maximum'}) {
            $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
                                         $maximum_formula,undef,
                                         %total_cell_translation);
        }
    } # End of MAXIMUM row output  if ($chosen_output->{'maximum_row'}) {
    $rows_output = $first_data_row;
    ##############################################
    # Output summary table, which actually is above the sequence name row.
    ##############################################
    if ($chosen_output->{'summary_table'}) {
        $cols_output = 0;
        $excel_sheet->write($summary_header_row,$cols_output++,
                            'Summary Table',$format->{'bold'});
        if ($chosen_output->{'maximum_row'}) {
            $excel_sheet->write($summary_header_row,$cols_output++,
                                'Maximum',$format->{'bold'});
        }
        $excel_sheet->write($summary_header_row,$cols_output++,
                            'Average',$format->{'bold'});
        $excel_sheet->write($summary_header_row,$cols_output++,
                            'Median',$format->{'bold'});
        $excel_sheet->write($summary_header_row,$cols_output++,
                            'Std Dev',$format->{'bold'});
        my $row = $summary_header_row+1;
        foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
            $cols_output = 0;
            $excel_sheet->write($row,$cols_output++,
                                $seq->{'title'},
                                $format->{'bold'});
            if ($chosen_output->{'maximum_row'}) {
                $excel_sheet->write
                    ($row,$cols_output++,
                     '='.
                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                     ($maximum_data_row,$seq->{'Excel:scorecol'})
                     );
            }
            my $range = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($first_data_row,$seq->{'Excel:scorecol'}).
                ':'.
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                ($first_data_row+$num_students-1,$seq->{'Excel:scorecol'});
            $excel_sheet->write($row,$cols_output++,
                                '=AVERAGE('.$range.')');
            $excel_sheet->write($row,$cols_output++,
                                '=MEDIAN('.$range.')');
            $excel_sheet->write($row,$cols_output++,
                                '=STDEV('.$range.')');
            $row++;
        }
    }
    ##############################################
    #   Take care of non-excel initialization
    ##############################################
    #
    # Let the user know what we are doing
    my $studentcount = scalar(@Apache::lonstatistics::Students); 
    if ($ENV{'form.SelectedStudent'}) {
        $studentcount = '1';
    }
    if ($studentcount > 1) {
        $r->print('<h1>'.&mt('Compiling Excel spreadsheet for [_1] students',
                             $studentcount)."</h1>\n");
    } else {
        $r->print('<h1>'.
                  &mt('Compiling Excel spreadsheet for 1 student').
                  "</h1>\n");
    }
    $r->rflush();
    #
    # Initialize progress window
    %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
        ($r,'Excel File Compilation Status',
         'Excel File Compilation Progress', $studentcount);
    #
    &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                                          'Processing first student');
    return;
}

sub excel_outputstudent {
    my ($r,$student) = @_;
    return if ($request_aborted);
    return if (! defined($excel_sheet));
    $cols_output=0;
    #
    # Write out student data
    my @to_show = &get_student_fields_to_show();
    foreach my $field (@to_show) {
        $excel_sheet->write($rows_output,$cols_output++,$student->{$field});
    }
    #
    # Get student assessment data
    my %StudentsData;
    my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
                                                        $student->{'domain'},
                                                        undef,
                                                   $ENV{'request.course.id'});
    if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
        %StudentsData = @tmp;
    }
    #
    # Write out sequence scores and totals data
    my %total_cell_translation;
    my %maximum_cell_translation;
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        $cols_output = $seq->{'Excel:startcol'};
        # Keep track of cells to translate in total cell
        $total_cell_translation{$seq->{'Excel:scorecell'}} = 
            &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                        ($rows_output,$seq->{'Excel:scorecol'});
        #
        my ($performance,$performance_length,$score,$seq_max,$rawdata);
        if ($chosen_output->{'tries'} || $chosen_output->{'correct'}){
            ($performance,$performance_length,$score,$seq_max,$rawdata) =
                &StudentTriesOnSequence($student,\%StudentsData,
                                        $seq,'no');
        } else {
            ($performance,$performance_length,$score,$seq_max,$rawdata) =
                &StudentPerformanceOnSequence($student,\%StudentsData,
                                              $seq,'no');
        } 
        if ($chosen_output->{'every_problem'}) {
            if ($chosen_output->{'correct'}) {
                # only indiciate if each item is correct or not
                foreach my $value (@$rawdata) {
                    # nonzero means correct
                    $value = 1 if ($value > 0);
                    $excel_sheet->write($rows_output,$cols_output++,$value);
                }
            } else {
                foreach my $value (@$rawdata) {
                    if ($score eq ' ' || !defined($value)) {
                        $cols_output++;
                    } else {                        
                        $excel_sheet->write($rows_output,$cols_output++,
                                            $value);
                    }
                }
            }
        }
        if ($chosen_output->{'sequence_sum'} && 
            $chosen_output->{'every_problem'}) {
            # Write a formula for the sum of this sequence
            my %replaceCells;
            $replaceCells{$seq->{'Excel:startcell'}} = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                            ($rows_output,$seq->{'Excel:startcol'});
            $replaceCells{$seq->{'Excel:endcell'}} = 
                &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                            ($rows_output,$seq->{'Excel:endcol'});
            # The undef is for the format
            if (scalar(keys(%replaceCells)) == 1) {
                $excel_sheet->repeat_formula($rows_output,$cols_output++,
                                             $seq->{'Excel:sum'},undef,
                                             %replaceCells,%replaceCells);
            } else {
                $excel_sheet->repeat_formula($rows_output,$cols_output++,
                                             $seq->{'Excel:sum'},undef,
                                             %replaceCells);
            }
        } elsif ($chosen_output->{'sequence_sum'}) {
            if ($score eq ' ') {
                $cols_output++;
            } else {
                $excel_sheet->write($rows_output,$cols_output++,$score);
            }
        }
        if ($chosen_output->{'sequence_max'}) {
            $excel_sheet->write($rows_output,$cols_output++,$seq_max);
        }
    }
    #
    if ($chosen_output->{'grand_total'}) {
        $excel_sheet->repeat_formula($rows_output,$cols_output++,
                                     $total_formula,undef,
                                     %total_cell_translation);
    }
    if ($chosen_output->{'grand_maximum'}) {
        $excel_sheet->repeat_formula($rows_output,$cols_output++,
                                     $maximum_formula,undef,
                                     %total_cell_translation);
    }
    #
    # Bookkeeping
    $rows_output++; 
    $cols_output=0;
    #
    # Update the progress window
    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
    return;
}

sub excel_finish {
    my ($r) = @_;
    return if ($request_aborted);
    return if (! defined($excel_sheet));
    #
    # Write the excel file
    $excel_workbook->close();
    my $c = $r->connection();
    #
    return if($c->aborted());
    #
    # Close the progress window
    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
    #
    # Tell the user where to get their excel file
    $r->print('<br />'.
              '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
    $r->rflush();
    return;
}

}
#######################################################
#######################################################

=pod

=head2 CSV output routines

=item &csv_initialize($r)

=item &csv_outputstudent($r,$student)

=item &csv_finish($r)

=cut

#######################################################
#######################################################
{

my $outputfile;
my $filename;
my $request_aborted;
my %prog_state; # progress window state

sub csv_initialize{
    my ($r) = @_;
    # 
    # Clean up
    undef($outputfile);
    undef($filename);
    undef($request_aborted);
    undef(%prog_state);
    #
    # Deal with unimplemented requests
    $request_aborted = undef;
    if ($chosen_output->{'base'} =~ /final table/) {
        $r->print(<<END);
<h2>Unable to Complete Request</h2>
<p>
The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
</p>
END
       $request_aborted = 1;
    }
    return if ($request_aborted);
    #
    # Initialize progress window
    my $studentcount = scalar(@Apache::lonstatistics::Students);
    %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
        ($r,'CSV File Compilation Status',
         'CSV File Compilation Progress', $studentcount);
    #
    # Open a file
    $filename = '/prtspool/'.
        $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
            time.'_'.rand(1000000000).'.csv';
    unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
        $r->log_error("Couldn't open $filename for output $!");
        $r->print("Problems occured in writing the csv file.  ".
                  "This error has been logged.  ".
                  "Please alert your LON-CAPA administrator.");
        $outputfile = undef;
    }
    #
    # Datestamp
    my $description = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
    print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
        '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
            "\n";
    #
    # Print out the headings
    my $sequence_row = '';
    my $resource_row = undef;
    foreach my $field (&get_student_fields_to_show()) {
        $sequence_row .='"",';
        $resource_row .= '"'.&Apache::loncommon::csv_translate($field).'",';
    }
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        $sequence_row .= '"'.
            &Apache::loncommon::csv_translate($seq->{'title'}).'",';
        my $count = 0;
        if ($chosen_output->{'every_problem'}) {
            foreach my $res (@{$seq->{'contents'}}) {
                if ($res->{'type'} ne 'assessment'  || 
                    ! exists($res->{'parts'})       ||
                    ref($res->{'parts'}) ne 'ARRAY' ||
                    scalar(@{$res->{'parts'}}) < 1) {
                    next;
                }
                foreach my $part (@{$res->{'parts'}}) {
                    $resource_row .= '"'.
                        &Apache::loncommon::csv_translate($res->{'title'}.
                                                          ', Part '.$part
                                                          ).'",';
                    $count++;
                }
            }
        }
        $sequence_row.='"",'x$count;
        if ($chosen_output->{'sequence_sum'}) {
            if($chosen_output->{'correct'}) {
                $resource_row .= '"sum",';
            } else {
                $resource_row .= '"score",';
            }
        }
        if ($chosen_output->{'sequence_max'}) {
            $sequence_row.= '"",';
            $resource_row .= '"maximum possible",';
        }
    }
    if ($chosen_output->{'grand_total'}) {
        $sequence_row.= '"",';
        $resource_row.= '"Total",';
    } 
    if ($chosen_output->{'grand_maximum'}) {
        $sequence_row.= '"",';
        $resource_row.= '"Maximum",';
    } 
    chomp($sequence_row);
    chomp($resource_row);
    print $outputfile $sequence_row."\n";
    print $outputfile $resource_row."\n";
    return;
}

sub csv_outputstudent {
    my ($r,$student) = @_;
    return if ($request_aborted);
    return if (! defined($outputfile));
    my $Str = '';
    #
    # Output student fields
    my @to_show = &get_student_fields_to_show();
    foreach my $field (@to_show) {
        $Str .= '"'.&Apache::loncommon::csv_translate($student->{$field}).'",';
    }
    #
    # Get student assessment data
    my %StudentsData;
    my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
                                                        $student->{'domain'},
                                                        undef,
                                                   $ENV{'request.course.id'});
    if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
        %StudentsData = @tmp;
    }
    #
    # Output performance data
    my $total = 0;
    my $maximum = 0;
    foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        my ($performance,$performance_length,$score,$seq_max,$rawdata);
        if ($chosen_output->{'tries'}){
            ($performance,$performance_length,$score,$seq_max,$rawdata) =
                &StudentTriesOnSequence($student,\%StudentsData,
                                        $seq,'no');
        } else {
            ($performance,$performance_length,$score,$seq_max,$rawdata) =
                &StudentPerformanceOnSequence($student,\%StudentsData,
                                              $seq,'no');
        }
        if ($chosen_output->{'every_problem'}) {
            if ($chosen_output->{'correct'}) {
                $score = 0;
                # Deal with number of parts correct data
                $Str .= '"'.join('","',( map { if ($_>0) { 
                                                   $score += 1;
                                                   1; 
                                               } else { 
                                                   0; 
                                               }
                                             } @$rawdata)).'",';
            } else {
                $Str .= '"'.join('","',(@$rawdata)).'",';
            }
        }
        if ($chosen_output->{'sequence_sum'}) {
            $Str .= '"'.$score.'",';
        } 
        if ($chosen_output->{'sequence_max'}) {
            $Str .= '"'.$seq_max.'",';
        }
        $total+=$score;
        $maximum += $seq_max;
    }
    if ($chosen_output->{'grand_total'}) {
        $Str .= '"'.$total.'",';
    }
    if ($chosen_output->{'grand_maximum'}) {
        $Str .= '"'.$maximum.'",';
    }
    chop($Str);
    $Str .= "\n";
    print $outputfile $Str;
    #
    # Update the progress window
    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
    return;
}

sub csv_finish {
    my ($r) = @_;
    return if ($request_aborted);
    return if (! defined($outputfile));
    close($outputfile);
    #
    my $c = $r->connection();
    return if ($c->aborted());
    #
    # Close the progress window
    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
    #
    # Tell the user where to get their csv file
    $r->print('<br />'.
              '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
    $r->rflush();
    return;
    
}

}

#######################################################
#######################################################

=pod

=item &StudentTriesOnSequence()

Inputs:

=over 4

=item $student

=item $studentdata Hash ref to all student data

=item $seq Hash ref, the sequence we are working on

=item $links if defined we will output links to each resource.

=back

=cut

#######################################################
#######################################################
sub StudentTriesOnSequence {
    my ($student,$studentdata,$seq,$links) = @_;
    $links = 'no' if (! defined($links));
    my $Str = '';
    my ($sum,$max) = (0,0);
    my $performance_length = 0;
    my @TriesData = ();
    my $tries;
    my $hasdata = 0; # flag - true if the student has any data on the sequence
    foreach my $resource (@{$seq->{'contents'}}) {
        next if ($resource->{'type'} ne 'assessment');
        my $resource_data = $studentdata->{$resource->{'symb'}};
        my $value = '';
        foreach my $partnum (@{$resource->{'parts'}}) {
            $tries = undef;
            $max++;
            $performance_length++;
            my $symbol = ' '; # default to space
            #
            my $awarded = 0;
            if (exists($resource_data->{'resource.'.$partnum.'.awarded'})) {
                $awarded = $resource_data->{'resource.'.$partnum.'.awarded'};
                $awarded = 0 if (! $awarded);
            }
            #
            my $status = '';
            if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
                $status = $resource_data->{'resource.'.$partnum.'.solved'};
            }
            #
            my $tries = 0;
            if(exists($resource_data->{'resource.'.$partnum.'.tries'})) {
                $tries = $resource_data->{'resource.'.$partnum.'.tries'};
                $hasdata =1;
            }
            #
            if ($awarded > 0) {
                # The student has gotten the problem correct to some degree
                if ($status eq 'excused') {
                    $symbol = 'x';
                    $max--;
                } elsif ($status eq 'correct_by_override') {
                    $symbol = '+';
                    $sum++;
                } elsif ($tries > 0) {
                    if ($tries > 9) {
                        $symbol = '*';
                    } else {
                        $symbol = $tries;
                    }
                    $sum++;
                } else {
                    $symbol = '+';
                    $sum++;
                }
            } else {
                # The student has the problem incorrect or it is ungraded
                if ($status eq 'excused') {
                    $symbol = 'x';
                    $max--;
                } elsif ($status eq 'incorrect_by_override') {
                    $symbol = '-';
                } elsif ($status eq 'ungraded_attempted') {
                    $symbol = '#';
                } elsif ($status eq 'incorrect_attempted' ||
                         $tries > 0)  {
                    $symbol = '.';
                } else {
                    # Problem is wrong and has not been attempted.
                    $symbol=' ';
                }
            }
            #
            if (! defined($tries)) {
                $tries = $symbol;
            }
            push (@TriesData,$tries);
            #
            if ( ($links eq 'yes' && $symbol ne ' ') ||
                 ($links eq 'all')) {
                if (length($symbol) > 1) {
                    &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
                }
                $symbol = '<a href="/adm/grades'.
                    '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
                        '&student='.$student->{'username'}.
                            '&userdom='.$student->{'domain'}.
                                '&command=submission">'.$symbol.'</a>';
            }
            $value .= $symbol;
        }
        $Str .= $value;
    }
    if ($seq->{'randompick'}) {
        $max = $seq->{'randompick'};
    }
    if (! $hasdata && $sum == 0) {
        $sum = ' ';
    }
    return ($Str,$performance_length,$sum,$max,\@TriesData);
}

#######################################################
#######################################################

=pod

=item &StudentPerformanceOnSequence()

Inputs:

=over 4

=item $student

=item $studentdata Hash ref to all student data

=item $seq Hash ref, the sequence we are working on

=item $links if defined we will output links to each resource.

=back

=cut

#######################################################
#######################################################
sub StudentPerformanceOnSequence {
    my ($student,$studentdata,$seq,$links) = @_;
    $links = 'no' if (! defined($links));
    my $Str = ''; # final result string
    my ($score,$max) = (0,0);
    my $performance_length = 0;
    my $symbol;
    my @ScoreData = ();
    my $partscore;
    my $hasdata = 0; # flag, 0 if there were no submissions on the sequence
    foreach my $resource (@{$seq->{'contents'}}) {
        next if ($resource->{'type'} ne 'assessment');
        my $resource_data = $studentdata->{$resource->{'symb'}};
        foreach my $part (@{$resource->{'parts'}}) {
            $partscore = undef;
            my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
                                              $resource->{'symb'},
                                              $student->{'domain'},
                                              $student->{'username'},
                                              $student->{'section'});
            if (!defined($weight) || ($weight eq '')) { 
                $weight=1;
            }
            #
            $max += $weight; # see the 'excused' branch below...
            $performance_length++; # one character per part
            $symbol = ' '; # default to space
            #
            my $awarded;
            if (exists($resource_data->{'resource.'.$part.'.awarded'})) {
                $awarded = $resource_data->{'resource.'.$part.'.awarded'};
                $awarded = 0 if (! $awarded);
                $hasdata = 1;
            }
            #
            $partscore = $weight*$awarded;
            if (! defined($awarded)) {
                $partscore = undef;
            }
            $score += $partscore;
            $symbol = $partscore; 
            if (abs($symbol - sprintf("%.0f",$symbol)) < 0.001) {
                $symbol = sprintf("%.0f",$symbol);
            }
            if (length($symbol) > 1) {
                $symbol = '*';
            }
            if (exists($resource_data->{'resource.'.$part.'.solved'})) {
                my $status = $resource_data->{'resource.'.$part.'.solved'};
                if ($status eq 'excused') {
                    $symbol = 'x';
                    $max -= $weight; # Do not count 'excused' problems.
                }
                $hasdata = 1;
            } else {
                # Unsolved.  Did they try?
                if (exists($resource_data->{'resource.'.$part.'.tries'})){
                    $symbol = '.';
                    $hasdata = 1;
                } else {
                    $symbol = ' ';
                }
            }
            #
            if (! defined($partscore)) {
                $partscore = $symbol;
            }
            push (@ScoreData,$partscore);
            #
            if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) {
                $symbol = '<a href="/adm/grades'.
                    '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
                    '&student='.$student->{'username'}.
                    '&userdom='.$student->{'domain'}.
                    '&command=submission">'.$symbol.'</a>';
            }
            $Str .= $symbol;
        }
    }
    if (! $hasdata && $score == 0) {
        $score = ' ';
    }
    return ($Str,$performance_length,$score,$max,\@ScoreData);
}

#######################################################
#######################################################

=pod

=item &CreateLegend()

This function returns a formatted string containing the legend for the
chart.  The legend describes the symbols used to represent grades for
problems.

=cut

#######################################################
#######################################################
sub CreateLegend {
    my $Str = "<p><pre>".
              "   1  correct by student in 1 try\n".
              "   7  correct by student in 7 tries\n".
              "   *  correct by student in more than 9 tries\n".
	      "   +  correct by hand grading or override\n".
              "   -  incorrect by override\n".
	      "   .  incorrect attempted\n".
	      "   #  ungraded attempted\n".
              "      not attempted (blank field)\n".
	      "   x  excused".
              "</pre><p>";
    return $Str;
}

#######################################################
#######################################################

=pod 

=back

=cut

#######################################################
#######################################################

1;

__END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>