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

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.90    ! matthew     3: # $Id: lonstudentassessment.pm,v 1.89 2004/02/12 16:29:00 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:     #
                    973:     # Add a worksheet
1.33      matthew   974:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.67      matthew   975:     $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
1.33      matthew   976:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
1.32      matthew   977:     #
1.85      matthew   978:     # Define some potentially useful formats
                    979:     my $format;
                    980:     $format->{'header'} = $excel_workbook->add_format(bold      => 1, 
                    981:                                                       bottom    => 1,
                    982:                                                       align     => 'center');
                    983:     $format->{'bold'} = $excel_workbook->add_format(bold=>1);
                    984:     $format->{'h1'}   = $excel_workbook->add_format(bold=>1, size=>18);
                    985:     $format->{'h2'}   = $excel_workbook->add_format(bold=>1, size=>16);
                    986:     $format->{'h3'}   = $excel_workbook->add_format(bold=>1, size=>14);
                    987:     $format->{'date'} = $excel_workbook->add_format(num_format=>
                    988:                                                     'mmm d yyyy hh:mm AM/PM');
                    989:     #
1.34      matthew   990:     # Put the course description in the header
1.90    ! matthew   991:     $excel_sheet->write($header_row,$cols_output++,
1.85      matthew   992:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
                    993:                         $format->{'h1'});
1.34      matthew   994:     $cols_output += 3;
                    995:     #
                    996:     # Put a description of the sections listed
                    997:     my $sectionstring = '';
                    998:     my @Sections = @Apache::lonstatistics::SelectedSections;
                    999:     if (scalar(@Sections) > 1) {
                   1000:         if (scalar(@Sections) > 2) {
                   1001:             my $last = pop(@Sections);
                   1002:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
                   1003:         } else {
                   1004:             $sectionstring = "Sections ".join(' and ',@Sections);
                   1005:         }
                   1006:     } else {
                   1007:         if ($Sections[0] eq 'all') {
                   1008:             $sectionstring = "All sections";
                   1009:         } else {
                   1010:             $sectionstring = "Section ".$Sections[0];
                   1011:         }
                   1012:     }
1.90    ! matthew  1013:     $excel_sheet->write($header_row,$cols_output++,$sectionstring,
1.85      matthew  1014:                         $format->{'h3'});
1.34      matthew  1015:     $cols_output += scalar(@Sections);
                   1016:     #
                   1017:     # Put the date in there too
1.90    ! matthew  1018:     $excel_sheet->write($header_row,$cols_output++,
1.85      matthew  1019:                         'Compiled on '.localtime(time),$format->{'h3'});
1.34      matthew  1020:     #
1.54      matthew  1021:     $cols_output = 0;
1.90    ! matthew  1022:     $excel_sheet->write($description_row,$cols_output++,
1.87      matthew  1023:                         $chosen_output->{'shortdesc'},
1.85      matthew  1024:                         $format->{'h3'});
1.90    ! matthew  1025:     ##############################################
        !          1026:     # Output headings for the raw data
        !          1027:     ##############################################
1.86      matthew  1028:     #
1.32      matthew  1029:     # Add the student headers
1.34      matthew  1030:     $cols_output = 0;
1.32      matthew  1031:     foreach my $field (&get_student_fields_to_show()) {
1.86      matthew  1032:         $excel_sheet->write($resource_name_row,$cols_output++,$field,
1.85      matthew  1033:                             $format->{'bold'});
1.32      matthew  1034:     }
                   1035:     #
1.54      matthew  1036:     # Add the remaining column headers
1.76      matthew  1037:     my $total_formula_string = '=0';
1.40      matthew  1038:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.86      matthew  1039:         $excel_sheet->write($sequence_name_row,,
1.90    ! matthew  1040:                             $cols_output,$seq->{'title'},$format->{'bold'});
1.86      matthew  1041:         # Determine starting cell
                   1042:         $seq->{'Excel:startcell'}=
                   1043:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1.90    ! matthew  1044:             ($first_data_row,$cols_output);
1.86      matthew  1045:         $seq->{'Excel:startcol'}=$cols_output;
1.87      matthew  1046:         my $count = 0;
                   1047:         if ($chosen_output->{'every_problem'}) {
1.76      matthew  1048:             # Put the names of the problems and parts into the sheet
1.54      matthew  1049:             foreach my $res (@{$seq->{'contents'}}) {
1.84      matthew  1050:                 if ($res->{'type'} ne 'assessment'  || 
                   1051:                     ! exists($res->{'parts'})       ||
                   1052:                     ref($res->{'parts'}) ne 'ARRAY' ||
                   1053:                     scalar(@{$res->{'parts'}}) < 1) {
                   1054:                     next;
                   1055:                 }
1.54      matthew  1056:                 if (scalar(@{$res->{'parts'}}) > 1) {
                   1057:                     foreach my $part (@{$res->{'parts'}}) {
1.86      matthew  1058:                         $excel_sheet->write($resource_name_row,
1.54      matthew  1059:                                             $cols_output++,
1.85      matthew  1060:                                             $res->{'title'}.' part '.$part,
                   1061:                                             $format->{'bold'});
1.54      matthew  1062:                     }
                   1063:                 } else {
1.86      matthew  1064:                     $excel_sheet->write($resource_name_row,
1.54      matthew  1065:                                         $cols_output++,
1.85      matthew  1066:                                         $res->{'title'},$format->{'bold'});
1.54      matthew  1067:                 }
1.82      matthew  1068:                 $count++;
1.54      matthew  1069:             }
1.79      matthew  1070:         }
1.87      matthew  1071:         # Determine ending cell
                   1072:         if ($count <= 1) {
                   1073:             $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
                   1074:             $seq->{'Excel:endcol'}  = $seq->{'Excel:startcol'};
                   1075:         } else {
                   1076:             $seq->{'Excel:endcell'} = 
                   1077:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1.90    ! matthew  1078:                 ($first_data_row,$cols_output-1);
1.87      matthew  1079:             $seq->{'Excel:endcol'} = $cols_output-1;
                   1080:         }
                   1081:         # Create the formula for summing up this sequence
                   1082:         if (! exists($seq->{'Excel:endcell'}) ||
                   1083:             ! defined($seq->{'Excel:endcell'})) {
                   1084:             $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
                   1085:         }
                   1086:         $seq->{'Excel:sum'}= $excel_sheet->store_formula
                   1087:             ('=SUM('.$seq->{'Excel:startcell'}.
                   1088:              ':'.$seq->{'Excel:endcell'}.')');
