File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.35: download - view: text, annotated - select for diffs
Wed Mar 5 20:31:13 2003 UTC (21 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Renamed 'Sequences and Folders' to the more generic 'Problem Sets'
Changed method of determining output mode variables.  The globals
$output_mode, $show, and $show_links are now given values from the
hashes in the array @OutputOptions.
Fixed bug which caused the five student groupings to be off when a
student did not have any course data.
Added beginnings of multi-sheet excel file output.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentassessment.pm,v 1.35 2003/03/05 20:31:13 matthew Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: # LON-CAPA is free software; you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation; either version 2 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # LON-CAPA is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with LON-CAPA; if not, write to the Free Software
   20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21: #
   22: # /home/httpd/html/adm/gpl.txt
   23: #
   24: # http://www.lon-capa.org/
   25: #
   26: # (Navigate problems for statistical reports
   27: #
   28: #######################################################
   29: #######################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: lonstudentassessment
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Presents assessment data about a student or a group of students.
   40: 
   41: =head1 Subroutines
   42: 
   43: =over 4 
   44: 
   45: =cut
   46: 
   47: #######################################################
   48: #######################################################
   49: 
   50: package Apache::lonstudentassessment;
   51: 
   52: use strict;
   53: use Apache::lonstatistics;
   54: use Apache::lonhtmlcommon;
   55: use Apache::loncoursedata;
   56: use Apache::lonnet; # for logging porpoises
   57: use Spreadsheet::WriteExcel;
   58: 
   59: #######################################################
   60: #######################################################
   61: =pod
   62: 
   63: =item Package Variables
   64: 
   65: =over 4
   66: 
   67: =item $Statistics Hash ref to store student data.  Indexed by symb,
   68:       contains hashes with keys 'score' and 'max'.
   69: 
   70: =cut
   71: 
   72: #######################################################
   73: #######################################################
   74: 
   75: my $Statistics;
   76: 
   77: #######################################################
   78: #######################################################
   79: 
   80: =pod
   81: 
   82: =item $show_links 'yes' or 'no' for linking to student performance data
   83: 
   84: =item $output_mode 'html', 'excel', or 'csv' for output mode
   85: 
   86: =item $show 'all', 'totals', or 'scores' determines how much data is output
   87: 
   88: =cut
   89: 
   90: #######################################################
   91: #######################################################
   92: my $show_links;
   93: my $output_mode;
   94: my $show;
   95: 
   96: #######################################################
   97: #######################################################
   98: # End of package variable declarations
   99: 
  100: =pod
  101: 
  102: =back
  103: 
  104: =cut
  105: 
  106: #######################################################
  107: #######################################################
  108: 
  109: =pod
  110: 
  111: =item &BuildStudentAssessmentPage()
  112: 
  113: Inputs: 
  114: 
  115: =over 4
  116: 
  117: =item $r Apache Request
  118: 
  119: =item $c Apache Connection 
  120: 
  121: =back
  122: 
  123: =cut
  124: 
  125: #######################################################
  126: #######################################################
  127: sub BuildStudentAssessmentPage {
  128:     my ($r,$c)=@_;
  129:     undef($Statistics);
  130:     #
  131:     # Print out the HTML headers for the interface
  132:     #    This also parses the output mode selector
  133:     #    This step must always be done.
  134:     $r->print(&CreateInterface());
  135:     $r->print('<input type="hidden" name="notfirstrun" value="true" />');
  136:     $r->rflush();
  137:     if (! exists($ENV{'form.notfirstrun'})) {
  138:         $r->print(<<ENDMSG);
  139: <p>
  140: <font size="+1">
  141: Please make your selections in the boxes above and hit 
  142: the button marked &quot;Update&nbsp;Display&quot;.
  143: </font>
  144: </p>
  145: ENDMSG
  146: #        $r->print(&OutputDescriptions());
  147:         return;
  148:     }
  149:     #
  150:     #
  151:     my $initialize     = \&html_initialize;
  152:     my $output_student = \&html_outputstudent;
  153:     my $finish         = \&html_finish;
  154:     #
  155:     if ($output_mode eq 'excel') {
  156:         $initialize     = \&excel_initialize;
  157:         $output_student = \&excel_outputstudent;
  158:         $finish         = \&excel_finish;
  159:     } elsif ($output_mode eq 'multi-sheet excel') {
  160:         $initialize     = \&multi_sheet_excel_initialize;
  161:         $output_student = \&multi_sheet_excel_outputstudent;
  162:         $finish         = \&multi_sheet_excel_finish;
  163:     } elsif ($output_mode eq 'csv') {
  164:         $initialize     = \&csv_initialize;
  165:         $output_student = \&csv_outputstudent;
  166:         $finish         = \&csv_finish;
  167:     }
  168:     #
  169:     if($c->aborted()) {  return ; }
  170:     #
  171:     # Call the initialize routine selected above
  172:     $initialize->($r);
  173:     foreach my $student (@Apache::lonstatistics::Students) {
  174:         if($c->aborted()) { 
  175:             $finish->($r);
  176:             return ; 
  177:         }
  178:         # Call the output_student routine selected above
  179:         $output_student->($r,$student);
  180:     }
  181:     # Call the "finish" routine selected above
  182:     $finish->($r);
  183:     #
  184:     return;
  185: }
  186: 
  187: #######################################################
  188: #######################################################
  189: 
  190: sub get_student_fields_to_show {
  191:     my @to_show = @Apache::lonstatistics::SelectedStudentData;
  192:     foreach (@to_show) {
  193:         if ($_ eq 'all') {
  194:             @to_show = @Apache::lonstatistics::StudentDataOrder;
  195:             last;
  196:         }
  197:     }
  198:     return @to_show;
  199: }
  200: 
  201: sub get_sequences_to_show {
  202:     my @Sequences;
  203:     foreach my $map_symb (@Apache::lonstatistics::SelectedMaps) {
  204:         foreach my $sequence (@Apache::lonstatistics::Sequences) {
  205:             next if ($sequence->{'symb'} ne $map_symb && $map_symb ne 'all');
  206:             next if ($sequence->{'num_assess'} < 1);
  207:             push (@Sequences,$sequence);
  208:         }
  209:     }
  210:     return @Sequences;
  211: }
  212: 
  213: 
  214: #######################################################
  215: #######################################################
  216: 
  217: =pod
  218: 
  219: =item &CreateInterface()
  220: 
  221: Called by &BuildStudentAssessmentPage to create the top part of the
  222: page which displays the chart.
  223: 
  224: Inputs: None
  225: 
  226: Returns:  A string containing the HTML for the headers and top table for 
  227: the chart page.
  228: 
  229: =cut
  230: 
  231: #######################################################
  232: #######################################################
  233: sub CreateInterface {
  234:     my $Str = '';
  235: #    $Str .= &CreateLegend();
  236:     $Str .= '<table cellspacing="5">'."\n";
  237:     $Str .= '<tr>';
  238:     $Str .= '<td align="center"><b>Sections</b></td>';
  239:     $Str .= '<td align="center"><b>Student Data</b></td>';
  240:     $Str .= '<td align="center"><b>Problem Sets</b></td>';
  241:     $Str .= '<td align="center"><b>Output Format</b></td>';
  242:     $Str .= '</tr>'."\n";
  243:     #
  244:     $Str .= '<tr><td align="center">'."\n";
  245:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  246:     $Str .= '</td><td align="center">';
  247:     my $only_seq_with_assessments = sub { 
  248:         my $s=shift;
  249:         if ($s->{'num_assess'} < 1) { 
  250:             return 0;
  251:         } else { 
  252:             return 1;
  253:         }
  254:     };
  255:     $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',
  256:                                                       5,undef);
  257:     $Str .= '</td><td>'."\n";
  258:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  259:                                               $only_seq_with_assessments);
  260:     $Str .= '</td><td>'."\n";
  261:     $Str .= &CreateAndParseOutputSelector();
  262:     $Str .= '</td></tr>'."\n";
  263:     $Str .= '</table>'."\n";
  264:     return $Str;
  265: }
  266: 
  267: #######################################################
  268: #######################################################
  269: 
  270: =pod
  271: 
  272: =item &CreateAndParseOutputSelector()
  273: 
  274: =cut
  275: 
  276: #######################################################
  277: #######################################################
  278: my @OutputOptions = 
  279:     ({ name  => 'HTML, with links',
  280:        value => 'html, with links',
  281:        description => 'Output HTML with each symbol linked to the problem '.
  282: 	   'which generated it.',
  283:        mode => 'html',
  284:        show => 'all',
  285:        show_links => 'yes',
  286:        },
  287:      { name  => 'HTML, without links',
  288:        value => 'html, without links',
  289:        description => 'Output HTML.  By not including links, the size of the'.
  290: 	   ' web page is greatly reduced.  If your browser crashes on the '.
  291: 	   'full display, try this.',
  292:        mode => 'html',
  293:        show => 'all',
  294:        show_links => 'no',
  295:            },
  296:      { name  => 'HTML, scores only',
  297:        value => 'html, scores only',
  298:        description => 'Output HTML, only showing the total number of correct'.
  299: 	   ' problems (or problem parts) and not the maximum possible for '.
  300: 	   'each student',
  301:        mode => 'html',
  302:        show => 'scores',
  303:        show_links => 'no',
  304:            },
  305:      { name  => 'HTML, totals',
  306:        value => 'html, totals',
  307:        description => 'Output HTML, but only the summary statistics for each'.
  308: 	   ' sequence selected.',
  309:        mode => 'html',
  310:        show => 'totals',
  311:        show_links => 'no',
  312:        },
  313:      { name  => 'Excel, scores only',
  314:        value => 'excel, scores only',
  315:        description => 'Output an Excel file (compatable with Excel 95), '.
  316: 	   'with a single column for each sequence showing the students '.
  317: 	   'score.',
  318:        mode => 'excel',
  319:        show => 'scores',
  320:        show_links => 'no',
  321:            },
  322:      { name  => 'Excel, totals',
  323:        value => 'excel, totals',
  324:        description => 'Output an Excel file (compatable with Excel 95), '.
  325: 	   'with two columns for each sequence, the students score on the '.
  326: 	   'sequence and the students maximum possible on the sequence',
  327:        mode => 'excel',
  328:        show => 'totals',
  329:        show_links => 'no',
  330:            },
  331:      { name  => 'multi-sheet Excel',
  332:        value => 'multi-sheet excel',
  333:        description => 'Output an Excel file (compatable with Excel 95), '.
  334: 	   'with a seperate worksheet for each sequence you have selected '.
  335:            'the data for each problem part '.
  336:            '(number of tries, status, points awarded) will be listed.',
  337:        mode => 'multi-sheet excel',
  338:        show => 'totals',
  339:        show_links => 'no',
  340:            },
  341:      { name  => 'multi-sheet Excel, by section',
  342:        value => 'multi-sheet excel, by section',
  343:        description => 'Output an Excel file (compatable with Excel 95), '.
  344: 	   'with a seperate worksheet for each sequence you have selected '.
  345:            'the data for each problem part '.
  346:            '(number of tries, status, points awarded) will be listed.  '.
  347:            'There will be one Excel workbook for each section selected.',
  348:        mode => 'multi-sheet excel',
  349:        show => 'by section',
  350:        show_links => 'no',
  351:            },
  352:      { name  => 'CSV, everything',
  353:        value => 'csv, everything',
  354:        description => '',
  355:        mode => 'csv',
  356:        show => 'all',
  357:        show_links => 'no',
  358:            },
  359:      { name  => 'CSV, scores only',
  360:        value => 'csv, scores only',
  361:        description => '',
  362:        mode => 'csv',
  363:        show => 'scores',
  364:        show_links => 'no',
  365:            },
  366:      { name  => 'CSV, totals',
  367:        value => 'csv, totals',
  368:        description => '',
  369:        mode => 'csv',
  370:        show => 'totals',
  371:        show_links => 'no',
  372:            },
  373:      );
  374: 
  375: sub OutputDescriptions {
  376:     my $Str = '';
  377:     $Str .= "<h2>Output Modes</h2>\n";
  378:     $Str .= "<dl>\n";
  379:     foreach my $outputmode (@OutputOptions) {
  380: 	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
  381: 	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
  382:     }
  383:     $Str .= "</dl>\n";
  384:     return $Str;
  385: }
  386: 
  387: sub CreateAndParseOutputSelector {
  388:     my $Str = '';
  389:     my $elementname = 'outputmode';
  390:     #
  391:     # Format for output options is 'mode, restrictions';
  392:     my $selected = 'html, with links';
  393:     if (exists($ENV{'form.'.$elementname})) {
  394:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
  395:             $selected = $ENV{'form.'.$elementname}->[0];
  396:         } else {
  397:             $selected = $ENV{'form.'.$elementname};
  398:         }
  399:     }
  400:     #
  401:     # Set package variables describing output mode
  402:     $show_links  = 'no';
  403:     $output_mode = 'html';
  404:     $show        = 'all';
  405:     foreach my $option (@OutputOptions) {
  406:         next if ($option->{'value'} ne $selected);
  407:         $output_mode = $option->{'mode'};
  408:         $show        = $option->{'show'};
  409:         $show_links  = $option->{'show_links'};
  410:     }
  411: 
  412:     #
  413:     # Build the form element
  414:     $Str = qq/<select size="5" name="$elementname">/;
  415:     foreach my $option (@OutputOptions) {
  416:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
  417:         $Str .= " selected " if ($option->{'value'} eq $selected);
  418:         $Str .= ">".$option->{'name'}."<\/option>";
  419:     }
  420:     $Str .= "\n</select>";
  421:     return $Str;
  422: }
  423: 
  424: #######################################################
  425: #######################################################
  426: 
  427: =pod
  428: 
  429: =head2 HTML output routines
  430: 
  431: =item &html_initialize($r)
  432: 
  433: Create labels for the columns of student data to show.
  434: 
  435: =item &html_outputstudent($r,$student)
  436: 
  437: Return a line of the chart for a student.
  438: 
  439: =item &html_finish($r)
  440: 
  441: =cut
  442: 
  443: #######################################################
  444: #######################################################
  445: {
  446:     my $padding;
  447:     my $count;
  448: 
  449: sub html_initialize {
  450:     my ($r) = @_;
  451:     #
  452:     $padding = ' 'x3;
  453:     $count = 0;
  454:     #
  455:     my $Str = "<pre>\n";
  456:     # First, the @StudentData fields need to be listed
  457:     my @to_show = &get_student_fields_to_show();
  458:     foreach my $field (@to_show) {
  459:         my $title=$Apache::lonstatistics::StudentData{$field}->{'title'};
  460:         my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'};
  461:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
  462:         $Str .= $title.' 'x($width-$base).$padding;
  463:     }
  464:     # Now the selected sequences need to be listed
  465:     foreach my $sequence (&get_sequences_to_show) {
  466:         my $title = $sequence->{'title'};
  467:         my $base  = $sequence->{'base_width'};
  468:         my $width = $sequence->{'width'};
  469:         $Str .= $title.' 'x($width-$base).$padding;
  470:     }
  471:     $Str .= "total (of shown problems)</pre>\n";
  472:     $Str .= "<pre>";
  473:     $r->print($Str);
  474:     $r->rflush();
  475:     return;
  476: }
  477: 
  478: sub html_outputstudent {
  479:     my ($r,$student) = @_;
  480:     my $Str = '';
  481:     #
  482:     if($count++ % 5 == 0 && $count > 0) {
  483:         $r->print("</pre><pre>");
  484:     }
  485:     # First, the @StudentData fields need to be listed
  486:     my @to_show = &get_student_fields_to_show();
  487:     foreach my $field (@to_show) {
  488:         my $title=$student->{$field};
  489:         my $base = length($title);
  490:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
  491:         $Str .= $title.' 'x($width-$base).$padding;
  492:     }
  493:     # Get ALL the students data
  494:     my %StudentsData;
  495:     my @tmp = &Apache::loncoursedata::get_current_state
  496:         ($student->{'username'},$student->{'domain'},undef,
  497:          $ENV{'request.course.id'});
  498:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
  499:         %StudentsData = @tmp;
  500:     }
  501:     if (scalar(@tmp) < 1) {
  502:         $Str .= '<font color="blue">No Course Data</font>'."\n";
  503:         $r->print($Str);
  504:         $r->rflush();
  505:         return;
  506:     }
  507:     #
  508:     # By sequence build up the data
  509:     my $studentstats;
  510:     my $PerformanceStr = '';
  511:     foreach my $seq (&get_sequences_to_show) {
  512:         my ($performance,$score,$seq_max) =
  513:             &StudentPerformanceOnSequence($student,\%StudentsData,
  514:                                           $seq,$show_links);
  515:         my $ratio = $score.'/'.$seq_max;
  516:         #
  517:         if ($show eq 'totals') {
  518:             $performance = ' 'x(length($seq_max)-length($score)).$ratio;
  519:             $performance .= ' 'x($seq->{'width'}-length($performance));
  520:         } elsif ($show eq 'scores') {
  521:             $performance = $score;
  522:             $performance .= ' 'x($seq->{'width'}-length($performance));
  523:         } else {
  524:             # Pad with extra spaces
  525:             $performance .= ' 'x($seq->{'width'}-$seq_max-
  526:                                  length($ratio)
  527:                                  ).$ratio;
  528:         }
  529:         #
  530:         $Str .= $performance.$padding;
  531:         #
  532:         $studentstats->{$seq->{'symb'}}->{'score'}= $score;
  533:         $studentstats->{$seq->{'symb'}}->{'max'}  = $seq_max;
  534:     }
  535:     #
  536:     # Total it up and store the statistics info.
  537:     my ($score,$max) = (0,0);
  538:     while (my ($symb,$seq_stats) = each (%{$studentstats})) {
  539:         $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
  540:         $Statistics->{$symb}->{'max'}   += $seq_stats->{'max'};
  541:         $score += $seq_stats->{'score'};
  542:         $max   += $seq_stats->{'max'};
  543:     }
  544:     $Str .= ' '.' 'x(length($max)-length($score)).$score.'/'.$max;
  545:     $Str .= " \n";
  546:     $r->print($Str);
  547:     #
  548:     $r->rflush();
  549:     return;
  550: }    
  551: 
  552: sub html_finish {
  553:     my ($r) = @_;
  554:     $r->print("</pre>\n"); 
  555:     $r->rflush();
  556:     return;
  557: }
  558: 
  559: }
  560: 
  561: #######################################################
  562: #######################################################
  563: 
  564: =pod
  565: 
  566: =head2 Multi-Sheet EXCEL subroutines
  567: 
  568: =item &multi_sheet_excel_initialize($r)
  569: 
  570: =item &multi_sheet_excel_outputstudent($r,$student)
  571: 
  572: =item &multi_sheet_excel_finish($r)
  573: 
  574: =cut
  575: 
  576: #######################################################
  577: #######################################################
  578: {
  579: 
  580: sub multi_sheet_excel_initialize {
  581:     my ($r)=@_;
  582:     $r->print("<h1>Not yet implemented</h1>");
  583:     # 
  584:     # Estimate the size of the file.  We would like to have < 5 megs of data.
  585:     my $max_size = 5000000;
  586:     my $num_students  = scalar(@Apache::lonstatistics::Students);
  587:     my $num_sequences = 0;
  588:     my $num_data_per_part  = 2; # 'status' and 'numtries'
  589:     my $fields_per_student = scalar(&get_student_fields_to_show());
  590:     my $bytes_per_field    = 20; # Back of the envelope calculation
  591:     foreach my $seq (&get_sequences_to_show) {
  592:         $num_sequences++ if ($seq->{'num_assess'} > 0);
  593:         $fields_per_student += $num_data_per_part * $seq->{'num_assess_parts'};
  594:     }
  595:     my $size_estimate = $fields_per_student*$num_students*$bytes_per_field;
  596:     #
  597:     # Compute number of workbooks
  598:     my $num_workbooks = 1;
  599:     if ($size_estimate > $max_size) { # try to stay under 5 megs
  600:         $num_workbooks += int($size_estimate / $max_size);
  601:     }
  602:     if ($show eq 'by section') {
  603:         if (@Apache::lonstatistics::SelectedSections > 1 && 
  604:             $Apache::lonstatistics::SelectedSections[0] ne 'all') {
  605:             $num_workbooks = scalar(@Apache::lonstatistics::SelectedSections);
  606:         } else {
  607:             # @Apache::lonstatistics::Sections contains 'all' as well.
  608:             $num_workbooks = scalar(@Apache::lonstatistics::Sections) - 1;
  609:         }
  610:     }
  611:     
  612:     $r->print("Maximum allowed size: ".$max_size." bytes<br />");
  613:     $r->print("Number of students: ".$num_students."<br />");
  614:     $r->print("Number of fields per student: ".$fields_per_student."<br />");
  615:     $r->print("Total number of fields: ".($fields_per_student*$num_students).
  616:               "<br />");
  617:     $r->print("Bytes per field: ".$bytes_per_field." (estimated)"."<br />");
  618:     $r->print("Estimated size: ".$size_estimate." bytes<br />");
  619:     $r->print("Number of workbooks: ".$num_workbooks."<br />");
  620:     $r->rflush();
  621:     return;
  622: }
  623: 
  624: sub multi_sheet_excel_outputstudent {
  625:     my ($r,$student) = @_;
  626: }
  627: 
  628: sub multi_sheet_excel_finish {
  629:     my ($r) = @_;
  630: }
  631: 
  632: }
  633: #######################################################
  634: #######################################################
  635: 
  636: =pod
  637: 
  638: =head2 EXCEL subroutines
  639: 
  640: =item &excel_initialize($r)
  641: 
  642: =item &excel_outputstudent($r,$student)
  643: 
  644: =item &excel_finish($r)
  645: 
  646: =cut
  647: 
  648: #######################################################
  649: #######################################################
  650: {
  651: 
  652: my $excel_sheet;
  653: my $excel_workbook;
  654: 
  655: my $filename;
  656: my $rows_output;
  657: my $cols_output;
  658: 
  659: my $num_students;
  660: my $start_time;
  661: 
  662: sub excel_initialize {
  663:     my ($r) = @_;
  664:     #
  665:     $filename = '/prtspool/'.
  666:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  667:             time.'_'.rand(1000000000).'.xls';
  668:     #
  669:     $excel_workbook = undef;
  670:     $excel_sheet = undef;
  671:     #
  672:     $rows_output = 0;
  673:     $cols_output = 0;
  674:     #
  675:     $num_students = 0;
  676:     $start_time   = time;
  677:     #
  678:     # Create sheet
  679:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  680:     #
  681:     # Check for errors
  682:     if (! defined($excel_workbook)) {
  683:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  684:         $r->print("Problems creating new Excel file.  ".
  685:                   "This error has been logged.  ".
  686:                   "Please alert your LON-CAPA administrator");
  687:         return ;
  688:     }
  689:     #
  690:     # The excel spreadsheet stores temporary data in files, then put them
  691:     # together.  If needed we should be able to disable this (memory only).
  692:     # The temporary directory must be specified before calling 'addworksheet'.
  693:     # File::Temp is used to determine the temporary directory.
  694:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  695:     #
  696:     # Add a worksheet
  697:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  698:     if (length($sheetname) > 31) {
  699:         $sheetname = substr($sheetname,0,31);
  700:     }
  701:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
  702:     #
  703:     # Put the course description in the header
  704:     $excel_sheet->write($rows_output,$cols_output++,
  705:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
  706:     $cols_output += 3;
  707:     #
  708:     # Put a description of the sections listed
  709:     my $sectionstring = '';
  710:     my @Sections = @Apache::lonstatistics::SelectedSections;
  711:     if (scalar(@Sections) > 1) {
  712:         if (scalar(@Sections) > 2) {
  713:             my $last = pop(@Sections);
  714:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
  715:         } else {
  716:             $sectionstring = "Sections ".join(' and ',@Sections);
  717:         }
  718:     } else {
  719:         if ($Sections[0] eq 'all') {
  720:             $sectionstring = "All sections";
  721:         } else {
  722:             $sectionstring = "Section ".$Sections[0];
  723:         }
  724:     }
  725:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
  726:     $cols_output += scalar(@Sections);
  727:     #
  728:     # Put the date in there too
  729:     $excel_sheet->write($rows_output,$cols_output++,
  730:                         'Compiled on '.localtime(time));
  731:     #
  732:     $rows_output++;
  733:     #
  734:     # Add the student headers
  735:     $cols_output = 0;
  736:     foreach my $field (&get_student_fields_to_show()) {
  737:         $excel_sheet->write($rows_output,$cols_output++,$field);
  738:     }
  739:     #
  740:     # Add the Sequence Headers
  741:     foreach my $seq (&get_sequences_to_show) {
  742:         $excel_sheet->write($rows_output,$cols_output,$seq->{'title'});
  743:         if ($show eq 'totals') {
  744:             $excel_sheet->write($rows_output+1,$cols_output,'score');
  745:             $excel_sheet->write($rows_output+1,$cols_output+1,'maximum');
  746:             $cols_output += 2;
  747:         } else {
  748:             $cols_output++;
  749:         }
  750:     }
  751:     #
  752:     # Bookkeeping
  753:     if ($show eq 'totals') {
  754:         $rows_output += 2;
  755:     } else {
  756:         $rows_output += 1;
  757:     }
  758:     #
  759:     # Let the user know what we are doing
  760:     my $studentcount = scalar(@Apache::lonstatistics::Students); 
  761:     $r->print("<h1>Compiling Excel spreadsheet for ".
  762:               $studentcount.' student');
  763:     $r->print('s') if ($studentcount > 1);
  764:     $r->print("</h1>\n");
  765:     $r->rflush();
  766:     #
  767:     return;
  768: }
  769: 
  770: sub excel_outputstudent {
  771:     my ($r,$student) = @_;
  772:     return if (! defined($excel_sheet));
  773:     $cols_output=0;
  774:     #
  775:     # Write out student data
  776:     my @to_show = &get_student_fields_to_show();
  777:     foreach my $field (@to_show) {
  778:         $excel_sheet->write($rows_output,$cols_output++,$student->{$field});
  779:     }
  780:     #
  781:     # Get student assessment data
  782:     my %StudentsData;
  783:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
  784:                                                         $student->{'domain'},
  785:                                                         undef,
  786:                                                    $ENV{'request.course.id'});
  787:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
  788:         %StudentsData = @tmp;
  789:     }
  790:     #
  791:     # Write out sequence scores and totals data
  792:     foreach my $seq (&get_sequences_to_show) {
  793:         my ($performance,$score,$seq_max) =
  794:             &StudentPerformanceOnSequence($student,\%StudentsData,
  795:                                           $seq,'no');
  796:         if ($show eq 'totals' || $show eq 'scores') {
  797:             $excel_sheet->write($rows_output,$cols_output++,$score);
  798:         }
  799:         if ($show eq 'totals') {
  800:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
  801:         }
  802:     }
  803:     #
  804:     # Bookkeeping
  805:     $rows_output++; 
  806:     $cols_output=0;
  807:     #
  808:     # Time estimate
  809:     $num_students++;
  810:     if ($num_students % 10 == 0) {
  811:         my $time_est = (time - $start_time)/$num_students * 
  812:             (scalar(@Apache::lonstatistics::Students)-$num_students);
  813:         $time_est = int($time_est);
  814:         if (int ($time_est/60) > 0) {
  815:             my $min = int($time_est/60);
  816:             my $sec = $time_est % 60;
  817:             $time_est = $min.' minutes';
  818:             if ($sec > 1) {
  819:                 $time_est.= ', '.$sec.' seconds';
  820:             } elsif ($sec > 0) {
  821:                 $time_est.= ', '.$sec.' second';
  822:             }
  823:         } else {
  824:             $time_est .= ' seconds';
  825:         }
  826:         $r->print($num_students.' out of '.
  827:                   (scalar(@Apache::lonstatistics::Students)).
  828:                   " students processed.  ".
  829:                   $time_est." remain.  Elapsed: ".(time - $start_time).
  830:                   "<br />\n");
  831:         $r->rflush();
  832:     }
  833:     return;
  834: }
  835: 
  836: sub excel_finish {
  837:     my ($r) = @_;
  838:     return if (! defined($excel_sheet));
  839:     #
  840:     # Write the excel file
  841:     $excel_workbook->close();
  842:     my $c = $r->connection();
  843:     #
  844:     return if($c->aborted());
  845:     #
  846:     # Tell the user where to get their excel file
  847:     $r->print('<br /><br />'.
  848:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
  849:     my $total_time = time - $start_time;
  850:     if (int ($total_time / 60) > 0) {
  851:         $total_time = int($total_time/60).' minutes, '.($total_time % 60);
  852:     }
  853:     $r->print('<br />'.$total_time.' seconds total');
  854:     $r->rflush();
  855:     return;
  856: }
  857: 
  858: }
  859: #######################################################
  860: #######################################################
  861: 
  862: =pod
  863: 
  864: =head2 CSV output routines
  865: 
  866: =item &csv_initialize($r)
  867: 
  868: =item &csv_outputstudent($r,$student)
  869: 
  870: =item &csv_finish($r)
  871: 
  872: =cut
  873: 
  874: #######################################################
  875: #######################################################
  876: {
  877: 
  878: sub csv_initialize{
  879:     my ($r) = @_;
  880:     $r->print("<h1>Not implemented yet</h1>");
  881:     return;
  882: }
  883: 
  884: sub csv_outputstudent {
  885:     my ($r,$student) = @_;
  886: }
  887: 
  888: sub csv_finish {
  889:     my ($r) = @_;
  890: }
  891: 
  892: }
  893: 
  894: #######################################################
  895: #######################################################
  896: 
  897: =pod
  898: 
  899: =item &StudentPerformanceOnSequence()
  900: 
  901: Inputs:
  902: 
  903: =over 4
  904: 
  905: =item $student
  906: 
  907: =item $studentdata Hash ref to all student data
  908: 
  909: =item $seq Hash ref, the sequence we are working on
  910: 
  911: =item $links if defined we will output links to each resource.
  912: 
  913: =back
  914: 
  915: =cut
  916: 
  917: #######################################################
  918: #######################################################
  919: sub StudentPerformanceOnSequence {
  920:     my ($student,$studentdata,$seq,$links) = @_;
  921:     $links = 'no' if (! defined($links));
  922:     my $Str = '';
  923:     my ($sum,$max) = (0,0);
  924:     foreach my $resource (@{$seq->{'contents'}}) {
  925:         next if ($resource->{'type'} ne 'assessment');
  926:         my $resource_data = $studentdata->{$resource->{'symb'}};
  927:         my $value = '';
  928:         foreach my $partnum (@{$resource->{'parts'}}) {
  929:             $max++;
  930:             my $symbol = ' '; # default to space
  931:             #
  932:             if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
  933:                 my $status = $resource_data->{'resource.'.$partnum.'.solved'};
  934:                 if ($status eq 'correct_by_override') {
  935:                     $symbol = '+';
  936:                     $sum++;
  937:                 } elsif ($status eq 'incorrect_by_override') {
  938:                     $symbol = '-';
  939:                 } elsif ($status eq 'ungraded_attempted') {
  940:                     $symbol = '#';
  941:                 } elsif ($status eq 'incorrect_attempted')  {
  942:                     $symbol = '.';
  943:                 } elsif ($status eq 'excused') {
  944:                     $symbol = 'x';
  945:                     $max--;
  946:                 } elsif ($status eq 'correct_by_student' &&
  947:                     exists($resource_data->{'resource.'.$partnum.'.tries'})){
  948:                     my $num = $resource_data->{'resource.'.$partnum.'.tries'};
  949:                     if ($num > 9) {
  950:                         $symbol = '*';
  951:                     } elsif ($num > 0) {
  952:                         $symbol = $num;
  953:                     } else {
  954:                         $symbol = ' ';
  955:                     }
  956:                     $sum++;
  957:                 } else {
  958:                     $symbol = ' ';
  959:                 }
  960:             } else {
  961:                 # Unsolved.  Did they try?
  962:                 if (exists($resource_data->{'resource.'.$partnum.'.tries'})){
  963:                     $symbol = '.';
  964:                 } else {
  965:                     $symbol = ' ';
  966:                 }
  967:             }
  968:             #
  969:             if ($links eq 'yes' && $symbol ne ' ') {
  970:                 $symbol = '<a href="/adm/grades'.
  971:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
  972:                         '&student='.$student->{'username'}.
  973:                             '&domain='.$student->{'domain'}.
  974:                                 '&command=submission">'.$symbol.'</a>';
  975:             }
  976:             $value .= $symbol;
  977:         }
  978:         $Str .= $value;
  979:     }
  980:     return ($Str,$sum,$max);
  981: }
  982: 
  983: #######################################################
  984: #######################################################
  985: sub StudentAverageTotal {
  986:     my ($cache, $students, $sequenceKeys)=@_;
  987:     my $Str = "\n<b>Summary Tables:</b>\n";
  988:     my %Correct = ();
  989:     my $ProblemsSolved = 0;
  990:     my $TotalProblems = 0;
  991:     my $StudentCount = 0;
  992: 
  993:     foreach my $name (@$students) {
  994:         $StudentCount++;
  995:         foreach my $sequence (@$sequenceKeys) {
  996:             $Correct{$sequence} +=
  997: 	       $cache->{$name.':'.$sequence.':problemsCorrect'};
  998:         }
  999: 	$ProblemsSolved += $cache->{$name.':problemsSolved'};
 1000:         $TotalProblems += $cache->{$name.':totalProblems'};
 1001:     }
 1002:     if ($StudentCount) { 
 1003:         $ProblemsSolved = sprintf( "%.2f", 
 1004:                              $ProblemsSolved/$StudentCount);
 1005:         $TotalProblems /= $StudentCount;
 1006:     } else {
 1007:         $ProblemsSolved = 0;
 1008:         $TotalProblems  = 0;
 1009:     }
 1010: 
 1011:     $Str .= '<table border=2 cellspacing="1">'."\n";
 1012:     $Str .= '<tr><td><b>Students Count</b></td><td><b>'.
 1013:             $StudentCount.'</b></td></tr>'."\n";
 1014:     $Str .= '<tr><td><b>Total Problems</b></td><td><b>'.
 1015:             $TotalProblems.'</b></td></tr>'."\n";
 1016:     $Str .= '<tr><td><b>Average Correct</b></td><td><b>'.
 1017:             $ProblemsSolved.'</b></td></tr>'."\n";
 1018:     $Str .= '</table>'."\n";
 1019: 
 1020:     $Str .= '<table border=2 cellspacing="1">'."\n";
 1021:     $Str .= '<tr><th>Title</th><th>Total Problems</th>'.
 1022:             '<th>Average Correct</th></tr>'."\n";
 1023:     foreach my $S(@$sequenceKeys) {
 1024:         my $title=$cache->{$S.':title'};
 1025: 	#$Str .= $cache->{$S.':problems'};
 1026: 	#my @problems=split(':', $cache->{$S.':problems'});
 1027: 	#my $pCount=scalar @problems;
 1028: 	my $pCount=MaxSeqPr($cache,@$students[0],$S);
 1029:         my $crr;
 1030: 	if ($StudentCount) {
 1031:             $crr=sprintf( "%.2f", $Correct{$S}/$StudentCount );
 1032:         } else {
 1033:             $crr="0.00";
 1034:         }
 1035:         $Str .= '<tr><td>'.$title.
 1036:                 '</td><td align=center>'.$pCount.
 1037:                 '</td><td align=center>'.$crr.
 1038:                 '</td></tr>'."\n";
 1039:     }
 1040: 
 1041:     $Str .= '</table>'."\n";
 1042: 
 1043:     return $Str;
 1044: }
 1045: 
 1046: #######################################################
 1047: #######################################################
 1048: 
 1049: =pod
 1050: 
 1051: =item &CreateLegend()
 1052: 
 1053: This function returns a formatted string containing the legend for the
 1054: chart.  The legend describes the symbols used to represent grades for
 1055: problems.
 1056: 
 1057: =cut
 1058: 
 1059: #######################################################
 1060: #######################################################
 1061: sub CreateLegend {
 1062:     my $Str = "<p><pre>".
 1063:               "   1  correct by student in 1 try\n".
 1064:               "   7  correct by student in 7 tries\n".
 1065:               "   *  correct by student in more than 9 tries\n".
 1066: 	      "   +  correct by hand grading or override\n".
 1067:               "   -  incorrect by override\n".
 1068: 	      "   .  incorrect attempted\n".
 1069: 	      "   #  ungraded attempted\n".
 1070:               "      not attempted (blank field)\n".
 1071: 	      "   x  excused".
 1072:               "</pre><p>";
 1073:     return $Str;
 1074: }
 1075: 
 1076: #######################################################
 1077: #######################################################
 1078: 
 1079: =pod 
 1080: 
 1081: =back
 1082: 
 1083: =cut
 1084: 
 1085: #######################################################
 1086: #######################################################
 1087: 
 1088: 1;
 1089: 
 1090: __END__

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