File:  [LON-CAPA] / loncom / interface / statistics / lonstudentassessment.pm
Revision 1.71: download - view: text, annotated - select for diffs
Tue Oct 21 21:23:53 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Simplify user interface.  After chatting with Felicia the following
changes have been made to the 'Output Data' selection:

'Scores' has been renamed 'Scores Per Problem'
'Scores Sum and Maximum' has been renamed 'Scores'
'Scores Sum' has been removed.
'Scores Summary Table Only' has been removed.
'Tries' remains the same
'Parts Correct and Maximums' has been renamed 'Parts Correct'
'Parts Correct' has been removed.
'Parts Summary Table Only' has been removed.

Removed means access to that option has been disabled.  Can be re-enabled
without trouble in response to the inevitable outcry.

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

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