1.79      matthew  1089:         # Determine cell the score is held in
                   1090:         $seq->{'Excel:scorecell'} = 
                   1091:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1.90    ! matthew  1092:             ($first_data_row,$cols_output);
1.79      matthew  1093:         $seq->{'Excel:scorecol'}=$cols_output;
1.87      matthew  1094:         if ($chosen_output->{'base'} eq 'parts correct total') {
1.86      matthew  1095:             $excel_sheet->write($resource_name_row,$cols_output++,
                   1096:                                 'parts correct',
1.85      matthew  1097:                                 $format->{'bold'});
1.87      matthew  1098:         } elsif ($chosen_output->{'sequence_sum'}) {
                   1099:             if ($chosen_output->{'correct'}) {
                   1100:                 # Only reporting the number correct, so do not call it score
                   1101:                 $excel_sheet->write($resource_name_row,$cols_output++,
                   1102:                                     'sum',
                   1103:                                     $format->{'bold'});
                   1104:             } else {
                   1105:                 $excel_sheet->write($resource_name_row,$cols_output++,
                   1106:                                     'score',
                   1107:                                     $format->{'bold'});
                   1108:             }
1.32      matthew  1109:         }
1.79      matthew  1110:         #
                   1111:         $total_formula_string.='+'.
                   1112:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1.90    ! matthew  1113:             ($first_data_row,$cols_output-1);
1.87      matthew  1114:         if ($chosen_output->{'sequence_max'}) {
                   1115:             $excel_sheet->write($resource_name_row,$cols_output++,
                   1116:                                 'maximum',
                   1117:                                 $format->{'bold'});
                   1118:         }
                   1119:     }
                   1120:     if ($chosen_output->{'grand_total'}) {
                   1121:         $excel_sheet->write($resource_name_row,$cols_output++,'Total',
1.85      matthew  1122:                             $format->{'bold'});
1.32      matthew  1123:     }
1.76      matthew  1124:     $total_formula = $excel_sheet->store_formula($total_formula_string);
1.90    ! matthew  1125:     ##############################################
1.87      matthew  1126:     # Output a row for MAX, if appropriate
1.90    ! matthew  1127:     ##############################################
        !          1128:     if ($chosen_output->{'maximum_row'}) {
1.87      matthew  1129:         $cols_output = 0;
                   1130:         foreach my $field (&get_student_fields_to_show()) {
                   1131:             if ($field eq 'username' || $field eq 'fullname' || 
                   1132:                 $field eq 'id') {
                   1133:                 $excel_sheet->write($maximum_data_row,$cols_output++,'Maximum',
                   1134:                                     $format->{'bold'});
                   1135:             } else {
                   1136:                 $excel_sheet->write($maximum_data_row,$cols_output++,'');
                   1137:             }
1.54      matthew  1138:         }
1.87      matthew  1139:         #
                   1140:         # Add the maximums for each sequence or assessment
                   1141:         my %total_cell_translation;
                   1142:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
                   1143:             $cols_output=$seq->{'Excel:startcol'};
                   1144:             $total_cell_translation{$seq->{'Excel:scorecell'}} = 
                   1145:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                   1146:                 ($maximum_data_row,$seq->{'Excel:scorecol'});
                   1147:             my $weight;
                   1148:             my $max = 0;
                   1149:             foreach my $resource (@{$seq->{'contents'}}) {
                   1150:                 next if ($resource->{'type'} ne 'assessment');
                   1151:                 foreach my $part (@{$resource->{'parts'}}) {
                   1152:                     $weight = 1;
                   1153:                     if ($chosen_output->{'scores'}) {
                   1154:                         $weight = &Apache::lonnet::EXT
                   1155:                             ('resource.'.$part.'.weight',$resource->{'symb'},
                   1156:                              undef,undef,undef);
                   1157:                         if (!defined($weight) || ($weight eq '')) { 
                   1158:                             $weight=1;
                   1159:                         }
                   1160:                     }
                   1161:                     if ($chosen_output->{'scores'} &&
                   1162:                         $chosen_output->{'every_problem'}) {
                   1163:                         $excel_sheet->write($maximum_data_row,$cols_output++,
                   1164:                                             $weight);
1.54      matthew  1165:                     }
1.87      matthew  1166:                     $max += $weight;
1.54      matthew  1167:                 }
1.87      matthew  1168:             } 
                   1169:             #
                   1170:             if ($chosen_output->{'sequence_sum'} && 
                   1171:                 $chosen_output->{'every_problem'}) {
                   1172:                 my %replaceCells;
                   1173:                 $replaceCells{$seq->{'Excel:startcell'}} = 
                   1174:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                   1175:                     ($maximum_data_row,$seq->{'Excel:startcol'});
                   1176:                 $replaceCells{$seq->{'Excel:endcell'}} = 
                   1177:                     &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                   1178:                     ($maximum_data_row,$seq->{'Excel:endcol'});
                   1179:                 $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
                   1180:                                              $seq->{'Excel:sum'},undef,
                   1181:                                              %replaceCells);
                   1182:             } elsif ($chosen_output->{'sequence_sum'}) {
                   1183:                 $excel_sheet->write($maximum_data_row,$cols_output++,$max);
                   1184:             }
                   1185:             if ($chosen_output->{'sequence_max'}) {
                   1186:                 $excel_sheet->write($maximum_data_row,$cols_output++,$max);
1.45      matthew  1187:             }
1.87      matthew  1188:             #
1.45      matthew  1189:         }
1.87      matthew  1190:         if ($chosen_output->{'grand_total'}) {
1.86      matthew  1191:             $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1.87      matthew  1192:                                          $total_formula,undef,
                   1193:                                          %total_cell_translation);
