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

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

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