Annotation of loncom/interface/statistics/lonsubmissiontimeanalysis.pm, revision 1.20

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: #
1.20    ! matthew     3: # $Id: lonsubmissiontimeanalysis.pm,v 1.19 2005/03/04 20:34:15 matthew Exp $
1.1       matthew     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: 
                     28: package Apache::lonsubmissiontimeanalysis;
                     29: 
                     30: use strict;
                     31: use Apache::lonnet();
                     32: use Apache::loncommon();
                     33: use Apache::lonhtmlcommon();
                     34: use Apache::loncoursedata();
                     35: use Apache::lonstatistics;
1.10      matthew    36: use Apache::lonstathelpers;
1.1       matthew    37: use Apache::lonlocal;
                     38: use HTML::Entities();
                     39: use Time::Local();
                     40: use Spreadsheet::WriteExcel();
                     41: 
                     42: my $plotcolors = ['#33ff00', 
                     43:                   '#ff33cc', '#990000', '#aaaa66', '#663399', '#ff9933',
                     44:                   '#66ccff', '#ff9999', '#cccc33', '#660000', '#33cc66',
                     45:                   ]; 
                     46: 
                     47: my @SubmitButtons = (
1.11      matthew    48:                      { name => 'PrevProblemAnalysis',
                     49:                        text => 'Previous Problem' },
                     50:                      { name => 'ProblemAnalysis',
1.1       matthew    51:                        text => 'Analyze Problem Again' },
1.11      matthew    52:                      { name => 'NextProblemAnalysis',
                     53:                        text => 'Next Problem' },
1.1       matthew    54:                      { name => 'SelectAnother',
1.11      matthew    55:                        text => 'Choose a different Problem' },
1.1       matthew    56:                      );
                     57: 
                     58: sub BuildSubmissionTimePage {
                     59:     my ($r,$c)=@_;
1.4       matthew    60:     #
                     61:     my %Saveable_Parameters = ('Status' => 'scalar',
                     62:                                'Section' => 'array');
                     63:     &Apache::loncommon::store_course_settings('submissiontime_analysis',
                     64:                                               \%Saveable_Parameters);
                     65:     &Apache::loncommon::restore_course_settings('submissiontime_analysis',
                     66:                                                 \%Saveable_Parameters);
                     67:     #
                     68:     &Apache::lonstatistics::PrepareClasslist();    
                     69:     #
1.1       matthew    70:     $r->print(&CreateInterface());
                     71:     #
                     72:     my @Students = @Apache::lonstatistics::Students;
                     73:     #
                     74:     if (@Students < 1) {
                     75:         $r->print('<h2>There are no students in the sections selected</h2>');
                     76:     }
                     77:     #
1.15      matthew    78:     my @CacheButtonHTML = 
                     79:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
1.1       matthew    80:     $r->rflush();
                     81:     #
                     82:     if (! exists($ENV{'form.problemchoice'}) ||
                     83:         exists($ENV{'form.SelectAnother'})) {
                     84:         $r->print('<input type="submit" name="" value="'.
                     85:                   &mt('Graph Problem Submission Times').'" />');
                     86:         $r->print('&nbsp;'x5);
                     87:         $r->print('<h3>'.&mt('Please select a problem to analyze').'</h3>');
1.18      matthew    88:         $r->print(&Apache::lonstathelpers::problem_selector('.'));
1.1       matthew    89:     } else {
                     90:         foreach my $button (@SubmitButtons) {
                     91:             $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
                     92:                       'value="'.&mt($button->{'text'}).'" />');
                     93:             $r->print('&nbsp;'x5);
                     94:         }
1.15      matthew    95:         foreach my $html (@CacheButtonHTML) {
                     96:             $r->print($html.('&nbsp;'x5));
                     97:         }
                     98:         $r->rflush();
1.11      matthew    99:         #
                    100:         # Determine which problem we are to analyze
                    101:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
                    102:             ($ENV{'form.problemchoice'});
                    103:         #
1.18      matthew   104:         my ($navmap,$prev,$curr,$next) = 
1.11      matthew   105:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
                    106:                                                         '.',
                    107:                                                         'part');
                    108:         if (exists($ENV{'form.PrevProblemAnalysis'}) && defined($prev)) {
                    109:             $current_problem = $prev;
                    110:         } elsif (exists($ENV{'form.NextProblemAnalysis'}) && defined($next)) {
                    111:             $current_problem = $next;
                    112:         } else {
                    113:             $current_problem = $curr;
                    114:         }
                    115:         #
                    116:         # Store the current problem choice and send it out in the form
                    117:         $ENV{'form.problemchoice'} = 
                    118:             &Apache::lonstathelpers::make_target_id($current_problem);
