Annotation of loncom/interface/statistics/lonstudentassessment.pm, revision 1.66

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.66    ! matthew     3: # $Id: lonstudentassessment.pm,v 1.65 2003/09/03 17:45:49 matthew Exp $
1.1       stredwic    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
1.28      matthew    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: #######################################################
1.1       stredwic   49: 
1.21      minaeibi   50: package Apache::lonstudentassessment;
1.1       stredwic   51: 
                     52: use strict;
1.28      matthew    53: use Apache::lonstatistics;
1.1       stredwic   54: use Apache::lonhtmlcommon;
                     55: use Apache::loncoursedata;
1.28      matthew    56: use Apache::lonnet; # for logging porpoises
1.31      matthew    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: #######################################################
1.1       stredwic   74: 
1.30      matthew    75: my $Statistics;
                     76: 
1.28      matthew    77: #######################################################
                     78: #######################################################
                     79: 
                     80: =pod
                     81: 
1.31      matthew    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: 
1.32      matthew    86: =item $show 'all', 'totals', or 'scores' determines how much data is output
1.31      matthew    87: 
1.54      matthew    88: =item $data  determines what performance data is shown
                     89: 
                     90: =item $datadescription A short description of the output data selected.
                     91: 
                     92: =item $base 'tries' or 'scores' determines the base of the performance shown
                     93: 
1.49      matthew    94: =item $single_student_mode evaluates to true if we are showing only one
                     95: student.
                     96: 
1.31      matthew    97: =cut
                     98: 
                     99: #######################################################
                    100: #######################################################
                    101: my $show_links;
                    102: my $output_mode;
1.54      matthew   103: my $data;
                    104: my $base;
                    105: my $datadescription;
1.49      matthew   106: my $single_student_mode;
1.28      matthew   107: 
1.31      matthew   108: #######################################################
                    109: #######################################################
                    110: # End of package variable declarations
1.28      matthew   111: 
1.31      matthew   112: =pod
1.28      matthew   113: 
1.31      matthew   114: =back
1.28      matthew   115: 
1.31      matthew   116: =cut
1.28      matthew   117: 
1.31      matthew   118: #######################################################
                    119: #######################################################
1.28      matthew   120: 
1.31      matthew   121: =pod
1.28      matthew   122: 
1.31      matthew   123: =item &BuildStudentAssessmentPage()
1.28      matthew   124: 
1.31      matthew   125: Inputs: 
1.4       stredwic  126: 
1.31      matthew   127: =over 4
1.28      matthew   128: 
                    129: =item $r Apache Request
                    130: 
                    131: =item $c Apache Connection 
                    132: 
                    133: =back
                    134: 
                    135: =cut
                    136: 
                    137: #######################################################
                    138: #######################################################
1.1       stredwic  139: sub BuildStudentAssessmentPage {
1.30      matthew   140:     my ($r,$c)=@_;
1.65      matthew   141: 
1.30      matthew   142:     undef($Statistics);
1.66    ! matthew   143:     undef($show_links);
        !           144:     undef($output_mode);
        !           145:     undef($data);
        !           146:     undef($base);
        !           147:     undef($datadescription);
        !           148:     undef($single_student_mode);
1.65      matthew   149: 
                    150:     $single_student_mode = 0;
1.49      matthew   151:     $single_student_mode = 1 if ($ENV{'form.SelectedStudent'});
1.59      matthew   152:     if ($ENV{'form.selectstudent'}) {
                    153:         &Apache::lonstatistics::DisplayClasslist($r);
                    154:         return;
                    155:     }
1.30      matthew   156:     #
1.31      matthew   157:     # Print out the HTML headers for the interface
                    158:     #    This also parses the output mode selector
1.54      matthew   159:     #    This step must *always* be done.
1.30      matthew   160:     $r->print(&CreateInterface());
1.31      matthew   161:     $r->print('<input type="hidden" name="notfirstrun" value="true" />');
1.49      matthew   162:     $r->print('<input type="hidden" name="sort" value="'.
                    163:               $ENV{'form.sort'}.'" />');
1.7       stredwic  164:     $r->rflush();
1.58      matthew   165:     #
1.49      matthew   166:     if (! exists($ENV{'form.notfirstrun'}) && ! $single_student_mode) {
1.31      matthew   167:         return;
                    168:     }
                    169:     #
                    170:     my $initialize     = \&html_initialize;
                    171:     my $output_student = \&html_outputstudent;
                    172:     my $finish         = \&html_finish;
                    173:     #
                    174:     if ($output_mode eq 'excel') {
                    175:         $initialize     = \&excel_initialize;
                    176:         $output_student = \&excel_outputstudent;
                    177:         $finish         = \&excel_finish;
                    178:     } elsif ($output_mode eq 'csv') {
                    179:         $initialize     = \&csv_initialize;
                    180:         $output_student = \&csv_outputstudent;
                    181:         $finish         = \&csv_finish;
                    182:     }
1.30      matthew   183:     #
                    184:     if($c->aborted()) {  return ; }
1.31      matthew   185:     #
1.49      matthew   186:     # Determine which students we want to look at
                    187:     my @Students;
                    188:     if ($single_student_mode) {
                    189:         @Students = (&Apache::lonstatistics::current_student());
                    190:         $r->print(&next_and_previous_buttons());
                    191:         $r->rflush();
                    192:     } else {
                    193:         @Students = @Apache::lonstatistics::Students;
                    194:     }
1.56      matthew   195:     #
                    196:     # Perform generic initialization tasks
                    197:     #       Since we use lonnet::EXT to retrieve problem weights,
                    198:     #       to ensure current data we must clear the caches out.
                    199:     #       This makes sure that parameter changes at the student level
                    200:     #       are immediately reflected in the chart.
                    201:     &Apache::lonnet::clear_EXT_cache_status();
1.49      matthew   202:     #
1.31      matthew   203:     # Call the initialize routine selected above
                    204:     $initialize->($r);
1.49      matthew   205:     foreach my $student (@Students) {
1.31      matthew   206:         if($c->aborted()) { 
                    207:             $finish->($r);
                    208:             return ; 
1.1       stredwic  209:         }
1.31      matthew   210:         # Call the output_student routine selected above
                    211:         $output_student->($r,$student);
                    212:     }
                    213:     # Call the "finish" routine selected above
                    214:     $finish->($r);
                    215:     #
                    216:     return;
                    217: }
                    218: 
                    219: #######################################################
                    220: #######################################################
1.49      matthew   221: sub next_and_previous_buttons {
                    222:     my $Str = '';
                    223:     $Str .= '<input type="hidden" name="SelectedStudent" value="'.
                    224:         $ENV{'form.SelectedStudent'}.'" />';
                    225:     #
                    226:     # Build the previous student link
                    227:     my $previous = &Apache::lonstatistics::previous_student();
                    228:     my $previousbutton = '';
                    229:     if (defined($previous)) {
                    230:         my $sname = $previous->{'username'}.':'.$previous->{'domain'};
                    231:         $previousbutton .= '<input type="button" value="'.
                    232:             'Previous Student ('.
                    233:             $previous->{'username'}.'@'.$previous->{'domain'}.')'.
                    234:             '" onclick="document.Statistics.SelectedStudent.value='.
                    235:             "'".$sname."'".';'.
                    236:             'document.Statistics.submit();" />';
                    237:     } else {
                    238:         $previousbutton .= '<input type="button" value="'.
                    239:             'Previous student (none)'.'" />';
                    240:     }
                    241:     #
                    242:     # Build the next student link
                    243:     my $next = &Apache::lonstatistics::next_student();
                    244:     my $nextbutton = '';
                    245:     if (defined($next)) {
                    246:         my $sname = $next->{'username'}.':'.$next->{'domain'};
                    247:         $nextbutton .= '<input type="button" value="'.
                    248:             'Next Student ('.
                    249:             $next->{'username'}.'@'.$next->{'domain'}.')'.
                    250:             '" onclick="document.Statistics.SelectedStudent.value='.
                    251:             "'".$sname."'".';'.
                    252:             'document.Statistics.submit();" />';
                    253:     } else {
                    254:         $nextbutton .= '<input type="button" value="'.
                    255:             'Next student (none)'.'" />';
                    256:     }
                    257:     #
                    258:     # Build the 'all students' button
                    259:     my $all = '';
                    260:     $all .= '<input type="button" value="All Students" '.
                    261:             '" onclick="document.Statistics.SelectedStudent.value='.
                    262:             "''".';'.'document.Statistics.submit();" />';
                    263:     $Str .= $previousbutton.('&nbsp;'x5).$all.('&nbsp;'x5).$nextbutton;
                    264:     return $Str;
                    265: }
                    266: 
                    267: #######################################################
                    268: #######################################################
