File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.159: download - view: text, annotated - select for diffs
Thu Jan 14 17:20:51 2010 UTC (14 years, 5 months ago) by bisitz
Branches: MAIN
CVS tags: bz6209-base, bz6209, HEAD
Consistent screen ergonomics:
- First checkbox, then option text
- Removed special styles

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

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