Annotation of loncom/interface/statistics/lonproblemstatistics.pm, revision 1.59

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.59    ! matthew     3: # $Id: lonproblemstatistics.pm,v 1.58 2003/09/03 16:07:06 matthew Exp $
1.1       stredwic    4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: # (Navigate problems for statistical reports
                     28: #
1.47      matthew    29: ###############################################
                     30: ###############################################
                     31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
                     36: lonproblemstatistics
                     37: 
                     38: =head1 SYNOPSIS
                     39: 
                     40: Routines to present problem statistics to instructors via tables,
                     41: Excel files, and plots.
                     42: 
                     43: =over 4
                     44: 
                     45: =cut
                     46: 
                     47: ###############################################
                     48: ###############################################
1.1       stredwic   49: 
1.36      minaeibi   50: package Apache::lonproblemstatistics;
1.1       stredwic   51: 
                     52: use strict;
                     53: use Apache::lonnet();
                     54: use Apache::lonhtmlcommon;
                     55: use Apache::loncoursedata;
1.41      matthew    56: use Apache::lonstatistics;
1.59    ! matthew    57: use Apache::lonlocal;
1.44      matthew    58: use Spreadsheet::WriteExcel;
1.1       stredwic   59: 
1.59    ! matthew    60: ##
        !            61: ## Localization notes:
        !            62: ##
        !            63: ## in @Fields[0]->{'long_title'} is placed in Excel files and is used as the
        !            64: ## header for plots created with Graph.pm, both of which more than likely do
        !            65: ## not support localization.
        !            66: ##
1.49      matthew    67: my @Fields = (
                     68:            { name => 'problem_num',
                     69:              title => 'P#',
                     70:              align => 'right',
                     71:              color => '#FFFFE6' },
                     72:            { name   => 'container',
1.51      matthew    73:              title  => 'Sequence or Folder',
1.49      matthew    74:              align  => 'left',
                     75:              color  => '#FFFFE6',
                     76:              sortable => 'yes' },
                     77:            { name   => 'title',
                     78:              title  => 'Title',
                     79:              align  => 'left',
                     80:              color  => '#FFFFE6',
                     81:              special  => 'link',
                     82:              sortable => 'yes', },
                     83:            { name   => 'part', 
                     84:              title  => 'Part',
                     85:              align  => 'left',
1.55      matthew    86:              color  => '#FFFFE6',
                     87:              },
1.49      matthew    88:            { name   => 'num_students',
                     89:              title  => '#Stdnts',
                     90:              align  => 'right',
                     91:              color  => '#EEFFCC',
                     92:              format => '%d',
                     93:              sortable  => 'yes',
                     94:              graphable => 'yes',
                     95:              long_title => 'Number of Students Attempting Problem' },
                     96:            { name   => 'tries',
                     97:              title  => 'Tries',
                     98:              align  => 'right',
                     99:              color  => '#EEFFCC',
                    100:              format => '%d',
                    101:              sortable  => 'yes',
                    102:              graphable => 'yes',
                    103:              long_title => 'Total Number of Tries' },
                    104:            { name   => 'max_tries',
                    105:              title  => 'Max Tries',
                    106:              align  => 'right',
                    107:              color  => '#DDFFFF',
                    108:              format => '%d',
                    109:              sortable  => 'yes',
                    110:              graphable => 'yes',
                    111:              long_title => 'Maximum Number of Tries' },
                    112:            { name   => 'mean_tries',
                    113:              title  => 'Mean Tries',
                    114:              align  => 'right',
                    115:              color  => '#DDFFFF',
                    116:              format => '%5.2f',
                    117:              sortable  => 'yes',
                    118:              graphable => 'yes',
                    119:              long_title => 'Average Number of Tries' },
                    120:            { name   => 'std_tries',
                    121:              title  => 'S.D. tries',
                    122:              align  => 'right',
                    123:              color  => '#DDFFFF',
                    124:              format => '%5.2f',
                    125:              sortable  => 'yes',
                    126:              graphable => 'yes',
                    127:              long_title => 'Standard Deviation of Number of Tries' },
                    128:            { name   => 'skew_tries',
                    129:              title  => 'Skew Tries',
                    130:              align  => 'right',
                    131:              color  => '#DDFFFF',
                    132:              format => '%5.2f',
                    133:              sortable  => 'yes',
                    134:              graphable => 'yes',
                    135:              long_title => 'Skew of Number of Tries' },
                    136:            { name   => 'deg_of_diff',
                    137:              title  => 'DoDiff',
                    138:              align  => 'right',
                    139:              color  => '#DDFFFF',
                    140:              format => '%5.2f',
                    141:              sortable  => 'yes',
                    142:              graphable => 'yes',
1.55      matthew   143:              long_title => 'Degree of Difficulty'.
                    144:                            '[ 1 - ((#YES+#yes) / Tries) ]'},
1.49      matthew   145:            { name   => 'num_solved',
                    146:              title  => '#YES',
                    147:              align  => 'right',
                    148:              color  => '#FFDDDD',
                    149:              format => '%d',
                    150:              sortable  => 'yes',
                    151:              graphable => 'yes',
                    152:              long_title => 'Number of Students able to Solve' },
                    153:            { name   => 'num_override',
                    154:              title  => '#yes',
                    155:              align  => 'right',
                    156:              color  => '#FFDDDD',
                    157:              format => '%d',
                    158:              sortable  => 'yes',
                    159:              graphable => 'yes',
                    160:              long_title => 'Number of Students given Override' },
                    161:            { name   => 'per_wrong',
                    162:              title  => '%Wrng',
                    163:              align  => 'right',
                    164:              color  => '#FFFFE6',
                    165:              format => '%4.1f',
                    166:              sortable  => 'yes',
                    167:              graphable => 'yes',
1.55      matthew   168:              long_title => 'Percent of students whose final answer is wrong' },
1.49      matthew   169: );
                    170: 