1.1       matthew   119:         $r->print('<input type="hidden" name="problemchoice" value="'.
                    120:                   $ENV{'form.problemchoice'}.'" />');
                    121:         #
                    122:         $r->print('<hr />');
                    123:         $r->rflush();
                    124:         #
1.11      matthew   125:         my $resource = $current_problem->{'resource'};
1.1       matthew   126:         if (! defined($resource)) {
                    127:             $r->print('resource is undefined');
                    128:         } else {
1.18      matthew   129:             $r->print('<h1>'.$resource->compTitle.'</h1>');
                    130:             $r->print('<h3>'.$resource->src.'</h3>');
1.1       matthew   131:             $r->rflush();
1.10      matthew   132:             $r->print(&Apache::lonstathelpers::render_resource($resource));
1.15      matthew   133:             $r->print('<br />');
1.1       matthew   134:             $r->rflush();
1.18      matthew   135:             $r->print(&analyze_times($r,$resource->symb,\@Students,
1.11      matthew   136:                                      $current_problem->{'part'}));
1.1       matthew   137:         }
                    138:         $r->print('<hr />');
                    139:     }
                    140: }
                    141: 
                    142: #########################################################
                    143: #########################################################
                    144: ##
                    145: ##                  Time Analysis
                    146: ##
                    147: #########################################################
                    148: #########################################################
                    149: sub get_week_start {
                    150:     my ($randomtime) = @_;
                    151:     my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
                    152:         localtime($randomtime);
                    153:     $randomtime -= $wday * 86400;
                    154:     ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
                    155:         localtime($randomtime);
                    156:     my $week_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
                    157:     return $week_start;
                    158: }
                    159: 
                    160: sub analyze_times {
1.18      matthew   161:     my ($r,$symb,$students,$part) = @_;
1.16      matthew   162:     my $htmltable;
1.2       matthew   163:     #
                    164:     # Convenience arrays
                    165:     my @FullWeekDay = (qw/Sunday Monday Tuesday Wednesday Thursday Friday
                    166:                        Saturday/);
                    167:     my @WeekDay = (qw/SUN MON TUE WED THU FRI SAT SUN/);
                    168:     my @Month = (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);
                    169:     #
1.1       matthew   170:     my $html; # holds results of analysis
                    171:     # Get the data
                    172:     my $SubData = &Apache::loncoursedata::get_response_time_data
1.18      matthew   173:         (\@Apache::lonstatistics::SelectedSections,
                    174:          $Apache::lonstatistics::enrollment_status,
                    175:          $symb,$part);
1.1       matthew   176:     if (! defined($SubData) || ! ref($SubData)) {
1.18      matthew   177:         $html.= '<h2>There is no submission data for this problem at all</h2>';
1.1       matthew   178:         return $html;
                    179:     }
                    180:     my $NumSub = scalar(@{$SubData});
                    181:     if (! @{$SubData}) {
1.9       matthew   182:         $html.= '<h2>There is no submission data for this problem</h2>';
1.1       matthew   183:         return $html;
                    184:     }
                    185:     # Process the data
                    186:     #
                    187:     my (undef,undef,undef,$mday,$month,$year,$wday,$yday,$isdst) = 
                    188:         localtime(&get_time_from_row($SubData->[0]));
                    189:     my $day_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
                    190:     #
1.2       matthew   191:     # Configure the bins used to store the data.
1.1       matthew   192:     my $binsize = 3600; # seconds
                    193:     my $bins_per_day = 86400/$binsize;
                    194:     my $bincount = 0;
                    195:     my $endtime = $day_start;
                    196:     #
1.2       matthew   197:     # Initialize loop variables
1.17      matthew   198:     my $max;            # The sum of @Ydata
                    199:     my @Ydata=(0);      # number of submissions
                    200:     my @AnsData=(0);    # number of correct submissions
                    201:     my @Xlabel=($WeekDay[$wday]); # Labels of itmes
                    202:     my @BinEnd;                   # The end time of each bin
                    203:     my $cumulative_answers = 0;   # The sum of @AnsData
1.19      matthew   204:     my %students;       # which students have attempted the problem?
1.2       matthew   205:     #
1.1       matthew   206:     foreach my $row (@$SubData) {
                    207:         my $subtime = &get_time_from_row($row);
                    208:         while ($subtime > $endtime && $endtime < time) {
                    209:             # Create a new bin
                    210:             $bincount++;
1.17      matthew   211:             $Ydata[$bincount]   = 0;
                    212:             $AnsData[$bincount] = 0;
1.1       matthew   213:             $endtime += $binsize;
1.16      matthew   214:             push(@BinEnd,$endtime);
1.1       matthew   215:             if ($bincount % (86400/$binsize) == 0) {
1.2       matthew   216:                 $wday++;
1.1       matthew   217:                 $wday %= 7;
1.2       matthew   218:                 $Xlabel[$bincount] = $WeekDay[$wday];
1.1       matthew   219:             } else {
                    220:                 $Xlabel[$bincount] = '';
                    221:             }
                    222:         }
                    223:         $Ydata[$bincount]++;
                    224:         $max = $Ydata[$bincount] if ($max < $Ydata[$bincount]);
                    225:         $AnsData[$bincount] += &successful_submission($row);
                    226:         $cumulative_answers += &successful_submission($row);
1.19      matthew   227:         $students{$row->[&Apache::loncoursedata::RT_student_id()]}++;
1.1       matthew   228:     }
1.17      matthew   229:     #
                    230:     # Pad the data to a full day
1.1       matthew   231:     while ($bincount % $bins_per_day != 0) {
                    232:         $bincount++;
                    233:         $Ydata[$bincount]=0;
1.17      matthew   234:         $AnsData[$bincount]=0;
1.1       matthew   235:         $endtime += $binsize;
1.17      matthew   236:         push(@BinEnd,$endtime);
1.1       matthew   237:         if ($bincount % (86400/$binsize) == 0) {
1.2       matthew   238:             $wday ++;
                    239:             $wday %= 7;
1.1       matthew   240:             $Xlabel[$bincount] = $WeekDay[$wday];
                    241:         } else {
                    242:             $Xlabel[$bincount] = '';
                    243:         }
                    244:     }
1.19      matthew   245:     my $numstudents = scalar(keys(%students));
1.17      matthew   246:     #
                    247:     # Determine a nice maximum value to use
                    248:     foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
                    249:                           120,150,200,250,300,350,400,450,500,
                    250:                           600,700,800,900,1000,1100,1200,1500,2000,
                    251:                           2500,3000,4000,5000) {
                    252:         if ($max < $maximum) {
                    253:             $max = $maximum;
                    254:             last;
                    255:         }
                    256:     }
                    257:     #
                    258:     # Build the data table