1.30      matthew   269: 
1.31      matthew   270: sub get_student_fields_to_show {
                    271:     my @to_show = @Apache::lonstatistics::SelectedStudentData;
                    272:     foreach (@to_show) {
                    273:         if ($_ eq 'all') {
                    274:             @to_show = @Apache::lonstatistics::StudentDataOrder;
                    275:             last;
                    276:         }
                    277:     }
                    278:     return @to_show;
                    279: }
                    280: 
1.28      matthew   281: #######################################################
                    282: #######################################################
                    283: 
                    284: =pod
1.2       stredwic  285: 
1.28      matthew   286: =item &CreateInterface()
1.21      minaeibi  287: 
1.28      matthew   288: Called by &BuildStudentAssessmentPage to create the top part of the
                    289: page which displays the chart.
                    290: 
1.30      matthew   291: Inputs: None
1.28      matthew   292: 
                    293: Returns:  A string containing the HTML for the headers and top table for 
                    294: the chart page.
                    295: 
                    296: =cut
                    297: 
                    298: #######################################################
                    299: #######################################################
1.2       stredwic  300: sub CreateInterface {
1.4       stredwic  301:     my $Str = '';
1.30      matthew   302: #    $Str .= &CreateLegend();
                    303:     $Str .= '<table cellspacing="5">'."\n";
                    304:     $Str .= '<tr>';
                    305:     $Str .= '<td align="center"><b>Sections</b></td>';
                    306:     $Str .= '<td align="center"><b>Student Data</b></td>';
1.46      matthew   307:     $Str .= '<td align="center"><b>Enrollment Status</b></td>';
1.41      matthew   308:     $Str .= '<td align="center"><b>Sequences and Folders</b></td>';
1.58      matthew   309:     $Str .= '<td align="center"><b>Output Format</b>'.
                    310:         &Apache::loncommon::help_open_topic("Chart_Output_Formats").
                    311:         '</td>';
                    312:     $Str .= '<td align="center"><b>Output Data</b>'.
                    313:         &Apache::loncommon::help_open_topic("Chart_Output_Data").
                    314:         '</td>';
1.30      matthew   315:     $Str .= '</tr>'."\n";
                    316:     #
1.4       stredwic  317:     $Str .= '<tr><td align="center">'."\n";
1.29      matthew   318:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1.4       stredwic  319:     $Str .= '</td><td align="center">';
1.30      matthew   320:     my $only_seq_with_assessments = sub { 
                    321:         my $s=shift;
                    322:         if ($s->{'num_assess'} < 1) { 
                    323:             return 0;
                    324:         } else { 
                    325:             return 1;
                    326:         }
                    327:     };
                    328:     $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',
                    329:                                                       5,undef);
1.46      matthew   330:     $Str .= '</td><td>'."\n";
                    331:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1.4       stredwic  332:     $Str .= '</td><td>'."\n";
1.30      matthew   333:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
                    334:                                               $only_seq_with_assessments);
1.31      matthew   335:     $Str .= '</td><td>'."\n";
                    336:     $Str .= &CreateAndParseOutputSelector();
1.54      matthew   337:     $Str .= '</td><td>'."\n";
                    338:     $Str .= &CreateAndParseOutputDataSelector();
1.30      matthew   339:     $Str .= '</td></tr>'."\n";
                    340:     $Str .= '</table>'."\n";
1.55      matthew   341:     $Str .= '<input type="submit" value="Generate Chart" />';
1.59      matthew   342:     $Str .= '&nbsp;'x5;
                    343:     $Str .= '<input type="submit" name="selectstudent" '.
                    344:                                   'value="Select One Student" />';
                    345:     $Str .= '&nbsp;'x5;
                    346:     $Str .= '<input type="submit" name="ClearCache" value="Clear Caches" />';
1.61      www       347:     $Str .= '&nbsp;'x5;
                    348:     $Str .= '<br />';
1.4       stredwic  349:     return $Str;
1.1       stredwic  350: }
1.30      matthew   351: 
                    352: #######################################################
                    353: #######################################################
                    354: 
                    355: =pod
                    356: 
1.31      matthew   357: =item &CreateAndParseOutputSelector()
1.30      matthew   358: 
                    359: =cut
                    360: 
                    361: #######################################################
                    362: #######################################################
1.32      matthew   363: my @OutputOptions = 
                    364:     ({ name  => 'HTML, with links',
                    365:        value => 'html, with links',
1.33      matthew   366:        description => 'Output HTML with each symbol linked to the problem '.
1.35      matthew   367: 	   'which generated it.',
                    368:        mode => 'html',
                    369:        show_links => 'yes',
                    370:        },
1.47      matthew   371:      { name  => 'HTML, with all links',
                    372:        value => 'html, with all links',
                    373:        description => 'Output HTML with each symbol linked to the problem '.
                    374: 	   'which generated it.  '.
                    375:            'This includes links for unattempted problems.',
                    376:        mode => 'html',
                    377:        show_links => 'all',
                    378:        },
1.32      matthew   379:      { name  => 'HTML, without links',
                    380:        value => 'html, without links',
1.33      matthew   381:        description => 'Output HTML.  By not including links, the size of the'.
                    382: 	   ' web page is greatly reduced.  If your browser crashes on the '.
1.35      matthew   383: 	   'full display, try this.',
                    384:        mode => 'html',
                    385:        show_links => 'no',
                    386:            },
1.54      matthew   387:      { name  => 'Excel',
                    388:        value => 'excel',
                    389:        description => 'Output an Excel file (compatable with Excel 95).',
1.35      matthew   390:        mode => 'excel',
                    391:        show_links => 'no',
1.54      matthew   392:    },
                    393:      { name  => 'CSV',
                    394:        value => 'csv',
                    395:        description => 'Output a comma seperated values file suitable for '.
1.57      matthew   396:            'import into a spreadsheet program.  Using this method as opposed '.
                    397:            'to Excel output allows you to organize your data before importing'.
                    398:            ' it into a spreadsheet program.',
1.35      matthew   399:        mode => 'csv',
                    400:        show_links => 'no',
                    401:            },
1.32      matthew   402:      );
                    403: 
1.33      matthew   404: sub OutputDescriptions {
                    405:     my $Str = '';
1.58      matthew   406:     $Str .= "<h2>Output Formats</h2>\n";
1.33      matthew   407:     $Str .= "<dl>\n";
                    408:     foreach my $outputmode (@OutputOptions) {
                    409: 	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
                    410: 	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
                    411:     }
                    412:     $Str .= "</dl>\n";
                    413:     return $Str;
                    414: }
                    415: 
1.31      matthew   416: sub CreateAndParseOutputSelector {
                    417:     my $Str = '';
1.44      matthew   418:     my $elementname = 'chartoutputmode';
1.31      matthew   419:     #
                    420:     # Format for output options is 'mode, restrictions';
1.50      matthew   421:     my $selected = 'html, without links';
1.31      matthew   422:     if (exists($ENV{'form.'.$elementname})) {
                    423:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
                    424:             $selected = $ENV{'form.'.$elementname}->[0];
                    425:         } else {
                    426:             $selected = $ENV{'form.'.$elementname};
                    427:         }
                    428:     }
                    429:     #
                    430:     # Set package variables describing output mode
                    431:     $show_links  = 'no';
                    432:     $output_mode = 'html';
1.35      matthew   433:     foreach my $option (@OutputOptions) {
                    434:         next if ($option->{'value'} ne $selected);
                    435:         $output_mode = $option->{'mode'};
                    436:         $show_links  = $option->{'show_links'};
1.31      matthew   437:     }
1.35      matthew   438: 
1.31      matthew   439:     #
                    440:     # Build the form element
                    441:     $Str = qq/<select size="5" name="$elementname">/;
1.32      matthew   442:     foreach my $option (@OutputOptions) {
                    443:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
                    444:         $Str .= " selected " if ($option->{'value'} eq $selected);
                    445:         $Str .= ">".$option->{'name'}."<\/option>";
1.31      matthew   446:     }
                    447:     $Str .= "\n</select>";
                    448:     return $Str;
                    449: }