1.79      matthew  1194:         }
1.90    ! matthew  1195:     } # End of MAXIMUM row output  if ($chosen_output->{'maximum_row'}) {
1.86      matthew  1196:     $rows_output = $first_data_row;
1.90    ! matthew  1197:     ##############################################
        !          1198:     # Output summary table, which actually is above the sequence name row.
        !          1199:     ##############################################
        !          1200:     if ($chosen_output->{'summary_table'}) {
        !          1201:         $cols_output = 0;
        !          1202:         $excel_sheet->write($summary_header_row,$cols_output++,
        !          1203:                             'Summary Table',$format->{'bold'});
        !          1204:         if ($chosen_output->{'maximum_row'}) {
        !          1205:             $excel_sheet->write($summary_header_row,$cols_output++,
        !          1206:                                 'Maximum',$format->{'bold'});
        !          1207:         }
        !          1208:         $excel_sheet->write($summary_header_row,$cols_output++,
        !          1209:                             'Average',$format->{'bold'});
        !          1210:         $excel_sheet->write($summary_header_row,$cols_output++,
        !          1211:                             'Median',$format->{'bold'});
        !          1212:         $excel_sheet->write($summary_header_row,$cols_output++,
        !          1213:                             'Std Dev',$format->{'bold'});
        !          1214:         my $row = $summary_header_row+1;
        !          1215:         foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
        !          1216:             $cols_output = 0;
        !          1217:             $excel_sheet->write($row,$cols_output++,
        !          1218:                                 $seq->{'title'},
        !          1219:                                 $format->{'bold'});
        !          1220:             if ($chosen_output->{'maximum_row'}) {
        !          1221:                 $excel_sheet->write
        !          1222:                     ($row,$cols_output++,
        !          1223:                      '='.
        !          1224:                      &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
        !          1225:                      ($maximum_data_row,$seq->{'Excel:scorecol'})
        !          1226:                      );
        !          1227:             }
        !          1228:             my $range = 
        !          1229:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
        !          1230:                 ($first_data_row,$seq->{'Excel:scorecol'}).
        !          1231:                 ':'.
        !          1232:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
        !          1233:                 ($first_data_row+$num_students-1,$seq->{'Excel:scorecol'});
        !          1234:             $excel_sheet->write($row,$cols_output++,
        !          1235:                                 '=AVERAGE('.$range.')');
        !          1236:             $excel_sheet->write($row,$cols_output++,
        !          1237:                                 '=MEDIAN('.$range.')');
        !          1238:             $excel_sheet->write($row,$cols_output++,
        !          1239:                                 '=STDEV('.$range.')');
        !          1240:             $row++;
        !          1241:         }
        !          1242:     }
        !          1243:     ##############################################
        !          1244:     #   Take care of non-excel initialization
        !          1245:     ##############################################
1.32      matthew  1246:     #
                   1247:     # Let the user know what we are doing
                   1248:     my $studentcount = scalar(@Apache::lonstatistics::Students); 
1.85      matthew  1249:     if ($ENV{'form.SelectedStudent'}) {
                   1250:         $studentcount = '1';
                   1251:     }
                   1252:     if ($studentcount > 1) {
                   1253:         $r->print('<h1>'.&mt('Compiling Excel spreadsheet for [_1] students',
                   1254:                              $studentcount)."</h1>\n");
                   1255:     } else {
                   1256:         $r->print('<h1>'.
                   1257:                   &mt('Compiling Excel spreadsheet for 1 student').
                   1258:                   "</h1>\n");
                   1259:     }
1.32      matthew  1260:     $r->rflush();
1.31      matthew  1261:     #
1.36      matthew  1262:     # Initialize progress window
                   1263:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                   1264:         ($r,'Excel File Compilation Status',
                   1265:          'Excel File Compilation Progress', $studentcount);
                   1266:     #
1.62      matthew  1267:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
                   1268:                                           'Processing first student');