1.47      matthew   171: ###############################################
                    172: ###############################################
                    173: 
                    174: =pod 
                    175: 
                    176: =item &CreateInterface()
                    177: 
                    178: Create the main intereface for the statistics page.  Allows the user to
                    179: select sections, maps, and output.
                    180: 
                    181: =cut
1.1       stredwic  182: 
1.47      matthew   183: ###############################################
                    184: ###############################################
1.57      matthew   185: my @OutputOptions = 
                    186:     (
                    187:      { name  => 'problem statistics grouped by sequence',
                    188:        value => 'HTML problem statistics grouped',
                    189:        description => 'Output statistics for the problem parts.',
                    190:        mode => 'html',
                    191:        show => 'grouped',
                    192:      },
                    193:      { name  => 'problem statistics ungrouped',
                    194:        value => 'HTML problem statistics ungrouped',
                    195:        description => 'Output statistics for the problem parts.',
                    196:        mode => 'html',
                    197:        show => 'ungrouped',
                    198:      },
                    199:      { name  => 'problem statistics, Excel',
                    200:        value => 'Excel problem statistics',
                    201:        description => 'Output statistics for the problem parts '.
                    202:            'in an Excel workbook',
                    203:        mode => 'excel',
                    204:        show => 'all',
                    205:      },
                    206:      );
                    207: 
1.41      matthew   208: sub CreateInterface {
                    209:     my $Str = '';
                    210:     $Str .= '<table cellspacing="5">'."\n";
                    211:     $Str .= '<tr>';
1.59    ! matthew   212:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
        !           213:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
        !           214:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
        !           215:     $Str .= '<td align="center"><b>'.&mt('Output').'</b></td>';
1.41      matthew   216:     $Str .= '</tr>'."\n";
                    217:     #
                    218:     $Str .= '<tr><td align="center">'."\n";
                    219:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1.50      matthew   220:     $Str .= '</td><td align="center">';
                    221:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1.41      matthew   222:     $Str .= '</td><td align="center">';
                    223:     #
                    224:     my $only_seq_with_assessments = sub { 
                    225:         my $s=shift;
                    226:         if ($s->{'num_assess'} < 1) { 
                    227:             return 0;
                    228:         } else { 
                    229:             return 1;
                    230:         }
                    231:     };
                    232:     $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
                    233:                                               $only_seq_with_assessments);
                    234:     $Str .= '</td><td>'."\n";
1.57      matthew   235:     my ($html,$outputmode,$show) = 
                    236:         &Apache::lonstatistics::CreateAndParseOutputSelector(
                    237:                                             'statsoutputmode',
                    238:                                             'HTML problem statistics grouped',
                    239:                                             @OutputOptions);
                    240:     $Str .= $html;
1.41      matthew   241:     $Str .= '</td></tr>'."\n";
                    242:     $Str .= '</table>'."\n";
1.59    ! matthew   243:     $Str .= '<input type="submit" name="GenerateStatistics" value="'.
        !           244:         &mt('Generate Statistics').'" />';
1.54      matthew   245:     $Str .= '&nbsp;'x5;
1.59    ! matthew   246:     $Str .= '<input type="submit" name="ClearCache" value="'.
        !           247:         &mt('Clear Caches').'" />';
1.54      matthew   248:     $Str .= '&nbsp;'x5;
1.57      matthew   249:     return ($Str,$outputmode,$show);
1.41      matthew   250: }
1.25      stredwic  251: 
1.41      matthew   252: ###############################################
                    253: ###############################################
1.28      stredwic  254: 
1.47      matthew   255: =pod 
                    256: 
                    257: =item &BuildProblemStatisticsPage()
                    258: 
                    259: Main interface to problem statistics.
                    260: 
                    261: =cut
                    262: 