1.30      matthew   450: 
1.54      matthew   451: ##
                    452: ## Data selector stuff
                    453: ##
                    454: my @OutputDataOptions =
1.57      matthew   455:     (
                    456:      { name  => 'Scores',
                    457:        base  => 'scores',
                    458:        value => 'scores',
                    459:        shortdesc => 'Score on each Problem Part',
                    460:        longdesc =>'The students score on each problem part, computed as'.
                    461:            'the part weight * part awarded',
                    462:        },
                    463:      { name  => 'Scores Sum',
                    464:        base  => 'scores',
                    465:        value => 'sum only',
                    466:        shortdesc => 'Sum of Scores on each Problem Part',
                    467:        longdesc =>'The total of the scores of the student on each problem'.
                    468:            ' part in the sequences or folders selected.',
                    469:        },
                    470:      { name  => 'Scores Sum & Maximums',
                    471:        base  => 'scores',
                    472:        value => 'sum and total',
                    473:        shortdesc => 'Total Score and Maximum Possible for each '.
                    474:            'Sequence or Folder',
                    475:        longdesc => 'The score of each student as well as the '.
                    476:            ' maximum possible on each Sequence or Folder.',
                    477:        },
                    478:      { name  => 'Scores Summary Table Only',
                    479:        base  => 'scores',
                    480:        value => 'final table scores',
                    481:        shortdesc => 'Summary of Scores',
                    482:        longdesc  => 'The average score on each sequence or folder for the '.
                    483:            'selected students.',
                    484:        },
                    485:      { name  =>'Tries',
                    486:        base  =>'tries',
                    487:        value => 'tries',
                    488:        shortdesc => 'Number of Tries before success on each Problem Part',
                    489:        longdesc =>'The number of tries before success on each problem part.',
                    490:        },
                    491:      { name  =>'Parts Correct',
                    492:        base  =>'tries',
                    493:        value => 'parts correct',
                    494:        shortdesc => 'Number of Problem Parts completed successfully.',
                    495:        longdesc => 'The Number of Problem Parts completed successfully'.
                    496:            ' on each sequence or folder.',
                    497:        },
                    498:      { name  =>'Parts Correct & Maximums',
                    499:        base  =>'tries',
                    500:        value => 'parts correct total',
                    501:        shortdesc => 'Number of Problem Parts completed successfully.',
                    502:        longdesc => 'The Number of Problem Parts completed successfully and '.
                    503:            'the maximum possible for each student',
                    504:        },
                    505:      { name  => 'Parts Summary Table Only',
                    506:        base  => 'tries',
                    507:        value => 'final table parts',
                    508:        shortdesc => 'Summary of Parts Correct',
                    509:        longdesc  => 'A summary table of the average number of problem parts '.
                    510:            'students were able to get correct on each sequence.',
                    511:        },
                    512:      );
                    513: 
                    514: sub HTMLifyOutputDataDescriptions {
                    515:     my $Str = '';
1.58      matthew   516:     $Str .= "<h2>Output Data</h2>\n";
1.57      matthew   517:     $Str .= "<dl>\n";
                    518:     foreach my $option (@OutputDataOptions) {
                    519:         $Str .= '    <dt>'.$option->{'name'}.'</dt>';
                    520:         $Str .= '<dd>'.$option->{'longdesc'}.'</dd>'."\n";
                    521:     }
                    522:     $Str .= "</dl>\n";
                    523:     return $Str;
                    524: }
1.54      matthew   525: 
                    526: sub CreateAndParseOutputDataSelector {
                    527:     my $Str = '';
                    528:     my $elementname = 'chartoutputdata';
                    529:     #
                    530:     my $selected = 'scores';
                    531:     if (exists($ENV{'form.'.$elementname})) {
                    532:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
                    533:             $selected = $ENV{'form.'.$elementname}->[0];
                    534:         } else {
                    535:             $selected = $ENV{'form.'.$elementname};
                    536:         }
                    537:     }
                    538:     #
                    539:     $data = 'scores';
                    540:     foreach my $option (@OutputDataOptions) {
                    541:         if ($option->{'value'} eq $selected) {
                    542:             $data = $option->{'value'};
                    543:             $base = $option->{'base'};
                    544:             $datadescription = $option->{'shortdesc'};
                    545:         }
                    546:     }
                    547:     #
                    548:     # Build the form element
                    549:     $Str = qq/<select size="5" name="$elementname">/;
                    550:     foreach my $option (@OutputDataOptions) {
                    551:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
                    552:         $Str .= " selected " if ($option->{'value'} eq $data);
                    553:         $Str .= ">".$option->{'name'}."<\/option>";
                    554:     }
                    555:     $Str .= "\n</select>";
                    556:     return $Str;
                    557: 
                    558: }
                    559: 
1.28      matthew   560: #######################################################
                    561: #######################################################
1.1       stredwic  562: 
1.28      matthew   563: =pod
                    564: 
1.31      matthew   565: =head2 HTML output routines
1.28      matthew   566: 
1.31      matthew   567: =item &html_initialize($r)
1.28      matthew   568: 
1.31      matthew   569: Create labels for the columns of student data to show.
1.28      matthew   570: 
1.31      matthew   571: =item &html_outputstudent($r,$student)
1.28      matthew   572: 
1.31      matthew   573: Return a line of the chart for a student.
1.28      matthew   574: 
1.31      matthew   575: =item &html_finish($r)
1.28      matthew   576: 
                    577: =cut
                    578: 
                    579: #######################################################
                    580: #######################################################
1.31      matthew   581: {
                    582:     my $padding;
                    583:     my $count;
                    584: 
1.39      matthew   585:     my $nodata_count; # The number of students for which there is no data
                    586:     my %prog_state;   # progress state used by loncommon PrgWin routines
                    587: 
1.31      matthew   588: sub html_initialize {
                    589:     my ($r) = @_;
1.30      matthew   590:     #
                    591:     $padding = ' 'x3;
1.35      matthew   592:     $count = 0;
1.39      matthew   593:     $nodata_count = 0;
1.66    ! matthew   594:     undef(%prog_state);
1.30      matthew   595:     #
1.38      matthew   596:     $r->print("<h3>".$ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
                    597:               "&nbsp;&nbsp;".localtime(time)."</h3>");
1.39      matthew   598: 
1.55      matthew   599:     if ($data !~ /^final table/) {
                    600:         $r->print("<h3>".$datadescription."</h3>");        
                    601:     }
1.39      matthew   602:     #
                    603:     # Set up progress window for 'final table' display only
1.54      matthew   604:     if ($data =~ /^final table/) {
1.39      matthew   605:         my $studentcount = scalar(@Apache::lonstatistics::Students); 
                    606:         %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                    607:             ($r,'Summary Table Status',
                    608:              'Summary Table Compilation Progress', $studentcount);
                    609:     }
1.31      matthew   610:     my $Str = "<pre>\n";
1.30      matthew   611:     # First, the @StudentData fields need to be listed
1.31      matthew   612:     my @to_show = &get_student_fields_to_show();
1.30      matthew   613:     foreach my $field (@to_show) {
                    614:         my $title=$Apache::lonstatistics::StudentData{$field}->{'title'};
                    615:         my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'};
                    616:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
                    617:         $Str .= $title.' 'x($width-$base).$padding;
                    618:     }
                    619:     # Now the selected sequences need to be listed
1.40      matthew   620:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()){
1.31      matthew   621:         my $title = $sequence->{'title'};
                    622:         my $base  = $sequence->{'base_width'};
                    623:         my $width = $sequence->{'width'};
                    624:         $Str .= $title.' 'x($width-$base).$padding;
1.30      matthew   625:     }
1.54      matthew   626:     $Str .= "total</pre>\n";
1.31      matthew   627:     $Str .= "<pre>";
1.39      matthew   628:     #
                    629:     # Check for suppression of output
1.54      matthew   630:     if ($data =~ /^final table/) {
1.39      matthew   631:         $Str = '';
                    632:     }
1.31      matthew   633:     $r->print($Str);
                    634:     $r->rflush();
                    635:     return;
1.30      matthew   636: }
                    637: 
