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

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

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