1.41      matthew   263: ###############################################
                    264: ###############################################
                    265: sub BuildProblemStatisticsPage {
                    266:     my ($r,$c)=@_;
                    267:     #
1.57      matthew   268:     my ($interface,$output_mode,$show) = &CreateInterface();
                    269:     $r->print($interface);
1.41      matthew   270:     $r->print('<input type="hidden" name="statsfirstcall" value="no" />');
                    271:     $r->print('<input type="hidden" name="sortby" value="'.$ENV{'form.sortby'}.
                    272:               '" />');
1.49      matthew   273:     $r->print('<input type="hidden" name="plot" value="" />');
1.41      matthew   274:     if (! exists($ENV{'form.statsfirstcall'})) {
                    275:         return;
1.28      stredwic  276:     }
1.41      matthew   277:     #
1.56      matthew   278:     &Apache::lonstatistics::Gather_Student_Data($r);
1.41      matthew   279:     #
                    280:     #
                    281:     if ($output_mode eq 'html') {
                    282:         $r->print("<h2>".
                    283:                   $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
                    284:                   "</h2>\n");
                    285:         $r->print("<h3>".localtime(time)."</h3>");
                    286:         $r->rflush();
                    287:         if ($show eq 'grouped') {
                    288:             &output_html_grouped_by_sequence($r);
                    289:         } elsif ($show eq 'ungrouped') {
                    290:             &output_html_ungrouped($r);
                    291:         }
1.44      matthew   292:     } elsif ($output_mode eq 'excel') {
1.59    ! matthew   293:         $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
1.44      matthew   294:         &output_excel($r);
1.41      matthew   295:     } else {
1.59    ! matthew   296:         $r->print('<h1>'.&mt('Not implemented').'</h1>');
1.28      stredwic  297:     }
1.41      matthew   298:     return;
                    299: }
1.21      stredwic  300: 
1.44      matthew   301: ###############################################
                    302: ###############################################
                    303: 
1.47      matthew   304: =pod 
                    305: 
                    306: =item &output_html_grouped_by_sequence()
                    307: 
                    308: Presents the statistics data as an html table organized by the order
                    309: the assessments appear in the course.
                    310: 
                    311: =cut
                    312: 
1.44      matthew   313: ###############################################
                    314: ###############################################
1.41      matthew   315: sub output_html_grouped_by_sequence {
                    316:     my ($r) = @_;
1.45      matthew   317:     my $problem_num = 0;
1.41      matthew   318:     #$r->print(&ProblemStatisticsLegend());
                    319:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
                    320:         next if ($sequence->{'num_assess'}<1);
                    321:         $r->print("<h3>".$sequence->{'title'}."</h3>");
                    322:         $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
                    323:         $r->print('<table border="0" cellpadding="3">'."\n");
1.49      matthew   324:         $r->print('<tr bgcolor="#FFFFE6">');
                    325:         my $Str = &statistics_table_header('no container no plots');
                    326:         $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
1.41      matthew   327:         foreach my $resource (@{$sequence->{'contents'}}) {
                    328:             next if ($resource->{'type'} ne 'assessment');
                    329:             foreach my $part (@{$resource->{'parts'}}) {
1.45      matthew   330:                 $problem_num++;
1.49      matthew   331:                 my $data = &get_statistics($sequence,$resource,$part,
                    332:                                            $problem_num);
1.45      matthew   333:                 my $option = '';
1.49      matthew   334:                 $r->print('<tr>'.&statistics_html_table_data($data,
                    335:                                                              'no container').
1.41      matthew   336:                           "</tr>\n");
                    337:             }
                    338:         }
                    339:         $r->print("</table>\n");
                    340:         $r->print("</td></tr></table>\n");
                    341:         $r->rflush();
1.21      stredwic  342:     }
1.41      matthew   343:     #
                    344:     return;
                    345: }
1.25      stredwic  346: 
1.41      matthew   347: ###############################################
                    348: ###############################################
1.26      stredwic  349: 
1.47      matthew   350: =pod 
                    351: 
                    352: =item &output_html_ungrouped()
                    353: 
                    354: Presents the statistics data in a single html table which can be sorted by
                    355: different columns.
                    356: 
                    357: =cut
                    358: 