1.31      matthew   638: sub html_outputstudent {
                    639:     my ($r,$student) = @_;
1.2       stredwic  640:     my $Str = '';
1.35      matthew   641:     #
1.55      matthew   642:     if($count++ % 5 == 0 && $count > 0 && $data !~ /^final table/) {
1.35      matthew   643:         $r->print("</pre><pre>");
                    644:     }
1.30      matthew   645:     # First, the @StudentData fields need to be listed
1.31      matthew   646:     my @to_show = &get_student_fields_to_show();
1.30      matthew   647:     foreach my $field (@to_show) {
                    648:         my $title=$student->{$field};
1.31      matthew   649:         my $base = length($title);
1.30      matthew   650:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
                    651:         $Str .= $title.' 'x($width-$base).$padding;
                    652:     }
                    653:     # Get ALL the students data
                    654:     my %StudentsData;
                    655:     my @tmp = &Apache::loncoursedata::get_current_state
                    656:         ($student->{'username'},$student->{'domain'},undef,
                    657:          $ENV{'request.course.id'});
                    658:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
                    659:         %StudentsData = @tmp;
                    660:     }
                    661:     if (scalar(@tmp) < 1) {
1.39      matthew   662:         $nodata_count++;
1.54      matthew   663:         return if ($data =~ /^final table/);
1.30      matthew   664:         $Str .= '<font color="blue">No Course Data</font>'."\n";
1.31      matthew   665:         $r->print($Str);
                    666:         $r->rflush();
                    667:         return;
1.30      matthew   668:     }
                    669:     #
                    670:     # By sequence build up the data
                    671:     my $studentstats;
1.31      matthew   672:     my $PerformanceStr = '';
1.40      matthew   673:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.54      matthew   674:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
                    675:         if ($base eq 'tries') {
                    676:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                    677:                 &StudentTriesOnSequence($student,\%StudentsData,
                    678:                                         $seq,$show_links);
                    679:         } else {
                    680:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                    681:                 &StudentPerformanceOnSequence($student,\%StudentsData,
                    682:                                               $seq,$show_links);
                    683:         }
                    684:         my $ratio = sprintf("%3d",$score).'/'.sprintf("%3d",$seq_max);
1.31      matthew   685:         #
1.54      matthew   686:         if ($data eq 'sum and total' || $data eq 'parts correct total') {
                    687:             $performance  = $ratio;
                    688:             $performance .= ' 'x($seq->{'width'}-length($ratio));
                    689:         } elsif ($data eq 'sum only' || $data eq 'parts correct') {
                    690:             $performance  = $score;
                    691:             $performance .= ' 'x($seq->{'width'}-length($score));
1.31      matthew   692:         } else {
                    693:             # Pad with extra spaces
1.51      matthew   694:             $performance .= ' 'x($seq->{'width'}-$performance_length-
1.31      matthew   695:                                  length($ratio)
                    696:                                  ).$ratio;
1.30      matthew   697:         }
1.31      matthew   698:         #
                    699:         $Str .= $performance.$padding;
                    700:         #
                    701:         $studentstats->{$seq->{'symb'}}->{'score'}= $score;
                    702:         $studentstats->{$seq->{'symb'}}->{'max'}  = $seq_max;
1.30      matthew   703:     }
                    704:     #
                    705:     # Total it up and store the statistics info.
                    706:     my ($score,$max) = (0,0);
                    707:     while (my ($symb,$seq_stats) = each (%{$studentstats})) {
                    708:         $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
1.54      matthew   709:         if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) {
                    710:             $Statistics->{$symb}->{'max'} = $seq_stats->{'max'};
                    711:         }
1.30      matthew   712:         $score += $seq_stats->{'score'};
                    713:         $max   += $seq_stats->{'max'};
                    714:     }
1.31      matthew   715:     $Str .= ' '.' 'x(length($max)-length($score)).$score.'/'.$max;
1.30      matthew   716:     $Str .= " \n";
1.39      matthew   717:     #
                    718:     # Check for suppressed output and update the progress window if so...
1.54      matthew   719:     if ($data =~ /^final table/) {
1.39      matthew   720:         $Str = '';
                    721:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                    722:                                                  'last student');
                    723:     }
                    724:     #
1.31      matthew   725:     $r->print($Str);
                    726:     #
                    727:     $r->rflush();
                    728:     return;
1.30      matthew   729: }    
1.2       stredwic  730: 
1.31      matthew   731: sub html_finish {
                    732:     my ($r) = @_;
1.39      matthew   733:     #
                    734:     # Check for suppressed output and close the progress window if so
1.54      matthew   735:     if ($data =~ /^final table/) {
1.39      matthew   736:         &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    737:     } else {
                    738:         $r->print("</pre>\n"); 
                    739:     }
1.49      matthew   740:     if ($single_student_mode) {
                    741:         $r->print(&SingleStudentTotal());
                    742:     } else {
                    743:         $r->print(&StudentAverageTotal());
                    744:     }
1.31      matthew   745:     $r->rflush();
                    746:     return;
                    747: }
                    748: 
1.39      matthew   749: sub StudentAverageTotal {
                    750:     my $Str = "<h3>Summary Tables</h3>\n";
                    751:     my $num_students = scalar(@Apache::lonstatistics::Students);
                    752:     my $total_ave = 0;
                    753:     my $total_max = 0;
                    754:     $Str .= '<table border=2 cellspacing="1">'."\n";
                    755:     $Str .= "<tr><th>Title</th><th>Average</th><th>Maximum</th></tr>\n";
1.40      matthew   756:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.42      matthew   757:         my $ave;
                    758:         if ($num_students > $nodata_count) {
                    759:             $ave = int(100*($Statistics->{$seq->{'symb'}}->{'score'}/
                    760:                             ($num_students-$nodata_count)))/100;
                    761:         } else {
                    762:             $ave = 0;
                    763:         }
1.39      matthew   764:         $total_ave += $ave;
1.54      matthew   765:         my $max = $Statistics->{$seq->{'symb'}}->{'max'};
1.39      matthew   766:         $total_max += $max;
                    767:         if ($ave == 0) {
                    768:             $ave = "0.00";
                    769:         }
                    770:         $ave .= '&nbsp;';
                    771:         $max .= '&nbsp;&nbsp;&nbsp;';
                    772:         $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
                    773:             '<td align="right">'.$ave.'</td>'.
                    774:                 '<td align="right">'.$max.'</td></tr>'."\n";
                    775:     }
                    776:     $total_ave = int(100*$total_ave)/100; # only two digit
                    777:     $Str .= "</table>\n";
                    778:     $Str .= '<table border=2 cellspacing="1">'."\n";
                    779:     $Str .= '<tr><th>Number of Students</th><th>Average</th>'.
                    780:         "<th>Maximum</th></tr>\n";
                    781:     $Str .= '<tr><td>'.($num_students-$nodata_count).'</td>'.
                    782:         '<td>'.$total_ave.'</td><td>'.$total_max.'</td>';
                    783:     $Str .= "</table>\n";
                    784:     return $Str;
                    785: }
                    786: 
1.49      matthew   787: sub SingleStudentTotal {
                    788:     my $student = &Apache::lonstatistics::current_student();
1.52      matthew   789:     my $Str = "<h3>Summary table for ".$student->{'fullname'}." ".
                    790:         $student->{'username'}.'@'.$student->{'domain'}."</h3>\n";
1.49      matthew   791:     $Str .= '<table border=2 cellspacing="1">'."\n";
                    792:     $Str .= 
                    793:         "<tr><th>Sequence or Folder</th><th>Score</th><th>Maximum</th></tr>\n";
                    794:     my $total = 0;
                    795:     my $total_max = 0;
                    796:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
                    797:         my $value = $Statistics->{$seq->{'symb'}}->{'score'};
                    798:         my $max = $Statistics->{$seq->{'symb'}}->{'max'};
                    799:         $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
                    800:             '<td align="right">'.$value.'</td>'.
                    801:                 '<td align="right">'.$max.'</td></tr>'."\n";
                    802:         $total += $value;
                    803:         $total_max +=$max;
                    804:     }
                    805:     $Str .= '<tr><td><b>Total</b></td>'.
                    806:         '<td align="right">'.$total.'</td>'.
                    807:         '<td align="right">'.$total_max."</td></tr>\n";
                    808:     $Str .= "</table>\n";
                    809:     return $Str;
                    810: }
                    811: 
