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

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

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