File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.39: download - view: text, annotated - select for diffs
Fri Mar 7 17:01:45 2003 UTC (21 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added summary table to html output.  Added new output mode to let instructors
get just the summary table, without all those pesky students.
Rewrote &StudentAverageTotal to implement this.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentassessment.pm,v 1.39 2003/03/07 17:01:45 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 for each student.',
  309:        mode => 'html',
  310:        show => 'totals',
  311:        show_links => 'no',
  312:        },
  313:      { name  => 'HTML, summary table only',
  314:        value => 'html summary table only',
  315:        description => 'Output HTML, but only the final summary table for '.
  316:            'all students across all sequences.',
  317:        mode => 'html',
  318:        show => 'final table',
  319:        show_links => 'no',
  320:        },
  321:      { name  => 'Excel, scores only',
  322:        value => 'excel, scores only',
  323:        description => 'Output an Excel file (compatable with Excel 95), '.
  324: 	   'with a single column for each sequence showing the students '.
  325: 	   'score.',
  326:        mode => 'excel',
  327:        show => 'scores',
  328:        show_links => 'no',
  329:            },
  330:      { name  => 'Excel, totals',
  331:        value => 'excel, totals',
  332:        description => 'Output an Excel file (compatable with Excel 95), '.
  333: 	   'with two columns for each sequence, the students score on the '.
  334: 	   'sequence and the students maximum possible on the sequence',
  335:        mode => 'excel',
  336:        show => 'totals',
  337:        show_links => 'no',
  338:            },
  339:      { name  => 'multi-sheet Excel',
  340:        value => 'multi-sheet excel',
  341:        description => 'Output an Excel file (compatable with Excel 95), '.
  342: 	   'with a seperate worksheet for each sequence you have selected '.
  343:            'the data for each problem part '.
  344:            '(number of tries, status, points awarded) will be listed.',
  345:        mode => 'multi-sheet excel',
  346:        show => 'totals',
  347:        show_links => 'no',
  348:            },
  349:      { name  => 'multi-sheet Excel, by section',
  350:        value => 'multi-sheet excel, by section',
  351:        description => 'Output an Excel file (compatable with Excel 95), '.
  352: 	   'with a seperate worksheet for each sequence you have selected '.
  353:            'the data for each problem part '.
  354:            '(number of tries, status, points awarded) will be listed.  '.
  355:            'There will be one Excel workbook for each section selected.',
  356:        mode => 'multi-sheet excel',
  357:        show => 'by section',
  358:        show_links => 'no',
  359:            },
  360:      { name  => 'CSV, everything',
  361:        value => 'csv, everything',
  362:        description => '',
  363:        mode => 'csv',
  364:        show => 'all',
  365:        show_links => 'no',
  366:            },
  367:      { name  => 'CSV, scores only',
  368:        value => 'csv, scores only',
  369:        description => '',
  370:        mode => 'csv',
  371:        show => 'scores',
  372:        show_links => 'no',
  373:            },
  374:      { name  => 'CSV, totals',
  375:        value => 'csv, totals',
  376:        description => '',
  377:        mode => 'csv',
  378:        show => 'totals',
  379:        show_links => 'no',
  380:            },
  381:      );
  382: 
  383: sub OutputDescriptions {
  384:     my $Str = '';
  385:     $Str .= "<h2>Output Modes</h2>\n";
  386:     $Str .= "<dl>\n";
  387:     foreach my $outputmode (@OutputOptions) {
  388: 	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
  389: 	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
  390:     }
  391:     $Str .= "</dl>\n";
  392:     return $Str;
  393: }
  394: 
  395: sub CreateAndParseOutputSelector {
  396:     my $Str = '';
  397:     my $elementname = 'outputmode';
  398:     #
  399:     # Format for output options is 'mode, restrictions';
  400:     my $selected = 'html, with links';
  401:     if (exists($ENV{'form.'.$elementname})) {
  402:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
  403:             $selected = $ENV{'form.'.$elementname}->[0];
  404:         } else {
  405:             $selected = $ENV{'form.'.$elementname};
  406:         }
  407:     }
  408:     #
  409:     # Set package variables describing output mode
  410:     $show_links  = 'no';
  411:     $output_mode = 'html';
  412:     $show        = 'all';
  413:     foreach my $option (@OutputOptions) {
  414:         next if ($option->{'value'} ne $selected);
  415:         $output_mode = $option->{'mode'};
  416:         $show        = $option->{'show'};
  417:         $show_links  = $option->{'show_links'};
  418:     }
  419: 
  420:     #
  421:     # Build the form element
  422:     $Str = qq/<select size="5" name="$elementname">/;
  423:     foreach my $option (@OutputOptions) {
  424:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
  425:         $Str .= " selected " if ($option->{'value'} eq $selected);
  426:         $Str .= ">".$option->{'name'}."<\/option>";
  427:     }
  428:     $Str .= "\n</select>";
  429:     return $Str;
  430: }
  431: 
  432: #######################################################
  433: #######################################################
  434: 
  435: =pod
  436: 
  437: =head2 HTML output routines
  438: 
  439: =item &html_initialize($r)
  440: 
  441: Create labels for the columns of student data to show.
  442: 
  443: =item &html_outputstudent($r,$student)
  444: 
  445: Return a line of the chart for a student.
  446: 
  447: =item &html_finish($r)
  448: 
  449: =cut
  450: 
  451: #######################################################
  452: #######################################################
  453: {
  454:     my $padding;
  455:     my $count;
  456: 
  457:     my $nodata_count; # The number of students for which there is no data
  458:     my %prog_state;   # progress state used by loncommon PrgWin routines
  459: 
  460: sub html_initialize {
  461:     my ($r) = @_;
  462:     #
  463:     $padding = ' 'x3;
  464:     $count = 0;
  465:     $nodata_count = 0;
  466:     #
  467:     $r->print("<h3>".$ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  468:               "&nbsp;&nbsp;".localtime(time)."</h3>");
  469: 
  470:     #
  471:     # Set up progress window for 'final table' display only
  472:     if ($show eq 'final table') {
  473:         my $studentcount = scalar(@Apache::lonstatistics::Students); 
  474:         %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  475:             ($r,'Summary Table Status',
  476:              'Summary Table Compilation Progress', $studentcount);
  477:     }
  478:     my $Str = "<pre>\n";
  479:     # First, the @StudentData fields need to be listed
  480:     my @to_show = &get_student_fields_to_show();
  481:     foreach my $field (@to_show) {
  482:         my $title=$Apache::lonstatistics::StudentData{$field}->{'title'};
  483:         my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'};
  484:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
  485:         $Str .= $title.' 'x($width-$base).$padding;
  486:     }
  487:     # Now the selected sequences need to be listed
  488:     foreach my $sequence (&get_sequences_to_show) {
  489:         my $title = $sequence->{'title'};
  490:         my $base  = $sequence->{'base_width'};
  491:         my $width = $sequence->{'width'};
  492:         $Str .= $title.' 'x($width-$base).$padding;
  493:     }
  494:     $Str .= "total (of shown problems)</pre>\n";
  495:     $Str .= "<pre>";
  496:     #
  497:     # Check for suppression of output
  498:     if ($show eq 'final table') {
  499:         $Str = '';
  500:     }
  501:     $r->print($Str);
  502:     $r->rflush();
  503:     return;
  504: }
  505: 
  506: sub html_outputstudent {
  507:     my ($r,$student) = @_;
  508:     my $Str = '';
  509:     #
  510:     if($count++ % 5 == 0 && $count > 0) {
  511:         $r->print("</pre><pre>");
  512:     }
  513:     # First, the @StudentData fields need to be listed
  514:     my @to_show = &get_student_fields_to_show();
  515:     foreach my $field (@to_show) {
  516:         my $title=$student->{$field};
  517:         my $base = length($title);
  518:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
  519:         $Str .= $title.' 'x($width-$base).$padding;
  520:     }
  521:     # Get ALL the students data
  522:     my %StudentsData;
  523:     my @tmp = &Apache::loncoursedata::get_current_state
  524:         ($student->{'username'},$student->{'domain'},undef,
  525:          $ENV{'request.course.id'});
  526:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
  527:         %StudentsData = @tmp;
  528:     }
  529:     if (scalar(@tmp) < 1) {
  530:         $nodata_count++;
  531:         return if ($show eq 'final table');
  532:         $Str .= '<font color="blue">No Course Data</font>'."\n";
  533:         $r->print($Str);
  534:         $r->rflush();
  535:         return;
  536:     }
  537:     #
  538:     # By sequence build up the data
  539:     my $studentstats;
  540:     my $PerformanceStr = '';
  541:     foreach my $seq (&get_sequences_to_show) {
  542:         my ($performance,$score,$seq_max) =
  543:             &StudentPerformanceOnSequence($student,\%StudentsData,
  544:                                           $seq,$show_links);
  545:         my $ratio = $score.'/'.$seq_max;
  546:         #
  547:         if ($show eq 'totals') {
  548:             $performance = ' 'x(length($seq_max)-length($score)).$ratio;
  549:             $performance .= ' 'x($seq->{'width'}-length($performance));
  550:         } elsif ($show eq 'scores') {
  551:             $performance = $score;
  552:             $performance .= ' 'x($seq->{'width'}-length($performance));
  553:         } else {
  554:             # Pad with extra spaces
  555:             $performance .= ' 'x($seq->{'width'}-$seq_max-
  556:                                  length($ratio)
  557:                                  ).$ratio;
  558:         }
  559:         #
  560:         $Str .= $performance.$padding;
  561:         #
  562:         $studentstats->{$seq->{'symb'}}->{'score'}= $score;
  563:         $studentstats->{$seq->{'symb'}}->{'max'}  = $seq_max;
  564:     }
  565:     #
  566:     # Total it up and store the statistics info.
  567:     my ($score,$max) = (0,0);
  568:     while (my ($symb,$seq_stats) = each (%{$studentstats})) {
  569:         $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
  570:         $Statistics->{$symb}->{'max'}   += $seq_stats->{'max'};
  571:         $score += $seq_stats->{'score'};
  572:         $max   += $seq_stats->{'max'};
  573:     }
  574:     $Str .= ' '.' 'x(length($max)-length($score)).$score.'/'.$max;
  575:     $Str .= " \n";
  576:     #
  577:     # Check for suppressed output and update the progress window if so...
  578:     if ($show eq 'final table') {
  579:         $Str = '';
  580:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  581:                                                  'last student');
  582:     }
  583:     #
  584:     $r->print($Str);
  585:     #
  586:     $r->rflush();
  587:     return;
  588: }    
  589: 
  590: sub html_finish {
  591:     my ($r) = @_;
  592:     #
  593:     # Check for suppressed output and close the progress window if so
  594:     if ($show eq 'final table') {
  595:         &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  596:     } else {
  597:         $r->print("</pre>\n"); 
  598:     }
  599:     $r->print(&StudentAverageTotal());
  600:     $r->rflush();
  601:     return;
  602: }
  603: 
  604: sub StudentAverageTotal {
  605:     my $Str = "<h3>Summary Tables</h3>\n";
  606:     my $num_students = scalar(@Apache::lonstatistics::Students);
  607:     my $total_ave = 0;
  608:     my $total_max = 0;
  609:     $Str .= '<table border=2 cellspacing="1">'."\n";
  610:     $Str .= "<tr><th>Title</th><th>Average</th><th>Maximum</th></tr>\n";
  611:     foreach my $seq (&get_sequences_to_show) {
  612:         my $ave = int(100*($Statistics->{$seq->{'symb'}}->{'score'}/
  613:             ($num_students-$nodata_count)))/100;
  614:         $total_ave += $ave;
  615:         my $max = $seq->{'num_assess_parts'};
  616:         $total_max += $max;
  617:         if ($ave == 0) {
  618:             $ave = "0.00";
  619:         }
  620:         $ave .= '&nbsp;';
  621:         $max .= '&nbsp;&nbsp;&nbsp;';
  622:         $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
  623:             '<td align="right">'.$ave.'</td>'.
  624:                 '<td align="right">'.$max.'</td></tr>'."\n";
  625:     }
  626:     $total_ave = int(100*$total_ave)/100; # only two digit
  627:     $Str .= "</table>\n";
  628:     $Str .= '<table border=2 cellspacing="1">'."\n";
  629:     $Str .= '<tr><th>Number of Students</th><th>Average</th>'.
  630:         "<th>Maximum</th></tr>\n";
  631:     $Str .= '<tr><td>'.($num_students-$nodata_count).'</td>'.
  632:         '<td>'.$total_ave.'</td><td>'.$total_max.'</td>';
  633:     $Str .= "</table>\n";
  634:     return $Str;
  635: }
  636: 
  637: }
  638: 
  639: #######################################################
  640: #######################################################
  641: 
  642: =pod
  643: 
  644: =head2 Multi-Sheet EXCEL subroutines
  645: 
  646: =item &multi_sheet_excel_initialize($r)
  647: 
  648: =item &multi_sheet_excel_outputstudent($r,$student)
  649: 
  650: =item &multi_sheet_excel_finish($r)
  651: 
  652: =cut
  653: 
  654: #######################################################
  655: #######################################################
  656: {
  657: 
  658: sub multi_sheet_excel_initialize {
  659:     my ($r)=@_;
  660:     $r->print("<h1>Not yet implemented</h1>");
  661:     # 
  662:     # Estimate the size of the file.  We would like to have < 5 megs of data.
  663:     my $max_size = 5000000;
  664:     my $num_students  = scalar(@Apache::lonstatistics::Students);
  665:     my $num_sequences = 0;
  666:     my $num_data_per_part  = 2; # 'status' and 'numtries'
  667:     my $fields_per_student = scalar(&get_student_fields_to_show());
  668:     my $bytes_per_field    = 20; # Back of the envelope calculation
  669:     foreach my $seq (&get_sequences_to_show) {
  670:         $num_sequences++ if ($seq->{'num_assess'} > 0);
  671:         $fields_per_student += $num_data_per_part * $seq->{'num_assess_parts'};
  672:     }
  673:     my $size_estimate = $fields_per_student*$num_students*$bytes_per_field;
  674:     #
  675:     # Compute number of workbooks
  676:     my $num_workbooks = 1;
  677:     if ($size_estimate > $max_size) { # try to stay under 5 megs
  678:         $num_workbooks += int($size_estimate / $max_size);
  679:     }
  680:     if ($show eq 'by section') {
  681:         if (@Apache::lonstatistics::SelectedSections > 1 && 
  682:             $Apache::lonstatistics::SelectedSections[0] ne 'all') {
  683:             $num_workbooks = scalar(@Apache::lonstatistics::SelectedSections);
  684:         } else {
  685:             # @Apache::lonstatistics::Sections contains 'all' as well.
  686:             $num_workbooks = scalar(@Apache::lonstatistics::Sections) - 1;
  687:         }
  688:     }
  689:     
  690:     $r->print("Maximum allowed size: ".$max_size." bytes<br />");
  691:     $r->print("Number of students: ".$num_students."<br />");
  692:     $r->print("Number of fields per student: ".$fields_per_student."<br />");
  693:     $r->print("Total number of fields: ".($fields_per_student*$num_students).
  694:               "<br />");
  695:     $r->print("Bytes per field: ".$bytes_per_field." (estimated)"."<br />");
  696:     $r->print("Estimated size: ".$size_estimate." bytes<br />");
  697:     $r->print("Number of workbooks: ".$num_workbooks."<br />");
  698:     $r->rflush();
  699:     return;
  700: }
  701: 
  702: sub multi_sheet_excel_outputstudent {
  703:     my ($r,$student) = @_;
  704: }
  705: 
  706: sub multi_sheet_excel_finish {
  707:     my ($r) = @_;
  708: }
  709: 
  710: }
  711: #######################################################
  712: #######################################################
  713: 
  714: =pod
  715: 
  716: =head2 EXCEL subroutines
  717: 
  718: =item &excel_initialize($r)
  719: 
  720: =item &excel_outputstudent($r,$student)
  721: 
  722: =item &excel_finish($r)
  723: 
  724: =cut
  725: 
  726: #######################################################
  727: #######################################################
  728: {
  729: 
  730: my $excel_sheet;
  731: my $excel_workbook;
  732: 
  733: my $filename;
  734: my $rows_output;
  735: my $cols_output;
  736: 
  737: my %prog_state; # progress window state
  738: 
  739: sub excel_initialize {
  740:     my ($r) = @_;
  741:     #
  742:     $filename = '/prtspool/'.
  743:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  744:             time.'_'.rand(1000000000).'.xls';
  745:     #
  746:     $excel_workbook = undef;
  747:     $excel_sheet = undef;
  748:     #
  749:     $rows_output = 0;
  750:     $cols_output = 0;
  751:     #
  752:     # Create sheet
  753:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  754:     #
  755:     # Check for errors
  756:     if (! defined($excel_workbook)) {
  757:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  758:         $r->print("Problems creating new Excel file.  ".
  759:                   "This error has been logged.  ".
  760:                   "Please alert your LON-CAPA administrator");
  761:         return ;
  762:     }
  763:     #
  764:     # The excel spreadsheet stores temporary data in files, then put them
  765:     # together.  If needed we should be able to disable this (memory only).
  766:     # The temporary directory must be specified before calling 'addworksheet'.
  767:     # File::Temp is used to determine the temporary directory.
  768:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  769:     #
  770:     # Add a worksheet
  771:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  772:     if (length($sheetname) > 31) {
  773:         $sheetname = substr($sheetname,0,31);
  774:     }
  775:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
  776:     #
  777:     # Put the course description in the header
  778:     $excel_sheet->write($rows_output,$cols_output++,
  779:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
  780:     $cols_output += 3;
  781:     #
  782:     # Put a description of the sections listed
  783:     my $sectionstring = '';
  784:     my @Sections = @Apache::lonstatistics::SelectedSections;
  785:     if (scalar(@Sections) > 1) {
  786:         if (scalar(@Sections) > 2) {
  787:             my $last = pop(@Sections);
  788:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
  789:         } else {
  790:             $sectionstring = "Sections ".join(' and ',@Sections);
  791:         }
  792:     } else {
  793:         if ($Sections[0] eq 'all') {
  794:             $sectionstring = "All sections";
  795:         } else {
  796:             $sectionstring = "Section ".$Sections[0];
  797:         }
  798:     }
  799:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
  800:     $cols_output += scalar(@Sections);
  801:     #
  802:     # Put the date in there too
  803:     $excel_sheet->write($rows_output,$cols_output++,
  804:                         'Compiled on '.localtime(time));
  805:     #
  806:     $rows_output++;
  807:     #
  808:     # Add the student headers
  809:     $cols_output = 0;
  810:     foreach my $field (&get_student_fields_to_show()) {
  811:         $excel_sheet->write($rows_output,$cols_output++,$field);
  812:     }
  813:     #
  814:     # Add the Sequence Headers
  815:     foreach my $seq (&get_sequences_to_show) {
  816:         $excel_sheet->write($rows_output,$cols_output,$seq->{'title'});
  817:         if ($show eq 'totals') {
  818:             $excel_sheet->write($rows_output+1,$cols_output,'score');
  819:             $excel_sheet->write($rows_output+1,$cols_output+1,'maximum');
  820:             $cols_output += 2;
  821:         } else {
  822:             $cols_output++;
  823:         }
  824:     }
  825:     #
  826:     # Bookkeeping
  827:     if ($show eq 'totals') {
  828:         $rows_output += 2;
  829:     } else {
  830:         $rows_output += 1;
  831:     }
  832:     #
  833:     # Let the user know what we are doing
  834:     my $studentcount = scalar(@Apache::lonstatistics::Students); 
  835:     $r->print("<h1>Compiling Excel spreadsheet for ".
  836:               $studentcount.' student');
  837:     $r->print('s') if ($studentcount > 1);
  838:     $r->print("</h1>\n");
  839:     $r->rflush();
  840:     #
  841:     # Initialize progress window
  842:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  843:         ($r,'Excel File Compilation Status',
  844:          'Excel File Compilation Progress', $studentcount);
  845:     #
  846:     return;
  847: }
  848: 
  849: sub excel_outputstudent {
  850:     my ($r,$student) = @_;
  851:     return if (! defined($excel_sheet));
  852:     $cols_output=0;
  853:     #
  854:     # Write out student data
  855:     my @to_show = &get_student_fields_to_show();
  856:     foreach my $field (@to_show) {
  857:         $excel_sheet->write($rows_output,$cols_output++,$student->{$field});
  858:     }
  859:     #
  860:     # Get student assessment data
  861:     my %StudentsData;
  862:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
  863:                                                         $student->{'domain'},
  864:                                                         undef,
  865:                                                    $ENV{'request.course.id'});
  866:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
  867:         %StudentsData = @tmp;
  868:     }
  869:     #
  870:     # Write out sequence scores and totals data
  871:     foreach my $seq (&get_sequences_to_show) {
  872:         my ($performance,$score,$seq_max) =
  873:             &StudentPerformanceOnSequence($student,\%StudentsData,
  874:                                           $seq,'no');
  875:         if ($show eq 'totals' || $show eq 'scores') {
  876:             $excel_sheet->write($rows_output,$cols_output++,$score);
  877:         }
  878:         if ($show eq 'totals') {
  879:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
  880:         }
  881:     }
  882:     #
  883:     # Bookkeeping
  884:     $rows_output++; 
  885:     $cols_output=0;
  886:     #
  887:     # Update the progress window
  888:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
  889:     return;
  890: }
  891: 
  892: sub excel_finish {
  893:     my ($r) = @_;
  894:     return if (! defined($excel_sheet));
  895:     #
  896:     # Write the excel file
  897:     $excel_workbook->close();
  898:     my $c = $r->connection();
  899:     #
  900:     return if($c->aborted());
  901:     #
  902:     # Close the progress window
  903:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  904:     #
  905:     # Tell the user where to get their excel file
  906:     $r->print('<br />'.
  907:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
  908:     $r->rflush();
  909:     return;
  910: }
  911: 
  912: }
  913: #######################################################
  914: #######################################################
  915: 
  916: =pod
  917: 
  918: =head2 CSV output routines
  919: 
  920: =item &csv_initialize($r)
  921: 
  922: =item &csv_outputstudent($r,$student)
  923: 
  924: =item &csv_finish($r)
  925: 
  926: =cut
  927: 
  928: #######################################################
  929: #######################################################
  930: {
  931: 
  932: my $outputfile;
  933: my $filename;
  934: 
  935: my %prog_state; # progress window state
  936: 
  937: sub csv_initialize{
  938:     my ($r) = @_;
  939:     # 
  940:     # Clean up
  941:     $filename = undef;
  942:     $outputfile = undef;
  943:     undef(%prog_state);
  944:     #
  945:     # Open a file
  946:     $filename = '/prtspool/'.
  947:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  948:             time.'_'.rand(1000000000).'.csv';
  949:     unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
  950:         $r->log_error("Couldn't open $filename for output $!");
  951:         $r->print("Problems occured in writing the csv file.  ".
  952:                   "This error has been logged.  ".
  953:                   "Please alert your LON-CAPA administrator.");
  954:         $outputfile = undef;
  955:     }
  956:     #
  957:     # Datestamp
  958:     my $description = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  959:     print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
  960:         '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
  961:             "\n";
  962: 
  963:     #
  964:     # Print out the headings
  965:     my $Str = '';
  966:     my $Str2 = undef;
  967:     foreach my $field (&get_student_fields_to_show()) {
  968:         if ($show eq 'scores') {
  969:             $Str .= '"'.&Apache::loncommon::csv_translate($field).'",';
  970:         } elsif ($show eq 'totals') {
  971:             $Str .= '"",'; # first row empty on the student fields
  972:             $Str2 .= '"'.&Apache::loncommon::csv_translate($field).'",';
  973:         } elsif ($show eq 'all') {
  974:             $Str .= '"'.&Apache::loncommon::csv_translate($field).'",';
  975:         }
  976:     }
  977:     foreach my $seq (&get_sequences_to_show) {
  978:         if ($show eq 'scores') {
  979:             $Str .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
  980:                 '",';
  981:         } elsif ($show eq 'totals') {
  982:             $Str  .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
  983:                 '","",';
  984:             $Str2 .= '"score","total possible",';
  985:         } elsif ($show eq 'all') {
  986:             $Str  .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
  987:                 '",';
  988:             $Str .= '"",'x($seq->{'num_assess_parts'}-1);
  989:             $Str .= '"score","total possible",';
  990:         }
  991:     }
  992:     chop($Str);
  993:     $Str .= "\n";
  994:     print $outputfile $Str;
  995:     if (defined($Str2)) {
  996:         chop($Str2);
  997:         $Str2 .= "\n";
  998:         print $outputfile $Str2;
  999:     }
 1000:     #
 1001:     # Initialize progress window
 1002:     my $studentcount = scalar(@Apache::lonstatistics::Students);
 1003:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
 1004:         ($r,'CSV File Compilation Status',
 1005:          'CSV File Compilation Progress', $studentcount);
 1006:     return;
 1007: }
 1008: 
 1009: sub csv_outputstudent {
 1010:     my ($r,$student) = @_;
 1011:     return if (! defined($outputfile));
 1012:     my $Str = '';
 1013:     #
 1014:     # Output student fields
 1015:     my @to_show = &get_student_fields_to_show();
 1016:     foreach my $field (@to_show) {
 1017:         $Str .= '"'.&Apache::loncommon::csv_translate($student->{$field}).'",';
 1018:     }
 1019:     #
 1020:     # Get student assessment data
 1021:     my %StudentsData;
 1022:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
 1023:                                                         $student->{'domain'},
 1024:                                                         undef,
 1025:                                                    $ENV{'request.course.id'});
 1026:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
 1027:         %StudentsData = @tmp;
 1028:     }
 1029:     #
 1030:     # Output performance data
 1031:     foreach my $seq (&get_sequences_to_show) {
 1032:         my ($performance,$score,$seq_max) =
 1033:             &StudentPerformanceOnSequence($student,\%StudentsData,
 1034:                                           $seq,'no');
 1035:         if ($show eq 'scores') {
 1036:             $Str .= '"'.$score.'",';
 1037:         } elsif ($show eq 'totals') {
 1038:             $Str  .= '"'.$score.'","'.$seq_max.'",';
 1039:         } elsif ($show eq 'all') {
 1040:             $Str .= '"'.join('","',(split(//,$performance),$score,$seq_max)).
 1041:                 '",';
 1042:         }
 1043:     }
 1044:     chop($Str);
 1045:     $Str .= "\n";
 1046:     print $outputfile $Str;
 1047:     #
 1048:     # Update the progress window
 1049:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
 1050:     return;
 1051: }
 1052: 
 1053: sub csv_finish {
 1054:     my ($r) = @_;
 1055:     return if (! defined($outputfile));
 1056:     close($outputfile);
 1057:     #
 1058:     my $c = $r->connection();
 1059:     return if ($c->aborted());
 1060:     #
 1061:     # Close the progress window
 1062:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1063:     #
 1064:     # Tell the user where to get their csv file
 1065:     $r->print('<br />'.
 1066:               '<a href="'.$filename.'">Your csv file.</a>'."\n");
 1067:     $r->rflush();
 1068:     return;
 1069:     
 1070: }
 1071: 
 1072: }
 1073: 
 1074: #######################################################
 1075: #######################################################
 1076: 
 1077: =pod
 1078: 
 1079: =item &StudentPerformanceOnSequence()
 1080: 
 1081: Inputs:
 1082: 
 1083: =over 4
 1084: 
 1085: =item $student
 1086: 
 1087: =item $studentdata Hash ref to all student data
 1088: 
 1089: =item $seq Hash ref, the sequence we are working on
 1090: 
 1091: =item $links if defined we will output links to each resource.
 1092: 
 1093: =back
 1094: 
 1095: =cut
 1096: 
 1097: #######################################################
 1098: #######################################################
 1099: sub StudentPerformanceOnSequence {
 1100:     my ($student,$studentdata,$seq,$links) = @_;
 1101:     $links = 'no' if (! defined($links));
 1102:     my $Str = '';
 1103:     my ($sum,$max) = (0,0);
 1104:     foreach my $resource (@{$seq->{'contents'}}) {
 1105:         next if ($resource->{'type'} ne 'assessment');
 1106:         my $resource_data = $studentdata->{$resource->{'symb'}};
 1107:         my $value = '';
 1108:         foreach my $partnum (@{$resource->{'parts'}}) {
 1109:             $max++;
 1110:             my $symbol = ' '; # default to space
 1111:             #
 1112:             if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
 1113:                 my $status = $resource_data->{'resource.'.$partnum.'.solved'};
 1114:                 if ($status eq 'correct_by_override') {
 1115:                     $symbol = '+';
 1116:                     $sum++;
 1117:                 } elsif ($status eq 'incorrect_by_override') {
 1118:                     $symbol = '-';
 1119:                 } elsif ($status eq 'ungraded_attempted') {
 1120:                     $symbol = '#';
 1121:                 } elsif ($status eq 'incorrect_attempted')  {
 1122:                     $symbol = '.';
 1123:                 } elsif ($status eq 'excused') {
 1124:                     $symbol = 'x';
 1125:                     $max--;
 1126:                 } elsif ($status eq 'correct_by_student' &&
 1127:                     exists($resource_data->{'resource.'.$partnum.'.tries'})){
 1128:                     my $num = $resource_data->{'resource.'.$partnum.'.tries'};
 1129:                     if ($num > 9) {
 1130:                         $symbol = '*';
 1131:                     } elsif ($num > 0) {
 1132:                         $symbol = $num;
 1133:                     } else {
 1134:                         $symbol = ' ';
 1135:                     }
 1136:                     $sum++;
 1137:                 } else {
 1138:                     $symbol = ' ';
 1139:                 }
 1140:             } else {
 1141:                 # Unsolved.  Did they try?
 1142:                 if (exists($resource_data->{'resource.'.$partnum.'.tries'})){
 1143:                     $symbol = '.';
 1144:                 } else {
 1145:                     $symbol = ' ';
 1146:                 }
 1147:             }
 1148:             #
 1149:             if ($links eq 'yes' && $symbol ne ' ') {
 1150:                 $symbol = '<a href="/adm/grades'.
 1151:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
 1152:                         '&student='.$student->{'username'}.
 1153:                             '&domain='.$student->{'domain'}.
 1154:                                 '&command=submission">'.$symbol.'</a>';
 1155:             }
 1156:             $value .= $symbol;
 1157:         }
 1158:         $Str .= $value;
 1159:     }
 1160:     return ($Str,$sum,$max);
 1161: }
 1162: 
 1163: #######################################################
 1164: #######################################################
 1165: 
 1166: =pod
 1167: 
 1168: =item &CreateLegend()
 1169: 
 1170: This function returns a formatted string containing the legend for the
 1171: chart.  The legend describes the symbols used to represent grades for
 1172: problems.
 1173: 
 1174: =cut
 1175: 
 1176: #######################################################
 1177: #######################################################
 1178: sub CreateLegend {
 1179:     my $Str = "<p><pre>".
 1180:               "   1  correct by student in 1 try\n".
 1181:               "   7  correct by student in 7 tries\n".
 1182:               "   *  correct by student in more than 9 tries\n".
 1183: 	      "   +  correct by hand grading or override\n".
 1184:               "   -  incorrect by override\n".
 1185: 	      "   .  incorrect attempted\n".
 1186: 	      "   #  ungraded attempted\n".
 1187:               "      not attempted (blank field)\n".
 1188: 	      "   x  excused".
 1189:               "</pre><p>";
 1190:     return $Str;
 1191: }
 1192: 
 1193: #######################################################
 1194: #######################################################
 1195: 
 1196: =pod 
 1197: 
 1198: =back
 1199: 
 1200: =cut
 1201: 
 1202: #######################################################
 1203: #######################################################
 1204: 
 1205: 1;
 1206: 
 1207: __END__

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