File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.144: download - view: text, annotated - select for diffs
Thu Dec 21 02:53:42 2006 UTC (17 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, HEAD
- when errors occur getting data for chrt display it

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

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