1.16      matthew   259:     $htmltable = '<p>'.
                    260:         '<table rules="groups" frame="border" '.
                    261:         'summary="Student submission data">'.
                    262:         '<thead>'.
                    263:         '<tr>'.
1.17      matthew   264:         '<th valign="bottom">'.&mt('Begin').'</th>'.
1.16      matthew   265:         '<th>'.('&nbsp;'x3).'</th>'.
1.17      matthew   266:         '<th valign="bottom">'.&mt('End').'</th>'.
1.20    ! matthew   267:         '<th valign="bottom">'.&mt('Submissions (plotted)').'</th>'.
1.16      matthew   268:         '<th>'.('&nbsp;'x3).'</th>'.
1.20    ! matthew   269:         '<th valign="bottom">'.&mt('Correct Submissions (not plotted)').'</th>'.
1.16      matthew   270:         '<th>'.('&nbsp;'x3).'</th>'.
1.20    ! matthew   271:         '<th valign="bottom">'.&mt('Cumulative Correct of those attempting the problem (not plotted)').'</th>'.
1.19      matthew   272:         '<th>'.('&nbsp;'x3).'</th>'.
1.20    ! matthew   273:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of those attempting the problem (not plotted)').'</th>'.
        !           274:         '<th>'.('&nbsp;'x3).'</th>'.
        !           275:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of selected students (plotted)').'</th>'.