1.31      matthew   812: }
                    813: 
                    814: #######################################################
                    815: #######################################################
                    816: 
                    817: =pod
                    818: 
                    819: =head2 EXCEL subroutines
                    820: 
                    821: =item &excel_initialize($r)
                    822: 
                    823: =item &excel_outputstudent($r,$student)
                    824: 
                    825: =item &excel_finish($r)
                    826: 
                    827: =cut
                    828: 
                    829: #######################################################
                    830: #######################################################
                    831: {
                    832: 
                    833: my $excel_sheet;
1.32      matthew   834: my $excel_workbook;
                    835: 
                    836: my $filename;
                    837: my $rows_output;
                    838: my $cols_output;
                    839: 
1.36      matthew   840: my %prog_state; # progress window state
1.54      matthew   841: my $request_aborted;
1.31      matthew   842: 
                    843: sub excel_initialize {
                    844:     my ($r) = @_;
                    845:     #
1.66    ! matthew   846:     undef ($excel_sheet);
        !           847:     undef ($excel_workbook);
        !           848:     undef ($filename);
        !           849:     undef ($rows_output);
        !           850:     undef ($cols_output);
        !           851:     undef (%prog_state);
        !           852:     undef ($request_aborted);
        !           853:     #
1.54      matthew   854:     my $total_columns = scalar(&get_student_fields_to_show());
                    855:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
                    856:         # Add 2 because we need a 'sum' and 'total' column for each
                    857:         $total_columns += $seq->{'num_assess_parts'}+2;
                    858:     }
                    859:     if ($data eq 'tries' && $total_columns > 255) {
                    860:         $r->print(<<END);
                    861: <h2>Unable to Complete Request</h2>
                    862: <p>
                    863: LON-CAPA is unable to produce your Excel spreadsheet because your selections
                    864: will result in more than 255 columns.  Excel allows only 255 columns in a
                    865: spreadsheet.
                    866: </p><p>
                    867: You may consider reducing the number of <b>Sequences or Folders</b> you
                    868: have selected.  
                    869: </p><p>
                    870: LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the
                    871: summary data (<b>Parts Correct</b> or <b>Parts Correct & Totals</b>).
                    872: </p>
                    873: END
                    874:        $request_aborted = 1;
                    875:     }
                    876:     if ($data eq 'scores' && $total_columns > 255) {
                    877:         $r->print(<<END);
                    878: <h2>Unable to Complete Request</h2>
                    879: <p>
                    880: LON-CAPA is unable to produce your Excel spreadsheet because your selections
                    881: will result in more than 255 columns.  Excel allows only 255 columns in a
                    882: spreadsheet.
                    883: </p><p>
                    884: You may consider reducing the number of <b>Sequences or Folders</b> you
                    885: have selected.  
                    886: </p><p>
                    887: LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the
                    888: summary data (<b>Scores Sum</b> or <b>Scores Sum & Totals</b>).
                    889: </p>
                    890: END
                    891:        $request_aborted = 1;
                    892:     }
                    893:     if ($data =~ /^final table/) {
                    894:         $r->print(<<END);
                    895: <h2>Unable to Complete Request</h2>
                    896: <p>
                    897: The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
                    898: </p>
                    899: END
                    900:        $request_aborted = 1;
                    901:     }
                    902:     return if ($request_aborted);
                    903:     #
1.32      matthew   904:     $filename = '/prtspool/'.
1.31      matthew   905:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                    906:             time.'_'.rand(1000000000).'.xls';
1.32      matthew   907:     #
                    908:     $excel_workbook = undef;
                    909:     $excel_sheet = undef;
                    910:     #
                    911:     $rows_output = 0;
                    912:     $cols_output = 0;
                    913:     #
                    914:     # Create sheet
                    915:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                    916:     #
                    917:     # Check for errors
                    918:     if (! defined($excel_workbook)) {
1.31      matthew   919:         $r->log_error("Error creating excel spreadsheet $filename: $!");
                    920:         $r->print("Problems creating new Excel file.  ".
                    921:                   "This error has been logged.  ".
                    922:                   "Please alert your LON-CAPA administrator");
1.32      matthew   923:         return ;
1.31      matthew   924:     }
                    925:     #
                    926:     # The excel spreadsheet stores temporary data in files, then put them
                    927:     # together.  If needed we should be able to disable this (memory only).
                    928:     # The temporary directory must be specified before calling 'addworksheet'.
                    929:     # File::Temp is used to determine the temporary directory.
1.32      matthew   930:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
                    931:     #
                    932:     # Add a worksheet
1.33      matthew   933:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    934:     if (length($sheetname) > 31) {
                    935:         $sheetname = substr($sheetname,0,31);
                    936:     }
                    937:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
1.32      matthew   938:     #
1.34      matthew   939:     # Put the course description in the header
                    940:     $excel_sheet->write($rows_output,$cols_output++,
                    941:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
                    942:     $cols_output += 3;
                    943:     #
                    944:     # Put a description of the sections listed
                    945:     my $sectionstring = '';
                    946:     my @Sections = @Apache::lonstatistics::SelectedSections;
                    947:     if (scalar(@Sections) > 1) {
                    948:         if (scalar(@Sections) > 2) {
                    949:             my $last = pop(@Sections);
                    950:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
                    951:         } else {
                    952:             $sectionstring = "Sections ".join(' and ',@Sections);
                    953:         }
                    954:     } else {
                    955:         if ($Sections[0] eq 'all') {
                    956:             $sectionstring = "All sections";
                    957:         } else {
                    958:             $sectionstring = "Section ".$Sections[0];
                    959:         }
                    960:     }
                    961:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
                    962:     $cols_output += scalar(@Sections);
                    963:     #
                    964:     # Put the date in there too
1.54      matthew   965:     $excel_sheet->write($rows_output++,$cols_output++,
1.34      matthew   966:                         'Compiled on '.localtime(time));
                    967:     #
1.54      matthew   968:     $cols_output = 0;
                    969:     $excel_sheet->write($rows_output++,$cols_output++,$datadescription);
                    970:     #
                    971:     if ($data eq 'tries' || $data eq 'scores') {
                    972:         $rows_output++;
                    973:     }
1.34      matthew   974:     #
1.32      matthew   975:     # Add the student headers
1.34      matthew   976:     $cols_output = 0;
1.32      matthew   977:     foreach my $field (&get_student_fields_to_show()) {
1.34      matthew   978:         $excel_sheet->write($rows_output,$cols_output++,$field);
1.32      matthew   979:     }
1.54      matthew   980:     my $row_offset = 0;
                    981:     if ($data eq 'tries' || $data eq 'scores') {
                    982:         $row_offset = -1;
                    983:     }
1.32      matthew   984:     #
1.54      matthew   985:     # Add the remaining column headers
1.40      matthew   986:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.54      matthew   987:         $excel_sheet->write($rows_output+$row_offset,
                    988:                             $cols_output,$seq->{'title'});
                    989:         if ($data eq 'tries' || $data eq 'scores') {
                    990:             foreach my $res (@{$seq->{'contents'}}) {
                    991:                 next if ($res->{'type'} ne 'assessment');
                    992:                 if (scalar(@{$res->{'parts'}}) > 1) {
                    993:                     foreach my $part (@{$res->{'parts'}}) {
                    994:                         $excel_sheet->write($rows_output,
                    995:                                             $cols_output++,
                    996:                                             $res->{'title'}.' part '.$part);
                    997:                     }
                    998:                 } else {
                    999:                     $excel_sheet->write($rows_output,
                   1000:                                         $cols_output++,
                   1001:                                         $res->{'title'});
                   1002:                 }
                   1003:             }
                   1004:             $excel_sheet->write($rows_output,$cols_output++,'score');
                   1005:             $excel_sheet->write($rows_output,$cols_output++,'maximum');
                   1006:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
1.34      matthew  1007:             $excel_sheet->write($rows_output+1,$cols_output,'score');
                   1008:             $excel_sheet->write($rows_output+1,$cols_output+1,'maximum');
1.32      matthew  1009:             $cols_output += 2;
                   1010:         } else {
                   1011:             $cols_output++;
                   1012:         }
                   1013:     }
                   1014:     #
                   1015:     # Bookkeeping
1.54      matthew  1016:     if ($data eq 'sum and total' || $data eq 'parts correct total') {
1.34      matthew  1017:         $rows_output += 2;
1.32      matthew  1018:     } else {
1.34      matthew  1019:         $rows_output += 1;
1.45      matthew  1020:     }
                   1021:     #
                   1022:     # Output a row for MAX
1.54      matthew  1023:     $cols_output = 0;
                   1024:     foreach my $field (&get_student_fields_to_show()) {
                   1025:         if ($field eq 'username' || $field eq 'fullname' || 
                   1026:             $field eq 'id') {
                   1027:             $excel_sheet->write($rows_output,$cols_output++,'Maximum');
                   1028:         } else {
                   1029:             $excel_sheet->write($rows_output,$cols_output++,'');
                   1030:         }
                   1031:     }
                   1032:     #
                   1033:     # Add the maximums for each sequence or assessment
                   1034:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
                   1035:         my $weight;
                   1036:         my $max = 0;
                   1037:         foreach my $resource (@{$seq->{'contents'}}) {
                   1038:             next if ($resource->{'type'} ne 'assessment');
                   1039:             foreach my $part (@{$resource->{'parts'}}) {
                   1040:                 $weight = 1;
                   1041:                 if ($base eq 'scores') {
                   1042:                     $weight = &Apache::lonnet::EXT
                   1043:                         ('resource.'.$part.'.weight',$resource->{'symb'},
                   1044:                          undef,undef,undef);
                   1045:                     if (!defined($weight) || ($weight eq '')) { 
                   1046:                         $weight=1;
                   1047:                     }
                   1048:                 }
                   1049:                 if ($data eq 'scores') {
                   1050:                     $excel_sheet->write($rows_output,$cols_output++,$weight);
                   1051:                 } elsif ($data eq 'tries') {
                   1052:                     $excel_sheet->write($rows_output,$cols_output++,'');
                   1053:                 }
                   1054:                 $max += $weight;
1.45      matthew  1055:             }
1.54      matthew  1056:         } 
                   1057:         if (! ($data eq 'sum only' || $data eq 'parts correct')) {
                   1058:             $excel_sheet->write($rows_output,$cols_output++,'');
1.45      matthew  1059:         }
1.54      matthew  1060:         $excel_sheet->write($rows_output,$cols_output++,$max);
1.32      matthew  1061:     }
1.54      matthew  1062:     $rows_output++;
1.32      matthew  1063:     #
                   1064:     # Let the user know what we are doing
                   1065:     my $studentcount = scalar(@Apache::lonstatistics::Students); 
                   1066:     $r->print("<h1>Compiling Excel spreadsheet for ".
                   1067:               $studentcount.' student');
                   1068:     $r->print('s') if ($studentcount > 1);
                   1069:     $r->print("</h1>\n");
                   1070:     $r->rflush();
