File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.169.2.6: download - view: text, annotated - select for diffs
Sat Apr 5 13:03:06 2014 UTC (10 years, 1 month ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0
- For 2.11
  - Backport 1.174

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

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