1.16      matthew   276:         '</tr>'.
                    277:         '</thead>'.
                    278:         '<tbody>';
1.17      matthew   279:     my @CumulativeCorrect=(0);
1.20    ! matthew   280:     my @corr_as_percent_of_selected;
        !           281:     my @corr_as_percent_of_answering;
1.17      matthew   282:     for (my $i=0;$i<=$#Ydata;$i++) {
                    283:         $CumulativeCorrect[$i]=$CumulativeCorrect[-1]+$AnsData[$i];
1.20    ! matthew   284:         $corr_as_percent_of_answering[$i] = 
        !           285:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/$numstudents);
        !           286:         $corr_as_percent_of_selected[$i] = 
        !           287:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/scalar(@$students));
1.16      matthew   288:         if ($Ydata[$i] != 0) {
                    289:             next if (! defined($BinEnd[$i]) || $BinEnd[$i] == 0);
                    290:             $htmltable .= 
                    291:                 '<tr>'.
1.17      matthew   292:                 '<td align="right"><nobr>'.
1.16      matthew   293:                 &Apache::lonlocal::locallocaltime($BinEnd[$i]-$binsize).
1.17      matthew   294:                 '</nobr></td>'.
1.16      matthew   295:                 '<td>&nbsp;</td>'.
1.17      matthew   296:                 '<td align="right"><nobr>'.
1.16      matthew   297:                     &Apache::lonlocal::locallocaltime($BinEnd[$i]).'</td>'.
1.17      matthew   298:                 '</nobr></td>'.
1.16      matthew   299:                 '<td align="right">'.$Ydata[$i].('&nbsp;'x3).'</td>'.
                    300:                 '<td>&nbsp;</td>'.
1.17      matthew   301:                 '<td align="right">'.$AnsData[$i].('&nbsp;'x3).'</td>'.
                    302:                 '<td>&nbsp;</td>'.
1.19      matthew   303:                 '<td align="right">'.$CumulativeCorrect[$i].'</td>'.
                    304:                 '<td>&nbsp;</td>'.
1.20    ! matthew   305:                 '<td align="right">'.$corr_as_percent_of_answering[$i].'</td>'.
        !           306:                 '<td>&nbsp;</td>'.
        !           307:                 '<td align="right">'.$corr_as_percent_of_selected[$i].'</td>'.
1.16      matthew   308:                 '</tr>'.$/;
                    309:         }
1.6       matthew   310:     }
1.16      matthew   311:     $htmltable .= '</tbody></table></p>';
1.17      matthew   312:     #
                    313:     # Build the plot
                    314:     my $title = '';#'Number of Submissions and Number Correct';
1.1       matthew   315:     my $xlabel;
1.2       matthew   316:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
                    317:     $xlabel .= $FullWeekDay[$wday].' '.
                    318:         join(' ',($Month[$month],$mday,1900+$year)).' - ';
                    319:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
                    320:     $xlabel .= $FullWeekDay[$wday].' '.
                    321:         join(' ',($Month[$month],$mday,1900+$year));
1.8       matthew   322:     my $width = 50+2*$bincount;
                    323:     if ($width < 250) {
                    324:         $width = 250;
                    325:     }
