File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.63: download - view: text, annotated - select for diffs
Tue Jun 24 19:23:45 2003 UTC (21 years ago) by matthew
Branches: MAIN
CVS tags: HEAD
Return the students score instead of the problem weight.

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

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