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

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

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