1.31      matthew  1269:     return;
                   1270: }
                   1271: 
                   1272: sub excel_outputstudent {
                   1273:     my ($r,$student) = @_;
1.54      matthew  1274:     return if ($request_aborted);
1.32      matthew  1275:     return if (! defined($excel_sheet));
                   1276:     $cols_output=0;
                   1277:     #
                   1278:     # Write out student data
                   1279:     my @to_show = &get_student_fields_to_show();
                   1280:     foreach my $field (@to_show) {
                   1281:         $excel_sheet->write($rows_output,$cols_output++,$student->{$field});
                   1282:     }
                   1283:     #
                   1284:     # Get student assessment data
                   1285:     my %StudentsData;
                   1286:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
                   1287:                                                         $student->{'domain'},
                   1288:                                                         undef,
                   1289:                                                    $ENV{'request.course.id'});
                   1290:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
                   1291:         %StudentsData = @tmp;
                   1292:     }
                   1293:     #
                   1294:     # Write out sequence scores and totals data
1.76      matthew  1295:     my %total_cell_translation;
1.40      matthew  1296:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.85      matthew  1297:         $cols_output = $seq->{'Excel:startcol'};
1.76      matthew  1298:         # Keep track of cells to translate in total cell
                   1299:         $total_cell_translation{$seq->{'Excel:scorecell'}} = 
                   1300:             &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                   1301:                         ($rows_output,$seq->{'Excel:scorecol'});
                   1302:         #
1.54      matthew  1303:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
1.87      matthew  1304:         if ($chosen_output->{'tries'} || $chosen_output->{'correct'}){
1.54      matthew  1305:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1306:                 &StudentTriesOnSequence($student,\%StudentsData,
                   1307:                                         $seq,'no');
                   1308:         } else {
                   1309:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1310:                 &StudentPerformanceOnSequence($student,\%StudentsData,
                   1311:                                               $seq,'no');
1.87      matthew  1312:         } 
                   1313:         if ($chosen_output->{'every_problem'}) {
                   1314:             if ($chosen_output->{'correct'}) {
                   1315:                 # only indiciate if each item is correct or not
                   1316:                 foreach my $value (@$rawdata) {
                   1317:                     # nonzero means correct
                   1318:                     $value = 1 if ($value > 0);
                   1319:                     $excel_sheet->write($rows_output,$cols_output++,$value);
                   1320:                 }
                   1321:             } else {
                   1322:                 foreach my $value (@$rawdata) {
                   1323:                     $excel_sheet->write($rows_output,$cols_output++,$value);
                   1324:                 }
                   1325:             }
1.54      matthew  1326:         }
1.87      matthew  1327:         if ($chosen_output->{'sequence_sum'} && 
                   1328:             $chosen_output->{'every_problem'}) {
1.76      matthew  1329:             # Write a formula for the sum of this sequence
                   1330:             my %replaceCells;
                   1331:             $replaceCells{$seq->{'Excel:startcell'}} = 
                   1332:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                   1333:                             ($rows_output,$seq->{'Excel:startcol'});
                   1334:             $replaceCells{$seq->{'Excel:endcell'}} = 
                   1335:                 &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
                   1336:                             ($rows_output,$seq->{'Excel:endcol'});
                   1337:             # The undef is for the format
1.81      matthew  1338:             if (scalar(keys(%replaceCells)) == 1) {
                   1339:                 $excel_sheet->repeat_formula($rows_output,$cols_output++,
                   1340:                                              $seq->{'Excel:sum'},undef,
                   1341:                                              %replaceCells,%replaceCells);
                   1342:             } else {
                   1343:                 $excel_sheet->repeat_formula($rows_output,$cols_output++,
                   1344:                                              $seq->{'Excel:sum'},undef,
                   1345:                                              %replaceCells);
                   1346:             }
1.87      matthew  1347:         } elsif ($chosen_output->{'sequence_sum'}) {
1.32      matthew  1348:             $excel_sheet->write($rows_output,$cols_output++,$score);
                   1349:         }
1.87      matthew  1350:         if ($chosen_output->{'sequence_max'}) {
1.32      matthew  1351:             $excel_sheet->write($rows_output,$cols_output++,$seq_max);
                   1352:         }
                   1353:     }
1.76      matthew  1354:     #
1.87      matthew  1355:     if ($chosen_output->{'grand_total'}) {
                   1356:         $excel_sheet->repeat_formula($rows_output,$cols_output++,
                   1357:                                      $total_formula,undef,
                   1358:                                      %total_cell_translation);
                   1359:     }
1.32      matthew  1360:     #
                   1361:     # Bookkeeping
                   1362:     $rows_output++; 
                   1363:     $cols_output=0;
                   1364:     #
1.36      matthew  1365:     # Update the progress window
                   1366:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1.32      matthew  1367:     return;
1.31      matthew  1368: }
                   1369: 
                   1370: sub excel_finish {
                   1371:     my ($r) = @_;
1.54      matthew  1372:     return if ($request_aborted);
1.32      matthew  1373:     return if (! defined($excel_sheet));
                   1374:     #
                   1375:     # Write the excel file
                   1376:     $excel_workbook->close();
                   1377:     my $c = $r->connection();
                   1378:     #
                   1379:     return if($c->aborted());
                   1380:     #
1.36      matthew  1381:     # Close the progress window
                   1382:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   1383:     #
1.32      matthew  1384:     # Tell the user where to get their excel file
1.36      matthew  1385:     $r->print('<br />'.
1.32      matthew  1386:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
                   1387:     $r->rflush();
                   1388:     return;
1.31      matthew  1389: }
                   1390: 
                   1391: }
