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

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

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