1.41      matthew   359: ###############################################
                    360: ###############################################
                    361: sub output_html_ungrouped {
1.45      matthew   362:     my ($r,$option) = @_;
1.41      matthew   363:     #
1.49      matthew   364:     if (exists($ENV{'form.plot'}) && $ENV{'form.plot'} ne '') {
                    365:         &plot_statistics($r,$ENV{'form.plot'});
                    366:     }
                    367:     #
1.45      matthew   368:     my $problem_num = 0;
1.41      matthew   369:     my $show_container = 0;
1.43      matthew   370:     my $show_part = 0;
1.41      matthew   371:     #$r->print(&ProblemStatisticsLegend());
1.42      matthew   372:     my $sortby = undef;
1.49      matthew   373:     foreach my $field (@Fields) {
                    374:         if ($ENV{'form.sortby'} eq $field->{'name'}) {
                    375:             $sortby = $field->{'name'};
1.42      matthew   376:         }
                    377:     }
1.49      matthew   378:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
                    379:         $sortby = 'container';
1.42      matthew   380:     }
1.45      matthew   381:     # If there is more than one sequence, list their titles
1.41      matthew   382:     my @Sequences = &Apache::lonstatistics::Sequences_with_Assess();
1.49      matthew   383:     if (@Sequences < 1) {
                    384:         $option .= ' no container';
1.45      matthew   385:     }
1.41      matthew   386:     #
1.42      matthew   387:     # Compile the data
                    388:     my @Statsarray;
1.41      matthew   389:     foreach my $sequence (@Sequences) {
                    390:         next if ($sequence->{'num_assess'}<1);
                    391:         foreach my $resource (@{$sequence->{'contents'}}) {
                    392:             next if ($resource->{'type'} ne 'assessment');
                    393:             foreach my $part (@{$resource->{'parts'}}) {
1.45      matthew   394:                 $problem_num++;
1.49      matthew   395:                 my $data = &get_statistics($sequence,$resource,$part,
                    396:                                            $problem_num);
1.43      matthew   397:                 $show_part = 1 if ($part ne '0');
                    398:                 #
1.49      matthew   399:                 push (@Statsarray,$data);
1.42      matthew   400:             }
                    401:         }
                    402:     }
                    403:     #
                    404:     # Sort the data
1.43      matthew   405:     my @OutputOrder;
1.49      matthew   406:     if ($sortby eq 'container') {
1.43      matthew   407:         @OutputOrder = @Statsarray;
1.42      matthew   408:     } else {
                    409:         # $sortby is already defined, so we can charge ahead
                    410:         if ($sortby =~ /^(title|part)$/i) {
                    411:             # Alpha comparison
                    412:             @OutputOrder = sort {
1.43      matthew   413:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
1.49      matthew   414:                     lc($a->{'title'}) cmp lc($b->{'title'}) ||
                    415:                         lc($a->{'part'}) cmp lc($b->{'part'});
1.42      matthew   416:             } @Statsarray;
                    417:         } else {
                    418:             # Numerical comparison
                    419:             @OutputOrder = sort {
                    420:                 my $retvalue = 0;
                    421:                 if ($b->{$sortby} eq 'nan') {
                    422:                     if ($a->{$sortby} ne 'nan') {
                    423:                         $retvalue = -1;
                    424:                     } else {
                    425:                         $retvalue = 0;
                    426:                     }
                    427:                 }
                    428:                 if ($a->{$sortby} eq 'nan') {
                    429:                     if ($b->{$sortby} ne 'nan') {
                    430:                         $retvalue = 1;
                    431:                     }
                    432:                 }
                    433:                 if ($retvalue eq '0') {
                    434:                     $retvalue = $b->{$sortby} <=> $a->{$sortby} ||
1.49      matthew   435:                                 lc($a->{'title'}) <=> lc($b->{'title'}) ||
                    436:                                 lc($a->{'part'})  <=> lc($b->{'part'});
1.41      matthew   437:                 }
1.42      matthew   438:                 $retvalue;
                    439:             } @Statsarray;
                    440:         }
1.43      matthew   441:     }
1.49      matthew   442:     $option .= 'no part' if (! $show_part);
                    443:     my $num_output = 0;
                    444:     #
                    445:     # output the headers
                    446:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
                    447:     $r->print('<table border="0" cellpadding="3">'."\n");
                    448:     my $Str = &statistics_table_header($option.' sortable');
                    449:     $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
                    450:     #
                    451:     foreach my $rowdata (@OutputOrder) {
                    452:         $num_output++;
                    453:         if ($num_output % 25 == 0) {
                    454:             $r->print("</table>\n</td></tr></table>\n");
                    455:             #
                    456:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n");
                    457:             $r->print('<table border="0" cellpadding="3">'."\n");
                    458:             my $Str = &statistics_table_header($option.' sortable');
                    459:             $r->print('<tr bgcolor="#FFFFE6">'.$Str."</tr>\n");
                    460:             $r->rflush();
                    461:         }
                    462:         $r->print('<tr>'.&statistics_html_table_data($rowdata,$option).
                    463:                   "</tr>\n");
1.26      stredwic  464:     }
1.41      matthew   465:     $r->print("</table>\n");
                    466:     $r->print("</td></tr></table>\n");
1.26      stredwic  467:     $r->rflush();
1.41      matthew   468:     #
                    469:     return;
1.42      matthew   470: }
                    471: 