1.30      matthew  1392: #######################################################
                   1393: #######################################################
                   1394: 
                   1395: =pod
                   1396: 
1.31      matthew  1397: =head2 CSV output routines
                   1398: 
                   1399: =item &csv_initialize($r)
                   1400: 
                   1401: =item &csv_outputstudent($r,$student)
                   1402: 
                   1403: =item &csv_finish($r)
1.30      matthew  1404: 
                   1405: =cut
                   1406: 
                   1407: #######################################################
                   1408: #######################################################
1.31      matthew  1409: {
                   1410: 
1.37      matthew  1411: my $outputfile;
                   1412: my $filename;
1.54      matthew  1413: my $request_aborted;
1.37      matthew  1414: my %prog_state; # progress window state
                   1415: 
1.31      matthew  1416: sub csv_initialize{
                   1417:     my ($r) = @_;
1.37      matthew  1418:     # 
                   1419:     # Clean up
1.66      matthew  1420:     undef($outputfile);
                   1421:     undef($filename);
                   1422:     undef($request_aborted);
1.37      matthew  1423:     undef(%prog_state);
                   1424:     #
1.54      matthew  1425:     # Deal with unimplemented requests
                   1426:     $request_aborted = undef;
1.87      matthew  1427:     if ($chosen_output->{'base'} =~ /final table/) {
1.54      matthew  1428:         $r->print(<<END);
                   1429: <h2>Unable to Complete Request</h2>
                   1430: <p>
                   1431: The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
                   1432: </p>
                   1433: END
                   1434:        $request_aborted = 1;
                   1435:     }
                   1436:     return if ($request_aborted);
1.87      matthew  1437:     #
                   1438:     # Initialize progress window
                   1439:     my $studentcount = scalar(@Apache::lonstatistics::Students);
                   1440:     %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                   1441:         ($r,'CSV File Compilation Status',
                   1442:          'CSV File Compilation Progress', $studentcount);
1.54      matthew  1443:     #
1.37      matthew  1444:     # Open a file
                   1445:     $filename = '/prtspool/'.
                   1446:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                   1447:             time.'_'.rand(1000000000).'.csv';
                   1448:     unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
                   1449:         $r->log_error("Couldn't open $filename for output $!");
                   1450:         $r->print("Problems occured in writing the csv file.  ".
                   1451:                   "This error has been logged.  ".
                   1452:                   "Please alert your LON-CAPA administrator.");
                   1453:         $outputfile = undef;
                   1454:     }
1.38      matthew  1455:     #
                   1456:     # Datestamp
                   1457:     my $description = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                   1458:     print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
                   1459:         '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
                   1460:             "\n";
1.37      matthew  1461:     #
                   1462:     # Print out the headings
1.87      matthew  1463:     my $sequence_row = '';
                   1464:     my $resource_row = undef;
1.37      matthew  1465:     foreach my $field (&get_student_fields_to_show()) {
1.87      matthew  1466:         $sequence_row .='"",';
                   1467:         $resource_row .= '"'.&Apache::loncommon::csv_translate($field).'",';
1.37      matthew  1468:     }
1.40      matthew  1469:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.87      matthew  1470:         $sequence_row .= '"'.
                   1471:             &Apache::loncommon::csv_translate($seq->{'title'}).'",';
                   1472:         my $count = 0;
                   1473:         if ($chosen_output->{'every_problem'}) {
1.53      matthew  1474:             foreach my $res (@{$seq->{'contents'}}) {
1.87      matthew  1475:                 if ($res->{'type'} ne 'assessment'  || 
                   1476:                     ! exists($res->{'parts'})       ||
                   1477:                     ref($res->{'parts'}) ne 'ARRAY' ||
                   1478:                     scalar(@{$res->{'parts'}}) < 1) {
                   1479:                     next;
                   1480:                 }
1.53      matthew  1481:                 foreach my $part (@{$res->{'parts'}}) {
1.87      matthew  1482:                     $resource_row .= '"'.
                   1483:                         &Apache::loncommon::csv_translate($res->{'title'}.
                   1484:                                                           ', Part '.$part
                   1485:                                                           ).'",';
                   1486:                     $count++;
1.53      matthew  1487:                 }
                   1488:             }
1.37      matthew  1489:         }
1.87      matthew  1490:         $sequence_row.='"",'x$count;
                   1491:         if ($chosen_output->{'sequence_sum'}) {
                   1492:             if($chosen_output->{'correct'}) {
                   1493:                 $resource_row .= '"sum",';
                   1494:             } else {
                   1495:                 $resource_row .= '"score",';
                   1496:             }
                   1497:         }
                   1498:         if ($chosen_output->{'sequence_max'}) {
                   1499:             $sequence_row.= '"",';
                   1500:             $resource_row .= '"maximum possible",';
                   1501:         }
1.37      matthew  1502:     }
1.87      matthew  1503:     if ($chosen_output->{'grand_total'}) {
                   1504:         $sequence_row.= '"",';
                   1505:         $resource_row.= '"Total",';
                   1506:     } 
                   1507:     chomp($sequence_row);
                   1508:     chomp($resource_row);
                   1509:     print $outputfile $sequence_row."\n";
                   1510:     print $outputfile $resource_row."\n";