1.31      matthew  1071:     #
1.36      matthew  1072:     # Initialize progress window
                   1073:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                   1074:         ($r,'Excel File Compilation Status',
                   1075:          'Excel File Compilation Progress', $studentcount);
                   1076:     #
1.62      matthew  1077:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                   1078:                                           'Processing first student');
1.31      matthew  1079:     return;
                   1080: }
                   1081: 
                   1082: sub excel_outputstudent {
                   1083:     my ($r,$student) = @_;
1.54      matthew  1084:     return if ($request_aborted);
1.32      matthew  1085:     return if (! defined($excel_sheet));
                   1086:     $cols_output=0;
                   1087:     #
                   1088:     # Write out student data
                   1089:     my @to_show = &get_student_fields_to_show();
                   1090:     foreach my $field (@to_show) {
                   1091:         $excel_sheet->write($rows_output,$cols_output++,$student->{$field});
                   1092:     }
                   1093:     #
                   1094:     # Get student assessment data
                   1095:     my %StudentsData;
                   1096:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
                   1097:                                                         $student->{'domain'},
                   1098:                                                         undef,
                   1099:                                                    $ENV{'request.course.id'});
                   1100:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
                   1101:         %StudentsData = @tmp;
                   1102:     }
                   1103:     #
                   1104:     # Write out sequence scores and totals data
1.40      matthew  1105:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.54      matthew  1106:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
                   1107:         if ($base eq 'tries') {
                   1108:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1109:                 &StudentTriesOnSequence($student,\%StudentsData,
                   1110:                                         $seq,'no');
                   1111:         } else {
                   1112:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1113:                 &StudentPerformanceOnSequence($student,\%StudentsData,
                   1114:                                               $seq,'no');
                   1115:         }
                   1116:         if ($data eq 'tries' || $data eq 'scores') {
                   1117:             foreach my $value (@$rawdata) {
                   1118:                 $excel_sheet->write($rows_output,$cols_output++,$value);
                   1119:             }
                   1120:             $excel_sheet->write($rows_output,$cols_output++,$score);
                   1121:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
                   1122:         } elsif ($data eq 'sum and total' || $data eq 'sum only' || 
                   1123:             $data eq 'parts correct' || $data eq 'parts correct total') {
1.32      matthew  1124:             $excel_sheet->write($rows_output,$cols_output++,$score);
                   1125:         }
1.54      matthew  1126:         if ($data eq 'sum and total' || $data eq 'parts correct total') {
1.32      matthew  1127:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
                   1128:         }
                   1129:     }
                   1130:     #
                   1131:     # Bookkeeping
                   1132:     $rows_output++; 
                   1133:     $cols_output=0;
                   1134:     #
1.36      matthew  1135:     # Update the progress window
                   1136:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1.32      matthew  1137:     return;
1.31      matthew  1138: }
                   1139: 
                   1140: sub excel_finish {
                   1141:     my ($r) = @_;
1.54      matthew  1142:     return if ($request_aborted);
1.32      matthew  1143:     return if (! defined($excel_sheet));
                   1144:     #
                   1145:     # Write the excel file
                   1146:     $excel_workbook->close();
                   1147:     my $c = $r->connection();
                   1148:     #
                   1149:     return if($c->aborted());
                   1150:     #
1.36      matthew  1151:     # Close the progress window
                   1152:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   1153:     #
1.32      matthew  1154:     # Tell the user where to get their excel file
1.36      matthew  1155:     $r->print('<br />'.
1.32      matthew  1156:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
                   1157:     $r->rflush();
                   1158:     return;
1.31      matthew  1159: }
                   1160: 
                   1161: }
1.30      matthew  1162: #######################################################
                   1163: #######################################################
                   1164: 
                   1165: =pod
                   1166: 
1.31      matthew  1167: =head2 CSV output routines
                   1168: 
                   1169: =item &csv_initialize($r)
                   1170: 
                   1171: =item &csv_outputstudent($r,$student)
                   1172: 
                   1173: =item &csv_finish($r)
1.30      matthew  1174: 
                   1175: =cut
                   1176: 
                   1177: #######################################################
                   1178: #######################################################
