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

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

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