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

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

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