1.41      matthew   472: ###############################################
                    473: ###############################################
1.26      stredwic  474: 
1.47      matthew   475: =pod 
                    476: 
                    477: =item &output_excel()
                    478: 
                    479: Presents the statistical data in an Excel 95 compatable spreadsheet file.
                    480: 
                    481: =cut
                    482: 
1.41      matthew   483: ###############################################
                    484: ###############################################
1.44      matthew   485: sub output_excel {
                    486:     my ($r) = @_;
                    487:     my $filename = '/prtspool/'.
                    488:         $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                    489:             time.'_'.rand(1000000000).'.xls';
                    490:     #
                    491:     my $excel_workbook = undef;
                    492:     my $excel_sheet = undef;
                    493:     #
                    494:     my $rows_output = 0;
                    495:     my $cols_output = 0;
                    496:     #
                    497:     # Create sheet
                    498:     $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
                    499:     #
                    500:     # Check for errors
                    501:     if (! defined($excel_workbook)) {
                    502:         $r->log_error("Error creating excel spreadsheet $filename: $!");
1.59    ! matthew   503:         $r->print(&mt("Problems creating new Excel file.  ".
1.44      matthew   504:                   "This error has been logged.  ".
1.59    ! matthew   505:                   "Please alert your LON-CAPA administrator."));
1.44      matthew   506:         return ;
                    507:     }
                    508:     #
                    509:     # The excel spreadsheet stores temporary data in files, then put them
                    510:     # together.  If needed we should be able to disable this (memory only).
                    511:     # The temporary directory must be specified before calling 'addworksheet'.
                    512:     # File::Temp is used to determine the temporary directory.
                    513:     $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
                    514:     #
                    515:     # Add a worksheet
                    516:     my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    517:     if (length($sheetname) > 31) {
                    518:         $sheetname = substr($sheetname,0,31);
                    519:     }
                    520:     $excel_sheet = $excel_workbook->addworksheet($sheetname);
                    521:     #
                    522:     # Put the course description in the header
                    523:     $excel_sheet->write($rows_output,$cols_output++,
                    524:                    $ENV{'course.'.$ENV{'request.course.id'}.'.description'});
                    525:     $cols_output += 3;
                    526:     #
                    527:     # Put a description of the sections listed
                    528:     my $sectionstring = '';
                    529:     my @Sections = @Apache::lonstatistics::SelectedSections;
                    530:     if (scalar(@Sections) > 1) {
                    531:         if (scalar(@Sections) > 2) {
                    532:             my $last = pop(@Sections);
                    533:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
                    534:         } else {
                    535:             $sectionstring = "Sections ".join(' and ',@Sections);
                    536:         }
                    537:     } else {
                    538:         if ($Sections[0] eq 'all') {
                    539:             $sectionstring = "All sections";
                    540:         } else {
                    541:             $sectionstring = "Section ".$Sections[0];
                    542:         }
                    543:     }
                    544:     $excel_sheet->write($rows_output,$cols_output++,$sectionstring);
                    545:     $cols_output += scalar(@Sections);
                    546:     #
                    547:     # Put the date in there too
                    548:     $excel_sheet->write($rows_output,$cols_output++,
                    549:                         'Compiled on '.localtime(time));
                    550:     #
                    551:     $rows_output++; 
                    552:     $cols_output=0;
                    553:     #
1.55      matthew   554:     # Long Headersheaders
                    555:     foreach my $field (@Fields) {
                    556:         next if ($field->{'name'} eq 'problem_num');
                    557:         if (exists($field->{'long_title'})) {
                    558:             $excel_sheet->write($rows_output,$cols_output++,
                    559:                                 $field->{'long_title'});
                    560:         } else {
                    561:             $excel_sheet->write($rows_output,$cols_output++,'');
                    562:         }
                    563:     }
                    564:     $rows_output++;
                    565:     $cols_output=0;
                    566:     # Brief headers
1.49      matthew   567:     foreach my $field (@Fields) {
                    568:         next if ($field->{'name'} eq 'problem_num');
1.59    ! matthew   569:         # Use english for excel as I am not sure how well excel handles 
        !           570:         # other character sets....
1.49      matthew   571:         $excel_sheet->write($rows_output,$cols_output++,$field->{'title'});
1.44      matthew   572:     }
                    573:     $rows_output++;
                    574:     #
                    575:     # Write the data
1.49      matthew   576:     my $problem_num=0;
1.44      matthew   577:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
                    578:         next if ($sequence->{'num_assess'}<1);
                    579:         foreach my $resource (@{$sequence->{'contents'}}) {
                    580:             next if ($resource->{'type'} ne 'assessment');
                    581:             foreach my $part (@{$resource->{'parts'}}) {
                    582:                 $cols_output=0;
1.49      matthew   583:                 $problem_num++;
                    584:                 my $data = &get_statistics($sequence,$resource,$part,
                    585:                                            $problem_num);
1.44      matthew   586:                 #
                    587:                 if (!defined($part) || $part eq '') {
                    588:                     $part = ' ';
                    589:                 }
1.49      matthew   590:                 foreach my $field (@Fields) {
                    591:                     next if ($field->{'name'} eq 'problem_num');
                    592:                     $excel_sheet->write($rows_output,$cols_output++,
                    593:                                         $data->{$field->{'name'}});
1.44      matthew   594:                 }
                    595:                 $rows_output++;
                    596:             }
                    597:         }
                    598:     }
                    599:     #
                    600:     # Write the excel file
                    601:     $excel_workbook->close();
                    602:     # Tell the user where to get their excel file
                    603:     $r->print('<br />'.
1.59    ! matthew   604:               '<a href="'.$filename.'">'.
        !           605:               &mt('Your Excel Spreadsheet').'</a>'."\n");