1.31      matthew  1511:     return;
                   1512: }
                   1513: 
                   1514: sub csv_outputstudent {
                   1515:     my ($r,$student) = @_;
1.54      matthew  1516:     return if ($request_aborted);
1.37      matthew  1517:     return if (! defined($outputfile));
                   1518:     my $Str = '';
                   1519:     #
                   1520:     # Output student fields
                   1521:     my @to_show = &get_student_fields_to_show();
                   1522:     foreach my $field (@to_show) {
                   1523:         $Str .= '"'.&Apache::loncommon::csv_translate($student->{$field}).'",';
                   1524:     }
                   1525:     #
                   1526:     # Get student assessment data
                   1527:     my %StudentsData;
                   1528:     my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
                   1529:                                                         $student->{'domain'},
                   1530:                                                         undef,
                   1531:                                                    $ENV{'request.course.id'});
                   1532:     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
                   1533:         %StudentsData = @tmp;
                   1534:     }
                   1535:     #
                   1536:     # Output performance data
1.87      matthew  1537:     my $total = 0;
1.40      matthew  1538:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1.54      matthew  1539:         my ($performance,$performance_length,$score,$seq_max,$rawdata);
1.87      matthew  1540:         if ($chosen_output->{'tries'}){
1.54      matthew  1541:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1542:                 &StudentTriesOnSequence($student,\%StudentsData,
                   1543:                                         $seq,'no');
                   1544:         } else {
                   1545:             ($performance,$performance_length,$score,$seq_max,$rawdata) =
                   1546:                 &StudentPerformanceOnSequence($student,\%StudentsData,
                   1547:                                               $seq,'no');
                   1548:         }
1.87      matthew  1549:         if ($chosen_output->{'every_problem'}) {
                   1550:             if ($chosen_output->{'correct'}) {
                   1551:                 $score = 0;
                   1552:                 # Deal with number of parts correct data
                   1553:                 $Str .= '"'.join('","',( map { if ($_>0) { 
                   1554:                                                    $score += 1;
                   1555:                                                    1; 
                   1556:                                                } else { 
                   1557:                                                    0; 
                   1558:                                                }
                   1559:                                              } @$rawdata)).'",';
                   1560:             } else {
                   1561:                 $Str .= '"'.join('","',(@$rawdata)).'",';
                   1562:             }
                   1563:         }
                   1564:         if ($chosen_output->{'sequence_sum'}) {
1.37      matthew  1565:             $Str .= '"'.$score.'",';
1.87      matthew  1566:         } 
                   1567:         if ($chosen_output->{'sequence_max'}) {
                   1568:             $Str .= '"'.$seq_max.'",';
1.37      matthew  1569:         }
1.87      matthew  1570:         $total+=$score;
                   1571:     }
                   1572:     if ($chosen_output->{'grand_total'}) {
                   1573:         $Str .= '"'.$total.'",';
1.37      matthew  1574:     }
                   1575:     chop($Str);
                   1576:     $Str .= "\n";
                   1577:     print $outputfile $Str;
                   1578:     #
                   1579:     # Update the progress window
                   1580:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
                   1581:     return;
1.31      matthew  1582: }
                   1583: 
                   1584: sub csv_finish {
                   1585:     my ($r) = @_;
1.54      matthew  1586:     return if ($request_aborted);
1.37      matthew  1587:     return if (! defined($outputfile));
                   1588:     close($outputfile);
                   1589:     #
                   1590:     my $c = $r->connection();
                   1591:     return if ($c->aborted());
                   1592:     #
                   1593:     # Close the progress window
                   1594:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                   1595:     #
                   1596:     # Tell the user where to get their csv file
                   1597:     $r->print('<br />'.
                   1598:               '<a href="'.$filename.'">Your csv file.</a>'."\n");
                   1599:     $r->rflush();
                   1600:     return;
                   1601:     
1.31      matthew  1602: }
1.2       stredwic 1603: 
                   1604: }
                   1605: 
1.28      matthew  1606: #######################################################
                   1607: #######################################################
                   1608: 
1.2       stredwic 1609: =pod
                   1610: 
1.54      matthew  1611: =item &StudentTriesOnSequence()
1.2       stredwic 1612: 
1.30      matthew  1613: Inputs:
1.2       stredwic 1614: 
                   1615: =over 4
                   1616: 
1.30      matthew  1617: =item $student
1.28      matthew  1618: 
1.30      matthew  1619: =item $studentdata Hash ref to all student data
1.2       stredwic 1620: 
1.30      matthew  1621: =item $seq Hash ref, the sequence we are working on
1.2       stredwic 1622: 
1.30      matthew  1623: =item $links if defined we will output links to each resource.
1.2       stredwic 1624: 
1.28      matthew  1625: =back
1.2       stredwic 1626: 
                   1627: =cut
1.1       stredwic 1628: 
1.28      matthew  1629: #######################################################
                   1630: #######################################################
