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

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

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