1.44      matthew   606:     $r->rflush();
                    607:     return;
                    608: }
                    609: 
1.45      matthew   610: ###############################################
                    611: ###############################################
1.44      matthew   612: 
1.47      matthew   613: =pod 
                    614: 
                    615: =item &statistics_html_table_data()
                    616: 
                    617: Help function used to format the rows for HTML table output.
                    618: 
                    619: =cut
                    620: 
1.45      matthew   621: ###############################################
                    622: ###############################################
1.41      matthew   623: sub statistics_html_table_data {
1.49      matthew   624:     my ($data,$options) = @_;
1.41      matthew   625:     my $row = '';
1.49      matthew   626:     foreach my $field (@Fields) {
                    627:         next if ($options =~ /no $field->{'name'}/);
                    628:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
                    629:         if (exists($field->{'align'})) {
                    630:             $row .= ' align="'.$field->{'align'}.'"';
                    631:             }
                    632:         $row .= '>';
                    633:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
1.53      matthew   634:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
1.49      matthew   635:         }
                    636:         if (exists($field->{'format'})) {
                    637:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
                    638:         } else {
                    639:             $row .= $data->{$field->{'name'}};
                    640:         }
                    641:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
                    642:             $row.= '</a>';
                    643:         }
                    644:         $row .= '</td>';
1.26      stredwic  645:     }
1.41      matthew   646:     return $row;
                    647: }
1.26      stredwic  648: 
1.49      matthew   649: sub statistics_table_header {
                    650:     my ($options) = @_;
                    651:     my $header_row;
                    652:     foreach my $field (@Fields) {
                    653:         next if ($options =~ /no $field->{'name'}/);
                    654:         $header_row .= '<th>';
                    655:         if ($options =~ /sortable/ && 
                    656:             exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
                    657:             $header_row .= '<a href="javascript:'.
                    658:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
                    659:                     ';document.Statistics.submit();">';
                    660:         }
1.59    ! matthew   661:         $header_row .= &mt($field->{'title'});
1.49      matthew   662:         if ($options =~ /sortable/) {
                    663:             $header_row.= '</a>';
                    664:         }
                    665:         if ($options !~ /no plots/        && 
                    666:             exists($field->{'graphable'}) && 
                    667:             $field->{'graphable'} eq 'yes') {
                    668:             $header_row.=' (';
                    669:             $header_row .= '<a href="javascript:'.
                    670:                 "document.Statistics.plot.value='$field->{'name'}'".
                    671:                     ';document.Statistics.submit();">';
1.59    ! matthew   672:             $header_row .= &mt('plot').'</a>)';
1.49      matthew   673:         }
                    674:         $header_row .= '</th>';
                    675:     }
                    676:     return $header_row;
                    677: }
                    678: 
1.41      matthew   679: ###############################################
                    680: ###############################################
1.47      matthew   681: 
                    682: =pod 
                    683: 
                    684: =item &plot_statistics()
                    685: 
                    686: =cut
                    687: 
                    688: ###############################################
                    689: ###############################################
1.45      matthew   690: sub plot_statistics {
                    691:     my ($r,$datafield) = @_;
                    692:     my @Data;
                    693:     #
1.49      matthew   694:     #
                    695:     my $sortfield = undef;
                    696:     my $title = undef;
                    697:     foreach my $field (@Fields) {
                    698:         if ($datafield eq $field->{'name'} &&
                    699:             exists($field->{'graphable'}) && $field->{'graphable'} eq 'yes') {
                    700:             $sortfield = $field->{'name'};
                    701:             $title = $field->{'long_title'};
                    702:         }
1.34      minaeibi  703:     }
1.49      matthew   704:     return if (! defined($sortfield) || $sortfield eq '');
1.45      matthew   705:     #
                    706:     my $Max = 0;
1.49      matthew   707:     my $problem_num = 0;
1.45      matthew   708:     foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()) {
                    709:         next if ($sequence->{'num_assess'}<1);
                    710:         foreach my $resource (@{$sequence->{'contents'}}) {
                    711:             next if ($resource->{'type'} ne 'assessment');
                    712:             foreach my $part (@{$resource->{'parts'}}) {
1.49      matthew   713:                 my $problem_number++;
                    714:                 my $data = &get_statistics($sequence,$resource,$part,
                    715:                                            $problem_num);
                    716:                 my $value = $data->{$sortfield};
                    717:                 $Max = $value if ($Max < $value);
                    718:                 push (@Data,$value);
1.45      matthew   719:             }
                    720:         }
1.26      stredwic  721:     }
1.45      matthew   722:     #
                    723:     # Print out plot request
