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

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

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