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

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

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