1.49      matthew   724:     my $yaxis = '';
                    725:     if ($sortfield eq 'per_wrong') {
                    726:         $yaxis = 'Percent';
1.45      matthew   727:     }
                    728:     #
                    729:     # Determine appropriate value for $Max
1.49      matthew   730:     if ($sortfield eq 'deg_of_diff') {
1.45      matthew   731:         if ($Max > 0.5) {
                    732:             $Max = 1;
                    733:         } elsif ($Max > 0.2) {
                    734:             $Max = 0.5;
                    735:         } elsif ($Max > 0.1) {
                    736:             $Max = 0.2;
                    737:         }
1.49      matthew   738:     } elsif ($sortfield eq 'per_wrong') {
1.45      matthew   739:         if ($Max > 50) {
                    740:             $Max = 100;
                    741:         } elsif ($Max > 25) {
                    742:             $Max = 50;
                    743:         } elsif ($Max > 20) {
                    744:             $Max = 25;
                    745:         } elsif ($Max > 10) {
                    746:             $Max = 20;
                    747:         } elsif ($Max > 5) {
                    748:             $Max = 10;
1.24      stredwic  749:         } else {
1.45      matthew   750:             $Max = 5;
1.24      stredwic  751:         }
                    752:     }
1.45      matthew   753:     
                    754:     $r->print("<p>".&DrawGraph(\@Data,$title,'Problem Number',$yaxis,
                    755:                                $Max)."</p>\n");
                    756:     #
                    757:     # Print out the data
                    758:     $ENV{'form.sortby'} = 'Contents';
1.49      matthew   759: #    &output_html_ungrouped($r);
1.24      stredwic  760:     return;
                    761: }
1.1       stredwic  762: 
1.45      matthew   763: ###############################################
                    764: ###############################################
                    765: 
1.47      matthew   766: =pod 
                    767: 
                    768: =item &DrawGraph()
                    769: 
                    770: =cut
                    771: 
1.45      matthew   772: ###############################################
                    773: ###############################################
1.35      minaeibi  774: sub DrawGraph {
1.45      matthew   775:     my ($values,$title,$xaxis,$yaxis,$Max)=@_;
1.49      matthew   776:     $title = '' if (! defined($title));
1.45      matthew   777:     $xaxis = '' if (! defined($xaxis));
                    778:     $yaxis = '' if (! defined($yaxis));
1.58      matthew   779:     $title = &Apache::lonnet::escape($title);
                    780:     $xaxis = &Apache::lonnet::escape($xaxis);
                    781:     $yaxis = &Apache::lonnet::escape($yaxis);
1.45      matthew   782:     #
1.35      minaeibi  783:     my $sendValues = join(',', @$values);
                    784:     my $sendCount = scalar(@$values);
1.49      matthew   785:     $Max =1 if ($Max < 1);
                    786:     if ( int($Max) < $Max ) {
                    787:         $Max++;
                    788:         $Max = int($Max);
1.1       stredwic  789:     }
1.45      matthew   790:     my @GData = ($title,$xaxis,$yaxis,$Max,$sendCount,$sendValues);
                    791:     return '<IMG src="/cgi-bin/graph.png?'.
                    792:         (join('&', @GData)).'" border="1" />';
1.48      matthew   793: }
                    794: 
                    795: sub get_statistics {
1.49      matthew   796:     my ($sequence,$resource,$part,$problem_num) = @_;
1.48      matthew   797:     #
1.49      matthew   798:     my $symb = $resource->{'symb'};
1.48      matthew   799:     my $courseid = $ENV{'request.course.id'};
                    800:     #
                    801:     my $students = \@Apache::lonstatistics::Students;
                    802:     if ($Apache::lonstatistics::SelectedSections[0] eq 'all') {
                    803:         $students = undef;
                    804:     }
1.49      matthew   805:     my $data = &Apache::loncoursedata::get_problem_statistics
1.48      matthew   806:                         ($students,$symb,$part,$courseid);
1.49      matthew   807:     $data->{'part'}        = $part;
                    808:     $data->{'problem_num'} = $problem_num;
                    809:     $data->{'container'}   = $sequence->{'title'};
                    810:     $data->{'title'}       = $resource->{'title'};
1.53      matthew   811:     $data->{'title.link'}  = $resource->{'src'}.'?symb='.
                    812:         &Apache::lonnet::escape($resource->{'symb'});
1.49      matthew   813:     #
                    814:     return $data;
1.1       stredwic  815: }
1.12      minaeibi  816: 
1.45      matthew   817: ###############################################
                    818: ###############################################