1.54      matthew  1631: sub StudentTriesOnSequence {
1.32      matthew  1632:     my ($student,$studentdata,$seq,$links) = @_;
1.31      matthew  1633:     $links = 'no' if (! defined($links));
1.1       stredwic 1634:     my $Str = '';
1.30      matthew  1635:     my ($sum,$max) = (0,0);
1.51      matthew  1636:     my $performance_length = 0;
1.54      matthew  1637:     my @TriesData = ();
                   1638:     my $tries;
1.30      matthew  1639:     foreach my $resource (@{$seq->{'contents'}}) {
                   1640:         next if ($resource->{'type'} ne 'assessment');
                   1641:         my $resource_data = $studentdata->{$resource->{'symb'}};
                   1642:         my $value = '';
                   1643:         foreach my $partnum (@{$resource->{'parts'}}) {
1.54      matthew  1644:             $tries = undef;
1.30      matthew  1645:             $max++;
1.51      matthew  1646:             $performance_length++;
1.30      matthew  1647:             my $symbol = ' '; # default to space
                   1648:             #
1.78      matthew  1649:             my $awarded = 0;
                   1650:             if (exists($resource_data->{'resource.'.$partnum.'.awarded'})) {
                   1651:                 $awarded = $resource_data->{'resource.'.$partnum.'.awarded'};
                   1652:                 $awarded = 0 if (! $awarded);
                   1653:             }
                   1654:             #
                   1655:             my $status = '';
1.30      matthew  1656:             if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
1.78      matthew  1657:                 $status = $resource_data->{'resource.'.$partnum.'.solved'};
                   1658:             }
                   1659:             #
                   1660:             my $tries = 0;
                   1661:             if(exists($resource_data->{'resource.'.$partnum.'.tries'})) {
                   1662:                 $tries = $resource_data->{'resource.'.$partnum.'.tries'};
                   1663:             }
                   1664:             #
                   1665:             if ($awarded > 0) {
                   1666:                 # The student has gotten the problem correct to some degree
                   1667:                 if ($status eq 'excused') {
                   1668:                     $symbol = 'x';
                   1669:                     $max--;
                   1670:                 } elsif ($status eq 'correct_by_override') {
1.30      matthew  1671:                     $symbol = '+';
                   1672:                     $sum++;
1.78      matthew  1673:                 } elsif ($tries > 0) {
1.54      matthew  1674:                     if ($tries > 9) {
1.30      matthew  1675:                         $symbol = '*';
1.78      matthew  1676:                     } else {
1.54      matthew  1677:                         $symbol = $tries;
1.30      matthew  1678:                     }
                   1679:                     $sum++;
                   1680:                 } else {
1.78      matthew  1681:                     $symbol = '+';
                   1682:                     $sum++;
1.2       stredwic 1683:                 }
1.30      matthew  1684:             } else {
1.78      matthew  1685:                 # The student has the problem incorrect or it is ungraded
                   1686:                 if ($status eq 'excused') {
                   1687:                     $symbol = 'x';
                   1688:                     $max--;
                   1689:                 } elsif ($status eq 'incorrect_by_override') {
                   1690:                     $symbol = '-';
                   1691:                 } elsif ($status eq 'ungraded_attempted') {
                   1692:                     $symbol = '#';
                   1693:                 } elsif ($status eq 'incorrect_attempted' ||
                   1694:                          $tries > 0)  {
1.30      matthew  1695:                     $symbol = '.';
                   1696:                 } else {
1.78      matthew  1697:                     # Problem is wrong and has not been attempted.
                   1698:                     $symbol=' ';
1.18      matthew  1699:                 }
1.2       stredwic 1700:             }
1.54      matthew  1701:             #
                   1702:             if (! defined($tries)) {
                   1703:                 $tries = $symbol;
                   1704:             }
                   1705:             push (@TriesData,$tries);
1.30      matthew  1706:             #
1.47      matthew  1707:             if ( ($links eq 'yes' && $symbol ne ' ') ||
                   1708:                  ($links eq 'all')) {
1.49      matthew  1709:                 if (length($symbol) > 1) {
                   1710:                     &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
                   1711:                 }
1.30      matthew  1712:                 $symbol = '<a href="/adm/grades'.
                   1713:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
                   1714:                         '&student='.$student->{'username'}.
1.64      albertel 1715:                             '&userdom='.$student->{'domain'}.
1.30      matthew  1716:                                 '&command=submission">'.$symbol.'</a>';
                   1717:             }
                   1718:             $value .= $symbol;
1.2       stredwic 1719:         }
1.30      matthew  1720:         $Str .= $value;
1.17      minaeibi 1721:     }
1.51      matthew  1722:     if ($seq->{'randompick'}) {
                   1723:         $max = $seq->{'randompick'};
                   1724:     }
