File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.165: download - view: text, annotated - select for diffs
Wed Dec 21 21:25:51 2011 UTC (12 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug 6455 - Progress Indicator now jQuery. Still need to test across browsers.

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

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