File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.40: download - view: text, annotated - select for diffs
Fri Mar 7 18:46:38 2003 UTC (21 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
moved &get_sequences_to_show from lonstudentassessment.pm to
&Sequences_with_Assess in lonstatistics.pm.

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

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