File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.98: download - view: text, annotated - select for diffs
Wed Mar 10 18:50:38 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fix bug introduced in last commit to html_outputstudent.  Columns now line
up again.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstudentassessment.pm,v 1.98 2004/03/10 18:50:38 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::loncommon();
   56: use Apache::loncoursedata;
   57: use Apache::lonnet; # for logging porpoises
   58: use Apache::lonlocal;
   59: use Spreadsheet::WriteExcel;
   60: use Spreadsheet::WriteExcel::Utility();
   61: 
   62: #######################################################
   63: #######################################################
   64: =pod
   65: 
   66: =item Package Variables
   67: 
   68: =over 4
   69: 
   70: =item $Statistics Hash ref to store student data.  Indexed by symb,
   71:       contains hashes with keys 'score' and 'max'.
   72: 
   73: =cut
   74: 
   75: #######################################################
   76: #######################################################
   77: 
   78: my $Statistics;
   79: 
   80: #######################################################
   81: #######################################################
   82: 
   83: =pod
   84: 
   85: =item $show_links 'yes' or 'no' for linking to student performance data
   86: 
   87: =item $output_mode 'html', 'excel', or 'csv' for output mode
   88: 
   89: =item $show 'all', 'totals', or 'scores' determines how much data is output
   90: 
   91: =item $single_student_mode evaluates to true if we are showing only one
   92: student.
   93: 
   94: =cut
   95: 
   96: #######################################################
   97: #######################################################
   98: my $show_links;
   99: my $output_mode;
  100: my $chosen_output;
  101: my $single_student_mode;
  102: 
  103: #######################################################
  104: #######################################################
  105: # End of package variable declarations
  106: 
  107: =pod
  108: 
  109: =back
  110: 
  111: =cut
  112: 
  113: #######################################################
  114: #######################################################
  115: 
  116: =pod
  117: 
  118: =item &BuildStudentAssessmentPage()
  119: 
  120: Inputs: 
  121: 
  122: =over 4
  123: 
  124: =item $r Apache Request
  125: 
  126: =item $c Apache Connection 
  127: 
  128: =back
  129: 
  130: =cut
  131: 
  132: #######################################################
  133: #######################################################
  134: sub BuildStudentAssessmentPage {
  135:     my ($r,$c)=@_;
  136:     #
  137:     undef($Statistics);
  138:     undef($show_links);
  139:     undef($output_mode);
  140:     undef($chosen_output);
  141:     undef($single_student_mode);
  142:     #
  143:     my %Saveable_Parameters = ('Status' => 'scalar',
  144:                                'chartoutputmode' => 'scalar',
  145:                                'chartoutputdata' => 'scalar',
  146:                                'Section' => 'array',
  147:                                'StudentData' => 'array',
  148:                                'Maps' => 'array');
  149:     &Apache::loncommon::store_course_settings('chart',\%Saveable_Parameters);
  150:     &Apache::loncommon::restore_course_settings('chart',\%Saveable_Parameters);
  151:     #
  152:     &Apache::lonstatistics::PrepareClasslist();
  153:     #
  154:     $single_student_mode = 0;
  155:     $single_student_mode = 1 if ($ENV{'form.SelectedStudent'});
  156:     if ($ENV{'form.selectstudent'}) {
  157:         &Apache::lonstatistics::DisplayClasslist($r);
  158:         return;
  159:     }
  160:     #
  161:     # Print out the HTML headers for the interface
  162:     #    This also parses the output mode selector
  163:     #    This step must *always* be done.
  164:     $r->print(&CreateInterface());
  165:     $r->print('<input type="hidden" name="notfirstrun" value="true" />');
  166:     $r->print('<input type="hidden" name="sort" value="'.
  167:               $ENV{'form.sort'}.'" />');
  168:     $r->rflush();
  169:     #
  170:     if (! exists($ENV{'form.notfirstrun'}) && ! $single_student_mode) {
  171:         return;
  172:     }
  173:     #
  174:     my $initialize     = \&html_initialize;
  175:     my $output_student = \&html_outputstudent;
  176:     my $finish         = \&html_finish;
  177:     #
  178:     if ($output_mode eq 'excel') {
  179:         $initialize     = \&excel_initialize;
  180:         $output_student = \&excel_outputstudent;
  181:         $finish         = \&excel_finish;
  182:     } elsif ($output_mode eq 'csv') {
  183:         $initialize     = \&csv_initialize;
  184:         $output_student = \&csv_outputstudent;
  185:         $finish         = \&csv_finish;
  186:     }
  187:     #
  188:     if($c->aborted()) {  return ; }
  189:     #
  190:     # Determine which students we want to look at
  191:     my @Students;
  192:     if ($single_student_mode) {
  193:         @Students = (&Apache::lonstatistics::current_student());
  194:         $r->print(&next_and_previous_buttons());
  195:         $r->rflush();
  196:     } else {
  197:         @Students = @Apache::lonstatistics::Students;
  198:     }
  199:     #
  200:     # Perform generic initialization tasks
  201:     #       Since we use lonnet::EXT to retrieve problem weights,
  202:     #       to ensure current data we must clear the caches out.
  203:     #       This makes sure that parameter changes at the student level
  204:     #       are immediately reflected in the chart.
  205:     &Apache::lonnet::clear_EXT_cache_status();
  206:     #
  207:     # Clean out loncoursedata's package data, just to be safe.
  208:     &Apache::loncoursedata::clear_internal_caches();
  209:     #
  210:     # Call the initialize routine selected above
  211:     $initialize->($r);
  212:     foreach my $student (@Students) {
  213:         if($c->aborted()) { 
  214:             $finish->($r);
  215:             return ; 
  216:         }
  217:         # Call the output_student routine selected above
  218:         $output_student->($r,$student);
  219:     }
  220:     # Call the "finish" routine selected above
  221:     $finish->($r);
  222:     #
  223:     return;
  224: }
  225: 
  226: #######################################################
  227: #######################################################
  228: sub next_and_previous_buttons {
  229:     my $Str = '';
  230:     $Str .= '<input type="hidden" name="SelectedStudent" value="'.
  231:         $ENV{'form.SelectedStudent'}.'" />';
  232:     #
  233:     # Build the previous student link
  234:     my $previous = &Apache::lonstatistics::previous_student();
  235:     my $previousbutton = '';
  236:     if (defined($previous)) {
  237:         my $sname = $previous->{'username'}.':'.$previous->{'domain'};
  238:         $previousbutton .= '<input type="button" value="'.
  239:             'Previous Student ('.
  240:             $previous->{'username'}.'@'.$previous->{'domain'}.')'.
  241:             '" onclick="document.Statistics.SelectedStudent.value='.
  242:             "'".$sname."'".';'.
  243:             'document.Statistics.submit();" />';
  244:     } else {
  245:         $previousbutton .= '<input type="button" value="'.
  246:             'Previous student (none)'.'" />';
  247:     }
  248:     #
  249:     # Build the next student link
  250:     my $next = &Apache::lonstatistics::next_student();
  251:     my $nextbutton = '';
  252:     if (defined($next)) {
  253:         my $sname = $next->{'username'}.':'.$next->{'domain'};
  254:         $nextbutton .= '<input type="button" value="'.
  255:             'Next Student ('.
  256:             $next->{'username'}.'@'.$next->{'domain'}.')'.
  257:             '" onclick="document.Statistics.SelectedStudent.value='.
  258:             "'".$sname."'".';'.
  259:             'document.Statistics.submit();" />';
  260:     } else {
  261:         $nextbutton .= '<input type="button" value="'.
  262:             'Next student (none)'.'" />';
  263:     }
  264:     #
  265:     # Build the 'all students' button
  266:     my $all = '';
  267:     $all .= '<input type="button" value="All Students" '.
  268:             '" onclick="document.Statistics.SelectedStudent.value='.
  269:             "''".';'.'document.Statistics.submit();" />';
  270:     $Str .= $previousbutton.('&nbsp;'x5).$all.('&nbsp;'x5).$nextbutton;
  271:     return $Str;
  272: }
  273: 
  274: #######################################################
  275: #######################################################
  276: 
  277: sub get_student_fields_to_show {
  278:     my @to_show = @Apache::lonstatistics::SelectedStudentData;
  279:     foreach (@to_show) {
  280:         if ($_ eq 'all') {
  281:             @to_show = @Apache::lonstatistics::StudentDataOrder;
  282:             last;
  283:         }
  284:     }
  285:     return @to_show;
  286: }
  287: 
  288: #######################################################
  289: #######################################################
  290: 
  291: =pod
  292: 
  293: =item &CreateInterface()
  294: 
  295: Called by &BuildStudentAssessmentPage to create the top part of the
  296: page which displays the chart.
  297: 
  298: Inputs: None
  299: 
  300: Returns:  A string containing the HTML for the headers and top table for 
  301: the chart page.
  302: 
  303: =cut
  304: 
  305: #######################################################
  306: #######################################################
  307: sub CreateInterface {
  308:     my $Str = '';
  309:     $Str .= &Apache::lonhtmlcommon::breadcrumbs(undef,'Chart');
  310: #    $Str .= &CreateLegend();
  311:     $Str .= '<table cellspacing="5">'."\n";
  312:     $Str .= '<tr>';
  313:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  314:     $Str .= '<td align="center"><b>'.&mt('Student Data</b>').'</td>';
  315:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  316:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
  317:     $Str .= '<td align="center"><b>'.&mt('Output Format').'</b>'.
  318:         &Apache::loncommon::help_open_topic("Chart_Output_Formats").
  319:         '</td>';
  320:     $Str .= '<td align="center"><b>'.&mt('Output Data').'</b>'.
  321:         &Apache::loncommon::help_open_topic("Chart_Output_Data").
  322:         '</td>';
  323:     $Str .= '</tr>'."\n";
  324:     #
  325:     $Str .= '<tr><td align="center">'."\n";
  326:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  327:     $Str .= '</td><td align="center">';
  328:     my $only_seq_with_assessments = sub { 
  329:         my $s=shift;
  330:         if ($s->{'num_assess'} < 1) { 
  331:             return 0;
  332:         } else { 
  333:             return 1;
  334:         }
  335:     };
  336:     $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',
  337:                                                       5,undef);
  338:     $Str .= '</td><td>'."\n";
  339:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  340:     $Str .= '</td><td>'."\n";
  341:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
  342:                                               $only_seq_with_assessments);
  343:     $Str .= '</td><td>'."\n";
  344:     $Str .= &CreateAndParseOutputSelector();
  345:     $Str .= '</td><td>'."\n";
  346:     $Str .= &CreateAndParseOutputDataSelector();
  347:     $Str .= '</td></tr>'."\n";
  348:     $Str .= '</table>'."\n";
  349:     $Str .= '<input type="submit" name="Generate Chart" value="'.
  350:         &mt('Generate Chart').'" />';
  351:     $Str .= '&nbsp;'x5;
  352:     $Str .= '<input type="submit" name="selectstudent" value="'.
  353:         &mt('Select One Student').'" />';
  354:     $Str .= '&nbsp;'x5;
  355:     $Str .= '<input type="submit" name="ClearCache" value="'.
  356:         &mt('Clear Caches').'" />';
  357:     $Str .= '&nbsp;'x5;
  358:     $Str .= '<br />';
  359:     return $Str;
  360: }
  361: 
  362: #######################################################
  363: #######################################################
  364: 
  365: =pod
  366: 
  367: =item &CreateAndParseOutputSelector()
  368: 
  369: =cut
  370: 
  371: #######################################################
  372: #######################################################
  373: my @OutputOptions = 
  374:     ({ name  => 'HTML, with links',
  375:        value => 'html, with links',
  376:        description => 'Output HTML with each symbol linked to the problem '.
  377: 	   'which generated it.',
  378:        mode => 'html',
  379:        show_links => 'yes',
  380:        },
  381:      { name  => 'HTML, with all links',
  382:        value => 'html, with all links',
  383:        description => 'Output HTML with each symbol linked to the problem '.
  384: 	   'which generated it.  '.
  385:            'This includes links for unattempted problems.',
  386:        mode => 'html',
  387:        show_links => 'all',
  388:        },
  389:      { name  => 'HTML, without links',
  390:        value => 'html, without links',
  391:        description => 'Output HTML.  By not including links, the size of the'.
  392: 	   ' web page is greatly reduced.  If your browser crashes on the '.
  393: 	   'full display, try this.',
  394:        mode => 'html',
  395:        show_links => 'no',
  396:            },
  397:      { name  => 'Excel',
  398:        value => 'excel',
  399:        description => 'Output an Excel file (compatable with Excel 95).',
  400:        mode => 'excel',
  401:        show_links => 'no',
  402:    },
  403:      { name  => 'CSV',
  404:        value => 'csv',
  405:        description => 'Output a comma separated values file suitable for '.
  406:            'import into a spreadsheet program.  Using this method as opposed '.
  407:            'to Excel output allows you to organize your data before importing'.
  408:            ' it into a spreadsheet program.',
  409:        mode => 'csv',
  410:        show_links => 'no',
  411:            },
  412:      );
  413: 
  414: sub OutputDescriptions {
  415:     my $Str = '';
  416:     $Str .= "<h2>Output Formats</h2>\n";
  417:     $Str .= "<dl>\n";
  418:     foreach my $outputmode (@OutputOptions) {
  419: 	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
  420: 	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
  421:     }
  422:     $Str .= "</dl>\n";
  423:     return $Str;
  424: }
  425: 
  426: sub CreateAndParseOutputSelector {
  427:     my $Str = '';
  428:     my $elementname = 'chartoutputmode';
  429:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  430:                                             [$elementname]);
  431:     #
  432:     # Format for output options is 'mode, restrictions';
  433:     my $selected = 'html, without links';
  434:     if (exists($ENV{'form.'.$elementname})) {
  435:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
  436:             $selected = $ENV{'form.'.$elementname}->[0];
  437:         } else {
  438:             $selected = $ENV{'form.'.$elementname};
  439:         }
  440:     }
  441:     #
  442:     # Set package variables describing output mode
  443:     $show_links  = 'no';
  444:     $output_mode = 'html';
  445:     foreach my $option (@OutputOptions) {
  446:         next if ($option->{'value'} ne $selected);
  447:         $output_mode = $option->{'mode'};
  448:         $show_links  = $option->{'show_links'};
  449:     }
  450: 
  451:     #
  452:     # Build the form element
  453:     $Str = qq/<select size="5" name="$elementname">/;
  454:     foreach my $option (@OutputOptions) {
  455:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
  456:         $Str .= " selected " if ($option->{'value'} eq $selected);
  457:         $Str .= ">".&mt($option->{'name'})."<\/option>";
  458:     }
  459:     $Str .= "\n</select>";
  460:     return $Str;
  461: }
  462: 
  463: ##
  464: ## Data selector stuff
  465: ##
  466: my @OutputDataOptions =
  467:     (
  468:      { name  => 'Scores Summary',
  469:        base  => 'scores',
  470:        value => 'sum and total',
  471:        scores => 1,
  472:        tries  => 0,
  473:        every_problem => 0,
  474:        sequence_sum => 1,
  475:        sequence_max => 1,
  476:        grand_total => 1,
  477:        summary_table => 1,
  478:        maximum_row => 1,
  479:        shortdesc => 'Total Score and Maximum Possible for each '.
  480:            'Sequence or Folder',
  481:        longdesc => 'The score of each student as well as the '.
  482:            ' maximum possible on each Sequence or Folder.',
  483:        },
  484:      { name  => 'Scores Per Problem',
  485:        base  => 'scores',
  486:        value => 'scores',
  487:        scores => 1,
  488:        tries  => 0,
  489:        correct => 0,
  490:        every_problem => 1,
  491:        sequence_sum => 1,
  492:        sequence_max => 1,
  493:        grand_total => 1,
  494:        summary_table => 1,
  495:        maximum_row => 1,
  496:        shortdesc => 'Score on each Problem Part',
  497:        longdesc =>'The students score on each problem part, computed as'.
  498:            'the part weight * part awarded',
  499:        },
  500:      { name  =>'Tries',
  501:        base  =>'tries',
  502:        value => 'tries',
  503:        scores => 0,
  504:        tries  => 1,
  505:        correct => 0,
  506:        every_problem => 1,
  507:        sequence_sum => 0,
  508:        sequence_max => 0,
  509:        grand_total => 0,
  510:        summary_table => 0,
  511:        maximum_row => 0,
  512:        shortdesc => 'Number of Tries before success on each Problem Part',
  513:        longdesc =>'The number of tries before success on each problem part.',
  514:        },
  515:      { name  =>'Parts Correct',
  516:        base  =>'tries',
  517:        value => 'parts correct total',
  518:        scores => 0,
  519:        tries  => 0,
  520:        correct => 1,
  521:        every_problem => 1,
  522:        sequence_sum => 1,
  523:        sequence_max => 1,
  524:        grand_total => 1,
  525:        summary_table => 1,
  526:        maximum_row => 0,
  527:        shortdesc => 'Number of Problem Parts completed successfully.',
  528:        longdesc => 'The Number of Problem Parts completed successfully and '.
  529:            'the maximum possible for each student',
  530:        },
  531:      );
  532: 
  533: sub HTMLifyOutputDataDescriptions {
  534:     my $Str = '';
  535:     $Str .= "<h2>Output Data</h2>\n";
  536:     $Str .= "<dl>\n";
  537:     foreach my $option (@OutputDataOptions) {
  538:         $Str .= '    <dt>'.$option->{'name'}.'</dt>';
  539:         $Str .= '<dd>'.$option->{'longdesc'}.'</dd>'."\n";
  540:     }
  541:     $Str .= "</dl>\n";
  542:     return $Str;
  543: }
  544: 
  545: sub CreateAndParseOutputDataSelector {
  546:     my $Str = '';
  547:     my $elementname = 'chartoutputdata';
  548:     #
  549:     my $selected = 'scores';
  550:     if (exists($ENV{'form.'.$elementname})) {
  551:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
  552:             $selected = $ENV{'form.'.$elementname}->[0];
  553:         } else {
  554:             $selected = $ENV{'form.'.$elementname};
  555:         }
  556:     }
  557:     #
  558:     $chosen_output = $OutputDataOptions[0];
  559:     foreach my $option (@OutputDataOptions) {
  560:         if ($option->{'value'} eq $selected) {
  561:             $chosen_output = $option;
  562:         }
  563:     }
  564:     #
  565:     # Build the form element
  566:     $Str = qq/<select size="5" name="$elementname">/;
  567:     foreach my $option (@OutputDataOptions) {
  568:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
  569:         $Str .= " selected " if ($option->{'value'} eq $chosen_output->{'value'});
  570:         $Str .= ">".&mt($option->{'name'})."<\/option>";
  571:     }
  572:     $Str .= "\n</select>";
  573:     return $Str;
  574: 
  575: }
  576: 
  577: #######################################################
  578: #######################################################
  579: 
  580: =pod
  581: 
  582: =head2 HTML output routines
  583: 
  584: =item &html_initialize($r)
  585: 
  586: Create labels for the columns of student data to show.
  587: 
  588: =item &html_outputstudent($r,$student)
  589: 
  590: Return a line of the chart for a student.
  591: 
  592: =item &html_finish($r)
  593: 
  594: =cut
  595: 
  596: #######################################################
  597: #######################################################
  598: {
  599:     my $padding;
  600:     my $count;
  601: 
  602:     my $nodata_count; # The number of students for which there is no data
  603:     my %prog_state;   # progress state used by loncommon PrgWin routines
  604: 
  605: sub html_initialize {
  606:     my ($r) = @_;
  607:     #
  608:     $padding = ' 'x3;
  609:     $count = 0;
  610:     $nodata_count = 0;
  611:     undef(%prog_state);
  612:     #
  613:     $r->print("<h3>".$ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
  614:               "&nbsp;&nbsp;".localtime(time)."</h3>");
  615: 
  616:     if ($chosen_output->{'base'} !~ /^final table/) {
  617:         $r->print("<h3>".$chosen_output->{'shortdesc'}."</h3>");        
  618:     }
  619:     my $Str = "<pre>\n";
  620:     # First, the @StudentData fields need to be listed
  621:     my @to_show = &get_student_fields_to_show();
  622:     foreach my $field (@to_show) {
  623:         my $title=$Apache::lonstatistics::StudentData{$field}->{'title'};
  624:         my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'};
  625:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
  626:         $Str .= $title.' 'x($width-$base).$padding;
  627:     }
  628:     #
  629:     # Compute the column widths and output the sequence titles
  630:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()){
  631:         #
  632:         # Comptue column widths
  633:         $sequence->{'width_sum'} = 0;
  634:         if ($chosen_output->{'sequence_sum'}) {
  635:             # Use 3 digits for the sum
  636:             $sequence->{'width_sum'} = 3;
  637:         }
  638:         if ($chosen_output->{'sequence_max'}) {
  639:             if ($sequence->{'width_sum'}>0) {
  640:                 # One digit for the '/'
  641:                 $sequence->{'width_sum'} +=1;
  642:             }
  643:             # Use 3 digits for the total
  644:             $sequence->{'width_sum'}+=3;
  645:         }
  646: 	#
  647:         if ($chosen_output->{'every_problem'}) {
  648:             # one problem per digit
  649:             $sequence->{'width_problem'} = $sequence->{'num_assess_parts'};
  650:         } else {
  651:             $sequence->{'width_problem'} = 0;
  652:         }
  653:         $sequence->{'width_total'} = $sequence->{'width_problem'} + 
  654:                                      $sequence->{'width_sum'};
  655:         if ($sequence->{'width_total'} < length(&HTML::Entities::decode($sequence->{'title'}))) {
  656:             $sequence->{'width_total'} = length(&HTML::Entities::decode($sequence->{'title'}));
  657:         }
  658:         #
  659:         # Output the sequence titles
  660:         $Str .= 
  661:             $sequence->{'title'}.' 'x($sequence->{'width_total'}-
  662:                                       length($sequence->{'title'})
  663:                                       ).$padding;
  664:     }
  665:     $Str .= "total</pre>\n";
  666:     $Str .= "<pre>";
  667:     $r->print($Str);
  668:     $r->rflush();
  669:     return;
  670: }
  671: 
  672: sub html_outputstudent {
  673:     my ($r,$student) = @_;
  674:     my $Str = '';
  675:     #
  676:     if($count++ % 5 == 0 && $count > 0) {
  677:         $r->print("</pre><pre>");
  678:     }
  679:     # First, the @StudentData fields need to be listed
  680:     my @to_show = &get_student_fields_to_show();
  681:     foreach my $field (@to_show) {
  682:         my $title=$student->{$field};
  683:         my $base = length($title);
  684:         my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
  685:         $Str .= $title.' 'x($width-$base).$padding;
  686:     }
  687:     # Get ALL the students data
  688:     my %StudentsData;
  689:     my @tmp = &Apache::loncoursedata::get_current_state
  690:         ($student->{'username'},$student->{'domain'},undef,
  691:          $ENV{'request.course.id'});
  692:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
  693:         %StudentsData = @tmp;
  694:     }
  695:     if (scalar(@tmp) < 1) {
  696:         $nodata_count++;
  697:         $Str .= '<font color="blue">No Course Data</font>'."\n";
  698:         $r->print($Str);
  699:         $r->rflush();
  700:         return;
  701:     }
  702:     #
  703:     # By sequence build up the data
  704:     my $studentstats;
  705:     my $PerformanceStr = '';
  706:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  707:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
  708:         if ($chosen_output->{'tries'}) {
  709:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
  710:                 &StudentTriesOnSequence($student,\%StudentsData,
  711:                                         $seq,$show_links);
  712:         } else {
  713:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
  714:                 &StudentPerformanceOnSequence($student,\%StudentsData,
  715:                                               $seq,$show_links);
  716:         }
  717:         my $ratio='';
  718:         if ($chosen_output->{'sequence_sum'} && $score ne ' ') {
  719:             $ratio .= sprintf("%3d",$score);
  720:         } elsif($chosen_output->{'sequence_sum'}) {
  721:             $ratio .= ' 'x3;
  722:         }
  723:         if ($chosen_output->{'sequence_max'}) {
  724:             if ($chosen_output->{'sequence_sum'}) {
  725:                 $ratio .= '/';
  726:             }
  727:             $ratio .= sprintf("%3d",$seq_max);
  728:         }
  729:         #
  730:         if (! $chosen_output->{'every_problem'}) {
  731:             $performance = '';
  732: 	    $performance_length=0;
  733:         }
  734:         $performance .= ' 'x($seq->{'width_total'}-$performance_length-$seq->{'width_sum'}).
  735:             $ratio;
  736:         #
  737:         $Str .= $performance.$padding;
  738:         #
  739:         $studentstats->{$seq->{'symb'}}->{'score'}= $score;
  740:         $studentstats->{$seq->{'symb'}}->{'max'}  = $seq_max;
  741:     }
  742:     #
  743:     # Total it up and store the statistics info.
  744:     my ($score,$max);
  745:     while (my ($symb,$seq_stats) = each (%{$studentstats})) {
  746:         $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
  747:         if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) {
  748:             $Statistics->{$symb}->{'max'} = $seq_stats->{'max'};
  749:         }
  750:         if ($seq_stats->{'score'} ne ' ') {
  751:             $score += $seq_stats->{'score'};
  752:             $Statistics->{$symb}->{'num_students'}++;
  753:         }
  754:         $max   += $seq_stats->{'max'};
  755:     }
  756:     if (! defined($score)) {
  757:         $score = ' 'x3;
  758:     }
  759:     $Str .= ' '.' 'x(length($max)-length($score)).$score.'/'.$max;
  760:     $Str .= " \n";
  761:     #
  762:     $r->print($Str);
  763:     #
  764:     $r->rflush();
  765:     return;
  766: }    
  767: 
  768: sub html_finish {
  769:     my ($r) = @_;
  770:     #
  771:     # Check for suppressed output and close the progress window if so
  772:     $r->print("</pre>\n"); 
  773:     if ($chosen_output->{'summary_table'}) {
  774:         if ($single_student_mode) {
  775:             $r->print(&SingleStudentTotal());
  776:         } else {
  777:             $r->print(&StudentAverageTotal());
  778:         }
  779:     }
  780:     $r->rflush();
  781:     return;
  782: }
  783: 
  784: sub StudentAverageTotal {
  785:     my $Str = "<h3>Summary Tables</h3>\n";
  786: #    my $max_students;
  787: #    my $total_ave = 0;
  788: #    my $total_max = 0;
  789:     $Str .= '<table border=2 cellspacing="1">'."\n";
  790:     $Str .= "<tr><th>Title</th><th>Average</th><th>Maximum</th></tr>\n";
  791:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  792:         my $ave;
  793:         my $num_students = $Statistics->{$seq->{'symb'}}->{'num_students'};
  794: #        if ($num_students > $max_students) {
  795: #            $max_students = $num_students;
  796: #        }
  797:         if ($num_students > 0) {
  798:             $ave = int(100*
  799:                        ($Statistics->{$seq->{'symb'}}->{'score'}/$num_students)
  800:                        )/100;
  801:         } else {
  802:             $ave = 0;
  803:         }
  804: #        $total_ave += $ave;
  805:         my $max = $Statistics->{$seq->{'symb'}}->{'max'};
  806: #        $total_max += $max;
  807:         $ave = sprintf("%.2f",$ave);
  808:         $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
  809:             '<td align="right">'.$ave.'&nbsp;</td>'.
  810:             '<td align="right">'.$max.'&nbsp;'.'</td></tr>'."\n";
  811:     }
  812: #    $total_ave = sprintf('%.2f',$total_ave); # only two digit
  813:     $Str .= "</table>\n";
  814: #    $Str .= '<table border=2 cellspacing="1">'."\n";
  815: #    $Str .= '<tr><th>Number of Students</th><th>Average</th>'.
  816: #        "<th>Maximum</th></tr>\n";
  817: #    $Str .= '<tr>'.
  818: #        '<td align="right">'.$max_students.'</td>'.
  819: #        '<td align="right">'.$total_ave.'&nbsp;'.'</td>'.
  820: #        '<td align="right">'.$total_max.'&nbsp;'.'</td>';
  821: #    $Str .= "</table>\n";
  822:     return $Str;
  823: }
  824: 
  825: sub SingleStudentTotal {
  826:     my $student = &Apache::lonstatistics::current_student();
  827:     my $Str = "<h3>Summary table for ".$student->{'fullname'}." ".
  828:         $student->{'username'}.'@'.$student->{'domain'}."</h3>\n";
  829:     $Str .= '<table border=2 cellspacing="1">'."\n";
  830:     $Str .= 
  831:         "<tr><th>Sequence or Folder</th><th>Score</th><th>Maximum</th></tr>\n";
  832:     my $total = 0;
  833:     my $total_max = 0;
  834:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  835:         my $value = $Statistics->{$seq->{'symb'}}->{'score'};
  836:         my $max = $Statistics->{$seq->{'symb'}}->{'max'};
  837:         $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
  838:             '<td align="right">'.$value.'</td>'.
  839:                 '<td align="right">'.$max.'</td></tr>'."\n";
  840:         $total += $value;
  841:         $total_max +=$max;
  842:     }
  843:     $Str .= '<tr><td><b>Total</b></td>'.
  844:         '<td align="right">'.$total.'</td>'.
  845:         '<td align="right">'.$total_max."</td></tr>\n";
  846:     $Str .= "</table>\n";
  847:     return $Str;
  848: }
  849: 
  850: }
  851: 
  852: #######################################################
  853: #######################################################
  854: 
  855: =pod
  856: 
  857: =head2 EXCEL subroutines
  858: 
  859: =item &excel_initialize($r)
  860: 
  861: =item &excel_outputstudent($r,$student)
  862: 
  863: =item &excel_finish($r)
  864: 
  865: =cut
  866: 
  867: #######################################################
  868: #######################################################
  869: {
  870: 
  871: my $excel_sheet;
  872: my $excel_workbook;
  873: 
  874: my $filename;
  875: my $rows_output;
  876: my $cols_output;
  877: 
  878: my %prog_state; # progress window state
  879: my $request_aborted;
  880: 
  881: my $total_formula;
  882: 
  883: sub excel_initialize {
  884:     my ($r) = @_;
  885:     #
  886:     undef ($excel_sheet);
  887:     undef ($excel_workbook);
  888:     undef ($filename);
  889:     undef ($rows_output);
  890:     undef ($cols_output);
  891:     undef (%prog_state);
  892:     undef ($request_aborted);
  893:     undef ($total_formula);
  894:     #
  895:     my $total_columns = scalar(&get_student_fields_to_show());
  896:     my $num_students = scalar(@Apache::lonstatistics::Students);
  897:     #
  898:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  899:         if ($chosen_output->{'every_problem'}) {
  900:             $total_columns += $seq->{'num_assess_parts'};
  901:         }
  902:         # Add 2 because we need a 'sequence_sum' and 'total' column for each
  903:         $total_columns += 2;
  904:     }
  905:     if ($chosen_output->{'base'} eq 'tries' && $total_columns > 255) {
  906:         $r->print(<<END);
  907: <h2>Unable to Complete Request</h2>
  908: <p>
  909: LON-CAPA is unable to produce your Excel spreadsheet because your selections
  910: will result in more than 255 columns.  Excel allows only 255 columns in a
  911: spreadsheet.
  912: </p><p>
  913: You may consider reducing the number of <b>Sequences or Folders</b> you
  914: have selected.  
  915: </p><p>
  916: LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the
  917: summary data (<b>Parts Correct</b> or <b>Parts Correct & Totals</b>).
  918: </p>
  919: END
  920:        $request_aborted = 1;
  921:     }
  922:     if ($chosen_output->{'base'} eq 'scores' && $total_columns > 255) {
  923:         $r->print(<<END);
  924: <h2>Unable to Complete Request</h2>
  925: <p>
  926: LON-CAPA is unable to produce your Excel spreadsheet because your selections
  927: will result in more than 255 columns.  Excel allows only 255 columns in a
  928: spreadsheet.
  929: </p><p>
  930: You may consider reducing the number of <b>Sequences or Folders</b> you
  931: have selected.  
  932: </p><p>
  933: LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the
  934: <b>Scores Summary</b> data.
  935: </p>
  936: END
  937:        $request_aborted = 1;
  938:     }
  939:     return if ($request_aborted);
  940:     #
  941:     $filename = '/prtspool/'.
  942:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  943:             time.'_'.rand(1000000000).'.xls';
  944:     #
  945:     $excel_workbook = undef;
  946:     $excel_sheet = undef;
  947:     #
  948:     $rows_output = 0;
  949:     $cols_output = 0;
  950:     #
  951:     # Determine rows 
  952:     my $header_row = $rows_output++;
  953:     my $description_row = $rows_output++;
  954:     $rows_output++;        # blank row
  955:     my $summary_header_row;
  956:     if ($chosen_output->{'summary_table'}) {
  957:         $summary_header_row = $rows_output++;
  958:         $rows_output+= scalar(&Apache::lonstatistics::Sequences_with_Assess());
  959:         $rows_output++;
  960:     }
  961:     my $sequence_name_row = $rows_output++;
  962:     my $resource_name_row = $rows_output++;
  963:     my $maximum_data_row = $rows_output++;
  964:     if (! $chosen_output->{'maximum_row'}) {
  965:         $rows_output--;
  966:     }
  967:     my $first_data_row = $rows_output++;
  968:     #
  969:     # Create sheet
  970:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
  971:     #
  972:     # Check for errors
  973:     if (! defined($excel_workbook)) {
  974:         $r->log_error("Error creating excel spreadsheet $filename: $!");
  975:         $r->print("Problems creating new Excel file.  ".
  976:                   "This error has been logged.  ".
  977:                   "Please alert your LON-CAPA administrator");
  978:         return ;
  979:     }
  980:     #
  981:     # The excel spreadsheet stores temporary data in files, then put them
  982:     # together.  If needed we should be able to disable this (memory only).
  983:     # The temporary directory must be specified before calling 'addworksheet'.
  984:     # File::Temp is used to determine the temporary directory.
  985:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
  986:     #
  987:     my $format = &Apache::loncommon::define_excel_formats($excel_workbook);
  988:     #
  989:     # Add a worksheet
  990:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  991:     $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
  992:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
  993:     #
  994:     # Put the course description in the header
  995:     $excel_sheet->write($header_row,$cols_output++,
  996:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
  997:                         $format->{'h1'});
  998:     $cols_output += 3;
  999:     #
 1000:     # Put a description of the sections listed
 1001:     my $sectionstring = '';
 1002:     my @Sections = @Apache::lonstatistics::SelectedSections;
 1003:     if (scalar(@Sections) > 1) {
 1004:         if (scalar(@Sections) > 2) {
 1005:             my $last = pop(@Sections);
 1006:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
 1007:         } else {
 1008:             $sectionstring = "Sections ".join(' and ',@Sections);
 1009:         }
 1010:     } else {
 1011:         if ($Sections[0] eq 'all') {
 1012:             $sectionstring = "All sections";
 1013:         } else {
 1014:             $sectionstring = "Section ".$Sections[0];
 1015:         }
 1016:     }
 1017:     $excel_sheet->write($header_row,$cols_output++,$sectionstring,
 1018:                         $format->{'h3'});
 1019:     $cols_output += scalar(@Sections);
 1020:     #
 1021:     # Put the date in there too
 1022:     $excel_sheet->write($header_row,$cols_output++,
 1023:                         'Compiled on '.localtime(time),$format->{'h3'});
 1024:     #
 1025:     $cols_output = 0;
 1026:     $excel_sheet->write($description_row,$cols_output++,
 1027:                         $chosen_output->{'shortdesc'},
 1028:                         $format->{'h3'});
 1029:     ##############################################
 1030:     # Output headings for the raw data
 1031:     ##############################################
 1032:     #
 1033:     # Add the student headers
 1034:     $cols_output = 0;
 1035:     foreach my $field (&get_student_fields_to_show()) {
 1036:         $excel_sheet->write($resource_name_row,$cols_output++,$field,
 1037:                             $format->{'bold'});
 1038:     }
 1039:     #
 1040:     # Add the remaining column headers
 1041:     my $total_formula_string = '=0';
 1042:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1043:         $excel_sheet->write($sequence_name_row,,
 1044:                             $cols_output,$seq->{'title'},$format->{'bold'});
 1045:         # Determine starting cell
 1046:         $seq->{'Excel:startcell'}=
 1047:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1048:             ($first_data_row,$cols_output);
 1049:         $seq->{'Excel:startcol'}=$cols_output;
 1050:         my $count = 0;
 1051:         if ($chosen_output->{'every_problem'}) {
 1052:             # Put the names of the problems and parts into the sheet
 1053:             foreach my $res (@{$seq->{'contents'}}) {
 1054:                 if ($res->{'type'} ne 'assessment'  || 
 1055:                     ! exists($res->{'parts'})       ||
 1056:                     ref($res->{'parts'}) ne 'ARRAY' ||
 1057:                     scalar(@{$res->{'parts'}}) < 1) {
 1058:                     next;
 1059:                 }
 1060:                 if (scalar(@{$res->{'parts'}}) > 1) {
 1061:                     foreach my $part (@{$res->{'parts'}}) {
 1062:                         $excel_sheet->write($resource_name_row,
 1063:                                             $cols_output++,
 1064:                                             $res->{'title'}.' part '.$part,
 1065:                                             $format->{'bold'});
 1066:                     }
 1067:                 } else {
 1068:                     $excel_sheet->write($resource_name_row,
 1069:                                         $cols_output++,
 1070:                                         $res->{'title'},$format->{'bold'});
 1071:                 }
 1072:                 $count++;
 1073:             }
 1074:         }
 1075:         # Determine ending cell
 1076:         if ($count <= 1) {
 1077:             $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
 1078:             $seq->{'Excel:endcol'}  = $seq->{'Excel:startcol'};
 1079:         } else {
 1080:             $seq->{'Excel:endcell'} = 
 1081:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1082:                 ($first_data_row,$cols_output-1);
 1083:             $seq->{'Excel:endcol'} = $cols_output-1;
 1084:         }
 1085:         # Create the formula for summing up this sequence
 1086:         if (! exists($seq->{'Excel:endcell'}) ||
 1087:             ! defined($seq->{'Excel:endcell'})) {
 1088:             $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
 1089:         }
 1090:         $seq->{'Excel:sum'}= $excel_sheet->store_formula
 1091:             ('=SUM('.$seq->{'Excel:startcell'}.
 1092:              ':'.$seq->{'Excel:endcell'}.')');
 1093:         # Determine cell the score is held in
 1094:         $seq->{'Excel:scorecell'} = 
 1095:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1096:             ($first_data_row,$cols_output);
 1097:         $seq->{'Excel:scorecol'}=$cols_output;
 1098:         if ($chosen_output->{'base'} eq 'parts correct total') {
 1099:             $excel_sheet->write($resource_name_row,$cols_output++,
 1100:                                 'parts correct',
 1101:                                 $format->{'bold'});
 1102:         } elsif ($chosen_output->{'sequence_sum'}) {
 1103:             if ($chosen_output->{'correct'}) {
 1104:                 # Only reporting the number correct, so do not call it score
 1105:                 $excel_sheet->write($resource_name_row,$cols_output++,
 1106:                                     'sum',
 1107:                                     $format->{'bold'});
 1108:             } else {
 1109:                 $excel_sheet->write($resource_name_row,$cols_output++,
 1110:                                     'score',
 1111:                                     $format->{'bold'});
 1112:             }
 1113:         }
 1114:         #
 1115:         $total_formula_string.='+'.
 1116:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1117:             ($first_data_row,$cols_output-1);
 1118:         if ($chosen_output->{'sequence_max'}) {
 1119:             $excel_sheet->write($resource_name_row,$cols_output++,
 1120:                                 'maximum',
 1121:                                 $format->{'bold'});
 1122:         }
 1123:     }
 1124:     if ($chosen_output->{'grand_total'}) {
 1125:         $excel_sheet->write($resource_name_row,$cols_output++,'Total',
 1126:                             $format->{'bold'});
 1127:     }
 1128:     $total_formula = $excel_sheet->store_formula($total_formula_string);
 1129:     ##############################################
 1130:     # Output a row for MAX, if appropriate
 1131:     ##############################################
 1132:     if ($chosen_output->{'maximum_row'}) {
 1133:         $cols_output = 0;
 1134:         foreach my $field (&get_student_fields_to_show()) {
 1135:             if ($field eq 'username' || $field eq 'fullname' || 
 1136:                 $field eq 'id') {
 1137:                 $excel_sheet->write($maximum_data_row,$cols_output++,'Maximum',
 1138:                                     $format->{'bold'});
 1139:             } else {
 1140:                 $excel_sheet->write($maximum_data_row,$cols_output++,'');
 1141:             }
 1142:         }
 1143:         #
 1144:         # Add the maximums for each sequence or assessment
 1145:         my %total_cell_translation;
 1146:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1147:             $cols_output=$seq->{'Excel:startcol'};
 1148:             $total_cell_translation{$seq->{'Excel:scorecell'}} = 
 1149:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1150:                 ($maximum_data_row,$seq->{'Excel:scorecol'});
 1151:             my $weight;
 1152:             my $max = 0;
 1153:             foreach my $resource (@{$seq->{'contents'}}) {
 1154:                 next if ($resource->{'type'} ne 'assessment');
 1155:                 foreach my $part (@{$resource->{'parts'}}) {
 1156:                     $weight = 1;
 1157:                     if ($chosen_output->{'scores'}) {
 1158:                         $weight = &Apache::lonnet::EXT
 1159:                             ('resource.'.$part.'.weight',$resource->{'symb'},
 1160:                              undef,undef,undef);
 1161:                         if (!defined($weight) || ($weight eq '')) { 
 1162:                             $weight=1;
 1163:                         }
 1164:                     }
 1165:                     if ($chosen_output->{'scores'} &&
 1166:                         $chosen_output->{'every_problem'}) {
 1167:                         $excel_sheet->write($maximum_data_row,$cols_output++,
 1168:                                             $weight);
 1169:                     }
 1170:                     $max += $weight;
 1171:                 }
 1172:             } 
 1173:             #
 1174:             if ($chosen_output->{'sequence_sum'} && 
 1175:                 $chosen_output->{'every_problem'}) {
 1176:                 my %replaceCells;
 1177:                 $replaceCells{$seq->{'Excel:startcell'}} = 
 1178:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1179:                     ($maximum_data_row,$seq->{'Excel:startcol'});
 1180:                 $replaceCells{$seq->{'Excel:endcell'}} = 
 1181:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1182:                     ($maximum_data_row,$seq->{'Excel:endcol'});
 1183:                 $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
 1184:                                              $seq->{'Excel:sum'},undef,
 1185:                                              %replaceCells);
 1186:             } elsif ($chosen_output->{'sequence_sum'}) {
 1187:                 $excel_sheet->write($maximum_data_row,$cols_output++,$max);
 1188:             }
 1189:             if ($chosen_output->{'sequence_max'}) {
 1190:                 $excel_sheet->write($maximum_data_row,$cols_output++,$max);
 1191:             }
 1192:             #
 1193:         }
 1194:         if ($chosen_output->{'grand_total'}) {
 1195:             $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
 1196:                                          $total_formula,undef,
 1197:                                          %total_cell_translation);
 1198:         }
 1199:     } # End of MAXIMUM row output  if ($chosen_output->{'maximum_row'}) {
 1200:     $rows_output = $first_data_row;
 1201:     ##############################################
 1202:     # Output summary table, which actually is above the sequence name row.
 1203:     ##############################################
 1204:     if ($chosen_output->{'summary_table'}) {
 1205:         $cols_output = 0;
 1206:         $excel_sheet->write($summary_header_row,$cols_output++,
 1207:                             'Summary Table',$format->{'bold'});
 1208:         if ($chosen_output->{'maximum_row'}) {
 1209:             $excel_sheet->write($summary_header_row,$cols_output++,
 1210:                                 'Maximum',$format->{'bold'});
 1211:         }
 1212:         $excel_sheet->write($summary_header_row,$cols_output++,
 1213:                             'Average',$format->{'bold'});
 1214:         $excel_sheet->write($summary_header_row,$cols_output++,
 1215:                             'Median',$format->{'bold'});
 1216:         $excel_sheet->write($summary_header_row,$cols_output++,
 1217:                             'Std Dev',$format->{'bold'});
 1218:         my $row = $summary_header_row+1;
 1219:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1220:             $cols_output = 0;
 1221:             $excel_sheet->write($row,$cols_output++,
 1222:                                 $seq->{'title'},
 1223:                                 $format->{'bold'});
 1224:             if ($chosen_output->{'maximum_row'}) {
 1225:                 $excel_sheet->write
 1226:                     ($row,$cols_output++,
 1227:                      '='.
 1228:                      &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1229:                      ($maximum_data_row,$seq->{'Excel:scorecol'})
 1230:                      );
 1231:             }
 1232:             my $range = 
 1233:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1234:                 ($first_data_row,$seq->{'Excel:scorecol'}).
 1235:                 ':'.
 1236:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1237:                 ($first_data_row+$num_students-1,$seq->{'Excel:scorecol'});
 1238:             $excel_sheet->write($row,$cols_output++,
 1239:                                 '=AVERAGE('.$range.')');
 1240:             $excel_sheet->write($row,$cols_output++,
 1241:                                 '=MEDIAN('.$range.')');
 1242:             $excel_sheet->write($row,$cols_output++,
 1243:                                 '=STDEV('.$range.')');
 1244:             $row++;
 1245:         }
 1246:     }
 1247:     ##############################################
 1248:     #   Take care of non-excel initialization
 1249:     ##############################################
 1250:     #
 1251:     # Let the user know what we are doing
 1252:     my $studentcount = scalar(@Apache::lonstatistics::Students); 
 1253:     if ($ENV{'form.SelectedStudent'}) {
 1254:         $studentcount = '1';
 1255:     }
 1256:     if ($studentcount > 1) {
 1257:         $r->print('<h1>'.&mt('Compiling Excel spreadsheet for [_1] students',
 1258:                              $studentcount)."</h1>\n");
 1259:     } else {
 1260:         $r->print('<h1>'.
 1261:                   &mt('Compiling Excel spreadsheet for 1 student').
 1262:                   "</h1>\n");
 1263:     }
 1264:     $r->rflush();
 1265:     #
 1266:     # Initialize progress window
 1267:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
 1268:         ($r,'Excel File Compilation Status',
 1269:          'Excel File Compilation Progress', $studentcount);
 1270:     #
 1271:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
 1272:                                           'Processing first student');
 1273:     return;
 1274: }
 1275: 
 1276: sub excel_outputstudent {
 1277:     my ($r,$student) = @_;
 1278:     return if ($request_aborted);
 1279:     return if (! defined($excel_sheet));
 1280:     $cols_output=0;
 1281:     #
 1282:     # Write out student data
 1283:     my @to_show = &get_student_fields_to_show();
 1284:     foreach my $field (@to_show) {
 1285:         $excel_sheet->write($rows_output,$cols_output++,$student->{$field});
 1286:     }
 1287:     #
 1288:     # Get student assessment data
 1289:     my %StudentsData;
 1290:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
 1291:                                                         $student->{'domain'},
 1292:                                                         undef,
 1293:                                                    $ENV{'request.course.id'});
 1294:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
 1295:         %StudentsData = @tmp;
 1296:     }
 1297:     #
 1298:     # Write out sequence scores and totals data
 1299:     my %total_cell_translation;
 1300:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1301:         $cols_output = $seq->{'Excel:startcol'};
 1302:         # Keep track of cells to translate in total cell
 1303:         $total_cell_translation{$seq->{'Excel:scorecell'}} = 
 1304:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1305:                         ($rows_output,$seq->{'Excel:scorecol'});
 1306:         #
 1307:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
 1308:         if ($chosen_output->{'tries'} || $chosen_output->{'correct'}){
 1309:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
 1310:                 &StudentTriesOnSequence($student,\%StudentsData,
 1311:                                         $seq,'no');
 1312:         } else {
 1313:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
 1314:                 &StudentPerformanceOnSequence($student,\%StudentsData,
 1315:                                               $seq,'no');
 1316:         } 
 1317:         if ($chosen_output->{'every_problem'}) {
 1318:             if ($chosen_output->{'correct'}) {
 1319:                 # only indiciate if each item is correct or not
 1320:                 foreach my $value (@$rawdata) {
 1321:                     # nonzero means correct
 1322:                     $value = 1 if ($value > 0);
 1323:                     $excel_sheet->write($rows_output,$cols_output++,$value);
 1324:                 }
 1325:             } else {
 1326:                 foreach my $value (@$rawdata) {
 1327:                     if ($score eq ' ' || !defined($value)) {
 1328:                         $cols_output++;
 1329:                     } else {                        
 1330:                         $excel_sheet->write($rows_output,$cols_output++,
 1331:                                             $value);
 1332:                     }
 1333:                 }
 1334:             }
 1335:         }
 1336:         if ($chosen_output->{'sequence_sum'} && 
 1337:             $chosen_output->{'every_problem'}) {
 1338:             # Write a formula for the sum of this sequence
 1339:             my %replaceCells;
 1340:             $replaceCells{$seq->{'Excel:startcell'}} = 
 1341:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1342:                             ($rows_output,$seq->{'Excel:startcol'});
 1343:             $replaceCells{$seq->{'Excel:endcell'}} = 
 1344:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
 1345:                             ($rows_output,$seq->{'Excel:endcol'});
 1346:             # The undef is for the format
 1347:             if (scalar(keys(%replaceCells)) == 1) {
 1348:                 $excel_sheet->repeat_formula($rows_output,$cols_output++,
 1349:                                              $seq->{'Excel:sum'},undef,
 1350:                                              %replaceCells,%replaceCells);
 1351:             } else {
 1352:                 $excel_sheet->repeat_formula($rows_output,$cols_output++,
 1353:                                              $seq->{'Excel:sum'},undef,
 1354:                                              %replaceCells);
 1355:             }
 1356:         } elsif ($chosen_output->{'sequence_sum'}) {
 1357:             if ($score eq ' ') {
 1358:                 $cols_output++;
 1359:             } else {
 1360:                 $excel_sheet->write($rows_output,$cols_output++,$score);
 1361:             }
 1362:         }
 1363:         if ($chosen_output->{'sequence_max'}) {
 1364:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
 1365:         }
 1366:     }
 1367:     #
 1368:     if ($chosen_output->{'grand_total'}) {
 1369:         $excel_sheet->repeat_formula($rows_output,$cols_output++,
 1370:                                      $total_formula,undef,
 1371:                                      %total_cell_translation);
 1372:     }
 1373:     #
 1374:     # Bookkeeping
 1375:     $rows_output++; 
 1376:     $cols_output=0;
 1377:     #
 1378:     # Update the progress window
 1379:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
 1380:     return;
 1381: }
 1382: 
 1383: sub excel_finish {
 1384:     my ($r) = @_;
 1385:     return if ($request_aborted);
 1386:     return if (! defined($excel_sheet));
 1387:     #
 1388:     # Write the excel file
 1389:     $excel_workbook->close();
 1390:     my $c = $r->connection();
 1391:     #
 1392:     return if($c->aborted());
 1393:     #
 1394:     # Close the progress window
 1395:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1396:     #
 1397:     # Tell the user where to get their excel file
 1398:     $r->print('<br />'.
 1399:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
 1400:     $r->rflush();
 1401:     return;
 1402: }
 1403: 
 1404: }
 1405: #######################################################
 1406: #######################################################
 1407: 
 1408: =pod
 1409: 
 1410: =head2 CSV output routines
 1411: 
 1412: =item &csv_initialize($r)
 1413: 
 1414: =item &csv_outputstudent($r,$student)
 1415: 
 1416: =item &csv_finish($r)
 1417: 
 1418: =cut
 1419: 
 1420: #######################################################
 1421: #######################################################
 1422: {
 1423: 
 1424: my $outputfile;
 1425: my $filename;
 1426: my $request_aborted;
 1427: my %prog_state; # progress window state
 1428: 
 1429: sub csv_initialize{
 1430:     my ($r) = @_;
 1431:     # 
 1432:     # Clean up
 1433:     undef($outputfile);
 1434:     undef($filename);
 1435:     undef($request_aborted);
 1436:     undef(%prog_state);
 1437:     #
 1438:     # Deal with unimplemented requests
 1439:     $request_aborted = undef;
 1440:     if ($chosen_output->{'base'} =~ /final table/) {
 1441:         $r->print(<<END);
 1442: <h2>Unable to Complete Request</h2>
 1443: <p>
 1444: The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
 1445: </p>
 1446: END
 1447:        $request_aborted = 1;
 1448:     }
 1449:     return if ($request_aborted);
 1450:     #
 1451:     # Initialize progress window
 1452:     my $studentcount = scalar(@Apache::lonstatistics::Students);
 1453:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
 1454:         ($r,'CSV File Compilation Status',
 1455:          'CSV File Compilation Progress', $studentcount);
 1456:     #
 1457:     # Open a file
 1458:     $filename = '/prtspool/'.
 1459:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1460:             time.'_'.rand(1000000000).'.csv';
 1461:     unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
 1462:         $r->log_error("Couldn't open $filename for output $!");
 1463:         $r->print("Problems occured in writing the csv file.  ".
 1464:                   "This error has been logged.  ".
 1465:                   "Please alert your LON-CAPA administrator.");
 1466:         $outputfile = undef;
 1467:     }
 1468:     #
 1469:     # Datestamp
 1470:     my $description = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1471:     print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
 1472:         '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
 1473:             "\n";
 1474:     #
 1475:     # Print out the headings
 1476:     my $sequence_row = '';
 1477:     my $resource_row = undef;
 1478:     foreach my $field (&get_student_fields_to_show()) {
 1479:         $sequence_row .='"",';
 1480:         $resource_row .= '"'.&Apache::loncommon::csv_translate($field).'",';
 1481:     }
 1482:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1483:         $sequence_row .= '"'.
 1484:             &Apache::loncommon::csv_translate($seq->{'title'}).'",';
 1485:         my $count = 0;
 1486:         if ($chosen_output->{'every_problem'}) {
 1487:             foreach my $res (@{$seq->{'contents'}}) {
 1488:                 if ($res->{'type'} ne 'assessment'  || 
 1489:                     ! exists($res->{'parts'})       ||
 1490:                     ref($res->{'parts'}) ne 'ARRAY' ||
 1491:                     scalar(@{$res->{'parts'}}) < 1) {
 1492:                     next;
 1493:                 }
 1494:                 foreach my $part (@{$res->{'parts'}}) {
 1495:                     $resource_row .= '"'.
 1496:                         &Apache::loncommon::csv_translate($res->{'title'}.
 1497:                                                           ', Part '.$part
 1498:                                                           ).'",';
 1499:                     $count++;
 1500:                 }
 1501:             }
 1502:         }
 1503:         $sequence_row.='"",'x$count;
 1504:         if ($chosen_output->{'sequence_sum'}) {
 1505:             if($chosen_output->{'correct'}) {
 1506:                 $resource_row .= '"sum",';
 1507:             } else {
 1508:                 $resource_row .= '"score",';
 1509:             }
 1510:         }
 1511:         if ($chosen_output->{'sequence_max'}) {
 1512:             $sequence_row.= '"",';
 1513:             $resource_row .= '"maximum possible",';
 1514:         }
 1515:     }
 1516:     if ($chosen_output->{'grand_total'}) {
 1517:         $sequence_row.= '"",';
 1518:         $resource_row.= '"Total",';
 1519:     } 
 1520:     chomp($sequence_row);
 1521:     chomp($resource_row);
 1522:     print $outputfile $sequence_row."\n";
 1523:     print $outputfile $resource_row."\n";
 1524:     return;
 1525: }
 1526: 
 1527: sub csv_outputstudent {
 1528:     my ($r,$student) = @_;
 1529:     return if ($request_aborted);
 1530:     return if (! defined($outputfile));
 1531:     my $Str = '';
 1532:     #
 1533:     # Output student fields
 1534:     my @to_show = &get_student_fields_to_show();
 1535:     foreach my $field (@to_show) {
 1536:         $Str .= '"'.&Apache::loncommon::csv_translate($student->{$field}).'",';
 1537:     }
 1538:     #
 1539:     # Get student assessment data
 1540:     my %StudentsData;
 1541:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
 1542:                                                         $student->{'domain'},
 1543:                                                         undef,
 1544:                                                    $ENV{'request.course.id'});
 1545:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
 1546:         %StudentsData = @tmp;
 1547:     }
 1548:     #
 1549:     # Output performance data
 1550:     my $total = 0;
 1551:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
 1552:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
 1553:         if ($chosen_output->{'tries'}){
 1554:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
 1555:                 &StudentTriesOnSequence($student,\%StudentsData,
 1556:                                         $seq,'no');
 1557:         } else {
 1558:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
 1559:                 &StudentPerformanceOnSequence($student,\%StudentsData,
 1560:                                               $seq,'no');
 1561:         }
 1562:         if ($chosen_output->{'every_problem'}) {
 1563:             if ($chosen_output->{'correct'}) {
 1564:                 $score = 0;
 1565:                 # Deal with number of parts correct data
 1566:                 $Str .= '"'.join('","',( map { if ($_>0) { 
 1567:                                                    $score += 1;
 1568:                                                    1; 
 1569:                                                } else { 
 1570:                                                    0; 
 1571:                                                }
 1572:                                              } @$rawdata)).'",';
 1573:             } else {
 1574:                 $Str .= '"'.join('","',(@$rawdata)).'",';
 1575:             }
 1576:         }
 1577:         if ($chosen_output->{'sequence_sum'}) {
 1578:             $Str .= '"'.$score.'",';
 1579:         } 
 1580:         if ($chosen_output->{'sequence_max'}) {
 1581:             $Str .= '"'.$seq_max.'",';
 1582:         }
 1583:         $total+=$score;
 1584:     }
 1585:     if ($chosen_output->{'grand_total'}) {
 1586:         $Str .= '"'.$total.'",';
 1587:     }
 1588:     chop($Str);
 1589:     $Str .= "\n";
 1590:     print $outputfile $Str;
 1591:     #
 1592:     # Update the progress window
 1593:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
 1594:     return;
 1595: }
 1596: 
 1597: sub csv_finish {
 1598:     my ($r) = @_;
 1599:     return if ($request_aborted);
 1600:     return if (! defined($outputfile));
 1601:     close($outputfile);
 1602:     #
 1603:     my $c = $r->connection();
 1604:     return if ($c->aborted());
 1605:     #
 1606:     # Close the progress window
 1607:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1608:     #
 1609:     # Tell the user where to get their csv file
 1610:     $r->print('<br />'.
 1611:               '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
 1612:     $r->rflush();
 1613:     return;
 1614:     
 1615: }
 1616: 
 1617: }
 1618: 
 1619: #######################################################
 1620: #######################################################
 1621: 
 1622: =pod
 1623: 
 1624: =item &StudentTriesOnSequence()
 1625: 
 1626: Inputs:
 1627: 
 1628: =over 4
 1629: 
 1630: =item $student
 1631: 
 1632: =item $studentdata Hash ref to all student data
 1633: 
 1634: =item $seq Hash ref, the sequence we are working on
 1635: 
 1636: =item $links if defined we will output links to each resource.
 1637: 
 1638: =back
 1639: 
 1640: =cut
 1641: 
 1642: #######################################################
 1643: #######################################################
 1644: sub StudentTriesOnSequence {
 1645:     my ($student,$studentdata,$seq,$links) = @_;
 1646:     $links = 'no' if (! defined($links));
 1647:     my $Str = '';
 1648:     my ($sum,$max) = (0,0);
 1649:     my $performance_length = 0;
 1650:     my @TriesData = ();
 1651:     my $tries;
 1652:     my $hasdata = 0; # flag - true if the student has any data on the sequence
 1653:     foreach my $resource (@{$seq->{'contents'}}) {
 1654:         next if ($resource->{'type'} ne 'assessment');
 1655:         my $resource_data = $studentdata->{$resource->{'symb'}};
 1656:         my $value = '';
 1657:         foreach my $partnum (@{$resource->{'parts'}}) {
 1658:             $tries = undef;
 1659:             $max++;
 1660:             $performance_length++;
 1661:             my $symbol = ' '; # default to space
 1662:             #
 1663:             my $awarded = 0;
 1664:             if (exists($resource_data->{'resource.'.$partnum.'.awarded'})) {
 1665:                 $awarded = $resource_data->{'resource.'.$partnum.'.awarded'};
 1666:                 $awarded = 0 if (! $awarded);
 1667:             }
 1668:             #
 1669:             my $status = '';
 1670:             if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
 1671:                 $status = $resource_data->{'resource.'.$partnum.'.solved'};
 1672:             }
 1673:             #
 1674:             my $tries = 0;
 1675:             if(exists($resource_data->{'resource.'.$partnum.'.tries'})) {
 1676:                 $tries = $resource_data->{'resource.'.$partnum.'.tries'};
 1677:                 $hasdata =1;
 1678:             }
 1679:             #
 1680:             if ($awarded > 0) {
 1681:                 # The student has gotten the problem correct to some degree
 1682:                 if ($status eq 'excused') {
 1683:                     $symbol = 'x';
 1684:                     $max--;
 1685:                 } elsif ($status eq 'correct_by_override') {
 1686:                     $symbol = '+';
 1687:                     $sum++;
 1688:                 } elsif ($tries > 0) {
 1689:                     if ($tries > 9) {
 1690:                         $symbol = '*';
 1691:                     } else {
 1692:                         $symbol = $tries;
 1693:                     }
 1694:                     $sum++;
 1695:                 } else {
 1696:                     $symbol = '+';
 1697:                     $sum++;
 1698:                 }
 1699:             } else {
 1700:                 # The student has the problem incorrect or it is ungraded
 1701:                 if ($status eq 'excused') {
 1702:                     $symbol = 'x';
 1703:                     $max--;
 1704:                 } elsif ($status eq 'incorrect_by_override') {
 1705:                     $symbol = '-';
 1706:                 } elsif ($status eq 'ungraded_attempted') {
 1707:                     $symbol = '#';
 1708:                 } elsif ($status eq 'incorrect_attempted' ||
 1709:                          $tries > 0)  {
 1710:                     $symbol = '.';
 1711:                 } else {
 1712:                     # Problem is wrong and has not been attempted.
 1713:                     $symbol=' ';
 1714:                 }
 1715:             }
 1716:             #
 1717:             if (! defined($tries)) {
 1718:                 $tries = $symbol;
 1719:             }
 1720:             push (@TriesData,$tries);
 1721:             #
 1722:             if ( ($links eq 'yes' && $symbol ne ' ') ||
 1723:                  ($links eq 'all')) {
 1724:                 if (length($symbol) > 1) {
 1725:                     &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
 1726:                 }
 1727:                 $symbol = '<a href="/adm/grades'.
 1728:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
 1729:                         '&student='.$student->{'username'}.
 1730:                             '&userdom='.$student->{'domain'}.
 1731:                                 '&command=submission">'.$symbol.'</a>';
 1732:             }
 1733:             $value .= $symbol;
 1734:         }
 1735:         $Str .= $value;
 1736:     }
 1737:     if ($seq->{'randompick'}) {
 1738:         $max = $seq->{'randompick'};
 1739:     }
 1740:     if (! $hasdata && $sum == 0) {
 1741:         $sum = ' ';
 1742:     }
 1743:     return ($Str,$performance_length,$sum,$max,\@TriesData);
 1744: }
 1745: 
 1746: #######################################################
 1747: #######################################################
 1748: 
 1749: =pod
 1750: 
 1751: =item &StudentPerformanceOnSequence()
 1752: 
 1753: Inputs:
 1754: 
 1755: =over 4
 1756: 
 1757: =item $student
 1758: 
 1759: =item $studentdata Hash ref to all student data
 1760: 
 1761: =item $seq Hash ref, the sequence we are working on
 1762: 
 1763: =item $links if defined we will output links to each resource.
 1764: 
 1765: =back
 1766: 
 1767: =cut
 1768: 
 1769: #######################################################
 1770: #######################################################
 1771: sub StudentPerformanceOnSequence {
 1772:     my ($student,$studentdata,$seq,$links) = @_;
 1773:     $links = 'no' if (! defined($links));
 1774:     my $Str = ''; # final result string
 1775:     my ($score,$max) = (0,0);
 1776:     my $performance_length = 0;
 1777:     my $symbol;
 1778:     my @ScoreData = ();
 1779:     my $partscore;
 1780:     my $hasdata = 0; # flag, 0 if there were no submissions on the sequence
 1781:     foreach my $resource (@{$seq->{'contents'}}) {
 1782:         next if ($resource->{'type'} ne 'assessment');
 1783:         my $resource_data = $studentdata->{$resource->{'symb'}};
 1784:         foreach my $part (@{$resource->{'parts'}}) {
 1785:             $partscore = undef;
 1786:             my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
 1787:                                               $resource->{'symb'},
 1788:                                               $student->{'domain'},
 1789:                                               $student->{'username'},
 1790:                                               $student->{'section'});
 1791:             if (!defined($weight) || ($weight eq '')) { 
 1792:                 $weight=1;
 1793:             }
 1794:             #
 1795:             $max += $weight; # see the 'excused' branch below...
 1796:             $performance_length++; # one character per part
 1797:             $symbol = ' '; # default to space
 1798:             #
 1799:             my $awarded;
 1800:             if (exists($resource_data->{'resource.'.$part.'.awarded'})) {
 1801:                 $awarded = $resource_data->{'resource.'.$part.'.awarded'};
 1802:                 $awarded = 0 if (! $awarded);
 1803:                 $hasdata = 1;
 1804:             }
 1805:             #
 1806:             $partscore = $weight*$awarded;
 1807:             if (! defined($awarded)) {
 1808:                 $partscore = undef;
 1809:             }
 1810:             $score += $partscore;
 1811:             $symbol = $partscore; 
 1812:             if (abs($symbol - sprintf("%.0f",$symbol)) < 0.001) {
 1813:                 $symbol = sprintf("%.0f",$symbol);
 1814:             }
 1815:             if (length($symbol) > 1) {
 1816:                 $symbol = '*';
 1817:             }
 1818:             if (exists($resource_data->{'resource.'.$part.'.solved'})) {
 1819:                 my $status = $resource_data->{'resource.'.$part.'.solved'};
 1820:                 if ($status eq 'excused') {
 1821:                     $symbol = 'x';
 1822:                     $max -= $weight; # Do not count 'excused' problems.
 1823:                 }
 1824:                 $hasdata = 1;
 1825:             } else {
 1826:                 # Unsolved.  Did they try?
 1827:                 if (exists($resource_data->{'resource.'.$part.'.tries'})){
 1828:                     $symbol = '.';
 1829:                     $hasdata = 1;
 1830:                 } else {
 1831:                     $symbol = ' ';
 1832:                 }
 1833:             }
 1834:             #
 1835:             if (! defined($partscore)) {
 1836:                 $partscore = $symbol;
 1837:             }
 1838:             push (@ScoreData,$partscore);
 1839:             #
 1840:             if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) {
 1841:                 $symbol = '<a href="/adm/grades'.
 1842:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
 1843:                     '&student='.$student->{'username'}.
 1844:                     '&userdom='.$student->{'domain'}.
 1845:                     '&command=submission">'.$symbol.'</a>';
 1846:             }
 1847:             $Str .= $symbol;
 1848:         }
 1849:     }
 1850:     if (! $hasdata && $score == 0) {
 1851:         $score = ' ';
 1852:     }
 1853:     return ($Str,$performance_length,$score,$max,\@ScoreData);
 1854: }
 1855: 
 1856: #######################################################
 1857: #######################################################
 1858: 
 1859: =pod
 1860: 
 1861: =item &CreateLegend()
 1862: 
 1863: This function returns a formatted string containing the legend for the
 1864: chart.  The legend describes the symbols used to represent grades for
 1865: problems.
 1866: 
 1867: =cut
 1868: 
 1869: #######################################################
 1870: #######################################################
 1871: sub CreateLegend {
 1872:     my $Str = "<p><pre>".
 1873:               "   1  correct by student in 1 try\n".
 1874:               "   7  correct by student in 7 tries\n".
 1875:               "   *  correct by student in more than 9 tries\n".
 1876: 	      "   +  correct by hand grading or override\n".
 1877:               "   -  incorrect by override\n".
 1878: 	      "   .  incorrect attempted\n".
 1879: 	      "   #  ungraded attempted\n".
 1880:               "      not attempted (blank field)\n".
 1881: 	      "   x  excused".
 1882:               "</pre><p>";
 1883:     return $Str;
 1884: }
 1885: 
 1886: #######################################################
 1887: #######################################################
 1888: 
 1889: =pod 
 1890: 
 1891: =back
 1892: 
 1893: =cut
 1894: 
 1895: #######################################################
 1896: #######################################################
 1897: 
 1898: 1;
 1899: 
 1900: __END__

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