File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.89: download - view: text, annotated - select for diffs
Thu Feb 12 16:29:00 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Moved chart-specific code from lonstatistics to lonstudentassessment.
Reworked the width computation of chart to deal with the new output structure
and to fix Bug 2192 (titles too long in chart).

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

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