File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.100: download - view: text, annotated - select for diffs
Thu Apr 1 18:49:39 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Bug 2895: Sorting the classlist lead to a blank page.  Should go in
version_1_1_X.

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

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