1.17      matthew   326:     #
1.1       matthew   327:     $html .= &Apache::loncommon::DrawXYYGraph($title,
                    328:                                               $xlabel,
1.6       matthew   329:                                               'Submissions vs Time',
1.1       matthew   330:                                               $plotcolors,
                    331:                                               \@Xlabel,
1.17      matthew   332:                                               \@Ydata,0,$max,
1.20    ! matthew   333:                                               \@corr_as_percent_of_selected,0,100,
1.1       matthew   334:                                               (xskip => $bins_per_day,
                    335:                                                x_ticks => $bins_per_day,
                    336:                                                x_tick_offset => $bins_per_day,
1.8       matthew   337:                                                width => $width,
1.7       matthew   338:                       y1_label=>'Number of Submissions per hour',
                    339:                       y2_label=>'Percent of Students answering Correctly',
                    340:                      'data.1.label'=>'Submissions per hour',
                    341:                      'data.2.label'=>'Percent correct',
                    342:                                                )
1.1       matthew   343:                                               );
1.16      matthew   344:     $html .= '<br />'.$htmltable;
1.1       matthew   345:     return $html;
                    346: }
                    347: 
                    348: sub successful_submission {
                    349:     my ($row) = @_;
                    350:     if (ref($row) eq 'ARRAY') {
1.3       matthew   351:         return $row->[&Apache::loncoursedata::RT_awarded()];
1.1       matthew   352:     }
                    353:     return undef;
                    354: }
                    355: 
                    356: sub get_time_from_row {
                    357:     my ($row) = @_;
                    358:     if (ref($row) eq 'ARRAY') {
1.3       matthew   359:         return $row->[&Apache::loncoursedata::RT_timestamp()];
1.1       matthew   360:     } 
                    361:     return undef;
                    362: }
                    363: 
                    364: sub get_tries_from_row {
                    365:     my ($row) = @_;
                    366:     if (ref($row) eq 'ARRAY') {
1.3       matthew   367:         return $row->[&Apache::loncoursedata::RT_tries()];
1.1       matthew   368:     }
                    369:     return undef;
                    370: }
                    371: 
                    372: sub Process_Row {
                    373:     my ($row) = @_;
                    374:     my %RowData;
                    375:     my ($student_id,$award,$tries,$time) = @$row;
                    376:     return %RowData;
                    377: }
                    378: 
                    379: #########################################################
                    380: #########################################################
                    381: ##
                    382: ##   Generic Interface Routines
                    383: ##
                    384: #########################################################
                    385: #########################################################
                    386: sub CreateInterface {
                    387:     ##
                    388:     ## Environment variable initialization
                    389:     if (! exists$ENV{'form.AnalyzeOver'}) {
                    390:         $ENV{'form.AnalyzeOver'} = 'Tries';
                    391:     }
                    392:     ##
                    393:     ## Build the menu
                    394:     my $Str = '';
1.14      matthew   395:     $Str .= &Apache::lonhtmlcommon::breadcrumbs(undef,'Submission Time Plots');
1.15      matthew   396:     $Str .= '<p>';
1.1       matthew   397:     $Str .= '<table cellspacing="5">'."\n";
                    398:     $Str .= '<tr>';
1.16      matthew   399:     $Str .= '<th align="center">'.&mt('Sections').'</th>';
                    400:     $Str .= '<th align="center">'.&mt('Enrollment Status').'</th>';
1.1       matthew   401:     $Str .= '</tr>'."\n";
                    402:     ##
                    403:     ## 
                    404:     $Str .= '<tr><td align="center">'."\n";
1.16      matthew   405:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',4);
1.1       matthew   406:     $Str .= '</td>';
                    407:     #
                    408:     $Str .= '<td align="center">';
1.16      matthew   409:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,4);
1.1       matthew   410:     $Str .= '</td>';
1.16      matthew   411:     #
1.15      matthew   412:     $Str .= '</tr>'."\n";
                    413:     $Str .= '</table>'."\n";
1.1       matthew   414:     #
1.15      matthew   415:     $Str .= '<nobr>'.&mt('Status: [_1]',
                    416:                          '<input type="text" '.
                    417:                          'name="stats_status" size="60" value="" />').
                    418:             '</nobr>'.'</p>';
1.1       matthew   419:     ##
                    420:     return $Str;
                    421: }
                    422: 
                    423: 1;
                    424: 
                    425: __END__

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