File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.85: download - view: text, annotated - select for diffs
Wed Feb 11 16:02:37 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Improve spreadsheet output by adding formats and being more explicit about
where columns are (so errors will be less likely to cascade throughout the
sheet).

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

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