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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: #
1.22    ! matthew     3: # $Id: lonsubmissiontimeanalysis.pm,v 1.21 2005/03/10 17:06:21 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.22    ! matthew   130:             $r->print('<h4>'.
        !           131:                  &Apache::lonstatistics::section_and_enrollment_description().
        !           132:                       '</h4>');
1.1       matthew   133:             $r->rflush();
1.10      matthew   134:             $r->print(&Apache::lonstathelpers::render_resource($resource));
1.15      matthew   135:             $r->print('<br />');
1.1       matthew   136:             $r->rflush();
1.18      matthew   137:             $r->print(&analyze_times($r,$resource->symb,\@Students,
1.11      matthew   138:                                      $current_problem->{'part'}));
1.1       matthew   139:         }
                    140:         $r->print('<hr />');
                    141:     }
                    142: }
                    143: 
                    144: #########################################################
                    145: #########################################################
                    146: ##
                    147: ##                  Time Analysis
                    148: ##
                    149: #########################################################
                    150: #########################################################
                    151: sub get_week_start {
                    152:     my ($randomtime) = @_;
                    153:     my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
                    154:         localtime($randomtime);
                    155:     $randomtime -= $wday * 86400;
                    156:     ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = 
                    157:         localtime($randomtime);
                    158:     my $week_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
                    159:     return $week_start;
                    160: }
                    161: 
                    162: sub analyze_times {
1.18      matthew   163:     my ($r,$symb,$students,$part) = @_;
1.16      matthew   164:     my $htmltable;
1.2       matthew   165:     #
                    166:     # Convenience arrays
                    167:     my @FullWeekDay = (qw/Sunday Monday Tuesday Wednesday Thursday Friday
                    168:                        Saturday/);
                    169:     my @WeekDay = (qw/SUN MON TUE WED THU FRI SAT SUN/);
                    170:     my @Month = (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/);
                    171:     #
1.1       matthew   172:     my $html; # holds results of analysis
                    173:     # Get the data
                    174:     my $SubData = &Apache::loncoursedata::get_response_time_data
1.22    ! matthew   175:         ([&Apache::lonstatistics::get_selected_sections()],
1.18      matthew   176:          $Apache::lonstatistics::enrollment_status,
                    177:          $symb,$part);
1.1       matthew   178:     if (! defined($SubData) || ! ref($SubData)) {
1.18      matthew   179:         $html.= '<h2>There is no submission data for this problem at all</h2>';
1.1       matthew   180:         return $html;
                    181:     }
                    182:     my $NumSub = scalar(@{$SubData});
                    183:     if (! @{$SubData}) {
1.9       matthew   184:         $html.= '<h2>There is no submission data for this problem</h2>';
1.1       matthew   185:         return $html;
                    186:     }
                    187:     # Process the data
                    188:     #
                    189:     my (undef,undef,undef,$mday,$month,$year,$wday,$yday,$isdst) = 
                    190:         localtime(&get_time_from_row($SubData->[0]));
                    191:     my $day_start = &Time::Local::timelocal(0,0,0,$mday,$month,$year);
                    192:     #
1.2       matthew   193:     # Configure the bins used to store the data.
1.1       matthew   194:     my $binsize = 3600; # seconds
                    195:     my $bins_per_day = 86400/$binsize;
                    196:     my $bincount = 0;
                    197:     my $endtime = $day_start;
                    198:     #
1.2       matthew   199:     # Initialize loop variables
1.17      matthew   200:     my $max;            # The sum of @Ydata
                    201:     my @Ydata=(0);      # number of submissions
                    202:     my @AnsData=(0);    # number of correct submissions
                    203:     my @Xlabel=($WeekDay[$wday]); # Labels of itmes
                    204:     my @BinEnd;                   # The end time of each bin
                    205:     my $cumulative_answers = 0;   # The sum of @AnsData
1.19      matthew   206:     my %students;       # which students have attempted the problem?
1.2       matthew   207:     #
1.1       matthew   208:     foreach my $row (@$SubData) {
                    209:         my $subtime = &get_time_from_row($row);
                    210:         while ($subtime > $endtime && $endtime < time) {
                    211:             # Create a new bin
                    212:             $bincount++;
1.17      matthew   213:             $Ydata[$bincount]   = 0;
                    214:             $AnsData[$bincount] = 0;
1.1       matthew   215:             $endtime += $binsize;
1.16      matthew   216:             push(@BinEnd,$endtime);
1.1       matthew   217:             if ($bincount % (86400/$binsize) == 0) {
1.2       matthew   218:                 $wday++;
1.1       matthew   219:                 $wday %= 7;
1.2       matthew   220:                 $Xlabel[$bincount] = $WeekDay[$wday];
1.1       matthew   221:             } else {
                    222:                 $Xlabel[$bincount] = '';
                    223:             }
                    224:         }
                    225:         $Ydata[$bincount]++;
                    226:         $max = $Ydata[$bincount] if ($max < $Ydata[$bincount]);
                    227:         $AnsData[$bincount] += &successful_submission($row);
                    228:         $cumulative_answers += &successful_submission($row);
1.19      matthew   229:         $students{$row->[&Apache::loncoursedata::RT_student_id()]}++;
1.1       matthew   230:     }
1.17      matthew   231:     #
                    232:     # Pad the data to a full day
1.1       matthew   233:     while ($bincount % $bins_per_day != 0) {
                    234:         $bincount++;
                    235:         $Ydata[$bincount]=0;
1.17      matthew   236:         $AnsData[$bincount]=0;
1.1       matthew   237:         $endtime += $binsize;
1.17      matthew   238:         push(@BinEnd,$endtime);
1.1       matthew   239:         if ($bincount % (86400/$binsize) == 0) {
1.2       matthew   240:             $wday ++;
                    241:             $wday %= 7;
1.1       matthew   242:             $Xlabel[$bincount] = $WeekDay[$wday];
                    243:         } else {
                    244:             $Xlabel[$bincount] = '';
                    245:         }
                    246:     }
1.19      matthew   247:     my $numstudents = scalar(keys(%students));
1.17      matthew   248:     #
                    249:     # Determine a nice maximum value to use
                    250:     foreach my $maximum (10,15,20,25,30,40,50,60,70,80,90,100,
                    251:                           120,150,200,250,300,350,400,450,500,
                    252:                           600,700,800,900,1000,1100,1200,1500,2000,
                    253:                           2500,3000,4000,5000) {
                    254:         if ($max < $maximum) {
                    255:             $max = $maximum;
                    256:             last;
                    257:         }
                    258:     }
                    259:     #
                    260:     # Build the data table
1.16      matthew   261:     $htmltable = '<p>'.
                    262:         '<table rules="groups" frame="border" '.
                    263:         'summary="Student submission data">'.
                    264:         '<thead>'.
                    265:         '<tr>'.
1.17      matthew   266:         '<th valign="bottom">'.&mt('Begin').'</th>'.
1.16      matthew   267:         '<th>'.('&nbsp;'x3).'</th>'.
1.17      matthew   268:         '<th valign="bottom">'.&mt('End').'</th>'.
1.20      matthew   269:         '<th valign="bottom">'.&mt('Submissions (plotted)').'</th>'.
1.16      matthew   270:         '<th>'.('&nbsp;'x3).'</th>'.
1.20      matthew   271:         '<th valign="bottom">'.&mt('Correct Submissions (not plotted)').'</th>'.
1.16      matthew   272:         '<th>'.('&nbsp;'x3).'</th>'.
1.20      matthew   273:         '<th valign="bottom">'.&mt('Cumulative Correct of those attempting the problem (not plotted)').'</th>'.
1.19      matthew   274:         '<th>'.('&nbsp;'x3).'</th>'.
1.20      matthew   275:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of those attempting the problem (not plotted)').'</th>'.
                    276:         '<th>'.('&nbsp;'x3).'</th>'.
                    277:         '<th valign="bottom">'.&mt('Cumulative Percent Correct of selected students (plotted)').'</th>'.
1.16      matthew   278:         '</tr>'.
                    279:         '</thead>'.
                    280:         '<tbody>';
1.17      matthew   281:     my @CumulativeCorrect=(0);
1.20      matthew   282:     my @corr_as_percent_of_selected;
                    283:     my @corr_as_percent_of_answering;
1.17      matthew   284:     for (my $i=0;$i<=$#Ydata;$i++) {
                    285:         $CumulativeCorrect[$i]=$CumulativeCorrect[-1]+$AnsData[$i];
1.20      matthew   286:         $corr_as_percent_of_answering[$i] = 
                    287:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/$numstudents);
                    288:         $corr_as_percent_of_selected[$i] = 
                    289:             sprintf('%3.1f',100*$CumulativeCorrect[$i]/scalar(@$students));
1.16      matthew   290:         if ($Ydata[$i] != 0) {
                    291:             next if (! defined($BinEnd[$i]) || $BinEnd[$i] == 0);
                    292:             $htmltable .= 
                    293:                 '<tr>'.
1.17      matthew   294:                 '<td align="right"><nobr>'.
1.16      matthew   295:                 &Apache::lonlocal::locallocaltime($BinEnd[$i]-$binsize).
1.17      matthew   296:                 '</nobr></td>'.
1.16      matthew   297:                 '<td>&nbsp;</td>'.
1.17      matthew   298:                 '<td align="right"><nobr>'.
1.16      matthew   299:                     &Apache::lonlocal::locallocaltime($BinEnd[$i]).'</td>'.
1.17      matthew   300:                 '</nobr></td>'.
1.16      matthew   301:                 '<td align="right">'.$Ydata[$i].('&nbsp;'x3).'</td>'.
                    302:                 '<td>&nbsp;</td>'.
1.17      matthew   303:                 '<td align="right">'.$AnsData[$i].('&nbsp;'x3).'</td>'.
                    304:                 '<td>&nbsp;</td>'.
1.19      matthew   305:                 '<td align="right">'.$CumulativeCorrect[$i].'</td>'.
                    306:                 '<td>&nbsp;</td>'.
1.20      matthew   307:                 '<td align="right">'.$corr_as_percent_of_answering[$i].'</td>'.
                    308:                 '<td>&nbsp;</td>'.
                    309:                 '<td align="right">'.$corr_as_percent_of_selected[$i].'</td>'.
1.16      matthew   310:                 '</tr>'.$/;
                    311:         }
1.6       matthew   312:     }
1.16      matthew   313:     $htmltable .= '</tbody></table></p>';
1.17      matthew   314:     #
                    315:     # Build the plot
                    316:     my $title = '';#'Number of Submissions and Number Correct';
