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

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

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