File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.108: download - view: text, annotated - select for diffs
Wed Jan 5 20:01:46 2005 UTC (19 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Chart now uses inline status display instead of opening a window.

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

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