1.47      matthew   819: 
                    820: =pod 
                    821: 
                    822: =item &ProblemStatisticsLegend()
1.59    ! matthew   823: 
        !           824: HELP  This needs to be localized, or at least generated automatically.
1.47      matthew   825: 
                    826: =cut
1.1       stredwic  827: 
1.45      matthew   828: ###############################################
                    829: ###############################################
1.1       stredwic  830: sub ProblemStatisticsLegend {
                    831:     my $Ptr = '';
                    832:     $Ptr = '<table border="0">';
                    833:     $Ptr .= '<tr><td>';
1.6       minaeibi  834:     $Ptr .= '<b>#Stdnts</b></td>';
1.19      stredwic  835:     $Ptr .= '<td>Total number of students attempted the problem.';
1.1       stredwic  836:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  837:     $Ptr .= '<b>Tries</b></td>';
1.19      stredwic  838:     $Ptr .= '<td>Total number of tries for solving the problem.';
1.1       stredwic  839:     $Ptr .= '</td></tr><tr><td>';
1.49      matthew   840:     $Ptr .= '<b>Max Tries</b></td>';
1.19      stredwic  841:     $Ptr .= '<td>Largest number of tries for solving the problem by a student.';
1.1       stredwic  842:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  843:     $Ptr .= '<b>Mean</b></td>';
1.19      stredwic  844:     $Ptr .= '<td>Average number of tries. [ Tries / #Stdnts ]';
1.1       stredwic  845:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  846:     $Ptr .= '<b>#YES</b></td>';
1.1       stredwic  847:     $Ptr .= '<td>Number of students solved the problem correctly.';
                    848:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  849:     $Ptr .= '<b>#yes</b></td>';
1.1       stredwic  850:     $Ptr .= '<td>Number of students solved the problem by override.';
                    851:     $Ptr .= '</td></tr><tr><td>';
1.19      stredwic  852:     $Ptr .= '<b>%Wrong</b></td>';
                    853:     $Ptr .= '<td>Percentage of students who tried to solve the problem ';
                    854:     $Ptr .= 'but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]';
1.1       stredwic  855:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  856:     $Ptr .= '<b>DoDiff</b></td>';
1.1       stredwic  857:     $Ptr .= '<td>Degree of Difficulty of the problem.  ';
                    858:     $Ptr .= '[ 1 - ((#YES+#yes) / Tries) ]';
                    859:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  860:     $Ptr .= '<b>S.D.</b></td>';
1.1       stredwic  861:     $Ptr .= '<td>Standard Deviation of the tries.  ';
                    862:     $Ptr .= '[ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) ';
                    863:     $Ptr .= 'where Xi denotes every student\'s tries ]';
                    864:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  865:     $Ptr .= '<b>Skew.</b></td>';
1.1       stredwic  866:     $Ptr .= '<td>Skewness of the students tries.';
                    867:     $Ptr .= '[(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]';
                    868:     $Ptr .= '</td></tr><tr><td>';
1.6       minaeibi  869:     $Ptr .= '<b>Dis.F.</b></td>';
1.1       stredwic  870:     $Ptr .= '<td>Discrimination Factor: A Standard for evaluating the ';
                    871:     $Ptr .= 'problem according to a Criterion<br>';
1.31      stredwic  872:     $Ptr .= '<b>[Criterion to group students into %27 Upper Students - ';
                    873:     $Ptr .= 'and %27 Lower Students]</b><br>';
1.1       stredwic  874:     $Ptr .= '<b>1st Criterion</b> for Sorting the Students: ';
                    875:     $Ptr .= '<b>Sum of Partial Credit Awarded / Total Number of Tries</b><br>';
                    876:     $Ptr .= '<b>2nd Criterion</b> for Sorting the Students: ';
                    877:     $Ptr .= '<b>Total number of Correct Answers / Total Number of Tries</b>';
                    878:     $Ptr .= '</td></tr>';
                    879:     $Ptr .= '<tr><td><b>Disc.</b></td>';
                    880:     $Ptr .= '<td>Number of Students had at least one discussion.';
                    881:     $Ptr .= '</td></tr></table>';
                    882:     return $Ptr;
                    883: }
1.24      stredwic  884: 
                    885: #---- END Problem Statistics Web Page ----------------------------------------
1.4       minaeibi  886: 
1.1       stredwic  887: 1;
                    888: __END__

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