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

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

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