File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.119: download - view: text, annotated - select for diffs
Mon Mar 14 20:28:22 2005 UTC (19 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
lonstatistics.pm:Removed package variable @SelectedSections.
    Added &get_selected_sections
    Added &section_and_enrollment_description to provide a brief
        description of those students selected for statistics work
        (hopefully this descriptive line will make it in to bug reports).
Added &section_and_enrollment_description to all statistics pages
Replaced use of @SelectedSections with &get_selected_sections in statistics
    pages.

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

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