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

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

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