File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.169.2.7.2.2: download - view: text, annotated - select for diffs
Fri Sep 8 00:56:04 2023 UTC (9 months, 4 weeks ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.169.2.7: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.175

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

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