1.31      matthew  1179: {
                   1180: 
1.37      matthew  1181: my $outputfile;
                   1182: my $filename;
1.54      matthew  1183: my $request_aborted;
1.37      matthew  1184: my %prog_state; # progress window state
                   1185: 
1.31      matthew  1186: sub csv_initialize{
                   1187:     my ($r) = @_;
1.37      matthew  1188:     # 
                   1189:     # Clean up
1.66    ! matthew  1190:     undef($outputfile);
        !          1191:     undef($filename);
        !          1192:     undef($request_aborted);
1.37      matthew  1193:     undef(%prog_state);
                   1194:     #
1.54      matthew  1195:     # Deal with unimplemented requests
                   1196:     $request_aborted = undef;
                   1197:     if ($data =~ /final table/) {
                   1198:         $r->print(<<END);
                   1199: <h2>Unable to Complete Request</h2>
                   1200: <p>
                   1201: The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
                   1202: </p>
                   1203: END
                   1204:        $request_aborted = 1;
                   1205:     }
                   1206:     return if ($request_aborted);
                   1207: 
                   1208:     #
1.37      matthew  1209:     # Open a file
                   1210:     $filename = '/prtspool/'.
                   1211:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                   1212:             time.'_'.rand(1000000000).'.csv';
                   1213:     unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
                   1214:         $r->log_error("Couldn't open $filename for output $!");
                   1215:         $r->print("Problems occured in writing the csv file.  ".
                   1216:                   "This error has been logged.  ".
                   1217:                   "Please alert your LON-CAPA administrator.");
                   1218:         $outputfile = undef;
                   1219:     }
1.38      matthew  1220:     #
                   1221:     # Datestamp
                   1222:     my $description = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                   1223:     print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
                   1224:         '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
                   1225:             "\n";
1.37      matthew  1226:     #
                   1227:     # Print out the headings
                   1228:     my $Str = '';
                   1229:     my $Str2 = undef;
                   1230:     foreach my $field (&get_student_fields_to_show()) {
1.54      matthew  1231:         if ($data eq 'sum only') {
1.37      matthew  1232:             $Str .= '"'.&Apache::loncommon::csv_translate($field).'",';
1.54      matthew  1233:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
1.37      matthew  1234:             $Str .= '"",'; # first row empty on the student fields
                   1235:             $Str2 .= '"'.&Apache::loncommon::csv_translate($field).'",';
1.54      matthew  1236:         } elsif ($data eq 'scores' || $data eq 'tries' || 
                   1237:                  $data eq 'parts correct') {
1.53      matthew  1238:             $Str  .= '"",';
                   1239:             $Str2 .= '"'.&Apache::loncommon::csv_translate($field).'",';
1.37      matthew  1240:         }
                   1241:     }
1.40      matthew  1242:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.54      matthew  1243:         if ($data eq 'sum only' || $data eq 'parts correct') {
1.37      matthew  1244:             $Str .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
                   1245:                 '",';
1.54      matthew  1246:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
1.37      matthew  1247:             $Str  .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
                   1248:                 '","",';
                   1249:             $Str2 .= '"score","total possible",';
1.54      matthew  1250:         } elsif ($data eq 'scores' || $data eq 'tries') {
1.37      matthew  1251:             $Str  .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
                   1252:                 '",';
1.53      matthew  1253:             $Str .= '"",'x($seq->{'num_assess_parts'}-1+2);
                   1254:             foreach my $res (@{$seq->{'contents'}}) {
1.54      matthew  1255:                 next if ($res->{'type'} ne 'assessment');
1.53      matthew  1256:                 foreach my $part (@{$res->{'parts'}}) {
                   1257:                     $Str2 .= '"'.&Apache::loncommon::csv_translate($res->{'title'}.', Part '.$part).'",';
                   1258:                 }
                   1259:             }
                   1260:             $Str2 .= '"score","total possible",';
1.37      matthew  1261:         }
                   1262:     }
                   1263:     chop($Str);
                   1264:     $Str .= "\n";
                   1265:     print $outputfile $Str;
                   1266:     if (defined($Str2)) {
                   1267:         chop($Str2);
                   1268:         $Str2 .= "\n";
                   1269:         print $outputfile $Str2;
                   1270:     }
                   1271:     #
                   1272:     # Initialize progress window
                   1273:     my $studentcount = scalar(@Apache::lonstatistics::Students);
                   1274:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                   1275:         ($r,'CSV File Compilation Status',
                   1276:          'CSV File Compilation Progress', $studentcount);
1.31      matthew  1277:     return;
                   1278: }
                   1279: 
                   1280: sub csv_outputstudent {
                   1281:     my ($r,$student) = @_;
1.54      matthew  1282:     return if ($request_aborted);
1.37      matthew  1283:     return if (! defined($outputfile));
                   1284:     my $Str = '';
                   1285:     #
                   1286:     # Output student fields
                   1287:     my @to_show = &get_student_fields_to_show();
                   1288:     foreach my $field (@to_show) {
                   1289:         $Str .= '"'.&Apache::loncommon::csv_translate($student->{$field}).'",';
                   1290:     }
                   1291:     #
                   1292:     # Get student assessment data
                   1293:     my %StudentsData;
                   1294:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
                   1295:                                                         $student->{'domain'},
                   1296:                                                         undef,
                   1297:                                                    $ENV{'request.course.id'});
                   1298:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
                   1299:         %StudentsData = @tmp;
                   1300:     }
                   1301:     #
                   1302:     # Output performance data
1.40      matthew  1303:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.54      matthew  1304:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
                   1305:         if ($base eq 'tries') {
                   1306:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1307:                 &StudentTriesOnSequence($student,\%StudentsData,
                   1308:                                         $seq,'no');
                   1309:         } else {
                   1310:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1311:                 &StudentPerformanceOnSequence($student,\%StudentsData,
                   1312:                                               $seq,'no');
                   1313:         }
                   1314:         if ($data eq 'sum only' || $data eq 'parts correct') {
1.37      matthew  1315:             $Str .= '"'.$score.'",';
1.54      matthew  1316:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
1.37      matthew  1317:             $Str  .= '"'.$score.'","'.$seq_max.'",';
1.54      matthew  1318:         } elsif ($data eq 'scores' || $data eq 'tries') {
                   1319:             $Str .= '"'.join('","',(@$rawdata,$score,$seq_max)).'",';
1.37      matthew  1320:         }
                   1321:     }
                   1322:     chop($Str);
                   1323:     $Str .= "\n";
                   1324:     print $outputfile $Str;
                   1325:     #
                   1326:     # Update the progress window
                   1327:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
                   1328:     return;
1.31      matthew  1329: }
                   1330: 
                   1331: sub csv_finish {
                   1332:     my ($r) = @_;
1.54      matthew  1333:     return if ($request_aborted);
1.37      matthew  1334:     return if (! defined($outputfile));
                   1335:     close($outputfile);
                   1336:     #
                   1337:     my $c = $r->connection();
                   1338:     return if ($c->aborted());
                   1339:     #
                   1340:     # Close the progress window
                   1341:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   1342:     #
                   1343:     # Tell the user where to get their csv file
                   1344:     $r->print('<br />'.
                   1345:               '<a href="'.$filename.'">Your csv file.</a>'."\n");
                   1346:     $r->rflush();
                   1347:     return;
                   1348:     
1.31      matthew  1349: }
1.2       stredwic 1350: 
                   1351: }
                   1352: 
1.28      matthew  1353: #######################################################
                   1354: #######################################################
                   1355: 
1.2       stredwic 1356: =pod
                   1357: 
1.54      matthew  1358: =item &StudentTriesOnSequence()
1.2       stredwic 1359: 
1.30      matthew  1360: Inputs:
1.2       stredwic 1361: 
                   1362: =over 4
                   1363: 
1.30      matthew  1364: =item $student
1.28      matthew  1365: 
1.30      matthew  1366: =item $studentdata Hash ref to all student data
1.2       stredwic 1367: 
1.30      matthew  1368: =item $seq Hash ref, the sequence we are working on
1.2       stredwic 1369: 
1.30      matthew  1370: =item $links if defined we will output links to each resource.
1.2       stredwic 1371: 
1.28      matthew  1372: =back
1.2       stredwic 1373: 
                   1374: =cut
1.1       stredwic 1375: 
1.28      matthew  1376: #######################################################
                   1377: #######################################################
