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

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

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