Annotation of loncom/interface/statistics/lonstudentassessment.pm, revision 1.98

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

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