1.54      matthew  1725:     return ($Str,$performance_length,$sum,$max,\@TriesData);
                   1726: }
                   1727: 
                   1728: #######################################################
                   1729: #######################################################
                   1730: 
                   1731: =pod
                   1732: 
                   1733: =item &StudentPerformanceOnSequence()
                   1734: 
                   1735: Inputs:
                   1736: 
                   1737: =over 4
                   1738: 
                   1739: =item $student
                   1740: 
                   1741: =item $studentdata Hash ref to all student data
                   1742: 
                   1743: =item $seq Hash ref, the sequence we are working on
                   1744: 
                   1745: =item $links if defined we will output links to each resource.
                   1746: 
                   1747: =back
                   1748: 
                   1749: =cut
                   1750: 
                   1751: #######################################################
                   1752: #######################################################
                   1753: sub StudentPerformanceOnSequence {
                   1754:     my ($student,$studentdata,$seq,$links) = @_;
                   1755:     $links = 'no' if (! defined($links));
                   1756:     my $Str = ''; # final result string
                   1757:     my ($score,$max) = (0,0);
                   1758:     my $performance_length = 0;
                   1759:     my $symbol;
                   1760:     my @ScoreData = ();
                   1761:     my $partscore;
                   1762:     foreach my $resource (@{$seq->{'contents'}}) {
                   1763:         next if ($resource->{'type'} ne 'assessment');
                   1764:         my $resource_data = $studentdata->{$resource->{'symb'}};
                   1765:         foreach my $part (@{$resource->{'parts'}}) {
                   1766:             $partscore = undef;
                   1767:             my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
                   1768:                                               $resource->{'symb'},
                   1769:                                               $student->{'domain'},
                   1770:                                               $student->{'username'},
                   1771:                                               $student->{'section'});
                   1772:             if (!defined($weight) || ($weight eq '')) { 
                   1773:                 $weight=1;
                   1774:             }
                   1775:             #
                   1776:             $max += $weight; # see the 'excused' branch below...
                   1777:             $performance_length++; # one character per part
                   1778:             $symbol = ' '; # default to space
                   1779:             #
                   1780:             my $awarded = 0;
                   1781:             if (exists($resource_data->{'resource.'.$part.'.awarded'})) {
                   1782:                 $awarded = $resource_data->{'resource.'.$part.'.awarded'};
1.67      matthew  1783:                 $awarded = 0 if (! $awarded);
1.54      matthew  1784:             }
                   1785:             #
                   1786:             $partscore = $weight*$awarded;
                   1787:             $score += $partscore;
1.63      matthew  1788:             $symbol = $partscore; 
1.70      matthew  1789:             if (abs($symbol - sprintf("%.0f",$symbol)) < 0.001) {
                   1790:                 $symbol = sprintf("%.0f",$symbol);
                   1791:             }
1.54      matthew  1792:             if (length($symbol) > 1) {
                   1793:                 $symbol = '*';
                   1794:             }
                   1795:             if (exists($resource_data->{'resource.'.$part.'.solved'})) {
                   1796:                 my $status = $resource_data->{'resource.'.$part.'.solved'};
                   1797:                 if ($status eq 'excused') {
                   1798:                     $symbol = 'x';
                   1799:                     $max -= $weight; # Do not count 'excused' problems.
                   1800:                 }
                   1801:             } else {
                   1802:                 # Unsolved.  Did they try?
                   1803:                 if (exists($resource_data->{'resource.'.$part.'.tries'})){
                   1804:                     $symbol = '.';
                   1805:                 } else {
                   1806:                     $symbol = ' ';
                   1807:                 }
                   1808:             }
                   1809:             #
1.60      matthew  1810:             if (! defined($partscore)) {
                   1811:                 $partscore = $symbol;
                   1812:             }
                   1813:             push (@ScoreData,$partscore);
                   1814:             #
1.54      matthew  1815:             if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) {
                   1816:                 $symbol = '<a href="/adm/grades'.
                   1817:                     '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
                   1818:                     '&student='.$student->{'username'}.
1.64      albertel 1819:                     '&userdom='.$student->{'domain'}.
1.54      matthew  1820:                     '&command=submission">'.$symbol.'</a>';
                   1821:             }
1.60      matthew  1822:             $Str .= $symbol;
1.54      matthew  1823:         }
                   1824:     }
                   1825:     return ($Str,$performance_length,$score,$max,\@ScoreData);
1.1       stredwic 1826: }
1.23      minaeibi 1827: 
1.28      matthew  1828: #######################################################
                   1829: #######################################################
1.23      minaeibi 1830: 
1.2       stredwic 1831: =pod
                   1832: 
                   1833: =item &CreateLegend()
                   1834: 
                   1835: This function returns a formatted string containing the legend for the
                   1836: chart.  The legend describes the symbols used to represent grades for
                   1837: problems.
                   1838: 
                   1839: =cut
                   1840: 
1.28      matthew  1841: #######################################################
                   1842: #######################################################
1.2       stredwic 1843: sub CreateLegend {
                   1844:     my $Str = "<p><pre>".
1.13      minaeibi 1845:               "   1  correct by student in 1 try\n".
                   1846:               "   7  correct by student in 7 tries\n".
1.12      minaeibi 1847:               "   *  correct by student in more than 9 tries\n".
1.20      minaeibi 1848: 	      "   +  correct by hand grading or override\n".
1.12      minaeibi 1849:               "   -  incorrect by override\n".
                   1850: 	      "   .  incorrect attempted\n".
                   1851: 	      "   #  ungraded attempted\n".
1.13      minaeibi 1852:               "      not attempted (blank field)\n".
1.12      minaeibi 1853: 	      "   x  excused".
1.17      minaeibi 1854:               "</pre><p>";
1.2       stredwic 1855:     return $Str;
                   1856: }
                   1857: 
1.28      matthew  1858: #######################################################
                   1859: #######################################################
                   1860: 
1.30      matthew  1861: =pod 
1.2       stredwic 1862: 
                   1863: =back
                   1864: 
                   1865: =cut
                   1866: 
1.28      matthew  1867: #######################################################
                   1868: #######################################################
1.2       stredwic 1869: 
1.28      matthew  1870: 1;
1.2       stredwic 1871: 
1.1       stredwic 1872: __END__

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