1.1       matthew   317:     my $xlabel;
1.2       matthew   318:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($day_start);
                    319:     $xlabel .= $FullWeekDay[$wday].' '.
                    320:         join(' ',($Month[$month],$mday,1900+$year)).' - ';
                    321:     (undef,undef,undef,$mday,$month,$year,$wday) = localtime($endtime);
                    322:     $xlabel .= $FullWeekDay[$wday].' '.
                    323:         join(' ',($Month[$month],$mday,1900+$year));
1.8       matthew   324:     my $width = 50+2*$bincount;
                    325:     if ($width < 250) {
                    326:         $width = 250;
                    327:     }
1.17      matthew   328:     #
1.1       matthew   329:     $html .= &Apache::loncommon::DrawXYYGraph($title,
                    330:                                               $xlabel,
1.6       matthew   331:                                               'Submissions vs Time',
1.1       matthew   332:                                               $plotcolors,
                    333:                                               \@Xlabel,
1.17      matthew   334:                                               \@Ydata,0,$max,
1.20      matthew   335:                                               \@corr_as_percent_of_selected,0,100,
1.1       matthew   336:                                               (xskip => $bins_per_day,
                    337:                                                x_ticks => $bins_per_day,
                    338:                                                x_tick_offset => $bins_per_day,
1.8       matthew   339:                                                width => $width,
1.7       matthew   340:                       y1_label=>'Number of Submissions per hour',
                    341:                       y2_label=>'Percent of Students answering Correctly',
                    342:                      'data.1.label'=>'Submissions per hour',
                    343:                      'data.2.label'=>'Percent correct',
                    344:                                                )
1.1       matthew   345:                                               );
1.16      matthew   346:     $html .= '<br />'.$htmltable;
1.1       matthew   347:     return $html;
                    348: }
                    349: 
                    350: sub successful_submission {
                    351:     my ($row) = @_;
                    352:     if (ref($row) eq 'ARRAY') {
1.3       matthew   353:         return $row->[&Apache::loncoursedata::RT_awarded()];
1.1       matthew   354:     }
                    355:     return undef;
                    356: }
                    357: 
                    358: sub get_time_from_row {
                    359:     my ($row) = @_;
                    360:     if (ref($row) eq 'ARRAY') {
1.3       matthew   361:         return $row->[&Apache::loncoursedata::RT_timestamp()];
1.1       matthew   362:     } 
                    363:     return undef;
                    364: }
                    365: 
                    366: sub get_tries_from_row {
                    367:     my ($row) = @_;
                    368:     if (ref($row) eq 'ARRAY') {
1.3       matthew   369:         return $row->[&Apache::loncoursedata::RT_tries()];
1.1       matthew   370:     }
                    371:     return undef;
                    372: }
                    373: 
                    374: sub Process_Row {
                    375:     my ($row) = @_;
                    376:     my %RowData;
                    377:     my ($student_id,$award,$tries,$time) = @$row;
                    378:     return %RowData;
                    379: }
                    380: 
                    381: #########################################################
                    382: #########################################################
                    383: ##
                    384: ##   Generic Interface Routines
                    385: ##
                    386: #########################################################
                    387: #########################################################
                    388: sub CreateInterface {
                    389:     ##
                    390:     ## Environment variable initialization
                    391:     if (! exists$ENV{'form.AnalyzeOver'}) {
                    392:         $ENV{'form.AnalyzeOver'} = 'Tries';
                    393:     }
                    394:     ##
                    395:     ## Build the menu
                    396:     my $Str = '';
1.14      matthew   397:     $Str .= &Apache::lonhtmlcommon::breadcrumbs(undef,'Submission Time Plots');
1.15      matthew   398:     $Str .= '<p>';
1.1       matthew   399:     $Str .= '<table cellspacing="5">'."\n";
                    400:     $Str .= '<tr>';
1.16      matthew   401:     $Str .= '<th align="center">'.&mt('Sections').'</th>';
                    402:     $Str .= '<th align="center">'.&mt('Enrollment Status').'</th>';
1.1       matthew   403:     $Str .= '</tr>'."\n";
                    404:     ##
                    405:     ## 
                    406:     $Str .= '<tr><td align="center">'."\n";
1.16      matthew   407:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',4);
1.1       matthew   408:     $Str .= '</td>';
                    409:     #
                    410:     $Str .= '<td align="center">';
1.16      matthew   411:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,4);
1.1       matthew   412:     $Str .= '</td>';
1.16      matthew   413:     #
1.15      matthew   414:     $Str .= '</tr>'."\n";
                    415:     $Str .= '</table>'."\n";
1.1       matthew   416:     #
1.15      matthew   417:     $Str .= '<nobr>'.&mt('Status: [_1]',
                    418:                          '<input type="text" '.
                    419:                          'name="stats_status" size="60" value="" />').
                    420:             '</nobr>'.'</p>';
1.1       matthew   421:     ##
                    422:     return $Str;
                    423: }
                    424: 
                    425: 1;
                    426: 
                    427: __END__

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