File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.177: download - view: text, annotated - select for diffs
Fri Apr 7 16:46:44 2023 UTC (14 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bug 5071
  - Align " / maximum" in total (rightmost) column for students with and without
    scores for selected folder(s).
  - Use 2 decimal places for maximum for consistency with total scores.

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

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