File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.57: download - view: text, annotated - select for diffs
Wed Jun 11 15:04:55 2003 UTC (21 years ago) by matthew
Branches: MAIN
CVS tags: HEAD
Mostly cosmetic changes.
    Modified descriptions and order of @OutputDataOptions.
    Added subroutine to output long descriptions of @OutputDataOptions.
    Excised multi-sheet excel code that was disabled anyway.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentassessment.pm,v 1.57 2003/06/11 15:04:55 matthew Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: # LON-CAPA is free software; you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation; either version 2 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # LON-CAPA is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with LON-CAPA; if not, write to the Free Software
   20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21: #
   22: # /home/httpd/html/adm/gpl.txt
   23: #
   24: # http://www.lon-capa.org/
   25: #
   26: # (Navigate problems for statistical reports
   27: #
   28: #######################################################
   29: #######################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: lonstudentassessment
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Presents assessment data about a student or a group of students.
   40: 
   41: =head1 Subroutines
   42: 
   43: =over 4 
   44: 
   45: =cut
   46: 
   47: #######################################################
   48: #######################################################
   49: 
   50: package Apache::lonstudentassessment;
   51: 
   52: use strict;
   53: use Apache::lonstatistics;
   54: use Apache::lonhtmlcommon;
   55: use Apache::loncoursedata;
   56: use Apache::lonnet; # for logging porpoises
   57: use Spreadsheet::WriteExcel;
   58: 
   59: #######################################################
   60: #######################################################
   61: =pod
   62: 
   63: =item Package Variables
   64: 
   65: =over 4
   66: 
   67: =item $Statistics Hash ref to store student data.  Indexed by symb,
   68:       contains hashes with keys 'score' and 'max'.
   69: 
   70: =cut
   71: 
   72: #######################################################
   73: #######################################################
   74: 
   75: my $Statistics;
   76: 
   77: #######################################################
   78: #######################################################
   79: 
   80: =pod
   81: 
   82: =item $show_links 'yes' or 'no' for linking to student performance data
   83: 
   84: =item $output_mode 'html', 'excel', or 'csv' for output mode
   85: 
   86: =item $show 'all', 'totals', or 'scores' determines how much data is output
   87: 
   88: =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: 
   94: =item $single_student_mode evaluates to true if we are showing only one
   95: student.
   96: 
   97: =cut
   98: 
   99: #######################################################
  100: #######################################################
  101: my $show_links;
  102: my $output_mode;
  103: my $data;
  104: my $base;
  105: my $datadescription;
  106: my $single_student_mode;
  107: 
  108: #######################################################
  109: #######################################################
  110: # End of package variable declarations
  111: 
  112: =pod
  113: 
  114: =back
  115: 
  116: =cut
  117: 
  118: #######################################################
  119: #######################################################
  120: 
  121: =pod
  122: 
  123: =item &BuildStudentAssessmentPage()
  124: 
  125: Inputs: 
  126: 
  127: =over 4
  128: 
  129: =item $r Apache Request
  130: 
  131: =item $c Apache Connection 
  132: 
  133: =back
  134: 
  135: =cut
  136: 
  137: #######################################################
  138: #######################################################
  139: sub BuildStudentAssessmentPage {
  140:     my ($r,$c)=@_;
  141:     undef($Statistics);
  142:     $single_student_mode = 1 if ($ENV{'form.SelectedStudent'});
  143:     #
  144:     # Print out the HTML headers for the interface
  145:     #    This also parses the output mode selector
  146:     #    This step must *always* be done.
  147:     $r->print(&CreateInterface());
  148:     $r->print('<input type="hidden" name="notfirstrun" value="true" />');
  149:     $r->print('<input type="hidden" name="sort" value="'.
  150:               $ENV{'form.sort'}.'" />');
  151:     $r->rflush();
  152:     if (! exists($ENV{'form.notfirstrun'}) && ! $single_student_mode) {
  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:     }
  169:     #
  170:     if($c->aborted()) {  return ; }
  171:     #
  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:     }
  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();
  188:     #
  189:     # Call the initialize routine selected above
  190:     $initialize->($r);
  191:     foreach my $student (@Students) {
  192:         if($c->aborted()) { 
  193:             $finish->($r);
  194:             return ; 
  195:         }
  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: #######################################################
  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: #######################################################
  255: 
  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: 
  267: #######################################################
  268: #######################################################
  269: 
  270: =pod
  271: 
  272: =item &CreateInterface()
  273: 
  274: Called by &BuildStudentAssessmentPage to create the top part of the
  275: page which displays the chart.
  276: 
  277: Inputs: None
  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: #######################################################
  286: sub CreateInterface {
  287:     my $Str = '';
  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>';
  293:     $Str .= '<td align="center"><b>Enrollment Status</b></td>';
  294:     $Str .= '<td align="center"><b>Sequences and Folders</b></td>';
  295:     $Str .= '<td align="center"><b>Output Format</b></td>';
  296:     $Str .= '<td align="center"><b>Output Data</b></td>';
  297:     $Str .= '</tr>'."\n";
  298:     #
  299:     $Str .= '<tr><td align="center">'."\n";
  300:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  301:     $Str .= '</td><td align="center">';
  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);
  312:     $Str .= '</td><td>'."\n";
  313:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  314:     $Str .= '</td><td>'."\n";
  315:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  316:                                               $only_seq_with_assessments);
  317:     $Str .= '</td><td>'."\n";
  318:     $Str .= &CreateAndParseOutputSelector();
  319:     $Str .= '</td><td>'."\n";
  320:     $Str .= &CreateAndParseOutputDataSelector();
  321:     $Str .= '</td></tr>'."\n";
  322:     $Str .= '</table>'."\n";
  323:     $Str .= '<input type="submit" value="Generate Chart" />';
  324:     $Str .= '&nbsp;'x8;
  325:     return $Str;
  326: }
  327: 
  328: #######################################################
  329: #######################################################
  330: 
  331: =pod
  332: 
  333: =item &CreateAndParseOutputSelector()
  334: 
  335: =cut
  336: 
  337: #######################################################
  338: #######################################################
  339: my @OutputOptions = 
  340:     ({ name  => 'HTML, with links',
  341:        value => 'html, with links',
  342:        description => 'Output HTML with each symbol linked to the problem '.
  343: 	   'which generated it.',
  344:        mode => 'html',
  345:        show_links => 'yes',
  346:        },
  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:        },
  355:      { name  => 'HTML, without links',
  356:        value => 'html, without links',
  357:        description => 'Output HTML.  By not including links, the size of the'.
  358: 	   ' web page is greatly reduced.  If your browser crashes on the '.
  359: 	   'full display, try this.',
  360:        mode => 'html',
  361:        show_links => 'no',
  362:            },
  363:      { name  => 'Excel',
  364:        value => 'excel',
  365:        description => 'Output an Excel file (compatable with Excel 95).',
  366:        mode => 'excel',
  367:        show_links => 'no',
  368:    },
  369:      { name  => 'CSV',
  370:        value => 'csv',
  371:        description => 'Output a comma seperated values file suitable for '.
  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.',
  375:        mode => 'csv',
  376:        show_links => 'no',
  377:            },
  378:      );
  379: 
  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: 
  392: sub CreateAndParseOutputSelector {
  393:     my $Str = '';
  394:     my $elementname = 'chartoutputmode';
  395:     #
  396:     # Format for output options is 'mode, restrictions';
  397:     my $selected = 'html, without links';
  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';
  409:     foreach my $option (@OutputOptions) {
  410:         next if ($option->{'value'} ne $selected);
  411:         $output_mode = $option->{'mode'};
  412:         $show_links  = $option->{'show_links'};
  413:     }
  414: 
  415:     #
  416:     # Build the form element
  417:     $Str = qq/<select size="5" name="$elementname">/;
  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>";
  422:     }
  423:     $Str .= "\n</select>";
  424:     return $Str;
  425: }
  426: 
  427: ##
  428: ## Data selector stuff
  429: ##
  430: my @OutputDataOptions =
  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: }
  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: 
  535: #######################################################
  536: #######################################################
  537: 
  538: =pod
  539: 
  540: =head2 HTML output routines
  541: 
  542: =item &html_initialize($r)
  543: 
  544: Create labels for the columns of student data to show.
  545: 
  546: =item &html_outputstudent($r,$student)
  547: 
  548: Return a line of the chart for a student.
  549: 
  550: =item &html_finish($r)
  551: 
  552: =cut
  553: 
  554: #######################################################
  555: #######################################################
  556: {
  557:     my $padding;
  558:     my $count;
  559: 
  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: 
  563: sub html_initialize {
  564:     my ($r) = @_;
  565:     #
  566:     $padding = ' 'x3;
  567:     $count = 0;
  568:     $nodata_count = 0;
  569:     #
  570:     $r->print("<h3>".$ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  571:               "&nbsp;&nbsp;".localtime(time)."</h3>");
  572: 
  573:     if ($data !~ /^final table/) {
  574:         $r->print("<h3>".$datadescription."</h3>");        
  575:     }
  576:     #
  577:     # Set up progress window for 'final table' display only
  578:     if ($data =~ /^final table/) {
  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:     }
  584:     my $Str = "<pre>\n";
  585:     # First, the @StudentData fields need to be listed
  586:     my @to_show = &get_student_fields_to_show();
  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
  594:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()){
  595:         my $title = $sequence->{'title'};
  596:         my $base  = $sequence->{'base_width'};
  597:         my $width = $sequence->{'width'};
  598:         $Str .= $title.' 'x($width-$base).$padding;
  599:     }
  600:     $Str .= "total</pre>\n";
  601:     $Str .= "<pre>";
  602:     #
  603:     # Check for suppression of output
  604:     if ($data =~ /^final table/) {
  605:         $Str = '';
  606:     }
  607:     $r->print($Str);
  608:     $r->rflush();
  609:     return;
  610: }
  611: 
  612: sub html_outputstudent {
  613:     my ($r,$student) = @_;
  614:     my $Str = '';
  615:     #
  616:     if($count++ % 5 == 0 && $count > 0 && $data !~ /^final table/) {
  617:         $r->print("</pre><pre>");
  618:     }
  619:     # First, the @StudentData fields need to be listed
  620:     my @to_show = &get_student_fields_to_show();
  621:     foreach my $field (@to_show) {
  622:         my $title=$student->{$field};
  623:         my $base = length($title);
  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) {
  636:         $nodata_count++;
  637:         return if ($data =~ /^final table/);
  638:         $Str .= '<font color="blue">No Course Data</font>'."\n";
  639:         $r->print($Str);
  640:         $r->rflush();
  641:         return;
  642:     }
  643:     #
  644:     # By sequence build up the data
  645:     my $studentstats;
  646:     my $PerformanceStr = '';
  647:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  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);
  659:         #
  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));
  666:         } else {
  667:             # Pad with extra spaces
  668:             $performance .= ' 'x($seq->{'width'}-$performance_length-
  669:                                  length($ratio)
  670:                                  ).$ratio;
  671:         }
  672:         #
  673:         $Str .= $performance.$padding;
  674:         #
  675:         $studentstats->{$seq->{'symb'}}->{'score'}= $score;
  676:         $studentstats->{$seq->{'symb'}}->{'max'}  = $seq_max;
  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'};
  683:         if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) {
  684:             $Statistics->{$symb}->{'max'} = $seq_stats->{'max'};
  685:         }
  686:         $score += $seq_stats->{'score'};
  687:         $max   += $seq_stats->{'max'};
  688:     }
  689:     $Str .= ' '.' 'x(length($max)-length($score)).$score.'/'.$max;
  690:     $Str .= " \n";
  691:     #
  692:     # Check for suppressed output and update the progress window if so...
  693:     if ($data =~ /^final table/) {
  694:         $Str = '';
  695:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  696:                                                  'last student');
  697:     }
  698:     #
  699:     $r->print($Str);
  700:     #
  701:     $r->rflush();
  702:     return;
  703: }    
  704: 
  705: sub html_finish {
  706:     my ($r) = @_;
  707:     #
  708:     # Check for suppressed output and close the progress window if so
  709:     if ($data =~ /^final table/) {
  710:         &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  711:     } else {
  712:         $r->print("</pre>\n"); 
  713:     }
  714:     if ($single_student_mode) {
  715:         $r->print(&SingleStudentTotal());
  716:     } else {
  717:         $r->print(&StudentAverageTotal());
  718:     }
  719:     $r->rflush();
  720:     return;
  721: }
  722: 
  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";
  730:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  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:         }
  738:         $total_ave += $ave;
  739:         my $max = $Statistics->{$seq->{'symb'}}->{'max'};
  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: 
  761: sub SingleStudentTotal {
  762:     my $student = &Apache::lonstatistics::current_student();
  763:     my $Str = "<h3>Summary table for ".$student->{'fullname'}." ".
  764:         $student->{'username'}.'@'.$student->{'domain'}."</h3>\n";
  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: 
  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;
  808: my $excel_workbook;
  809: 
  810: my $filename;
  811: my $rows_output;
  812: my $cols_output;
  813: 
  814: my %prog_state; # progress window state
  815: my $request_aborted;
  816: 
  817: sub excel_initialize {
  818:     my ($r) = @_;
  819:     #
  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:     #
  871:     $filename = '/prtspool/'.
  872:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  873:             time.'_'.rand(1000000000).'.xls';
  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)) {
  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");
  890:         return ;
  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.
  897:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  898:     #
  899:     # Add a worksheet
  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);
  905:     #
  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
  932:     $excel_sheet->write($rows_output++,$cols_output++,
  933:                         'Compiled on '.localtime(time));
  934:     #
  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:     }
  941:     #
  942:     # Add the student headers
  943:     $cols_output = 0;
  944:     foreach my $field (&get_student_fields_to_show()) {
  945:         $excel_sheet->write($rows_output,$cols_output++,$field);
  946:     }
  947:     my $row_offset = 0;
  948:     if ($data eq 'tries' || $data eq 'scores') {
  949:         $row_offset = -1;
  950:     }
  951:     #
  952:     # Add the remaining column headers
  953:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  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') {
  974:             $excel_sheet->write($rows_output+1,$cols_output,'score');
  975:             $excel_sheet->write($rows_output+1,$cols_output+1,'maximum');
  976:             $cols_output += 2;
  977:         } else {
  978:             $cols_output++;
  979:         }
  980:     }
  981:     #
  982:     # Bookkeeping
  983:     if ($data eq 'sum and total' || $data eq 'parts correct total') {
  984:         $rows_output += 2;
  985:     } else {
  986:         $rows_output += 1;
  987:     }
  988:     #
  989:     # Output a row for MAX
  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;
 1022:             }
 1023:         } 
 1024:         if (! ($data eq 'sum only' || $data eq 'parts correct')) {
 1025:             $excel_sheet->write($rows_output,$cols_output++,'');
 1026:         }
 1027:         $excel_sheet->write($rows_output,$cols_output++,$max);
 1028:     }
 1029:     $rows_output++;
 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();
 1038:     #
 1039:     # Initialize progress window
 1040:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
 1041:         ($r,'Excel File Compilation Status',
 1042:          'Excel File Compilation Progress', $studentcount);
 1043:     #
 1044:     return;
 1045: }
 1046: 
 1047: sub excel_outputstudent {
 1048:     my ($r,$student) = @_;
 1049:     return if ($request_aborted);
 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
 1070:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 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') {
 1089:             $excel_sheet->write($rows_output,$cols_output++,$score);
 1090:         }
 1091:         if ($data eq 'sum and total' || $data eq 'parts correct total') {
 1092:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
 1093:         }
 1094:     }
 1095:     #
 1096:     # Bookkeeping
 1097:     $rows_output++; 
 1098:     $cols_output=0;
 1099:     #
 1100:     # Update the progress window
 1101:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
 1102:     return;
 1103: }
 1104: 
 1105: sub excel_finish {
 1106:     my ($r) = @_;
 1107:     return if ($request_aborted);
 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:     #
 1116:     # Close the progress window
 1117:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1118:     #
 1119:     # Tell the user where to get their excel file
 1120:     $r->print('<br />'.
 1121:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
 1122:     $r->rflush();
 1123:     return;
 1124: }
 1125: 
 1126: }
 1127: #######################################################
 1128: #######################################################
 1129: 
 1130: =pod
 1131: 
 1132: =head2 CSV output routines
 1133: 
 1134: =item &csv_initialize($r)
 1135: 
 1136: =item &csv_outputstudent($r,$student)
 1137: 
 1138: =item &csv_finish($r)
 1139: 
 1140: =cut
 1141: 
 1142: #######################################################
 1143: #######################################################
 1144: {
 1145: 
 1146: my $outputfile;
 1147: my $filename;
 1148: my $request_aborted;
 1149: my %prog_state; # progress window state
 1150: 
 1151: sub csv_initialize{
 1152:     my ($r) = @_;
 1153:     # 
 1154:     # Clean up
 1155:     $filename = undef;
 1156:     $outputfile = undef;
 1157:     undef(%prog_state);
 1158:     #
 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:     #
 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:     }
 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";
 1190:     #
 1191:     # Print out the headings
 1192:     my $Str = '';
 1193:     my $Str2 = undef;
 1194:     foreach my $field (&get_student_fields_to_show()) {
 1195:         if ($data eq 'sum only') {
 1196:             $Str .= '"'.&Apache::loncommon::csv_translate($field).'",';
 1197:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
 1198:             $Str .= '"",'; # first row empty on the student fields
 1199:             $Str2 .= '"'.&Apache::loncommon::csv_translate($field).'",';
 1200:         } elsif ($data eq 'scores' || $data eq 'tries' || 
 1201:                  $data eq 'parts correct') {
 1202:             $Str  .= '"",';
 1203:             $Str2 .= '"'.&Apache::loncommon::csv_translate($field).'",';
 1204:         }
 1205:     }
 1206:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1207:         if ($data eq 'sum only' || $data eq 'parts correct') {
 1208:             $Str .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
 1209:                 '",';
 1210:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
 1211:             $Str  .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
 1212:                 '","",';
 1213:             $Str2 .= '"score","total possible",';
 1214:         } elsif ($data eq 'scores' || $data eq 'tries') {
 1215:             $Str  .= '"'.&Apache::loncommon::csv_translate($seq->{'title'}).
 1216:                 '",';
 1217:             $Str .= '"",'x($seq->{'num_assess_parts'}-1+2);
 1218:             foreach my $res (@{$seq->{'contents'}}) {
 1219:                 next if ($res->{'type'} ne 'assessment');
 1220:                 foreach my $part (@{$res->{'parts'}}) {
 1221:                     $Str2 .= '"'.&Apache::loncommon::csv_translate($res->{'title'}.', Part '.$part).'",';
 1222:                 }
 1223:             }
 1224:             $Str2 .= '"score","total possible",';
 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);
 1241:     return;
 1242: }
 1243: 
 1244: sub csv_outputstudent {
 1245:     my ($r,$student) = @_;
 1246:     return if ($request_aborted);
 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
 1267:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 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') {
 1279:             $Str .= '"'.$score.'",';
 1280:         } elsif ($data eq 'sum and total' || $data eq 'parts correct total') {
 1281:             $Str  .= '"'.$score.'","'.$seq_max.'",';
 1282:         } elsif ($data eq 'scores' || $data eq 'tries') {
 1283:             $Str .= '"'.join('","',(@$rawdata,$score,$seq_max)).'",';
 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;
 1293: }
 1294: 
 1295: sub csv_finish {
 1296:     my ($r) = @_;
 1297:     return if ($request_aborted);
 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:     
 1313: }
 1314: 
 1315: }
 1316: 
 1317: #######################################################
 1318: #######################################################
 1319: 
 1320: =pod
 1321: 
 1322: =item &StudentTriesOnSequence()
 1323: 
 1324: Inputs:
 1325: 
 1326: =over 4
 1327: 
 1328: =item $student
 1329: 
 1330: =item $studentdata Hash ref to all student data
 1331: 
 1332: =item $seq Hash ref, the sequence we are working on
 1333: 
 1334: =item $links if defined we will output links to each resource.
 1335: 
 1336: =back
 1337: 
 1338: =cut
 1339: 
 1340: #######################################################
 1341: #######################################################
 1342: sub StudentTriesOnSequence {
 1343:     my ($student,$studentdata,$seq,$links) = @_;
 1344:     $links = 'no' if (! defined($links));
 1345:     my $Str = '';
 1346:     my ($sum,$max) = (0,0);
 1347:     my $performance_length = 0;
 1348:     my @TriesData = ();
 1349:     my $tries;
 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'}}) {
 1355:             $tries = undef;
 1356:             $max++;
 1357:             $performance_length++;
 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'})){
 1376:                     $tries = $resource_data->{'resource.'.$partnum.'.tries'};
 1377:                     if ($tries > 9) {
 1378:                         $symbol = '*';
 1379:                     } elsif ($tries > 0) {
 1380:                         $symbol = $tries;
 1381:                     } else {
 1382:                         $symbol = ' ';
 1383:                     }
 1384:                     $sum++;
 1385:                 } elsif (exists($resource_data->{'resource.'.
 1386:                                                      $partnum.'.tries'})){
 1387:                     $symbol = '.';
 1388:                 } else {
 1389:                     $symbol = ' ';
 1390:                 }
 1391:             } else {
 1392:                 # Unsolved.  Did they try?
 1393:                 if (exists($resource_data->{'resource.'.$partnum.'.tries'})){
 1394:                     $symbol = '.';
 1395:                 } else {
 1396:                     $symbol = ' ';
 1397:                 }
 1398:             }
 1399:             #
 1400:             if (! defined($tries)) {
 1401:                 $tries = $symbol;
 1402:             }
 1403:             push (@TriesData,$tries);
 1404:             #
 1405:             if ( ($links eq 'yes' && $symbol ne ' ') ||
 1406:                  ($links eq 'all')) {
 1407:                 if (length($symbol) > 1) {
 1408:                     &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
 1409:                 }
 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;
 1417:         }
 1418:         $Str .= $value;
 1419:     }
 1420:     if ($seq->{'randompick'}) {
 1421:         $max = $seq->{'randompick'};
 1422:     }
 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);
 1519: }
 1520: 
 1521: #######################################################
 1522: #######################################################
 1523: 
 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: 
 1534: #######################################################
 1535: #######################################################
 1536: sub CreateLegend {
 1537:     my $Str = "<p><pre>".
 1538:               "   1  correct by student in 1 try\n".
 1539:               "   7  correct by student in 7 tries\n".
 1540:               "   *  correct by student in more than 9 tries\n".
 1541: 	      "   +  correct by hand grading or override\n".
 1542:               "   -  incorrect by override\n".
 1543: 	      "   .  incorrect attempted\n".
 1544: 	      "   #  ungraded attempted\n".
 1545:               "      not attempted (blank field)\n".
 1546: 	      "   x  excused".
 1547:               "</pre><p>";
 1548:     return $Str;
 1549: }
 1550: 
 1551: #######################################################
 1552: #######################################################
 1553: 
 1554: =pod 
 1555: 
 1556: =back
 1557: 
 1558: =cut
 1559: 
 1560: #######################################################
 1561: #######################################################
 1562: 
 1563: 1;
 1564: 
 1565: __END__

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