File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.74: download - view: text, annotated - select for diffs
Tue Nov 11 22:14:28 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Use the new &Apache::loncommon::store_course_settings and
restore_course_settings subroutines to remember form settings.
lonstatistics.pm was changed to not parse the student enrollment status
form elements before calling the sub-module routines which present the
interface.  Now each of the sub-modules (statistics/*) must call
&Apache::lonstatistics::PrepareClasslist(); at the right time.

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

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