1.54      matthew  1378: sub StudentTriesOnSequence {
1.32      matthew  1379:     my ($student,$studentdata,$seq,$links) = @_;
1.31      matthew  1380:     $links = 'no' if (! defined($links));
1.1       stredwic 1381:     my $Str = '';
1.30      matthew  1382:     my ($sum,$max) = (0,0);
1.51      matthew  1383:     my $performance_length = 0;
1.54      matthew  1384:     my @TriesData = ();
                   1385:     my $tries;
1.30      matthew  1386:     foreach my $resource (@{$seq->{'contents'}}) {
                   1387:         next if ($resource->{'type'} ne 'assessment');
                   1388:         my $resource_data = $studentdata->{$resource->{'symb'}};
                   1389:         my $value = '';
                   1390:         foreach my $partnum (@{$resource->{'parts'}}) {
1.54      matthew  1391:             $tries = undef;
1.30      matthew  1392:             $max++;
1.51      matthew  1393:             $performance_length++;
1.30      matthew  1394:             my $symbol = ' '; # default to space
                   1395:             #
                   1396:             if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
                   1397:                 my $status = $resource_data->{'resource.'.$partnum.'.solved'};
                   1398:                 if ($status eq 'correct_by_override') {
                   1399:                     $symbol = '+';
                   1400:                     $sum++;
                   1401:                 } elsif ($status eq 'incorrect_by_override') {
                   1402:                     $symbol = '-';
                   1403:                 } elsif ($status eq 'ungraded_attempted') {
                   1404:                     $symbol = '#';
                   1405:                 } elsif ($status eq 'incorrect_attempted')  {
                   1406:                     $symbol = '.';
                   1407:                 } elsif ($status eq 'excused') {
                   1408:                     $symbol = 'x';
                   1409:                     $max--;
                   1410:                 } elsif ($status eq 'correct_by_student' &&
                   1411:                     exists($resource_data->{'resource.'.$partnum.'.tries'})){
1.54      matthew  1412:                     $tries = $resource_data->{'resource.'.$partnum.'.tries'};
                   1413:                     if ($tries > 9) {
1.30      matthew  1414:                         $symbol = '*';
1.54      matthew  1415:                     } elsif ($tries > 0) {
                   1416:                         $symbol = $tries;
1.30      matthew  1417:                     } else {
                   1418:                         $symbol = ' ';
                   1419:                     }
                   1420:                     $sum++;
1.43      matthew  1421:                 } elsif (exists($resource_data->{'resource.'.
                   1422:                                                      $partnum.'.tries'})){
                   1423:                     $symbol = '.';
1.30      matthew  1424:                 } else {
                   1425:                     $symbol = ' ';
1.2       stredwic 1426:                 }
1.30      matthew  1427:             } else {
                   1428:                 # Unsolved.  Did they try?
                   1429:                 if (exists($resource_data->{'resource.'.$partnum.'.tries'})){
                   1430:                     $symbol = '.';
                   1431:                 } else {
                   1432:                     $symbol = ' ';
1.18      matthew  1433:                 }
1.2       stredwic 1434:             }
1.54      matthew  1435:             #
                   1436:             if (! defined($tries)) {
                   1437:                 $tries = $symbol;
                   1438:             }
                   1439:             push (@TriesData,$tries);
1.30      matthew  1440:             #
1.47      matthew  1441:             if ( ($links eq 'yes' && $symbol ne ' ') ||
                   1442:                  ($links eq 'all')) {
1.49      matthew  1443:                 if (length($symbol) > 1) {
                   1444:                     &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
                   1445:                 }
1.30      matthew  1446:                 $symbol = '<a href="/adm/grades'.
                   1447:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
                   1448:                         '&student='.$student->{'username'}.
1.64      albertel 1449:                             '&userdom='.$student->{'domain'}.
1.30      matthew  1450:                                 '&command=submission">'.$symbol.'</a>';
                   1451:             }
                   1452:             $value .= $symbol;
1.2       stredwic 1453:         }
1.30      matthew  1454:         $Str .= $value;
1.17      minaeibi 1455:     }
1.51      matthew  1456:     if ($seq->{'randompick'}) {
                   1457:         $max = $seq->{'randompick'};
                   1458:     }
1.54      matthew  1459:     return ($Str,$performance_length,$sum,$max,\@TriesData);
                   1460: }
                   1461: 
                   1462: #######################################################
                   1463: #######################################################
                   1464: 
                   1465: =pod
                   1466: 
                   1467: =item &StudentPerformanceOnSequence()
                   1468: 
                   1469: Inputs:
                   1470: 
                   1471: =over 4
                   1472: 
                   1473: =item $student
                   1474: 
                   1475: =item $studentdata Hash ref to all student data
                   1476: 
                   1477: =item $seq Hash ref, the sequence we are working on
                   1478: 
                   1479: =item $links if defined we will output links to each resource.
                   1480: 
                   1481: =back
                   1482: 
                   1483: =cut
                   1484: 
                   1485: #######################################################
                   1486: #######################################################
                   1487: sub StudentPerformanceOnSequence {
                   1488:     my ($student,$studentdata,$seq,$links) = @_;
                   1489:     $links = 'no' if (! defined($links));
                   1490:     my $Str = ''; # final result string
                   1491:     my ($score,$max) = (0,0);
                   1492:     my $performance_length = 0;
                   1493:     my $symbol;
                   1494:     my @ScoreData = ();
                   1495:     my $partscore;
                   1496:     foreach my $resource (@{$seq->{'contents'}}) {
                   1497:         next if ($resource->{'type'} ne 'assessment');
                   1498:         my $resource_data = $studentdata->{$resource->{'symb'}};
                   1499:         foreach my $part (@{$resource->{'parts'}}) {
                   1500:             $partscore = undef;
                   1501:             my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
                   1502:                                               $resource->{'symb'},
                   1503:                                               $student->{'domain'},
                   1504:                                               $student->{'username'},
                   1505:                                               $student->{'section'});
                   1506:             if (!defined($weight) || ($weight eq '')) { 
                   1507:                 $weight=1;
                   1508:             }
                   1509:             #
                   1510:             $max += $weight; # see the 'excused' branch below...
                   1511:             $performance_length++; # one character per part
                   1512:             $symbol = ' '; # default to space
                   1513:             #
                   1514:             my $awarded = 0;
                   1515:             if (exists($resource_data->{'resource.'.$part.'.awarded'})) {
                   1516:                 $awarded = $resource_data->{'resource.'.$part.'.awarded'};
                   1517:             }
                   1518:             #
                   1519:             $partscore = $weight*$awarded;
                   1520:             $score += $partscore;
1.63      matthew  1521:             $symbol = $partscore; 
1.54      matthew  1522:             if (length($symbol) > 1) {
                   1523:                 $symbol = '*';
                   1524:             }
                   1525:             if (exists($resource_data->{'resource.'.$part.'.solved'})) {
                   1526:                 my $status = $resource_data->{'resource.'.$part.'.solved'};
                   1527:                 if ($status eq 'excused') {
                   1528:                     $symbol = 'x';
                   1529:                     $max -= $weight; # Do not count 'excused' problems.
                   1530:                 }
                   1531:             } else {
                   1532:                 # Unsolved.  Did they try?
                   1533:                 if (exists($resource_data->{'resource.'.$part.'.tries'})){
                   1534:                     $symbol = '.';
                   1535:                 } else {
                   1536:                     $symbol = ' ';
                   1537:                 }
                   1538:             }
                   1539:             #
1.60      matthew  1540:             if (! defined($partscore)) {
                   1541:                 $partscore = $symbol;
                   1542:             }
                   1543:             push (@ScoreData,$partscore);
                   1544:             #
1.54      matthew  1545:             if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) {
                   1546:                 $symbol = '<a href="/adm/grades'.
                   1547:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
                   1548:                     '&student='.$student->{'username'}.
1.64      albertel 1549:                     '&userdom='.$student->{'domain'}.
1.54      matthew  1550:                     '&command=submission">'.$symbol.'</a>';
                   1551:             }
1.60      matthew  1552:             $Str .= $symbol;
1.54      matthew  1553:         }
                   1554:     }
                   1555:     return ($Str,$performance_length,$score,$max,\@ScoreData);
1.1       stredwic 1556: }
1.23      minaeibi 1557: 
1.28      matthew  1558: #######################################################
                   1559: #######################################################
1.23      minaeibi 1560: 
1.2       stredwic 1561: =pod
                   1562: 
                   1563: =item &CreateLegend()
                   1564: 
                   1565: This function returns a formatted string containing the legend for the
                   1566: chart.  The legend describes the symbols used to represent grades for
                   1567: problems.
                   1568: 
                   1569: =cut
                   1570: 
1.28      matthew  1571: #######################################################
                   1572: #######################################################
1.2       stredwic 1573: sub CreateLegend {
                   1574:     my $Str = "<p><pre>".
1.13      minaeibi 1575:               "   1  correct by student in 1 try\n".
                   1576:               "   7  correct by student in 7 tries\n".
1.12      minaeibi 1577:               "   *  correct by student in more than 9 tries\n".
1.20      minaeibi 1578: 	      "   +  correct by hand grading or override\n".
1.12      minaeibi 1579:               "   -  incorrect by override\n".
                   1580: 	      "   .  incorrect attempted\n".
                   1581: 	      "   #  ungraded attempted\n".
1.13      minaeibi 1582:               "      not attempted (blank field)\n".
1.12      minaeibi 1583: 	      "   x  excused".
1.17      minaeibi 1584:               "</pre><p>";
1.2       stredwic 1585:     return $Str;
                   1586: }
                   1587: 
1.28      matthew  1588: #######################################################
                   1589: #######################################################
                   1590: 
1.30      matthew  1591: =pod 
1.2       stredwic 1592: 
                   1593: =back
                   1594: 
                   1595: =cut
                   1596: 
1.28      matthew  1597: #######################################################
                   1598: #######################################################
1.2       stredwic 1599: 
1.28      matthew  1600: 1;
1.2       stredwic 1601: 
1.1       stredwic 1602: __END__

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